Util Anti Player Health Detector

Discussion in 'Resources' started by Zombieghost_391, Feb 22, 2015.

Thread Status:
Not open for further replies.
  1. Offline

    Zombieghost_391

    Hey there guys made a anti health detector and thought I would share it, always room for improvement. :3

    Code:
    public class AntiHealthCheat implements Listener{
    
        private static HashMap<Player, Integer> health = new HashMap<>();
    
        public static HashMap<Player, Integer> getHealth() {
            return health;
        }
    
        public static void setHealth(Player player, Integer health) {
            getHealth().put(player, health);
        }
    
        private Plugin pl;
    
        public AntiHealthCheat(Plugin pl) {
            this.pl = pl;
        }
    
        @EventHandler
        public void onJoin(PlayerJoinEvent e){
            Regen r = new Regen(e.getPlayer());
            Bukkit.getScheduler().runTaskTimer(pl, r, 20 * 10, 20 * 10);
        }
    
        @EventHandler
        public void onDamage(EntityDamageEvent e) {
            if (e.getEntity() instanceof Player) {
                if (!e.isCancelled()) {
                    Player p = (Player) e.getEntity();
                    setHealth(p, (int) (p.getHealth() - e.getDamage()));
                    PacketPlayOutUpdateHealth health = new PacketPlayOutUpdateHealth(getHealth().get(p), p.getFoodLevel(), p.getSaturation());
                    ((CraftPlayer) p).getHandle().playerConnection.sendPacket(health);
                    e.setDamage(0);
                    if (getHealth().get(p) == 0) {
                        p.setHealth(0);
                    }
                }
            }
        }
    
        public class Regen extends BukkitRunnable {
    
            private Player p;
    
            public Regen(Player player) {
                this.p = player;
                setHealth(player, (int) player.getMaxHealth());
            }
    
            @Override
            public void run() {
                try {
                    if (getHealth().get(p) != p.getMaxHealth()) {
                        setHealth(p, getHealth().get(p) + 1);
                        PacketPlayOutUpdateHealth health = new PacketPlayOutUpdateHealth(getHealth().get(p), p.getFoodLevel(), p.getSaturation());
                        ((CraftPlayer) p).getHandle().playerConnection.sendPacket(health);
                    }
                }catch (Exception x){
                    x.printStackTrace();
                    this.cancel();
                }
            }
        }
    
    }
    
     
    mickedplay and 000nick like this.
  2. Offline

    sethrem

    What does this do? Is this anti-regeneration?
     
  3. Offline

    Zombieghost_391

    For anyone using a mod that shows other players health, the mod with tell the "cheater" that the player they are looking at has full health, but the players real health is being tracked by the plugin. Sadly this is bad for other plugins that want the players health.
     
  4. Offline

    MisterErwin

    @Zombieghost_391 You also could fetch the Packets with ProtocolLib and change the health value...
     
  5. Offline

    MiniDigger

    any examples? ;D
     
  6. Offline

    MisterErwin

    @MiniDigger Google? :p
    And your signature: Why was your "Bettgestell" "am quitschen"? ;)
     
  7. Offline

    MiniDigger

    @MisterErwin, guess what, there are actually people using thier bed not only for sleeping ;D
     
    mrCookieSlime likes this.
  8. Offline

    xTrollxDudex

    private static HashMap<Player, Integer> health = new HashMap<>();

    Great way to have memory leaks. Static collections. That stores players.
     
  9. Offline

    jebbo

    Why is everyone so anti-static? Can you send me a link where it is explained why static is bad and cause memory leaks? I can't find anything in google.
     
  10. Offline

    teej107

    jebbo likes this.
  11. Offline

    RainoBoy97

    The main problem isnt that it's static, the main problem is that he is storing player instances and not dealing with them properly. Which will cause memory leaks.
     
  12. Offline

    xTrollxDudex

    Static fields aren't unloaded correctly. Especially with static collections, which can become very big.
     
  13. Offline

    Mr_Little_Kitty

    Do you actually have some information or a source that backs up that claim?
     
  14. Offline

    xTrollxDudex

    https://bukkit.org/threads/petition-to-remove-the-reload-command.43212/
    "All of the classes and any static variables remain in memory."

    https://bukkit.org/threads/storing-players-bad-practice.80469/
    "The real problem exists in the fact that the player object will stay referenced in your hashmap until the end of time"

    TL;DR
    Static variables aren't unloaded by the PluginClassLoader. I don't have the time to browse the source, but I do believe that the same class loader is used from the last reload. This is bad because classes may only be unloaded once the class loader is garbage collected.
    Additionally this does not remove players from the collection. When the player leaves, what happens? Everything they've done is left behind without being cleaned up. The server will hold useless amounts of memory for players that don't exist on the server, because the Player object directly references the underlying implementation of EntityPlayer, which holds all resources such as chunks, tick data, debugging messages, entity properties, protocol meta, etc...
     
    MiniDigger likes this.
  15. Offline

    nj2miami

    The issue is with how BUKKIT handles plugins and statics. Specifically on RELOAD, which is why reload is bad for servers. Stop/Start is the only sincere way to handle reloading.

    DATA reloading is totally different though, where a plugin simply needs to reread a Yaml file to import manaual updates made. There is no true reload using Bukkit and really should just be disabled as little good is done when you /reload.

    Or when you restart the server, whichever comes first. (I will go with the latter in this case.)
     
  16. Offline

    teej107


    I wouldn't exaxtly say that the issue is with Bukkit. The real issue is the poorly written plugins. I don't have a problem with using the reload command because I try and make sure the plugins I use are written well enough.
     
Thread Status:
Not open for further replies.

Share This Page