如果您在键盘 GUI 中键入的代码是正确的,我正在尝试使用 setBlock 命令来放置我的键盘的活动(发出红石电源)版本。我即将向您展示的代码在客户端(单人游戏)端完美运行,但在服务器(多人游戏)端却没有。继承人的代码:
BlockKeypad 'onBlockActivated' 代码:
public boolean onBlockActivated(World par1World, int par2, int par3, int par4, EntityPlayer par5EntityPlayer, int par6, float par7, float par8, float par9){
this.playerObj = par5EntityPlayer;
this.lastKeypadX = par2;
this.lastKeypadY = par3;
this.lastKeypadZ = par4;
if(par1World instanceof WorldServer){
this.worldServerObj = par1World;
TileEntityKeypad TEK = (TileEntityKeypad) par1World.getBlockTileEntity(par2, par3, par4);
System.out.println(TEK.getKeypadCode() + " | Code from TileEntity");
this.openCode = TEK.getKeypadCode();
if(TEK.getKeypadCode() == 0){
par5EntityPlayer.openGui(mod_SecurityCraft.instance, 1, par1World, par2, par3, par4);
}else{
par5EntityPlayer.openGui(mod_SecurityCraft.instance, 0, par1World, par2, par3, par4);
}
return true;
}else{
this.worldObj = par1World;
//Rest of client code here
return true;
}
}
Gui键盘代码:
private void checkCode(String par1String) {
int code = 0;
try{
code = Integer.parseInt(par1String);
}catch(Exception e){
e.printStackTrace();
}
System.out.println(BlockKeypad.openCode + " | GUI");
if(code == BlockKeypad.openCode){
new ScheduleUpdate(3, BlockKeypad.worldServerObj, BlockKeypad.lastKeypadX,BlockKeypad.lastKeypadY, BlockKeypad.lastKeypadZ, BlockKeypad.openCode, this.keypadInventory);
BlockKeypad.playerObj.closeScreen();
}
}
ScheduleUpdate 类:
public class ScheduleUpdate{
Timer timer;
private int xCoord;
private int yCoord;
private int zCoord;
private int metadata;
private int passcode;
private TileEntityKeypad TEK;
public ScheduleUpdate(int seconds, World par2World, int par3, int par4, int par5, int par6, TileEntityKeypad keypadInventory){
timer = new Timer();
timer.schedule(new RemindTask(), seconds*1000); //TODO 60
xCoord = par3;
yCoord = par4;
zCoord = par5;
passcode = par6;
TEK = keypadInventory;
//BlockKeypad.shouldCheckMeta = false;
metadata = par2World.getBlockMetadata(par3, par4 , par5);
System.out.println(metadata + " | metadata");
par2World.setBlock(par3, par4, par5, mod_SecurityCraft.KeypadActive.blockID, metadata, 3);
par2World.notifyBlocksOfNeighborChange(par3, par4 , par5, mod_SecurityCraft.KeypadActive.blockID);
}
class RemindTask extends TimerTask{
public void run(){
BlockKeypad.worldObj.setBlock(xCoord, yCoord, zCoord, mod_SecurityCraft.Keypad.blockID, metadata, 3);
BlockKeypad.worldObj.notifyBlocksOfNeighborChange(xCoord, yCoord, zCoord, mod_SecurityCraft.Keypad.blockID);
((TileEntityKeypad) BlockKeypad.worldObj.getBlockTileEntity(xCoord, yCoord, zCoord)).setKeypadCode(passcode);
timer.cancel();
}
}
}
如果我使用我现在拥有的这段代码,由于某种原因,当我创建一个新的 ScheduleUpdate 对象(在 GuiKeypad 代码中)时,它会在 BlockKeypad.worldServerObj 上给我一个 NullPointerException。如果我用 BlockKeypad.worldObj (我的客户端 World 对象)切换它,它看起来像是放置了块,但是如果我更新新块(右键单击它,在它旁边放置一个块等),它就会消失. 所以我猜我必须使用worldServerObj,但同样,每当我尝试使用它时它都会抛出NullPointerException,即使我在GUI打开之前设置为服务器的World对象。
我为 System.out.println() 制作了一个项目,这是我在 BlockKeypad 类中制作的两个世界对象。
当我右键单击它时,它会在控制台(客户端)中打印:
net.minecraft.multiplayer.WorldClient()
null
在服务器控制台中:
null
net.minecraft.multiplayer.WorldServer()
有谁知道怎么了?我将非常感谢任何帮助,因为这是唯一阻止我发布我的 mod 的多人兼容版本的事情。谢谢!