From 81602bb5ae47851ed6f36529f49cdc2c6bd91c29 Mon Sep 17 00:00:00 2001 From: Rammelkast Date: Sun, 17 Jul 2016 20:25:38 +0200 Subject: [PATCH] Fixed NPE, added criticals check, started to change strings to UUID's in lists/maps --- .../anticheat/check/Backend.java | 55 ++++++++++++------- .../anticheat/check/CheckType.java | 3 +- .../check/combat/CriticalsCheck.java | 33 +++++++++++ .../anticheat/event/EntityListener.java | 7 ++- .../anticheat/util/Permission.java | 2 +- 5 files changed, 78 insertions(+), 22 deletions(-) create mode 100644 src/main/java/net/gravitydevelopment/anticheat/check/combat/CriticalsCheck.java diff --git a/src/main/java/net/gravitydevelopment/anticheat/check/Backend.java b/src/main/java/net/gravitydevelopment/anticheat/check/Backend.java index b92ba85..a15a4a6 100644 --- a/src/main/java/net/gravitydevelopment/anticheat/check/Backend.java +++ b/src/main/java/net/gravitydevelopment/anticheat/check/Backend.java @@ -23,6 +23,7 @@ import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; +import java.util.UUID; import org.bukkit.ChatColor; import org.bukkit.GameMode; @@ -53,9 +54,8 @@ import net.gravitydevelopment.anticheat.util.User; import net.gravitydevelopment.anticheat.util.Utilities; public class Backend { - private List isAscending = new ArrayList(); - private Map interactionCount = new HashMap(); - private Map ascensionCount = new HashMap(); + private List isAscending = new ArrayList(); + private Map ascensionCount = new HashMap(); private Map chatLevel = new HashMap(); private Map commandLevel = new HashMap(); private Map nofallViolation = new HashMap(); @@ -133,8 +133,8 @@ public class Backend { WaterWalkCheck.isInWater.remove(pN); WaterWalkCheck.isInWaterCache.remove(pN); instantBreakExempt.remove(pN); - isAscending.remove(pN); - ascensionCount.remove(pN); + isAscending.remove(player.getUniqueId()); + ascensionCount.remove(player.getUniqueId()); FlightCheck.blocksOverFlight.remove(pN); nofallViolation.remove(pN); fastBreakViolation.remove(pN); @@ -188,7 +188,7 @@ public class Backend { } public CheckResult checkProjectile(Player player) { - increment(player, projectilesShot, 10); + incrementOld(player, projectilesShot, 10); if (!projectileTime.containsKey(player.getName())) { projectileTime.put(player.getName(), System.currentTimeMillis()); return new CheckResult(CheckResult.Result.PASSED); @@ -204,7 +204,7 @@ public class Backend { } public CheckResult checkFastDrop(Player player) { - increment(player, blocksDropped, 10); + incrementOld(player, blocksDropped, 10); if (!blockTime.containsKey(player.getName())) { blockTime.put(player.getName(), System.currentTimeMillis()); return new CheckResult(CheckResult.Result.PASSED); @@ -323,7 +323,7 @@ public class Backend { max += speed > 0 ? player.getWalkSpeed() - 0.2f : 0; if (x > max || z > max) { - int num = this.increment(player, speedViolation, magic.SPEED_MAX()); + int num = this.incrementOld(player, speedViolation, magic.SPEED_MAX()); if (num >= magic.SPEED_MAX()) { return new CheckResult(CheckResult.Result.FAILED, player.getName() + "'s speed was too high " + reason + num + " times in a row (max=" + magic.SPEED_MAX() + ", speed=" + (x > z ? x : z) + ", max speed=" + max + ")"); } else { @@ -394,7 +394,7 @@ public class Backend { if (step == 1) { stepTime.put(name, System.currentTimeMillis()); } - increment(player, steps, step); + incrementOld(player, steps, step); if (step == magic.TIMER_STEP_CHECK()) { long time = System.currentTimeMillis() - stepTime.get(name); steps.put(name, 0); @@ -442,7 +442,7 @@ public class Backend { } public void logAscension(Player player, double y1, double y2) { - String name = player.getName(); + UUID name = player.getUniqueId(); if (y1 < y2 && !isAscending.contains(name)) { isAscending.add(name); } else { @@ -463,12 +463,12 @@ public class Backend { if (y1 < y2) { if (!block.getRelative(BlockFace.NORTH).isLiquid() && !block.getRelative(BlockFace.SOUTH).isLiquid() && !block.getRelative(BlockFace.EAST).isLiquid() && !block.getRelative(BlockFace.WEST).isLiquid()) { increment(player, ascensionCount, max); - if (ascensionCount.get(name) >= max) { + if (ascensionCount.get(player.getUniqueId()) >= max) { return new CheckResult(CheckResult.Result.FAILED, player.getName() + " ascended " + ascensionCount.get(name) + " times in a row (max = " + max + string + ")"); } } } else { - ascensionCount.put(name, 0); + ascensionCount.put(player.getUniqueId(), 0); } } return PASS; @@ -903,18 +903,35 @@ public class Backend { commandLevel.put(user.getName(), level + 1); } - public int increment(Player player, Map map, int num) { - String name = player.getName(); - if (map.get(name) == null) { - map.put(name, 1); + public int increment(Player player, Map ascensionCount2, int num) { + UUID name = player.getUniqueId(); + if (ascensionCount2.get(name) == null) { + ascensionCount2.put(name, 1); return 1; } else { - int amount = map.get(name) + 1; + int amount = ascensionCount2.get(name) + 1; if (amount < num + 1) { - map.put(name, amount); + ascensionCount2.put(name, amount); return amount; } else { - map.put(name, num); + ascensionCount2.put(name, num); + return num; + } + } + } + + public int incrementOld(Player player, Map ascensionCount2, int num) { + String name = player.getName(); + if (ascensionCount2.get(name) == null) { + ascensionCount2.put(name, 1); + return 1; + } else { + int amount = ascensionCount2.get(name) + 1; + if (amount < num + 1) { + ascensionCount2.put(name, amount); + return amount; + } else { + ascensionCount2.put(name, num); return num; } } diff --git a/src/main/java/net/gravitydevelopment/anticheat/check/CheckType.java b/src/main/java/net/gravitydevelopment/anticheat/check/CheckType.java index a91fd28..4df1e8a 100644 --- a/src/main/java/net/gravitydevelopment/anticheat/check/CheckType.java +++ b/src/main/java/net/gravitydevelopment/anticheat/check/CheckType.java @@ -60,7 +60,8 @@ public enum CheckType { AUTOTOOL(Permission.CHECK_AUTOTOOL), KILLAURA(Permission.CHECK_KILLAURA), BLINK(Permission.CHECK_BLINK), - VELOCITY(Permission.VELOCITY); + VELOCITY(Permission.VELOCITY), + CRITICALS(Permission.CRITICALS); private final Permission permission; private final Map level = new HashMap(); diff --git a/src/main/java/net/gravitydevelopment/anticheat/check/combat/CriticalsCheck.java b/src/main/java/net/gravitydevelopment/anticheat/check/combat/CriticalsCheck.java new file mode 100644 index 0000000..910f4f9 --- /dev/null +++ b/src/main/java/net/gravitydevelopment/anticheat/check/combat/CriticalsCheck.java @@ -0,0 +1,33 @@ +package net.gravitydevelopment.anticheat.check.combat; + +import org.bukkit.Material; +import org.bukkit.entity.Player; +import org.bukkit.event.entity.EntityDamageByEntityEvent; +import org.bukkit.event.entity.EntityDamageEvent.DamageCause; +import org.bukkit.potion.PotionEffectType; + +import net.gravitydevelopment.anticheat.AntiCheat; +import net.gravitydevelopment.anticheat.check.CheckResult; +import net.gravitydevelopment.anticheat.check.CheckType; +import net.gravitydevelopment.anticheat.event.EventListener; +import net.gravitydevelopment.anticheat.util.Utilities; + +public class CriticalsCheck { + + public static void doDamageEvent(EntityDamageByEntityEvent e, Player damager) { + if (!(e.getDamager() instanceof Player) || e.getCause() != DamageCause.ENTITY_ATTACK) + return; + Player p = (Player)e.getDamager(); + if (isCritical(p)) { + if ((p.getLocation().getY() % 1.0 == 0 || p.getLocation().getY() % 0.5 == 0) && p.getLocation().clone().subtract(0, 1.0, 0).getBlock().getType().isSolid()) { + e.setCancelled(true); + EventListener.log(new CheckResult(CheckResult.Result.FAILED, p.getName() + " failed Criticals, tried to do a critical without needed conditions").getMessage(), p, CheckType.CRITICALS); + } + } + } + + private static boolean isCritical(Player player) { + return player.getFallDistance() > 0.0f && !player.isOnGround() && !player.isInsideVehicle() && !player.hasPotionEffect(PotionEffectType.BLINDNESS) && !Utilities.isHoveringOverWater(player.getLocation()) && player.getEyeLocation().getBlock().getType() != Material.LADDER; + } + +} diff --git a/src/main/java/net/gravitydevelopment/anticheat/event/EntityListener.java b/src/main/java/net/gravitydevelopment/anticheat/event/EntityListener.java index 4f2b061..2f6923e 100644 --- a/src/main/java/net/gravitydevelopment/anticheat/event/EntityListener.java +++ b/src/main/java/net/gravitydevelopment/anticheat/event/EntityListener.java @@ -35,6 +35,7 @@ import org.bukkit.event.entity.FoodLevelChangeEvent; import net.gravitydevelopment.anticheat.AntiCheat; import net.gravitydevelopment.anticheat.check.CheckResult; import net.gravitydevelopment.anticheat.check.CheckType; +import net.gravitydevelopment.anticheat.check.combat.CriticalsCheck; import net.gravitydevelopment.anticheat.check.combat.KillAuraCheck; import net.gravitydevelopment.anticheat.check.combat.VelocityCheck; import net.gravitydevelopment.anticheat.util.Distance; @@ -104,7 +105,11 @@ public class EntityListener extends EventListener { if (event instanceof EntityDamageByEntityEvent) { EntityDamageByEntityEvent e = (EntityDamageByEntityEvent) event; if (e.getDamager() instanceof Player) { - KillAuraCheck.doDamageEvent(e, (Player)e.getDamager()); + if (getCheckManager().willCheck((Player)e.getDamager(), CheckType.KILLAURA)) { + KillAuraCheck.doDamageEvent(e, (Player)e.getDamager()); + }else if (getCheckManager().willCheck((Player)e.getDamager(), CheckType.CRITICALS)) { + CriticalsCheck.doDamageEvent(e, (Player)e.getDamager()); + } } if (event.getEntity() instanceof Player) { Player player = (Player) event.getEntity(); diff --git a/src/main/java/net/gravitydevelopment/anticheat/util/Permission.java b/src/main/java/net/gravitydevelopment/anticheat/util/Permission.java index 45b68be..63648bf 100644 --- a/src/main/java/net/gravitydevelopment/anticheat/util/Permission.java +++ b/src/main/java/net/gravitydevelopment/anticheat/util/Permission.java @@ -61,7 +61,7 @@ public enum Permission { SYSTEM_CALIBRATE, SYSTEM_CHECK, SYSTEM_DEBUG, - SYSTEM_RELOAD, CHECK_KILLAURA, CHECK_BLINK, VELOCITY; + SYSTEM_RELOAD, CHECK_KILLAURA, CHECK_BLINK, VELOCITY, CRITICALS; private static final String PERMISSION_ALL = "anticheat.*"; private static final String PERMISSION_SPAM = "anticheat.check.spam";