From 9408e87d42746bae2d2dea437e8c70798bae52af Mon Sep 17 00:00:00 2001 From: Zerthick Date: Sat, 13 May 2017 17:38:41 -0400 Subject: [PATCH] Refactor drop listener to utilize entity filtering, add non-functional forge and brew listeners, enable smelting listener, update to API 6.0. --- build.gradle | 9 ++-- .../protectionperms/ProtectionPerms.java | 2 +- .../listeners/item/BrewItemListener.java | 42 +++++++++++++++++++ .../listeners/item/DropItemDeathListener.java | 28 +++++++++++++ ...ner.java => DropItemDispenseListener.java} | 26 ++++++------ .../listeners/item/ForgeItemListener.java | 34 +++++++++++++++ .../listeners/item/ItemListenerManager.java | 7 +++- 7 files changed, 127 insertions(+), 21 deletions(-) create mode 100644 src/main/java/io/github/zerthick/protectionperms/events/listeners/item/BrewItemListener.java create mode 100644 src/main/java/io/github/zerthick/protectionperms/events/listeners/item/DropItemDeathListener.java rename src/main/java/io/github/zerthick/protectionperms/events/listeners/item/{DropItemListener.java => DropItemDispenseListener.java} (68%) create mode 100644 src/main/java/io/github/zerthick/protectionperms/events/listeners/item/ForgeItemListener.java diff --git a/build.gradle b/build.gradle index 48d6837..db60f66 100644 --- a/build.gradle +++ b/build.gradle @@ -33,14 +33,15 @@ repositories { } } -spongestart{ +spongestart { eula true - minecraft '1.10.2' - type 'stable' + minecraft '1.11.2' + type 'bleeding' + spongeForgeVersion '1.11.2-2282-6.0.0-BETA-2297' } dependencies { - compile 'org.spongepowered:spongeapi:5.1.0' + compile 'org.spongepowered:spongeapi:6.0.0' } diff --git a/src/main/java/io/github/zerthick/protectionperms/ProtectionPerms.java b/src/main/java/io/github/zerthick/protectionperms/ProtectionPerms.java index 16e56b5..2050210 100644 --- a/src/main/java/io/github/zerthick/protectionperms/ProtectionPerms.java +++ b/src/main/java/io/github/zerthick/protectionperms/ProtectionPerms.java @@ -31,7 +31,7 @@ @Plugin(id = "protectionperms", name = "ProtectionPerms", - version = "0.6.0", + version = "0.7.0", description = "A simple player protection and control plugin.", authors = { "Zerthick" diff --git a/src/main/java/io/github/zerthick/protectionperms/events/listeners/item/BrewItemListener.java b/src/main/java/io/github/zerthick/protectionperms/events/listeners/item/BrewItemListener.java new file mode 100644 index 0000000..a6aac8a --- /dev/null +++ b/src/main/java/io/github/zerthick/protectionperms/events/listeners/item/BrewItemListener.java @@ -0,0 +1,42 @@ +package io.github.zerthick.protectionperms.events.listeners.item; + +import org.spongepowered.api.entity.living.player.Player; +import org.spongepowered.api.event.Listener; +import org.spongepowered.api.event.filter.Getter; +import org.spongepowered.api.event.filter.cause.Root; +import org.spongepowered.api.event.item.inventory.ClickInventoryEvent; +import org.spongepowered.api.item.inventory.Inventory; +import org.spongepowered.api.item.inventory.InventoryArchetypes; +import org.spongepowered.api.item.inventory.slot.FuelSlot; +import org.spongepowered.api.item.inventory.slot.InputSlot; +import org.spongepowered.api.text.Text; +import org.spongepowered.api.text.chat.ChatTypes; +import org.spongepowered.api.text.format.TextColors; + +public class BrewItemListener { + + @Listener + public void onItemSmelt(ClickInventoryEvent event, @Root Player player, @Getter("getTargetInventory") Inventory inventory) { + + if (inventory.getArchetype() == InventoryArchetypes.BREWING_STAND) { + + Inventory brewingInputs = inventory.query(InputSlot.class); + + brewingInputs.slots().forEach(slot -> slot.peek().ifPresent(itemStack -> { + String itemId = itemStack.getItem().getType().getId(); + + if (slot instanceof FuelSlot) { + if (!player.hasPermission("protectionperms.item.fuel." + itemId)) { + event.setCancelled(true); + player.sendMessage(ChatTypes.ACTION_BAR, Text.of(TextColors.RED, "You don't have permission to use " + itemId + " as fuel!")); + } + } else { + if (!player.hasPermission("protectionperms.item.brew." + itemId)) { + event.setCancelled(true); + player.sendMessage(ChatTypes.ACTION_BAR, Text.of(TextColors.RED, "You don't have permission to brew " + itemId + '!')); + } + } + })); + } + } +} diff --git a/src/main/java/io/github/zerthick/protectionperms/events/listeners/item/DropItemDeathListener.java b/src/main/java/io/github/zerthick/protectionperms/events/listeners/item/DropItemDeathListener.java new file mode 100644 index 0000000..c9dae48 --- /dev/null +++ b/src/main/java/io/github/zerthick/protectionperms/events/listeners/item/DropItemDeathListener.java @@ -0,0 +1,28 @@ +package io.github.zerthick.protectionperms.events.listeners.item; + +import org.spongepowered.api.entity.EntityTypes; +import org.spongepowered.api.entity.Item; +import org.spongepowered.api.entity.living.player.Player; +import org.spongepowered.api.event.Listener; +import org.spongepowered.api.event.cause.entity.spawn.EntitySpawnCause; +import org.spongepowered.api.event.filter.cause.Root; +import org.spongepowered.api.event.item.inventory.DropItemEvent; + +public class DropItemDeathListener { + + @Listener + public void onItemDropDeath(DropItemEvent.Destruct event, @Root EntitySpawnCause spawnCause) { + + if (spawnCause.getEntity() instanceof Player) { + Player player = (Player) spawnCause.getEntity(); + event.filterEntities(entity -> { + if (entity.getType().equals(EntityTypes.ITEM)) { + Item item = (Item) entity; + String itemId = item.getItemType().getId(); + return player.hasPermission("protectionperms.item.drop." + itemId + ".death"); + } + return true; + }); + } + } +} diff --git a/src/main/java/io/github/zerthick/protectionperms/events/listeners/item/DropItemListener.java b/src/main/java/io/github/zerthick/protectionperms/events/listeners/item/DropItemDispenseListener.java similarity index 68% rename from src/main/java/io/github/zerthick/protectionperms/events/listeners/item/DropItemListener.java rename to src/main/java/io/github/zerthick/protectionperms/events/listeners/item/DropItemDispenseListener.java index 2564781..f544675 100644 --- a/src/main/java/io/github/zerthick/protectionperms/events/listeners/item/DropItemListener.java +++ b/src/main/java/io/github/zerthick/protectionperms/events/listeners/item/DropItemDispenseListener.java @@ -30,26 +30,24 @@ import org.spongepowered.api.text.chat.ChatTypes; import org.spongepowered.api.text.format.TextColors; -import java.util.List; -import java.util.stream.Collectors; - -public class DropItemListener { +public class DropItemDispenseListener { @Listener public void onItemDrop(DropItemEvent.Dispense event, @Root EntitySpawnCause spawnCause) { + if (spawnCause.getEntity() instanceof Player) { Player player = (Player) spawnCause.getEntity(); - List itemEntities = event.getEntities() - .stream().filter(entity -> entity.getType().equals(EntityTypes.ITEM)) - .map(Item.class::cast) - .collect(Collectors.toList()); - for (Item item : itemEntities) { - String itemId = item.getItemType().getId(); - if (!player.hasPermission("protectionperms.item.drop." + itemId)) { - event.setCancelled(true); - player.sendMessage(ChatTypes.ACTION_BAR, Text.of(TextColors.RED, "You don't have permission to drop " + itemId + '!')); + event.filterEntities(entity -> { + if (entity.getType().equals(EntityTypes.ITEM)) { + Item item = (Item) entity; + String itemId = item.getItemType().getId(); + if (!player.hasPermission("protectionperms.item.drop." + itemId + ".dispense")) { + player.sendMessage(ChatTypes.ACTION_BAR, Text.of(TextColors.RED, "You don't have permission to drop " + itemId + '!')); + return false; + } } - } + return true; + }); } } } diff --git a/src/main/java/io/github/zerthick/protectionperms/events/listeners/item/ForgeItemListener.java b/src/main/java/io/github/zerthick/protectionperms/events/listeners/item/ForgeItemListener.java new file mode 100644 index 0000000..352e897 --- /dev/null +++ b/src/main/java/io/github/zerthick/protectionperms/events/listeners/item/ForgeItemListener.java @@ -0,0 +1,34 @@ +package io.github.zerthick.protectionperms.events.listeners.item; + +import org.spongepowered.api.entity.living.player.Player; +import org.spongepowered.api.event.Listener; +import org.spongepowered.api.event.filter.Getter; +import org.spongepowered.api.event.filter.cause.Root; +import org.spongepowered.api.event.item.inventory.ClickInventoryEvent; +import org.spongepowered.api.item.inventory.Inventory; +import org.spongepowered.api.item.inventory.InventoryArchetypes; +import org.spongepowered.api.item.inventory.slot.InputSlot; +import org.spongepowered.api.text.Text; +import org.spongepowered.api.text.chat.ChatTypes; +import org.spongepowered.api.text.format.TextColors; + +public class ForgeItemListener { + + @Listener + public void onItemForge(ClickInventoryEvent event, @Root Player player, @Getter("getTargetInventory") Inventory inventory) { + + if (inventory.getArchetype() == InventoryArchetypes.ANVIL) { + + Inventory forgingInputs = inventory.query(InputSlot.class); + + forgingInputs.slots().forEach(slot -> slot.peek().ifPresent(itemStack -> { + String itemId = itemStack.getItem().getType().getId(); + if (!player.hasPermission("protectionperms.item.forge." + itemId)) { + event.setCancelled(true); + player.sendMessage(ChatTypes.ACTION_BAR, Text.of(TextColors.RED, "You don't have permission to forge " + itemId + '!')); + } + })); + } + } + +} diff --git a/src/main/java/io/github/zerthick/protectionperms/events/listeners/item/ItemListenerManager.java b/src/main/java/io/github/zerthick/protectionperms/events/listeners/item/ItemListenerManager.java index 9a53ab2..417dca6 100644 --- a/src/main/java/io/github/zerthick/protectionperms/events/listeners/item/ItemListenerManager.java +++ b/src/main/java/io/github/zerthick/protectionperms/events/listeners/item/ItemListenerManager.java @@ -39,10 +39,13 @@ public ItemListenerManager() { listeners.add(new PrimaryEntityListener()); listeners.add(new SecondaryEntityListener()); listeners.add(new UseItemListener()); - listeners.add(new DropItemListener()); + listeners.add(new DropItemDispenseListener()); + listeners.add(new DropItemDeathListener()); listeners.add(new CraftItemListener()); //listeners.add(new EnchantItemListener()); - //listeners.add(new SmeltItemListener()); + listeners.add(new SmeltItemListener()); + //listeners.add(new ForgeItemListener()); + //listeners.add(new BrewItemListener()); } @Override