public class ChairTile extends TileEntity implements IHostedPeripheral { public static String Username; private static IComputerAccess pc; private static boolean canWaitForKey = true; private int side; private boolean startPressing = false; public static String keyDown = null; public ChairTile(){ } public ChairTile(String player){ this.Username = player; } @Override public String getType() { return "Russoul"; } @Override public String[] getMethodNames() { return new String[]{"getPlayer","setPlayer","getKey","canWaitForKey","openPC","setSide","getSide"}; } @Override public Object[] callMethod(IComputerAccess computer,ILuaContext event, int method, Object[] args) throws Exception { this.pc = computer; if(Username != null){ if(method == 0){ return new Object[]{Username}; }else if(method == 1){ if(args.length == 1){ String arg = String.valueOf(args[0]); this.Username = arg; }else{ throw new Exception("args length should be 1 arg"); } }else if(method == 2){ Object[] info = null; if(canWaitForKey){ while(true){ this.startPressing = true; info = event.pullEvent("My_Key_Event"); this.startPressing = false; return info; } }else{ throw new Exception("Can't !"); } }else if(method == 3){ if(args.length == 1){ if(args[0].getClass().equals(Boolean.class)){ this.canWaitForKey = (Boolean) args[0]; }else{ throw new Exception("arg must be a boolean"); } }else{ throw new Exception("args length must be 1 arg"); } }else if(method == 4){ if(Username != null && pc != null){ TileEntityComputer cmp = this.getComputerBySide(side); if(cmp != null){ MyTickHandler.Computer = this.getComputerBySide(side); MyTickHandler.Username = Username; MyTickHandler.openPC = true; return null; }else{ throw new Exception("PC's Tile is not found !"); } }else{ throw new Exception("Player or(and) PC is(are) null"); } }else if(method == 5){ if(Username != null && pc != null){ if(args.length == 1){ this.side = (Integer) args[0]; } }else{ throw new Exception("Player or(and) PC is(are) null"); } }else if(method == 6){ if(Username != null && pc != null){ return new Object[]{side}; }else{ throw new Exception("Player or(and) PC is(are) null"); } } } return null; } @Override public boolean canAttachToSide(int side) { return true; } @Override public void attach(IComputerAccess computer) { this.pc = computer; } @Override public void detach(IComputerAccess computer) { this.pc = null; } @Override public void update() { if(pc != null && startPressing == true){ MyTickHandler.pressKeyForPC(Username, pc); } } @Override public void writeToNBT(NBTTagCompound par1NBTTagCompound){ super.writeToNBT(par1NBTTagCompound); if(this.Username != null){ par1NBTTagCompound.setString("owner", Username); }else{ par1NBTTagCompound.setString("owner", "none"); } par1NBTTagCompound.setBoolean("canWaitForKey",canWaitForKey); par1NBTTagCompound.setInteger("side", side); } public void readFromNBT(NBTTagCompound par1NBTTagCompound){ super.readFromNBT(par1NBTTagCompound); this.Username = par1NBTTagCompound.getString("owner"); this.canWaitForKey = par1NBTTagCompound.getBoolean("canWaitForKey"); this.side = par1NBTTagCompound.getInteger("side"); } protected TileEntityComputer getNearByComputer(){ TileEntity[] tiles = new TileEntity[6]; tiles[0] = this.worldObj.getBlockTileEntity(this.xCoord+1, this.yCoord, this.zCoord); tiles[1] = this.worldObj.getBlockTileEntity(this.xCoord-1, this.yCoord, this.zCoord); tiles[2] = this.worldObj.getBlockTileEntity(this.xCoord, this.yCoord+1, this.zCoord); tiles[3] = this.worldObj.getBlockTileEntity(this.xCoord, this.yCoord-1, this.zCoord); tiles[4] = this.worldObj.getBlockTileEntity(this.xCoord, this.yCoord, this.zCoord+1); tiles[5] = this.worldObj.getBlockTileEntity(this.xCoord, this.yCoord, this.zCoord-1); for(int i = 0;i<6;i++){ if(tiles[i] instanceof IComputerAccess){ return (TileEntityComputer) tiles[i]; } } return null; } protected TileEntityComputer getComputerBySide(int side){ if(side >= 0 && side <= 5){ if(side == 0){ TileEntity tile = this.worldObj.getBlockTileEntity(this.xCoord, this.yCoord-1, this.zCoord); if(tile != null && tile instanceof TileEntityComputer){ return (TileEntityComputer) tile; } }else if(side == 1){ TileEntity tile = this.worldObj.getBlockTileEntity(this.xCoord, this.yCoord+1, this.zCoord); if(tile != null && tile instanceof TileEntityComputer){ return (TileEntityComputer) tile; } }else if(side == 2){ TileEntity tile = this.worldObj.getBlockTileEntity(this.xCoord, this.yCoord, this.zCoord-1); if(tile != null && tile instanceof TileEntityComputer){ return (TileEntityComputer) tile; } }else if(side == 3){ TileEntity tile = this.worldObj.getBlockTileEntity(this.xCoord, this.yCoord, this.zCoord+1); if(tile != null && tile instanceof TileEntityComputer){ return (TileEntityComputer) tile; } }else if(side == 4){ TileEntity tile = this.worldObj.getBlockTileEntity(this.xCoord+1, this.yCoord, this.zCoord); if(tile != null && tile instanceof TileEntityComputer){ return (TileEntityComputer) tile; } }else if(side == 5){ TileEntity tile = this.worldObj.getBlockTileEntity(this.xCoord-1, this.yCoord, this.zCoord); if(tile != null && tile instanceof TileEntityComputer){ return (TileEntityComputer) tile; } } } return null; } }