Solved Nothing

Discussion in 'Spigot Plugin Development' started by Chronuak, Feb 15, 2020.

  1. public class Shield implements Listener{

    HashMap<Player, BukkitRunnable> run = new HashMap<>();

    @EventHandler
    public void onInteract(PlayerInteractEvent e) {
    Player p = e.getPlayer();
    try {
    if(p.getWorld().getName().equalsIgnoreCase(Main.world)){
    if(e.getItem().getItemMeta().getDisplayName().equalsIgnoreCase("§6§1Shield")) {
    if(e.getAction().equals(Action.RIGHT_CLICK_AIR) || e.getAction().equals(Action.RIGHT_CLICK_BLOCK)) {
    e.setCancelled(true);
    if(run.containsKey(p)) {
    p.sendMessage(Main.pr + "§5Schild deaktiviert!");
    run.get(p).cancel();
    run.remove(p);
    }else {
    run.put(p, new BukkitRunnable() {
    @Override
    public void run() {
    p.getWorld().playEffect(p.getLocation(), Effect.ENDER_SIGNAL, 3);
    }
    });
    run.get(p).runTaskTimer(Main.getInstance(), 20, 20);
    p.sendMessage(Main.pr + "§5Schild aktiviert!");
    }
    }
    }
    }
    }catch (NullPointerException e6) {

    }
    }
    }
     
  2. SteelPhoenix

    Moderator

    This is not a resource and please put your code in code tags.

    1) What's with the public static field access?
    2) Compare enum values using ==
    3) Why are you catching a NullPointerException?
    4) Use the ChatColor enum
    5) Use player UUIDs instead of the player object
    6) Why does your field have package visibility?
    7) Apply Liskov Substitution Principle where possible
     
    • Like Like x 1
  3. 2) You can use either for enums, it doesn't matter. (specifically for enums)
     
  4. SteelPhoenix

    Moderator

    Using == slightly increases performance and is null safe
     
    • Like Like x 1
  5. You can put code in code tags for easier readability. :coffee:

    [​IMG]
     
  6. Storing the player and a bukkitrunnable in an hashmap is heavy. Store the player name (and remove it on disconnect) or the uuid and the bukkitrunnable task id.
     
  7. Also is there a specific reason you dont want it to work now or just turn it off completly?

    If thats the case you can disable it's recipe and make some checks to delete them if anyone somehow gets one
     
  8. Thanks for the replys.
    However I still dont really know what I could do to solve my problem.
    My problem is, that I can activate the shield, however if I click on it another time, I only get the message, that it was activated an not
    disabled.
    Here is my hole code if you need it.

    Code (Java):
    public class Shield implements Listener{
       
        HashMap<Player, BukkitRunnable> run = new HashMap<>();
       
        @EventHandler
        public void onInteract(PlayerInteractEvent e) {
            Player p = e.getPlayer();
            if(p.getWorld().getName().equalsIgnoreCase(Main.world)){
                if(e.getItem().getItemMeta().getDisplayName().equalsIgnoreCase("§6§1Shield")) {
                    if(e.getAction().equals(Action.RIGHT_CLICK_AIR) || e.getAction().equals(Action.RIGHT_CLICK_BLOCK)) {
                        e.setCancelled(true);
                       
                        if(run.containsKey(p)) {
                            p.sendMessage(Main.pr + "§5Schild deaktiviert!");
                            run.get(p).cancel();
                            run.remove(p);
                        }else if(!run.containsKey(p)) {
                            run.put(p, new BukkitRunnable() {
                                @Override
                                public void run() {
                                    p.getWorld().playEffect(p.getLocation(), Effect.ENDER_SIGNAL, 3);
                                   
                                }
                            });
                            run.get(p).runTaskTimer(Main.getInstance(), 20, 20);
                            p.sendMessage(Main.pr + "§5Schild aktiviert!");
                        }
                    }
                }
            }
        }
        @EventHandler
        public void onQuit(PlayerInteractEvent e) {
            if(run.containsKey(e.getPlayer())) {
                run.get(e.getPlayer()).cancel();
                run.remove(e.getPlayer());
            }
        }
        @EventHandler
        public void onMove(PlayerMoveEvent e) {
            Player p = e.getPlayer();
           
            for(Player players : run.keySet()) {
                if(p != players) {
                    if(p.getLocation().distance(players.getLocation()) <= 4) {
                        if(p != players && !p.hasPermission("lobby.shield.bypass")) {
                        double Ax = p.getLocation().getX();
                        double Ay = p.getLocation().getY();
                        double Az = p.getLocation().getZ();
                       
                        double Bx = players.getLocation().getX();
                        double By = players.getLocation().getY();
                        double Bz = players.getLocation().getZ();
                       
                        double x = Ax - Bx;
                        double y = Ay - By;
                        double z = Az - Bz;
                        Vector v = new Vector(x, y, z).multiply(0.5D).normalize();
                        p.setVelocity(v);
                        }
                    }
                }
            }
            if(run.containsKey(p)) {
            for(Entity entity : p.getNearbyEntities(4, 4, 4)) {
                if(entity instanceof Player) {
                    Player target = (Player) entity;
                        if(!(target.hasPermission("lobby.shield.bypass"))) {
                        double Ax = p.getLocation().getX();
                        double Ay = p.getLocation().getY();
                        double Az = p.getLocation().getZ();
                       
                        double Bx = target.getLocation().getX();
                        double By = target.getLocation().getY();
                        double Bz = target.getLocation().getZ();
                       
                        double x = Bx - Ax;
                        double y = By - Ay;
                        double z = Bz - Az;
                        Vector v = new Vector(x, y, z).multiply(0.5D).normalize();
                        target.setVelocity(v);
                        }
                    }
                }
            }
        }
    }
     
  9. As I told you, store the player uuid and the bukkitrunnabloe ID in the hashmap.
     
  10. And then it will work?
     
  11. Cant know until you test it
     
  12. thats not how maps work.. maps hold references, you can hold supermassive objects and theres no performance difference except in how #equals or #hashCode calculates. the size of the object makes no difference.

    @op so to understand your issue, it only displays your activation message, and it only runs the task on the first time?
     
  13. SteelPhoenix

    Moderator

    But it will cause memory leaks if not dealt with properly - player objects should not stay referenced if the player is offline.
     
    • Like Like x 1
  14. any object used for players will cause memory leaks. if its a player object or a player name. if the player is offline, its a memory leak anyway. this is resolved by removing and readding on quit/join, not by going through the tedious nature of looking up the map for the player every time you wish to access the player object.
    you could make the argument that a player object isnt necessary and you just need a unique identifying value, which uuid would indeed be better, but that doesnt really correlate with what i was talking about. and it still doesnt deny that he was wrong in how maps work.

    /e incase what i mean is unclear: if you have a map with a uuid key and a configuration value on a hub server where youll have 1000s of players come through, youll have memory leaks. it doesnt matter if its a player object or a uuid. it needs to be removed when the player leaves anyway. so using a player object is fine. using a uuid key is okay too. as long as theyre removed from the map when they leave. it doesnt correlate to the player object itself.
     
    #14 Warren1001, Feb 15, 2020
    Last edited: Feb 15, 2020