Skip to content

Commit

Permalink
Attempt to fix issues with structure inventories.
Browse files Browse the repository at this point in the history
Also cleaned up the Hatch pipe/inventory interaction code, modernizing it and removing duplicate code.
This involved adding my AdjecentInventoryCache and AdjecentTileCache code from Railcraft for greatly improved performance.
ISpecialInventory is now 100% unused.
  • Loading branch information
CovertJaguar committed Dec 16, 2014
1 parent c7d88d7 commit 89c3fb8
Show file tree
Hide file tree
Showing 17 changed files with 534 additions and 238 deletions.
2 changes: 1 addition & 1 deletion api
9 changes: 7 additions & 2 deletions src/main/java/forestry/apiculture/gadgets/TileAlveary.java
Expand Up @@ -26,6 +26,7 @@
import java.util.Collection;
import java.util.LinkedList;
import net.minecraft.inventory.IInventory;
import net.minecraft.inventory.ISidedInventory;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
import net.minecraftforge.common.util.ForgeDirection;
Expand Down Expand Up @@ -125,7 +126,6 @@ public PacketPayload getPacketPayload() {
}

/* ITILESTRUCTURE */

@Override
public String getTypeUID() {
return structureLogic.getTypeUID();
Expand Down Expand Up @@ -208,7 +208,12 @@ public boolean isIntegratedIntoStructure() {
}

@Override
public IInventory getInventory() {
public final IInventory getInventory() {
return getStructureInventory();
}

@Override
public ISidedInventory getStructureInventory() {
return getInternalInventory();
}

Expand Down
Expand Up @@ -134,9 +134,8 @@ protected void updateServerSide() {
TileInventoryAdapter canInventory = getInternalInventory();

// Check if we have suitable items waiting in the item slot
if (canInventory.getStackInSlot(0) != null) {
if (canInventory.getStackInSlot(0) != null)
FluidHelper.drainContainers(tankManager, canInventory, 0);
}

}

Expand Down Expand Up @@ -179,11 +178,6 @@ public int getIcon(int side, int metadata) {
return BlockAlveary.ALVEARY_HYGRO;
}

@Override
public IInventory getInventory() {
return getInternalInventory();
}

/* IINVENTORY */
@Override
public int getSizeInventory() {
Expand Down
180 changes: 89 additions & 91 deletions src/main/java/forestry/apiculture/gadgets/TileAlvearyPlain.java
Expand Up @@ -22,14 +22,12 @@
import forestry.api.core.EnumTemperature;
import forestry.api.core.ForestryAPI;
import forestry.api.core.IErrorState;
import forestry.api.core.ISpecialInventory;
import forestry.api.core.ITileStructure;
import forestry.api.genetics.IIndividual;
import forestry.apiculture.gui.ContainerAlveary;
import forestry.core.EnumErrorCode;
import forestry.core.config.Config;
import forestry.core.config.Defaults;
import forestry.core.config.ForestryItem;
import forestry.core.interfaces.IClimatised;
import forestry.core.interfaces.IErrorSource;
import forestry.core.interfaces.IHintSource;
Expand All @@ -50,9 +48,8 @@
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.world.World;
import net.minecraft.world.biome.BiomeGenBase;
import net.minecraftforge.common.util.ForgeDirection;

public class TileAlvearyPlain extends TileAlveary implements ISidedInventory, ISpecialInventory, IBeeHousing, IClimatised, IHintSource {
public class TileAlvearyPlain extends TileAlveary implements ISidedInventory, IBeeHousing, IClimatised, IHintSource {

// / CONSTANTS
public static final int SLOT_QUEEN = 0;
Expand Down Expand Up @@ -144,7 +141,7 @@ protected void updateServerSide() {

// Add swarm effects
if (worldObj.getTotalWorldTime() % 200 * 10 == 0)
onQueenChange(getInternalInventory().getStackInSlot(SLOT_QUEEN));
onQueenChange(getStructureInventory().getStackInSlot(SLOT_QUEEN));
if (getErrorState() == EnumErrorCode.OK)
queen.doFX(beekeepingLogic.getEffectData(), this);

Expand All @@ -169,13 +166,13 @@ protected void updateClientSide() {
if (!isMaster())
return;

if (getInternalInventory() == null)
if (getStructureInventory() == null)
return;

// / Multiplayer FX
if (PluginApiculture.beeInterface.isMated(getInternalInventory().getStackInSlot(SLOT_QUEEN)))
if (PluginApiculture.beeInterface.isMated(getStructureInventory().getStackInSlot(SLOT_QUEEN)))
if (getErrorState() == EnumErrorCode.OK && worldObj.getTotalWorldTime() % 2 == 0) {
IBee displayQueen = PluginApiculture.beeInterface.getMember(getInternalInventory().getStackInSlot(SLOT_QUEEN));
IBee displayQueen = PluginApiculture.beeInterface.getMember(getStructureInventory().getStackInSlot(SLOT_QUEEN));
displayQueen.doFX(beekeepingLogic.getEffectData(), this);
}
}
Expand All @@ -200,7 +197,7 @@ private void equalizeHumidity() {

/* STATE INFORMATION */
private int getHealthDisplay() {
IInventory inventory = getInternalInventory();
IInventory inventory = getStructureInventory();
if (inventory == null || inventory.getStackInSlot(SLOT_QUEEN) == null)
return 0;

Expand All @@ -213,7 +210,7 @@ else if (!PluginApiculture.beeInterface.isDrone(inventory.getStackInSlot(SLOT_QU
}

private int getMaxHealthDisplay() {
IInventory inventory = getInternalInventory();
IInventory inventory = getStructureInventory();
if (inventory == null || inventory.getStackInSlot(SLOT_QUEEN) == null)
return 0;

Expand Down Expand Up @@ -431,7 +428,7 @@ public boolean canBreed() {

@Override
public boolean addProduct(ItemStack product, boolean all) {
TileInventoryAdapter inventory = getInternalInventory();
TileInventoryAdapter inventory = getStructureInventory();
if (inventory == null)
return false;

Expand Down Expand Up @@ -530,17 +527,17 @@ public boolean isHellish() {
}

/* IINVENTORY */
private IInventory getStructureInventory() {
@Override
public TileInventoryAdapter getStructureInventory() {
TileInventoryAdapter inventory = getInternalInventory();
if (inventory != null) {
if (isMaster() || !Proxies.common.isSimulating(worldObj))
return inventory;
} else if (hasMaster()) {
ITileStructure central = getCentralTE();
if (central != null)
return central.getInventory();
return (TileInventoryAdapter) central.getInventory();
}

return null;
}

Expand Down Expand Up @@ -582,12 +579,13 @@ public ItemStack getStackInSlotOnClosing(int slotIndex) {

@Override
public void setInventorySlotContents(int slotIndex, ItemStack itemstack) {
TileInventoryAdapter inventory = getInternalInventory();
IInventory inv = getStructureInventory();

// Client side handling for container synch
if (inventory == null && !Proxies.common.isSimulating(worldObj))
if (inv == null && !Proxies.common.isSimulating(worldObj)) {
createInventory();

IInventory inv = getStructureInventory();
inv = getInternalInventory();
}
if (inv != null)
inv.setInventorySlotContents(slotIndex, itemstack);
}
Expand Down Expand Up @@ -626,7 +624,7 @@ public boolean hasCustomInventoryName() {

@Override
public boolean isItemValidForSlot(int slotIndex, ItemStack itemstack) {
TileInventoryAdapter inventory = getInternalInventory();
ISidedInventory inventory = getStructureInventory();
if (inventory == null)
return false;

Expand All @@ -646,7 +644,7 @@ public boolean canInsertItem(int slotIndex, ItemStack itemstack, int side) {

@Override
public boolean canExtractItem(int slotIndex, ItemStack itemstack, int side) {
TileInventoryAdapter inventory = getInternalInventory();
ISidedInventory inventory = getStructureInventory();
if (inventory == null)
return false;
if (!inventory.canExtractItem(slotIndex, itemstack, side))
Expand All @@ -657,83 +655,83 @@ public boolean canExtractItem(int slotIndex, ItemStack itemstack, int side) {

@Override
public int[] getAccessibleSlotsFromSide(int side) {
TileInventoryAdapter inventory = getInternalInventory();
ISidedInventory inventory = getStructureInventory();
if (inventory == null)
return Defaults.FACINGS_NONE;
return inventory.getAccessibleSlotsFromSide(side);
}

/* ISPECIALINVENTORY */
@Override
public int addItem(ItemStack stack, boolean doAdd, ForgeDirection from) {

IInventory inv = getStructureInventory();
if (inv == null)
return 0;

// Princesses && Queens
if (ForestryItem.beePrincessGE.isItemEqual(stack) || ForestryItem.beeQueenGE.isItemEqual(stack))
if (inv.getStackInSlot(SLOT_QUEEN) == null) {
if (doAdd) {
inv.setInventorySlotContents(SLOT_QUEEN, stack.copy());
inv.getStackInSlot(SLOT_QUEEN).stackSize = 1;
}
return 1;
}

// Drones
if (ForestryItem.beeDroneGE.isItemEqual(stack)) {

ItemStack droneStack = inv.getStackInSlot(SLOT_DRONE);
if (droneStack == null) {
if (doAdd)
inv.setInventorySlotContents(SLOT_DRONE, stack.copy());
return stack.stackSize;
} else {
if (!droneStack.isItemEqual(stack))
return 0;
if (!ItemStack.areItemStackTagsEqual(droneStack, stack))
return 0;
int space = droneStack.getMaxStackSize() - droneStack.stackSize;
if (space <= 0)
return 0;

int added = space > stack.stackSize ? stack.stackSize : space;
if (doAdd)
droneStack.stackSize += added;
return added;
}
}

return 0;
}

@Override
public ItemStack[] extractItem(boolean doRemove, ForgeDirection from, int maxItemCount) {

IInventory inv = getStructureInventory();
if (inv == null)
return new ItemStack[0];

ItemStack product = null;

for (int i = SLOT_PRODUCT_1; i < inv.getSizeInventory(); i++) {
if (inv.getStackInSlot(i) == null)
continue;

ItemStack stack = inv.getStackInSlot(i);

if (doRemove)
product = inv.decrStackSize(i, 1);
else {
product = stack.copy();
product.stackSize = 1;
}
break;
}

return new ItemStack[]{product};
}
// @Override
// public int addItem(ItemStack stack, boolean doAdd, ForgeDirection from) {
//
// IInventory inv = getStructureInventory();
// if (inv == null)
// return 0;
//
// // Princesses && Queens
// if (ForestryItem.beePrincessGE.isItemEqual(stack) || ForestryItem.beeQueenGE.isItemEqual(stack))
// if (inv.getStackInSlot(SLOT_QUEEN) == null) {
// if (doAdd) {
// inv.setInventorySlotContents(SLOT_QUEEN, stack.copy());
// inv.getStackInSlot(SLOT_QUEEN).stackSize = 1;
// }
// return 1;
// }
//
// // Drones
// if (ForestryItem.beeDroneGE.isItemEqual(stack)) {
//
// ItemStack droneStack = inv.getStackInSlot(SLOT_DRONE);
// if (droneStack == null) {
// if (doAdd)
// inv.setInventorySlotContents(SLOT_DRONE, stack.copy());
// return stack.stackSize;
// } else {
// if (!droneStack.isItemEqual(stack))
// return 0;
// if (!ItemStack.areItemStackTagsEqual(droneStack, stack))
// return 0;
// int space = droneStack.getMaxStackSize() - droneStack.stackSize;
// if (space <= 0)
// return 0;
//
// int added = space > stack.stackSize ? stack.stackSize : space;
// if (doAdd)
// droneStack.stackSize += added;
// return added;
// }
// }
//
// return 0;
// }
//
// @Override
// public ItemStack[] extractItem(boolean doRemove, ForgeDirection from, int maxItemCount) {
//
// IInventory inv = getStructureInventory();
// if (inv == null)
// return new ItemStack[0];
//
// ItemStack product = null;
//
// for (int i = SLOT_PRODUCT_1; i < inv.getSizeInventory(); i++) {
// if (inv.getStackInSlot(i) == null)
// continue;
//
// ItemStack stack = inv.getStackInSlot(i);
//
// if (doRemove)
// product = inv.decrStackSize(i, 1);
// else {
// product = stack.copy();
// product.stackSize = 1;
// }
// break;
// }
//
// return new ItemStack[]{product};
// }

/* SMP GUI */
public void getGUINetworkData(int i, int j) {
Expand Down
Expand Up @@ -53,6 +53,7 @@ public boolean hasFunction() {
}

/* TEXTURES */
@Override
public int getIcon(int side, int metadata) {
if(side == 0 || side == 1)
return BlockAlveary.BOTTOM;
Expand Down
Expand Up @@ -32,7 +32,6 @@
import java.util.Map.Entry;
import java.util.Stack;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.IInventory;
import net.minecraft.inventory.ISidedInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
Expand Down Expand Up @@ -219,11 +218,6 @@ protected void createInventory() {
setInternalInventory(new TileInventoryAdapter(this, 4, "SwarmInv"));
}

@Override
public IInventory getInventory() {
return getInternalInventory();
}

/* IINVENTORY */
@Override
public int getSizeInventory() {
Expand Down
Expand Up @@ -130,6 +130,7 @@ public boolean canBlockStay(World world, int x, int y, int z) {

@Override
public void onNeighborBlockChange(World world, int x, int y, int z, Block neighbour) {
super.onNeighborBlockChange(world, x, y, z, neighbour);
if (Proxies.common.isSimulating(world) && !this.canBlockStay(world, x, y, z)) {
dropAsSapling(world, x, y, z);
world.setBlockToAir(x, y, z);
Expand Down

1 comment on commit 89c3fb8

@Parker8283
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

\o/ Burn it with fire then!

Please sign in to comment.