Clean up some code, fix ItemStacks with damages being counted as two different items when sorting blocks for the top stack (Swords for example) and switch to vanilla's way of handling saving items to Byte data.

This commit is contained in:
alexbegt 2017-05-27 21:49:53 -04:00
parent 767528115e
commit 63047fb477
9 changed files with 44 additions and 64 deletions

View File

@ -47,10 +47,9 @@ public class BlockIronChest extends Block
public BlockIronChest() public BlockIronChest()
{ {
super(Material.IRON); super(Material.IRON);
this.setRegistryName(new ResourceLocation(IronChest.MOD_ID, "BlockIronChest")); this.setRegistryName(new ResourceLocation(IronChest.MOD_ID, "BlockIronChest"));
this.setDefaultState(this.blockState.getBaseState().withProperty(VARIANT_PROP, IronChestType.IRON)); this.setDefaultState(this.blockState.getBaseState().withProperty(VARIANT_PROP, IronChestType.IRON));
this.setHardness(3.0F); this.setHardness(3.0F);
this.setUnlocalizedName("IronChest"); this.setUnlocalizedName("IronChest");
this.setCreativeTab(CreativeTabs.DECORATIONS); this.setCreativeTab(CreativeTabs.DECORATIONS);
@ -103,6 +102,7 @@ public class BlockIronChest extends Block
} }
player.openGui(IronChest.instance, ((TileEntityIronChest) te).getType().ordinal(), world, pos.getX(), pos.getY(), pos.getZ()); player.openGui(IronChest.instance, ((TileEntityIronChest) te).getType().ordinal(), world, pos.getX(), pos.getY(), pos.getZ());
return true; return true;
} }
@ -153,6 +153,7 @@ public class BlockIronChest extends Block
public void onBlockAdded(World world, BlockPos pos, IBlockState blockState) public void onBlockAdded(World world, BlockPos pos, IBlockState blockState)
{ {
super.onBlockAdded(world, pos, blockState); super.onBlockAdded(world, pos, blockState);
world.notifyBlockUpdate(pos, blockState, blockState, 3); world.notifyBlockUpdate(pos, blockState, blockState, 3);
} }
@ -160,11 +161,14 @@ public class BlockIronChest extends Block
public void onBlockPlacedBy(World world, BlockPos pos, IBlockState blockState, EntityLivingBase entityliving, ItemStack itemStack) public void onBlockPlacedBy(World world, BlockPos pos, IBlockState blockState, EntityLivingBase entityliving, ItemStack itemStack)
{ {
TileEntity te = world.getTileEntity(pos); TileEntity te = world.getTileEntity(pos);
if (te != null && te instanceof TileEntityIronChest) if (te != null && te instanceof TileEntityIronChest)
{ {
TileEntityIronChest teic = (TileEntityIronChest) te; TileEntityIronChest teic = (TileEntityIronChest) te;
teic.wasPlaced(entityliving, itemStack); teic.wasPlaced(entityliving, itemStack);
teic.setFacing(entityliving.getHorizontalFacing().getOpposite()); teic.setFacing(entityliving.getHorizontalFacing().getOpposite());
world.notifyBlockUpdate(pos, blockState, blockState, 3); world.notifyBlockUpdate(pos, blockState, blockState, 3);
} }
} }
@ -186,6 +190,7 @@ public class BlockIronChest extends Block
InventoryHelper.dropInventoryItems(worldIn, pos, tileentity); InventoryHelper.dropInventoryItems(worldIn, pos, tileentity);
} }
super.breakBlock(worldIn, pos, state); super.breakBlock(worldIn, pos, state);
} }
@ -193,14 +198,17 @@ public class BlockIronChest extends Block
public float getExplosionResistance(World world, BlockPos pos, Entity exploder, Explosion explosion) public float getExplosionResistance(World world, BlockPos pos, Entity exploder, Explosion explosion)
{ {
TileEntity te = world.getTileEntity(pos); TileEntity te = world.getTileEntity(pos);
if (te instanceof TileEntityIronChest) if (te instanceof TileEntityIronChest)
{ {
TileEntityIronChest teic = (TileEntityIronChest) te; TileEntityIronChest teic = (TileEntityIronChest) te;
if (teic.getType().isExplosionResistant()) if (teic.getType().isExplosionResistant())
{ {
return 10000F; return 10000F;
} }
} }
return super.getExplosionResistance(world, pos, exploder, explosion); return super.getExplosionResistance(world, pos, exploder, explosion);
} }
@ -214,10 +222,12 @@ public class BlockIronChest extends Block
public int getComparatorInputOverride(IBlockState blockState, World world, BlockPos pos) public int getComparatorInputOverride(IBlockState blockState, World world, BlockPos pos)
{ {
TileEntity te = world.getTileEntity(pos); TileEntity te = world.getTileEntity(pos);
if (te instanceof IInventory) if (te instanceof IInventory)
{ {
return Container.calcRedstoneFromInventory((IInventory) te); return Container.calcRedstoneFromInventory((IInventory) te);
} }
return 0; return 0;
} }
@ -236,14 +246,18 @@ public class BlockIronChest extends Block
{ {
return false; return false;
} }
if (axis == EnumFacing.UP || axis == EnumFacing.DOWN) if (axis == EnumFacing.UP || axis == EnumFacing.DOWN)
{ {
TileEntity tileEntity = worldObj.getTileEntity(pos); TileEntity tileEntity = worldObj.getTileEntity(pos);
if (tileEntity instanceof TileEntityIronChest) if (tileEntity instanceof TileEntityIronChest)
{ {
TileEntityIronChest icte = (TileEntityIronChest) tileEntity; TileEntityIronChest icte = (TileEntityIronChest) tileEntity;
icte.rotateAround(); icte.rotateAround();
} }
return true; return true;
} }
return false; return false;
@ -254,7 +268,9 @@ public class BlockIronChest extends Block
public boolean eventReceived(IBlockState state, World worldIn, BlockPos pos, int id, int param) public boolean eventReceived(IBlockState state, World worldIn, BlockPos pos, int id, int param)
{ {
super.eventReceived(state, worldIn, pos, id, param); super.eventReceived(state, worldIn, pos, id, param);
TileEntity tileentity = worldIn.getTileEntity(pos); TileEntity tileentity = worldIn.getTileEntity(pos);
return tileentity != null && tileentity.receiveClientEvent(id, param); return tileentity != null && tileentity.receiveClientEvent(id, param);
} }
} }

View File

@ -57,8 +57,11 @@ public enum ChestChangerType
public ItemChestChanger buildItem() public ItemChestChanger buildItem()
{ {
this.item = new ItemChestChanger(this); this.item = new ItemChestChanger(this);
this.item.setRegistryName(this.itemName); this.item.setRegistryName(this.itemName);
GameRegistry.register(this.item); GameRegistry.register(this.item);
return this.item; return this.item;
} }

View File

@ -45,10 +45,12 @@ public class ContainerIronChest extends Container
{ {
ItemStack itemstack = null; ItemStack itemstack = null;
Slot slot = this.inventorySlots.get(i); Slot slot = this.inventorySlots.get(i);
if (slot != null && slot.getHasStack()) if (slot != null && slot.getHasStack())
{ {
ItemStack itemstack1 = slot.getStack(); ItemStack itemstack1 = slot.getStack();
itemstack = itemstack1.copy(); itemstack = itemstack1.copy();
if (i < this.type.size) if (i < this.type.size)
{ {
if (!this.mergeItemStack(itemstack1, this.type.size, this.inventorySlots.size(), true)) if (!this.mergeItemStack(itemstack1, this.type.size, this.inventorySlots.size(), true))
@ -73,6 +75,7 @@ public class ContainerIronChest extends Container
slot.onSlotChanged(); slot.onSlotChanged();
} }
} }
return itemstack; return itemstack;
} }
@ -80,6 +83,7 @@ public class ContainerIronChest extends Container
public void onContainerClosed(EntityPlayer entityplayer) public void onContainerClosed(EntityPlayer entityplayer)
{ {
super.onContainerClosed(entityplayer); super.onContainerClosed(entityplayer);
this.chest.closeInventory(entityplayer); this.chest.closeInventory(entityplayer);
} }
@ -101,12 +105,14 @@ public class ContainerIronChest extends Container
} }
int leftCol = (xSize - 162) / 2 + 1; int leftCol = (xSize - 162) / 2 + 1;
for (int playerInvRow = 0; playerInvRow < 3; playerInvRow++) for (int playerInvRow = 0; playerInvRow < 3; playerInvRow++)
{ {
for (int playerInvCol = 0; playerInvCol < 9; playerInvCol++) for (int playerInvCol = 0; playerInvCol < 9; playerInvCol++)
{ {
this.addSlotToContainer( //@formatter:off
new Slot(playerInventory, playerInvCol + playerInvRow * 9 + 9, leftCol + playerInvCol * 18, ySize - (4 - playerInvRow) * 18 - 10)); this.addSlotToContainer(new Slot(playerInventory, playerInvCol + playerInvRow * 9 + 9, leftCol + playerInvCol * 18, ySize - (4 - playerInvRow) * 18 - 10));
//@formatter:on
} }
} }

View File

@ -52,6 +52,7 @@ public class IronChest
String minor = properties.getProperty("IronChest.build.minor.number"); String minor = properties.getProperty("IronChest.build.minor.number");
String rev = properties.getProperty("IronChest.build.revision.number"); String rev = properties.getProperty("IronChest.build.revision.number");
String build = properties.getProperty("IronChest.build.number"); String build = properties.getProperty("IronChest.build.number");
event.getModMetadata().version = String.format("%s.%s.%s build %s", major, minor, rev, build); event.getModMetadata().version = String.format("%s.%s.%s build %s", major, minor, rev, build);
} }

View File

@ -109,11 +109,13 @@ public enum IronChestType implements IStringSerializable
public static void registerBlocksAndRecipes(BlockIronChest blockResult) public static void registerBlocksAndRecipes(BlockIronChest blockResult)
{ {
Object previous = "chestWood"; Object previous = "chestWood";
for (IronChestType typ : values()) for (IronChestType typ : values())
{ {
generateRecipesForType(blockResult, previous, typ); generateRecipesForType(blockResult, previous, typ);
ItemStack chest = new ItemStack(blockResult, 1, typ.ordinal()); ItemStack chest = new ItemStack(blockResult, 1, typ.ordinal());
// if (typ.isValidForCreativeMode()) GameRegistry.registerCustomItemStack(typ.friendlyName, chest);//TODO fix this!!
if (typ.tieredChest) if (typ.tieredChest)
{ {
previous = chest; previous = chest;
@ -152,12 +154,14 @@ public enum IronChestType implements IStringSerializable
{ {
return Blocks.DIRT; return Blocks.DIRT;
} }
return mat; return mat;
} }
public static void addRecipe(ItemStack is, Object... parts) public static void addRecipe(ItemStack is, Object... parts)
{ {
ShapedOreRecipe oreRecipe = new ShapedOreRecipe(is, parts); ShapedOreRecipe oreRecipe = new ShapedOreRecipe(is, parts);
GameRegistry.addRecipe(oreRecipe); GameRegistry.addRecipe(oreRecipe);
} }

View File

@ -168,7 +168,7 @@ public class TileEntityIronChest extends TileEntityLockableLoot implements ITick
{ {
ItemStack tempCopyStack = tempCopy[j]; ItemStack tempCopyStack = tempCopy[j];
if (ItemStack.areItemsEqual(tempCopyStack, itemStack)) if (ItemStack.areItemsEqualIgnoreDurability(tempCopyStack, itemStack))
{ {
if (itemStack.stackSize != tempCopyStack.stackSize) if (itemStack.stackSize != tempCopyStack.stackSize)
{ {
@ -196,13 +196,6 @@ public class TileEntityIronChest extends TileEntityLockableLoot implements ITick
this.topStacks[i] = null; this.topStacks[i] = null;
} }
if (this.world != null)
{
IBlockState iblockstate = this.world.getBlockState(this.pos);
this.world.notifyBlockUpdate(this.pos, iblockstate, iblockstate, 3);
}
return; return;
} }
@ -249,13 +242,6 @@ public class TileEntityIronChest extends TileEntityLockableLoot implements ITick
this.topStacks[i] = null; this.topStacks[i] = null;
} }
if (this.world != null)
{
IBlockState iblockstate = this.world.getBlockState(this.pos);
this.world.notifyBlockUpdate(this.pos, iblockstate, iblockstate, 3);
}
sendTopStacksPacket(); sendTopStacksPacket();
} }

View File

@ -66,6 +66,7 @@ public class ClientProxy extends CommonProxy
public Object getClientGuiElement(int ID, EntityPlayer player, World world, int x, int y, int z) public Object getClientGuiElement(int ID, EntityPlayer player, World world, int x, int y, int z)
{ {
TileEntity te = world.getTileEntity(new BlockPos(x, y, z)); TileEntity te = world.getTileEntity(new BlockPos(x, y, z));
if (te != null && te instanceof TileEntityIronChest) if (te != null && te instanceof TileEntityIronChest)
{ {
return GUIChest.GUI.buildGUI(IronChestType.values()[ID], player.inventory, (TileEntityIronChest) te); return GUIChest.GUI.buildGUI(IronChestType.values()[ID], player.inventory, (TileEntityIronChest) te);

View File

@ -91,10 +91,12 @@ public class GUIChest extends GuiContainer
protected void drawGuiContainerBackgroundLayer(float f, int i, int j) protected void drawGuiContainerBackgroundLayer(float f, int i, int j)
{ {
GlStateManager.color(1.0F, 1.0F, 1.0F, 1.0F); GlStateManager.color(1.0F, 1.0F, 1.0F, 1.0F);
// new "bind tex"
this.mc.getTextureManager().bindTexture(this.type.guiResourceList.location); this.mc.getTextureManager().bindTexture(this.type.guiResourceList.location);
int x = (this.width - this.xSize) / 2; int x = (this.width - this.xSize) / 2;
int y = (this.height - this.ySize) / 2; int y = (this.height - this.ySize) / 2;
this.drawTexturedModalRect(x, y, 0, 0, this.xSize, this.ySize); this.drawTexturedModalRect(x, y, 0, 0, this.xSize, this.ySize);
} }
} }

View File

@ -3,7 +3,6 @@ package cpw.mods.ironchest.network;
import cpw.mods.ironchest.IronChest; import cpw.mods.ironchest.IronChest;
import cpw.mods.ironchest.TileEntityIronChest; import cpw.mods.ironchest.TileEntityIronChest;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity; import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.BlockPos;
@ -38,18 +37,9 @@ public class MessageCrystalChestSync implements IMessage
int size = buf.readInt(); int size = buf.readInt();
this.topStacks = new ItemStack[size]; this.topStacks = new ItemStack[size];
int stackListSize = buf.readInt(); for (int i = 0; i < size; i++)
for (int i = 0; i < stackListSize; i++)
{ {
if (i < stackListSize) this.topStacks[i] = ByteBufUtils.readItemStack(buf);
{
this.topStacks[i] = readItemStack(buf);
}
else
{
this.topStacks[i] = null;
}
} }
} }
@ -62,42 +52,12 @@ public class MessageCrystalChestSync implements IMessage
buf.writeInt(this.pos.getZ()); buf.writeInt(this.pos.getZ());
buf.writeInt(topStacks.length); buf.writeInt(topStacks.length);
int stackListSize = 0;
for (ItemStack stack : topStacks) for (ItemStack stack : topStacks)
{ {
if (stack != null) ByteBufUtils.writeItemStack(buf, stack);
{
stackListSize++;
} }
} }
buf.writeInt(stackListSize);
for (ItemStack stack : topStacks)
{
if (stack != null)
{
writeItemStack(buf, stack);
}
}
}
public static void writeItemStack(ByteBuf buf, ItemStack stack)
{
buf.writeInt(Item.getIdFromItem(stack.getItem()));
buf.writeInt(stack.stackSize);
buf.writeInt(stack.getItemDamage());
ByteBufUtils.writeTag(buf, stack.getItem().getNBTShareTag(stack));
}
public static ItemStack readItemStack(ByteBuf buf)
{
ItemStack stack = new ItemStack(Item.getItemById(buf.readInt()), buf.readInt(), buf.readInt());
stack.setTagCompound(ByteBufUtils.readTag(buf));
return stack;
}
public static class Handler implements IMessageHandler<MessageCrystalChestSync, IMessage> public static class Handler implements IMessageHandler<MessageCrystalChestSync, IMessage>
{ {
@Override @Override
@ -112,6 +72,7 @@ public class MessageCrystalChestSync implements IMessage
if (tile instanceof TileEntityIronChest) if (tile instanceof TileEntityIronChest)
((TileEntityIronChest) tile).receiveMessageFromServer(message.topStacks); ((TileEntityIronChest) tile).receiveMessageFromServer(message.topStacks);
} }
return null; return null;
} }
} }