From 110eccc9418543f69d4c951a1c9f54bdb2a90d15 Mon Sep 17 00:00:00 2001 From: Zerthick Date: Tue, 9 Jan 2018 15:28:45 -0500 Subject: [PATCH] Add fishing start listener --- .../listeners/item/FishingStartListener.java | 34 +++++++++++++++++++ .../listeners/item/ItemListenerManager.java | 1 + .../listeners/item/UseItemListener.java | 2 +- 3 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 src/main/java/io/github/zerthick/protectionperms/events/listeners/item/FishingStartListener.java diff --git a/src/main/java/io/github/zerthick/protectionperms/events/listeners/item/FishingStartListener.java b/src/main/java/io/github/zerthick/protectionperms/events/listeners/item/FishingStartListener.java new file mode 100644 index 0000000..33cf114 --- /dev/null +++ b/src/main/java/io/github/zerthick/protectionperms/events/listeners/item/FishingStartListener.java @@ -0,0 +1,34 @@ +package io.github.zerthick.protectionperms.events.listeners.item; + +import io.github.zerthick.protectionperms.PermHandler; +import org.spongepowered.api.entity.living.player.Player; +import org.spongepowered.api.event.Listener; +import org.spongepowered.api.event.action.FishingEvent; +import org.spongepowered.api.event.cause.EventContext; +import org.spongepowered.api.event.cause.EventContextKeys; +import org.spongepowered.api.event.filter.Getter; +import org.spongepowered.api.event.filter.cause.Root; +import org.spongepowered.api.item.ItemType; +import org.spongepowered.api.text.Text; +import org.spongepowered.api.text.chat.ChatTypes; +import org.spongepowered.api.text.format.TextColors; + +public class FishingStartListener { + + @Listener + public void onItemUse(FishingEvent.Start event, @Root Player player, @Getter("getContext") EventContext context) { + + context.get(EventContextKeys.USED_ITEM).ifPresent(itemStackSnapshot -> { + ItemType itemType = itemStackSnapshot.getType(); + String itemId = itemType.getId(); + + PermHandler ph = PermHandler.getInstance(); + + if (!ph.checkPerm(player, "protectionperms.item.use." + itemId + ".on.self")) { + event.setCancelled(true); + player.sendMessage(ChatTypes.ACTION_BAR, Text.of(TextColors.RED, "You don't have permission to use " + itemType.getName() + '!')); + } + }); + } + +} 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 5394a8e..5bbf39b 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 @@ -48,6 +48,7 @@ public ItemListenerManager() { //*listeners.add(new BrewItemListener()); listeners.add(new EquipItemListener()); listeners.add(new PickupItemListener()); + listeners.add(new FishingStartListener()); } @Override diff --git a/src/main/java/io/github/zerthick/protectionperms/events/listeners/item/UseItemListener.java b/src/main/java/io/github/zerthick/protectionperms/events/listeners/item/UseItemListener.java index 44086ea..3164c60 100644 --- a/src/main/java/io/github/zerthick/protectionperms/events/listeners/item/UseItemListener.java +++ b/src/main/java/io/github/zerthick/protectionperms/events/listeners/item/UseItemListener.java @@ -39,7 +39,7 @@ public void onItemUse(UseItemStackEvent.Start event, @Root Player player) { PermHandler ph = PermHandler.getInstance(); - if (!ph.checkPerm(player, "protectionperms.item.use." + itemId + ".on.self")) { + if (!ph.checkPerm(player, "protectionperms.item.use." + itemId)) { event.setCancelled(true); player.sendMessage(ChatTypes.ACTION_BAR, Text.of(TextColors.RED, "You don't have permission to use " + itemType.getName() + '!')); }