Fixed errors and turned crate into a scala class
This commit is contained in:
parent
1fe751f547
commit
fcce578fab
5 changed files with 303 additions and 400 deletions
|
@ -1,11 +1,8 @@
|
|||
package resonantinduction.archaic
|
||||
|
||||
import resonantinduction.archaic.firebox.BlockFirebox
|
||||
import archaic.firebox.BlockHotPlate
|
||||
import resonantinduction.archaic.fluid.gutter.TileGutter
|
||||
import net.minecraft.block.Block
|
||||
import net.minecraft.init.Blocks
|
||||
import net.minecraft.item.Item
|
||||
import net.minecraft.init.Items
|
||||
import net.minecraft.item.ItemStack
|
||||
import net.minecraftforge.oredict.OreDictionary
|
||||
|
@ -14,24 +11,23 @@ import resonant.content.loader.ModManager
|
|||
import resonant.lib.recipe.UniversalRecipe
|
||||
import resonantinduction.archaic.blocks.TileTurntable
|
||||
import resonantinduction.archaic.crate.BlockCrate
|
||||
import resonantinduction.archaic.crate.CrateCraftingHandler
|
||||
import resonantinduction.archaic.crate.CrateRecipe
|
||||
import resonantinduction.archaic.crate.ItemBlockCrate
|
||||
import resonantinduction.archaic.crate.TileCrate
|
||||
import archaic.engineering.ItemHammer
|
||||
import resonantinduction.archaic.engineering.ItemHammer
|
||||
import resonantinduction.archaic.engineering.TileEngineeringTable
|
||||
import archaic.filter.BlockImprinter
|
||||
import resonantinduction.archaic.filter.BlockImprinter
|
||||
import resonantinduction.archaic.filter.TileFilter
|
||||
import archaic.filter.TileImprinter
|
||||
import archaic.firebox.BlockHotPlate
|
||||
import archaic.firebox.TileFirebox
|
||||
import resonantinduction.archaic.filter.TileImprinter
|
||||
import resonantinduction.archaic.firebox.BlockHotPlate
|
||||
import resonantinduction.archaic.firebox.TileFirebox
|
||||
import resonantinduction.archaic.firebox.TileHotPlate
|
||||
import resonantinduction.archaic.fluid.grate.TileGrate
|
||||
import resonantinduction.archaic.fluid.tank.TileTank
|
||||
import archaic.process.BlockCastingMold
|
||||
import archaic.process.BlockMillstone
|
||||
import archaic.process.TileCastingMold
|
||||
import archaic.process.TileMillstone
|
||||
import resonantinduction.archaic.waila.Waila
|
||||
import resonantinduction.archaic.process.BlockCastingMold
|
||||
import resonantinduction.archaic.process.BlockMillstone
|
||||
import resonantinduction.archaic.process.TileCastingMold
|
||||
import resonantinduction.archaic.process.TileMillstone
|
||||
import resonantinduction.core.Reference
|
||||
import resonantinduction.core.ResonantInduction
|
||||
import resonantinduction.core.ResonantTab
|
||||
|
@ -49,8 +45,6 @@ import cpw.mods.fml.common.event.FMLPreInitializationEvent
|
|||
import cpw.mods.fml.common.network.NetworkRegistry
|
||||
import cpw.mods.fml.common.registry.GameRegistry
|
||||
import Archaic._
|
||||
//remove if not needed
|
||||
import scala.collection.JavaConversions._
|
||||
|
||||
object Archaic {
|
||||
|
||||
|
@ -82,7 +76,7 @@ class Archaic {
|
|||
ArchaicBlocks.blockCrate = contentRegistry.createBlock( classOf[ BlockCrate ], classOf[ ItemBlockCrate ], classOf[ TileCrate ] )
|
||||
ArchaicBlocks.blockImprinter = contentRegistry.createTile( classOf[ BlockImprinter ], classOf[ TileImprinter ] )
|
||||
ArchaicBlocks.blockTurntable = contentRegistry.newBlock( classOf[ TileTurntable ] )
|
||||
ArchaicBlocks.blockFirebox = contentRegistry.createBlock( classOf[ BlockFirebox ], classOf[ ItemBlockMetadata ], classOf[ TileFirebox ] )
|
||||
ArchaicBlocks.blockFirebox = contentRegistry.createBlock( classOf[ BlockFirebox ], null, classOf[ TileFirebox ] )
|
||||
ArchaicBlocks.blockHotPlate = contentRegistry.createTile( classOf[ BlockHotPlate ], classOf[ TileHotPlate ] )
|
||||
ArchaicBlocks.blockMillstone = contentRegistry.createTile( classOf[ BlockMillstone ], classOf[ TileMillstone ] )
|
||||
ArchaicBlocks.blockCast = contentRegistry.createTile( classOf[ BlockCastingMold ], classOf[ TileCastingMold ] )
|
||||
|
@ -116,9 +110,9 @@ class Archaic {
|
|||
GameRegistry.addRecipe( new ShapedOreRecipe( ArchaicBlocks.blockFilter, Array( "B", "P", "B", 'B', Blocks.iron_bars, 'P', Items.paper ) ) )
|
||||
GameRegistry.addRecipe( new ShapedOreRecipe( new ItemStack( ArchaicBlocks.blockCrate, 1, 0 ), Array( "WWW", "WSW", "WWW",
|
||||
'S', "stickWood", 'W', "logWood" ) ) )
|
||||
GameRegistry.addRecipe( new ShapedOreRecipe( new ItemStack( ArchaicBlocks.blockCrate, 1, 1 ), Array( "WWW", "WSW", "WWW",
|
||||
GameRegistry.addRecipe( new CrateRecipe( new ItemStack( ArchaicBlocks.blockCrate, 1, 1 ), Array( "WWW", "WSW", "WWW",
|
||||
'S', new ItemStack( ArchaicBlocks.blockCrate, 1, 0 ), 'W', "ingotIron" ) ) )
|
||||
GameRegistry.addRecipe( new ShapedOreRecipe( new ItemStack( ArchaicBlocks.blockCrate, 1, 2 ), Array( "WWW", "WSW", "WWW",
|
||||
GameRegistry.addRecipe( new CrateRecipe( new ItemStack( ArchaicBlocks.blockCrate, 1, 2 ), Array( "WWW", "WSW", "WWW",
|
||||
'S', new ItemStack( ArchaicBlocks.blockCrate, 1, 1 ), 'W', UniversalRecipe.PRIMARY_METAL.get ) ) )
|
||||
GameRegistry.addRecipe( new ShapedOreRecipe( ArchaicBlocks.blockFirebox, Array( "III", "SFS", "SSS", 'I', Items.iron_ingot,
|
||||
'F', Blocks.furnace, 'S', Blocks.stone ) ) )
|
||||
|
@ -145,7 +139,6 @@ class Archaic {
|
|||
new ItemStack( Items.dye, 0 ) ) ) )
|
||||
GameRegistry.addRecipe( new ShapedOreRecipe( ArchaicItems.itemHammer, Array( "CC ", "CS ", " S", 'C', "cobblestone", 'S',
|
||||
"stickWood" ) ) )
|
||||
GameRegistry.registerCraftingHandler( new CrateCraftingHandler() )
|
||||
proxy.postInit()
|
||||
}
|
||||
}
|
||||
|
|
|
@ -4,10 +4,6 @@ import net.minecraft.block.Block;
|
|||
import net.minecraft.inventory.InventoryCrafting;
|
||||
import net.minecraft.item.Item;
|
||||
import net.minecraft.item.crafting.IRecipe;
|
||||
import net.minecraft.world.World;
|
||||
import resonantinduction.archaic.Archaic;
|
||||
import net.minecraft.entity.player.EntityPlayer;
|
||||
import net.minecraft.inventory.IInventory;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import resonantinduction.archaic.ArchaicBlocks;
|
||||
import net.minecraftforge.oredict.ShapedOreRecipe;
|
||||
|
|
|
@ -3,20 +3,21 @@ package resonantinduction.archaic.crate;
|
|||
import java.util.List;
|
||||
|
||||
import net.minecraft.entity.Entity;
|
||||
import net.minecraft.block.Block;
|
||||
import net.minecraft.item.ItemBlock;
|
||||
import net.minecraft.entity.player.EntityPlayer;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.nbt.NBTTagCompound;
|
||||
import net.minecraft.potion.Potion;
|
||||
import net.minecraft.potion.PotionEffect;
|
||||
import net.minecraft.world.World;
|
||||
import resonant.lib.prefab.item.ItemBlockTooltip;
|
||||
import resonant.lib.utility.LanguageUtility;
|
||||
|
||||
public class ItemBlockCrate extends ItemBlockTooltip
|
||||
public class ItemBlockCrate extends ItemBlock
|
||||
{
|
||||
public ItemBlockCrate(int id)
|
||||
public ItemBlockCrate(Block block)
|
||||
{
|
||||
super(id);
|
||||
super(block);
|
||||
this.setHasSubtypes(true);
|
||||
}
|
||||
|
||||
|
@ -130,7 +131,7 @@ public class ItemBlockCrate extends ItemBlockTooltip
|
|||
for (int slot = 0; slot < tileEntity.getInventory().getSizeInventory(); slot++)
|
||||
{
|
||||
int stackSize = Math.min(64, count);
|
||||
tileEntity.getInventory().setInventorySlotContents(slot, new ItemStack(containingItem.itemID, stackSize, containingItem.getItemDamage()));
|
||||
tileEntity.getInventory().setInventorySlotContents(slot, new ItemStack(containingItem.getItem(), stackSize, containingItem.getItemDamage()));
|
||||
count -= stackSize;
|
||||
|
||||
if (count <= 0)
|
||||
|
|
|
@ -1,371 +0,0 @@
|
|||
package resonantinduction.archaic.crate;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import resonant.content.spatial.block.SpatialTile;
|
||||
import resonantinduction.archaic.Archaic;
|
||||
import net.minecraft.entity.player.EntityPlayer;
|
||||
import net.minecraft.item.Item;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.nbt.NBTTagCompound;
|
||||
import net.minecraft.network.packet.Packet;
|
||||
import net.minecraftforge.common.util.ForgeDirection;
|
||||
import net.minecraftforge.oredict.OreDictionary;
|
||||
import resonant.api.IExtendedStorage;
|
||||
import resonant.api.IFilterable;
|
||||
import resonant.api.IRemovable.ISneakPickup;
|
||||
import resonant.lib.network.IPacketReceiver;
|
||||
import resonant.lib.network.PacketHandler;
|
||||
import resonant.lib.prefab.tile.TileExternalInventory;
|
||||
import resonantinduction.archaic.Archaic;
|
||||
import resonantinduction.archaic.Archaic;
|
||||
import resonantinduction.core.ResonantInduction;
|
||||
|
||||
import com.google.common.io.ByteArrayDataInput;
|
||||
|
||||
/** Basic single stack inventory.
|
||||
* <p/>
|
||||
* TODO: Add filter-locking feature. Put filter in, locks the crate to only use that item.
|
||||
*
|
||||
* @author DarkGuardsman */
|
||||
public class TileCrate extends SpatialTile implements TInventory, IPacketReceiver, IExtendedStorage, IFilterable, ISneakPickup
|
||||
{
|
||||
/** max meta size of the crate */
|
||||
public static final int maxSize = 2;
|
||||
|
||||
/** delay from last click */
|
||||
public long prevClickTime = -1000;
|
||||
|
||||
/** Check to see if oreName items can be force stacked */
|
||||
public boolean oreFilterEnabled = false;
|
||||
|
||||
/** Collective total stack of all inv slots */
|
||||
private ItemStack sampleStack;
|
||||
private ItemStack filterStack;
|
||||
|
||||
private long updateTick = 1;
|
||||
private boolean doUpdate = false;
|
||||
|
||||
@Override
|
||||
public void updateEntity()
|
||||
{
|
||||
super.updateEntity();
|
||||
if (!worldObj.isRemote)
|
||||
{
|
||||
this.writeToNBT(new NBTTagCompound());
|
||||
if (ticks % updateTick == 0)
|
||||
{
|
||||
updateTick = 5 + worldObj.rand.nextInt(50);
|
||||
doUpdate = true;
|
||||
}
|
||||
if (doUpdate)
|
||||
{
|
||||
doUpdate = false;
|
||||
PacketHandler.sendPacketToClients(getDescriptionPacket(), this.worldObj);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/** Gets the slot count for the crate meta */
|
||||
public static int getSlotCount(int metadata)
|
||||
{
|
||||
if (metadata >= 2)
|
||||
{
|
||||
return 256;
|
||||
}
|
||||
else if (metadata >= 1)
|
||||
{
|
||||
return 64;
|
||||
}
|
||||
return 32;
|
||||
}
|
||||
|
||||
@Override
|
||||
public InventoryCrate getInventory()
|
||||
{
|
||||
if (this.inventory == null)
|
||||
{
|
||||
inventory = new InventoryCrate(this);
|
||||
}
|
||||
return (InventoryCrate) this.inventory;
|
||||
}
|
||||
|
||||
/** Gets the sample stack that represent the total inventory */
|
||||
public ItemStack getSampleStack()
|
||||
{
|
||||
if (this.sampleStack == null)
|
||||
{
|
||||
this.buildSampleStack();
|
||||
}
|
||||
return this.sampleStack;
|
||||
}
|
||||
|
||||
/** Builds the sample stack using the inventory as a point of reference. Assumes all items match
|
||||
* each other, and only takes into account stack sizes */
|
||||
public void buildSampleStack()
|
||||
{
|
||||
buildSampleStack(true);
|
||||
}
|
||||
|
||||
public void buildSampleStack(boolean buildInv)
|
||||
{
|
||||
if (worldObj == null || !worldObj.isRemote)
|
||||
{
|
||||
ItemStack newSampleStack = null;
|
||||
boolean rebuildBase = false;
|
||||
|
||||
/* Creates the sample stack that is used as a collective itemstack */
|
||||
for (int slot = 0; slot < this.getSizeInventory(); slot++)
|
||||
{
|
||||
ItemStack slotStack = this.getInventory().getContainedItems()[slot];
|
||||
if (slotStack != null && Item.itemsList[slotStack.itemID] != null && slotStack.stackSize > 0)
|
||||
{
|
||||
if (newSampleStack == null)
|
||||
{
|
||||
newSampleStack = slotStack.copy();
|
||||
}
|
||||
else
|
||||
{
|
||||
newSampleStack.stackSize += slotStack.stackSize;
|
||||
}
|
||||
|
||||
if (slotStack.stackSize > slotStack.getMaxStackSize())
|
||||
{
|
||||
rebuildBase = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (newSampleStack == null || newSampleStack.itemID == 0 || newSampleStack.stackSize <= 0)
|
||||
{
|
||||
this.sampleStack = this.getFilter() != null ? this.getFilter().copy() : null;
|
||||
}
|
||||
else
|
||||
{
|
||||
this.sampleStack = newSampleStack.copy();
|
||||
}
|
||||
|
||||
/* Rebuild inventory if the inventory is not valid */
|
||||
if (buildInv && this.sampleStack != null && (rebuildBase || this.getInventory().getContainedItems().length > this.getSizeInventory()))
|
||||
{
|
||||
this.getInventory().buildInventory(this.sampleStack);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public ItemStack addStackToStorage(ItemStack stack)
|
||||
{
|
||||
return BlockCrate.addStackToCrate(this, stack);
|
||||
}
|
||||
|
||||
/** Adds an item to the stack */
|
||||
public void addToStack(ItemStack stack, int amount)
|
||||
{
|
||||
if (stack != null)
|
||||
{
|
||||
ItemStack newStack = stack.copy();
|
||||
newStack.stackSize = amount;
|
||||
this.addToStack(newStack);
|
||||
}
|
||||
}
|
||||
|
||||
/** Adds the stack to the sample stack */
|
||||
public void addToStack(ItemStack stack)
|
||||
{
|
||||
if (stack != null && stack.stackSize > 0)
|
||||
{
|
||||
if (this.getSampleStack() == null)
|
||||
{
|
||||
this.sampleStack = stack;
|
||||
getInventory().buildInventory(getSampleStack());
|
||||
}
|
||||
else if (this.getSampleStack().isItemEqual(stack) || (this.oreFilterEnabled && OreDictionary.getOreID(getSampleStack()) == OreDictionary.getOreID(stack)))
|
||||
{
|
||||
getSampleStack().stackSize += stack.stackSize;
|
||||
getInventory().buildInventory(getSampleStack());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public ItemStack decrStackSize(int slot, int amount)
|
||||
{
|
||||
if (sampleStack != null)
|
||||
{
|
||||
ItemStack var3;
|
||||
|
||||
if (sampleStack.stackSize <= amount)
|
||||
{
|
||||
var3 = sampleStack;
|
||||
sampleStack = null;
|
||||
this.onInventoryChanged();
|
||||
getInventory().buildInventory(getSampleStack());
|
||||
return var3;
|
||||
}
|
||||
else
|
||||
{
|
||||
var3 = sampleStack.splitStack(amount);
|
||||
|
||||
if (sampleStack.stackSize == 0)
|
||||
{
|
||||
sampleStack = null;
|
||||
}
|
||||
|
||||
getInventory().buildInventory(getSampleStack());
|
||||
onInventoryChanged();
|
||||
return var3;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onInventoryChanged()
|
||||
{
|
||||
super.onInventoryChanged();
|
||||
if (worldObj != null && !worldObj.isRemote)
|
||||
doUpdate = true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canStore(ItemStack stack, int slot, ForgeDirection side)
|
||||
{
|
||||
return getSampleStack() == null || stack != null && (stack.isItemEqual(getSampleStack()) || (this.oreFilterEnabled && OreDictionary.getOreID(getSampleStack()) == OreDictionary.getOreID(stack)));
|
||||
}
|
||||
|
||||
/** Gets the current slot count for the crate */
|
||||
public int getSlotCount()
|
||||
{
|
||||
if (this.worldObj == null)
|
||||
{
|
||||
return TileCrate.getSlotCount(TileCrate.maxSize);
|
||||
}
|
||||
return TileCrate.getSlotCount(this.getBlockMetadata());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onReceivePacket(ByteArrayDataInput data, EntityPlayer player, Object... extra)
|
||||
{
|
||||
if (this.worldObj.isRemote)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (data.readBoolean())
|
||||
{
|
||||
this.sampleStack = ItemStack.loadItemStackFromNBT(PacketHandler.readNBTTagCompound(data));
|
||||
this.sampleStack.stackSize = data.readInt();
|
||||
}
|
||||
else
|
||||
{
|
||||
this.sampleStack = null;
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Packet getDescriptionPacket()
|
||||
{
|
||||
this.buildSampleStack();
|
||||
ItemStack stack = this.getSampleStack();
|
||||
if (stack != null)
|
||||
{
|
||||
return ResonantInduction.PACKET_TILE.getPacket(this, true, stack.writeToNBT(new NBTTagCompound()), stack.stackSize);
|
||||
}
|
||||
else
|
||||
{
|
||||
return ResonantInduction.PACKET_TILE.getPacket(this, false);
|
||||
}
|
||||
}
|
||||
|
||||
/** NBT Data */
|
||||
@Override
|
||||
public void readFromNBT(NBTTagCompound nbt)
|
||||
{
|
||||
super.readFromNBT(nbt);
|
||||
/* Load current two inv methods */
|
||||
ItemStack stack = null;
|
||||
int count = nbt.getInteger("Count");
|
||||
if (nbt.hasKey("itemID"))
|
||||
{
|
||||
stack = new ItemStack(nbt.getInteger("itemID"), count, nbt.getInteger("itemMeta"));
|
||||
}
|
||||
else
|
||||
{
|
||||
stack = ItemStack.loadItemStackFromNBT(nbt.getCompoundTag("stack"));
|
||||
if (stack != null)
|
||||
{
|
||||
stack.stackSize = count;
|
||||
}
|
||||
}
|
||||
|
||||
/* Only load sample stack if the read stack is valid */
|
||||
if (stack != null && stack.itemID != 0 && stack.stackSize > 0)
|
||||
{
|
||||
this.sampleStack = stack;
|
||||
this.getInventory().buildInventory(this.sampleStack);
|
||||
}
|
||||
this.oreFilterEnabled = nbt.getBoolean("oreFilter");
|
||||
if (nbt.hasKey("filter"))
|
||||
{
|
||||
filterStack = ItemStack.loadItemStackFromNBT(nbt.getCompoundTag("filter"));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeToNBT(NBTTagCompound nbt)
|
||||
{
|
||||
super.writeToNBT(nbt);
|
||||
/* Re-Build sample stack for saving */
|
||||
this.buildSampleStack(false);
|
||||
ItemStack stack = this.getSampleStack();
|
||||
/* Save sample stack */
|
||||
if (stack != null)
|
||||
{
|
||||
nbt.setInteger("Count", stack.stackSize);
|
||||
nbt.setCompoundTag("stack", stack.writeToNBT(new NBTTagCompound()));
|
||||
}
|
||||
nbt.setBoolean("oreFilter", this.oreFilterEnabled);
|
||||
if (this.filterStack != null)
|
||||
{
|
||||
nbt.setCompoundTag("filter", filterStack.writeToNBT(new NBTTagCompound()));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public ItemStack getFilter()
|
||||
{
|
||||
return this.filterStack;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setFilter(ItemStack filter)
|
||||
{
|
||||
this.filterStack = filter;
|
||||
this.onInventoryChanged();
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public List<ItemStack> getRemovedItems(EntityPlayer entity)
|
||||
{
|
||||
ItemStack sampleStack = getSampleStack();
|
||||
ItemStack drop = new ItemStack(Archaic.blockCrate, 1, this.getBlockMetadata());
|
||||
if (sampleStack != null && sampleStack.stackSize > 0)
|
||||
{
|
||||
ItemBlockCrate.setContainingItemStack(drop, sampleStack);
|
||||
}
|
||||
return Arrays.asList(new ItemStack[] { drop });
|
||||
}
|
||||
|
||||
}
|
284
src/main/scala/resonantinduction/archaic/crate/TileCrate.scala
Normal file
284
src/main/scala/resonantinduction/archaic/crate/TileCrate.scala
Normal file
|
@ -0,0 +1,284 @@
|
|||
package resonantinduction.archaic.crate
|
||||
|
||||
import java.util
|
||||
import java.util.List
|
||||
|
||||
import io.netty.buffer.ByteBuf
|
||||
import net.minecraft.block.material.Material
|
||||
import net.minecraft.entity.player.EntityPlayer
|
||||
import net.minecraft.item.{Item, ItemStack}
|
||||
import net.minecraft.nbt.NBTTagCompound
|
||||
import net.minecraftforge.common.util.ForgeDirection
|
||||
import net.minecraftforge.oredict.OreDictionary
|
||||
import resonant.api.IFilterable
|
||||
import resonant.api.IRemovable.ISneakPickup
|
||||
import resonant.content.spatial.block.SpatialTile
|
||||
import resonant.engine.ResonantEngine
|
||||
import resonant.lib.content.prefab.TInventory
|
||||
import resonant.lib.network.ByteBufWrapper._
|
||||
import resonant.lib.network.discriminator.{PacketTile, PacketType}
|
||||
import resonant.lib.network.handle.TPacketReceiver
|
||||
import resonantinduction.archaic.ArchaicBlocks
|
||||
import universalelectricity.core.transform.vector.Vector3
|
||||
|
||||
/** Basic single stack inventory.
|
||||
* <p/>
|
||||
* TODO: Add filter-locking feature. Put filter in, locks the crate to only use that item.
|
||||
*
|
||||
* @author DarkGuardsman */
|
||||
object TileCrate {
|
||||
/** Gets the slot count for the crate meta */
|
||||
def getSlotCount(metadata: Int): Int = {
|
||||
if (metadata >= 2) {
|
||||
return 256
|
||||
}
|
||||
else if (metadata >= 1) {
|
||||
return 64
|
||||
}
|
||||
return 32
|
||||
}
|
||||
|
||||
/** max meta size of the crate */
|
||||
final val maxSize: Int = 2
|
||||
}
|
||||
|
||||
class TileCrate extends SpatialTile(Material.rock) with TInventory with TPacketReceiver with IFilterable with ISneakPickup {
|
||||
|
||||
/** delay from last click */
|
||||
var prevClickTime: Long = -1000
|
||||
/** Check to see if oreName items can be force stacked */
|
||||
var oreFilterEnabled: Boolean = false
|
||||
/** Collective total stack of all inv slots */
|
||||
private var sampleStack: ItemStack = null
|
||||
private var filterStack: ItemStack = null
|
||||
private var updateTick: Long = 1
|
||||
private var doUpdate: Boolean = false
|
||||
|
||||
override def getSizeInventory: Int = TileCrate.getSlotCount(getBlockMetadata)
|
||||
|
||||
override def updateEntity {
|
||||
super.updateEntity
|
||||
if (!worldObj.isRemote) {
|
||||
this.writeToNBT(new NBTTagCompound)
|
||||
if (ticks % updateTick eq 0) {
|
||||
updateTick = 5 + worldObj.rand.nextInt(50)
|
||||
doUpdate = true
|
||||
}
|
||||
if (doUpdate) {
|
||||
doUpdate = false
|
||||
ResonantEngine.instance.packetHandler.sendToAllAround(getDescriptionPacket, this.worldObj, new Vector3(this), 50)
|
||||
}
|
||||
}
|
||||
}
|
||||
override protected lazy val inventory = new InventoryCrate(this)
|
||||
|
||||
/** Gets the sample stack that represent the total inventory */
|
||||
def getSampleStack: ItemStack = {
|
||||
if (this.sampleStack == null) {
|
||||
this.buildSampleStack
|
||||
}
|
||||
return this.sampleStack
|
||||
}
|
||||
|
||||
/** Builds the sample stack using the inventory as a point of reference. Assumes all items match
|
||||
* each other, and only takes into account stack sizes */
|
||||
def buildSampleStack {
|
||||
buildSampleStack(true)
|
||||
}
|
||||
|
||||
def buildSampleStack(buildInv: Boolean) {
|
||||
if (worldObj == null || !worldObj.isRemote) {
|
||||
var newSampleStack: ItemStack = null
|
||||
var rebuildBase: Boolean = false
|
||||
for (slot <- getSizeInventory) {
|
||||
|
||||
val slotStack: ItemStack = this.getInventory.getStackInSlot(slot)
|
||||
if (slotStack != null && slotStack.getItem != null && slotStack.stackSize > 0) {
|
||||
if (newSampleStack == null) {
|
||||
newSampleStack = slotStack.copy
|
||||
}
|
||||
else {
|
||||
newSampleStack.stackSize += slotStack.stackSize
|
||||
}
|
||||
if (slotStack.stackSize > slotStack.getMaxStackSize) {
|
||||
rebuildBase = true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
if (newSampleStack == null || newSampleStack.getItem == null || newSampleStack.stackSize <= 0) {
|
||||
this.sampleStack = if (this.getFilter != null) this.getFilter.copy else null
|
||||
}
|
||||
else {
|
||||
this.sampleStack = newSampleStack.copy
|
||||
}
|
||||
if (buildInv && this.sampleStack != null && (rebuildBase || this.getInventory.getContainedItems.length > this.getSizeInventory)) {
|
||||
this.getInventory.asInstanceOf[InventoryCrate].buildInventory(this.sampleStack)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
def addStackToStorage(stack: ItemStack): ItemStack = {
|
||||
return BlockCrate.addStackToCrate(this, stack)
|
||||
}
|
||||
|
||||
/** Adds an item to the stack */
|
||||
def addToStack(stack: ItemStack, amount: Int) {
|
||||
if (stack != null) {
|
||||
val newStack: ItemStack = stack.copy
|
||||
newStack.stackSize = amount
|
||||
this.addToStack(newStack)
|
||||
}
|
||||
}
|
||||
|
||||
/** Adds the stack to the sample stack */
|
||||
def addToStack(stack: ItemStack) {
|
||||
if (stack != null && stack.stackSize > 0) {
|
||||
if (this.getSampleStack == null) {
|
||||
this.sampleStack = stack
|
||||
getInventory.asInstanceOf[InventoryCrate].buildInventory(getSampleStack)
|
||||
}
|
||||
else if (this.getSampleStack.isItemEqual(stack) || (this.oreFilterEnabled && OreDictionary.getOreID(getSampleStack) == OreDictionary.getOreID(stack))) {
|
||||
getSampleStack.stackSize += stack.stackSize
|
||||
getInventory.asInstanceOf[InventoryCrate].buildInventory(getSampleStack)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override def decrStackSize(slot: Int, amount: Int): ItemStack = {
|
||||
if (sampleStack != null) {
|
||||
var var3: ItemStack = null
|
||||
if (sampleStack.stackSize <= amount) {
|
||||
var3 = sampleStack
|
||||
sampleStack = null
|
||||
this.onInventoryChanged
|
||||
getInventory.asInstanceOf[InventoryCrate].buildInventory(getSampleStack)
|
||||
return var3
|
||||
}
|
||||
else {
|
||||
var3 = sampleStack.splitStack(amount)
|
||||
if (sampleStack.stackSize == 0) {
|
||||
sampleStack = null
|
||||
}
|
||||
getInventory.asInstanceOf[InventoryCrate].buildInventory(getSampleStack)
|
||||
onInventoryChanged
|
||||
return var3
|
||||
}
|
||||
}
|
||||
else {
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
def onInventoryChanged {
|
||||
if (worldObj != null && !worldObj.isRemote) doUpdate = true
|
||||
}
|
||||
|
||||
override def canStore(stack: ItemStack, slot: Int, side: ForgeDirection): Boolean = {
|
||||
return getSampleStack == null || stack != null && (stack.isItemEqual(getSampleStack) || (this.oreFilterEnabled && OreDictionary.getOreID(getSampleStack) == OreDictionary.getOreID(stack)))
|
||||
}
|
||||
|
||||
/** Gets the current slot count for the crate */
|
||||
def getSlotCount: Int = {
|
||||
if (this.worldObj == null) {
|
||||
return TileCrate.getSlotCount(TileCrate.maxSize)
|
||||
}
|
||||
return TileCrate.getSlotCount(this.getBlockMetadata)
|
||||
}
|
||||
|
||||
def read(data: ByteBuf, player: EntityPlayer, packet: PacketType)
|
||||
{
|
||||
if (this.worldObj.isRemote) {
|
||||
try {
|
||||
if (data.readBoolean) {
|
||||
this.sampleStack = ItemStack.loadItemStackFromNBT(data.readTag())
|
||||
this.sampleStack.stackSize = data.readInt
|
||||
}
|
||||
else {
|
||||
this.sampleStack = null
|
||||
}
|
||||
}
|
||||
catch {
|
||||
case e: Exception => {
|
||||
e.printStackTrace
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override def getDescriptionPacket: PacketTile = {
|
||||
var packet: PacketTile = null
|
||||
this.buildSampleStack
|
||||
val stack: ItemStack = this.getSampleStack
|
||||
if (stack != null) {
|
||||
|
||||
packet = new PacketTile(this, true, stack.writeToNBT(new NBTTagCompound), stack.stackSize)
|
||||
}
|
||||
else {
|
||||
packet = new PacketTile(this, false)
|
||||
}
|
||||
return packet
|
||||
}
|
||||
|
||||
/** NBT Data */
|
||||
override def readFromNBT(nbt: NBTTagCompound) {
|
||||
super.readFromNBT(nbt)
|
||||
var stack: ItemStack = null
|
||||
val count: Int = nbt.getInteger("Count")
|
||||
if (nbt.hasKey("itemID")) {
|
||||
stack = new ItemStack(Item.getItemById(nbt.getInteger("itemID")), count, nbt.getInteger("itemMeta"))
|
||||
}
|
||||
else {
|
||||
stack = ItemStack.loadItemStackFromNBT(nbt.getCompoundTag("stack"))
|
||||
if (stack != null) {
|
||||
stack.stackSize = count
|
||||
}
|
||||
}
|
||||
if (stack != null && stack.getItem != null && stack.stackSize > 0) {
|
||||
this.sampleStack = stack
|
||||
this.getInventory.asInstanceOf[InventoryCrate].buildInventory(this.sampleStack)
|
||||
}
|
||||
this.oreFilterEnabled = nbt.getBoolean("oreFilter")
|
||||
if (nbt.hasKey("filter")) {
|
||||
filterStack = ItemStack.loadItemStackFromNBT(nbt.getCompoundTag("filter"))
|
||||
}
|
||||
}
|
||||
|
||||
override def writeToNBT(nbt: NBTTagCompound) {
|
||||
super.writeToNBT(nbt)
|
||||
this.buildSampleStack(false)
|
||||
val stack: ItemStack = this.getSampleStack
|
||||
if (stack != null) {
|
||||
nbt.setInteger("Count", stack.stackSize)
|
||||
nbt.setTag("stack", stack.writeToNBT(new NBTTagCompound))
|
||||
}
|
||||
nbt.setBoolean("oreFilter", this.oreFilterEnabled)
|
||||
if (this.filterStack != null) {
|
||||
nbt.setTag("filter", filterStack.writeToNBT(new NBTTagCompound))
|
||||
}
|
||||
}
|
||||
|
||||
def getFilter: ItemStack = {
|
||||
return this.filterStack
|
||||
}
|
||||
|
||||
def setFilter(filter: ItemStack) {
|
||||
this.filterStack = filter
|
||||
this.onInventoryChanged
|
||||
}
|
||||
|
||||
@SuppressWarnings(Array("unchecked")) def getRemovedItems(entity: EntityPlayer): List[ItemStack] = {
|
||||
val list = new util.ArrayList[ItemStack]()
|
||||
val sampleStack: ItemStack = getSampleStack
|
||||
val drop: ItemStack = new ItemStack(Item.getItemFromBlock(ArchaicBlocks.blockCrate), 1, this.getBlockMetadata)
|
||||
if (sampleStack != null && sampleStack.stackSize > 0)
|
||||
{
|
||||
ItemBlockCrate.setContainingItemStack(drop, sampleStack)
|
||||
}
|
||||
list.add(drop)
|
||||
return list
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue