1.8.8 Basic Array Trouble

Discussion in 'Spigot Plugin Development' started by hamstervhamster, Jan 20, 2020.

  1. Idk if this is just me, but I can't find any errors with this code.
    My 1 array seems to have two separate memory addresses, thus not allowing the event to work right.
    Code (Text):

    @Override
        public void onEnable() {
            Bukkit.getServer().getPluginManager().registerEvents(new Events(), this);
            final CommandBase cb = new CommandBase();
            this.getCommand("command").setExecutor(cb);
        }
        public static Plugin getPlugin() {
            return Bukkit.getServer().getPluginManager().getPlugin("PluginName");
        }
     
    Code (Text):

    public class CommandBase implements CommandExecutor {
        public ArrayList<Player> array = new ArrayList<Player>();

        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String commandlabel, String[] args) {
            Player p = (Player) sender;
            if(commandlabel.equalsIgnoreCase("command")) {
                if(args.length == 2) {
                    if(args[0].equalsIgnoreCase("event")) {
                        final Player t = Bukkit.getServer().getPlayer(args[1]);
                        if(t == null) {

                        }

                        if(array.contains(t)) {
                            //Remove Player
                            array.remove(t);
                            p.sendMessage("Removed + " + array.size());

                        } else {
                                //Add Player
                                array.add(t);
                                p.sendMessage("Summoned + " + array.size());
                        }
                    }
                }
            }

            return false;
        }
     
    Code (Text):

    public class Events extends CommandBase implements Listener {
        @EventHandler
        public void arraymethod(PlayerMoveEvent e) {
            final Player p = e.getPlayer();
            p.sendMessage("Array Sep Class -> " + super.array.size());
            if(super.array.size() > 0) {
                p.sendMessage("Msg1");
                if(super.array.contains(p)) {
                    p.sendMessage("Msg2");
                 
                }
            }
        }
    }
    What am I doing wrong? Thank you :D
     
    #1 hamstervhamster, Jan 20, 2020
    Last edited: Jan 20, 2020
  2. I think it is due to you making two different CommandBases
    One in the eventName and one in the OnEnable.

    Just because it is from the same class does not mean it contains the same data and properties.


    Code (Java):
    package dev.jdsgames.basicoop;

    public class main
    {
        public static void main(String[] args)
        {
            // Declaring Objects from my Test Class
            TestClass testClass1 = new TestClass(1,5);
            TestClass testClass2 = new TestClass(2,5);
           
            // Printing their Sums Note values will be different as they are two unique objects.
            testClass1.printSum();
            testClass2.printSum();
        }
    }

    Code (Java):
    package dev.jdsgames.basicoop;

    public class TestClass
    {
        // Basic private variables to store the two int arguements.
        private int int1;
        private int int2;
       
        // Public Constructor
        public TestClass(int int1, int int2)
        {
            this.int1 = int1;
            this.int2 = int2;
        }
       
        // Print the sums of int1 and int2
        public void printSum()
        {
            System.out.println("The sum of: " + int1 + " " + int2 + " is equal to " + (int1+int2));
        }
    }
     
     
    #2 Jdsgames, Jan 20, 2020
    Last edited: Jan 20, 2020
    • Agree Agree x 1
  3. Good point, (I have changed the code up above). However it still has the same issue. Any other thoughts?
     
  4. This. If you need your list in different parts of your plugin you shouldn't store it in your command executor but instead in a object that holds and encapsulates the data (like a manager) and dependency inject this manager everywhere you need access to the data.

    Edit: After your code edit above you just moved your listener inside of your command executor class (which is most likely bad practice in this scenario) while the class doesn't even implement the Listener interface. (You also have to register your listener)
     
    #4 Dexuby, Jan 20, 2020
    Last edited: Jan 20, 2020
    • Agree Agree x 1

  5. Code (Java):

            Bukkit.getServer().getPluginManager().registerEvents(new CommandBase(), this);
            final CommandBase cb = new CommandBase();
            this.getCommand("command").setExecutor(cb);
     
    You would effectively be doing the same double creating CommandBase here.
    every time you see "new CommandBase();" that means it is a new CommandBase object being created.

    You are then branching off these two CommandBases to your event and your command.
    This means your event has one array and your command has another.
     
    #5 Jdsgames, Jan 20, 2020
    Last edited: Jan 20, 2020
    • Agree Agree x 1
  6. Changing my code again up above, I now extended the class. I don't think it makes a "new CommandBase()" but I could be wrong. Any thoughts or should I use different method like Dexuby's
     
  7. From what @Dexuby had said before. I think this is coming down to unfamiliarity with Java structure. You are making changes throwing around Extends and other keywords randomly.

    I would suggest reviewing core java concepts (Short videos more of them Videos up to 40)
    https://www.youtube.com/playlist?list=PLS1QulWo1RIbfTjQvTdj8Y6yyq4R7g-Al

    This way you will have a better understanding of Object Oriented Programming as a whole. As it is definitely needed to write any useful / decent code utilizing Spigot.
     
  8. To quickly correct the problem about the double instance of the class, just create it once.

    Code (Java):
    private CommandBase cb;

    onEnable()
    {
        cb = new CommandBase();

        registerEvents(cb, this);
        getCommand("command").setExecutor(cb);
    }
     
    • Like Like x 1