1.8.8 Issue with events

Discussion in 'Spigot Plugin Development' started by GoDragonGoGoYT, Oct 18, 2020.

  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
    Last edited: Oct 18, 2020
  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. Does the command work?
     
  7. No, no code at all.
     
  8. 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
     
  9. 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
     
  10. What happen if you perform the command?
     
  11. nothing, just the unknown command message
     
  12. 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
     
  13. 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
    Last edited: Oct 18, 2020
  14. omg im a idiot lol
    thanks you fixed it
     
  15. yeah