From 98a3a34b5df0612206c87b8b1e2223c1cba62c3e Mon Sep 17 00:00:00 2001 From: Rammelkast Date: Thu, 26 May 2016 16:47:08 +0200 Subject: [PATCH] Started with moving checks to their own classes, started with flight --- .../anticheat/check/Backend.java | 82 +++++++------------ .../anticheat/check/movement/FlightCheck.java | 79 ++++++++++++++++++ .../command/executors/CommandHelp.java | 20 ++--- .../anticheat/event/PlayerListener.java | 38 ++++++--- 4 files changed, 147 insertions(+), 72 deletions(-) create mode 100644 src/main/java/net/gravitydevelopment/anticheat/check/movement/FlightCheck.java diff --git a/src/main/java/net/gravitydevelopment/anticheat/check/Backend.java b/src/main/java/net/gravitydevelopment/anticheat/check/Backend.java index 8f8c08f..b969209 100644 --- a/src/main/java/net/gravitydevelopment/anticheat/check/Backend.java +++ b/src/main/java/net/gravitydevelopment/anticheat/check/Backend.java @@ -18,14 +18,12 @@ package net.gravitydevelopment.anticheat.check; -import net.gravitydevelopment.anticheat.AntiCheat; -import net.gravitydevelopment.anticheat.config.Configuration; -import net.gravitydevelopment.anticheat.config.providers.Lang; -import net.gravitydevelopment.anticheat.config.providers.Magic; -import net.gravitydevelopment.anticheat.manage.AntiCheatManager; -import net.gravitydevelopment.anticheat.util.User; -import net.gravitydevelopment.anticheat.util.Distance; -import net.gravitydevelopment.anticheat.util.Utilities; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; + import org.bukkit.ChatColor; import org.bukkit.GameMode; import org.bukkit.Location; @@ -38,7 +36,15 @@ import org.bukkit.entity.Player; import org.bukkit.event.player.PlayerToggleSprintEvent; import org.bukkit.potion.PotionEffectType; -import java.util.*; +import net.gravitydevelopment.anticheat.AntiCheat; +import net.gravitydevelopment.anticheat.check.movement.FlightCheck; +import net.gravitydevelopment.anticheat.config.Configuration; +import net.gravitydevelopment.anticheat.config.providers.Lang; +import net.gravitydevelopment.anticheat.config.providers.Magic; +import net.gravitydevelopment.anticheat.manage.AntiCheatManager; +import net.gravitydevelopment.anticheat.util.Distance; +import net.gravitydevelopment.anticheat.util.User; +import net.gravitydevelopment.anticheat.util.Utilities; public class Backend { private List isInWater = new ArrayList(); @@ -46,7 +52,6 @@ public class Backend { private List isAscending = new ArrayList(); private Map interactionCount = new HashMap(); private Map ascensionCount = new HashMap(); - private Map blocksOverFlight = new HashMap(); private Map chatLevel = new HashMap(); private Map commandLevel = new HashMap(); private Map nofallViolation = new HashMap(); @@ -77,7 +82,6 @@ public class Backend { private Map sprinted = new HashMap(); private Map brokenBlock = new HashMap(); private Map placedBlock = new HashMap(); - private Map movingExempt = new HashMap(); private Map blockTime = new HashMap(); private Map blocksDropped = new HashMap(); private Map lastInventoryTime = new HashMap(); @@ -100,6 +104,10 @@ public class Backend { lang = manager.getConfiguration().getLang(); transparent.add((byte) -1); } + + public Magic getMagic() { + return magic; + } public void updateConfig(Configuration config) { magic = config.getMagic(); @@ -115,7 +123,7 @@ public class Backend { blocksDropped.remove(pN); blockTime.remove(pN); - movingExempt.remove(pN); + FlightCheck.movingExempt.remove(pN); brokenBlock.remove(pN); placedBlock.remove(pN); bowWindUp.remove(pN); @@ -127,7 +135,7 @@ public class Backend { instantBreakExempt.remove(pN); isAscending.remove(pN); ascensionCount.remove(pN); - blocksOverFlight.remove(pN); + FlightCheck.blocksOverFlight.remove(pN); nofallViolation.remove(pN); fastBreakViolation.remove(pN); yAxisViolations.remove(pN); @@ -155,7 +163,7 @@ public class Backend { sprinted.remove(pN); brokenBlock.remove(pN); placedBlock.remove(pN); - movingExempt.remove(pN); + FlightCheck.movingExempt.remove(pN); blockTime.remove(pN); blocksDropped.remove(pN); lastInventoryTime.remove(pN); @@ -557,36 +565,6 @@ public class Backend { return PASS; } - public CheckResult checkFlight(Player player, Distance distance) { - if (distance.getYDifference() > magic.TELEPORT_MIN()) { - // This was a teleport, so we don't care about it. - return PASS; - } - final String name = player.getName(); - final double y1 = distance.fromY(); - final double y2 = distance.toY(); - if (!isMovingExempt(player) && !Utilities.isHoveringOverWater(player.getLocation(), 1) && Utilities.cantStandAtExp(player.getLocation()) && Utilities.blockIsnt(player.getLocation().getBlock().getRelative(BlockFace.DOWN), new Material[]{Material.FENCE, Material.FENCE_GATE, Material.COBBLE_WALL})) { - - if (!blocksOverFlight.containsKey(name)) { - blocksOverFlight.put(name, 0D); - } - - blocksOverFlight.put(name, (blocksOverFlight.get(name) + distance.getXDifference() + distance.getYDifference() + distance.getZDifference())); - - if (y1 > y2) { - blocksOverFlight.put(name, (blocksOverFlight.get(name) - distance.getYDifference())); - } - - if (blocksOverFlight.get(name) > magic.FLIGHT_BLOCK_LIMIT() && (y1 <= y2)) { - return new CheckResult(CheckResult.Result.FAILED, player.getName() + " flew over " + blocksOverFlight.get(name) + " blocks (max=" + magic.FLIGHT_BLOCK_LIMIT() + ")"); - } - } else { - blocksOverFlight.put(name, 0D); - } - - return PASS; - } - public void logAscension(Player player, double y1, double y2) { String name = player.getName(); if (y1 < y2 && !isAscending.contains(name)) { @@ -991,24 +969,24 @@ public class Backend { break; } - movingExempt.put(player.getName(), System.currentTimeMillis() + time); + FlightCheck.movingExempt.put(player.getName(), System.currentTimeMillis() + time); // Only map in which termination time is calculated beforehand. } public void logEnterExit(final Player player) { - movingExempt.put(player.getName(), System.currentTimeMillis() + magic.ENTERED_EXITED_TIME()); + FlightCheck.movingExempt.put(player.getName(), System.currentTimeMillis() + magic.ENTERED_EXITED_TIME()); } public void logToggleSneak(final Player player) { - movingExempt.put(player.getName(), System.currentTimeMillis() + magic.SNEAK_TIME()); + FlightCheck.movingExempt.put(player.getName(), System.currentTimeMillis() + magic.SNEAK_TIME()); } public void logTeleport(final Player player) { - movingExempt.put(player.getName(), System.currentTimeMillis() + magic.TELEPORT_TIME()); + FlightCheck.movingExempt.put(player.getName(), System.currentTimeMillis() + magic.TELEPORT_TIME()); /* Data for fly/speed should be reset */ nofallViolation.remove(player.getName()); - blocksOverFlight.remove(player.getName()); + FlightCheck.blocksOverFlight.remove(player.getName()); yAxisViolations.remove(player.getName()); yAxisLastViolation.remove(player.getName()); lastYcoord.remove(player.getName()); @@ -1016,15 +994,15 @@ public class Backend { } public void logExitFly(final Player player) { - movingExempt.put(player.getName(), System.currentTimeMillis() + magic.EXIT_FLY_TIME()); + FlightCheck.movingExempt.put(player.getName(), System.currentTimeMillis() + magic.EXIT_FLY_TIME()); } public void logJoin(final Player player) { - movingExempt.put(player.getName(), System.currentTimeMillis() + magic.JOIN_TIME()); + FlightCheck.movingExempt.put(player.getName(), System.currentTimeMillis() + magic.JOIN_TIME()); } public boolean isMovingExempt(Player player) { - return isDoing(player, movingExempt, -1); + return isDoing(player, FlightCheck.movingExempt, -1); } public boolean isAscending(Player player) { diff --git a/src/main/java/net/gravitydevelopment/anticheat/check/movement/FlightCheck.java b/src/main/java/net/gravitydevelopment/anticheat/check/movement/FlightCheck.java new file mode 100644 index 0000000..11b72d9 --- /dev/null +++ b/src/main/java/net/gravitydevelopment/anticheat/check/movement/FlightCheck.java @@ -0,0 +1,79 @@ +package net.gravitydevelopment.anticheat.check.movement; + +import java.util.HashMap; +import java.util.Map; + +import org.bukkit.Material; +import org.bukkit.block.BlockFace; +import org.bukkit.entity.Player; + +import net.gravitydevelopment.anticheat.AntiCheat; +import net.gravitydevelopment.anticheat.check.CheckResult; +import net.gravitydevelopment.anticheat.util.Distance; +import net.gravitydevelopment.anticheat.util.Utilities; + +public class FlightCheck { + + private static final CheckResult PASS = new CheckResult(CheckResult.Result.PASSED); + + public static Map blocksOverFlight = new HashMap(); + public static Map movingExempt = new HashMap(); + + public static CheckResult runCheck(Player player, Distance distance) { + if (distance.getYDifference() > AntiCheat.getManager().getBackend().getMagic().TELEPORT_MIN()) { + // This was a teleport, so we don't care about it. + return PASS; + } + final String name = player.getName(); + final double y1 = distance.fromY(); + final double y2 = distance.toY(); + if (!isMovingExempt(player) && !Utilities.isHoveringOverWater(player.getLocation(), 1) && Utilities.cantStandAtExp(player.getLocation()) && Utilities.blockIsnt(player.getLocation().getBlock().getRelative(BlockFace.DOWN), new Material[]{Material.FENCE, Material.FENCE_GATE, Material.COBBLE_WALL})) { + + if (!blocksOverFlight.containsKey(name)) { + blocksOverFlight.put(name, 0D); + } + + blocksOverFlight.put(name, (blocksOverFlight.get(name) + distance.getXDifference() + distance.getYDifference() + distance.getZDifference())); + + if (y1 > y2) { + blocksOverFlight.put(name, (blocksOverFlight.get(name) - distance.getYDifference())); + } + + if (blocksOverFlight.get(name) > AntiCheat.getManager().getBackend().getMagic().FLIGHT_BLOCK_LIMIT() && (y1 <= y2)) { + return new CheckResult(CheckResult.Result.FAILED, player.getName() + " flew over " + blocksOverFlight.get(name) + " blocks (max=" + AntiCheat.getManager().getBackend().getMagic().FLIGHT_BLOCK_LIMIT() + ")"); + } + } else { + blocksOverFlight.put(name, 0D); + } + + return PASS; + } + + private static boolean isMovingExempt(Player player) { + return isDoing(player, movingExempt, -1); + } + + private static boolean isDoing(Player player, Map map, double max) { + if (map.containsKey(player.getName())) { + if (max != -1) { + if (((System.currentTimeMillis() - map.get(player.getName())) / 1000) > max) { + map.remove(player.getName()); + return false; + } else { + return true; + } + } else { + // Termination time has already been calculated + if (map.get(player.getName()) < System.currentTimeMillis()) { + map.remove(player.getName()); + return false; + } else { + return true; + } + } + } else { + return false; + } + } + +} diff --git a/src/main/java/net/gravitydevelopment/anticheat/command/executors/CommandHelp.java b/src/main/java/net/gravitydevelopment/anticheat/command/executors/CommandHelp.java index 9e644e4..bcac64c 100644 --- a/src/main/java/net/gravitydevelopment/anticheat/command/executors/CommandHelp.java +++ b/src/main/java/net/gravitydevelopment/anticheat/command/executors/CommandHelp.java @@ -24,20 +24,20 @@ import org.bukkit.command.CommandSender; public class CommandHelp extends CommandBase { - private static final String NAME = "AntiCheat Help"; + private static final String NAME = "AntiCheatReloaded Help"; private static final String COMMAND = "help"; private static final String USAGE = "anticheat help"; private static final Permission PERMISSION = Permission.SYSTEM_HELP; private static final String[] HELP = { - GRAY + "/anti " + AQUA + "help", - GRAY + "/anti " + AQUA + "reload", - GRAY + "/anti " + AQUA + "update", - GRAY + "/anti " + AQUA + "debug " + AQUA + "", - GRAY + "/anti " + AQUA + "check " + GOLD + "[check] [on/off]", - GRAY + "/anti " + AQUA + "log " + GOLD + "[file/console] [on/off]", - GRAY + "/anti " + AQUA + "report " + GOLD + "[group/user]", - GRAY + "/anti " + AQUA + "reset " + GOLD + "[user]", - GRAY + "/anti " + AQUA + "spy " + GOLD + "[user]", + RED + "/anti " + GRAY + "help", + RED + "/anti " + GRAY + "reload", + RED + "/anti " + GRAY + "update", + RED + "/anti " + GRAY + "debug " + WHITE + "", + RED + "/anti " + GRAY + "check " + WHITE + "[check] [on/off]", + RED + "/anti " + GRAY + "log " + WHITE + "[file/console] [on/off]", + RED + "/anti " + GRAY + "report " + WHITE + "[group/user]", + RED + "/anti " + GRAY + "reset " + WHITE + "[user]", + RED + "/anti " + GRAY + "spy " + WHITE + "[user]", }; public CommandHelp() { diff --git a/src/main/java/net/gravitydevelopment/anticheat/event/PlayerListener.java b/src/main/java/net/gravitydevelopment/anticheat/event/PlayerListener.java index 3ebc2a6..bba189f 100644 --- a/src/main/java/net/gravitydevelopment/anticheat/event/PlayerListener.java +++ b/src/main/java/net/gravitydevelopment/anticheat/event/PlayerListener.java @@ -18,14 +18,6 @@ package net.gravitydevelopment.anticheat.event; -import net.gravitydevelopment.anticheat.AntiCheat; -import net.gravitydevelopment.anticheat.check.CheckType; -import net.gravitydevelopment.anticheat.util.User; -import net.gravitydevelopment.anticheat.check.CheckResult; -import net.gravitydevelopment.anticheat.util.Distance; -import net.gravitydevelopment.anticheat.util.Permission; -import net.gravitydevelopment.anticheat.util.Utilities; - import org.bukkit.Bukkit; import org.bukkit.ChatColor; import org.bukkit.GameMode; @@ -38,10 +30,36 @@ import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; import org.bukkit.event.block.Action; import org.bukkit.event.entity.ProjectileLaunchEvent; -import org.bukkit.event.player.*; +import org.bukkit.event.player.AsyncPlayerChatEvent; +import org.bukkit.event.player.PlayerAnimationEvent; +import org.bukkit.event.player.PlayerBedEnterEvent; +import org.bukkit.event.player.PlayerBedLeaveEvent; +import org.bukkit.event.player.PlayerChangedWorldEvent; +import org.bukkit.event.player.PlayerCommandPreprocessEvent; +import org.bukkit.event.player.PlayerDropItemEvent; +import org.bukkit.event.player.PlayerGameModeChangeEvent; +import org.bukkit.event.player.PlayerInteractEvent; +import org.bukkit.event.player.PlayerJoinEvent; +import org.bukkit.event.player.PlayerKickEvent; +import org.bukkit.event.player.PlayerMoveEvent; +import org.bukkit.event.player.PlayerQuitEvent; +import org.bukkit.event.player.PlayerTeleportEvent; import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause; +import org.bukkit.event.player.PlayerToggleFlightEvent; +import org.bukkit.event.player.PlayerToggleSneakEvent; +import org.bukkit.event.player.PlayerToggleSprintEvent; +import org.bukkit.event.player.PlayerVelocityEvent; import org.bukkit.inventory.PlayerInventory; +import net.gravitydevelopment.anticheat.AntiCheat; +import net.gravitydevelopment.anticheat.check.CheckResult; +import net.gravitydevelopment.anticheat.check.CheckType; +import net.gravitydevelopment.anticheat.check.movement.FlightCheck; +import net.gravitydevelopment.anticheat.util.Distance; +import net.gravitydevelopment.anticheat.util.Permission; +import net.gravitydevelopment.anticheat.util.User; +import net.gravitydevelopment.anticheat.util.Utilities; + public class PlayerListener extends EventListener { @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) @@ -317,7 +335,7 @@ public class PlayerListener extends EventListener { } } if (getCheckManager().willCheckQuick(player, CheckType.FLY) && !player.isFlying() && (Bukkit.getVersion().contains("1.9") && !player.isGliding()) /*TODO: ElytraFly check, this is a quick workaround */) { - CheckResult result = getBackend().checkFlight(player, distance); + CheckResult result = FlightCheck.runCheck(player, distance); if (result.failed()) { if (!silentMode()) { event.setTo(user.getGoodLocation(from.clone()));