Arraylist

Discussion in 'Plugin Development' started by Light__WOLF, Jul 29, 2020.

  1. Offline

    Light__WOLF

    public class MainEffekte implements CommandExecutor, Listener{







    ArrayList<UUID> ekfeuer = new ArrayList<>();





    @Override
    public boolean onCommand(CommandSender cs, Command cmd, String label, String[] args) {
    Player p = (Player) cs;
    if(cmd.getName().equalsIgnoreCase("effekt")){
    if(args[0].equalsIgnoreCase("feuer")) {
    if(args.length == 1) {
    if(p.hasPermission("GlowStoneMC.effekte")) {
    if(ekfeuer.contains(p.getUniqueId())) {

    ekfeuer.remove(p.getUniqueId());
    p.sendMessage(ChatColor.RED + "DEBUG="+ ChatColor.WHITE + "gelöscht");
    return true;
    }else {

    ekfeuer.add(p.getUniqueId());
    p.sendMessage(ChatColor.RED + "DEBUG="+ ChatColor.WHITE + "hinzugefügt");
    return true;
    }
    }
    }
    }
    }

    return false;
    }

    @EventHandler
    public void ekfeuer(PlayerMoveEvent e) {
    Player p = e.getPlayer();
    if(ekfeuer.contains(p.getUniqueId())) {
    p.sendMessage("t");
    }

    }





    }




    what do i wrong ? the eventhandler wont work i did register it
     
  2. Offline

    KarimAKL

    @Light__WOLF
    1. Please format and syntax highlight your code.
    2. Where did you register the Listener?
     
  3. Offline

    Strahan

    Show us your main class, the one that extends JavaPlugin. Also:

    Code:
    ArrayList<UUID> ekfeuer = new ArrayList<>();
    This should be List<UUID> ekfeuer, per SOLID design principles

    Code:
    Player p = (Player) cs;
    You are casting this without type checking it. That can crash the plugin. You should check if sender is a Player first.

    Code:
    if(cmd.getName().equalsIgnoreCase("effekt")){
    If effekt is the only command using this class as executor, this is superfluous.

    Code:
    if(args[0].equalsIgnoreCase("feuer")) {
      if(args.length == 1) {
    You are checking length after you already access the first argument. That's backwards and will cause a crash if the user does /effekt with no argument.

    Lastly, doing negative check and return would make the code easier to read, IMO. Example:
    Code:
    public class MainEffekte implements CommandExecutor, Listener{
        List<UUID> ekfeuer = new ArrayList<>();
       
        @Override
        public boolean onCommand(CommandSender cs, Command cmd, String label, String[] args) {
            if (!(sender instanceof Player)) {
                // throw error and return
            }
    
            if (!p.hasPermission("GlowStoneMC.effekte")) {
                // throw error and return
            }
           
            if (args.length == 0) {
                // throw error and return
            }
           
          Player p = (Player) cs;
          if (ekfeuer.contains(p.getUniqueId())) {
              ekfeuer.remove(p.getUniqueId());
              p.sendMessage(ChatColor.RED + "DEBUG="+ ChatColor.WHITE + "gelöscht");
              return true;
            }
    
          ekfeuer.add(p.getUniqueId());
          p.sendMessage(ChatColor.RED + "DEBUG="+ ChatColor.WHITE + "hinzugefügt");
          return true;
        }
       
        @EventHandler
        public void ekfeuer(PlayerMoveEvent e) {
            if (!ekfeuer.contains(e.getPlayer().getUniqueId())) return;
    
            Player p = e.getPlayer();
            p.sendMessage("t");
        }
    }
     

Share This Page