diff --git a/src/main/java/net/h31ix/anticheat/Anticheat.java b/src/main/java/net/h31ix/anticheat/Anticheat.java index 629ce48..2ab46bc 100644 --- a/src/main/java/net/h31ix/anticheat/Anticheat.java +++ b/src/main/java/net/h31ix/anticheat/Anticheat.java @@ -59,7 +59,8 @@ public class Anticheat extends JavaPlugin eventList.add(new PlayerListener()); eventList.add(new BlockListener()); eventList.add(new EntityListener()); - eventList.add(new VehicleListener()); + eventList.add(new VehicleListener()); + eventList.add(new InventoryListener()); // Order is important in some cases, don't screw with these unless needed, especially config setupConfig(); // Xray must come before events diff --git a/src/main/java/net/h31ix/anticheat/event/InventoryListener.java b/src/main/java/net/h31ix/anticheat/event/InventoryListener.java new file mode 100644 index 0000000..1015928 --- /dev/null +++ b/src/main/java/net/h31ix/anticheat/event/InventoryListener.java @@ -0,0 +1,56 @@ +/* + * AntiCheat for Bukkit. + * Copyright (C) 2012 AntiCheat Team | http://gravitydevelopment.net + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package net.h31ix.anticheat.event; + +import net.h31ix.anticheat.Anticheat; +import net.h31ix.anticheat.manage.Backend; +import net.h31ix.anticheat.manage.CheckManager; +import net.h31ix.anticheat.manage.CheckType; +import net.h31ix.anticheat.util.Configuration; +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.inventory.InventoryClickEvent; + +public class InventoryListener extends EventListener{ + + private final Backend backend = getBackend(); + private final CheckManager checkManager = getCheckManager(); + private final Configuration config = Anticheat.getManager().getConfiguration(); + + @EventHandler + public void onInventoryClick(InventoryClickEvent event) + { + if (event.getWhoClicked() instanceof Player) + { + Player player = (Player) event.getWhoClicked(); + if(checkManager.willCheck(player, CheckType.FAST_INVENTORY)) + { + if(backend.checkInventoryClicks(player)) + { + event.setCancelled(!config.silentMode()); + log("tried to use inventories too fast", player, CheckType.FAST_INVENTORY); + } + else + { + decrease(player); + } + } + } + } +} diff --git a/src/main/java/net/h31ix/anticheat/manage/Backend.java b/src/main/java/net/h31ix/anticheat/manage/Backend.java index d31936c..37aefb6 100644 --- a/src/main/java/net/h31ix/anticheat/manage/Backend.java +++ b/src/main/java/net/h31ix/anticheat/manage/Backend.java @@ -83,6 +83,9 @@ public class Backend private Map movingExempt = new HashMap(); private Map blockTime = new HashMap(); private Map blocksDropped = new HashMap(); + private Map lastInventoryTime = new HashMap(); + private Map inventoryTime = new HashMap(); + private Map inventoryChanges = new HashMap(); private Magic magic; private AnticheatManager micromanage = null; @@ -883,6 +886,53 @@ public class Backend return false; } + public boolean checkInventoryClicks(Player player) + { + String name = player.getName(); + long time = System.currentTimeMillis(); + if (!inventoryTime.containsKey(name) || !inventoryChanges.containsKey(name)) + { + inventoryTime.put(name, 0l); + if(!inventoryChanges.containsKey(name)) + { + inventoryChanges.put(name, 0); + } + } + else if (inventoryChanges.containsKey(name) && inventoryChanges.get(name) > magic.INVENTORY_MAXVIOLATIONS) + { + long diff = System.currentTimeMillis() - lastInventoryTime.get(name); + System.out.println("inventoryTime = " + inventoryTime.get(name)); + System.out.println("diff = " + diff); + if (inventoryTime.get(name) > 0 && diff < magic.INVENTORY_MAXVIOLATIONTIME) + { + inventoryTime.put(name, diff); + player.sendMessage(ChatColor.RED + "[AntiCheat] Inventory hack detected. Please wait 10 seconds before using inventories."); + return true; + } else if (inventoryTime.get(name) > 0 && diff > magic.INVENTORY_MAXVIOLATIONTIME) + { + inventoryChanges.put(name, 0); + } + } + else if (inventoryTime.containsKey(name)) + { + long last = lastInventoryTime.get(name); + long thisTime = time - last; + if (thisTime < magic.INVENTORY_TIMEMAX) + { + inventoryChanges.put(name, inventoryChanges.get(name) + 1); + if(inventoryChanges.get(name) > magic.INVENTORY_MAXVIOLATIONS) + { + inventoryTime.put(name, thisTime); + player.sendMessage(ChatColor.RED + "[AntiCheat] Inventory hack detected. Please wait 10 seconds before using inventories."); + return true; + } + } + inventoryTime.put(name, thisTime); + } + lastInventoryTime.put(name, time); + return false; + } + public void clearChatLevel(Player player) { chatLevel.remove(player.getName()); diff --git a/src/main/java/net/h31ix/anticheat/manage/CheckType.java b/src/main/java/net/h31ix/anticheat/manage/CheckType.java index 6e6bb28..b9a588f 100644 --- a/src/main/java/net/h31ix/anticheat/manage/CheckType.java +++ b/src/main/java/net/h31ix/anticheat/manage/CheckType.java @@ -52,7 +52,8 @@ public enum CheckType XRAY(Permission.CHECK_XRAY), LONG_REACH(Permission.CHECK_LONGREACH), FAST_PROJECTILE(Permission.CHECK_FASTPROJECTILE), - ITEM_SPAM(Permission.CHECK_ITEMSPAM); + ITEM_SPAM(Permission.CHECK_ITEMSPAM), + FAST_INVENTORY(Permission.CHECK_FASTINVENTORY); private final Permission permission; private final Map level = new HashMap(); diff --git a/src/main/java/net/h31ix/anticheat/util/Magic.java b/src/main/java/net/h31ix/anticheat/util/Magic.java index 2f0ae68..946badf 100644 --- a/src/main/java/net/h31ix/anticheat/util/Magic.java +++ b/src/main/java/net/h31ix/anticheat/util/Magic.java @@ -304,6 +304,18 @@ public class Magic * Maximum times a player can fail the speed check before action is taken; Type=integer, +Leniency=Increase. */ public final int SPEED_MAX; + /** + * Number of times a player can fail the inventory check before action is taken; Type=integer, +Leniency=Increase. + */ + public final int INVENTORY_MAXVIOLATIONS; + /** + * Time player is forced to wait after inventory detection; Type=integer; +Leniency=Decrease. + */ + public final int INVENTORY_MAXVIOLATIONTIME; + /** + * Time between inventory changes that triggers inventory violations; Type=integer; +Leniency=Decrease. + */ + public final int INVENTORY_TIMEMAX; private FileConfiguration defaults; private FileConfiguration magic; @@ -382,6 +394,9 @@ public class Magic XZ_SPEED_MAX_WATER = getDouble("XZ_SPEED_MAX_WATER"); XZ_SPEED_MAX_WATER_SPRINT = getDouble("XZ_SPEED_MAX_WATER_SPRINT"); SPEED_MAX = getInt("SPEED_MAX"); + INVENTORY_MAXVIOLATIONS = getInt("INVENTORY_MAXVIOLATIONS"); + INVENTORY_MAXVIOLATIONTIME = getInt("INVENTORY_MAXVIOLATIONTIME"); + INVENTORY_TIMEMAX = getInt("INVENTORY_TIMEMAX"); } private int getInt(String path) diff --git a/src/main/java/net/h31ix/anticheat/util/Permission.java b/src/main/java/net/h31ix/anticheat/util/Permission.java index 462b6ea..5fce17a 100644 --- a/src/main/java/net/h31ix/anticheat/util/Permission.java +++ b/src/main/java/net/h31ix/anticheat/util/Permission.java @@ -47,6 +47,7 @@ public enum Permission CHECK_FASTPROJECTILE, CHECK_ITEMSPAM, CHECK_VISUAL, + CHECK_FASTINVENTORY, /* System Nodes */ SYSTEM_LOG, diff --git a/src/main/resources/magic.yml b/src/main/resources/magic.yml index a84357a..3c0fc42 100644 --- a/src/main/resources/magic.yml +++ b/src/main/resources/magic.yml @@ -66,4 +66,7 @@ XZ_SPEED_MAX_POTION: 0.7 XZ_SPEED_MAX_SNEAK: 0.2 XZ_SPEED_MAX_WATER: 0.19 XZ_SPEED_MAX_WATER_SPRINT: 0.3 -SPEED_MAX: 3 \ No newline at end of file +SPEED_MAX: 3 +INVENTORY_MAXVIOLATIONS: 2 +INVENTORY_MAXVIOLATIONTIME: 10000 +INVENTORY_TIMEMAX: 100 \ No newline at end of file diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index 47909a2..51f512b 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -122,6 +122,9 @@ permissions: anticheat.check.vclip: description: Allow players to become slimeballs and clip down through blocks default: false + anticheat.check.fastinventory: + description: Allows players to use fastloot and similar inventory hacks + default: false anticheat.check.exempt: description: Total exemption from checks default: false @@ -149,4 +152,5 @@ permissions: anticheat.check.projectile: true anticheat.check.itemspam: true anticheat.check.visual: true - anticheat.check.vclip: true \ No newline at end of file + anticheat.check.vclip: true + anticheat.check.fastinventory: true \ No newline at end of file