Store players in ArrayList and disable movement for them

Discussion in 'Plugin Development' started by xImCadium, May 2, 2016.

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

    xImCadium

    Hey guys, In my plugin I wanted that when I execute a command the player name gets saved in a arraylist, and for the players in the array list the movement should get canceled. Storing the names in the array list works, also that the players can't move (tested with another command), but I don't know how to check if the player is in the array list, I tried following in my PlayerMove listener class:
    Code:
    @EventHandler
        public void onMove(PlayerMoveEvent e) {
            //Player p = e.getPlayer();
            Location from = e.getFrom();
            Location to = e.getTo();
    
    
            if (troll.noMove.contains(e.getPlayer())){  //Here I wanted to check if the player of the event is in the                                                                                        array list
                Player player = e.getPlayer();
                if (from.getX() != to.getX() || from.getZ() != to.getZ()) {
                    player.teleport(new Location(player.getWorld(), from.getX(), to.getY(), from.getZ(), to.getYaw(), to.getPitch()));
                }
            }
    Here the class with the array list and the commandexecutor, the command works, maybe I made a mistake in adding the player to the array list:

    Code:
    ...
    Player playertrolled = Bukkit.getPlayerExact(args[1]);
    ... // I shortend out the if clauses etc. to check if the player's online
    
    player.sendMessage(plugin.getPrefix() + "NoMove for " + playertrolled + " enabled!");
                            noMove.add(playertrolled);
                            playerName = playertrolled.getName();
    Hope you know how to fix my problem, thank you :D
     
    Last edited: May 2, 2016
  2. Offline

    mine-care

    I saw this line and i must point out the following:
    1. Encapsulate
    2. Don't store Player objects in Collections or maps* since they cause Ram leaks.

    Now as far as your problem is concerned, you mentioned that you dont know how to check if the Player is in the list, Well you check fine, and you add them fine as well. What is the problem? What is the wanted/unwanted behaviour of this code?


    *Exclusions apply.
     
  3. Offline

    xImCadium

    I want that if the player of the event is in that list, the player isn't allowed to move anymore, so Idk how to check if the player of the event is in my list
     
  4. Offline

    XxTimexX

    @xImCadium
    You have checked fine, like mine-care said, or I got you wrong? :confused:
     
  5. Offline

    xImCadium

    Yeah ok, I found the mistake, I had to set the Array list static, but no, it's a bit laggy, could this be caused of the ArrayList, or is it probably just my internet?
     
  6. Offline

    mine-care

    @XxTimexX No, you got it right ;)
    And why does it 'have to be' static?
    From the piece of code you gave us, i cant tell if it is the list to blame although i dought it.

    Also, dont forget to tahg me! (@mine-care) otherwise i wont respond!
     
  7. Offline

    xImCadium

    @mine-care Probably it was my internet, everything works perfect now, but I really have no idea why I had to set it static, I just tried it, and it works ;D
     
  8. Offline

    mine-care

    @xImCadium that is not a valid excuse to use static though!
    Can I see your full code?
    Also static members have to be assigned to null on disable otherwise they become ram leaks (on disable)
     
  9. Offline

    xImCadium

    @mine-care
    Here you are
    Code:
    public class PlayerMove implements Listener{
       
        TrollPlugin Ptroll;
        cmdTroll troll;
       
        public PlayerMove(TrollPlugin Ptroll, cmdTroll troll){
            this.Ptroll = Ptroll;
            this.troll = troll;
        }
       
        @EventHandler
        public void onMove(PlayerMoveEvent e){
           
            Player p = e.getPlayer();
            Location from = e.getFrom();
            Location to = e.getTo();
           
            if (troll.noMove.contains(e.getPlayer().getName())){
                Player player = e.getPlayer();
                if (from.getX() != to.getX() || from.getZ() != to.getZ()) {
                    player.teleport(new Location(player.getWorld(), from.getX(), to.getY(), from.getZ(), to.getYaw(), to.getPitch()));
                }
            }
           
        }
    
    }
    
    Code:
    public class cmdTroll implements CommandExecutor {
    
        public static ArrayList<String> noMove = new ArrayList<String>();
        public static ArrayList<String> noChat = new ArrayList<String>();
        public static ArrayList<String> noHit = new ArrayList<String>();
    
        TrollPlugin plugin;
    
        public static String playerName;
    
        public cmdTroll(TrollPlugin plugin) {
            this.plugin = plugin;
        }
    
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            Player player = (Player) sender;
            if (cmd.getName().equalsIgnoreCase("troll")) {
                if (args.length != 0) {
    
                    switch (args[0]) {
                    case "list":
                        if(!(noMove.contains(player.getName()) || noHit.contains(player.getName()) || noChat.contains(player.getName()))){
                        player.sendMessage(plugin.getPrefix() + "NoMove: " + noMove);
                        player.sendMessage(plugin.getPrefix() + "NoChat: " + noChat);
                        player.sendMessage(plugin.getPrefix() + "NoHit: " + noHit);
                        }else{
                            player.sendMessage(plugin.getPrefix() + "NoMove: []");
                            player.sendMessage(plugin.getPrefix() + "NoChat: []");
                            player.sendMessage(plugin.getPrefix() + "NoHit: []");
                        }
                        break;
                    case "noMove":
                        if (args[1].equalsIgnoreCase("cancel")) {
                            Player playercanceled = Bukkit.getPlayerExact(args[2]);
                            if (playercanceled == null) {
                                sender.sendMessage(plugin.getPrefix() + "The player could not be found");
                                player.sendMessage("jup");
                                return true;
                            } else {
                                player.sendMessage(
                                        plugin.getPrefix() + "NoMove for " + playercanceled.getName() + " disabled!");
                                noMove.remove(playercanceled.getName());
                            }
                        } else {
                            Player playertrolled = Bukkit.getPlayerExact(args[1]);
                            if (playertrolled == null) {
                                sender.sendMessage(plugin.getPrefix() + "The player could not be found");
                                return true;
                            } else {
                                player.sendMessage(
                                        plugin.getPrefix() + "NoMove for " + playertrolled.getName() + " enabled!");
                                noMove.add(playertrolled.getName());
                            }
                        }
                        break;
     
  10. Offline

    XxTimexX

    @xImCadium
    Just noticed, you need to check before casting (check if sender is console or player).
     
  11. Offline

    mine-care

    @XxTimexX or command block

    I still see no reason for so much static there, it is abusive! :eek:
     
Thread Status:
Not open for further replies.

Share This Page