From 9205b143ede29c2c0a958f26441ff91130802dd2 Mon Sep 17 00:00:00 2001 From: LinoxGH Date: Tue, 23 Jun 2020 21:11:08 +0300 Subject: [PATCH] Added Magic Pills. Added Magic Pills which are used to cure zombie villagers. Approved suggestion #178 --- .../implementation/SlimefunItems.java | 2 + .../items/magical/MagicPills.java | 59 +++++++++++++++++++ .../PlayerInteractEntityListener.java | 48 +++++++++++++++ .../implementation/setup/ResearchSetup.java | 1 + .../setup/SlimefunItemSetup.java | 15 +++++ .../handlers/EntityInteractHandler.java | 36 +++++++++++ .../Objects/handlers/ItemHandler.java | 1 + .../Slimefun/SlimefunPlugin.java | 2 + 8 files changed, 164 insertions(+) create mode 100644 src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/magical/MagicPills.java create mode 100644 src/main/java/io/github/thebusybiscuit/slimefun4/implementation/listeners/PlayerInteractEntityListener.java create mode 100644 src/main/java/me/mrCookieSlime/Slimefun/Objects/handlers/EntityInteractHandler.java diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/SlimefunItems.java b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/SlimefunItems.java index 8977a6878..6131fd18a 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/SlimefunItems.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/SlimefunItems.java @@ -327,6 +327,7 @@ public final class SlimefunItems { public static final SlimefunItemStack ENDER_LUMP_2 = new SlimefunItemStack("ENDER_LUMP_2", Material.GOLD_NUGGET, "&5Ender Lump &7- &eII", "", "&c&oTier: II"); public static final SlimefunItemStack ENDER_LUMP_3 = new SlimefunItemStack("ENDER_LUMP_3", Material.GOLD_NUGGET, "&5Ender Lump &7- &eIII", "", "&c&oTier: III"); public static final SlimefunItemStack MAGICAL_BOOK_COVER = new SlimefunItemStack("MAGICAL_BOOK_COVER", Material.PAPER, "&6Magical Book Cover", "", "&a&oUsed for various Magic Books"); + public static final SlimefunItemStack MAGIC_PILLS = new SlimefunItemStack("MAGIC_PILLS", Material.NETHER_WART, "&cMagic Pills", "", "&a&oUsed to cure zombie villagers instantly"); public static final SlimefunItemStack BASIC_CIRCUIT_BOARD = new SlimefunItemStack("BASIC_CIRCUIT_BOARD", Material.ACTIVATOR_RAIL, "&bBasic Circuit Board"); public static final SlimefunItemStack ADVANCED_CIRCUIT_BOARD = new SlimefunItemStack("ADVANCED_CIRCUIT_BOARD", Material.POWERED_RAIL, "&bAdvanced Circuit Board"); public static final SlimefunItemStack WHEAT_FLOUR = new SlimefunItemStack("WHEAT_FLOUR", Material.SUGAR, "&rWheat Flour"); @@ -380,6 +381,7 @@ public final class SlimefunItems { public static final SlimefunItemStack RAINBOW_CONCRETE = new SlimefunItemStack("RAINBOW_CONCRETE", Material.WHITE_CONCRETE, "&5Rainbow Concrete", "", "&dCycles through all Colors of the Rainbow!"); public static final SlimefunItemStack RAINBOW_GLAZED_TERRACOTTA = new SlimefunItemStack("RAINBOW_GLAZED_TERRACOTTA", Material.WHITE_GLAZED_TERRACOTTA, "&5Rainbow Glazed Terracotta", "", "&dCycles through all Colors of the Rainbow!"); + /* Seasonal */ private static final String CHRISTMAS = ChatUtils.christmas("[Christmas Edition]"); public static final SlimefunItemStack RAINBOW_WOOL_XMAS = new SlimefunItemStack("RAINBOW_WOOL_XMAS", Material.WHITE_WOOL, "&5Rainbow Wool &7(Christmas)", "", CHRISTMAS); public static final SlimefunItemStack RAINBOW_GLASS_XMAS = new SlimefunItemStack("RAINBOW_GLASS_XMAS", Material.WHITE_STAINED_GLASS, "&5Rainbow Glass &7(Christmas)", "", CHRISTMAS); diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/magical/MagicPills.java b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/magical/MagicPills.java new file mode 100644 index 000000000..58f4b4a75 --- /dev/null +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/magical/MagicPills.java @@ -0,0 +1,59 @@ +package io.github.thebusybiscuit.slimefun4.implementation.items.magical; + +import org.bukkit.Sound; +import org.bukkit.entity.EntityType; +import org.bukkit.entity.Player; +import org.bukkit.entity.Villager; +import org.bukkit.entity.ZombieVillager; +import org.bukkit.inventory.EquipmentSlot; +import org.bukkit.inventory.ItemStack; + +import io.github.thebusybiscuit.slimefun4.api.MinecraftVersion; +import me.mrCookieSlime.Slimefun.Lists.RecipeType; +import me.mrCookieSlime.Slimefun.Objects.Category; +import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.SimpleSlimefunItem; +import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.SlimefunItem; +import me.mrCookieSlime.Slimefun.Objects.handlers.EntityInteractHandler; +import me.mrCookieSlime.Slimefun.SlimefunPlugin; +import me.mrCookieSlime.Slimefun.api.SlimefunItemStack; + +/** + * This {@link SlimefunItem} allows you to convert any {@link ZombieVillager} to + * their {@link Villager} variant. + * + * @author Linox + * + * @see EntityInteractHandler + * + */ +public class MagicPills extends SimpleSlimefunItem { + + public MagicPills(Category category, SlimefunItemStack item, RecipeType recipeType, ItemStack[] recipe, ItemStack recipeOutput) { + super(category, item, recipeType, recipe, recipeOutput); + } + + @Override + public EntityInteractHandler getItemHandler() { + return (e, item) -> { + if (e.getRightClicked().getType() == EntityType.ZOMBIE_VILLAGER) { + Player p = e.getPlayer(); + + item.setAmount(item.getAmount() - 1); + if (e.getHand() == EquipmentSlot.OFF_HAND) { + p.getInventory().setItemInOffHand(item); + } else { + p.getInventory().setItemInMainHand(item); + } + + p.playSound(p.getLocation(), Sound.ENTITY_ZOMBIE_VILLAGER_CONVERTED, 1, 1); + + ZombieVillager zombieVillager = (ZombieVillager) e.getRightClicked(); + zombieVillager.setConversionTime(1); + if (SlimefunPlugin.getMinecraftVersion().isAtLeast(MinecraftVersion.MINECRAFT_1_15)) { + zombieVillager.setConversionPlayer(p); + } + } + }; + } + +} \ No newline at end of file diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/listeners/PlayerInteractEntityListener.java b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/listeners/PlayerInteractEntityListener.java new file mode 100644 index 000000000..82d7665cc --- /dev/null +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/listeners/PlayerInteractEntityListener.java @@ -0,0 +1,48 @@ +package io.github.thebusybiscuit.slimefun4.implementation.listeners; + +import org.bukkit.entity.Entity; +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.player.PlayerInteractAtEntityEvent; +import org.bukkit.inventory.EquipmentSlot; +import org.bukkit.inventory.ItemStack; + +import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.SlimefunItem; +import me.mrCookieSlime.Slimefun.Objects.handlers.EntityInteractHandler; +import me.mrCookieSlime.Slimefun.SlimefunPlugin; +import me.mrCookieSlime.Slimefun.api.Slimefun; + +/** + * The Listener class responsible for a {@link Player} interacting with an {@link Entity}. + * + * @author Linox + * + * @see EntityInteractHandler + * + */ +public class PlayerInteractEntityListener implements Listener { + + public PlayerInteractEntityListener(SlimefunPlugin plugin) { + plugin.getServer().getPluginManager().registerEvents(this, plugin); + } + + @EventHandler + public void onInteractEntity(PlayerInteractAtEntityEvent e) { + + if (!e.getRightClicked().isValid()) return; + + ItemStack itemStack; + if (e.getHand() == EquipmentSlot.OFF_HAND) { + itemStack = e.getPlayer().getInventory().getItemInOffHand(); + } else { + itemStack = e.getPlayer().getInventory().getItemInMainHand(); + } + SlimefunItem sfItem = SlimefunItem.getByItem(itemStack); + if (sfItem == null) return; + + if (Slimefun.hasUnlocked(e.getPlayer(), sfItem, true)) { + sfItem.callItemHandler(EntityInteractHandler.class, handler -> handler.onInteract(e, itemStack)); + } + } +} \ No newline at end of file diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/setup/ResearchSetup.java b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/setup/ResearchSetup.java index b7a8bf535..a35f46fdb 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/setup/ResearchSetup.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/setup/ResearchSetup.java @@ -265,6 +265,7 @@ public final class ResearchSetup { register("tree_growth_accelerator", 256, "Faster Trees", 18, SlimefunItems.TREE_GROWTH_ACCELERATOR); register("industrial_miner", 95, "Industrial Mining", 28, SlimefunItems.INDUSTRIAL_MINER); register("advanced_industrial_miner", 98, "Better Mining", 36, SlimefunItems.ADVANCED_INDUSTRIAL_MINER); + register("magic_pills", 257, "De-Zombification", 22, SlimefunItems.MAGIC_PILLS); } private static void register(String key, int id, String name, int defaultCost, ItemStack... items) { diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/setup/SlimefunItemSetup.java b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/setup/SlimefunItemSetup.java index 3c3585388..3320d0bc1 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/setup/SlimefunItemSetup.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/setup/SlimefunItemSetup.java @@ -6,6 +6,8 @@ import java.util.List; import org.bukkit.Material; import org.bukkit.NamespacedKey; import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.meta.PotionMeta; +import org.bukkit.potion.PotionData; import org.bukkit.potion.PotionEffect; import org.bukkit.potion.PotionEffectType; @@ -119,6 +121,7 @@ import io.github.thebusybiscuit.slimefun4.implementation.items.magical.InfusedMa import io.github.thebusybiscuit.slimefun4.implementation.items.magical.KnowledgeFlask; import io.github.thebusybiscuit.slimefun4.implementation.items.magical.KnowledgeTome; import io.github.thebusybiscuit.slimefun4.implementation.items.magical.MagicEyeOfEnder; +import io.github.thebusybiscuit.slimefun4.implementation.items.magical.MagicPills; import io.github.thebusybiscuit.slimefun4.implementation.items.magical.SoulboundItem; import io.github.thebusybiscuit.slimefun4.implementation.items.magical.SoulboundRune; import io.github.thebusybiscuit.slimefun4.implementation.items.magical.StormStaff; @@ -176,6 +179,7 @@ import me.mrCookieSlime.Slimefun.Objects.Category; import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.SlimefunItem; import me.mrCookieSlime.Slimefun.Objects.handlers.RainbowTicker; import me.mrCookieSlime.Slimefun.api.SlimefunItemStack; +import org.bukkit.potion.PotionType; /** * This static utility class holds the recipes of all items. @@ -775,6 +779,17 @@ public final class SlimefunItemSetup { .register(plugin); } + if (SlimefunPlugin.getMinecraftVersion().isAtLeast(MinecraftVersion.MINECRAFT_1_14)) { + ItemStack magicPillsRecipePotion = new ItemStack(Material.POTION); + PotionMeta meta = (PotionMeta) magicPillsRecipePotion.getItemMeta(); + meta.setBasePotionData(new PotionData(PotionType.WEAKNESS, false, false)); + magicPillsRecipePotion.setItemMeta(meta); + new MagicPills(categories.magicalGadgets, SlimefunItems.MAGIC_PILLS, RecipeType.MAGIC_WORKBENCH, + new ItemStack[] {new ItemStack(Material.GOLD_INGOT), SlimefunItems.MAGIC_LUMP_2, new ItemStack(Material.GOLD_INGOT), new ItemStack(Material.APPLE), magicPillsRecipePotion, new ItemStack(Material.APPLE), new ItemStack(Material.GOLD_INGOT), SlimefunItems.MAGIC_LUMP_2, new ItemStack(Material.GOLD_INGOT)}, + new CustomItem(SlimefunItems.MAGIC_PILLS, 4)) + .register(plugin); + } + new SmeltersPickaxe(categories.tools, SlimefunItems.SMELTERS_PICKAXE, RecipeType.ENHANCED_CRAFTING_TABLE, new ItemStack[] {SlimefunItems.LAVA_CRYSTAL, SlimefunItems.LAVA_CRYSTAL, SlimefunItems.LAVA_CRYSTAL, null, SlimefunItems.FERROSILICON, null, null, SlimefunItems.FERROSILICON, null}) .register(plugin); diff --git a/src/main/java/me/mrCookieSlime/Slimefun/Objects/handlers/EntityInteractHandler.java b/src/main/java/me/mrCookieSlime/Slimefun/Objects/handlers/EntityInteractHandler.java new file mode 100644 index 000000000..32cc21542 --- /dev/null +++ b/src/main/java/me/mrCookieSlime/Slimefun/Objects/handlers/EntityInteractHandler.java @@ -0,0 +1,36 @@ +package me.mrCookieSlime.Slimefun.Objects.handlers; + +import org.bukkit.entity.Entity; +import org.bukkit.entity.Player; +import org.bukkit.event.player.PlayerInteractAtEntityEvent; +import org.bukkit.inventory.ItemStack; + +import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.SimpleSlimefunItem; +import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.SlimefunItem; + +/** + * This is triggered when a {@link Player} interacts with an {@link Entity}. + * + * @author Linox + * + * @see ItemHandler + * @see SimpleSlimefunItem + * + */ +@FunctionalInterface +public interface EntityInteractHandler extends ItemHandler { + + /** + * This function is triggered when a {@link Player} right clicks with the assigned {@link SlimefunItem} + * in his hand. + * + * @param e + * The {@link PlayerInteractAtEntityEvent} that was triggered + */ + void onInteract(PlayerInteractAtEntityEvent e, ItemStack item); + + @Override + default Class getIdentifier() { + return EntityInteractHandler.class; + } +} \ No newline at end of file diff --git a/src/main/java/me/mrCookieSlime/Slimefun/Objects/handlers/ItemHandler.java b/src/main/java/me/mrCookieSlime/Slimefun/Objects/handlers/ItemHandler.java index 0d2cef6d6..3b685fe40 100644 --- a/src/main/java/me/mrCookieSlime/Slimefun/Objects/handlers/ItemHandler.java +++ b/src/main/java/me/mrCookieSlime/Slimefun/Objects/handlers/ItemHandler.java @@ -15,6 +15,7 @@ import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.SlimefunItem; * @see ItemConsumptionHandler * @see BlockUseHandler * @see EntityKillHandler + * @see EntityInteractHandler */ @FunctionalInterface public interface ItemHandler { diff --git a/src/main/java/me/mrCookieSlime/Slimefun/SlimefunPlugin.java b/src/main/java/me/mrCookieSlime/Slimefun/SlimefunPlugin.java index f220ebcfe..f91cf72d5 100644 --- a/src/main/java/me/mrCookieSlime/Slimefun/SlimefunPlugin.java +++ b/src/main/java/me/mrCookieSlime/Slimefun/SlimefunPlugin.java @@ -69,6 +69,7 @@ import io.github.thebusybiscuit.slimefun4.implementation.listeners.IronGolemList import io.github.thebusybiscuit.slimefun4.implementation.listeners.ItemPickupListener; import io.github.thebusybiscuit.slimefun4.implementation.listeners.MobDropListener; import io.github.thebusybiscuit.slimefun4.implementation.listeners.MultiBlockListener; +import io.github.thebusybiscuit.slimefun4.implementation.listeners.PlayerInteractEntityListener; import io.github.thebusybiscuit.slimefun4.implementation.listeners.PlayerProfileListener; import io.github.thebusybiscuit.slimefun4.implementation.listeners.SeismicAxeListener; import io.github.thebusybiscuit.slimefun4.implementation.listeners.SlimefunBootsListener; @@ -236,6 +237,7 @@ public final class SlimefunPlugin extends JavaPlugin implements SlimefunAddon { new FireworksListener(this); new WitherListener(this); new IronGolemListener(this); + new PlayerInteractEntityListener(this); new MobDropListener(this, (BasicCircuitBoard) SlimefunItems.BASIC_CIRCUIT_BOARD.getItem());