Skip to content

Commit

Permalink
Refactor drop listener to utilize entity filtering, add non-functiona…
Browse files Browse the repository at this point in the history
…l forge and brew listeners, enable smelting listener, update to API 6.0.
  • Loading branch information
Zerthick committed May 13, 2017
1 parent 227d85e commit 9408e87
Show file tree
Hide file tree
Showing 7 changed files with 127 additions and 21 deletions.
9 changes: 5 additions & 4 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Original file line number Diff line number Diff line change
@@ -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 + '!'));
}
}
}));
}
}
}
Original file line number Diff line number Diff line change
@@ -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;
});
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<Item> 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;
});
}
}
}
Original file line number Diff line number Diff line change
@@ -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 + '!'));
}
}));
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 9408e87

Please sign in to comment.