From d18cbe854af9ce9694387a63e376f92f3805cf00 Mon Sep 17 00:00:00 2001 From: Henry Mao Date: Wed, 30 Jan 2013 18:49:35 +0800 Subject: [PATCH] Added Break & Place Commands --- .../machine/armbot/TileEntityArmbot.java | 51 +++++++------ .../common/machine/command/Command.java | 1 + .../common/machine/command/CommandBreak.java | 11 +-- .../common/machine/command/CommandDrop.java | 16 ++-- .../common/machine/command/CommandGrab.java | 7 +- .../common/machine/command/CommandPlace.java | 74 +++++++++++++++++++ .../machine/command/CommandRotateTo.java | 13 ++-- 7 files changed, 129 insertions(+), 44 deletions(-) create mode 100644 src/minecraft/assemblyline/common/machine/command/CommandPlace.java diff --git a/src/minecraft/assemblyline/common/machine/armbot/TileEntityArmbot.java b/src/minecraft/assemblyline/common/machine/armbot/TileEntityArmbot.java index 74346e63..ee7173bd 100644 --- a/src/minecraft/assemblyline/common/machine/armbot/TileEntityArmbot.java +++ b/src/minecraft/assemblyline/common/machine/armbot/TileEntityArmbot.java @@ -92,6 +92,24 @@ public class TileEntityArmbot extends TileEntityAssemblyNetwork implements IMult public void onUpdate() { Vector3 handPosition = this.getHandPosition(); + + for (Entity entity : this.grabbedEntities) + { + if (entity != null) + { + entity.setPosition(handPosition.x, handPosition.y, handPosition.z); + entity.motionX = 0; + entity.motionY = 0; + entity.motionZ = 0; + + if (entity instanceof EntityItem) + { + ((EntityItem) entity).delayBeforeCanPickup = 20; + ((EntityItem) entity).age = 0; + } + } + } + if (this.isRunning()) { if (FMLCommonHandler.instance().getEffectiveSide() == Side.SERVER) @@ -175,23 +193,6 @@ public class TileEntityArmbot extends TileEntityAssemblyNetwork implements IMult } } - for (Entity entity : this.grabbedEntities) - { - if (entity != null) - { - entity.setPosition(handPosition.x, handPosition.y, handPosition.z); - entity.motionX = 0; - entity.motionY = 0; - entity.motionZ = 0; - - if (entity instanceof EntityItem) - { - ((EntityItem) entity).delayBeforeCanPickup = 20; - ((EntityItem) entity).age = 0; - } - } - } - // System.out.println("Ren: " + this.renderYaw + "; Rot: " + this.rotationYaw); if (Math.abs(this.renderYaw - this.rotationYaw) > 0.001f) { @@ -479,11 +480,16 @@ public class TileEntityArmbot extends TileEntityAssemblyNetwork implements IMult this.rotationYaw = nbt.getFloat("yaw"); this.rotationPitch = nbt.getFloat("pitch"); - if (this.worldObj.isRemote) - this.displayText = nbt.getString("cmdText"); - + if (this.worldObj != null) + { + if (this.worldObj.isRemote) + { + this.displayText = nbt.getString("cmdText"); + } + } /* - * NBTTagCompound cmdManager = nbt.getCompoundTag("cmdManager"); this.commandManager.readFromNBT(this, cmdManager); + * NBTTagCompound cmdManager = nbt.getCompoundTag("cmdManager"); + * this.commandManager.readFromNBT(this, cmdManager); */ this.commandManager.setCurrentTask(nbt.getInteger("curTask")); @@ -520,7 +526,8 @@ public class TileEntityArmbot extends TileEntityAssemblyNetwork implements IMult nbt.setFloat("pitch", this.rotationPitch); /* - * NBTTagCompound cmdManager = new NBTTagCompound("cmdManager"); this.commandManager.writeToNBT(cmdManager); nbt.setCompoundTag("cmdManager", cmdManager); + * NBTTagCompound cmdManager = new NBTTagCompound("cmdManager"); + * this.commandManager.writeToNBT(cmdManager); nbt.setCompoundTag("cmdManager", cmdManager); */ nbt.setString("cmdText", this.displayText); diff --git a/src/minecraft/assemblyline/common/machine/command/Command.java b/src/minecraft/assemblyline/common/machine/command/Command.java index 2d633a4d..f76efe7a 100644 --- a/src/minecraft/assemblyline/common/machine/command/Command.java +++ b/src/minecraft/assemblyline/common/machine/command/Command.java @@ -34,6 +34,7 @@ public abstract class Command registerCommand("use", CommandUse.class); registerCommand("fire", CommandFire.class); registerCommand("break", CommandBreak.class); + registerCommand("place", CommandPlace.class); } public static void registerCommand(String command, Class commandClass) diff --git a/src/minecraft/assemblyline/common/machine/command/CommandBreak.java b/src/minecraft/assemblyline/common/machine/command/CommandBreak.java index 0679e747..52afc338 100644 --- a/src/minecraft/assemblyline/common/machine/command/CommandBreak.java +++ b/src/minecraft/assemblyline/common/machine/command/CommandBreak.java @@ -11,21 +11,13 @@ import universalelectricity.core.vector.Vector3; */ public class CommandBreak extends Command { - public static final float radius = 0.5f; - - public CommandBreak() - { - super(); - } + private CommandRotateTo rotateToCommand; @Override protected boolean doTask() { super.doTask(); - if (this.tileEntity.grabbedEntities.size() > 0) - return false; - Vector3 serachPosition = this.tileEntity.getHandPosition(); Block block = Block.blocksList[serachPosition.getBlockID(this.world)]; @@ -34,6 +26,7 @@ public class CommandBreak extends Command { block.dropBlockAsItem(this.world, serachPosition.intX(), serachPosition.intY(), serachPosition.intZ(), serachPosition.getBlockMetadata(this.world), 0); serachPosition.setBlockWithNotify(this.world, 0); + return false; } return true; diff --git a/src/minecraft/assemblyline/common/machine/command/CommandDrop.java b/src/minecraft/assemblyline/common/machine/command/CommandDrop.java index 18f2bb02..6a8d6175 100644 --- a/src/minecraft/assemblyline/common/machine/command/CommandDrop.java +++ b/src/minecraft/assemblyline/common/machine/command/CommandDrop.java @@ -13,24 +13,30 @@ public class CommandDrop extends Command if (this.tileEntity.grabbedEntities.size() == 0) return false; - // TODO: Animate Armbot to move down and drop all items. for (Entity entity : this.tileEntity.grabbedEntities) { if (entity != null) { entity.isDead = false; entity.worldObj = this.tileEntity.worldObj; + if (entity instanceof EntityItem) + { if (!world.isRemote) - world.spawnEntityInWorld(entity); // items don't move right, so we render - // them manually - this.world.playSound(this.tileEntity.xCoord, this.tileEntity.yCoord, this.tileEntity.zCoord, "random.pop", 0.2F, ((this.tileEntity.worldObj.rand.nextFloat() - this.tileEntity.worldObj.rand.nextFloat()) * 0.7F + 1.0F) * 1.0F, true); + { + // TODO: This causes crash. + // world.spawnEntityInWorld(entity); + } + } } } + + this.world.playSound(this.tileEntity.xCoord, this.tileEntity.yCoord, this.tileEntity.zCoord, "random.pop", 0.2F, ((this.tileEntity.worldObj.rand.nextFloat() - this.tileEntity.worldObj.rand.nextFloat()) * 0.7F + 1.0F) * 1.0F, true); + this.tileEntity.grabbedEntities.clear(); return false; } - + @Override public String toString() { diff --git a/src/minecraft/assemblyline/common/machine/command/CommandGrab.java b/src/minecraft/assemblyline/common/machine/command/CommandGrab.java index 55e47746..ebdc9550 100644 --- a/src/minecraft/assemblyline/common/machine/command/CommandGrab.java +++ b/src/minecraft/assemblyline/common/machine/command/CommandGrab.java @@ -48,15 +48,16 @@ public class CommandGrab extends Command if (found.get(i) != null && !(found.get(i) instanceof EntityPlayer) && !(found.get(i) instanceof EntityArrow) && found.get(i).ridingEntity == null) { this.tileEntity.grabbedEntities.add(found.get(i)); - + if (found.get(i) instanceof EntityItem) { // items don't move right, so we render them manually this.tileEntity.worldObj.removeEntity(found.get(i)); } - - this.world.playSound(this.tileEntity.xCoord, this.tileEntity.yCoord, this.tileEntity.zCoord, "random.pop", 0.2F, ((this.tileEntity.worldObj.rand.nextFloat() - this.tileEntity.worldObj.rand.nextFloat()) * 0.7F + 1.0F) * 1.0F, true); + found.get(i).isDead = false; + + this.world.playSound(this.tileEntity.xCoord, this.tileEntity.yCoord, this.tileEntity.zCoord, "random.pop", 0.2F, ((this.tileEntity.worldObj.rand.nextFloat() - this.tileEntity.worldObj.rand.nextFloat()) * 0.7F + 1.0F) * 1.0F, true); return false; } } diff --git a/src/minecraft/assemblyline/common/machine/command/CommandPlace.java b/src/minecraft/assemblyline/common/machine/command/CommandPlace.java new file mode 100644 index 00000000..18667152 --- /dev/null +++ b/src/minecraft/assemblyline/common/machine/command/CommandPlace.java @@ -0,0 +1,74 @@ +package assemblyline.common.machine.command; + +import net.minecraft.block.Block; +import net.minecraft.entity.Entity; +import net.minecraft.entity.item.EntityItem; +import net.minecraft.item.ItemBlock; +import net.minecraft.item.ItemStack; +import net.minecraftforge.common.IPlantable; +import universalelectricity.core.vector.Vector3; + +/** + * Used by arms to break a specific block in a position. + * + * @author Calclavia + */ +public class CommandPlace extends Command +{ + @Override + protected boolean doTask() + { + super.doTask(); + + Vector3 serachPosition = this.tileEntity.getHandPosition(); + + Block block = Block.blocksList[serachPosition.getBlockID(this.world)]; + + if (block == null) + { + for (Entity entity : this.tileEntity.grabbedEntities) + { + if (entity instanceof EntityItem) + { + ItemStack itemStack = ((EntityItem) entity).getEntityItem(); + + if (itemStack != null) + { + if (itemStack.getItem() instanceof ItemBlock) + { + ((ItemBlock) itemStack.getItem()).placeBlockAt(itemStack, null, this.world, serachPosition.intX(), serachPosition.intY(), serachPosition.intZ(), 0, 0.5f, 0.5f, 0.5f, itemStack.getItemDamage()); + + this.tileEntity.grabbedEntities.remove(entity); + return false; + } + else if (itemStack.getItem() instanceof IPlantable) + { + IPlantable plantable = ((IPlantable) itemStack.getItem()); + int blockID = plantable.getPlantID(this.world, serachPosition.intX(), serachPosition.intY(), serachPosition.intZ()); + int blockMetadata = plantable.getPlantMetadata(this.world, serachPosition.intX(), serachPosition.intY(), serachPosition.intZ()); + + if (!world.setBlockAndMetadataWithNotify(serachPosition.intX(), serachPosition.intY(), serachPosition.intZ(), blockID, blockMetadata)) { return false; } + + if (world.getBlockId(serachPosition.intX(), serachPosition.intY(), serachPosition.intZ()) == blockID) + { + Block.blocksList[blockID].onBlockPlacedBy(world, serachPosition.intX(), serachPosition.intY(), serachPosition.intZ(), null); + Block.blocksList[blockID].onPostBlockPlaced(world, serachPosition.intX(), serachPosition.intY(), serachPosition.intZ(), blockMetadata); + } + + this.tileEntity.grabbedEntities.remove(entity); + return false; + } + } + } + } + } + + return false; + } + + @Override + public String toString() + { + return "PLACE"; + } +} diff --git a/src/minecraft/assemblyline/common/machine/command/CommandRotateTo.java b/src/minecraft/assemblyline/common/machine/command/CommandRotateTo.java index 7f8c65ee..cdb475cb 100644 --- a/src/minecraft/assemblyline/common/machine/command/CommandRotateTo.java +++ b/src/minecraft/assemblyline/common/machine/command/CommandRotateTo.java @@ -9,9 +9,9 @@ import net.minecraft.nbt.NBTTagCompound; */ public class CommandRotateTo extends Command { - float targetRotationYaw = 0; - float targetRotationPitch = 0; - int totalTicks = 0; + float targetRotationYaw = 0; + float targetRotationPitch = 0; + int totalTicks = 0; @Override public void onTaskStart() @@ -43,7 +43,7 @@ public class CommandRotateTo extends Command this.targetRotationYaw += 360; while (this.targetRotationYaw > 360) this.targetRotationYaw -= 360; - while (this.targetRotationPitch < 0) + while (this.targetRotationPitch < -60) this.targetRotationPitch += 60; while (this.targetRotationPitch > 60) this.targetRotationPitch -= 60; @@ -60,7 +60,10 @@ public class CommandRotateTo extends Command /* * float rotationalDifference = Math.abs(this.tileEntity.rotationYaw - this.targetRotation); * - * if (rotationalDifference < ROTATION_SPEED) { this.tileEntity.rotationYaw = this.targetRotation; } else { if (this.tileEntity.rotationYaw > this.targetRotation) { this.tileEntity.rotationYaw -= ROTATION_SPEED; } else { this.tileEntity.rotationYaw += ROTATION_SPEED; } this.ticks = 0; } + * if (rotationalDifference < ROTATION_SPEED) { this.tileEntity.rotationYaw = + * this.targetRotation; } else { if (this.tileEntity.rotationYaw > this.targetRotation) { + * this.tileEntity.rotationYaw -= ROTATION_SPEED; } else { this.tileEntity.rotationYaw += + * ROTATION_SPEED; } this.ticks = 0; } */ // set the rotation to the target immediately and let the client handle animating it