Improved itemdupe check + reliable fix for the underlying problem

This commit is contained in:
Evenprime 2011-04-26 21:04:06 +02:00
parent d0ba809f2a
commit 85a52c5fbf
4 changed files with 87 additions and 3 deletions

View File

@ -3,7 +3,7 @@ name: NoCheat
author: Evenprime
main: cc.co.evenprime.bukkit.nocheat.NoCheat
version: 0.8.9
version: 0.9
commands:
nocheat:

View File

@ -3,17 +3,24 @@ package cc.co.evenprime.bukkit.nocheat.checks;
import java.util.logging.Level;
import org.bukkit.Bukkit;
import org.bukkit.craftbukkit.entity.CraftPlayer;
import org.bukkit.entity.Item;
import org.bukkit.entity.Player;
import org.bukkit.event.Event;
import org.bukkit.event.Listener;
import org.bukkit.event.Event.Priority;
import org.bukkit.event.entity.EntityDeathEvent;
import org.bukkit.event.player.PlayerDropItemEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.event.player.PlayerInventoryEvent;
import org.bukkit.event.player.PlayerPickupItemEvent;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.plugin.PluginManager;
import cc.co.evenprime.bukkit.nocheat.NoCheat;
import cc.co.evenprime.bukkit.nocheat.data.PermissionData;
import cc.co.evenprime.bukkit.nocheat.listeners.ItemdupeEntityListener;
import cc.co.evenprime.bukkit.nocheat.listeners.ItemdupePlayerListener;
public class ItemdupeCheck extends Check {
@ -34,7 +41,9 @@ public class ItemdupeCheck extends Check {
if(s.getAmount() <= 0) {// buggy item
event.getItem().remove();
event.setCancelled(true);
plugin.log(Level.WARNING, event.getPlayer().getName() + " tried to pick up an invalid item. Item will be removed now.");
plugin.log(Level.WARNING, event.getPlayer().getName() + " tried to pick up an invalid item. Item was removed.");
cleanPlayerInventory(event.getPlayer());
}
}
}
@ -46,8 +55,43 @@ public class ItemdupeCheck extends Check {
if(event.hasItem() && event.getItem().getAmount() <= 0) {// buggy item
event.setCancelled(true);
plugin.log(Level.WARNING, event.getPlayer().getName() + " tried to use an invalid item. Item will be removed now.");
plugin.log(Level.WARNING, event.getPlayer().getName() + " tried to use an invalid item. Item was removed.");
event.getPlayer().getInventory().remove(event.getItem());
cleanPlayerInventory(event.getPlayer());
}
}
public void check(PlayerDropItemEvent event) {
if(hasPermission(event.getPlayer())) return;
Item item = event.getItemDrop();
if(item.getItemStack() != null) {
ItemStack stack = item.getItemStack();
if(stack.getAmount() <= 0) {
plugin.log(Level.WARNING, event.getPlayer().getName() + " tried to drop an invalid item. Dropped item was changed to dirt.");
stack.setTypeId(3); // dirt
stack.setAmount(1);
cleanPlayerInventory(event.getPlayer());
}
}
}
private void cleanPlayerInventory(Player player) {
Inventory inv = player.getInventory();
ItemStack stacks[] = inv.getContents();
for(int i = 0; i < stacks.length; i++) {
if(stacks[i] != null && stacks[i].getAmount() <= 0) {
inv.clear(i);
System.out.println("Removed illegal item from inventory of " + player.getName());
}
}
}
@ -60,8 +104,18 @@ public class ItemdupeCheck extends Check {
// Register listeners for itemdupe check
pm.registerEvent(Event.Type.PLAYER_PICKUP_ITEM, itemdupePlayerListener, Priority.Lowest, plugin);
pm.registerEvent(Event.Type.ENTITY_DEATH, new ItemdupeEntityListener(this), Priority.Lowest, plugin);
pm.registerEvent(Event.Type.PLAYER_DROP_ITEM, itemdupePlayerListener, Priority.Lowest, plugin);
pm.registerEvent(Event.Type.PLAYER_INTERACT, itemdupePlayerListener, Priority.Lowest, plugin);
}
public void check(EntityDeathEvent event) {
if(event.getEntity() instanceof CraftPlayer) {
if(hasPermission((CraftPlayer)event.getEntity())) return;
((CraftPlayer)event.getEntity()).getHandle().x(); // close all inventory screens
}
}
}

View File

@ -0,0 +1,21 @@
package cc.co.evenprime.bukkit.nocheat.listeners;
import org.bukkit.event.entity.EntityDeathEvent;
import org.bukkit.event.entity.EntityListener;
import cc.co.evenprime.bukkit.nocheat.checks.ItemdupeCheck;
public class ItemdupeEntityListener extends EntityListener {
ItemdupeCheck check;
public ItemdupeEntityListener(ItemdupeCheck itemdupeCheck) {
check = itemdupeCheck;
}
@Override
public void onEntityDeath(EntityDeathEvent event) {
check.check(event);
}
}

View File

@ -1,5 +1,6 @@
package cc.co.evenprime.bukkit.nocheat.listeners;
import org.bukkit.event.player.PlayerDropItemEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.event.player.PlayerListener;
import org.bukkit.event.player.PlayerPickupItemEvent;
@ -20,9 +21,17 @@ public class ItemdupePlayerListener extends PlayerListener {
if(!event.isCancelled()) check.check(event);
}
@Override
public void onPlayerDropItem(PlayerDropItemEvent event) {
check.check(event);
}
@Override
public void onPlayerInteract(PlayerInteractEvent event) {
if(!event.isCancelled()) check.check(event);
}
}