From 09d4febe1325cfe77779881d39f5512a9110f8c9 Mon Sep 17 00:00:00 2001 From: Brian Ricketts Date: Fri, 11 Jan 2013 22:11:18 -0600 Subject: [PATCH] Some work on ArmBot (trying to smooth things up a bit) --- .../client/render/RenderArmbot.java | 6 ++ .../assemblyline/common/AssemblyLine.java | 8 ++ .../machine/armbot/TileEntityArmbot.java | 102 ++++++++++++++---- .../common/machine/command/Command.java | 13 +++ .../machine/command/CommandManager.java | 19 +++- .../common/machine/command/CommandRotate.java | 11 +- 6 files changed, 129 insertions(+), 30 deletions(-) diff --git a/src/minecraft/assemblyline/client/render/RenderArmbot.java b/src/minecraft/assemblyline/client/render/RenderArmbot.java index fa7cd5c98..186d52d3f 100644 --- a/src/minecraft/assemblyline/client/render/RenderArmbot.java +++ b/src/minecraft/assemblyline/client/render/RenderArmbot.java @@ -1,10 +1,16 @@ package assemblyline.client.render; +import net.minecraft.client.renderer.RenderBlocks; +import net.minecraft.client.renderer.RenderEngine; +import net.minecraft.client.renderer.RenderGlobal; +import net.minecraft.client.renderer.RenderHelper; import net.minecraft.client.renderer.tileentity.TileEntitySpecialRenderer; import net.minecraft.tileentity.TileEntity; import org.lwjgl.opengl.GL11; +import cpw.mods.fml.client.registry.RenderingRegistry; + import assemblyline.client.model.ModelArmbot; import assemblyline.common.AssemblyLine; import assemblyline.common.machine.armbot.TileEntityArmbot; diff --git a/src/minecraft/assemblyline/common/AssemblyLine.java b/src/minecraft/assemblyline/common/AssemblyLine.java index 0fbf6fe0a..23ac55410 100644 --- a/src/minecraft/assemblyline/common/AssemblyLine.java +++ b/src/minecraft/assemblyline/common/AssemblyLine.java @@ -25,6 +25,7 @@ import assemblyline.common.machine.encoder.BlockEncoder; import assemblyline.common.machine.encoder.ItemDisk; import assemblyline.common.machine.imprinter.BlockImprinter; import assemblyline.common.machine.imprinter.ItemImprinter; +import cpw.mods.fml.common.FMLCommonHandler; import cpw.mods.fml.common.Loader; import cpw.mods.fml.common.Mod; import cpw.mods.fml.common.Mod.Init; @@ -36,6 +37,7 @@ import cpw.mods.fml.common.event.FMLPreInitializationEvent; import cpw.mods.fml.common.network.NetworkMod; import cpw.mods.fml.common.network.NetworkRegistry; import cpw.mods.fml.common.registry.GameRegistry; +import cpw.mods.fml.relauncher.Side; @Mod(modid = AssemblyLine.CHANNEL, name = AssemblyLine.NAME, version = AssemblyLine.VERSION, dependencies = "after:BasicComponents") @NetworkMod(channels = { AssemblyLine.CHANNEL }, clientSideRequired = true, serverSideRequired = false, packetHandler = PacketManager.class) @@ -154,4 +156,10 @@ public class AssemblyLine UETab.setItemStack(new ItemStack(blockConveyorBelt)); } + + public static void printSidedData(String data) + { + System.out.print(FMLCommonHandler.instance().getEffectiveSide() == Side.CLIENT ? "[C]" : "[S]"); + System.out.println(" " + data); + } } \ No newline at end of file diff --git a/src/minecraft/assemblyline/common/machine/armbot/TileEntityArmbot.java b/src/minecraft/assemblyline/common/machine/armbot/TileEntityArmbot.java index cbe2f7d94..835f15194 100644 --- a/src/minecraft/assemblyline/common/machine/armbot/TileEntityArmbot.java +++ b/src/minecraft/assemblyline/common/machine/armbot/TileEntityArmbot.java @@ -6,6 +6,7 @@ import java.io.IOException; import java.util.ArrayList; import java.util.EnumSet; import java.util.List; +import java.util.Random; import net.minecraft.entity.Entity; import net.minecraft.entity.item.EntityItem; @@ -15,6 +16,7 @@ import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.network.INetworkManager; import net.minecraft.network.packet.Packet; +import net.minecraft.network.packet.Packet132TileEntityData; import net.minecraft.network.packet.Packet250CustomPayload; import net.minecraft.tileentity.TileEntity; import net.minecraftforge.common.ForgeDirection; @@ -61,6 +63,8 @@ public class TileEntityArmbot extends TileEntityAssemblyNetwork implements IMult public float rotationPitch = 0; public float rotationYaw = 0; + private int ticksSincePower = 0; + /** * An entity that the armbot is grabbed onto. */ @@ -72,23 +76,15 @@ public class TileEntityArmbot extends TileEntityAssemblyNetwork implements IMult ElectricityConnections.registerConnector(this, EnumSet.range(ForgeDirection.DOWN, ForgeDirection.EAST)); } - @Override - public void onUpdate() + private void updateCommands(boolean reset, int currentTask) { if (this.disk != null) { - try + if (reset) { - if (this.commandManager.hasTasks()) - { - if (this.commandManager.getCommands().get(0) instanceof CommandReturn) - { - this.commandManager.clearTasks(); - } - } - - if (!this.commandManager.hasTasks()) + try { + this.commandManager.clearTasks(); List commands = ItemDisk.getCommands(this.disk); for (String commandString : commands) @@ -116,11 +112,63 @@ public class TileEntityArmbot extends TileEntityAssemblyNetwork implements IMult this.commandManager.addTask(this, newCommand, commandParameters.toArray(new String[0])); } } + + this.commandManager.setCurrentTask(currentTask); + } + catch (Exception e) + { + e.printStackTrace(); } } - catch (Exception e) + else { - e.printStackTrace(); + 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 @@ -135,10 +183,15 @@ public class TileEntityArmbot extends TileEntityAssemblyNetwork implements IMult { this.commandManager.addTask(this, new CommandReturn()); } - + this.commandManager.setCurrentTask(0); } + } + @Override + public void onUpdate() + { + updateCommands(false, -1); if (this.isRunning()) { Vector3 handPosition = this.getHandPosition(); @@ -153,6 +206,7 @@ public class TileEntityArmbot extends TileEntityAssemblyNetwork implements IMult if (entity instanceof EntityItem) { ((EntityItem) entity).delayBeforeCanPickup = 20; + ((EntityItem) entity).age = 0; } } @@ -168,11 +222,18 @@ public class TileEntityArmbot extends TileEntityAssemblyNetwork implements IMult if (this.rotationYaw >= 360) this.rotationYaw -= 360; + this.ticksSincePower = 0; + } + else + { + this.ticksSincePower++; + if (FMLCommonHandler.instance().getEffectiveSide() == Side.CLIENT && ticksSincePower <= 20) + this.commandManager.onUpdate(); } - if (FMLCommonHandler.instance().getEffectiveSide() == Side.SERVER && this.ticks % 5 == 0) + 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), 20); } } @@ -226,16 +287,17 @@ public class TileEntityArmbot extends TileEntityAssemblyNetwork implements IMult { ByteArrayInputStream bis = new ByteArrayInputStream(packet.data); DataInputStream dis = new DataInputStream(bis); - final int id, x, y, z; - + int id, x, y, z; id = dis.readInt(); x = dis.readInt(); y = dis.readInt(); z = dis.readInt(); - this.commandManager.setCurrentTask(dis.readInt()); + int curTask = dis.readInt(); NBTTagCompound tag = Packet.readNBTTagCompound(dis); readFromNBT(tag); + + updateCommands(true, curTask); } catch (IOException e) { diff --git a/src/minecraft/assemblyline/common/machine/command/Command.java b/src/minecraft/assemblyline/common/machine/command/Command.java index 1ebd6e7dc..aa20375dc 100644 --- a/src/minecraft/assemblyline/common/machine/command/Command.java +++ b/src/minecraft/assemblyline/common/machine/command/Command.java @@ -2,6 +2,7 @@ package assemblyline.common.machine.command; import java.util.HashMap; +import net.minecraft.nbt.NBTTagCompound; import net.minecraft.world.World; import assemblyline.common.machine.armbot.TileEntityArmbot; @@ -19,6 +20,7 @@ public abstract class Command * String - Command name. Command - The actual command class. */ private static final HashMap COMMANDS = new HashMap(); + private static final HashMap REVERSE_LOOKUP = new HashMap(); static { @@ -33,12 +35,18 @@ public abstract class Command public static void registerCommand(String command, Class commandClass) { COMMANDS.put(command, commandClass); + REVERSE_LOOKUP.put(commandClass, command); } public static Class getCommand(String command) { return COMMANDS.get(command.toLowerCase()); } + + public static String getCommandName(Class command) + { + return REVERSE_LOOKUP.get(command); + } /** * The amount of ticks this command has been running for. @@ -114,4 +122,9 @@ public abstract class Command return 0; } + + public void writeToNBT(NBTTagCompound taskCompound) + { + + } } diff --git a/src/minecraft/assemblyline/common/machine/command/CommandManager.java b/src/minecraft/assemblyline/common/machine/command/CommandManager.java index 04c77e61c..50f46a66b 100644 --- a/src/minecraft/assemblyline/common/machine/command/CommandManager.java +++ b/src/minecraft/assemblyline/common/machine/command/CommandManager.java @@ -1,12 +1,11 @@ package assemblyline.common.machine.command; import java.util.ArrayList; -import java.util.Iterator; import java.util.List; +import net.minecraft.nbt.NBTTagCompound; +import net.minecraft.nbt.NBTTagList; import assemblyline.common.machine.armbot.TileEntityArmbot; - -import net.minecraft.tileentity.TileEntity; import cpw.mods.fml.common.FMLLog; public class CommandManager @@ -62,8 +61,7 @@ public class CommandManager } /** - * Used to register Tasks for a TileEntity, executes onTaskStart for the Task after registering - * it + * 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 @@ -110,4 +108,15 @@ public class CommandManager { return this.currentTask; } + + public void writeToNBT(NBTTagCompound nbt) + { + NBTTagList taskList = new NBTTagList(); + for (int i = 0; i < this.tasks.size(); i++) + { + NBTTagCompound taskCompound = new NBTTagCompound(); + this.tasks.get(i).writeToNBT(taskCompound); + taskList.appendTag(taskCompound); + } + } } diff --git a/src/minecraft/assemblyline/common/machine/command/CommandRotate.java b/src/minecraft/assemblyline/common/machine/command/CommandRotate.java index 77fbea78f..4c05431ca 100644 --- a/src/minecraft/assemblyline/common/machine/command/CommandRotate.java +++ b/src/minecraft/assemblyline/common/machine/command/CommandRotate.java @@ -1,5 +1,6 @@ package assemblyline.common.machine.command; +import assemblyline.common.AssemblyLine; import cpw.mods.fml.common.FMLCommonHandler; import cpw.mods.fml.relauncher.Side; @@ -15,7 +16,9 @@ public class CommandRotate extends Command @Override public void onTaskStart() - { + { + super.onTaskStart(); + this.ticks = 0; if (this.getArg(0) == null) { this.targetRotation = this.tileEntity.rotationYaw + 90; @@ -25,11 +28,11 @@ public class CommandRotate extends Command this.targetRotation = this.tileEntity.rotationYaw + this.getIntArg(0); } - while (this.targetRotation > 360) + while (this.targetRotation >= 360) { this.targetRotation -= 360; } - while (this.targetRotation < -360) + while (this.targetRotation <= -360) { this.targetRotation += 360; } @@ -55,8 +58,6 @@ public class CommandRotate extends Command { this.tileEntity.rotationYaw += ROTATION_SPEED; } - - return true; } if (this.ticks < 80) { return true; }