From f02f0855990220373d2ca5ad3595f8aa9879a91f Mon Sep 17 00:00:00 2001 From: Henry Mao Date: Sat, 12 Jan 2013 23:55:11 +0800 Subject: [PATCH] Got Armbot working nicely --- .../machine/armbot/TileEntityArmbot.java | 173 +++++------------- .../common/machine/command/Command.java | 11 +- .../common/machine/command/CommandGrab.java | 1 - .../common/machine/command/CommandIdle.java | 4 + .../machine/command/CommandManager.java | 68 +++++-- .../common/machine/command/CommandRepeat.java | 4 +- .../common/machine/command/CommandReturn.java | 4 +- .../common/machine/command/CommandRotate.java | 4 +- 8 files changed, 114 insertions(+), 155 deletions(-) diff --git a/src/minecraft/assemblyline/common/machine/armbot/TileEntityArmbot.java b/src/minecraft/assemblyline/common/machine/armbot/TileEntityArmbot.java index 835f1519..94ca8d5a 100644 --- a/src/minecraft/assemblyline/common/machine/armbot/TileEntityArmbot.java +++ b/src/minecraft/assemblyline/common/machine/armbot/TileEntityArmbot.java @@ -76,122 +76,9 @@ public class TileEntityArmbot extends TileEntityAssemblyNetwork implements IMult ElectricityConnections.registerConnector(this, EnumSet.range(ForgeDirection.DOWN, ForgeDirection.EAST)); } - private void updateCommands(boolean reset, int currentTask) - { - if (this.disk != null) - { - if (reset) - { - try - { - this.commandManager.clearTasks(); - List commands = ItemDisk.getCommands(this.disk); - - for (String commandString : commands) - { - String commandName = commandString.split(" ")[0]; - - Class command = Command.getCommand(commandName); - - if (command != null) - { - Command newCommand = command.newInstance(); - newCommand.world = this.worldObj; - newCommand.tileEntity = this; - - List commandParameters = new ArrayList(); - - for (String param : commandString.split(" ")) - { - if (!param.equals(commandName)) - { - commandParameters.add(param); - } - } - - this.commandManager.addTask(this, newCommand, commandParameters.toArray(new String[0])); - } - } - - this.commandManager.setCurrentTask(currentTask); - } - catch (Exception e) - { - e.printStackTrace(); - } - } - else - { - if (this.commandManager.hasTasks()) - { - if (this.commandManager.getCommands().get(0) instanceof CommandReturn) - { - this.commandManager.clearTasks(); - } - } - - if (!this.commandManager.hasTasks()) - { - try - { - List commands = ItemDisk.getCommands(this.disk); - - for (String commandString : commands) - { - String commandName = commandString.split(" ")[0]; - - Class command = Command.getCommand(commandName); - - if (command != null) - { - Command newCommand = command.newInstance(); - newCommand.world = this.worldObj; - newCommand.tileEntity = this; - - List commandParameters = new ArrayList(); - - for (String param : commandString.split(" ")) - { - if (!param.equals(commandName)) - { - commandParameters.add(param); - } - } - - this.commandManager.addTask(this, newCommand, commandParameters.toArray(new String[0])); - } - } - - this.commandManager.setCurrentTask(0); - } - catch (Exception e) - { - e.printStackTrace(); - } - } - } - } - else - { - this.commandManager.clearTasks(); - - if (this.grabbedEntities.size() > 0) - { - this.commandManager.addTask(this, new CommandDrop()); - } - else - { - this.commandManager.addTask(this, new CommandReturn()); - } - - this.commandManager.setCurrentTask(0); - } - } - @Override public void onUpdate() { - updateCommands(false, -1); if (this.isRunning()) { Vector3 handPosition = this.getHandPosition(); @@ -210,6 +97,22 @@ public class TileEntityArmbot extends TileEntityAssemblyNetwork implements IMult } } + if (this.disk == null) + { + this.commandManager.clear(); + + if (this.grabbedEntities.size() > 0) + { + this.commandManager.addCommand(this, CommandDrop.class); + } + else + { + this.commandManager.addCommand(this, CommandReturn.class); + } + + this.commandManager.setCurrentTask(0); + } + this.commandManager.onUpdate(); // keep it within 0 - 360 degrees so ROTATE commands work properly @@ -233,7 +136,7 @@ public class TileEntityArmbot extends TileEntityAssemblyNetwork implements IMult if (FMLCommonHandler.instance().getEffectiveSide() == Side.SERVER && this.ticks % 20 == 0) { - PacketManager.sendPacketToClients(this.getDescriptionPacket());// , this.worldObj, new Vector3(this), 20); + PacketManager.sendPacketToClients(this.getDescriptionPacket(), this.worldObj, new Vector3(this), 50); } } @@ -292,12 +195,9 @@ public class TileEntityArmbot extends TileEntityAssemblyNetwork implements IMult x = dis.readInt(); y = dis.readInt(); z = dis.readInt(); - int curTask = dis.readInt(); - + this.commandManager.setCurrentTask(dis.readInt()); NBTTagCompound tag = Packet.readNBTTagCompound(dis); readFromNBT(tag); - - updateCommands(true, curTask); } catch (IOException e) { @@ -381,11 +281,7 @@ public class TileEntityArmbot extends TileEntityAssemblyNetwork implements IMult public void setInventorySlotContents(int par1, ItemStack par2ItemStack) { this.disk = par2ItemStack; - - if (par2ItemStack != null && par2ItemStack.stackSize > this.getInventoryStackLimit()) - { - par2ItemStack.stackSize = this.getInventoryStackLimit(); - } + this.onInventoryChanged(); } @Override @@ -490,7 +386,6 @@ public class TileEntityArmbot extends TileEntityAssemblyNetwork implements IMult } this.setInventorySlotContents(0, null); - onInventoryChanged(); return true; } else @@ -500,7 +395,6 @@ public class TileEntityArmbot extends TileEntityAssemblyNetwork implements IMult if (player.getCurrentEquippedItem().getItem() instanceof ItemDisk) { this.setInventorySlotContents(0, player.getCurrentEquippedItem()); - onInventoryChanged(); player.inventory.setInventorySlotContents(player.inventory.currentItem, null); return true; } @@ -513,7 +407,34 @@ public class TileEntityArmbot extends TileEntityAssemblyNetwork implements IMult @Override public void onInventoryChanged() { - this.commandManager.setCurrentTask(0); + this.commandManager.clear(); + + if (this.disk != null) + { + List commands = ItemDisk.getCommands(this.disk); + + for (String commandString : commands) + { + String commandName = commandString.split(" ")[0]; + + Class command = Command.getCommand(commandName); + + if (command != null) + { + List commandParameters = new ArrayList(); + + for (String param : commandString.split(" ")) + { + if (!param.equals(commandName)) + { + commandParameters.add(param); + } + } + + this.commandManager.addCommand(this, command, commandParameters.toArray(new String[0])); + } + } + } } @Override diff --git a/src/minecraft/assemblyline/common/machine/command/Command.java b/src/minecraft/assemblyline/common/machine/command/Command.java index aa20375d..184e776d 100644 --- a/src/minecraft/assemblyline/common/machine/command/Command.java +++ b/src/minecraft/assemblyline/common/machine/command/Command.java @@ -42,7 +42,7 @@ public abstract class Command { return COMMANDS.get(command.toLowerCase()); } - + public static String getCommandName(Class command) { return REVERSE_LOOKUP.get(command); @@ -72,7 +72,7 @@ public abstract class Command protected boolean doTask() { this.ticks++; - return true; + return false; } public void onTaskStart() @@ -96,6 +96,11 @@ public abstract class Command this.parameters = strings; } + public String[] getArgs() + { + return this.parameters; + } + /** * Some functions to help get parameter arguments. */ @@ -125,6 +130,6 @@ public abstract class Command public void writeToNBT(NBTTagCompound taskCompound) { - + } } diff --git a/src/minecraft/assemblyline/common/machine/command/CommandGrab.java b/src/minecraft/assemblyline/common/machine/command/CommandGrab.java index b880c228..524c9e13 100644 --- a/src/minecraft/assemblyline/common/machine/command/CommandGrab.java +++ b/src/minecraft/assemblyline/common/machine/command/CommandGrab.java @@ -31,7 +31,6 @@ public class CommandGrab extends Command protected boolean doTask() { super.doTask(); - Vector3 serachPosition = this.tileEntity.getHandPosition(); List found = this.world.getEntitiesWithinAABB(this.entityToInclude, AxisAlignedBB.getBoundingBox(serachPosition.x - radius, serachPosition.y - radius, serachPosition.z - radius, serachPosition.x + radius, serachPosition.y + radius, serachPosition.z + radius)); diff --git a/src/minecraft/assemblyline/common/machine/command/CommandIdle.java b/src/minecraft/assemblyline/common/machine/command/CommandIdle.java index 6a9c66b4..b6670b68 100644 --- a/src/minecraft/assemblyline/common/machine/command/CommandIdle.java +++ b/src/minecraft/assemblyline/common/machine/command/CommandIdle.java @@ -11,10 +11,14 @@ public class CommandIdle extends Command public void onTaskStart() { + super.onTaskStart(); + if (this.getIntArg(0) > 0) { this.idleTime = this.getIntArg(0); } + + System.out.println("INITIATE"); } protected boolean doTask() diff --git a/src/minecraft/assemblyline/common/machine/command/CommandManager.java b/src/minecraft/assemblyline/common/machine/command/CommandManager.java index 50f46a66..0bfe09be 100644 --- a/src/minecraft/assemblyline/common/machine/command/CommandManager.java +++ b/src/minecraft/assemblyline/common/machine/command/CommandManager.java @@ -28,6 +28,8 @@ public class CommandManager { if (this.tasks.size() > 0) { + System.out.println(this.lastTask + " vs " + currentTask + ": " + this.tasks.size()); + if (this.currentTask < this.tasks.size()) { if (this.currentTask < 0) @@ -45,8 +47,11 @@ public class CommandManager if (!task.doTask()) { - task.onTaskEnd(); + // End the task and reinitiate it into a new class to make sure it is fresh. + int tempCurrentTask = this.currentTask; this.currentTask++; + task.onTaskEnd(); + this.tasks.set(tempCurrentTask, this.getNewCommand(task.tileEntity, task.getClass(), task.getArgs())); } } } @@ -60,25 +65,46 @@ public class CommandManager this.ticks++; } - /** - * Used to register Tasks for a TileEntity, executes onTaskStart for the Task after registering it - * - * @param tileEntity TE instance to register the task for - * @param task Task instance to register - */ - public void addTask(TileEntityArmbot tileEntity, Command task, String[] parameters) + public Command getNewCommand(TileEntityArmbot tileEntity, Class commandClass, String[] parameters) { - task.world = tileEntity.worldObj; - task.tileEntity = tileEntity; - task.commandManager = this; - task.setParameters(parameters); - this.tasks.add(task); - task.onTaskStart(); + try + { + Command newCommand = commandClass.newInstance(); + newCommand.world = tileEntity.worldObj; + newCommand.tileEntity = tileEntity; + newCommand.commandManager = this; + newCommand.setParameters(parameters); + return newCommand; + } + catch (Exception e) + { + FMLLog.severe("Failed to add command"); + e.printStackTrace(); + } + + return null; } - public void addTask(TileEntityArmbot tileEntity, Command task) + /** + * Used to register Tasks for a TileEntity, executes onTaskStart for the Task after registering + * it + * + * @param tileEntity TE instance to register the task for + * @param newCommand Task instance to register + */ + public void addCommand(TileEntityArmbot tileEntity, Class commandClass, String[] parameters) { - this.addTask(tileEntity, task, new String[0]); + Command newCommand = this.getNewCommand(tileEntity, commandClass, parameters); + + if (newCommand != null) + { + this.tasks.add(newCommand); + } + } + + public void addCommand(TileEntityArmbot tileEntity, Class task) + { + this.addCommand(tileEntity, task, new String[0]); } /** @@ -94,14 +120,20 @@ public class CommandManager return tasks; } - public void clearTasks() + /** + * Resets the command manager. + */ + public void clear() { this.tasks.clear(); + this.currentTask = 0; + this.lastTask = -1; + this.ticks = 0; } public void setCurrentTask(int i) { - this.currentTask = Math.max(Math.min(i, this.tasks.size() - 1), 0); + this.currentTask = Math.max(Math.min(i, this.tasks.size()), 0); } public int getCurrentTask() diff --git a/src/minecraft/assemblyline/common/machine/command/CommandRepeat.java b/src/minecraft/assemblyline/common/machine/command/CommandRepeat.java index 7c1b4388..51a5f7fd 100644 --- a/src/minecraft/assemblyline/common/machine/command/CommandRepeat.java +++ b/src/minecraft/assemblyline/common/machine/command/CommandRepeat.java @@ -19,7 +19,7 @@ public class CommandRepeat extends Command } @Override - protected boolean doTask() + public void onTaskEnd() { if (this.tasksToRepeat > 0) { @@ -29,7 +29,5 @@ public class CommandRepeat extends Command { this.commandManager.setCurrentTask(0); } - - return false; } } diff --git a/src/minecraft/assemblyline/common/machine/command/CommandReturn.java b/src/minecraft/assemblyline/common/machine/command/CommandReturn.java index 0a43552a..6931652e 100644 --- a/src/minecraft/assemblyline/common/machine/command/CommandReturn.java +++ b/src/minecraft/assemblyline/common/machine/command/CommandReturn.java @@ -13,7 +13,7 @@ public class CommandReturn extends Command /** * Move the arm rotation to idle position if the machine is not idling */ - if (Math.abs(this.tileEntity.rotationPitch - IDLE_ROTATION_PITCH) > 0.001 || Math.abs(this.tileEntity.rotationYaw - IDLE_ROTATION_YAW) > 0.001) + if (Math.abs(this.tileEntity.rotationPitch - IDLE_ROTATION_PITCH) > 0.01 || Math.abs(this.tileEntity.rotationYaw - IDLE_ROTATION_YAW) > 0.01) { if (Math.abs(IDLE_ROTATION_PITCH - this.tileEntity.rotationPitch) > 0.125) this.tileEntity.rotationPitch += (IDLE_ROTATION_PITCH - this.tileEntity.rotationPitch) * 0.05; @@ -28,9 +28,9 @@ public class CommandReturn extends Command this.tileEntity.rotationYaw += Math.signum(IDLE_ROTATION_YAW - this.tileEntity.rotationYaw) * (0.125 * 0.05); if (Math.abs(this.tileEntity.rotationYaw - IDLE_ROTATION_YAW) < 0.0125) this.tileEntity.rotationYaw = IDLE_ROTATION_YAW; - return true; } + return false; } diff --git a/src/minecraft/assemblyline/common/machine/command/CommandRotate.java b/src/minecraft/assemblyline/common/machine/command/CommandRotate.java index 4c05431c..f09aa263 100644 --- a/src/minecraft/assemblyline/common/machine/command/CommandRotate.java +++ b/src/minecraft/assemblyline/common/machine/command/CommandRotate.java @@ -18,7 +18,7 @@ public class CommandRotate extends Command public void onTaskStart() { super.onTaskStart(); - this.ticks = 0; + if (this.getArg(0) == null) { this.targetRotation = this.tileEntity.rotationYaw + 90; @@ -44,7 +44,7 @@ public class CommandRotate extends Command super.doTask(); float rotationalDifference = Math.abs(this.tileEntity.rotationYaw - this.targetRotation); - if (rotationalDifference < 0.8) + if (rotationalDifference < 0.1) { this.tileEntity.rotationYaw = this.targetRotation; }