Advertisement
Guest User

PeripheralTileEntity

a guest
Aug 28th, 2013
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.00 KB | None | 0 0
  1. public class ChairTile extends TileEntity implements IHostedPeripheral {
  2.    
  3.     public static String Username;
  4.     private static IComputerAccess pc;
  5.     private static boolean canWaitForKey = true;
  6.     private int side;
  7.     private boolean startPressing = false;
  8.     public static String keyDown = null;
  9.    
  10.    
  11.     public ChairTile(){
  12.        
  13.     }
  14.    
  15.     public ChairTile(String player){
  16.         this.Username = player;
  17.     }
  18.    
  19.  
  20.     @Override
  21.     public String getType() {
  22.         return "Russoul";
  23.     }
  24.  
  25.     @Override
  26.     public String[] getMethodNames() {
  27.         return new String[]{"getPlayer","setPlayer","getKey","canWaitForKey","openPC","setSide","getSide"};
  28.     }
  29.  
  30.     @Override
  31.     public Object[] callMethod(IComputerAccess computer,ILuaContext event, int method,
  32.             Object[] args) throws Exception {
  33.         this.pc = computer;
  34.         if(Username != null){
  35.             if(method == 0){
  36.                 return new Object[]{Username};
  37.             }else if(method == 1){
  38.                 if(args.length == 1){
  39.                     String arg = String.valueOf(args[0]);
  40.                     this.Username = arg;
  41.                    
  42.                 }else{
  43.                     throw new Exception("args length should be 1 arg");
  44.                    
  45.                 }
  46.                
  47.             }else if(method == 2){
  48.                 Object[] info = null;
  49.                 if(canWaitForKey){
  50.                     while(true){
  51.                         this.startPressing = true;
  52.                        
  53.                         info = event.pullEvent("My_Key_Event");
  54.                        
  55.                         this.startPressing  = false;
  56.                         return info;
  57.                     }
  58.                 }else{
  59.                     throw new Exception("Can't !");
  60.                 }
  61.                
  62.                
  63.                
  64.             }else if(method == 3){
  65.                 if(args.length == 1){
  66.                     if(args[0].getClass().equals(Boolean.class)){
  67.                         this.canWaitForKey = (Boolean) args[0];
  68.                     }else{
  69.                         throw new Exception("arg must be a boolean");
  70.                     }
  71.                 }else{
  72.                     throw new Exception("args length must be 1 arg");
  73.                 }
  74.             }else if(method == 4){
  75.                 if(Username != null && pc != null){
  76.                     TileEntityComputer cmp = this.getComputerBySide(side);
  77.                     if(cmp != null){
  78.                         MyTickHandler.Computer = this.getComputerBySide(side);
  79.                         MyTickHandler.Username = Username;
  80.                         MyTickHandler.openPC = true;
  81.                         return null;
  82.                     }else{
  83.                         throw new Exception("PC's Tile is not found !");
  84.                     }
  85.                 }else{
  86.                     throw new Exception("Player or(and) PC is(are) null");
  87.                 }
  88.             }else if(method == 5){
  89.                 if(Username != null && pc != null){
  90.                     if(args.length == 1){
  91.                         this.side = (Integer) args[0];
  92.                        
  93.                     }
  94.                 }else{
  95.                     throw new Exception("Player or(and) PC is(are) null");
  96.                 }
  97.             }else if(method == 6){
  98.                 if(Username != null && pc != null){
  99.                     return new Object[]{side};
  100.                 }else{
  101.                     throw new Exception("Player or(and) PC is(are) null");
  102.                 }
  103.             }
  104.  
  105.            
  106.         }
  107.         return null;
  108.     }
  109.  
  110.     @Override
  111.     public boolean canAttachToSide(int side) {
  112.         return true;
  113.     }
  114.  
  115.     @Override
  116.     public void attach(IComputerAccess computer) {
  117.         this.pc = computer;
  118.        
  119.     }
  120.  
  121.     @Override
  122.     public void detach(IComputerAccess computer) {
  123.         this.pc = null;
  124.     }
  125.  
  126.     @Override
  127.     public void update() {
  128.         if(pc != null && startPressing == true){
  129.             MyTickHandler.pressKeyForPC(Username, pc);
  130.            
  131.         }
  132.        
  133.        
  134.     }
  135.    
  136.     @Override
  137.     public void writeToNBT(NBTTagCompound par1NBTTagCompound){
  138.         super.writeToNBT(par1NBTTagCompound);
  139.         if(this.Username != null){
  140.             par1NBTTagCompound.setString("owner", Username);
  141.         }else{
  142.             par1NBTTagCompound.setString("owner", "none");
  143.         }
  144.        
  145.         par1NBTTagCompound.setBoolean("canWaitForKey",canWaitForKey);
  146.         par1NBTTagCompound.setInteger("side", side);
  147.        
  148.     }
  149.    
  150.     public void readFromNBT(NBTTagCompound par1NBTTagCompound){
  151.         super.readFromNBT(par1NBTTagCompound);
  152.         this.Username = par1NBTTagCompound.getString("owner");
  153.         this.canWaitForKey = par1NBTTagCompound.getBoolean("canWaitForKey");
  154.         this.side = par1NBTTagCompound.getInteger("side");
  155.        
  156.     }
  157.    
  158.     protected TileEntityComputer getNearByComputer(){
  159.         TileEntity[] tiles = new TileEntity[6];
  160.         tiles[0] = this.worldObj.getBlockTileEntity(this.xCoord+1, this.yCoord, this.zCoord);
  161.         tiles[1] = this.worldObj.getBlockTileEntity(this.xCoord-1, this.yCoord, this.zCoord);
  162.         tiles[2] = this.worldObj.getBlockTileEntity(this.xCoord, this.yCoord+1, this.zCoord);
  163.         tiles[3] = this.worldObj.getBlockTileEntity(this.xCoord, this.yCoord-1, this.zCoord);
  164.         tiles[4] = this.worldObj.getBlockTileEntity(this.xCoord, this.yCoord, this.zCoord+1);
  165.         tiles[5] = this.worldObj.getBlockTileEntity(this.xCoord, this.yCoord, this.zCoord-1);
  166.         for(int i = 0;i<6;i++){
  167.             if(tiles[i] instanceof IComputerAccess){
  168.                 return (TileEntityComputer) tiles[i];
  169.             }
  170.         }
  171.         return null;
  172.     }
  173.    
  174.     protected TileEntityComputer getComputerBySide(int side){
  175.         if(side >= 0 && side <= 5){
  176.             if(side == 0){
  177.                 TileEntity tile = this.worldObj.getBlockTileEntity(this.xCoord, this.yCoord-1, this.zCoord);
  178.                 if(tile != null && tile instanceof TileEntityComputer){
  179.                    return (TileEntityComputer) tile;
  180.                 }
  181.             }else if(side == 1){
  182.                 TileEntity tile = this.worldObj.getBlockTileEntity(this.xCoord, this.yCoord+1, this.zCoord);
  183.                 if(tile != null && tile instanceof TileEntityComputer){
  184.                     return (TileEntityComputer) tile;
  185.                 }
  186.             }else if(side == 2){
  187.                 TileEntity tile = this.worldObj.getBlockTileEntity(this.xCoord, this.yCoord, this.zCoord-1);
  188.                 if(tile != null && tile instanceof TileEntityComputer){
  189.                     return (TileEntityComputer) tile;
  190.                 }
  191.             }else if(side == 3){
  192.                 TileEntity tile = this.worldObj.getBlockTileEntity(this.xCoord, this.yCoord, this.zCoord+1);
  193.                 if(tile != null && tile instanceof TileEntityComputer){
  194.                     return (TileEntityComputer) tile;
  195.                 }
  196.             }else if(side == 4){
  197.                 TileEntity tile = this.worldObj.getBlockTileEntity(this.xCoord+1, this.yCoord, this.zCoord);
  198.                 if(tile != null && tile instanceof TileEntityComputer){
  199.                     return (TileEntityComputer) tile;
  200.                 }
  201.             }else if(side == 5){
  202.                 TileEntity tile = this.worldObj.getBlockTileEntity(this.xCoord-1, this.yCoord, this.zCoord);
  203.                 if(tile != null && tile instanceof TileEntityComputer){
  204.                     return (TileEntityComputer) tile;
  205.                 }
  206.             }
  207.         }
  208.         return null;
  209.     }
  210.    
  211.    
  212.    
  213.  
  214. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement