From 6eabebd905aeb9d076649ea245fa8eaac84e1ffc Mon Sep 17 00:00:00 2001 From: ElementalMP4 Date: Fri, 15 Mar 2024 15:56:38 +0000 Subject: [PATCH] Added Billy --- src/main/java/elementalmp4/GlobalConfig.java | 3 +- src/main/java/elementalmp4/SebUtils.java | 3 + .../command/admin/EnableBillyCommand.java | 34 ++++++++++ .../listener/VillagerInteractionListener.java | 24 +++++++ .../elementalmp4/service/MerchantService.java | 65 +++++++++++++++++++ src/plugin.yml | 4 ++ 6 files changed, 132 insertions(+), 1 deletion(-) create mode 100644 src/main/java/elementalmp4/command/admin/EnableBillyCommand.java create mode 100644 src/main/java/elementalmp4/listener/VillagerInteractionListener.java create mode 100644 src/main/java/elementalmp4/service/MerchantService.java diff --git a/src/main/java/elementalmp4/GlobalConfig.java b/src/main/java/elementalmp4/GlobalConfig.java index e2a64f2..39d35d3 100644 --- a/src/main/java/elementalmp4/GlobalConfig.java +++ b/src/main/java/elementalmp4/GlobalConfig.java @@ -2,9 +2,10 @@ public enum GlobalConfig { - TNT_EXPLODES("tnt_explodes", "true"), + TNT_EXPLODES("tnt_enabled", "true"), COWS_EXPLODE("cows_explode", "false"), SHEEP_SMITE("sheep_smite", "false"), + BILLY_ENABLED("billy_enabled", "false"), PLOT_MAX_SIZE("plot_max_size", "10000"); private final String key; diff --git a/src/main/java/elementalmp4/SebUtils.java b/src/main/java/elementalmp4/SebUtils.java index be4d462..4e57f37 100644 --- a/src/main/java/elementalmp4/SebUtils.java +++ b/src/main/java/elementalmp4/SebUtils.java @@ -81,6 +81,7 @@ public void onEnable() { getCommand("revokepermit").setExecutor(new RevokePermitCommand()); getCommand("permits").setExecutor(new PermitsCommand()); getCommand("smite").setExecutor(new SmiteCommand()); + getCommand("enablebilly").setExecutor(new EnableBillyCommand()); logger.info(ConsoleColours.YELLOW + "Registering autofill"); getCommand("namecolour").setTabCompleter(new ColourTabCompleter()); @@ -93,6 +94,7 @@ public void onEnable() { getCommand("revokepermit").setTabCompleter(new PermitCompleter()); getCommand("permits").setTabCompleter(new PlotCompleter()); getCommand("deleteplot").setTabCompleter(new PlotCompleter()); + getCommand("enablebilly").setTabCompleter(new BooleanTabCompleter()); logger.info(ConsoleColours.YELLOW + "Registering listeners"); getServer().getPluginManager().registerEvents(new ChatInterceptor(), this); @@ -101,6 +103,7 @@ public void onEnable() { getServer().getPluginManager().registerEvents(new ExplosionListener(), this); getServer().getPluginManager().registerEvents(new AnimalInteractionListener(), this); getServer().getPluginManager().registerEvents(new PlotListener(), this); + getServer().getPluginManager().registerEvents(new VillagerInteractionListener(), this); logger.info(ConsoleColours.GREEN + "Ready!"); } diff --git a/src/main/java/elementalmp4/command/admin/EnableBillyCommand.java b/src/main/java/elementalmp4/command/admin/EnableBillyCommand.java new file mode 100644 index 0000000..2c5992c --- /dev/null +++ b/src/main/java/elementalmp4/command/admin/EnableBillyCommand.java @@ -0,0 +1,34 @@ +package main.java.elementalmp4.command.admin; + +import main.java.elementalmp4.GlobalConfig; +import main.java.elementalmp4.service.GlobalConfigService; +import org.bukkit.ChatColor; +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; + +import java.util.Set; + +public class EnableBillyCommand implements CommandExecutor { + @Override + public boolean onCommand(CommandSender commandSender, Command command, String label, String[] args) { + if (args.length == 0) { + boolean billyEnabled = GlobalConfigService.getAsBoolean(GlobalConfig.BILLY_ENABLED); + commandSender.sendMessage("Billy's Shop is currently " + format(billyEnabled)); + return true; + } + + if (!Set.of("true", "false").contains(args[0])) { + commandSender.sendMessage(ChatColor.RED + "You must specify true or false"); + return true; + } + + GlobalConfigService.set(GlobalConfig.BILLY_ENABLED, args[0]); + commandSender.sendMessage("Billy's Shop is now " + format(Boolean.parseBoolean(args[0]))); + return true; + } + + private String format(boolean enabled) { + return (enabled ? ChatColor.GREEN + "enabled" : ChatColor.RED + "disabled"); + } +} diff --git a/src/main/java/elementalmp4/listener/VillagerInteractionListener.java b/src/main/java/elementalmp4/listener/VillagerInteractionListener.java new file mode 100644 index 0000000..776ca40 --- /dev/null +++ b/src/main/java/elementalmp4/listener/VillagerInteractionListener.java @@ -0,0 +1,24 @@ +package main.java.elementalmp4.listener; + +import main.java.elementalmp4.service.MerchantService; +import org.bukkit.entity.EntityType; +import org.bukkit.entity.Player; +import org.bukkit.entity.Villager; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.player.PlayerInteractEntityEvent; + +public class VillagerInteractionListener implements Listener { + + @EventHandler + public void onPlayerInteractEntityEvent(PlayerInteractEntityEvent event) { + if (event.getRightClicked().getType() == EntityType.VILLAGER) { + Villager villager = (Villager) event.getRightClicked(); + if (villager.getProfession() == Villager.Profession.NITWIT) { + Player player = event.getPlayer(); + MerchantService.showSuperSecretShop(player); + } + } + } + +} diff --git a/src/main/java/elementalmp4/service/MerchantService.java b/src/main/java/elementalmp4/service/MerchantService.java new file mode 100644 index 0000000..eaee8ed --- /dev/null +++ b/src/main/java/elementalmp4/service/MerchantService.java @@ -0,0 +1,65 @@ +package main.java.elementalmp4.service; + +import main.java.elementalmp4.GlobalConfig; +import org.bukkit.Bukkit; +import org.bukkit.Material; +import org.bukkit.enchantments.Enchantment; +import org.bukkit.entity.Player; +import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.Merchant; +import org.bukkit.inventory.MerchantRecipe; +import org.bukkit.inventory.meta.EnchantmentStorageMeta; + +import java.util.ArrayList; +import java.util.List; + +public class MerchantService { + + private static final List BILLY_RECIPES = new ArrayList<>(); + + static { + //Elytra + MerchantRecipe elytra = new MerchantRecipe(new ItemStack(Material.ELYTRA), 100); + elytra.addIngredient(new ItemStack(Material.DIAMOND, 32)); + elytra.addIngredient(new ItemStack(Material.NETHERITE_INGOT, 1)); + BILLY_RECIPES.add(elytra); + + //Firework Rockets + MerchantRecipe rockets = new MerchantRecipe(new ItemStack(Material.FIREWORK_ROCKET, 32), 100); + rockets.addIngredient(new ItemStack(Material.IRON_INGOT, 20)); + BILLY_RECIPES.add(rockets); + + //Netherite + MerchantRecipe netherite = new MerchantRecipe(new ItemStack(Material.NETHERITE_INGOT), 100); + netherite.addIngredient(new ItemStack(Material.EMERALD, 16)); + BILLY_RECIPES.add(netherite); + + //Unbreaking 3 + ItemStack unbreakingBook = new ItemStack(Material.ENCHANTED_BOOK); + EnchantmentStorageMeta unbreakingMeta = (EnchantmentStorageMeta) unbreakingBook.getItemMeta(); + unbreakingMeta.addStoredEnchant(Enchantment.DURABILITY, 3, true); + unbreakingBook.setItemMeta(unbreakingMeta); + + MerchantRecipe unbreaking = new MerchantRecipe(unbreakingBook, 100); + unbreaking.addIngredient(new ItemStack(Material.EMERALD, 10)); + BILLY_RECIPES.add(unbreaking); + + //Mending + ItemStack mendingBook = new ItemStack(Material.ENCHANTED_BOOK); + EnchantmentStorageMeta mendingMeta = (EnchantmentStorageMeta) mendingBook.getItemMeta(); + mendingMeta.addStoredEnchant(Enchantment.MENDING, 1, true); + mendingBook.setItemMeta(mendingMeta); + + MerchantRecipe mending = new MerchantRecipe(mendingBook, 100); + mending.addIngredient(new ItemStack(Material.EMERALD, 10)); + BILLY_RECIPES.add(mending); + } + + public static void showSuperSecretShop(Player player) { + if (GlobalConfigService.getAsBoolean(GlobalConfig.BILLY_ENABLED)) { + Merchant merchant = Bukkit.createMerchant("Billy"); + merchant.setRecipes(BILLY_RECIPES); + player.openMerchant(merchant, true); + } + } +} \ No newline at end of file diff --git a/src/plugin.yml b/src/plugin.yml index c185543..82f4a66 100644 --- a/src/plugin.yml +++ b/src/plugin.yml @@ -88,6 +88,10 @@ commands: description: "Toggle whether sheep are smited when shaved" usage: "/sheepsmite [true/false]" permission: "sebutils.admin" + enablebilly: + description: "Toggle whether nitwit villagers will show a special item shop for difficult to acquire items" + usage: "/enablebilly [true/false]" + permission: "sebutils.admin" smite: description: "Smite a player" usage: "/smite [player]"