Added rarity option to /giverandombook

This commit is contained in:
Auxilor
2021-05-20 21:34:47 +01:00
parent 77f542c4b1
commit 22b6e79afc
2 changed files with 86 additions and 2 deletions

View File

@@ -4,9 +4,10 @@ import com.willfp.eco.core.EcoPlugin;
import com.willfp.eco.core.command.AbstractCommand;
import com.willfp.eco.core.command.AbstractTabCompleter;
import com.willfp.eco.util.NumberUtils;
import com.willfp.ecoenchants.command.tabcompleters.TabCompleterRandomEnchant;
import com.willfp.ecoenchants.command.tabcompleters.TabCompleterGiverandombook;
import com.willfp.ecoenchants.display.EnchantmentCache;
import com.willfp.ecoenchants.enchantments.EcoEnchant;
import com.willfp.ecoenchants.enchantments.meta.EnchantmentRarity;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.command.CommandSender;
@@ -32,7 +33,7 @@ public class CommandGiverandombook extends AbstractCommand {
@Override
public AbstractTabCompleter getTab() {
return new TabCompleterRandomEnchant(this);
return new TabCompleterGiverandombook(this);
}
@Override
@@ -44,6 +45,8 @@ public class CommandGiverandombook extends AbstractCommand {
}
Player player = Bukkit.getServer().getPlayer(args.get(0));
EnchantmentRarity rarity = args.size() >= 2 ? EnchantmentRarity.getByName(args.get(1).toLowerCase()) : null;
if (player == null) {
sender.sendMessage(this.getPlugin().getLangYml().getMessage("invalid-player"));
return;
@@ -51,11 +54,20 @@ public class CommandGiverandombook extends AbstractCommand {
ItemStack itemStack = new ItemStack(Material.ENCHANTED_BOOK);
EnchantmentStorageMeta meta = (EnchantmentStorageMeta) itemStack.getItemMeta();
List<Enchantment> allowed = Arrays.stream(Enchantment.values()).filter(enchantment -> {
if (enchantment instanceof EcoEnchant) {
return ((EcoEnchant) enchantment).isEnabled();
}
return true;
}).filter(enchantment -> {
if (rarity != null) {
if (!(enchantment instanceof EcoEnchant)) {
return false;
}
return ((EcoEnchant) enchantment).getRarity().equals(rarity);
}
return true;
}).collect(Collectors.toList());
Enchantment enchantment = allowed.get(NumberUtils.randInt(0, allowed.size() - 1));
int level = NumberUtils.randInt(1, enchantment.getMaxLevel());

View File

@@ -0,0 +1,72 @@
package com.willfp.ecoenchants.command.tabcompleters;
import com.willfp.eco.core.command.AbstractCommand;
import com.willfp.eco.core.command.AbstractTabCompleter;
import com.willfp.eco.core.config.ConfigUpdater;
import com.willfp.ecoenchants.enchantments.meta.EnchantmentRarity;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.util.StringUtil;
import org.jetbrains.annotations.NotNull;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
public class TabCompleterGiverandombook extends AbstractTabCompleter {
/**
* The cached enchantment names.
*/
private static final List<String> RARITY_NAMES = EnchantmentRarity.values().stream().map(EnchantmentRarity::getName).collect(Collectors.toList());
/**
* Instantiate a new tab-completer for /enchantinfo.
*
* @param command /enchantinfo.
*/
public TabCompleterGiverandombook(@NotNull final AbstractCommand command) {
super(command);
}
/**
* Called on /ecoreload.
*/
@ConfigUpdater
public static void reload() {
RARITY_NAMES.clear();
RARITY_NAMES.addAll(EnchantmentRarity.values().stream().map(EnchantmentRarity::getName).collect(Collectors.toList()));
}
/**
* The execution of the tabcompleter.
*
* @param sender The sender of the command.
* @param args The arguments of the command.
* @return A list of tab-completions.
*/
@Override
public List<String> onTab(@NotNull final CommandSender sender,
@NotNull final List<String> args) {
List<String> completions = new ArrayList<>();
List<String> playerNames = Bukkit.getOnlinePlayers().stream().map(Player::getName).collect(Collectors.toList());
if (args.isEmpty()) {
// Currently, this case is not ever reached
return playerNames;
}
if (args.size() == 1) {
StringUtil.copyPartialMatches(String.join(" ", args.get(0)), playerNames, completions);
}
if (args.size() == 2) {
StringUtil.copyPartialMatches(String.join(" ", args.get(1)), RARITY_NAMES, completions);
}
Collections.sort(completions);
return completions;
}
}