gpt4 book ai didi

java - 如何在类中引用 EntityPlayer 方法

转载 作者:太空宇宙 更新时间:2023-11-04 11:55:01 28 4
gpt4 key购买 nike

我正在尝试对 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() 是一个必须从对象访问的方法,因此它返回 玩家 手中的 ItemStack,而不是静态手中的 ItemStack(不属于任何对象,因此没有人!)。

你应该做什么:

  1. 首先,也是更重要的一点:您不应该永远通过 new 创建新玩家。Player 是 Bukkit 创建的对象,您不应该尝试创建自己的新 Player。
  2. 您应该通过说明您想要进入 bukkit 的玩家来获取您的玩家:

通过玩家名字获取玩家手中的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!

您很可能希望从事件中检测手中的元素,例如当您用棍子点击时生成一只鸡:

(查看 the bukkit event handling documentation 了解更多事件处理知识)

@EventHandler
public void onPlayerInteract(PlayerInteractEvent event) {
Player player = event.getPlayer();
ItemStack is = player.getItemInHand();
}

关于java - 如何在类中引用 EntityPlayer 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41446339/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com