Merge pull request #65

New /ecoenchants giverandombook argument (discord suggestion)
This commit is contained in:
Will FP 2021-09-19 11:20:54 +01:00 committed by GitHub
commit 5b32004d66
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 29 additions and 2 deletions

View File

@ -10,6 +10,7 @@ import com.willfp.eco.util.NumberUtils;
import com.willfp.ecoenchants.display.EnchantmentCache;
import com.willfp.ecoenchants.enchantments.EcoEnchant;
import com.willfp.ecoenchants.enchantments.meta.EnchantmentRarity;
import com.willfp.ecoenchants.enchantments.meta.EnchantmentType;
import org.bukkit.Bukkit;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.Player;
@ -28,6 +29,7 @@ public class CommandGiverandombook extends Subcommand {
* The cached enchantment names.
*/
private static final List<String> RARITY_NAMES = EnchantmentRarity.values().stream().map(EnchantmentRarity::getName).collect(Collectors.toList());
private static final List<String> TYPE_NAMES = EnchantmentType.values().stream().map(EnchantmentType::getName).collect(Collectors.toList());
/**
* Instantiate a new command handler.
@ -58,6 +60,9 @@ public class CommandGiverandombook extends Subcommand {
EnchantmentRarity rarity = args.size() >= 2 ? EnchantmentRarity.getByName(args.get(1).toLowerCase()) : null;
EnchantmentType type = rarity == null && args.size() >= 2 ? EnchantmentType.getByName(args.get(1).toLowerCase()) : null;
if (player == null) {
sender.sendMessage(this.getPlugin().getLangYml().getMessage("invalid-player"));
return;
@ -70,10 +75,16 @@ public class CommandGiverandombook extends Subcommand {
return true;
}).filter(enchantment -> {
if (rarity != null) {
if (!(enchantment instanceof EcoEnchant)) {
if (!(enchantment instanceof EcoEnchant ecoEnchant)) {
return false;
}
return ((EcoEnchant) enchantment).getEnchantmentRarity().equals(rarity);
return ecoEnchant.getEnchantmentRarity().equals(rarity);
}
else if (type != null) {
if (!(enchantment instanceof EcoEnchant ecoEnchant)) {
return false;
}
return ecoEnchant.getType().equals(type);
}
return true;
}).collect(Collectors.toList());
@ -117,6 +128,7 @@ public class CommandGiverandombook extends Subcommand {
if (args.size() == 2) {
StringUtil.copyPartialMatches(String.join(" ", args.get(1)), RARITY_NAMES, completions);
StringUtil.copyPartialMatches(String.join(" ", args.get(1)), TYPE_NAMES, completions);
}
Collections.sort(completions);

View File

@ -69,6 +69,9 @@ public class InfernalTouch extends EcoEnchant {
@EventHandler
public void infernalTouchBreak(@NotNull final BlockDropItemEvent event) {
System.out.println("Drop event");
Player player = event.getPlayer();
Block block = event.getBlock();

View File

@ -13,6 +13,7 @@ import org.jetbrains.annotations.Nullable;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Supplier;
public class EnchantmentType {
@ -201,6 +202,17 @@ public class EnchantmentType {
REGISTERED.add(this);
}
/**
* Get EnchantmentType matching name.
*
* @param name The name to search for.
* @return The matching EnchantmentType, or null if not found.
*/
public static EnchantmentType getByName(@NotNull final String name) {
Optional<EnchantmentType> matching = REGISTERED.stream().filter(enchantmentType -> enchantmentType.getName().equalsIgnoreCase(name)).findFirst();
return matching.orElse(null);
}
/**
* Update suppliers of all types.
*/