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?
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.
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;
}
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.
Related
This code doesn't work. Wonder, where I went wrong.
public class Basic extends JavaPlugin {
#EventHandler
public void onPlayerJoin (PlayerJoinEvent evt) {
Player player = evt.getPlayer();
PlayerInventory inventory = player.getInventory();
ItemStack itemstack = new ItemStack(Material.STICK,1);
if (player.hasPermission("lobby.lobby")) {
player.setHealth(0.5);
inventory.setItem(1,itemstack);
inventory.addItem(itemstack);
player.setWalkSpeed(3);
player.addPotionEffect(new PotionEffect(PotionEffectType.SPEED,9999, 1));
player.hidePlayer(player);
}
}
}
When I start my server, there are no errors in console. Everything is fine, but I don't get any items, no speed, no effects.
This could be any number of issues, and this is a poor question, but I would recommend adding a couple lines that print debug messages to the console, that way you know what parts of your code have run if any.
Most likely you do not have the permission node lobby.lobby, being an OP does not actually give you permission nodes. Get a permissions plugin such as Luck Perms and add the permission node to the default group or to yourself.
My football team has 5 different teams and I am trying to make a program to keep a track of which players are registered with each team. I want to be able to add a new player but first the program checks if that player is already registered. I would also be able to add new teams in the future and again I would like to check if that team already exists.
I have made a Map variable with
private Map<String, List<Player>> teamName;
then initialised this in the constructor
teamName = new HashMap<>();
I then have a method to add new teams and new players, I want the method to check if the Club name already exists and then if it does exist, add the player name to that Club. if it doesn't exist then I want the program to add a new Club and then add that player to that club.
So far I have a method for adding a new player,
public void newPlayer(String club, Player name) {
}
I am not sure how I now go about checking that an ArrayList exists for club and if it does add name to this list, if club does not exist then I want to make a new list and add name to it.
if I then run the program and write,
Player jamesAtkinson = new Player();
newPlayer("first team", jamesAtkinson);
it would check if there is a List in the Map called 'first team' and then either add James Atikinson to it, or create a new List called first team and then add James Atkinson.
Is this even possible to do?
Although there are a few problems with code you've provided in the question. What you're looking for is the .containsKey function that hangs off of the Map interface.
if (players.containsKey("first team") {
// Do something
} else {
List<Player> firstTeam = new ArrayList<>();
firstTeam.add(jamesAtikson);
players.put("first team", jamesAtikson);
}
short description of what I am trying to do: I'm building a simple game where the user controls a vehicle and after some time more and more ghosts begin following the player, they follow the same trajectory as the player did with a delay.
To accomplish this I create an Array that contains the history of the player's location as a sequence of points. The problem, however, is that when I look at the data stored in this array I see that on all indices only the most recent location is stored.
First I create the array in a botManager class:
public class BotManager {
private ArrayList<Bots> bots;
private List<Point> history;
BotManager() {
history = new ArrayList<>();
bots = new ArrayList<>();
}
Then in the update method of the manager class I add the current location of the player to the array
public void update(Point currLoc) {
history.add(currLoc);
for (Bots bot : bots) {
bot.setLocationData(history);
bot.update();
}
}
A look at the update method in the main GameView class, in case I forgot something here
public void update() {
player.update(playerPoint);
botManager.update(playerPoint);
}
In the bots class' constructor I pass the history list (locationData) and determine its length to find out the delay in positioning. After which the following code handles the position of the bot.
#Override
public void update() {
loc = locationData.get(delay - 1);
this.rectangle = new Rect(loc.x - Constants.BOTSIZE/2, loc.y - Constants.BOTSIZE/2,
loc.x + Constants.BOTSIZE/2, loc.y + Constants.BOTSIZE/2);
}
To get back to the problem, whenever I check the contents of the history array, I find that it only contains one point on all indices, and its the most recent even when I moved the player, causing the ghost to always remain on top of me.
So my question here is, what am I doing wrong here?
Not clear from your posted code, but could it be, that you are just modifying the Point which you are adding instead renewing your object's Point objects?
Try
public void update(Point currLoc) {
history.add(new Point(currLoc)); // new Point object added here
for (Bots bot : bots) {
bot.setLocationData(history);
bot.update();
}
}
I have the following method inside the class Player that uses one of his emporiums available and sets it on a determined City
public void setEmporium(City city){
Emporium emporium = getEmporiums().get(0);//gets the emporium from the player
city.getEmporium().add(emporium);//adds it to the city
LinkedList<Bonus> bonus = city.getBonus();
for (int i=0;bonus!=null&&i<bonus.size();i++){
bonus.get(i).applyBonus(player);//Error line
}
getEmporiums().remove(0);//removes the emporium from the player
}
To correct the error of course i can just use the parameter player like this
public void setEmporium(City city,Player player){
but it isn't kind of redundant to use a parameter in its same class?
because when i test it would be like
Player player = new Player(1);
City city = new City(CityName.BURGEN,Color.YELLOW,4);
player.setEmporium(city, player);
and if i would create another object i would be able to do this
Player player2 = new Player(2);
player2.setEmporium(city, player);
This would mean that another player could build using emporiums from another player which shouldn't be possible, a player can only build using its own emporiums.
The only other solution that eclipse gives me is to create a local variable Player player; but then it says that it's not initialized, is there another way?
I've researched this and so far not found what I was looking for here on the site. I'm more or less trying my hand at a semi-basic text based adventure type game where the player chooses an option such as (l)ook, (w)ander, (s)leep, etc. I've created the class for Room, and have initialized it and all the rest, and descriptions and the like are set. Inside one of the options, you enter a cave, and I have not learned enough from others and my teacher to allow the description of the area you are in to change. The lines of code so far for this are as follows:
public void Gameloop(Room startRoom)
{
String input;
boolean notDead = false;
Room currentRoom = startRoom;
while(!notDead)
{
//Shows description of the room
input = JOptionPane.showInputDialog(null, currentRoom.getDescription() + " What do you do? \n(l)ook around\n(g)rab a stick\n(s)leep.");
that is for the start of the game where you are in a forest. The description shows properly and advancement through choices is all proper. The problem is with this area of code
if(input.equalsIgnoreCase("l"))
{
input = JOptionPane.showInputDialog(null,"You see trees and a cave. Do you want to go into the casve? \n(y)es \n(n)o");
if(input.equalsIgnoreCase("y"))
{
input = JOptionPane.showInputDialog(null,currentRoom.getDescription() + " Do you want to set up a camp?\n(y)es\n(n)o");
The problem specifically is that I have not learnt how to implement room changes at all, otherwise, the game basis would be sound and do-able, options would be more well thought out and an item system would later be implemented. Basically, how do I change the "room" the person is in. Note, this game has no GUI and is literally based on you typing a letter for each action
I would make use of the Strategy pattern here. Ill give you some pseudo code to get you started :), note this doesnt compile.
The idea is that you make a IRoom interface. This will allow you to make as many different kind of rooms as you like. Each room can define the actions that are possible in this room, so you can add certain actions to a forest room for example.
It also has a method that will make sure the room performs the action. This could result in a roomchange, thus it will return a new IRoom.
public class Game {
private IRoom currentRoom;
public void Gameloop(IRoom startRoom){
while(!notDead)
{
//Shows description of the room
input = JOptionPane.showInputDialog(null, currentRoom.getDescription() + " What do you do? " + dissplayPossibleActionsToUser(startRoom.getPossibleActionsInRoom()));
Action chosenAction = dericeActionFromImput(input);
currentRoom = startRoom.performAction(chosenAction);
}
}
}
public interface IRoom{
//Returns all the actions the current room provides
List<Actions> getPossibleActionsInRoom();
//Does the action
IRoom performAction(Action action);
}
public enum Actions{
LOOK, WANDER, SLEEP;
}
public class Forest implements IRoom{
public List<Actions> getPossibleActionsInRoom(){
return new ArrayList(Actions.wander);
}
public IRoom performAction(Action action){
if(action == wander){
return new HouseRoom();
}
}
}