(Freezing players) help?

Discussion in 'Plugin Development' started by Vamure, Dec 31, 2014.

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

    Vamure

    Hey, so I have a freeze command that simply freezes players.

    I have a problem with my ArrayList.

    Code:
         public static ArrayList<Player> frozen = new ArrayList<Player>();
    This ONLY works when it has the public static modifier.
    If it's public or private, the player will not be frozen and is able to move.

    Event:
    Code:
        @EventHandler
        public void onPlayerMove(PlayerMoveEvent e) {
            Player p = e.getPlayer();
            if (frozen.contains(p)) {
                     // freeze player
    OnCommand:
    Code:
    if (frozen.contains(target)) {
                    frozen.remove(target);
    //  msg player bla bla
    }
    frozen.add(target);
    // msg player bla bla
    
    
    So yeah. I don't get errors, the messages get sent, but the Event just isn't detecting it or something. And again, it works perfectly fine when it's public static.
     
  2. Offline

    teej107

    That does not make any sense. Post your full class.

    @Vamure I know what your problem is. Instantiate one Freeze class only and register the event and command executor with that one Freeze object. You can stop with the public static non-sense now.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Oct 31, 2016
  3. Offline

    AlphaRLee

    You are trying to use a static instance of your arrayList, and then you reference to your arrayList in a static fashion. That works fine...for only one time. If you want your arrayList to behave more like an object (so that there can be multiple lists for...I'm not sure, multiple worlds/servers?), you need to treat it more like an object.

    If your eventListener/eventHandler methods are in a different class than your main class, then this is fairly quick:
    First, you can exclude the word Static on your ArrayList
    Code:
    public ArrayList<Player> frozen = new ArrayList<Player>();
    Next, in your eventListener/eventHandler class, create a blank arrayList. Then change your eventHandler class to request the Frozen arrayList in your constructor
    Code:
    private Main main;
    private ArrayList<Player> frozenList; //No need to instantiate it yet
    
    public EventListener (Main m, ArrayList<Player> f) {
    main = m;
    frozenList = f;
    }
    Last, just edit your eventHandlers to use frozenList, not frozen. If your frozen list isn't in your main class, then you are able to have multiple lists.
     
    mine-care likes this.
Thread Status:
Not open for further replies.

Share This Page