I am coding a Minecraft plugin. In this plugin I created an object called CPlayer (stands for CustomPlayer) since there is already a Player object built-in. Whenever a player joins the server, I create a new object with the player's name (final, String), condition (String, will always be "on" or "off"), uuid (final, String) as attributes.
When a player writes the command "/mycond" in-game, they see the condition attribute for the CPlayer that was created for them ("on" by default) and the CPlayer object is printed to the console.
However, when a player types the command "/mycond on/off", they should set the condition attribute for the CPlayer that was created for them, and the CPlayer object is, again, printed to the console (which always matches with the CPlayer that was printed to the console when the player uses "/mycond"). Which for some reason does not work.
I did this like so:
In my main class:
public static HashMap<String, CPlayer> playerList = new HashMap<>();
...(unrelated code)...
playerList.put(player.getName(), new CPlayer(player.getName()));
Which basically just adds the players' name and a new CPlayer object to a <String, CPlayer>
HashMap so I can address that specific CPlayer object later.
In the command code:
Player player = (Player) sender;
CPlayer cplayer = TestPlugin.playerList.get(player.getName()); //Gets the player's CPlayer object from the HashMap.
if (args.length == 0) { //if player executed command "/mycond".
System.out.println("/mycond command (without args) " + cplayer); //Prints that CPlayer to the console.
player.sendMessage(cplayer.getCondition()); //gets the condition attribute from that CPlayer object and sends it to the player.
return true; //doesnt relate to the problem
} else {
if (args[0].equals("on")) {//if executed command "/mycond on".
player.setCondition(true); //sets the players CPlayer objects' condition attribute to "on" and prints to the console that CPlayer object (always the EXACT same as when executed command "/mycond" above).
player.sendMessage("Turned on"); //sends the player that the condition has changed to on (just to indicate that this code actually ran).
return true; //doesnt relate to the problem
} else if (args[0].equals("off")) { //if executed command "/mycond off".
cplayer.setCondition(false);//sets the players CPlayer objects' condition attribute to "off" and prints to the console that CPlayer object (always the EXACT same as when executed command "/mycond" above).
player.sendMessage("Turned off"); //sends the player that the condition has changed to off (just to indicate that this code actually ran).
return true; //doesnt relate to the problem
}
}
Basically, the problem is that either the condition doesn't change for the players' specific CPlayer object or that "/mycond" command always sends "on" to the player for some reason (doesn't read the condition attribute of the players' specific CPlayer object).
CPlayer class:
public class CPlayer {
private final String playerName; //the players' name
private String condition; //on/off
private final UUID uuid;
public String getName() {
return this.playerName;
}
...[Unrilated code]...
public void setCondition(boolean bul) { //the setCondition() method I used in the command code.
if (bul = true) {
this.condition = "on";
} else {
this.condition = "off";
}
System.out.println("setCondition method! (with args) " + this);
}
public String getCondition() { //used in the "/mycond" command to return the players' CPlayers' condition attribute and send it to the player.
return this.condition;
}
public CPlayer(String playerName) { //c'tor used to create the players' CPlayer object when they join the server.
this.playerName = playerName;
this.condition = "on";
this.uuid = this.getPlayer().getUniqueId();
}
public CPlayer(String playerName, boolean bul) { //unused c'tor.
this.playerName = playerName;
if (bul = true) {
this.condition = "on";
} else {
this.condition = "off";
}
this.uuid = this.getPlayer().getUniqueId();
}
...[Unrilated code]...
}