tile still wont save inventory

This commit is contained in:
Mohammad-Ali Minaie
2017-04-11 06:42:09 -04:00
parent 6cca149ebb
commit 871bb014f9
2 changed files with 19 additions and 69 deletions

View File

@@ -43,7 +43,7 @@ import java.util.concurrent.ThreadLocalRandom;
/** /**
* Created by mminaie on 4/9/17. * Created by mminaie on 4/9/17.
*/ */
public class Breaker extends CustomContainerFacing implements ITileEntityProvider { public class Breaker extends CustomContainerFacing {
public static final PropertyBool ACTIVE = PropertyBool.create("active"); public static final PropertyBool ACTIVE = PropertyBool.create("active");
@@ -60,6 +60,7 @@ public class Breaker extends CustomContainerFacing implements ITileEntityProvide
public boolean onBlockActivated(World world, BlockPos pos, IBlockState state, EntityPlayer player, EnumHand hand, EnumFacing facing, float hitx, float hity, float hitz) { public boolean onBlockActivated(World world, BlockPos pos, IBlockState state, EntityPlayer player, EnumHand hand, EnumFacing facing, float hitx, float hity, float hitz) {
if(!world.isRemote){ if(!world.isRemote){
System.out.println("tile: " + pos);
TileBreaker tile = (TileBreaker) world.getTileEntity(pos); TileBreaker tile = (TileBreaker) world.getTileEntity(pos);
ItemStack pItem = player.inventory.getCurrentItem(); ItemStack pItem = player.inventory.getCurrentItem();
if(state.getValue(ACTIVE) == true && player.isSneaking() && pItem.isEmpty()){ if(state.getValue(ACTIVE) == true && player.isSneaking() && pItem.isEmpty()){
@@ -125,7 +126,7 @@ public class Breaker extends CustomContainerFacing implements ITileEntityProvide
System.out.println(pItem); System.out.println(pItem);
tile.setSlotStack(0, pItem); tile.setSlotStack(0, pItem);
pItem.shrink(1); pItem.shrink(1);
System.out.println(tile.getSlotStack(0) + " " + tile.getSlotListSize());
return true; return true;
} }

View File

@@ -1,16 +1,19 @@
package nmd.primal.forgecraft.tiles; package nmd.primal.forgecraft.tiles;
import net.minecraft.block.state.IBlockState;
import net.minecraft.inventory.ItemStackHelper; import net.minecraft.inventory.ItemStackHelper;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound; import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.ITickable;
import net.minecraft.util.NonNullList; import net.minecraft.util.NonNullList;
import net.minecraft.world.World;
import java.util.List; import java.util.List;
/** /**
* Created by mminaie on 4/9/17. * Created by mminaie on 4/9/17.
*/ */
public class TileBreaker extends BaseTile { public class TileBreaker extends TileBaseSlot implements ITickable {
private float charge; private float charge;
@@ -22,71 +25,19 @@ public class TileBreaker extends BaseTile {
this.charge = charge; this.charge = charge;
} }
public NonNullList<ItemStack> slotList = NonNullList.<ItemStack>withSize(2, ItemStack.EMPTY); //public NonNullList<ItemStack> slotList = NonNullList.<ItemStack>withSize(2, ItemStack.EMPTY);
// ***************************************************************************** // @Override
// Controls public void update () {
// ***************************************************************************** // World world = this.getWorld();
if (!world.isRemote) {
// ***************************************************************************** // IBlockState state = world.getBlockState(this.pos);
// get for(int i =0; i < this.getSlotListSize(); i++) {
// System.out.println(this.getSlotStack(i));
public NonNullList<ItemStack> getSlotList()
{
return this.slotList;
} }
public ItemStack getSlotStack(int i)
{
return this.slotList.get(i);
} }
public int getSlotListSize()
{
return slotList.size();
} }
public int getSlotLimit()
{
//return ((ShelfBasic)this.getBlockType()).getShelfSize();
//return 1;
return 64;
}
public ItemStack decrStackSize(int index, int count)
{
return ItemStackHelper.getAndSplit(this.getSlotList(), index, count);
}
public ItemStack incrementStackSize(List<ItemStack> stacks, int index, int count) {
Integer tempCount = ((ItemStack)stacks.get(index)).getCount();
ItemStack tempStack = ((ItemStack)stacks.get(index));
if(tempCount + count > 64) {
tempStack.setCount(64);
}
if(tempCount + count <= 64) {
tempStack.setCount(tempCount + count);
}
return tempStack;
}
// ***************************************************************************** //
// set
//
public void setSlotStack(int index, ItemStack stack)
{
this.slotList.set(index, stack);
this.markDirty();
this.updateBlock();
}
public void clearSlots()
{
this.slotList.clear();
}
// ***************************************************************************** // // ***************************************************************************** //
// NBT // NBT
// ***************************************************************************** // // ***************************************************************************** //
@@ -94,18 +45,16 @@ public class TileBreaker extends BaseTile {
public NBTTagCompound readNBT(NBTTagCompound nbt) public NBTTagCompound readNBT(NBTTagCompound nbt)
{ {
super.readNBT(nbt); super.readNBT(nbt);
this.slotList = NonNullList.<ItemStack>withSize(this.getSlotListSize(), ItemStack.EMPTY); this.charge = nbt.getFloat("charge");
ItemStackHelper.loadAllItems(nbt, this.slotList);
return nbt; return nbt;
} }
@Override @Override
public NBTTagCompound writeNBT(NBTTagCompound nbt) public NBTTagCompound writeNBT(NBTTagCompound nbt)
{ {
nbt.setFloat("charge", this.charge);
super.writeNBT(nbt); super.writeNBT(nbt);
ItemStackHelper.saveAllItems(nbt, this.slotList);
return nbt; return nbt;
} }
} }