diff --git a/src/mantle/blocks/abstracts/AdaptiveInventoryLogic.java b/src/mantle/blocks/abstracts/AdaptiveInventoryLogic.java new file mode 100644 index 0000000..311146c --- /dev/null +++ b/src/mantle/blocks/abstracts/AdaptiveInventoryLogic.java @@ -0,0 +1,102 @@ +package mantle.blocks.abstracts; + +import java.util.Random; + +import net.minecraft.entity.item.EntityItem; +import net.minecraft.item.ItemStack; +import net.minecraft.nbt.NBTTagCompound; +import mantle.blocks.iface.IFacingLogic; +import mantle.blocks.abstracts.InventoryLogic; + +public abstract class AdaptiveInventoryLogic extends InventoryLogic implements IFacingLogic +{ + Random random = new Random(); + protected int inventorySize; + + public AdaptiveInventoryLogic() + { + super(0); + } + + protected void adjustInventory (int size, boolean forceAdjust) + { + if (size != inventorySize || forceAdjust) + { + inventorySize = size; + + ItemStack[] tempInv = inventory; + inventory = new ItemStack[size]; + int invLength = tempInv.length > inventory.length ? inventory.length : tempInv.length; + System.arraycopy(tempInv, 0, inventory, 0, invLength); + + if (tempInv.length > inventory.length) + { + for (int i = inventory.length; i < tempInv.length; i++) + { + ItemStack stack = tempInv[i]; + if (stack != null) + { + float jumpX = random.nextFloat() * 0.8F + 0.1F; + float jumpY = random.nextFloat() * 0.8F + 0.1F; + float jumpZ = random.nextFloat() * 0.8F + 0.1F; + + int offsetX = 0; + int offsetY = 0; + int offsetZ = 0; + switch (getTossDirection()) + { + case 0: // -y + offsetY--; + break; + case 1: // +y + offsetY++; + break; + case 2: // +z + offsetZ--; + break; + case 3: // -z + offsetZ++; + break; + case 4: // +x + offsetX--; + break; + case 5: // -x + offsetX++; + break; + } + + while (stack.stackSize > 0) + { + int itemSize = random.nextInt(21) + 10; + + if (itemSize > stack.stackSize) + { + itemSize = stack.stackSize; + } + + stack.stackSize -= itemSize; + EntityItem entityitem = new EntityItem(worldObj, (double) ((float) xCoord + jumpX + offsetX), (double) ((float) yCoord + jumpY), + (double) ((float) zCoord + jumpZ + offsetZ), new ItemStack(stack.itemID, itemSize, stack.getItemDamage())); + + if (stack.hasTagCompound()) + { + entityitem.getEntityItem().setTagCompound((NBTTagCompound) stack.getTagCompound().copy()); + } + + float offset = 0.05F; + entityitem.motionX = (double) ((float) random.nextGaussian() * offset); + entityitem.motionY = (double) ((float) random.nextGaussian() * offset + 0.2F); + entityitem.motionZ = (double) ((float) random.nextGaussian() * offset); + worldObj.spawnEntityInWorld(entityitem); + } + } + } + } + } + } + + public int getTossDirection () + { + return getRenderDirection(); + } +} diff --git a/src/mantle/blocks/abstracts/ExpandableInventoryLogic.java b/src/mantle/blocks/abstracts/ExpandableInventoryLogic.java new file mode 100644 index 0000000..e1f79a6 --- /dev/null +++ b/src/mantle/blocks/abstracts/ExpandableInventoryLogic.java @@ -0,0 +1,218 @@ +package mantle.blocks.abstracts; + +import java.util.*; +import net.minecraft.entity.player.*; +import net.minecraft.inventory.*; +import net.minecraft.item.ItemStack; +import net.minecraft.nbt.*; +import net.minecraft.world.World; +import mantle.blocks.abstracts.InventoryLogic; + +public abstract class ExpandableInventoryLogic extends InventoryLogic implements IInventory +{ + + public ExpandableInventoryLogic() + { + super(0); + } + + protected ArrayList inventory = new ArrayList(); + protected String invName; + + @Override + public ItemStack getStackInSlot (int slot) + { + return slot < inventory.size() ? inventory.get(slot) : null; + } + + public boolean isStackInSlot (int slot) + { + return slot < inventory.size() && inventory.get(slot) != null; + } + + @Override + public int getSizeInventory () + { + return inventory.size(); + } + + public int getMaxSize () + { + return 64; + } + + @Override + public int getInventoryStackLimit () + { + return 64; + } + + public boolean canDropInventorySlot (int slot) + { + return true; + } + + @Override + public void setInventorySlotContents (int slot, ItemStack itemstack) + { + if (slot < inventory.size()) + { + inventory.set(slot, itemstack); + } + else if (slot == inventory.size()) + { + inventory.add(itemstack); + } + else if (slot < getMaxSize()) + { + inventory.ensureCapacity(slot); + inventory.set(slot, itemstack); + } + else + { + return; + } + if (itemstack != null && itemstack.stackSize > getInventoryStackLimit()) + { + itemstack.stackSize = getInventoryStackLimit(); + } + } + + @Override + public ItemStack decrStackSize (int slot, int quantity) + { + if (slot < inventory.size() && inventory.get(slot) != null) + { + if (inventory.get(slot).stackSize <= quantity) + { + ItemStack stack = inventory.get(slot); + inventory.set(slot, null); + return stack; + } + ItemStack split = inventory.get(slot).splitStack(quantity); + if (inventory.get(slot).stackSize == 0) + { + inventory.set(slot, null); + } + return split; + } + else + { + return null; + } + } + + /* Supporting methods */ + @Override + public boolean isUseableByPlayer (EntityPlayer entityplayer) + { + if (worldObj.getBlockTileEntity(xCoord, yCoord, zCoord) != this) + return false; + + else + return entityplayer.getDistance((double) xCoord + 0.5D, (double) yCoord + 0.5D, (double) zCoord + 0.5D) <= 64D; + + } + + public abstract Container getGuiContainer (InventoryPlayer inventoryplayer, World world, int x, int y, int z); + + /* NBT */ + @Override + public void readFromNBT (NBTTagCompound tags) + { + super.readFromNBT(tags); + this.invName = tags.getString("InvName"); + NBTTagList nbttaglist = tags.getTagList("Items"); + inventory = new ArrayList(); + inventory.ensureCapacity(nbttaglist.tagCount() > getMaxSize() ? getMaxSize() : nbttaglist.tagCount()); + for (int iter = 0; iter < nbttaglist.tagCount(); iter++) + { + NBTTagCompound tagList = (NBTTagCompound) nbttaglist.tagAt(iter); + byte slotID = tagList.getByte("Slot"); + if (slotID >= 0 && slotID < inventory.size()) + { + inventory.set(slotID, ItemStack.loadItemStackFromNBT(tagList)); + } + } + } + + @Override + public void writeToNBT (NBTTagCompound tags) + { + super.writeToNBT(tags); + if (invName != null) + tags.setString("InvName", invName); + NBTTagList nbttaglist = new NBTTagList(); + for (int iter = 0; iter < inventory.size(); iter++) + { + if (inventory.get(iter) != null) + { + NBTTagCompound tagList = new NBTTagCompound(); + tagList.setByte("Slot", (byte) iter); + inventory.get(iter).writeToNBT(tagList); + nbttaglist.appendTag(tagList); + } + } + + tags.setTag("Items", nbttaglist); + } + + /* Default implementations of hardly used methods */ + public ItemStack getStackInSlotOnClosing (int slot) + { + return null; + } + + public void openChest () + { + } + + public void closeChest () + { + } + + protected abstract String getDefaultName (); + + public void setInvName (String name) + { + this.invName = name; + } + + public String getInvName () + { + return this.isInvNameLocalized() ? this.invName : getDefaultName(); + } + + public boolean isInvNameLocalized () + { + return this.invName != null && this.invName.length() > 0; + } + + public void cleanInventory () + { + Iterator i1 = inventory.iterator(); + while (i1.hasNext()) + { + if (i1.next() == null) + { + i1.remove(); + } + } + } + + @Override + public boolean isItemValidForSlot (int slot, ItemStack itemstack) + { + if (slot < getSizeInventory()) + { + if (inventory.get(slot) == null || itemstack.stackSize + inventory.get(slot).stackSize <= getInventoryStackLimit()) + return true; + } + else + { + return slot < getMaxSize(); + } + return false; + } + +} diff --git a/src/mantle/blocks/iface/IActiveLogic.java b/src/mantle/blocks/iface/IActiveLogic.java index 8785e8a..c670f8d 100644 --- a/src/mantle/blocks/iface/IActiveLogic.java +++ b/src/mantle/blocks/iface/IActiveLogic.java @@ -4,7 +4,6 @@ package mantle.blocks.iface; * Marks blocks that can be active and inactive. * * @author mDiyo - * @author Sunstrike */ public interface IActiveLogic { diff --git a/src/mantle/blocks/iface/IMasterLogic.java b/src/mantle/blocks/iface/IMasterLogic.java new file mode 100644 index 0000000..8788ed3 --- /dev/null +++ b/src/mantle/blocks/iface/IMasterLogic.java @@ -0,0 +1,12 @@ +package mantle.blocks.iface; + +public interface IMasterLogic +{ + /** Called when servants change their state + * + * @param x Servant X + * @param y Servant Y + * @param z Servant Z + */ + public void notifyChange (IServantLogic servant, int x, int y, int z); +} diff --git a/src/mantle/blocks/iface/IMasterNode.java b/src/mantle/blocks/iface/IMasterNode.java new file mode 100644 index 0000000..1906f92 --- /dev/null +++ b/src/mantle/blocks/iface/IMasterNode.java @@ -0,0 +1,8 @@ +package mantle.blocks.iface; + +public interface IMasterNode extends IMasterLogic, IServantLogic +{ + public boolean isCurrentlyMaster (); + + public boolean isEquivalentMaster (IMasterLogic master); +} diff --git a/src/mantle/blocks/iface/IServantLogic.java b/src/mantle/blocks/iface/IServantLogic.java new file mode 100644 index 0000000..edd2335 --- /dev/null +++ b/src/mantle/blocks/iface/IServantLogic.java @@ -0,0 +1,44 @@ +package mantle.blocks.iface; + +import mantle.world.CoordTuple; +import net.minecraft.world.World; + +public interface IServantLogic +{ + public CoordTuple getMasterPosition (); + + /** The block should already have a valid master */ + public void notifyMasterOfChange (); + + /** Checks if this block can be tied to this master + * + * @param master + * @param x xCoord of master + * @param y yCoord of master + * @param z zCoord of master + * @return whether the servant can be tied to this master + */ + + public boolean setPotentialMaster (IMasterLogic master, World world, int xMaster, int yMaster, int zMaster); + + /** Used to set and verify that this is the block's master + * + * @param master + * @param x xCoord of master + * @param y yCoord of master + * @param z zCoord of master + * @return Is this block tied to this master? + */ + + public boolean verifyMaster (IMasterLogic master, World world, int xMaster, int yMaster, int zMaster); + + /** Exactly what it says on the tin + * + * @param master + * @param x xCoord of master + * @param y yCoord of master + * @param z zCoord of master + */ + + public void invalidateMaster (IMasterLogic master, World world, int xMaster, int yMaster, int zMaster); +}