Home > Back-end >  having trouble with my event identifying the hashset from my command class minecraft plugin (spigot)
having trouble with my event identifying the hashset from my command class minecraft plugin (spigot)

Time:07-04

So I'm experimenting with hashmaps/sets and I can't get my event class to recognize the contents of my hashset. The toggle command works (identifies and sends back correct results of if the player is in the set or not) but, the event when I cast the rod always returns the else part of the code (always stating false) rather than using the data from the set to check if the player is there or not. (code will be below)

Yes my events and commands are all registered

What I've tried

changing players.add(p.getUniqueId()); to players.add(p.getName()); and vice versa multiple if statements random small changes to how the hashset is created/checked

Code:

TOGGLE COMMAND*


import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

import java.util.HashSet;
import java.util.UUID;

public class HashCommand implements CommandExecutor {
    HashSet<UUID> players = new HashSet<UUID>();

    @Override
    public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
        if(!(sender instanceof Player)) {
            sender.sendMessage("Players Only");

        }else{
            Player p = (Player) sender;
            boolean check = players.contains(p.getUniqueId());
            if(!check){
                players.add(p.getUniqueId());
                p.sendMessage("Toggled On");

            }else{
                players.remove(p.getUniqueId());
                p.sendMessage("Toggled Off");
            }
        }
        return false;
    }
}

EVENT COMMAND


import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;

import org.bukkit.event.player.PlayerFishEvent;




public class Event implements Listener {
    public HashCommand hashCommand = new HashCommand();

    @EventHandler
    public void onCast(PlayerFishEvent e){
        Player p = e.getPlayer();
        if (hashCommand.players.contains(p.getUniqueId())) {
            p.sendMessage("Fish");
        }else{
            p.sendMessage("You do not have it enabled!");

        }

    }


}

CodePudding user response:

You can't use the object oriented way in your case. Every time a command is triggered Spigot creates a new instance of your HashCommand class. Spigot does not use the instance you created in your event listener: public HashCommand hashCommand = new HashCommand();. Having multible instances means you have multible sets with each set having it's own content. So you are accessing a different set from the event than from the command. The set of the event is always empty.

Solution make the set static. Static content is the 'same' for every instance.

public static Set<UUID> players = new HashSet<UUID>();

In the Event class you can access it via the class name as shown:

HashCommand.players.contains(...)

No need for the instance named hashCommand.


Note that you might want to add a player disconnect event to remove players that are not online from your set.

  • Related