From 5ff93b75e04a6101507ecd11259bf52dcd85cbf4 Mon Sep 17 00:00:00 2001 From: poma123 Date: Sat, 25 Jul 2020 13:37:04 +0200 Subject: [PATCH] Added PiglinListener --- .../implementation/SlimefunPlugin.java | 39 +++---------- .../listeners/ItemPickupListener.java | 12 +--- .../listeners/PiglinListener.java | 56 +++++++++++++++++++ 3 files changed, 64 insertions(+), 43 deletions(-) create mode 100644 src/main/java/io/github/thebusybiscuit/slimefun4/implementation/listeners/PiglinListener.java diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/SlimefunPlugin.java b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/SlimefunPlugin.java index 3d8567458..0a4330eaa 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/SlimefunPlugin.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/SlimefunPlugin.java @@ -10,9 +10,12 @@ import java.util.Set; import java.util.logging.Level; import java.util.stream.Collectors; +import io.github.thebusybiscuit.slimefun4.implementation.listeners.*; +import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.SlimefunItem; import org.bukkit.Bukkit; import org.bukkit.Server; import org.bukkit.command.Command; +import org.bukkit.entity.EntityType; import org.bukkit.entity.Player; import org.bukkit.plugin.Plugin; import org.bukkit.plugin.PluginDescriptionFile; @@ -50,38 +53,6 @@ import io.github.thebusybiscuit.slimefun4.implementation.items.electric.reactors import io.github.thebusybiscuit.slimefun4.implementation.items.tools.GrapplingHook; import io.github.thebusybiscuit.slimefun4.implementation.items.weapons.SeismicAxe; import io.github.thebusybiscuit.slimefun4.implementation.items.weapons.VampireBlade; -import io.github.thebusybiscuit.slimefun4.implementation.listeners.AncientAltarListener; -import io.github.thebusybiscuit.slimefun4.implementation.listeners.BackpackListener; -import io.github.thebusybiscuit.slimefun4.implementation.listeners.BlockListener; -import io.github.thebusybiscuit.slimefun4.implementation.listeners.BlockPhysicsListener; -import io.github.thebusybiscuit.slimefun4.implementation.listeners.CargoNodeListener; -import io.github.thebusybiscuit.slimefun4.implementation.listeners.CoolerListener; -import io.github.thebusybiscuit.slimefun4.implementation.listeners.DeathpointListener; -import io.github.thebusybiscuit.slimefun4.implementation.listeners.DebugFishListener; -import io.github.thebusybiscuit.slimefun4.implementation.listeners.DispenserListener; -import io.github.thebusybiscuit.slimefun4.implementation.listeners.EnhancedFurnaceListener; -import io.github.thebusybiscuit.slimefun4.implementation.listeners.ExplosionsListener; -import io.github.thebusybiscuit.slimefun4.implementation.listeners.FireworksListener; -import io.github.thebusybiscuit.slimefun4.implementation.listeners.GadgetsListener; -import io.github.thebusybiscuit.slimefun4.implementation.listeners.GrapplingHookListener; -import io.github.thebusybiscuit.slimefun4.implementation.listeners.IronGolemListener; -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; -import io.github.thebusybiscuit.slimefun4.implementation.listeners.SlimefunBowListener; -import io.github.thebusybiscuit.slimefun4.implementation.listeners.SlimefunGuideListener; -import io.github.thebusybiscuit.slimefun4.implementation.listeners.SlimefunItemConsumeListener; -import io.github.thebusybiscuit.slimefun4.implementation.listeners.SlimefunItemListener; -import io.github.thebusybiscuit.slimefun4.implementation.listeners.SoulboundListener; -import io.github.thebusybiscuit.slimefun4.implementation.listeners.TalismanListener; -import io.github.thebusybiscuit.slimefun4.implementation.listeners.VampireBladeListener; -import io.github.thebusybiscuit.slimefun4.implementation.listeners.VanillaMachinesListener; -import io.github.thebusybiscuit.slimefun4.implementation.listeners.WitherListener; -import io.github.thebusybiscuit.slimefun4.implementation.listeners.WorldListener; import io.github.thebusybiscuit.slimefun4.implementation.resources.GEOResourcesSetup; import io.github.thebusybiscuit.slimefun4.implementation.setup.PostSetup; import io.github.thebusybiscuit.slimefun4.implementation.setup.ResearchSetup; @@ -430,6 +401,10 @@ public final class SlimefunPlugin extends JavaPlugin implements SlimefunAddon { new PlayerInteractEntityListener(this); new MobDropListener(this); + if (SlimefunPlugin.getMinecraftVersion().isAtLeast(MinecraftVersion.MINECRAFT_1_16)) { + new PiglinListener(this); + } + // Item-specific Listeners new VampireBladeListener(this, (VampireBlade) SlimefunItems.BLADE_OF_VAMPIRES.getItem()); new CoolerListener(this, (Cooler) SlimefunItems.COOLER.getItem()); diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/listeners/ItemPickupListener.java b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/listeners/ItemPickupListener.java index 4aae0f50e..8b7216751 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/listeners/ItemPickupListener.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/listeners/ItemPickupListener.java @@ -1,8 +1,5 @@ package io.github.thebusybiscuit.slimefun4.implementation.listeners; -import io.github.thebusybiscuit.slimefun4.api.MinecraftVersion; -import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.SlimefunItem; -import org.bukkit.entity.EntityType; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; import org.bukkit.event.entity.EntityPickupItemEvent; @@ -13,8 +10,7 @@ import io.github.thebusybiscuit.slimefun4.implementation.SlimefunPlugin; import io.github.thebusybiscuit.slimefun4.utils.SlimefunUtils; /** - * Listens to the ItemPickup events to prevent it if the item has the "no_pickup" metadata or is an ALTAR_PROBE - * or if a piglin wants to pickup SlimefunItems. + * Listens to the ItemPickup events to prevent it if the item has the "no_pickup" metadata or is an ALTAR_PROBE. * * @author TheBusyBiscuit */ @@ -34,12 +30,6 @@ public class ItemPickupListener implements Listener { else if (e.getItem().getItemStack().hasItemMeta() && e.getItem().getItemStack().getItemMeta().hasDisplayName() && e.getItem().getItemStack().getItemMeta().getDisplayName().startsWith(ITEM_PREFIX)) { e.setCancelled(true); e.getItem().remove(); - } else if (SlimefunPlugin.getMinecraftVersion().isAtLeast(MinecraftVersion.MINECRAFT_1_16)) { - if (e.getEntityType() == EntityType.PIGLIN) { - if (SlimefunItem.getByItem(e.getItem().getItemStack()) != null) { - e.setCancelled(true); - } - } } } diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/listeners/PiglinListener.java b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/listeners/PiglinListener.java new file mode 100644 index 000000000..a1898c8b1 --- /dev/null +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/listeners/PiglinListener.java @@ -0,0 +1,56 @@ +package io.github.thebusybiscuit.slimefun4.implementation.listeners; + +import io.github.thebusybiscuit.slimefun4.implementation.SlimefunPlugin; +import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.SlimefunItem; +import org.bukkit.entity.EntityType; +import org.bukkit.entity.Item; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.entity.EntityPickupItemEvent; +import org.bukkit.event.entity.EntityTargetEvent; +import org.bukkit.event.player.PlayerInteractAtEntityEvent; +import org.bukkit.event.player.PlayerInteractEntityEvent; +import org.bukkit.inventory.EquipmentSlot; +import org.bukkit.inventory.ItemStack; + +/** + * Listens to "Piglin trading events" to prevent trading with Slimefun items. + * + * @author poma123 + */ +public class PiglinListener implements Listener { + + public PiglinListener(SlimefunPlugin plugin) { + plugin.getServer().getPluginManager().registerEvents(this, plugin); + } + + @EventHandler + public void onEntityPickup(EntityPickupItemEvent e) { + if (e.getEntityType() == EntityType.PIGLIN) { + if (SlimefunItem.getByItem(e.getItem().getItemStack()) != null) { + e.setCancelled(true); + } + } + } + + @EventHandler + public void onInteractEntity(PlayerInteractEntityEvent e) { + if (!e.getRightClicked().isValid() || e.getRightClicked().getType() != EntityType.PIGLIN) { + 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) { + e.setCancelled(true); + } + } +}