diff --git a/src/main/java/magicbees/main/Config.java b/src/main/java/magicbees/main/Config.java index 105cf86..7b0feb2 100644 --- a/src/main/java/magicbees/main/Config.java +++ b/src/main/java/magicbees/main/Config.java @@ -240,6 +240,7 @@ public class Config { setupEffectJar(); setupHives(); + setupApiary(); } public void setupItems() @@ -388,6 +389,12 @@ public class Config GameRegistry.registerBlock(effectJar, "effectJar"); GameRegistry.registerTileEntity(TileEntityEffectJar.class, TileEntityEffectJar.tileEntityName); } + + private void setupApiary() { + thaumicApiary = new BlockThaumicApiary(); + GameRegistry.registerBlock(thaumicApiary, "thaumicApiary"); + GameRegistry.registerTileEntity(TileEntityThaumicApiary.class, TileEntityThaumicApiary.tileEntityName); + } private void setupFrames() { hiveFrameMagic = new ItemMagicHiveFrame(HiveFrameType.MAGIC); @@ -493,12 +500,8 @@ public class Config if (ThaumcraftHelper.isActive()) { // Items - solidFlux = new ItemCrystalAspect(); - - // Blocks - thaumicApiary = new BlockThaumicApiary(); - GameRegistry.registerBlock(thaumicApiary, "thaumicApiary"); - GameRegistry.registerTileEntity(TileEntityThaumicApiary.class, TileEntityThaumicApiary.tileEntityName); + //solidFlux replaced by Thaumcraft's crystalized essentia. + //solidFlux = new ItemCrystalAspect(); } } diff --git a/src/main/java/magicbees/main/utils/ItemStackUtils.java b/src/main/java/magicbees/main/utils/ItemStackUtils.java new file mode 100644 index 0000000..4cc9560 --- /dev/null +++ b/src/main/java/magicbees/main/utils/ItemStackUtils.java @@ -0,0 +1,103 @@ +package magicbees.main.utils; + +import net.minecraft.inventory.IInventory; +import net.minecraft.item.ItemStack; + +public class ItemStackUtils { + + /** + * Attempts to add all items to the inventory, starting at startSlot and considering slotCount item slots. + * @return Number of items successfully added to the inventory. + */ + public static int tryAddItemStacksToInvenotry(IInventory inventory, ItemStack[] items, int startSlot, int slotCount) { + int itemsAdded = 0; + + for (ItemStack item : items) { + itemsAdded += addItemToInventory(inventory, item, startSlot, slotCount); + } + + return itemsAdded; + } + + /** + * Attempts to add all items to the inventory, starting at startSlot and considering slotCount item slots. + * @return true if and only if all items were successfully added to the inventory. + */ + public static boolean tryAddAllItemStacksToInventory(IInventory inventory, ItemStack[] items, int startSlot, int slotCount) { + int itemsToAdd = 0; + int itemsAdded = 0; + + for (ItemStack item : items) { + itemsToAdd += item.stackSize; + itemsAdded += addItemToInventory(inventory, item, startSlot, slotCount); + } + + return itemsToAdd == itemsAdded; + } + + /** + * Attempts to add an item to an inventory, starting at startSlot and considering slotCount item slots. + * @return number of items in the stack successfully added to the inventory. + */ + public static int addItemToInventory(IInventory inventory, ItemStack item, int startSlot, int slotCount) { + int itemsAdded = 0; + + itemsAdded = addToExistingSlot(inventory, item, startSlot, slotCount); + if (itemsAdded < item.stackSize) { + item.stackSize -= itemsAdded; + itemsAdded += addToEmptySlot(inventory, item, startSlot, slotCount); + } + + return itemsAdded; + } + + private static int addToExistingSlot(IInventory inventory, ItemStack item, int startSlot, int slotCount) { + int itemsAdded = 0; + + for (int slotIndex = startSlot; slotIndex < (startSlot + slotCount); ++slotIndex) { + ItemStack slotStack = inventory.getStackInSlot(slotIndex); + + // Skip empty slots. + if (slotStack != null && areItemsEqualNBT(item, slotStack)) { + int spaceAvailable = slotStack.getMaxStackSize() - slotStack.stackSize; + + if (spaceAvailable > item.stackSize) { + slotStack.stackSize += item.stackSize; + inventory.setInventorySlotContents(slotIndex, slotStack); + itemsAdded += item.stackSize; + item.stackSize = 0; + } + else if (spaceAvailable > 0) { + slotStack.stackSize = slotStack.getMaxStackSize(); + inventory.setInventorySlotContents(slotIndex, slotStack); + itemsAdded += spaceAvailable; + item.stackSize -= spaceAvailable; + } + } + + // Don't add beyond what we have. + if (item.stackSize == 0) { + break; + } + } + + return itemsAdded; + } + + private static int addToEmptySlot(IInventory inventory, ItemStack item, int startSlot, int slotCount) { + for (int slotIndex = startSlot; slotIndex < (startSlot + slotCount); ++slotIndex) { + ItemStack slotStack = inventory.getStackInSlot(slotIndex); + + if (slotStack == null) { + inventory.setInventorySlotContents(slotIndex, item); + return item.stackSize; + } + } + + return 0; + } + + public static boolean areItemsEqualNBT(ItemStack item, ItemStack otherItem) { + return item.isItemEqual(otherItem) && ItemStack.areItemStackTagsEqual(item, otherItem); + } +} diff --git a/src/main/java/magicbees/reference/Names.java b/src/main/java/magicbees/reference/Names.java deleted file mode 100644 index 19b1a74..0000000 --- a/src/main/java/magicbees/reference/Names.java +++ /dev/null @@ -1,13 +0,0 @@ -package magicbees.reference; - -public class Names { - public static final class Blocks - { - public static final String THAUMIC_APIARY = "thaumicApiary"; - } - - public static final class Containers - { - public static final String THAUMIC_APIARY = "container.mb." + Blocks.THAUMIC_APIARY; - } -} diff --git a/src/main/java/magicbees/tileentity/TileEntityThaumicApiary.java b/src/main/java/magicbees/tileentity/TileEntityThaumicApiary.java index 18144c3..74df2cc 100644 --- a/src/main/java/magicbees/tileentity/TileEntityThaumicApiary.java +++ b/src/main/java/magicbees/tileentity/TileEntityThaumicApiary.java @@ -3,8 +3,7 @@ package magicbees.tileentity; import magicbees.bees.BeeManager; import magicbees.main.CommonProxy; import magicbees.main.MagicBees; -import magicbees.main.utils.LogHelper; -import magicbees.reference.Names; +import magicbees.main.utils.ItemStackUtils; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.inventory.Container; import net.minecraft.inventory.ICrafting; @@ -29,41 +28,39 @@ import forestry.api.core.EnumTemperature; import forestry.api.core.ErrorStateRegistry; import forestry.api.core.IErrorState; import forestry.api.genetics.IIndividual; -import forestry.core.inventory.InvTools; -import forestry.core.inventory.InventoryAdapter; import forestry.core.utils.Utils; public class TileEntityThaumicApiary extends TileEntity implements ISidedInventory, IBeeHousing { public static final String tileEntityName = CommonProxy.DOMAIN + ".thaumicApiary"; - private GameProfile ownerName; + private GameProfile ownerProfile; // Constants private static final int SLOT_QUEEN = 0; private static final int SLOT_DRONE = 1; private static final int SLOT_FRAME_START = 2; private static final int SLOT_FRAME_COUNT = 3; - private static final int SLOT_INVENTORY_START = 5; - private static final int SLOT_INVENTORY_COUNT = 7; - - + private static final int SLOT_PRODUCTS_START = 5; + private static final int SLOT_PRODUCTS_COUNT = 7; + private final IBeekeepingLogic logic; private BiomeGenBase biome; private int displayHealthMax = 0; private int displayHealth = 0; private boolean init = false; + + private IErrorState errorState = ErrorStateRegistry.getErrorState("ok"); private ItemStack[] items; - protected final InventoryAdapter inventory = new InventoryAdapter(12, "Items"); - - public TileEntityThaumicApiary() - { + public TileEntityThaumicApiary(){ items = new ItemStack[12]; logic = BeeManager.beeRoot.createBeekeepingLogic(this); } - public void setOwner(EntityPlayer player) { this.ownerName = player.getGameProfile(); } + public void setOwner(EntityPlayer player) { + this.ownerProfile = player.getGameProfile(); + } @Override public ItemStack getQueen() { @@ -94,7 +91,6 @@ public class TileEntityThaumicApiary extends TileEntity implements ISidedInvento public void onQueenChange(ItemStack queenStack) { if (!worldObj.isRemote) { MagicBees.object.netHandler.sendInventoryUpdate(this, SLOT_QUEEN, queenStack); - this.markDirty(); } } @@ -135,14 +131,17 @@ public class TileEntityThaumicApiary extends TileEntity implements ISidedInvento return false; } + private boolean isItemStackFrame(int i) { + return getStackInSlot(i) != null && getStackInSlot(i).getItem() instanceof IHiveFrame; + } + @Override public float getTerritoryModifier(IBeeGenome genome, float currentModifier) { float mod = 1.0f; - for (int i = SLOT_FRAME_START; i < SLOT_FRAME_START + SLOT_FRAME_COUNT; i++){ - if (getStackInSlot(i) == null) - continue; - if (getStackInSlot(i).getItem() instanceof IHiveFrame) - mod *= ((IHiveFrame) getStackInSlot(i).getItem()).getTerritoryModifier(genome, mod); + for (int slotIndex = SLOT_FRAME_START; slotIndex < SLOT_FRAME_START + SLOT_FRAME_COUNT; slotIndex++) { + if (isItemStackFrame(slotIndex)) { + mod *= ((IHiveFrame) getStackInSlot(slotIndex).getItem()).getTerritoryModifier(genome, mod); + } } return mod; } @@ -150,11 +149,10 @@ public class TileEntityThaumicApiary extends TileEntity implements ISidedInvento @Override public float getMutationModifier(IBeeGenome genome, IBeeGenome mate, float currentModifier) { float mod = 1.0f; - for (int i = SLOT_FRAME_START; i < SLOT_FRAME_START + SLOT_FRAME_COUNT; i++){ - if (getStackInSlot(i) == null) - continue; - if (getStackInSlot(i).getItem() instanceof IHiveFrame) - mod *= ((IHiveFrame) getStackInSlot(i).getItem()).getMutationModifier(genome, mate, mod); + for (int slotIndex = SLOT_FRAME_START; slotIndex < SLOT_FRAME_START + SLOT_FRAME_COUNT; slotIndex++) { + if (isItemStackFrame(slotIndex)) { + mod *= ((IHiveFrame) getStackInSlot(slotIndex).getItem()).getMutationModifier(genome, mate, mod); + } } if (this.isMutationBoosted()) { mod = mod * 2f; @@ -165,11 +163,10 @@ public class TileEntityThaumicApiary extends TileEntity implements ISidedInvento @Override public float getLifespanModifier(IBeeGenome genome, IBeeGenome mate, float currentModifier) { float mod = 1.0f; - for (int i = SLOT_FRAME_START; i < SLOT_FRAME_START + SLOT_FRAME_COUNT; i++){ - if (getStackInSlot(i) == null) - continue; - if (getStackInSlot(i).getItem() instanceof IHiveFrame) - mod *= ((IHiveFrame) getStackInSlot(i).getItem()).getLifespanModifier(genome, mate, mod); + for (int slotIndex = SLOT_FRAME_START; slotIndex < SLOT_FRAME_START + SLOT_FRAME_COUNT; slotIndex++) { + if (isItemStackFrame(slotIndex)) { + mod *= ((IHiveFrame) getStackInSlot(slotIndex).getItem()).getLifespanModifier(genome, mate, mod); + } } if (this.isDeathRateBoosted()) { mod = mod / 2f; @@ -179,12 +176,11 @@ public class TileEntityThaumicApiary extends TileEntity implements ISidedInvento @Override public float getProductionModifier(IBeeGenome genome, float currentModifier) { - float mod = 1.0f; - for (int i = SLOT_FRAME_START; i < SLOT_FRAME_START + SLOT_FRAME_COUNT; i++){ - if (getStackInSlot(i) == null) - continue; - if (getStackInSlot(i).getItem() instanceof IHiveFrame) - mod *= ((IHiveFrame) getStackInSlot(i).getItem()).getProductionModifier(genome, mod); + float mod = 0.9f; + for (int slotIndex = SLOT_FRAME_START; slotIndex < SLOT_FRAME_START + SLOT_FRAME_COUNT; slotIndex++){ + if (isItemStackFrame(slotIndex)) { + mod *= ((IHiveFrame) getStackInSlot(slotIndex).getItem()).getProductionModifier(genome, mod); + } } if (this.isWorkBoosted()) { mod = mod * 2f; @@ -195,23 +191,21 @@ public class TileEntityThaumicApiary extends TileEntity implements ISidedInvento @Override public float getFloweringModifier(IBeeGenome genome, float currentModifier) { float mod = 1.0f; - for (int i = SLOT_FRAME_START; i < SLOT_FRAME_START + SLOT_FRAME_COUNT; i++){ - if (getStackInSlot(i) == null) - continue; - if (getStackInSlot(i).getItem() instanceof IHiveFrame) - mod *= ((IHiveFrame) getStackInSlot(i).getItem()).getFloweringModifier(genome, mod); + for (int slotIndex = SLOT_FRAME_START; slotIndex < SLOT_FRAME_START + SLOT_FRAME_COUNT; slotIndex++) { + if (isItemStackFrame(slotIndex)) { + mod *= ((IHiveFrame) getStackInSlot(slotIndex).getItem()).getFloweringModifier(genome, mod); + } } return mod; } @Override public float getGeneticDecay(IBeeGenome genome, float currentModifier) { - float mod = 1.0f; - for (int i = SLOT_FRAME_START; i < SLOT_FRAME_START + SLOT_FRAME_COUNT; i++){ - if (getStackInSlot(i) == null) - continue; - if (getStackInSlot(i).getItem() instanceof IHiveFrame) - mod *= ((IHiveFrame) getStackInSlot(i).getItem()).getGeneticDecay(genome, mod); + float mod = 0.8f; + for (int slotIndex = SLOT_FRAME_START; slotIndex < SLOT_FRAME_START + SLOT_FRAME_COUNT; slotIndex++) { + if (isItemStackFrame(slotIndex)) { + mod *= ((IHiveFrame) getStackInSlot(slotIndex).getItem()).getGeneticDecay(genome, mod); + } } return mod; } @@ -238,7 +232,7 @@ public class TileEntityThaumicApiary extends TileEntity implements ISidedInvento @Override public GameProfile getOwnerName() { - return this.ownerName; + return this.ownerProfile; } @Override @@ -283,33 +277,34 @@ public class TileEntityThaumicApiary extends TileEntity implements ISidedInvento @Override public void setErrorState(int state) { - + this.errorState = ErrorStateRegistry.getErrorState((short) state); } @Override public void setErrorState(IErrorState state) { - + this.errorState = state; } @Override public int getErrorOrdinal() { - return 0; + return this.errorState.getID(); } @Override public IErrorState getErrorState() { - return null; + return this.errorState; } - public InventoryAdapter getInternalInventory() { - return inventory; - } - - // TODO: Implement own version of this @Override public boolean addProduct(ItemStack product, boolean all) { - LogHelper.info("Trying to add " + product.getDisplayName() + " to thaumicApiary"); - return InvTools.tryAddStack(getInternalInventory(), product, SLOT_INVENTORY_START, SLOT_INVENTORY_COUNT, all, true); + int countAdded = ItemStackUtils.addItemToInventory(this, product, SLOT_PRODUCTS_START, SLOT_PRODUCTS_COUNT); + + if (all) { + return countAdded == product.stackSize; + } + else { + return countAdded > 0; + } } @Override @@ -358,7 +353,7 @@ public class TileEntityThaumicApiary extends TileEntity implements ISidedInvento @Override public String getInventoryName() { - return Names.Containers.THAUMIC_APIARY; + return tileEntityName; } @Override @@ -532,10 +527,10 @@ public class TileEntityThaumicApiary extends TileEntity implements ISidedInvento } if (worldObj.isRemote) { - updateServerSide(); + updateClientSide(); } else { - updateClientSide(); + updateServerSide(); } }