Skip to content

Commit

Permalink
Fixed a Digital Miner oversight, fixed Bins losing NBT data and allow…
Browse files Browse the repository at this point in the history
… Bins to accept items with varying item damage
  • Loading branch information
aidancbrady committed Aug 6, 2014
1 parent 06c66d5 commit 1221549
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 37 deletions.
74 changes: 74 additions & 0 deletions src/main/java/mekanism/common/PacketHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@
import mekanism.common.network.PacketWalkieTalkieState.WalkieTalkieStateMessage;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.CompressedStreamTools;
import net.minecraft.nbt.NBTSizeTracker;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.server.MinecraftServer;
import net.minecraft.util.AxisAlignedBB;
import net.minecraft.util.Vec3;
Expand Down Expand Up @@ -151,6 +156,14 @@ else if(data instanceof Byte)
{
output.writeByte((Byte)data);
}
else if(data instanceof ItemStack)
{
writeStack(output, (ItemStack)data);
}
else if(data instanceof NBTTagCompound)
{
writeNBT(output, (NBTTagCompound)data);
}
else if(data instanceof int[])
{
for(int i : (int[])data)
Expand Down Expand Up @@ -187,6 +200,67 @@ public static String readString(ByteBuf input)
return new String(input.readBytes(input.readInt()).array());
}

public static void writeStack(ByteBuf output, ItemStack stack)
{
output.writeInt(stack != null ? Item.getIdFromItem(stack.getItem()) : -1);

if(stack != null)
{
output.writeInt(stack.stackSize);
output.writeInt(stack.getItemDamage());

if(stack.getTagCompound() != null && stack.getItem().getShareTag())
{
output.writeBoolean(true);
writeNBT(output, stack.getTagCompound());
}
else {
output.writeBoolean(false);
}
}
}

public static ItemStack readStack(ByteBuf input)
{
int id = input.readInt();

if(id >= 0)
{
ItemStack stack = new ItemStack(Item.getItemById(id), input.readInt(), input.readInt());

if(input.readBoolean())
{
stack.setTagCompound(readNBT(input));
}

return stack;
}

return null;
}

public static void writeNBT(ByteBuf output, NBTTagCompound nbtTags)
{
try {
byte[] buffer = CompressedStreamTools.compress(nbtTags);

output.writeInt(buffer.length);
output.writeBytes(buffer);
} catch(Exception e) {}
}

public static NBTTagCompound readNBT(ByteBuf input)
{
try {
byte[] buffer = new byte[input.readInt()];
input.readBytes(buffer);

return CompressedStreamTools.func_152457_a(buffer, new NBTSizeTracker(2097152L));
} catch(Exception e) {
return null;
}
}

