Advertisement
PeachGaming

Untitled

Apr 18th, 2020
604
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.40 KB | None | 0 0
  1. package train.common.mtc;
  2.  
  3. import cpw.mods.fml.common.Optional;
  4. import dan200.computercraft.api.lua.ILuaContext;
  5. import dan200.computercraft.api.lua.LuaException;
  6. import dan200.computercraft.api.peripheral.IComputerAccess;
  7. import dan200.computercraft.api.peripheral.IPeripheral;
  8. import li.cil.oc.api.Network;
  9. import li.cil.oc.api.machine.Arguments;
  10. import li.cil.oc.api.machine.Callback;
  11. import li.cil.oc.api.machine.Context;
  12. import li.cil.oc.api.network.Environment;
  13. import li.cil.oc.api.network.Message;
  14. import li.cil.oc.api.network.Node;
  15. import li.cil.oc.api.network.SimpleComponent;
  16. import net.minecraft.nbt.NBTTagCompound;
  17. import net.minecraft.tileentity.TileEntity;
  18. import net.minecraft.util.AxisAlignedBB;
  19. import train.common.api.Locomotive;
  20.  
  21. import java.util.ArrayList;
  22. import java.util.List;
  23. @Optional.Interface(iface = "li.cil.oc.api.network.SimpleComponent", modid = "OpenComputers")
  24.  
  25. public class TileInfoGrabberDestination extends TileEntity implements IPeripheral, SimpleComponent, Environment {
  26.     public Boolean isActivated = false;
  27.     public String trainDestination = "";
  28.     public Boolean trainThere = false;
  29.     public AxisAlignedBB boundingBox = null;
  30.     public ArrayList<IComputerAccess> computers = new ArrayList<IComputerAccess>();
  31.     protected boolean addedToNetwork = false;
  32.     public Node node = null;
  33.     public TileInfoGrabberDestination() {
  34.  
  35.     }
  36.  
  37.     @Override
  38.     public void readFromNBT(NBTTagCompound nbttagcompound) {
  39.         super.readFromNBT(nbttagcompound);
  40.  
  41.         this.isActivated = nbttagcompound.getBoolean("isActivated");
  42.         this.trainDestination = nbttagcompound.getString("trainDestination");
  43.         //this.trainID = nbttagcompound.getString("trainID");
  44.         if (node != null && node.host() == this) {
  45.             // This restores the node's address, which is required for networks
  46.             // to continue working without interruption across loads. If the
  47.             // node is a power connector this is also required to restore the
  48.             // internal energy buffer of the node.
  49.             node.load(nbttagcompound.getCompoundTag("oc:node"));
  50.         }
  51.     }
  52.  
  53.     @Override
  54.     public AxisAlignedBB getRenderBoundingBox() {
  55.         if (boundingBox == null) {
  56.             boundingBox = AxisAlignedBB.getBoundingBox(xCoord, yCoord, zCoord, xCoord + 2, yCoord + 2, zCoord + 2);
  57.         }
  58.         return boundingBox;
  59.     }
  60.  
  61.     @Override
  62.     public void writeToNBT(NBTTagCompound nbttagcompound) {
  63.         super.writeToNBT(nbttagcompound);
  64.         //    nbttagcompound.setBoolean("isActivated", this.isActivated);
  65.         //   nbttagcompound.setInteger("setSpeed", this.setSpeed);
  66.  
  67.         nbttagcompound.setBoolean("isActivated", this.isActivated);
  68.         nbttagcompound.setString("trainDestination", this.trainDestination);
  69.         if (node != null && node.host() == this) {
  70.             final NBTTagCompound nodeNbt = new NBTTagCompound();
  71.             node.save(nodeNbt);
  72.             nbttagcompound.setTag("oc:node", nodeNbt);
  73.         }
  74.     }
  75.  
  76.     @Override
  77.     public String getType() {
  78.         return "info_receiver_destination";
  79.     }
  80.  
  81.     @Override
  82.     public String[] getMethodNames() {
  83.         return new String[]  {"activate", "deactivate", "getDestination", "isTrainOverSensor"};
  84.     }
  85.  
  86.     @Override
  87.     public Object[] callMethod(IComputerAccess computer, ILuaContext context, int method, Object[] arguments) throws LuaException, InterruptedException {
  88.         switch(method) {
  89.             case 0: {
  90.                 isActivated = true;
  91.                 return new Object[] {true};
  92.  
  93.             } case 1: {
  94.                 isActivated = false;
  95.                 return new Object[] {true};
  96.  
  97.             } case 2: {
  98.  
  99.                 return new Object[]{trainDestination};
  100.  
  101.             } case 3: {
  102.                 return new Object[] {trainThere};
  103.  
  104.  
  105.  
  106.             } default:
  107.                 return new Object[] {"nil"};
  108.         }
  109.     }
  110.  
  111.     @Override
  112.     public void updateEntity() {
  113.         if (worldObj == null) {
  114.             return;
  115.         }
  116.         if (!addedToNetwork) {
  117.             addedToNetwork = true;
  118.             Network.joinOrCreateNetwork(this);
  119.         }
  120.  
  121.         if (isActivated) {
  122.             List<Object> list = this.worldObj.getEntitiesWithinAABBExcludingEntity(null, this.getRenderBoundingBox());
  123.             if (list != null && list.size() > 0) {
  124.                 for (Object obj : list) {
  125.  
  126.                     if (obj instanceof Locomotive) {
  127.  
  128.                         Locomotive daTrain = (Locomotive) obj;
  129.                         if (daTrain.mtcOverridePressed) { return;}
  130.                        trainThere = true;
  131.                        trainDestination = daTrain.getDestinationGUI();
  132.                         try {
  133.                             if (computers != null && computers.size() > 0) {
  134.                                 for (IComputerAccess c : computers) {
  135.                                     c.queueEvent("mtc_trainoversensor", new Object[]{c.getAttachmentName()});
  136.  
  137.                                     // System.out.println(message.message);
  138.                                 }
  139.                             }
  140.                             //Ooor, if it's OpenComputers..
  141.  
  142.                             if (node != null) {
  143.                                 node().sendToReachable("computer.signal","trainOverSensor", node.address());
  144.                             }
  145.                         } catch (Exception ex) {
  146.                             ex.printStackTrace();
  147.                         }
  148.                     }
  149.                 }
  150.             } else {
  151.                 trainThere = false;
  152.             }
  153.         }
  154.     }
  155.  
  156.  
  157.     public void attach(IComputerAccess computer) {
  158.         computers.add(computer);
  159.     }
  160.  
  161.     @Override
  162.     public void detach(IComputerAccess computer) {
  163.         computers.remove(computer);
  164.     }
  165.  
  166.     @Override
  167.     public boolean equals(IPeripheral other) {
  168.         return false;
  169.     }
  170.  
  171.     //OpenComputers!
  172.     @Override
  173.     public String getComponentName() {
  174.         return "info_receiver_destination";
  175.     }
  176.     @Callback
  177.     @Optional.Method(modid = "OpenComputers")
  178.     public Object[] activate(Context context, Arguments args) {
  179.         this.isActivated = true;
  180.         return new Object[]{true};
  181.     }
  182.     @Callback
  183.     @Optional.Method(modid = "OpenComputers")
  184.     public Object[] deactivate(Context context, Arguments args) {
  185.         this.isActivated = false;
  186.         return new Object[]{true};
  187.     }
  188.     @Callback
  189.     @Optional.Method(modid = "OpenComputers")
  190.     public Object[] getDestination(Context context, Arguments args) {
  191.         return new Object[]{trainDestination};
  192.     }
  193.     @Callback
  194.     @Optional.Method(modid = "OpenComputers")
  195.     public Object[] isTrainOverSensor(Context context, Arguments args) {
  196.         return new Object[]{trainThere};
  197.     }
  198.  
  199.     public Node node() {
  200.         return node;
  201.     }
  202.  
  203.     @Optional.Method(modid = "OpenComputers")
  204.     @Override
  205.     public void onConnect(Node node) {
  206.         this.node = node;
  207.     }
  208.  
  209.     @Override
  210.     public void onDisconnect(Node node) {
  211.  
  212.     }
  213.  
  214.     @Override
  215.     public void onMessage(Message message) {}
  216.  
  217.     @Override
  218.     public void onChunkUnload() {
  219.         super.onChunkUnload();
  220.         if (node != null) node.remove();
  221.     }
  222.  
  223.     @Override
  224.     public void invalidate() {
  225.         super.invalidate();
  226.         if (node != null) node.remove();
  227.     }
  228.  
  229. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement