From 648f57ed6adb662de673d07d433bfc1a41584454 Mon Sep 17 00:00:00 2001 From: tterrag1098 Date: Mon, 12 Jan 2015 00:19:05 -0500 Subject: [PATCH] Finalize auto chisel animation. Closes #135 --- .../tileentity/TileEntityAutoChisel.java | 83 ++++++++++--- .../chisel/client/GeneralChiselClient.java | 14 +++ .../client/render/tile/RenderAutoChisel.java | 117 ++++++------------ .../network/message/MessageAutoChisel.java | 29 ++++- 4 files changed, 143 insertions(+), 100 deletions(-) diff --git a/src/main/java/com/cricketcraft/chisel/block/tileentity/TileEntityAutoChisel.java b/src/main/java/com/cricketcraft/chisel/block/tileentity/TileEntityAutoChisel.java index 754de116..b39cc89f 100644 --- a/src/main/java/com/cricketcraft/chisel/block/tileentity/TileEntityAutoChisel.java +++ b/src/main/java/com/cricketcraft/chisel/block/tileentity/TileEntityAutoChisel.java @@ -16,6 +16,7 @@ import net.minecraft.util.StatCollector; import com.cricketcraft.chisel.api.IChiselItem; import com.cricketcraft.chisel.carving.Carving; +import com.cricketcraft.chisel.client.GeneralChiselClient; import com.cricketcraft.chisel.init.ChiselItems; import com.cricketcraft.chisel.network.PacketHandler; import com.cricketcraft.chisel.network.message.MessageAutoChisel; @@ -39,19 +40,28 @@ public class TileEntityAutoChisel extends TileEntity implements ISidedInventory } public static final int BASE = 0, TARGET = 1, OUTPUT = 2, CHISEL = 3, MIN_UPGRADE = 4; + private static final int FAST_SPEED = 1, SLOW_SPEED = 4; + + private int progress = 0; private static EntityItem ghostItem; boolean equal = false; private ItemStack[] inventory = new ItemStack[7]; private String name = "autoChisel"; - // used for floating target client only + // client animation fields + + // used for floating target public float xRot, yRot, zRot; - // used for chisel item rotation client only + // used for chisel item rotation + public static final int maxRot = 60; + public static final int rotAmnt = 15; public float chiselRot; public boolean chiseling = false; + private ItemStack lastBase; + @Override public boolean canUpdate() { return true; @@ -98,13 +108,16 @@ public class TileEntityAutoChisel extends TileEntity implements ISidedInventory @Override public void updateEntity() { - int checkPeriod = hasUpgrade(Upgrade.SPEED) ? 10 : 40; - ItemStack base = inventory[BASE], target = inventory[TARGET], output = inventory[OUTPUT]; - if (!worldObj.isRemote && worldObj.getTotalWorldTime() % checkPeriod == 0 && hasChisel()) { - if (base != null && target != null) { - if (canBeMadeFrom(base, target)) { + if (!worldObj.isRemote && hasChisel() && base != null && target != null) { + if (canBeMadeFrom(base, target)) { + + if (progress >= (hasUpgrade(Upgrade.SPEED) ? FAST_SPEED : SLOW_SPEED)) { + + // reset progress + progress = 0; + // the max possible for this craft int canBeMade = target.getMaxStackSize(); @@ -136,18 +149,29 @@ public class TileEntityAutoChisel extends TileEntity implements ISidedInventory inventory[OUTPUT].stackSize += chiseled.stackSize; slotChanged(OUTPUT); } + + chiselItem(); + // remove what we made from the stack base.stackSize -= chiseled.stackSize; - slotChanged(BASE); if (base.stackSize <= 0) { setInventorySlotContents(BASE, null); // clear out 0 size itemstacks } - - // TODO here we need to send a packet to the client so that the animation and sound can be played - chiselItem(); } + } else if (worldObj.getTotalWorldTime() % 10 == 0) { + progress++; } } + } else if (worldObj.isRemote) { + System.out.println(lastBase); + if (chiseling) { + chiselRot += rotAmnt; + if (chiselRot >= maxRot) { + chiselItem(); + } + } else { + chiselRot = Math.max(chiselRot - rotAmnt, 0); + } } } @@ -182,8 +206,19 @@ public class TileEntityAutoChisel extends TileEntity implements ISidedInventory /** Calls IChiselItem#onChisel() and sends the chisel packet for sound/animation */ private void chiselItem() { - ((IChiselItem) inventory[CHISEL].getItem()).onChisel(worldObj, this, CHISEL, inventory[CHISEL], inventory[TARGET]); - PacketHandler.INSTANCE.sendToDimension(new MessageAutoChisel(this), worldObj.provider.dimensionId); + if (!worldObj.isRemote) { + ((IChiselItem) inventory[CHISEL].getItem()).onChisel(worldObj, this, CHISEL, inventory[CHISEL], inventory[TARGET]); + PacketHandler.INSTANCE.sendToDimension(new MessageAutoChisel(this, true), worldObj.provider.dimensionId); + } else { + GeneralChiselClient.spawnAutoChiselFX(this, lastBase != null ? lastBase : inventory[BASE]); + chiseling = false; + if (lastBase != null) { + lastBase.stackSize--; + if (lastBase.stackSize <= 0) { + lastBase = null; + } + } + } } @Override @@ -234,6 +269,11 @@ public class TileEntityAutoChisel extends TileEntity implements ISidedInventory if (stack != null && stack.stackSize > getInventoryStackLimit()) { stack.stackSize = getInventoryStackLimit(); } + + if (worldObj.isRemote && slot == BASE && stack != null) { + lastBase = stack.copy(); + lastBase.stackSize++; + } if (!worldObj.isRemote) { slotChanged(slot); @@ -316,7 +356,10 @@ public class TileEntityAutoChisel extends TileEntity implements ISidedInventory ghostItem.hoverStart = 0.0F; } - if (inventory[slot] == null) { + if (slot == BASE && lastBase != null) { + ghostItem.setEntityItemStack(lastBase.copy()); + return ghostItem; + } else if (inventory[slot] == null) { return null; } else { ghostItem.setEntityItemStack(new ItemStack(inventory[slot].getItem(), 1, inventory[slot].getItemDamage())); @@ -359,7 +402,15 @@ public class TileEntityAutoChisel extends TileEntity implements ISidedInventory } } - public void doChiselAnim() { - chiseling = true; + public void doChiselAnim(ItemStack lastChiseled, boolean playSound) { + System.out.println(lastChiseled + " " + playSound); + this.lastBase = lastChiseled == null ? null : lastChiseled.copy(); + if (playSound) { + this.chiseling = true; + } + } + + public ItemStack getLastBase() { + return lastBase == null ? null : lastBase.copy(); } } diff --git a/src/main/java/com/cricketcraft/chisel/client/GeneralChiselClient.java b/src/main/java/com/cricketcraft/chisel/client/GeneralChiselClient.java index b88ef912..14f72411 100644 --- a/src/main/java/com/cricketcraft/chisel/client/GeneralChiselClient.java +++ b/src/main/java/com/cricketcraft/chisel/client/GeneralChiselClient.java @@ -9,12 +9,15 @@ import net.minecraft.client.particle.EffectRenderer; import net.minecraft.client.particle.EntityDiggingFX; import net.minecraft.client.particle.EntityLavaFX; import net.minecraft.entity.Entity; +import net.minecraft.item.ItemStack; import net.minecraft.world.World; import com.cricketcraft.chisel.block.BlockGrimstone; import com.cricketcraft.chisel.block.BlockHolystone; import com.cricketcraft.chisel.block.BlockLavastone; import com.cricketcraft.chisel.block.BlockSnakestoneObsidian; +import com.cricketcraft.chisel.block.tileentity.TileEntityAutoChisel; +import com.cricketcraft.chisel.carving.Carving; import com.cricketcraft.chisel.config.Configurations; import com.cricketcraft.chisel.entity.fx.EntityBallOMossFX; import com.cricketcraft.chisel.entity.fx.EntityGrimstoneFX; @@ -253,4 +256,15 @@ public class GeneralChiselClient { entity.motionZ = Configurations.concreteVelocity * entity.motionZ / velocity; } + public static void spawnAutoChiselFX(TileEntityAutoChisel te, ItemStack base) { + if (base != null) { + for (int i = 0; i < 10; i++) { + EntityDiggingFX particle = new EntityDiggingFX(te.getWorldObj(), te.xCoord + 0.5, te.yCoord + 0.95, te.zCoord + 0.5, 0, 0, 0, Block.getBlockFromItem(base.getItem()), base.getItemDamage()); + particle.setVelocity((te.getWorldObj().rand.nextDouble() / 4) - 0.125, te.getWorldObj().rand.nextDouble() / 8, (te.getWorldObj().rand.nextDouble() / 4) - 0.125); + Minecraft.getMinecraft().effectRenderer.addEffect(particle); + } + String sound = Carving.chisel.getVariationSound(base.getItem(), base.getItemDamage()); + GeneralClient.playChiselSound(te.getWorldObj(), te.xCoord, te.yCoord, te.zCoord, sound); + } + } } diff --git a/src/main/java/com/cricketcraft/chisel/client/render/tile/RenderAutoChisel.java b/src/main/java/com/cricketcraft/chisel/client/render/tile/RenderAutoChisel.java index 7fe9c67b..40da271e 100644 --- a/src/main/java/com/cricketcraft/chisel/client/render/tile/RenderAutoChisel.java +++ b/src/main/java/com/cricketcraft/chisel/client/render/tile/RenderAutoChisel.java @@ -4,7 +4,6 @@ import java.util.Random; import net.minecraft.block.Block; import net.minecraft.client.Minecraft; -import net.minecraft.client.particle.EntityDiggingFX; import net.minecraft.client.renderer.RenderBlocks; import net.minecraft.client.renderer.Tessellator; import net.minecraft.client.renderer.entity.RenderItem; @@ -14,10 +13,8 @@ import net.minecraft.entity.item.EntityItem; import net.minecraft.item.ItemStack; import net.minecraft.tileentity.TileEntity; import net.minecraft.util.IIcon; -import net.minecraft.util.MathHelper; import net.minecraft.util.ResourceLocation; import net.minecraft.world.IBlockAccess; -import net.minecraft.world.World; import net.minecraftforge.client.IItemRenderer; import net.minecraftforge.client.model.obj.Face; import net.minecraftforge.client.model.obj.GroupObject; @@ -25,18 +22,13 @@ import net.minecraftforge.client.model.obj.TextureCoordinate; import net.minecraftforge.client.model.obj.Vertex; import net.minecraftforge.client.model.obj.WavefrontObject; -import org.lwjgl.opengl.GL11; - import com.cricketcraft.chisel.Chisel; import com.cricketcraft.chisel.block.tileentity.TileEntityAutoChisel; -import com.cricketcraft.chisel.carving.Carving; -import com.cricketcraft.chisel.client.GeneralChiselClient; -import com.cricketcraft.chisel.utils.GeneralClient; import cpw.mods.fml.client.registry.ISimpleBlockRenderingHandler; import cpw.mods.fml.client.registry.RenderingRegistry; -import cpw.mods.fml.relauncher.Side; -import cpw.mods.fml.relauncher.SideOnly; + +import static org.lwjgl.opengl.GL11.*; public class RenderAutoChisel extends TileEntitySpecialRenderer implements ISimpleBlockRenderingHandler, IItemRenderer { @@ -57,24 +49,24 @@ public class RenderAutoChisel extends TileEntitySpecialRenderer implements ISimp public void renderItem(ItemRenderType type, ItemStack item, Object... data) { switch (type) { case ENTITY: - GL11.glTranslatef(-0.5f, 0, 0.5f); + glTranslatef(-0.5f, 0, 0.5f); break; case EQUIPPED: - GL11.glRotatef(20, 0, 1, 0); - GL11.glRotatef(-15, 1, 0, -1); - GL11.glScalef(0.65f, 0.65f, 0.65f); - GL11.glTranslatef(0.75f, -0.5f, 1.25f); + glRotatef(20, 0, 1, 0); + glRotatef(-15, 1, 0, -1); + glScalef(0.65f, 0.65f, 0.65f); + glTranslatef(0.75f, -0.5f, 1.25f); break; case EQUIPPED_FIRST_PERSON: - GL11.glRotatef(20, 0, 0, 1); - GL11.glRotatef(30, 0, 1, 0); - GL11.glTranslatef(0.2f, 0, -0.1f); - GL11.glScalef(0.5f, 0.5f, 0.5f); + glRotatef(20, 0, 0, 1); + glRotatef(30, 0, 1, 0); + glTranslatef(0.2f, 0, -0.1f); + glScalef(0.5f, 0.5f, 0.5f); break; case FIRST_PERSON_MAP: break; case INVENTORY: - GL11.glTranslatef(-0.5f, -0.5f, 0.5f); + glTranslatef(-0.5f, -0.5f, 0.5f); break; } @@ -119,8 +111,6 @@ public class RenderAutoChisel extends TileEntitySpecialRenderer implements ISimp return Chisel.renderAutoChiselId; } - private static EntityItem ghostItem; - private final RenderItem renderItem; public RenderAutoChisel() { @@ -157,80 +147,43 @@ public class RenderAutoChisel extends TileEntitySpecialRenderer implements ISimp } if (item != null) { - GL11.glPushMatrix(); - GL11.glPushAttrib(GL11.GL_ALL_ATTRIB_BITS); - GL11.glTranslated(x + 0.5, y + 1.5, z + 0.5); - GL11.glRotatef(autoChisel.xRot, 1, 0, 0); - GL11.glRotatef(autoChisel.yRot, 0, 1, 0); - GL11.glRotatef(autoChisel.zRot, 0, 0, 1); - GL11.glEnable(GL11.GL_BLEND); - GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_CONSTANT_ALPHA); + glPushMatrix(); + glPushAttrib(GL_ALL_ATTRIB_BITS); + glTranslated(x + 0.5, y + 1.5, z + 0.5); + glRotatef(autoChisel.xRot, 1, 0, 0); + glRotatef(autoChisel.yRot, 0, 1, 0); + glRotatef(autoChisel.zRot, 0, 0, 1); + glEnable(GL_BLEND); + glDepthMask(false); + glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_CONSTANT_ALPHA); renderItem.doRender(item, 0, 0, 0, 0, 0); - GL11.glPopMatrix(); - GL11.glPopAttrib(); + glPopMatrix(); + glPopAttrib(); } item = autoChisel.getItemForRendering(TileEntityAutoChisel.BASE); if (item != null) { - GL11.glPushMatrix(); - GL11.glTranslated(x + 0.35, y + 0.934, z + 0.5); - item.getEntityItem().stackSize = autoChisel.getStackInSlot(TileEntityAutoChisel.BASE).stackSize; + glPushMatrix(); + glTranslated(x + 0.35, y + 0.934, z + 0.5); + item.getEntityItem().stackSize = autoChisel.getLastBase() == null ? 1 : autoChisel.getLastBase().stackSize; renderItem.doRender(item, 0, 0, 0, 0, 0); - GL11.glPopMatrix(); + glPopMatrix(); } item = autoChisel.getItemForRendering(TileEntityAutoChisel.CHISEL); if (item != null) { - if (autoChisel.chiseling) { - autoChisel.chiselRot += 1; - if (autoChisel.chiselRot > 70) { - chiselItem(autoChisel); - } - } else { - autoChisel.chiselRot = Math.max(autoChisel.chiselRot - 1, 0); - } - GL11.glPushMatrix(); - GL11.glTranslated(x + 0.7, y + 1.01, z + 0.5); - GL11.glRotatef(autoChisel.chiselRot, 0, 0, 1); - GL11.glTranslated(-0.12, 0, 0); - GL11.glScalef(0.9f, 0.9f, 0.9f); + glPushMatrix(); + glTranslated(x + 0.7, y + 1.01, z + 0.5); + float rot = autoChisel.chiselRot == 0 ? 0 : autoChisel.chiseling ? autoChisel.chiselRot + (TileEntityAutoChisel.rotAmnt * scale) : autoChisel.chiselRot + - (TileEntityAutoChisel.rotAmnt * scale); + glRotatef(rot, 0, 0, 1); + glTranslated(-0.12, 0, 0); + glScalef(0.9f, 0.9f, 0.9f); renderItem.doRender(item, 0, 0, 0, 0, 0); - GL11.glPopMatrix(); + glPopMatrix(); } else { autoChisel.chiselRot = 0; autoChisel.chiseling = false; } } - - private void chiselItem(TileEntityAutoChisel autoChisel) { - World world = autoChisel.getWorldObj(); - ItemStack base = autoChisel.getStackInSlot(TileEntityAutoChisel.BASE); - if (base != null) { - for (int i = 0; i < 5; i++) { - EntityDiggingFX particle = new EntityDiggingFX(world, autoChisel.xCoord + 0.5, autoChisel.yCoord + 1, autoChisel.zCoord + 0.5, 0, 0, 0, Block.getBlockFromItem(base.getItem()), - base.getItemDamage()); - particle.setVelocity((world.rand.nextDouble() / 4) - 0.125, world.rand.nextDouble() / 8, (world.rand.nextDouble() / 4) - 0.125); - Minecraft.getMinecraft().effectRenderer.addEffect(particle); - } - String sound = Carving.chisel.getVariationSound(base.getItem(), base.getItemDamage()); - GeneralClient.playChiselSound(world, autoChisel.xCoord, autoChisel.yCoord, autoChisel.zCoord, sound); - } - autoChisel.chiseling = false; - } - - @SideOnly(Side.CLIENT) - public EntityItem getGhostItem(World world, ItemStack itemStack) { - if (ghostItem == null) { - ghostItem = new EntityItem(world); - ghostItem.hoverStart = 0.0F; - } - - if (itemStack == null) { - return null; - } else { - ghostItem.setEntityItemStack(itemStack); - return ghostItem; - } - } - } diff --git a/src/main/java/com/cricketcraft/chisel/network/message/MessageAutoChisel.java b/src/main/java/com/cricketcraft/chisel/network/message/MessageAutoChisel.java index d667e84d..c4a811a9 100644 --- a/src/main/java/com/cricketcraft/chisel/network/message/MessageAutoChisel.java +++ b/src/main/java/com/cricketcraft/chisel/network/message/MessageAutoChisel.java @@ -1,10 +1,13 @@ package com.cricketcraft.chisel.network.message; +import io.netty.buffer.ByteBuf; +import net.minecraft.item.ItemStack; import net.minecraft.tileentity.TileEntity; import com.cricketcraft.chisel.block.tileentity.TileEntityAutoChisel; import com.cricketcraft.chisel.network.message.base.MessageCoords; +import cpw.mods.fml.common.network.ByteBufUtils; import cpw.mods.fml.common.network.simpleimpl.IMessage; import cpw.mods.fml.common.network.simpleimpl.IMessageHandler; import cpw.mods.fml.common.network.simpleimpl.MessageContext; @@ -14,8 +17,30 @@ public class MessageAutoChisel extends MessageCoords { public MessageAutoChisel() { } - public MessageAutoChisel(TileEntityAutoChisel tile) { + ItemStack base; + boolean playSound; + + public MessageAutoChisel(TileEntityAutoChisel tile, boolean playSound) { super(tile); + this.base = tile.getStackInSlot(TileEntityAutoChisel.BASE); + if (base != null) { + base = base.copy(); + } + this.playSound = playSound; + } + + @Override + public void toBytes(ByteBuf buf) { + super.toBytes(buf); + buf.writeBoolean(playSound); + ByteBufUtils.writeItemStack(buf, base); + } + + @Override + public void fromBytes(ByteBuf buf) { + super.fromBytes(buf); + this.playSound = buf.readBoolean(); + this.base = ByteBufUtils.readItemStack(buf); } public static class Handler implements IMessageHandler { @@ -24,7 +49,7 @@ public class MessageAutoChisel extends MessageCoords { TileEntity te = message.getTileEntity(ctx); if (te instanceof TileEntityAutoChisel) { - ((TileEntityAutoChisel) te).doChiselAnim(); + ((TileEntityAutoChisel) te).doChiselAnim(message.base, message.playSound); } return null; }