[英]How to reference an EntityPlayer method in a class
我正在嘗試編寫一個Minecraft服務器,但我需要從EntityPlayer類訪問getCurrentActiveItem(或類似的東西)方法。 需要這樣做的原因是讓一個項目在做一些像治療玩家或其他什么的東西。 問題是, 沒有什么是靜態的。 我學會了編寫一個名為youth Digital的程序代碼,他們不允許我編輯任何不是由我創建的代碼,所以我不能把靜態放在這個方法上。 我做了一些研究,發現了一些非常具體的答案。 我猜他們是關於創建一個新類的實例。 將它放在代碼中只會給我一個錯誤。 我試過這樣的東西:
EntityPlayer player = new EntityPlayer.class;
public class player = new EntityPlayer.class;
class player = player.instanceOf("EntityPlayer.class");
和其他類似的東西。 所有這些都給了我一個錯誤,我沒有足夠的進展來破譯。 這是我的代碼:
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() {
}
}
好的,你提到你無法訪問的內容,因為它是靜態的。
問題是,你不應該以靜態的方式訪問它!
player.getItemInHand()
是一個必須從對象訪問的方法,因此它返回player
手中的ItemStack,而不是靜態手(不屬於任何對象,因此沒有人!)。
你應該做什么:
通過玩家的名字在玩家手中獲取ItemStack
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!
你很可能想要從一個事件中檢測出手中的物品,例如當你用棍子點擊時產生一只雞:
(查看bukkit事件處理文檔以獲取更多事件處理知識)
@EventHandler
public void onPlayerInteract(PlayerInteractEvent event) {
Player player = event.getPlayer();
ItemStack is = player.getItemInHand();
}
HumanEntity上有getItemInHand
。 Bukkit JavaDocs
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.