1.8.8 Issue with events

Discussion in 'Spigot Plugin Development' started by GoDragonGoGoYT, Oct 18, 2020 at 3:55 PM.

  1. I am getting a console error in line 15 and 20, not sure what is wrong, also if your wondering why I put the command and event in the same class its because i need to have an array list over both of them.

    Code (Text):

    package dev.ioyo.mactest;

    import com.connorlinfoot.actionbarapi.ActionBarAPI;
    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.player.PlayerMoveEvent;
    import org.bukkit.plugin.java.JavaPlugin;

    import java.util.ArrayList;

    public class MacTest extends JavaPlugin implements org.bukkit.event.Listener {

        @Override
        public void onEnable() {
            // Plugin startup logic
            getServer().getPluginManager().registerEvents(new MacTest(), this);

        }

        @Override
        public void onDisable() {
            // Plugin shutdown logic
        }

        public ArrayList<Player> list_of_toggled_players = new ArrayList<>();

        @EventHandler
        public void PlayerMove(PlayerMoveEvent event) {
            Player player = event.getPlayer();
            if (list_of_toggled_players.contains(player)) {
                Location location = player.getLocation();
                int x = (int) location.getX();
                int y = (int) location.getY();
                int z = (int) location.getZ();
                ActionBarAPI.sendActionBar(player, ChatColor.GREEN + " X: " + x + " Y: " + y + " Z: " + z);


            } else {
            }
        }

        @Override
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
            if(command.getName.equals("toggleactionbar")
    Player player = (Player) sender;
            if (list_of_toggled_players.contains(player)) {
                list_of_toggled_players.remove(player);
                player.sendMessage(ChatColor.GREEN + "Your coordinates in your action bar have been toggled off!");
            } else {
                if (list_of_toggled_players.contains(player)) {
                    list_of_toggled_players.add(player);
                    player.sendMessage(ChatColor.GREEN + "Your coordinates in your action bar have been toggled on!");
                }

            }return true;
        }
    }
     
     
  2. Choco

    Moderator

    You cannot create new instances of your main class. Just register this instance instead of new MacTest()
     
    • Like Like x 1
  3. Your code would result in a PluginAlreadyInitializedException. You cannot have more than 1 instance of your main class
     
    #3 DragoFantasma_, Oct 18, 2020 at 4:03 PM
    Last edited: Oct 18, 2020 at 4:14 PM
  4. Yes, that did happen, ok thanks.
     
  5. Code (Text):

    package dev.ioyo.mactest;

    import com.connorlinfoot.actionbarapi.ActionBarAPI;
    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.player.PlayerMoveEvent;
    import org.bukkit.plugin.java.JavaPlugin;

    import java.util.ArrayList;

    public class MacTest extends JavaPlugin implements org.bukkit.event.Listener {

        @Override
        public void onEnable() {
            // Plugin startup logic
            getServer().getPluginManager().registerEvents(this, this);

        }

        @Override
        public void onDisable() {
            // Plugin shutdown logic
        }

        public ArrayList<Player> list_of_toggled_players = new ArrayList<>();

        @EventHandler
        public void PlayerMove(PlayerMoveEvent event) {
            Player player = event.getPlayer();
            if (list_of_toggled_players.contains(player)) {
                Location location = player.getLocation();
                int x = (int) location.getX();
                int y = (int) location.getY();
                int z = (int) location.getZ();
                ActionBarAPI.sendActionBar(player, ChatColor.GREEN + " X: " + x + " Y: " + y + " Z: " + z);


            } else {
            }
        }

        @Override
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
            if (command.getName().equals("toggleactionbar")) {

                Player player = (Player) sender;
                if (list_of_toggled_players.contains(player)) {
                    list_of_toggled_players.remove(player);
                    player.sendMessage(ChatColor.GREEN + "Your coordinates in your action bar have been toggled off!");
                } else {
                    if (list_of_toggled_players.contains(player)) {
                        list_of_toggled_players.add(player);
                        player.sendMessage(ChatColor.GREEN + "Your coordinates in your action bar have been toggled on!");
                    }

                }

            }return true;
        }
    }
     
    I did that, but none of the code worked (I got no error)
     
  6. No, no code at all.
     
  7. Wait, replace the onCommand body with this:
    Code (Java):

    if (command.getName().equals("toggleactionbar")) {
                // You should check before casting
                Player player = (Player) sender;
                if (list_of_toggled_players.contains(player)) {
                    list_of_toggled_players.remove(player);
                    player.sendMessage(ChatColor.GREEN + "Your coordinates in your action bar have been toggled off!");
                } else {
                        list_of_toggled_players.add(player);
                        player.sendMessage(ChatColor.GREEN + "Your coordinates in your action bar have been toggled on!");
                }
            }
    return true;
    The player was never added to the list because:
    Code (Java):

    if (list_of_toggled_players.contains(player)) {
           list_of_toggled_players.add(player);
           player.sendMessage(ChatColor.GREEN + "Your coordinates in your action bar have been toggled on!");
    }
    Was always false
     
  8. Nope:
    Code (Text):

    package dev.ioyo.mactest;

    import com.connorlinfoot.actionbarapi.ActionBarAPI;
    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.player.PlayerMoveEvent;
    import org.bukkit.plugin.java.JavaPlugin;

    import java.util.ArrayList;

    public class MacTest extends JavaPlugin implements org.bukkit.event.Listener {

        @Override
        public void onEnable() {
            // Plugin startup logic
            getServer().getPluginManager().registerEvents(this, this);

        }

        @Override
        public void onDisable() {
            // Plugin shutdown logic
        }

        public ArrayList<Player> list_of_toggled_players = new ArrayList<>();

        @EventHandler
        public void PlayerMove(PlayerMoveEvent event) {
            Player player = event.getPlayer();
            if (list_of_toggled_players.contains(player)) {
                Location location = player.getLocation();
                int x = (int) location.getX();
                int y = (int) location.getY();
                int z = (int) location.getZ();
                ActionBarAPI.sendActionBar(player, ChatColor.GREEN + " X: " + x + " Y: " + y + " Z: " + z);


            } else {
            }
        }

        @Override
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
            if (command.getName().equals("toggleactionbar")) {
                // You should check before casting
                Player player = (Player) sender;
                if (list_of_toggled_players.contains(player)) {
                    list_of_toggled_players.remove(player);
                    player.sendMessage(ChatColor.GREEN + "Your coordinates in your action bar have been toggled off!");
                } else {
                    list_of_toggled_players.add(player);
                    player.sendMessage(ChatColor.GREEN + "Your coordinates in your action bar have been toggled on!");
                }
            }
            return true;
        }
    }
     
    No console error either
     
  9. What happen if you perform the command?
     
  10. nothing, just the unknown command message
     
  11. name: ActionBarCoords
    version: ${project.version}
    main: dev.ioyo.mactest.MacTest
    api-version: 1.16
    authors: [ Ioyo ]
    description: See your coordinates in the action bar!
    website: ioyo.dev
     
  12. You have to specify commands here
    It should look like this:
    Code (Java):
    name: ActionBarCoords
    version: ${project.version}
    main: dev.ioyo.mactest.MacTest
    api-version: 1.16
    authors: [ Ioyo ]
    description: See your coordinates in the action bar!
    website: ioyo.dev
    commands:
      toggleactionbar:
        description: Your description
     
    #15 DragoFantasma_, Oct 18, 2020 at 4:49 PM
    Last edited: Oct 18, 2020 at 4:56 PM
  13. omg im a idiot lol
    thanks you fixed it
     
  14. yeah