From 6119180b0bcc7d2914c16e0e97a41a7f04eb501d Mon Sep 17 00:00:00 2001 From: Paldiu Date: Thu, 21 Apr 2022 09:54:36 -0500 Subject: [PATCH] Add VillagerInventory.java Added the possibility to find a special rabbits foot on a villager when trading with a BUTCHER profession. --- .../io/github/simplex/luck/FeelingLucky.java | 1 + .../luck/listener/VillagerInventory.java | 56 +++++++++++++++++++ 2 files changed, 57 insertions(+) create mode 100644 src/main/java/io/github/simplex/luck/listener/VillagerInventory.java diff --git a/src/main/java/io/github/simplex/luck/FeelingLucky.java b/src/main/java/io/github/simplex/luck/FeelingLucky.java index bdbd2d4..6615b52 100644 --- a/src/main/java/io/github/simplex/luck/FeelingLucky.java +++ b/src/main/java/io/github/simplex/luck/FeelingLucky.java @@ -70,6 +70,7 @@ public final class FeelingLucky extends JavaPlugin { new RestoreHunger(this); new TakeDamage(this); new UnbreakableTool(this); + new VillagerInventory(this); } public PlayerHandler getHandler() { diff --git a/src/main/java/io/github/simplex/luck/listener/VillagerInventory.java b/src/main/java/io/github/simplex/luck/listener/VillagerInventory.java new file mode 100644 index 0000000..025cbe5 --- /dev/null +++ b/src/main/java/io/github/simplex/luck/listener/VillagerInventory.java @@ -0,0 +1,56 @@ +package io.github.simplex.luck.listener; + +import io.github.simplex.lib.ItemBuilder; +import io.github.simplex.luck.FeelingLucky; +import io.github.simplex.luck.player.Luck; +import io.github.simplex.luck.util.SpecialFootItem; +import org.bukkit.Material; +import org.bukkit.entity.Villager; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.player.PlayerInteractAtEntityEvent; +import org.bukkit.inventory.MerchantRecipe; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +public class VillagerInventory implements Listener { + private final SpecialFootItem foot = new SpecialFootItem(); + private final MerchantRecipe recipe = new MerchantRecipe(foot.get(), 0, 2, true); + + private final FeelingLucky plugin; + + public VillagerInventory(FeelingLucky plugin) { + this.plugin = plugin; + + recipe.setIngredients(Arrays.asList( + ItemBuilder.of(Material.EMERALD).build(), + ItemBuilder.of(Material.RABBIT_HIDE).build() + )); + recipe.setDemand(8); + recipe.setPriceMultiplier(1.25F); + recipe.setVillagerExperience(25); + recipe.setSpecialPrice(4); + + plugin.getServer().getPluginManager().registerEvents(this, plugin); + } + + @EventHandler + public void addRabbitFootToVillagerInventory(PlayerInteractAtEntityEvent event) { + if (event.getRightClicked() instanceof Villager vil) { + if (!vil.getProfession().equals(Villager.Profession.BUTCHER)) return; + + List recipeList = new ArrayList<>(vil.getRecipes()); + if (recipeList.contains(recipe)) return; + + Luck luck = plugin.getHandler().getLuckContainer(event.getPlayer()); + if (luck == null) return; + + if (luck.quickRNG(luck.getPercentage())) { + recipeList.add(recipe); + vil.setRecipes(recipeList); + } + } + } +}