I am trying to program a minecraft server, but I need to access the getCurrentActiveItem(or something like that) method from the EntityPlayer class. The reason for needing to do this is to have an item in hand do something like heal the player or something. The problem is, Nothing is static. I learned to code from a program called youth Digital, and they don't allow me to edit any code that was not created by me, so I cannot just put static on this method. I did some research and found some pretty specific answers. They were about making a new instance of a class, I guess. Putting it in the code just gives me an error. I have tried stuff like this:
EntityPlayer player = new EntityPlayer.class;
public class player = new EntityPlayer.class;
class player = player.instanceOf("EntityPlayer.class");
and other similar stuff. All gave me an error that I am not advanced enough to decipher. Here is my code:
package myservermod;
import com.youthdigital.servermod.game.*;
public class Player extends PlayerData {
public Player(EntityPlayer parPlayerObject) {
super(parPlayerObject);
}
@Override
public void onUpdate() {
/*Cheats*/
//Teleport Cheat
if(Conditions.cheatEntered("teleport")){
Actions.teleportPlayers(GameManager.getStringFromCheat(1));
}
/*Red Team*/
//Enter the Red Team
if(Conditions.didRightClickBlock("redTeamEntrance")){
Actions.teleportPlayers("redTeamBase");
}
if(Conditions.didRightClickBlock("dirtBlockBuy")){
Actions.setBlockWithMetaAtPos("redDirtButton" , Blocks.stone_button, 3);
}
}
@Override
public void onJoinedServer(){
Actions.teleportPlayers("lobby");
}
@Override
public void onStartGame() {
}
@Override
public void onResetGameToLobby() {
Actions.teleportPlayers("lobby");
}
@Override
public void onRespawned() {
}
}
Ok, you're mentioning what you can't access because it's static.
The thing is, you should not access it in a static way!
player.getItemInHand()
is a method that must be accessed from an object, so it returns the ItemStack that is in the hand of the player
, not on a static hand (belongs to no object, therefore nobody!).
What you should do:
To get the ItemStack in a player's hand by the player's name
Player player = Bukkit.getPlayer("YourPlayer"); //Notice that the method getPlayer() is static to Bukkit!
ItemStack item = player.getItemInHand(); //Notice that you're accessing your object player, not creating a completely new one, and not accessing it statically!
You mostly likely want to detect the Item in Hand from an event, for example to spawn a chicken when you click with a stick:
(Take a look at the bukkit event handling documentation for further event-handling knowledge)
@EventHandler
public void onPlayerInteract(PlayerInteractEvent event) {
Player player = event.getPlayer();
ItemStack is = player.getItemInHand();
}