From 5c5f4395bbbeb13e9a9ae421979520baf307f298 Mon Sep 17 00:00:00 2001 From: Brian Ricketts Date: Mon, 14 Jan 2013 17:01:00 -0600 Subject: [PATCH] Started implementing a Lua API to act as a "wrapper" for the ArmBot. --- .../assemblyline/common/CommonProxy.java | 68 +++++++++++++++++++ .../machine/armbot/TileEntityArmbot.java | 50 +++++++++----- 2 files changed, 102 insertions(+), 16 deletions(-) diff --git a/src/minecraft/assemblyline/common/CommonProxy.java b/src/minecraft/assemblyline/common/CommonProxy.java index 54f15beb6..73eb8e189 100644 --- a/src/minecraft/assemblyline/common/CommonProxy.java +++ b/src/minecraft/assemblyline/common/CommonProxy.java @@ -1,5 +1,11 @@ package assemblyline.common; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.util.zip.ZipEntry; +import java.util.zip.ZipInputStream; + import net.minecraft.entity.player.EntityPlayer; import net.minecraft.tileentity.TileEntity; import net.minecraft.world.World; @@ -14,6 +20,7 @@ import assemblyline.common.machine.encoder.ContainerEncoder; import assemblyline.common.machine.encoder.TileEntityEncoder; import assemblyline.common.machine.imprinter.ContainerImprinter; import assemblyline.common.machine.imprinter.TileEntityImprinter; +import cpw.mods.fml.common.FMLCommonHandler; import cpw.mods.fml.common.network.IGuiHandler; import cpw.mods.fml.common.registry.GameRegistry; @@ -40,6 +47,67 @@ public class CommonProxy implements IGuiHandler GameRegistry.registerTileEntity(TileEntityMulti.class, "ALMulti"); } + private void extractZipToLocation(File zipFile, String sourceFolder, String destFolder) + { + try + { + + File destFile = new File(FMLCommonHandler.instance().getMinecraftServerInstance().getFile("."), destFolder); + String destinationName = destFile.getAbsolutePath(); + byte[] buf = new byte[1024]; + ZipInputStream zipinputstream = null; + ZipEntry zipentry; + zipinputstream = new ZipInputStream(new FileInputStream(zipFile)); + + zipentry = zipinputstream.getNextEntry(); + while (zipentry != null) + { + // for each entry to be extracted + String zipentryName = zipentry.getName(); + if (!zipentryName.startsWith(sourceFolder)) + { + zipentry = zipinputstream.getNextEntry(); + continue; + } + + String entryName = destinationName + zipentryName.substring(Math.min(zipentryName.length(), sourceFolder.length() - 1)); + entryName = entryName.replace('/', File.separatorChar); + entryName = entryName.replace('\\', File.separatorChar); + int n; + FileOutputStream fileoutputstream; + File newFile = new File(entryName); + if (zipentry.isDirectory()) + { + if (!newFile.mkdirs()) + { + break; + } + zipentry = zipinputstream.getNextEntry(); + continue; + } + + fileoutputstream = new FileOutputStream(entryName); + + while ((n = zipinputstream.read(buf, 0, 1024)) > -1) + { + fileoutputstream.write(buf, 0, n); + } + + fileoutputstream.close(); + zipinputstream.closeEntry(); + zipentry = zipinputstream.getNextEntry(); + + }// while + + zipinputstream.close(); + } + catch (Exception e) + { + System.out.println("Error while loading AssemblyLine Lua libraries: "); + e.printStackTrace(); + } + } + @Override public Object getServerGuiElement(int ID, EntityPlayer player, World world, int x, int y, int z) { diff --git a/src/minecraft/assemblyline/common/machine/armbot/TileEntityArmbot.java b/src/minecraft/assemblyline/common/machine/armbot/TileEntityArmbot.java index a67166062..037d6fd04 100644 --- a/src/minecraft/assemblyline/common/machine/armbot/TileEntityArmbot.java +++ b/src/minecraft/assemblyline/common/machine/armbot/TileEntityArmbot.java @@ -59,6 +59,8 @@ public class TileEntityArmbot extends TileEntityAssemblyNetwork implements IMult public double wattsReceived = 0; private int playerUsing = 0; + + private int computersAttached = 0; /** * The rotation of the arms. In Degrees. @@ -115,7 +117,7 @@ public class TileEntityArmbot extends TileEntityAssemblyNetwork implements IMult } } - if (this.disk == null) + if (this.disk == null && this.computersAttached == 0) { this.commandManager.clear(); @@ -125,7 +127,9 @@ public class TileEntityArmbot extends TileEntityAssemblyNetwork implements IMult } else { - this.commandManager.addCommand(this, CommandReturn.class); + if (!this.commandManager.hasTasks()) + if (Math.abs(this.rotationYaw - CommandReturn.IDLE_ROTATION_YAW) > 0.01) + this.commandManager.addCommand(this, CommandReturn.class); } this.commandManager.setCurrentTask(0); @@ -485,24 +489,25 @@ public class TileEntityArmbot extends TileEntityAssemblyNetwork implements IMult @Override public Object[] callMethod(IComputerAccess computer, int method, Object[] arguments) throws Exception { - System.out.println(getMethodNames()[method] + ": "); - for (Object o : arguments) - { - System.out.println(" " + o.toString()); - } switch (method) { case 0: // rotateBy: rotates by a certain amount { if (arguments.length > 0) { - if (arguments[0] instanceof Float) + try + // try to cast to Float { - float angle = (Float) arguments[0]; - this.commandManager.addCommand(this, CommandRotate.class, new String[] { Float.toString(angle) }); + double angle = (Double) arguments[0]; + this.commandManager.addCommand(this, CommandRotate.class, new String[] { Double.toString(angle) }); + while(this.commandManager.hasTasks()) + { + Thread.sleep(1); + } } - else + catch (Exception ex) { + ex.printStackTrace(); throw new IllegalArgumentException("expected number"); } } @@ -518,12 +523,17 @@ public class TileEntityArmbot extends TileEntityAssemblyNetwork implements IMult { try { - float angle = (Float) arguments[0]; - float diff = angle - this.rotationYaw; - this.commandManager.addCommand(this, CommandRotate.class, new String[] { Float.toString(diff) }); + double angle = (Double) arguments[0]; + double diff = angle - this.rotationYaw; + this.commandManager.addCommand(this, CommandRotate.class, new String[] { Double.toString(diff) }); + while(this.commandManager.hasTasks()) + { + Thread.sleep(1); + } } catch (Exception ex) { + ex.printStackTrace(); throw new IllegalArgumentException("expected number"); } } @@ -536,11 +546,19 @@ public class TileEntityArmbot extends TileEntityAssemblyNetwork implements IMult case 2: // grab: grabs an item { this.commandManager.addCommand(this, CommandGrab.class); + while(this.commandManager.hasTasks()) + { + Thread.sleep(1); + } break; } case 3: // drop: drops an item { this.commandManager.addCommand(this, CommandDrop.class); + while(this.commandManager.hasTasks()) + { + Thread.sleep(1); + } break; } case 4: // reset: calls the RETURN command @@ -565,13 +583,13 @@ public class TileEntityArmbot extends TileEntityAssemblyNetwork implements IMult @Override public void attach(IComputerAccess computer) { - + computersAttached++; } @Override public void detach(IComputerAccess computer) { - + computersAttached--; } }