From b424a830624b61df969b21d5e23cbc98a4d8d7ca Mon Sep 17 00:00:00 2001 From: Paldiu Date: Sat, 16 Apr 2022 17:27:54 -0500 Subject: [PATCH] Beta 20220416-SNAPSHOT Changelog: - Changed the way classes interact with the PlayerHandler by using a getter instead of the actual field. - Added #isTool(Material) to ItemBuilder to check if an item is, in fact, a type of tool. - Created the UnbreakableTool listener which will grant the unbreakable status to a tool if a user is lucky enough. NOTE: This is a beta feature, and while it may remain included the way it works will most likely change. --- .../io/github/simplex/lib/ItemBuilder.java | 13 ++++++ .../io/github/simplex/lib/MiniComponent.java | 8 ++-- .../io/github/simplex/luck/FeelingLucky.java | 10 +++-- .../simplex/luck/listener/BlockDrops.java | 2 +- .../luck/listener/BonemealFullCrop.java | 3 +- .../simplex/luck/listener/CheatDeath.java | 2 +- .../luck/listener/EnchantmentBoost.java | 2 +- .../simplex/luck/listener/ExpBoost.java | 2 +- .../simplex/luck/listener/ItemDrops.java | 3 +- .../simplex/luck/listener/PlayerListener.java | 8 ++-- .../simplex/luck/listener/RestoreHunger.java | 4 +- .../simplex/luck/listener/TakeDamage.java | 4 +- .../luck/listener/UnbreakableTool.java | 40 +++++++++++++++++++ .../simplex/luck/player/PlayerConfig.java | 2 +- .../io/github/simplex/luck/util/LuckCMD.java | 24 +++++------ 15 files changed, 91 insertions(+), 36 deletions(-) create mode 100644 src/main/java/io/github/simplex/luck/listener/UnbreakableTool.java diff --git a/src/main/java/io/github/simplex/lib/ItemBuilder.java b/src/main/java/io/github/simplex/lib/ItemBuilder.java index 8e3a3f0..2507755 100644 --- a/src/main/java/io/github/simplex/lib/ItemBuilder.java +++ b/src/main/java/io/github/simplex/lib/ItemBuilder.java @@ -26,6 +26,19 @@ public final class ItemBuilder { return new ItemBuilder(material); } + @Contract(pure = true) + public static boolean isTool(Material m) { + String name = m.getKey().getKey(); + + return m.equals(Material.SHEARS) + || name.endsWith("bow") + || name.endsWith("pickaxe") + || name.endsWith("axe") + || name.endsWith("sword") + || name.endsWith("shovel") + || name.endsWith("hoe"); + } + public ItemBuilder setName(String name) { meta.displayName(MiniComponent.of(name).send()); return this; diff --git a/src/main/java/io/github/simplex/lib/MiniComponent.java b/src/main/java/io/github/simplex/lib/MiniComponent.java index 917d901..d5db9d6 100644 --- a/src/main/java/io/github/simplex/lib/MiniComponent.java +++ b/src/main/java/io/github/simplex/lib/MiniComponent.java @@ -11,6 +11,10 @@ public class MiniComponent { private TextDecoration decoration = null; private TextColor color = null; + public MiniComponent(String content) { + this.content = content; + } + @Contract("_ -> new") public static MiniComponent of(String content) { return new MiniComponent(content); @@ -31,10 +35,6 @@ public class MiniComponent { return new MiniComponent(content).color(ChatColor.RED).decorate(TextDecoration.BOLD).send(); } - public MiniComponent(String content) { - this.content = content; - } - public MiniComponent decorate(TextDecoration decoration) { this.decoration = decoration; return this; diff --git a/src/main/java/io/github/simplex/luck/FeelingLucky.java b/src/main/java/io/github/simplex/luck/FeelingLucky.java index 2c2fc1a..c366a5f 100644 --- a/src/main/java/io/github/simplex/luck/FeelingLucky.java +++ b/src/main/java/io/github/simplex/luck/FeelingLucky.java @@ -16,8 +16,7 @@ import java.util.UUID; public final class FeelingLucky extends JavaPlugin { private final Map configMap = new HashMap<>(); - public LuckCMD cmd; - public PlayerHandler handler; + private PlayerHandler handler; public Map getConfigMap() { return configMap; @@ -32,7 +31,7 @@ public final class FeelingLucky extends JavaPlugin { getLogger().info("Registration complete! Attempting to load all player configuration files..."); loadConfigurations(); Bukkit.getLogger().info("Attempting to load the Luck command..."); - cmd = new LuckCMD(this); + new LuckCMD(this); Bukkit.getLogger().info("Successfully loaded the Luck command!"); Bukkit.getLogger().info("Successfully initialized!"); @@ -68,5 +67,10 @@ public final class FeelingLucky extends JavaPlugin { new ExpBoost(this); new CheatDeath(this); new BonemealFullCrop(this); + new UnbreakableTool(this); + } + + public PlayerHandler getHandler() { + return handler; } } diff --git a/src/main/java/io/github/simplex/luck/listener/BlockDrops.java b/src/main/java/io/github/simplex/luck/listener/BlockDrops.java index 12268a0..1267b44 100644 --- a/src/main/java/io/github/simplex/luck/listener/BlockDrops.java +++ b/src/main/java/io/github/simplex/luck/listener/BlockDrops.java @@ -19,7 +19,7 @@ public record BlockDrops(FeelingLucky plugin) implements Listener { @EventHandler public void extraBlockDrops(BlockDropItemEvent event) { Player player = event.getPlayer(); - Luck luck = plugin.handler.getLuckContainer(player); + Luck luck = plugin.getHandler().getLuckContainer(player); List items = event.getItems(); if (luck.quickRNG(luck.getPercentage())) { items.forEach(SneakyWorker::move); diff --git a/src/main/java/io/github/simplex/luck/listener/BonemealFullCrop.java b/src/main/java/io/github/simplex/luck/listener/BonemealFullCrop.java index b7f07ff..d62430f 100644 --- a/src/main/java/io/github/simplex/luck/listener/BonemealFullCrop.java +++ b/src/main/java/io/github/simplex/luck/listener/BonemealFullCrop.java @@ -6,7 +6,6 @@ import io.github.simplex.luck.FeelingLucky; import io.github.simplex.luck.player.Luck; import org.bukkit.Material; import org.bukkit.block.Block; -import org.bukkit.block.BlockFace; import org.bukkit.block.data.Ageable; import org.bukkit.block.data.BlockData; import org.bukkit.entity.Player; @@ -26,7 +25,7 @@ public record BonemealFullCrop(FeelingLucky plugin) implements Listener { Player player = event.getPlayer(); Action action = event.getAction(); ItemStack bonemeal = ItemBuilder.of(Material.BONE_MEAL).build(); - Luck luck = plugin.handler.getLuckContainer(player); + Luck luck = plugin.getHandler().getLuckContainer(player); ItemStack handItem = event.getItem(); if (handItem == null) return; diff --git a/src/main/java/io/github/simplex/luck/listener/CheatDeath.java b/src/main/java/io/github/simplex/luck/listener/CheatDeath.java index 071b3ff..17b3083 100644 --- a/src/main/java/io/github/simplex/luck/listener/CheatDeath.java +++ b/src/main/java/io/github/simplex/luck/listener/CheatDeath.java @@ -17,7 +17,7 @@ public record CheatDeath(FeelingLucky plugin) implements Listener { @EventHandler public void cheatDeath(PlayerDeathEvent event) { Player player = event.getPlayer(); - Luck luck = plugin.handler.getLuckContainer(player); + Luck luck = plugin.getHandler().getLuckContainer(player); double absorption = Math.round(Luck.RNG().nextDouble(5.0, 10.0)); if (luck.quickRNG(luck.getPercentage())) { event.setCancelled(true); diff --git a/src/main/java/io/github/simplex/luck/listener/EnchantmentBoost.java b/src/main/java/io/github/simplex/luck/listener/EnchantmentBoost.java index c5fd936..09c0e77 100644 --- a/src/main/java/io/github/simplex/luck/listener/EnchantmentBoost.java +++ b/src/main/java/io/github/simplex/luck/listener/EnchantmentBoost.java @@ -22,7 +22,7 @@ public record EnchantmentBoost(FeelingLucky plugin) implements Listener { Map enchMap = event.getEnchantsToAdd(); List enchList = enchMap.keySet().stream().toList(); Player player = event.getEnchanter(); - Luck luck = plugin.handler.getLuckContainer(player); + Luck luck = plugin.getHandler().getLuckContainer(player); if (luck.quickRNG(luck.getPercentage())) { Enchantment particular = enchList.get(Luck.RNG().nextInt(enchList.size())); int rng = Luck.RNG().nextInt(1, 5); diff --git a/src/main/java/io/github/simplex/luck/listener/ExpBoost.java b/src/main/java/io/github/simplex/luck/listener/ExpBoost.java index 6fc8c40..3679c76 100644 --- a/src/main/java/io/github/simplex/luck/listener/ExpBoost.java +++ b/src/main/java/io/github/simplex/luck/listener/ExpBoost.java @@ -21,7 +21,7 @@ public record ExpBoost(FeelingLucky plugin) implements Listener { int math = (5 * n ^ 2) / (2 * n + 4); int rounded = Math.round(math); Player player = event.getPlayer(); - Luck luck = plugin.handler.getLuckContainer(player); + Luck luck = plugin.getHandler().getLuckContainer(player); if (luck.quickRNG(luck.getPercentage())) { orb.setExperience(rounded); } diff --git a/src/main/java/io/github/simplex/luck/listener/ItemDrops.java b/src/main/java/io/github/simplex/luck/listener/ItemDrops.java index 3c7924d..f3bf388 100644 --- a/src/main/java/io/github/simplex/luck/listener/ItemDrops.java +++ b/src/main/java/io/github/simplex/luck/listener/ItemDrops.java @@ -2,7 +2,6 @@ package io.github.simplex.luck.listener; import io.github.simplex.luck.FeelingLucky; import io.github.simplex.luck.player.Luck; -import org.bukkit.Bukkit; import org.bukkit.Location; import org.bukkit.Material; import org.bukkit.World; @@ -59,7 +58,7 @@ public class ItemDrops implements Listener { if (entityPlayerMap.get(entity.getUniqueId()) == null) return; Player player = entityPlayerMap.get(entity.getUniqueId()); - Luck luck = plugin.handler.getLuckContainer(player); + Luck luck = plugin.getHandler().getLuckContainer(player); Item item = event.getItemDrop(); ItemStack stack = item.getItemStack(); int amount = stack.getAmount(); diff --git a/src/main/java/io/github/simplex/luck/listener/PlayerListener.java b/src/main/java/io/github/simplex/luck/listener/PlayerListener.java index 8ba28aa..22ae990 100644 --- a/src/main/java/io/github/simplex/luck/listener/PlayerListener.java +++ b/src/main/java/io/github/simplex/luck/listener/PlayerListener.java @@ -29,7 +29,7 @@ public record PlayerListener(FeelingLucky plugin) implements Listener { ItemStack foot = new ItemStack(Material.RABBIT_FOOT); SpecialFootItem special = new SpecialFootItem(); Player player = event.getPlayer(); - Luck luck = plugin.handler.getLuckContainer(player); + Luck luck = plugin.getHandler().getLuckContainer(player); if (action.isRightClick() && player.getInventory().getItemInMainHand().isSimilar(foot)) { if (foot.getItemMeta().equals(special.meta()) || foot.equals(special.get())) { luck.setMultiplier(luck.multiplier() + 1); @@ -37,7 +37,7 @@ public record PlayerListener(FeelingLucky plugin) implements Listener { double rng = Luck.RNG().nextDouble(2.0, 5.0); player.getInventory().remove(player.getInventory().getItemInMainHand()); luck.addTo(rng); - plugin.handler.updatePlayer(player, luck); + plugin.getHandler().updatePlayer(player, luck); player.sendMessage(Component.empty().content("Your luck has been increased by " + rng + " points.")); } } @@ -56,11 +56,11 @@ public record PlayerListener(FeelingLucky plugin) implements Listener { return; } - Luck luck = plugin.handler.getLuckContainer(player); + Luck luck = plugin.getHandler().getLuckContainer(player); if (cause.equals(EntityDamageEvent.DamageCause.MAGIC) || cause.equals(EntityDamageEvent.DamageCause.POISON)) { if (luck.quickRNG(33.0)) { luck.takeFrom(5.0); - plugin.handler.updatePlayer(player, luck); + plugin.getHandler().updatePlayer(player, luck); } } } diff --git a/src/main/java/io/github/simplex/luck/listener/RestoreHunger.java b/src/main/java/io/github/simplex/luck/listener/RestoreHunger.java index 4d9d7d9..7f03ec6 100644 --- a/src/main/java/io/github/simplex/luck/listener/RestoreHunger.java +++ b/src/main/java/io/github/simplex/luck/listener/RestoreHunger.java @@ -2,8 +2,8 @@ package io.github.simplex.luck.listener; import io.github.simplex.lib.PotionEffectBuilder; import io.github.simplex.luck.FeelingLucky; -import io.github.simplex.luck.util.ListBox; import io.github.simplex.luck.player.Luck; +import io.github.simplex.luck.util.ListBox; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; import org.bukkit.event.player.PlayerItemConsumeEvent; @@ -19,7 +19,7 @@ public record RestoreHunger(FeelingLucky plugin) implements Listener { @EventHandler public void restoreHunger(PlayerItemConsumeEvent event) { ItemStack item = event.getItem(); - Luck luck = plugin.handler.getLuckContainer(event.getPlayer()); + Luck luck = plugin.getHandler().getLuckContainer(event.getPlayer()); PotionEffect effect = PotionEffectBuilder.newEffect().type(PotionEffectType.SATURATION).amplifier(2).duration(10).particles(false).create(); if (luck.notDefault()) { double percentage = luck.getPercentage(); diff --git a/src/main/java/io/github/simplex/luck/listener/TakeDamage.java b/src/main/java/io/github/simplex/luck/listener/TakeDamage.java index 92cd9c6..12e3c09 100644 --- a/src/main/java/io/github/simplex/luck/listener/TakeDamage.java +++ b/src/main/java/io/github/simplex/luck/listener/TakeDamage.java @@ -2,8 +2,8 @@ package io.github.simplex.luck.listener; import io.github.simplex.lib.PotionEffectBuilder; import io.github.simplex.luck.FeelingLucky; -import io.github.simplex.luck.util.ListBox; import io.github.simplex.luck.player.Luck; +import io.github.simplex.luck.util.ListBox; import net.kyori.adventure.text.Component; import org.bukkit.entity.Entity; import org.bukkit.entity.Player; @@ -23,7 +23,7 @@ public record TakeDamage(FeelingLucky plugin) implements Listener { return; } Player player = (Player) event.getEntity(); - Luck luck = plugin.handler.getLuckContainer(player); + Luck luck = plugin.getHandler().getLuckContainer(player); if (ListBox.acceptedCauses.contains(event.getCause())) { if (luck.notDefault()) { double percentage = luck.getPercentage(); diff --git a/src/main/java/io/github/simplex/luck/listener/UnbreakableTool.java b/src/main/java/io/github/simplex/luck/listener/UnbreakableTool.java new file mode 100644 index 0000000..12ad1a0 --- /dev/null +++ b/src/main/java/io/github/simplex/luck/listener/UnbreakableTool.java @@ -0,0 +1,40 @@ +package io.github.simplex.luck.listener; + +import io.github.simplex.lib.ItemBuilder; +import io.github.simplex.lib.MiniComponent; +import io.github.simplex.luck.FeelingLucky; +import io.github.simplex.luck.player.Luck; +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.inventory.CraftItemEvent; +import org.bukkit.inventory.CraftingInventory; +import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.meta.ItemMeta; + +public record UnbreakableTool(FeelingLucky plugin) implements Listener { + public UnbreakableTool { + plugin.getServer().getPluginManager().registerEvents(this, plugin); + } + + @EventHandler + public void unbreakableTool(CraftItemEvent event) { + CraftingInventory inventory = event.getInventory(); + ItemStack stack = inventory.getResult(); + + if (stack == null) return; + ItemMeta meta = stack.getItemMeta(); + + if (ItemBuilder.isTool(stack.getType())) { + if (event.getWhoClicked() instanceof Player player) { + Luck luck = plugin.getHandler().getLuckContainer(player); + if (luck.quickRNG(luck.getPercentage())) { + meta.setUnbreakable(true); + stack.setItemMeta(meta); + inventory.setResult(stack); + player.sendMessage(MiniComponent.info("By the grace of Luck you have crafted an unbreakable tool!")); + } + } + } + } +} diff --git a/src/main/java/io/github/simplex/luck/player/PlayerConfig.java b/src/main/java/io/github/simplex/luck/player/PlayerConfig.java index b200876..76d660d 100644 --- a/src/main/java/io/github/simplex/luck/player/PlayerConfig.java +++ b/src/main/java/io/github/simplex/luck/player/PlayerConfig.java @@ -47,7 +47,7 @@ public class PlayerConfig { if (tempUsername == null) { config.set("username", player.getName()); - config.set("luck", plugin.handler.getLuckContainer(player).getDefaultValue()); + config.set("luck", plugin.getHandler().getLuckContainer(player).getDefaultValue()); config.set("multiplier", "1.0"); save(); } diff --git a/src/main/java/io/github/simplex/luck/util/LuckCMD.java b/src/main/java/io/github/simplex/luck/util/LuckCMD.java index 16cee88..e68842f 100644 --- a/src/main/java/io/github/simplex/luck/util/LuckCMD.java +++ b/src/main/java/io/github/simplex/luck/util/LuckCMD.java @@ -5,11 +5,11 @@ import io.github.simplex.lib.MiniComponent; import io.github.simplex.luck.FeelingLucky; import io.github.simplex.luck.player.Luck; import io.github.simplex.luck.player.PlayerConfig; -import net.kyori.adventure.text.Component; -import net.kyori.adventure.text.TextComponent; -import net.kyori.adventure.text.format.TextColor; import org.bukkit.Bukkit; -import org.bukkit.command.*; +import org.bukkit.command.Command; +import org.bukkit.command.CommandSender; +import org.bukkit.command.ConsoleCommandSender; +import org.bukkit.command.TabCompleter; import org.bukkit.entity.Player; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -46,27 +46,27 @@ public class LuckCMD extends Command implements TabCompleter { return true; } - Luck luck = plugin.handler.getLuckContainer(player); + Luck luck = plugin.getHandler().getLuckContainer(player); PlayerConfig config = plugin.getConfigMap().get(player.getUniqueId()); switch (args[0]) { case "set" -> { luck.setValue(amount); - plugin.handler.updatePlayer(player, luck); + plugin.getHandler().updatePlayer(player, luck); config.setLuck(luck.getValue()); sender.sendMessage(MiniComponent.info("Successfully reset " + args[1] + "'s Luck stat.")); return true; } case "give" -> { luck.addTo(amount); - plugin.handler.updatePlayer(player, luck); + plugin.getHandler().updatePlayer(player, luck); config.setLuck(luck.getValue()); sender.sendMessage(MiniComponent.info("Successfully reset " + args[1] + "'s Luck stat.")); return true; } case "take" -> { luck.takeFrom(amount); - plugin.handler.updatePlayer(player, luck); + plugin.getHandler().updatePlayer(player, luck); config.setLuck(luck.getValue()); sender.sendMessage(MiniComponent.info("Successfully reset " + args[1] + "'s Luck stat.")); return true; @@ -88,7 +88,7 @@ public class LuckCMD extends Command implements TabCompleter { return true; } - Luck luck = plugin.handler.getLuckContainer(player); + Luck luck = plugin.getHandler().getLuckContainer(player); sender.sendMessage(MiniComponent.info("Luck stat for " + args[1] + ": " + luck.getValue())); return true; } @@ -101,10 +101,10 @@ public class LuckCMD extends Command implements TabCompleter { return true; } - Luck luck = plugin.handler.getLuckContainer(player); + Luck luck = plugin.getHandler().getLuckContainer(player); PlayerConfig config = plugin.getConfigMap().get(player.getUniqueId()); luck.reset(); - plugin.handler.updatePlayer(player, luck); + plugin.getHandler().updatePlayer(player, luck); config.setLuck(luck.getValue()); sender.sendMessage(MiniComponent.info("Successfully reset " + args[1] + "'s Luck stat.")); return true; @@ -124,7 +124,7 @@ public class LuckCMD extends Command implements TabCompleter { if ((sender instanceof Player player) && player.hasPermission("luck.default")) { if (args[0].equalsIgnoreCase("info")) { - Luck luck = plugin.handler.getLuckContainer(player); + Luck luck = plugin.getHandler().getLuckContainer(player); player.sendMessage(MiniComponent.info("Your Luck: " + luck.getPercentage())); return true; }