public static EntityPlayer getPlayer(MessageContext context)
{
return Mekanism.proxy.getPlayer(context);
Expand Down
6 changes: 4 additions & 2 deletions src/main/java/mekanism/common/block/BlockBasic.java
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ public boolean onBlockActivated(World world, int x, int y, int z, EntityPlayer e

if(bin.getItemCount() < bin.MAX_STORAGE)
{
if(bin.addTicks == 0)
if(bin.addTicks == 0 && entityplayer.getCurrentEquippedItem() != null)
{
if(entityplayer.getCurrentEquippedItem() != null)
{
Expand All @@ -389,7 +389,8 @@ public boolean onBlockActivated(World world, int x, int y, int z, EntityPlayer e
bin.addTicks = 5;
}
}
else {
else if(bin.addTicks > 0 && bin.getItemCount() > 0)
{
ItemStack[] inv = entityplayer.inventory.mainInventory;

for(int i = 0; i < inv.length; i++)
Expand All @@ -403,6 +404,7 @@ public boolean onBlockActivated(World world, int x, int y, int z, EntityPlayer e
{
ItemStack remain = bin.add(inv[i]);
inv[i] = remain;
bin.addTicks = 5;
}

((EntityPlayerMP)entityplayer).sendContainerAndContentsToPlayer(entityplayer.openContainer, entityplayer.openContainer.getInventory());
Expand Down
26 changes: 5 additions & 21 deletions src/main/java/mekanism/common/inventory/InventoryBin.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package mekanism.common.inventory;

import mekanism.api.StackUtils;
import mekanism.common.item.ItemBlockBasic;
import mekanism.common.util.MekanismUtils;
import net.minecraft.item.Item;
Expand Down Expand Up @@ -77,11 +78,6 @@ public boolean isValid(ItemStack stack)
return false;
}

if(stack.isItemStackDamageable() && stack.isItemDamaged())
{
return false;
}

if(stack.getItem() instanceof ItemBlockBasic && stack.getItemDamage() == 6)
{
return false;
Expand Down Expand Up @@ -132,16 +128,7 @@ public ItemStack getItemType()
return null;
}

int id = bin.stackTagCompound.getInteger("itemID");
int meta = bin.stackTagCompound.getInteger("itemMeta");

if(getItemCount() == 0 || id == 0)
{
setItemType(null);
return null;
}

return new ItemStack(Item.getItemById(id), 1, meta);
return ItemStack.loadItemStackFromNBT(bin.stackTagCompound.getCompoundTag("storedItem"));
}

public void setItemType(ItemStack stack)
Expand All @@ -153,15 +140,12 @@ public void setItemType(ItemStack stack)

if(stack == null)
{
bin.stackTagCompound.removeTag("itemID");
bin.stackTagCompound.removeTag("itemMeta");
bin.stackTagCompound.removeTag("storedItem");
return;
}

ItemStack ret = stack.copy();
ret.stackSize = 1;
ItemStack ret = StackUtils.size(stack, 1);

bin.stackTagCompound.setInteger("itemID", MekanismUtils.getID(stack));
bin.stackTagCompound.setInteger("itemMeta", stack.getItemDamage());
bin.stackTagCompound.setTag("storedItem", StackUtils.size(stack, 1).writeToNBT(new NBTTagCompound()));
}
}
6 changes: 2 additions & 4 deletions src/main/java/mekanism/common/tile/TileEntityBasicBlock.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@
import io.netty.buffer.ByteBuf;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import mekanism.api.Coord4D;
import mekanism.common.ITileComponent;
Expand All @@ -33,10 +31,10 @@ public abstract class TileEntityBasicBlock extends TileEntity implements IWrench

public int clientFacing;

public Set<EntityPlayer> openedThisTick = Collections.synchronizedSet(new HashSet<EntityPlayer>());
public HashSet<EntityPlayer> openedThisTick = new HashSet<EntityPlayer>();

/** The players currently using this block. */
public Set<EntityPlayer> playersUsing = Collections.synchronizedSet(new HashSet<EntityPlayer>());
public HashSet<EntityPlayer> playersUsing = new HashSet<EntityPlayer>();

/** A timer used to send packets to clients. */
public int ticker;
Expand Down
12 changes: 3 additions & 9 deletions src/main/java/mekanism/common/tile/TileEntityBin.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import mekanism.common.IActiveState;
import mekanism.common.ILogisticalTransporter;
import mekanism.common.Mekanism;
import mekanism.common.PacketHandler;
import mekanism.common.item.ItemBlockBasic;
import mekanism.common.network.PacketTileEntity.TileEntityMessage;
import mekanism.common.transporter.TransporterManager;
Expand All @@ -25,7 +26,6 @@
import net.minecraft.tileentity.TileEntity;
import net.minecraftforge.common.util.ForgeDirection;
import cpw.mods.fml.common.Optional.Interface;

import powercrystals.minefactoryreloaded.api.IDeepStorageUnit;

@Interface(iface = "powercrystals.minefactoryreloaded.api.IDeepStorageUnit", modid = "MineFactoryReloaded")
Expand Down Expand Up @@ -95,11 +95,6 @@ public boolean isValid(ItemStack stack)
return false;
}

if(stack.isItemStackDamageable() && stack.isItemDamaged())
{
return false;
}

if(stack.getItem() instanceof ItemBlockBasic && stack.getItemDamage() == 6)
{
return false;
Expand Down Expand Up @@ -273,8 +268,7 @@ public ArrayList getNetworkedData(ArrayList data)

if(getItemCount() > 0)
{
data.add(MekanismUtils.getID(itemType));
data.add(itemType.getItemDamage());
data.add(itemType);
}

return data;
Expand All @@ -290,7 +284,7 @@ public void handlePacketData(ByteBuf dataStream)

if(clientAmount > 0)
{
itemType = new ItemStack(Item.getItemById(dataStream.readInt()), 1, dataStream.readInt());
itemType = PacketHandler.readStack(dataStream);
}
else {
itemType = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ public void onUpdate()

if(getActive())
{
for(EntityPlayer player : playersUsing)
for(EntityPlayer player : (HashSet<EntityPlayer>)playersUsing.clone())
{
if(player.openContainer instanceof ContainerNull || player.openContainer instanceof ContainerFilter)
{
Expand Down

0 comments on commit 1221549

Please sign in to comment.