Advertisement
Guest User

Untitled

a guest
Mar 17th, 2014
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.82 KB | None | 0 0
  1. package redstoneInMotion;
  2.  
  3. import java.util.HashMap;
  4. import java.util.Map;
  5.  
  6. import cpw.mods.fml.common.Optional;
  7.  
  8. import dan200.computer.api.IComputerAccess;
  9. import dan200.computer.api.ILuaContext;
  10. import dan200.computer.api.IPeripheral;
  11.  
  12. import li.cil.oc.api.network.Arguments;
  13. import li.cil.oc.api.network.Context;
  14. import li.cil.oc.api.network.ManagedPeripheral;
  15. import li.cil.oc.api.network.SimpleComponent;
  16.  
  17.  
  18. @Optional.InterfaceList({
  19. @Optional.Interface(iface = "li.cil.oc.api.network.SimpleComponent", modid = "OpenComputers"),
  20. @Optional.Interface(iface = "li.cil.oc.api.network.ManagedPeripheral", modid = "OpenComputers"),
  21. @Optional.Interface(iface = "dan200.computer.api.IPeripheral", modid = "ComputerCraft")
  22. })
  23. public class CarriageControllerEntity extends CarriageDriveEntity implements IPeripheral, SimpleComponent, ManagedPeripheral
  24. {
  25.  
  26. public enum ComputerMethod
  27. {
  28. move,
  29. anchored_move,
  30. check_anchored_move,
  31. unanchored_move,
  32. check_unanchored_move;
  33. }
  34.  
  35. public static final int numMethods = ComputerMethod.values().length;
  36.  
  37. public static final String[] methodNames = new String[numMethods]; {
  38. for (int Index = 0; Index < numMethods; Index ++)
  39. {
  40. methodNames [Index] = ComputerMethod.values()[Index].name ();
  41. }
  42. }
  43.  
  44. public static final Map<String, Integer> methodIds = new HashMap<String, Integer>();
  45. static {
  46. for (int i = 0; i < numMethods; ++i) {
  47. methodIds.put(methodNames[i], i);
  48. }
  49. }
  50.  
  51. public Object ThreadLockObject = new Object();
  52. public boolean Simulating;
  53. public Directions MotionDirection;
  54. public CarriageMotionException Error;
  55. public boolean Obstructed;
  56. public int ObstructionX;
  57. public int ObstructionY;
  58. public int ObstructionZ;
  59.  
  60. @Override
  61. public void HandleToolUsage (int Side, boolean Sneaking)
  62. {
  63. }
  64.  
  65. @Override
  66. public synchronized void updateEntity()
  67. {
  68. if (worldObj.isRemote)
  69. {
  70. return;
  71. }
  72.  
  73. if (Stale)
  74. {
  75. HandleNeighbourBlockChange();
  76. }
  77.  
  78. if (MotionDirection == null)
  79. {
  80. return;
  81. }
  82.  
  83. try
  84. {
  85. Move();
  86. }
  87. catch (CarriageMotionException Error)
  88. {
  89. this.Error = Error;
  90.  
  91. if (Error instanceof CarriageObstructionException)
  92. {
  93. Obstructed = true;
  94.  
  95. ObstructionX = ((CarriageObstructionException) Error).X;
  96. ObstructionY = ((CarriageObstructionException) Error).Y;
  97. ObstructionZ = ((CarriageObstructionException) Error).Z;
  98. }
  99. }
  100. MotionDirection = null;
  101. notify ();
  102. }
  103.  
  104. public boolean Anchored;
  105.  
  106. @Override
  107. public boolean Anchored()
  108. {
  109. return (Anchored);
  110. }
  111.  
  112.  
  113.  
  114. public boolean ParseBooleanArgument (Object Argument, String Label) throws Exception
  115. {
  116. try
  117. {
  118. return ((Boolean) Argument);
  119. }
  120. catch (Throwable Throwable)
  121. {
  122. throw (new Exception ("invalid " + Label + " flag"));
  123. }
  124. }
  125.  
  126. public Directions ParseDirectionArgument (Object Argument) throws Exception
  127. {
  128. if (Argument instanceof Double)
  129. {
  130. try
  131. {
  132. return (Directions.values () [ (int) Math.round ((Double) Argument) ]);
  133. }
  134. catch (Throwable Throwable)
  135. {
  136. throw (new Exception ("direction index out of range"));
  137. }
  138. }
  139.  
  140. try
  141. {
  142. String Direction = (String) Argument;
  143.  
  144. if (Direction.equalsIgnoreCase ("down") || Direction.equalsIgnoreCase ("negy"))
  145. {
  146. return (Directions.NegY);
  147. }
  148.  
  149. if (Direction.equalsIgnoreCase ("up") || Direction.equalsIgnoreCase ("posy"))
  150. {
  151. return (Directions. PosY);
  152. }
  153.  
  154. if (Direction.equalsIgnoreCase ("north") || Direction.equalsIgnoreCase ("negz"))
  155. {
  156. return (Directions.NegZ);
  157. }
  158.  
  159. if (Direction.equalsIgnoreCase ("south") || Direction.equalsIgnoreCase ("posz"))
  160. {
  161. return (Directions.PosZ);
  162. }
  163.  
  164. if (Direction.equalsIgnoreCase ("west") || Direction.equalsIgnoreCase ("negx"))
  165. {
  166. return (Directions.NegX);
  167. }
  168.  
  169. if (Direction.equalsIgnoreCase ("east") || Direction.equalsIgnoreCase ("posx"))
  170. {
  171. return (Directions.PosX);
  172. }
  173. }
  174. catch (Throwable Throwable)
  175. {
  176. }
  177.  
  178. throw (new Exception ("invalid direction"));
  179. }
  180.  
  181. public void SetupMotion (Directions MotionDirection, boolean Simulating, boolean Anchored)
  182. {
  183. this.MotionDirection = MotionDirection;
  184.  
  185. this.Simulating = Simulating;
  186.  
  187. this.Anchored = Anchored;
  188. }
  189.  
  190.  
  191. public synchronized Object[] callMethod (int MethodIndex, Object[] Arguments) throws Exception
  192. {
  193. try
  194. {
  195. switch (ComputerMethod.values () [ MethodIndex ])
  196. {
  197. case move :
  198.  
  199. if (Arguments.length != 3) {
  200. throw (new Exception ("usage: move('direction', true/false, true/false)"));
  201. }
  202.  
  203. SetupMotion (ParseDirectionArgument(Arguments[0]), ParseBooleanArgument(Arguments[1], "simulation"), ParseBooleanArgument(Arguments[2], "anchoring"));
  204.  
  205. break;
  206.  
  207. case anchored_move :
  208.  
  209. if (Arguments.length != 1) {
  210. throw (new Exception ("usage: anchored_move('direction')"));
  211. }
  212.  
  213. SetupMotion (ParseDirectionArgument(Arguments[0]), false, true);
  214.  
  215. break;
  216.  
  217. case check_anchored_move :
  218.  
  219. if (Arguments.length != 1) {
  220. throw (new Exception ("usage: check_anchored_move('direction')"));
  221. }
  222.  
  223. SetupMotion (ParseDirectionArgument(Arguments[0]), true, true);
  224.  
  225. break;
  226.  
  227. case unanchored_move :
  228.  
  229. if (Arguments.length != 1) {
  230. throw (new Exception ("usage: unanchored_move('direction')"));
  231. }
  232.  
  233. SetupMotion (ParseDirectionArgument(Arguments[0]), false, false);
  234.  
  235. break;
  236.  
  237. case check_unanchored_move :
  238.  
  239. if (Arguments.length != 1) {
  240. throw (new Exception ("usage: check_unanchored_move('direction')"));
  241. }
  242.  
  243. SetupMotion (ParseDirectionArgument(Arguments[0]), true, false);
  244.  
  245. break;
  246. }
  247. }
  248. catch (Throwable Throwable)
  249. {
  250. throw (new Exception ("no such command"));
  251. }
  252.  
  253. Error = null;
  254.  
  255. Obstructed = false;
  256.  
  257. try
  258. {
  259. do
  260. {
  261. wait ();
  262. }
  263. while (MotionDirection != null);
  264. }
  265. catch (Throwable Throwable)
  266. {
  267. throw (new Exception (Throwable));
  268. }
  269.  
  270. if (Error == null)
  271. {
  272. return (new Object[] { true });
  273. }
  274.  
  275. if (Obstructed == false)
  276. {
  277. return (new Object[] { false, Error.getMessage () });
  278. }
  279.  
  280. return (new Object[] { false, Error.getMessage (), ObstructionX, ObstructionY, ObstructionZ });
  281. }
  282.  
  283. public void Move () throws CarriageMotionException
  284. {
  285. if (Active)
  286. {
  287. throw (new CarriageMotionException ("controller already active"));
  288. }
  289.  
  290. if (CarriageDirection == null)
  291. {
  292. throw (new CarriageMotionException ("no carriage or too many carriages attached to controller"));
  293. }
  294.  
  295. CarriagePackage Package = PreparePackage (MotionDirection);
  296.  
  297. if (Simulating)
  298. {
  299. return;
  300. }
  301.  
  302. InitiateMotion (Package);
  303. }
  304.  
  305. @Override
  306. public CarriagePackage GeneratePackage (CarriageEntity Carriage, Directions CarriageDirection, Directions MotionDirection) throws CarriageMotionException
  307. {
  308. CarriagePackage Package;
  309.  
  310. if (Anchored)
  311. {
  312. if (MotionDirection == CarriageDirection)
  313. {
  314. throw (new CarriageMotionException ("cannot push carriage away from controller in anchored mode"));
  315. }
  316.  
  317. if (MotionDirection == CarriageDirection.Opposite ())
  318. {
  319. throw (new CarriageMotionException ("cannot pull carriage into controller in anchored mode"));
  320. }
  321.  
  322. Package = new CarriagePackage (this, Carriage, MotionDirection);
  323.  
  324. Carriage.FillPackage (Package);
  325.  
  326. if (Package.Body.contains (Package.DriveRecord))
  327. {
  328. throw (new CarriageMotionException ("carriage is attempting to move controller while in anchored mode"));
  329. }
  330.  
  331. if (Package.Body.contains (Package.DriveRecord.NextInDirection (MotionDirection.Opposite ())))
  332. {
  333. throw (new CarriageMotionException ("carriage is obstructed by controller while in anchored mode"));
  334. }
  335. }
  336. else
  337. {
  338. Package = new CarriagePackage (this, Carriage, MotionDirection);
  339.  
  340. Package.AddBlock (Package.DriveRecord);
  341.  
  342. if (MotionDirection != CarriageDirection)
  343. {
  344. Package.AddPotentialObstruction (Package.DriveRecord.NextInDirection (MotionDirection));
  345. }
  346.  
  347. Carriage.FillPackage (Package);
  348. }
  349.  
  350. Package.Finalize ();
  351.  
  352. return (Package);
  353. }
  354.  
  355. // ComputerCraft
  356.  
  357. @Optional.Method(modid = "ComputerCraft")
  358. @Override
  359. public String getType()
  360. {
  361. return ("RIM_CarriageController") ;
  362. }
  363.  
  364.  
  365. @Optional.Method(modid = "ComputerCraft")
  366. @Override
  367. public boolean canAttachToSide(int Side)
  368. {
  369. return ( true ) ;
  370. }
  371.  
  372. @Optional.Method(modid = "ComputerCraft")
  373. @Override
  374. public void attach(IComputerAccess Computer)
  375. {
  376. }
  377.  
  378. @Optional.Method(modid = "ComputerCraft")
  379. @Override
  380. public void detach(IComputerAccess Computer)
  381. {
  382. }
  383.  
  384. @Optional.Method(modid = "ComputerCraft")
  385. @Override
  386. public String[] getMethodNames() {
  387. return methodNames;
  388. }
  389.  
  390. @Optional.Method(modid = "ComputerCraft")
  391. @Override
  392. public Object[] callMethod (IComputerAccess Computer, ILuaContext LuaContext,
  393. int MethodIndex, Object[] Arguments) throws Exception
  394. {
  395. return (callMethod (MethodIndex, Arguments));
  396. }
  397.  
  398.  
  399.  
  400. // OpenComputers
  401.  
  402.  
  403. @Override
  404. @Optional.Method(modid = "OpenComputers")
  405. public String getComponentName() {
  406. // Convention for OC names is a) lower case, b) valid variable names,
  407. // so this can be used as `component.br_reactor.setActive(true)` e.g.
  408. return "RIM_Carriage_Controller";
  409. }
  410.  
  411. @Override
  412. @Optional.Method(modid = "OpenComputers")
  413. public String[] methods() {
  414. return methodNames;
  415. }
  416.  
  417. @Override
  418. @Optional.Method(modid = "OpenComputers")
  419. public Object[] invoke(final String method, final Context context,
  420. final Arguments args) throws Exception {
  421. final Object[] arguments = new Object[args.count()];
  422. for (int i = 0; i < args.count(); ++i) {
  423. arguments[i] = args.checkAny(i);
  424. }
  425. final Integer methodId = methodIds.get(method);
  426. if (methodId == null) {
  427. throw new NoSuchMethodError();
  428. }
  429. return callMethod(methodId, arguments);
  430. }
  431.  
  432. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement