Reputation: 1072
So I'm developing a Java Plugin and I need to do something like the KitPvP servers do these days, the player selects a kit, and then they are only allowed to get a kit again once they've died.
I've tried this using strings to check if the player is in command, but I really don't know what/"how" to do with them. Any suggestions?
Upvotes: 2
Views: 799
Reputation:
You could use a list to check if the player has got the kit in "this life". For example in your main plugin class or in your command class you could add a static member like this:
public static ArrayList<UUID> usedKit = new ArrayList<UUID>();
In the onCommand() method you can check if the player hasn't got his kit yet:
if (!usedKit.contains(player.getUniqueId())) {
// Code to give the kit here...
usedKit.add(player.getUniqueId()); // Adds the player to the list
} else
player.sendMessage("You already got your kit.");
return true;
When the player dies you have to remove him from the list:
@EventHandler
public void onPlayerDeath(PlayerDeathEvent event) {
if (usedKit.contains(event.getPlayer().getUniqueId()))
usedKit.remove(event.getPlayer().getUniqueId());
}
I know that this answer is similar to Kerooker's but you should try to use UniqueIds since you can change your name in minecraft.
Upvotes: 2
Reputation: 7143
What you can do is create a list containing the names of the players that have already used the kits
List<String> players = new ArrayList<String>();
Then whenever they use the command, you check if the player is inside your list
boolean isInList = players.contain(yourPlayer.getName());
You can check that when you handle the command
if (isInList) {
player.sendMessage("You must die to use this again!");
return true; //To commandExecutor
}
If the player is not in the list, add him to the list and give the kit
players.add(yourPlayer.getName());
//Give the kit
Whenever a player die, you should try to remove his name from the List.
@EventHandler
public void onDeath(PlayerDeathEvent e) {
//Remove player from the list
}
Note that inside a onCommand
, you have the following attributes of a command: CommandSender sender, Command cmd, String label, String[] args
.
The sender argument may be your player, and you can check that by
if (sender instanceof Player) {
Player commandPlayer = (Player) sender;
}
Upvotes: 0
Reputation: 466
I assume that you use the Spigot API and you'r probably searching for something like this PlayerDeathEvent
This gets triggered everytime a Player dies and you can compare them with your Players list and update the permissions(bukkit) permission(spiggot).
Hopefully this answered your question.
Upvotes: -1