Skip to content

Commit

Permalink
Update item listeners
Browse files Browse the repository at this point in the history
  • Loading branch information
Zerthick committed Jan 2, 2018
1 parent 9aff142 commit a0a8d8c
Show file tree
Hide file tree
Showing 12 changed files with 104 additions and 76 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
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.filter.cause.First;
import org.spongepowered.api.event.item.inventory.ClickInventoryEvent;
import org.spongepowered.api.item.ItemType;
import org.spongepowered.api.item.inventory.Inventory;
import org.spongepowered.api.item.inventory.InventoryArchetypes;
import org.spongepowered.api.item.inventory.crafting.CraftingOutput;
import org.spongepowered.api.item.inventory.query.QueryOperationTypes;
import org.spongepowered.api.text.Text;
import org.spongepowered.api.text.chat.ChatTypes;
import org.spongepowered.api.text.format.TextColors;
Expand All @@ -16,17 +18,19 @@
public class CraftItemListener {

@Listener
public void onItemCraft(ClickInventoryEvent event, @Root Player player, @Getter("getTargetInventory") Inventory inventory) {
public void onItemCraft(ClickInventoryEvent event, @First Player player, @Getter("getTargetInventory") Inventory inventory) {

if (inventory.getArchetype() == InventoryArchetypes.PLAYER || inventory.getArchetype() == InventoryArchetypes.WORKBENCH) {

Inventory craftingOutputs = inventory.query(CraftingOutput.class);
Inventory craftingOutputs = inventory.query(QueryOperationTypes.INVENTORY_TYPE.of(CraftingOutput.class));

craftingOutputs.slots().forEach(slot -> slot.peek().ifPresent(itemStack -> {
String itemId = itemStack.getItem().getType().getId();

ItemType itemType = itemStack.getType();
String itemId = itemType.getId();
if (!player.hasPermission("protectionperms.item.craft." + itemId)) {
event.setCancelled(true);
player.sendMessage(ChatTypes.ACTION_BAR, Text.of(TextColors.RED, "You don't have permission to craft " + itemId + '!'));
player.sendMessage(ChatTypes.ACTION_BAR, Text.of(TextColors.RED, "You don't have permission to craft " + itemType.getName() + '!'));
}
}));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,14 @@
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) {
public void onItemDropDeath(DropItemEvent.Destruct event, @Root Player player) {

if (spawnCause.getEntity() instanceof Player) {
Player player = (Player) spawnCause.getEntity();
event.filterEntities(entity -> {
if (entity.getType().equals(EntityTypes.ITEM)) {
Item item = (Item) entity;
Expand All @@ -23,6 +20,5 @@ public void onItemDropDeath(DropItemEvent.Destruct event, @Root EntitySpawnCause
}
return true;
});
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,31 +23,29 @@
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;
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 DropItemDispenseListener {

@Listener
public void onItemDrop(DropItemEvent.Dispense event, @Root EntitySpawnCause spawnCause) {
public void onItemDrop(DropItemEvent.Dispense event, @Root Player player) {

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();
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;
}
event.filterEntities(entity -> {
if (entity.getType().equals(EntityTypes.ITEM)) {
Item item = (Item) entity;
ItemType itemType = item.getItemType();
String itemId = itemType.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 " + itemType.getName() + '!'));
return false;
}
return true;
});
}
}
return true;
});
}
}
Original file line number Diff line number Diff line change
@@ -1,24 +1,18 @@
package io.github.zerthick.protectionperms.events.listeners.item;

import org.spongepowered.api.data.key.Keys;
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 EnchantItemListener {

@Listener
public void onItemEnchant(ClickInventoryEvent event, @Root Player player, @Getter("getTargetInventory") Inventory inventory) {

if (inventory.getArchetype() == InventoryArchetypes.ENCHANTING_TABLE) {
/*if (inventory.getArchetype() == InventoryArchetypes.ENCHANTING_TABLE) {
Inventory enchantingInputs = inventory.query(InputSlot.class);
Expand All @@ -33,7 +27,7 @@ public void onItemEnchant(ClickInventoryEvent event, @Root Player player, @Gette
}
}));
}));
}
}*/
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
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.filter.cause.First;
import org.spongepowered.api.event.item.inventory.ClickInventoryEvent;
import org.spongepowered.api.item.ItemType;
import org.spongepowered.api.item.inventory.Inventory;
import org.spongepowered.api.item.inventory.InventoryArchetypes;
import org.spongepowered.api.item.inventory.query.QueryOperationTypes;
import org.spongepowered.api.item.inventory.slot.EquipmentSlot;
import org.spongepowered.api.text.Text;
import org.spongepowered.api.text.chat.ChatTypes;
Expand All @@ -15,17 +17,20 @@
public class EquipItemListener {

@Listener
public void onItemEquip(ClickInventoryEvent event, @Root Player player, @Getter("getTargetInventory") Inventory inventory) {
public void onItemEquip(ClickInventoryEvent event, @First Player player, @Getter("getTargetInventory") Inventory inventory) {

if (inventory.getArchetype() == InventoryArchetypes.PLAYER) {

Inventory equipmentSlots = inventory.query(EquipmentSlot.class);
Inventory equipmentSlots = inventory.query(QueryOperationTypes.INVENTORY_TYPE.of(EquipmentSlot.class));

equipmentSlots.slots().forEach(slot -> slot.peek().ifPresent(itemStack -> {
String itemId = itemStack.getItem().getType().getId();

ItemType itemType = itemStack.getType();
String itemId = itemType.getId();

if (!player.hasPermission("protectionperms.item.equip." + itemId + ".on.self")) {
event.setCancelled(true);
player.sendMessage(ChatTypes.ACTION_BAR, Text.of(TextColors.RED, "You don't have permission to equip " + itemId + '!'));
player.sendMessage(ChatTypes.ACTION_BAR, Text.of(TextColors.RED, "You don't have permission to equip " + itemType.getName() + '!'));
}
}));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,6 @@
package io.github.zerthick.protectionperms.events.listeners.item;

import io.github.zerthick.protectionperms.events.listeners.ListenerManager;
import io.github.zerthick.protectionperms.events.listeners.item.interactBlockEvent.PrimaryBlockListener;
import io.github.zerthick.protectionperms.events.listeners.item.interactBlockEvent.SecondaryBlockListener;
import io.github.zerthick.protectionperms.events.listeners.item.interactEntityEvent.PrimaryEntityListener;
import io.github.zerthick.protectionperms.events.listeners.item.interactEntityEvent.SecondaryEntityListener;

import java.util.LinkedList;
import java.util.List;
Expand All @@ -34,18 +30,18 @@ public class ItemListenerManager implements ListenerManager{

public ItemListenerManager() {
listeners = new LinkedList<>();
listeners.add(new PrimaryBlockListener());
listeners.add(new SecondaryBlockListener());
listeners.add(new PrimaryEntityListener());
listeners.add(new SecondaryEntityListener());
listeners.add(new UseItemListener());
listeners.add(new DropItemDispenseListener());
listeners.add(new DropItemDeathListener());
listeners.add(new CraftItemListener());
//listeners.add(new EnchantItemListener());
listeners.add(new SmeltItemListener());
//listeners.add(new ForgeItemListener());
//listeners.add(new BrewItemListener());
//listeners.add(new PrimaryBlockListener());
//listeners.add(new SecondaryBlockListener());
//listeners.add(new PrimaryEntityListener());
//listeners.add(new SecondaryEntityListener());
//listeners.add(new UseItemListener());
//listeners.add(new DropItemDispenseListener());
//listeners.add(new DropItemDeathListener());
//listeners.add(new CraftItemListener());
//*listeners.add(new EnchantItemListener());
//listeners.add(new SmeltItemListener());
//*listeners.add(new ForgeItemListener());
//*listeners.add(new BrewItemListener());
listeners.add(new EquipItemListener());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
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.filter.cause.First;
import org.spongepowered.api.event.item.inventory.ClickInventoryEvent;
import org.spongepowered.api.item.ItemType;
import org.spongepowered.api.item.inventory.Inventory;
import org.spongepowered.api.item.inventory.InventoryArchetypes;
import org.spongepowered.api.item.inventory.query.QueryOperationTypes;
import org.spongepowered.api.item.inventory.slot.FuelSlot;
import org.spongepowered.api.item.inventory.slot.InputSlot;
import org.spongepowered.api.text.Text;
Expand All @@ -16,24 +18,26 @@
public class SmeltItemListener {

@Listener
public void onItemSmelt(ClickInventoryEvent event, @Root Player player, @Getter("getTargetInventory") Inventory inventory) {
public void onItemSmelt(ClickInventoryEvent event, @First Player player, @Getter("getTargetInventory") Inventory inventory) {

if (inventory.getArchetype() == InventoryArchetypes.FURNACE) {

Inventory smeltingInputs = inventory.query(InputSlot.class);
Inventory smeltingInputs = inventory.query(QueryOperationTypes.INVENTORY_TYPE.of(InputSlot.class));

smeltingInputs.slots().forEach(slot -> slot.peek().ifPresent(itemStack -> {
String itemId = itemStack.getItem().getType().getId();

ItemType itemType = itemStack.getType();
String itemId = itemType.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!"));
player.sendMessage(ChatTypes.ACTION_BAR, Text.of(TextColors.RED, "You don't have permission to use " + itemType.getName() + " as fuel!"));
}
} else {
if (!player.hasPermission("protectionperms.item.smelt." + itemId)) {
event.setCancelled(true);
player.sendMessage(ChatTypes.ACTION_BAR, Text.of(TextColors.RED, "You don't have permission to smelt " + itemId + '!'));
player.sendMessage(ChatTypes.ACTION_BAR, Text.of(TextColors.RED, "You don't have permission to smelt " + itemType.getName() + '!'));
}
}
}));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.spongepowered.api.event.Listener;
import org.spongepowered.api.event.filter.cause.Root;
import org.spongepowered.api.event.item.inventory.UseItemStackEvent;
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;
Expand All @@ -31,10 +32,12 @@ public class UseItemListener {

@Listener
public void onItemUse(UseItemStackEvent.Start event, @Root Player player) {
String itemId = event.getItemStackInUse().getType().getId();

ItemType itemType = event.getItemStackInUse().getType();
String itemId = itemType.getId();
if (!player.hasPermission("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 " + itemId + '!'));
player.sendMessage(ChatTypes.ACTION_BAR, Text.of(TextColors.RED, "You don't have permission to use " + itemType.getName() + '!'));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@

package io.github.zerthick.protectionperms.events.listeners.item.interactBlockEvent;

import org.spongepowered.api.block.BlockState;
import org.spongepowered.api.entity.living.player.Player;
import org.spongepowered.api.event.Listener;
import org.spongepowered.api.event.block.InteractBlockEvent;
import org.spongepowered.api.event.filter.cause.Root;
import org.spongepowered.api.item.ItemType;
import org.spongepowered.api.item.inventory.ItemStack;
import org.spongepowered.api.text.Text;
import org.spongepowered.api.text.chat.ChatTypes;
Expand All @@ -36,14 +38,19 @@ public class PrimaryBlockListener {
public void onInteractBlockPrimaryMain(InteractBlockEvent.Primary event, @Root Player player) {
Optional<ItemStack> itemStackOptional = player.getItemInHand(event.getHandType());
if(itemStackOptional.isPresent()) {
String blockTypeId = event.getTargetBlock().getState().getType().getId();
String blockStateId = event.getTargetBlock().getState().getId();
String itemId = itemStackOptional.get().getItem().getId();

BlockState blockState = event.getTargetBlock().getState();
String blockTypeId = blockState.getType().getId();
String blockStateId = blockState.getId();

ItemType itemType = itemStackOptional.get().getType();
String itemId = itemType.getId();

if (!player.hasPermission("protectionperms.item.use." + itemId + ".on." + blockTypeId + ".primary") &&
!player.hasPermission("protectionperms.item.use." + itemId + ".on." + blockStateId + ".primary")) {
event.setCancelled(true);
player.sendMessage(ChatTypes.ACTION_BAR,
Text.of(TextColors.RED, "You don't have permission to primary use " + itemId + " on " + blockStateId + '!'));
Text.of(TextColors.RED, "You don't have permission to primary use " + itemType.getName() + " on " + blockState.getName() + '!'));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@

package io.github.zerthick.protectionperms.events.listeners.item.interactBlockEvent;

import org.spongepowered.api.block.BlockState;
import org.spongepowered.api.entity.living.player.Player;
import org.spongepowered.api.event.Listener;
import org.spongepowered.api.event.block.InteractBlockEvent;
import org.spongepowered.api.event.filter.cause.Root;
import org.spongepowered.api.item.ItemType;
import org.spongepowered.api.item.inventory.ItemStack;
import org.spongepowered.api.text.Text;
import org.spongepowered.api.text.chat.ChatTypes;
Expand All @@ -36,14 +38,19 @@ public class SecondaryBlockListener {
public void onInteractBlockSecondaryMain(InteractBlockEvent.Secondary event, @Root Player player) {
Optional<ItemStack> itemStackOptional = player.getItemInHand(event.getHandType());
if(itemStackOptional.isPresent()) {
String blockTypeId = event.getTargetBlock().getState().getType().getId();
String blockStateId = event.getTargetBlock().getState().getId();
String itemId = itemStackOptional.get().getItem().getId();

BlockState blockState = event.getTargetBlock().getState();
String blockTypeId = blockState.getType().getId();
String blockStateId = blockState.getId();

ItemType itemType = itemStackOptional.get().getType();
String itemId = itemType.getId();

if (!player.hasPermission("protectionperms.item.use." + itemId + ".on." + blockTypeId + ".secondary") &&
!player.hasPermission("protectionperms.item.use." + itemId + ".on." + blockStateId + ".secondary")) {
event.setCancelled(true);
player.sendMessage(ChatTypes.ACTION_BAR,
Text.of(TextColors.RED, "You don't have permission to secondary use " + itemId + " on " + blockStateId + '!'));
Text.of(TextColors.RED, "You don't have permission to secondary use " + itemType.getName() + " on " + blockState.getName() + '!'));
}
}
}
Expand Down
Loading

0 comments on commit a0a8d8c

Please sign in to comment.