Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 44 additions & 56 deletions src/main/java/cmd/CmdPlayer.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,59 +18,48 @@ public CmdPlayer(RemoteSession session, FruitJuicePlugin plugin) {
this.plugin = plugin;
}

private boolean serverHasPlayer() {
return !Bukkit.getOnlinePlayers().isEmpty();
}

private Player getCurrentPlayer() {
// if no players, return null
if (!serverHasPlayer()) {
session.send("Fail,There are no players in the server.");
return null;
}

// if the player hasnt already been retreived for this session, go and get it.
Player player = attachedPlayer;
if (player == null) {
player = plugin.getHostPlayer();
attachedPlayer = player;
return player;
}

// otherwise, return the player
return player;
}

private Player getCurrentPlayer(String name) {

// if no players, return null
if (!serverHasPlayer()) {
session.send("Fail,There are no players in the server.");
return null;
}

// if a named player is returned use that
Player player = plugin.getNamedPlayer(name);
if (player == null) {
player = attachedPlayer;
// otherwise go and get the host player and make that the attached player
if (player == null) {
player = plugin.getHostPlayer();
}
}
attachedPlayer = player;
return player;
}

public void execute(String command, String[] args) {

Player currentPlayer = getCurrentPlayer();
if (currentPlayer == null) {
session.send("Fail,There are no players in the server.");
return;
}
/**
* Finds a player on the server by their entity ID.
*
* @param id The entity ID of the player.
* @return The Player object if found, otherwise null.
*/
private Player getPlayerById(int id) {
for (Player player : Bukkit.getOnlinePlayers()) {
if (player.getEntityId() == id) {
return player;
}
}
return null; // Return null if no player with the given ID is found.
}

// player.getTile
public void execute(String command, String[] args) {
// --- Start of Recommended Fix ---

// 1. Check if the required ID argument was provided.
if (args.length == 0) {
session.send("Fail,Required player ID argument is missing.");
return;
}

Player currentPlayer;
try {
// 2. Parse the ID, handling potential non-numeric input.
int playerId = Integer.parseInt(args[0]);
currentPlayer = getPlayerById(playerId);

} catch (NumberFormatException e) {
session.send("Fail,Invalid player ID format. ID must be an integer.");
return;
}

// 3. Check if a player with that ID was actually found.
if (currentPlayer == null) {
// Using args[0] here to show the user which ID failed.
session.send("Fail,No player found with ID: " + args[0]);
return;
}
// player.getTile
if (command.equals("getTile")) {

session.send(session.blockLocationToRelative(currentPlayer.getLocation()));
Expand Down Expand Up @@ -114,9 +103,8 @@ public void execute(String command, String[] args) {

// player.setPlayer
} else if (command.equals("setPlayer")) {
String playerName = args[0];
getCurrentPlayer(playerName);

// String playerName = args[0];
// getCurrentPlayer(playerName);
// player.setDirection
} else if (command.equals("setDirection")) {
Double x = Double.parseDouble(args[0]);
Expand Down Expand Up @@ -194,7 +182,7 @@ public void execute(String command, String[] args) {
currentPlayer.sendTitle(title, subTitle, fadeIn, stay, fadeOut);

} else {
session.plugin.getLogger().warning(preFix + command + " is not supported.");
// session.plugin.getLogger().warning(preFix + command + " is not supported.");
session.send("Fail," + preFix + command + " is not supported.");
}
}
Expand Down