Jump to content

Need help with readFrom/Write to NBT


Zeher_Monkey

Recommended Posts

Added that, doesnt seem to do much. Basically what is happening is that when i change a side, and exit the world, the side reverts to what is used to be. Say i changed it from 0 to 2, i left the world, came back and it was 0 again. With a block update (Place and destroy another block next to my block), it stays as 2. I want to know if theres a way i can make sure it gets updated so it doesnt revert or require a block update. If i leave it enough time writeToNBT gets called anyway, however i want to make sure it doesnt revert. I may not have been clear, sorry :/ 

Link to comment
Share on other sites

Just now, Jay Avery said:

Can you post all your latest code? It's hard to keep track with various updates in different posts.

Of course :)

BlockCapacitorBasic

Spoiler

package com.zeher.orecraft.core.block.capacitor;

import java.util.Random;

import javax.annotation.Nullable;

import com.zeher.orecraft.OreCraftCore;
import com.zeher.orecraft.client.tileentity.capacitor.TileEntityCapacitorAdvanced;
import com.zeher.orecraft.client.tileentity.capacitor.TileEntityCapacitorBasic;
import com.zeher.orecraft.client.tileentity.energypipe.TileEntityEnergyPipeBasic;
import com.zeher.orecraft.core.handlers.BlockHandlerOreCraft;
import com.zeher.orecraft.core.net.NetUtils;
import com.zeher.orecraft.core.net.packet.PacketCapacitorBasic;
import com.zeher.trzcore.api.TRZUtil;
import com.zeher.trzcore.api.energy.TRZPipeConnectBase;
import com.zeher.trzcore.core.block.TRZBlockCapacitor;
import com.zeher.trzcore.core.tileentity.TRZTileEntityEnergyPipe;

import net.minecraft.block.Block;
import net.minecraft.block.material.Material;
import net.minecraft.block.properties.IProperty;
import net.minecraft.block.properties.PropertyBool;
import net.minecraft.block.properties.PropertyInteger;
import net.minecraft.block.state.BlockStateContainer;
import net.minecraft.block.state.IBlockState;
import net.minecraft.client.Minecraft;
import net.minecraft.enchantment.EnchantmentHelper;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Enchantments;
import net.minecraft.init.Items;
import net.minecraft.item.Item;
import net.minecraft.item.ItemBlock;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.stats.StatList;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.EnumBlockRenderType;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.EnumHand;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.IBlockAccess;
import net.minecraft.world.IWorldNameable;
import net.minecraft.world.World;
import net.minecraftforge.fml.common.network.internal.FMLNetworkHandler;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;

public class BlockCapacitorBasic extends TRZBlockCapacitor{
    
    private final Random capacitor_random = new Random();
    private boolean keepCapacitorInventory;
    
    public static final PropertyInteger NORTH = PropertyInteger.create("north", 0, 2);
    public static final PropertyInteger EAST = PropertyInteger.create("east", 0, 2);
    public static final PropertyInteger SOUTH = PropertyInteger.create("south", 0, 2);
    public static final PropertyInteger WEST = PropertyInteger.create("west", 0, 2);
    public static final PropertyInteger UP = PropertyInteger.create("up", 0, 2);
    public static final PropertyInteger DOWN = PropertyInteger.create("down", 0, 2);
    
    public int upSide;
    public int downSide;
    public int eastSide;
    public int westSide;
    public int northSide;
    public int southSide;

    public BlockCapacitorBasic(Material material, String name) {
        super(material, name);
        this.setDefaultState(this.blockState.getBaseState().withProperty(NORTH, 0)
                .withProperty(EAST, 0)
                .withProperty(SOUTH, 0)
                .withProperty(WEST, 0)
                .withProperty(UP, 0)
                .withProperty(DOWN, 0));
        setHardness(8);
        setResistance(10);
        
    }
    
    
    @Override
    public Item getItemDropped(IBlockState state, Random rand, int fortune)
    {
        Item item = Item.getItemFromBlock(this);
        ItemStack stack = new ItemStack(this, 1, 1000);
        return stack.getItem();
    }

    @Override
    public boolean isOpaqueCube(IBlockState state){
        return false;
    }
    
    @Override
    public boolean isFullCube(IBlockState state){
        return false;
    }
    
    public EnumBlockRenderType getRenderType(IBlockState state)
    {
        return EnumBlockRenderType.MODEL;
    }
    
    public TileEntity createNewTileEntity(World world, int i){
        return new TileEntityCapacitorBasic();
    }
    
    public void onBlockClicked(World worldIn, BlockPos pos, EntityPlayer playerIn)
    {
        //updateBlock((TileEntityCapacitorBasic) worldIn.getTileEntity(pos), worldIn, pos);
        if(!(worldIn.isRemote) && !(playerIn.isSneaking()) && (TRZUtil.isHoldingHammer(playerIn))){
                FMLNetworkHandler.openGui(playerIn, OreCraftCore.instance, 0, worldIn, pos.getX(), pos.getY(), pos.getZ());
                //this.getActualState(worldIn.getBlockState(pos), worldIn, pos);
        }
    }
    
    public boolean onBlockActivated(World worldIn, BlockPos pos, IBlockState state, EntityPlayer playerIn, EnumHand hand, EnumFacing side, float hitX, float hitY, float hitZ)
    {
        worldIn.setBlockState(pos, state);
        worldIn.notifyBlockUpdate(pos, state, state, 3);
        
        //updateBlock((TileEntityCapacitorBasic) worldIn.getTileEntity(pos), worldIn, pos);
        this.getActualState(state, worldIn, pos);
        if(!(worldIn.isRemote) && !(playerIn.isSneaking()) && !(TRZUtil.isHoldingHammer(playerIn))){
            FMLNetworkHandler.openGui(playerIn, OreCraftCore.instance, 1, worldIn, pos.getX(), pos.getY(), pos.getZ());
        }
        if((TRZUtil.isHoldingHammer(playerIn)) && (playerIn.isSneaking()) && !(worldIn.isRemote)){
            playerIn.swingArm(EnumHand.MAIN_HAND);
                
                TileEntityCapacitorBasic tile = (TileEntityCapacitorBasic)worldIn.getTileEntity(pos);
                int set_power = tile.getPower();
                
                Item item1 = ItemBlock.getItemFromBlock(this);
                ItemStack stack = new ItemStack(item1);
                item1.setDamage(stack, item1.getMaxDamage() - (set_power / 1000));
                
                spawnAsEntity(worldIn, pos, stack);
                
                worldIn.destroyBlock(pos, false);
        }
        TileEntityCapacitorBasic tile = (TileEntityCapacitorBasic) worldIn.getTileEntity(pos);
        if ((TRZUtil.isHoldingHammer(playerIn)) && (!playerIn.isSneaking())) {
            playerIn.swingArm(EnumHand.MAIN_HAND);
            if (side.equals(EnumFacing.UP)) {
                if(worldIn.isRemote){
                    tile.cycleSide("up");
                } else {
                    tile.cycleSide("up");
                }
            }
            if (side.equals(EnumFacing.DOWN)) {
                if(worldIn.isRemote){
                    tile.cycleSide("down");
                } else {
                    tile.cycleSide("down");
                }
            }
            if (side.equals(EnumFacing.NORTH)) {
                if(worldIn.isRemote){
                    tile.cycleSide("north");
                } else {
                    tile.cycleSide("north");
                }
            }
            if (side.equals(EnumFacing.SOUTH)) {
                if(worldIn.isRemote){
                    tile.cycleSide("south");
                } else {
                    tile.cycleSide("south");
                }
            }
            if (side.equals(EnumFacing.EAST)) {
                if(worldIn.isRemote){
                    tile.cycleSide("east");
                } else {
                    tile.cycleSide("east");
                }
            }
            if (side.equals(EnumFacing.WEST)) {
                if(worldIn.isRemote){
                    tile.cycleSide("west");
                } else {
                    tile.cycleSide("west");
                }
            }
        }
        
        //tile.getUpdatePacket();
        //tile.writeToNBT(new NBTTagCompound());
        return false;
    }
    
    protected BlockStateContainer createBlockState()
    {
        return new BlockStateContainer(this, new IProperty[] {NORTH, EAST, WEST, SOUTH, UP, DOWN});
    }
    
    public int getMetaFromState(IBlockState state)
    {
        return 0;
    }
    
    public IBlockState getActualState(IBlockState state, IBlockAccess worldIn, BlockPos pos)
    {    
        TileEntityCapacitorBasic tile = (TileEntityCapacitorBasic)worldIn.getTileEntity(pos);
        
        return state.withProperty(NORTH, tile.getSide("north"))
                .withProperty(EAST, tile.getSide("east"))
                .withProperty(SOUTH, tile.getSide("south"))
                .withProperty(WEST, tile.getSide("west"))
                .withProperty(UP, tile.getSide("up"))
                .withProperty(DOWN, tile.getSide("down"));
    }
    
    public boolean getSide(EnumFacing side, World world, BlockPos pos){
        TileEntity tile = world.getTileEntity(pos);
        int northInt = 0;
        int eastInt = 0;
        int southInt = 0;
        int westInt = 0;
        int downInt = 0;
        int upInt = 0;
        if(tile != null && tile instanceof TileEntityCapacitorBasic){
            northInt = ((TileEntityCapacitorBasic) tile).getSide("north");
            eastInt = ((TileEntityCapacitorBasic) tile).getSide("east");
            southInt = ((TileEntityCapacitorBasic) tile).getSide("south");
            westInt = ((TileEntityCapacitorBasic) tile).getSide("west");
            downInt = ((TileEntityCapacitorBasic) tile).getSide("down");
            upInt = ((TileEntityCapacitorBasic) tile).getSide("up");
        } else {
            
        }
        
        boolean upBool = false;
        boolean downBool = false;
        boolean northBool = false;
        boolean southBool = false;
        boolean eastBool = false;
        boolean westBool = false;
        if(side.equals(EnumFacing.UP)){
            if(upInt == 1){
                upBool = false;
            } else if(upInt == 2 || upInt == 0){
                upBool = true;
            }
            return upBool;
        }
        
        if(side.equals(EnumFacing.DOWN)){
            if(downInt == 1){
                downBool = false;
            } else if(downInt == 2 || downInt == 0){
                downBool = true;
            }
            return downBool;
        }
        if(side.equals(EnumFacing.NORTH)){
            if(northInt == 1){
                northBool = false;
            } else if(northInt == 2 || northInt == 0){
                northBool = true;
            }
            return northBool;
        }
        if(side.equals(EnumFacing.SOUTH)){
            if(southInt == 1){
                southBool = false;
            } else if(southInt == 2 || southInt == 0){
                southBool = true;
            }
            return southBool;
        }
        if(side.equals(EnumFacing.EAST)){
            if(eastInt == 1){
                eastBool = false;
            } else if(eastInt == 2 || eastInt == 0){
                eastBool = true;
            }
            return eastBool;
        }
        if(side.equals(EnumFacing.WEST)){
            if(westInt == 1){
                westBool = false;
            } else if(westInt == 2 || westInt == 0){
                westBool = true;
            }
            return westBool;
        }
        return false;
    }
    
    public void breakBlock(World worldIn, BlockPos pos, IBlockState state)
    {
        if (!keepCapacitorInventory)
        {
          TileEntityCapacitorBasic tileentityenergystorage_1 = (TileEntityCapacitorBasic)worldIn.getTileEntity(pos);
          if (tileentityenergystorage_1 != null)
          {
            for (int j1 = 0; j1 < tileentityenergystorage_1.getSizeInventory(); j1++)
            {
              ItemStack itemstack = tileentityenergystorage_1.getStackInSlot(j1);
              if (itemstack != null)
              {
                float f = this.capacitor_random.nextFloat() * 0.8F + 0.1F;
                float f1 = this.capacitor_random.nextFloat() * 0.8F + 0.1F;
                float f2 = this.capacitor_random.nextFloat() * 0.8F + 0.1F;
                while (itemstack.getCount() > 0)
                {
                  int k1 = this.capacitor_random.nextInt(21) + 10;
                  if (k1 > itemstack.getCount()) {
                    k1 = itemstack.getCount();
                  }
                  itemstack.setCount(itemstack.getCount() - k1);
                  EntityItem entityitem = new EntityItem(worldIn, pos.getX() + f, pos.getY() + f1, pos.getZ() + f2, new ItemStack(itemstack.getItem(), k1, itemstack.getItemDamage()));
                  if (itemstack.hasTagCompound()) {
                    entityitem.getEntityItem().setTagCompound((NBTTagCompound)itemstack.getTagCompound().copy());
                  }
                  float f3 = 0.05F;
                  entityitem.posX = ((float)this.capacitor_random.nextGaussian() * f3);
                  entityitem.posY = ((float)this.capacitor_random.nextGaussian() * f3 + 0.2F);
                  entityitem.posZ = ((float)this.capacitor_random.nextGaussian() * f3);
                  worldIn.spawnEntity(entityitem);
                }
              }
            }
          }
        }
        super.breakBlock(worldIn, pos, state);
      }
    
    
    
}

 

TileEntityCapacitorBasic

Spoiler

package com.zeher.orecraft.client.tileentity.capacitor;

import javax.annotation.Nullable;

import com.zeher.orecraft.OreCraftCore;
import com.zeher.orecraft.core.block.capacitor.BlockCapacitorBasic;
import com.zeher.orecraft.core.net.NetUtils;
import com.zeher.orecraft.core.net.packet.PacketCapacitorBasic;
import com.zeher.trzcore.api.TRZBlockPosition;
import com.zeher.trzcore.api.energy.TRZICapacitor;
import com.zeher.trzcore.api.energy.TRZItemEnergyBase;
import com.zeher.trzcore.core.block.TRZBlockEnergyPipe;
import com.zeher.trzcore.core.item.TRZItemBattery;
import com.zeher.trzcore.core.tileentity.TRZTileEntityCapacitor;

import net.minecraft.block.Block;
import net.minecraft.block.state.IBlockState;
import net.minecraft.client.Minecraft;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Items;
import net.minecraft.inventory.IInventory;
import net.minecraft.inventory.ISidedInventory;
import net.minecraft.inventory.ItemStackHelper;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.network.play.server.SPacketUpdateTileEntity;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.ITickable;
import net.minecraft.util.NonNullList;
import net.minecraft.world.World;

public class TileEntityCapacitorBasic extends TRZTileEntityCapacitor
        implements ISidedInventory, IInventory, ITickable, TRZICapacitor {

    private String custom_name;
    private NonNullList<ItemStack> capacitorItemStacks = NonNullList.<ItemStack>withSize(8, ItemStack.EMPTY);
    private static final int[] slots_top = { 1 };
    private static final int[] slots_bottom = { 2, 1 };
    private static final int[] slots_sides = { 1 };
    public int _power;
    public int _max_power = 20000000;

    public int energy_recieve;
    public int energy_send;

    public int up;
    public int down;
    public int north;
    public int south;
    public int east;
    public int west;

    public byte[] side_cache = { 0, 0, 0, 0, 0, 0 };

    @Override
    public int getSizeInventory() {
        return this.capacitorItemStacks.size();
    }
    
    public void cycleSide(String side){
        if(side.equals("up")){
            int i = getSide("up");
            i = i+1;
            if(i > 2){
                i= 0;
            }
            up = i;
        }
        if(side.equals("down")){
            int i = getSide("down");
            i = i+1;
            if(i > 2){
                i= 0;
            }
            down = i;
        }
        if(side.equals("east")){
            int i = getSide("east");
            i = i+1;
            if(i > 2){
                i= 0;
            }
            east = i;
        }
        if(side.equals("west")){
            int i = getSide("west");
            i = i+1;
            if(i > 2){
                i= 0;
            }
            west = i;
        }
        if(side.equals("north")){
            int i = getSide("north");
            i = i+1;
            if(i > 2){
                i= 0;
            }
            north = i;
        }
        if(side.equals("south")){
            int i = getSide("south");
            i = i+1;
            if(i > 2){
                i= 0;
            }
            south = i;
        }
        Minecraft.getMinecraft().renderGlobal.markBlockRangeForRenderUpdate(pos.getX(), pos.getY(), pos.getZ(), pos.getX(), pos.getY(), pos.getZ());
        world.markBlockRangeForRenderUpdate(pos, pos);
        //world.markAndNotifyBlock(pos, null, world.getBlockState(pos), world.getBlockState(pos), 3);
        /*Block block =  world.getBlockState(pos).getBlock();
        if(block instanceof BlockCapacitorBasic && block != null){
            ((BlockCapacitorBasic)block).updateBlock((TileEntityCapacitorBasic) world.getTileEntity(pos), world, pos);
            block.getActualState(world.getBlockState(pos), world, pos);
        }*/
    }
    
    
    public void setSideNBT(String str, int value) {
        if (str == "up") {
            this.up = value;
        }
        if (str == "down") {
            this.down = value;
        }
        if (str == "north") {
            this.north = value;
        }
        if (str == "south") {
            this.south = value;
        }
        if (str == "east") {
            this.east = value;
        }
        if (str == "west") {
            this.west = value;
        }
        markDirty();
    }

    public int getSide(String str) {
        if (str == "up") {
            return this.up;
        }
        if (str == "down") {
            return this.down;
        }
        if (str == "north") {
            return this.north;
        }
        if (str == "south") {
            return this.south;
        }
        if (str == "east") {
            return this.east;
        }
        if (str == "west") {
            return this.west;
        }
        return 0;
    }

    @Override
    public void update() {
        //BlockCapacitorBasic blockToUpdate = (BlockCapacitorBasic) world.getBlockState(pos).getBlock();
        //blockToUpdate.updateBlock((TileEntityCapacitorBasic) world.getTileEntity(pos), world, pos);
        
        boolean flag = this._power > 0;
        boolean flag1 = false;
        ItemStack stackInput = capacitorItemStacks.get(1);
        ItemStack stackOutput = capacitorItemStacks.get(0);
        if (!this.world.isRemote) {
            if ((isItemPowerStorage(0)) && !(stackOutput.isEmpty()) && (this._power > 0)) {
                if (stackOutput.getItemDamage() > 0) {
                    stackOutput.setItemDamage(stackOutput.getItemDamage() - 1);
                    this._power -= TRZItemEnergyBase.getItemPower(stackOutput.getItem());
                }
            }
            if ((isItemPower(1))
                    && (this._power <= this._max_power - TRZItemEnergyBase.getItemPower(stackInput.getItem()))) {
                if (!stackInput.isItemStackDamageable()) {
                    this._power += TRZItemEnergyBase.getItemPower(stackInput.getItem());

                    flag1 = true;
                    if (!(stackInput.isEmpty())) {
                        stackInput.setCount(stackInput.getCount() - 1);
                        if (stackInput.isEmpty()) {
                            this.capacitorItemStacks.set(1, stackInput.getItem().getContainerItem(stackInput));
                        }
                    }
                } else if (stackInput.getItemDamage() < stackInput.getMaxDamage()) {
                    this._power += TRZItemEnergyBase.getItemPower(stackInput.getItem());
                    this.capacitorItemStacks.set(1,
                            new ItemStack(stackInput.getItem(), stackInput.getCount(), stackInput.getItemDamage() + 1));
                }
            }
            if (flag1) {
                this.update();
                this.markDirty();
            }
        }

        IBlockState state = world.getBlockState(this.pos);
        Block block = state.getBlock();
        if (block instanceof BlockCapacitorBasic) {
            boolean up = ((BlockCapacitorBasic) block).getSide(EnumFacing.UP, world, this.pos);
            boolean down = ((BlockCapacitorBasic) block).getSide(EnumFacing.DOWN, world, pos);
            boolean east = ((BlockCapacitorBasic) block).getSide(EnumFacing.EAST, world, this.pos);
            boolean west = ((BlockCapacitorBasic) block).getSide(EnumFacing.WEST, world, pos);
            boolean north = ((BlockCapacitorBasic) block).getSide(EnumFacing.NORTH, world, this.pos);
            boolean south = ((BlockCapacitorBasic) block).getSide(EnumFacing.SOUTH, world, pos);

            if (up) {
                TRZBlockPosition bp = new TRZBlockPosition(pos);
                bp.orientation = EnumFacing.UP;
                bp.moveForwards(1);
                Block blockUp = world.getBlockState(bp.pos).getBlock();

                if (this.up == 1) {
                    if (blockUp != null) {
                        capacitorItemStacks.set(2, new ItemStack(blockUp));
                    }
                }
                if (this.up == 1) {
                    capacitorItemStacks.set(2, ItemStack.EMPTY);
                }
                if (this.up == 2) {
                    capacitorItemStacks.set(2, new ItemStack(blockUp));
                }
            } else {
                capacitorItemStacks.set(2, ItemStack.EMPTY);
            }

            if (down) {
                TRZBlockPosition bp = new TRZBlockPosition(pos);
                bp.orientation = EnumFacing.DOWN;
                bp.moveForwards(1);
                Block blockDown = world.getBlockState(bp.pos).getBlock();

                if (this.down == 0) {
                    if (blockDown instanceof TRZBlockEnergyPipe) {
                        capacitorItemStacks.set(3, new ItemStack(blockDown));
                    } else {
                        capacitorItemStacks.set(3, ItemStack.EMPTY);
                    }
                }
                if (this.down == 1) {
                    capacitorItemStacks.set(3, ItemStack.EMPTY);
                }
                if (this.down == 2) {
                    capacitorItemStacks.set(3, new ItemStack(blockDown));
                }
            } else {
                capacitorItemStacks.set(3, ItemStack.EMPTY);
            }

            if (east) {
                TRZBlockPosition bp = new TRZBlockPosition(pos);
                bp.orientation = EnumFacing.EAST;
                bp.moveForwards(1);
                Block blockEast = world.getBlockState(bp.pos).getBlock();

                if (this.east == 0) {
                    if (blockEast instanceof TRZBlockEnergyPipe) {
                        capacitorItemStacks.set(4, new ItemStack(blockEast));
                    } else {
                        capacitorItemStacks.set(4, ItemStack.EMPTY);
                    }
                }
                if (this.east == 1) {
                    capacitorItemStacks.set(4, ItemStack.EMPTY);
                }
                if (this.east == 2) {
                    capacitorItemStacks.set(4, new ItemStack(blockEast));
                }
            } else {
                capacitorItemStacks.set(4, ItemStack.EMPTY);
            }

            if (west) {
                TRZBlockPosition bp = new TRZBlockPosition(pos);
                bp.orientation = EnumFacing.WEST;
                bp.moveForwards(1);
                Block blockWest = world.getBlockState(bp.pos).getBlock();

                if (this.west == 0) {
                    if (blockWest instanceof TRZBlockEnergyPipe) {
                        capacitorItemStacks.set(5, new ItemStack(blockWest));
                    } else {
                        capacitorItemStacks.set(5, ItemStack.EMPTY);
                    }
                }
                if (this.west == 1) {
                    capacitorItemStacks.set(5, ItemStack.EMPTY);
                }
                if (this.west == 2) {
                    capacitorItemStacks.set(5, new ItemStack(blockWest));
                } 
            } else {
                capacitorItemStacks.set(5, ItemStack.EMPTY);
            }

            if (north) {
                TRZBlockPosition bp = new TRZBlockPosition(pos);
                bp.orientation = EnumFacing.NORTH;
                bp.moveForwards(1);
                Block blockNorth = world.getBlockState(bp.pos).getBlock();

                if (this.north == 0) {
                    if (blockNorth instanceof TRZBlockEnergyPipe) {
                        capacitorItemStacks.set(6, new ItemStack(blockNorth));
                    } else {
                        capacitorItemStacks.set(6, ItemStack.EMPTY);
                    }
                }
                if (this.north == 1) {
                    capacitorItemStacks.set(6, ItemStack.EMPTY);
                }
                if (this.north == 2) {
                    capacitorItemStacks.set(6, new ItemStack(blockNorth));
                }
            } else {
                capacitorItemStacks.set(6, ItemStack.EMPTY);
            }

            if (south) {
                TRZBlockPosition bp = new TRZBlockPosition(pos);
                bp.orientation = EnumFacing.SOUTH;
                bp.moveForwards(1);
                Block blockSouth = world.getBlockState(bp.pos).getBlock();

                if (this.south == 0) {
                    if (blockSouth instanceof TRZBlockEnergyPipe) {
                        capacitorItemStacks.set(7, new ItemStack(blockSouth));
                    } else {
                        capacitorItemStacks.set(7, ItemStack.EMPTY);
                    }
                }
                if (this.south == 1) {
                    capacitorItemStacks.set(7, ItemStack.EMPTY);
                }
                if (this.south == 2) {
                    capacitorItemStacks.set(7, new ItemStack(blockSouth));
                } 
            } else {
                capacitorItemStacks.set(7, ItemStack.EMPTY);
            }
        }
    }

    private boolean isItemPowerStorage(int index) {
        if (!(this.capacitorItemStacks.get(index).isEmpty())) {
            return this.capacitorItemStacks.get(index).getItem() instanceof TRZItemBattery;
        }
        return false;
    }

    public int getPowerReaminingScaled(int par1) {
        return this._power * par1 / this._max_power;
    }

    public ItemStack getStackInSlot(int i) {
        return this.capacitorItemStacks.get(i);
    }

    @Override
    public ItemStack decrStackSize(int index, int count) {
        return ItemStackHelper.getAndSplit(this.capacitorItemStacks, index, count);
    }

    @Override
    public ItemStack removeStackFromSlot(int index) {
        return ItemStackHelper.getAndRemove(this.capacitorItemStacks, index);
    }

    public boolean isItemPower(int index) {
        return TRZItemEnergyBase.getItemPower(this.capacitorItemStacks.get(index).getItem()) > 0;
    }

    @Override
    public void setInventorySlotContents(int index, ItemStack stack) {
        ItemStack itemstack = (ItemStack) this.capacitorItemStacks.get(index);
        boolean flag = !stack.isEmpty() && stack.isItemEqual(itemstack)
                && ItemStack.areItemStackTagsEqual(stack, itemstack);
        this.capacitorItemStacks.set(index, stack);
        if (stack.getCount() > this.getInventoryStackLimit()) {
            stack.setCount(this.getInventoryStackLimit());
        }
    }

    public String getName() {
        return this.hasCustomName() ? this.custom_name : "Basic Capacitor";
    }

    public boolean hasCustomName() {
        return (this.custom_name != null) && (this.custom_name.length() > 0);
    }

    public void setGuiDisplayName(String par1Str) {
        this.custom_name = par1Str;
    }

    @Override
    public int getInventoryStackLimit() {
        return 64;
    }

    @Override
    public boolean isUsableByPlayer(EntityPlayer player) {
        return player.getDistanceSq(this.pos) <= 64.0D;
    }

    public void openInventory() {
    }

    public void closeInventory() {
    }

    @Override
    public boolean isItemValidForSlot(int i, ItemStack itemstack) {
        return true;
    }

    public void readFromNBT(NBTTagCompound compound) {
        this.capacitorItemStacks = NonNullList.<ItemStack>withSize(this.getSizeInventory(), ItemStack.EMPTY);
        ItemStackHelper.loadAllItems(compound, this.capacitorItemStacks);
        this._power = compound.getInteger("power");
        
        System.out.println("READ (NBT) - " + compound.getInteger("DirUp") + " " + compound.getInteger("DirDown") + " " + compound.getInteger("DirNorth") + " " + compound.getInteger("DirSouth") + " " + compound.getInteger("DirEast") + " " + compound.getInteger("DirWest"));
        this.up = compound.getInteger("DirUp");
        this.down = compound.getInteger("DirDown");
        this.north = compound.getInteger("DirNorth");
        this.south = compound.getInteger("DirSouth");
        this.east = compound.getInteger("DirEast");
        this.west = compound.getInteger("DirWest");
        
        if (compound.hasKey("custom_name", 8)) {
            this.custom_name = compound.getString("custom_name");
        }
        super.readFromNBT(compound);
    }

    public NBTTagCompound writeToNBT(NBTTagCompound compound) {
        ItemStackHelper.saveAllItems(compound, this.capacitorItemStacks);
        compound.setInteger("power", this._power);
        
        System.out.println("WRITE (NBT)" + up + " " + down + " " + north + " " + south + " " + east + " " + west);
        compound.setInteger("DirUp", this.up);
        compound.setInteger("DirDown", this.down);
        compound.setInteger("DirNorth", this.north);
        compound.setInteger("DirSouth", this.south);
        compound.setInteger("DirEast", this.east);
        compound.setInteger("DirWest", this.west);
        
        if (this.hasCustomName()) {
            compound.setString("custom_name", this.custom_name);
        }
        return super.writeToNBT(compound);
    }
    
    @Nullable
    public SPacketUpdateTileEntity getUpdatePacket()
    {
        NBTTagCompound tag = new NBTTagCompound();
        tag.setInteger("DirUp", this.up);
        tag.setInteger("DirDown", this.down);
        tag.setInteger("DirNorth", this.north);
        tag.setInteger("DirSouth", this.south);
        tag.setInteger("DirEast", this.east);
        tag.setInteger("DirWest", this.west);
        
        System.out.println("Sending Packet - " + getSide("up") + " " + getSide("down") + " " + getSide("north") + " " + getSide("south") + " " + getSide("east") + " " + getSide("west"));
        writeToNBT(tag);
        return new SPacketUpdateTileEntity(pos, 0, tag);
    }

    public void onDataPacket(net.minecraft.network.NetworkManager net, net.minecraft.network.play.server.SPacketUpdateTileEntity pkt)
    {
        NBTTagCompound tag = pkt.getNbtCompound();
        int a = tag.getInteger("DirUp");
        int b = tag.getInteger("DirDown"); 
        int c = tag.getInteger("DirNorth");
        int d = tag.getInteger("DirSouth");
        int e = tag.getInteger("DirEast");
        int f = tag.getInteger("DirWest");
        System.out.println("Packet Received - " + a + " " + b + " " + c + " " + d + " " + e + " " + f);
        //handleUpdateTag(tag);
        this.up = a;
        this.down = b;
        this.north = c; 
        this.south = d;
        this.east = e;
        this.west = f;
    }
    
    public void handleUpdateTag(NBTTagCompound tag)
    {
        this.readFromNBT(tag);
    }
    
    public NBTTagCompound getUpdateTag()
    {
        return writeToNBT(new NBTTagCompound());
    }
    
    @Override
    public boolean isEmpty() {
        for (ItemStack itemstack : this.capacitorItemStacks) {
            if (!itemstack.isEmpty()) {
                return false;
            }
        }

        return true;
    }

    @Override
    public void openInventory(EntityPlayer player) {
        // TODO Auto-generated method stub

    }

    @Override
    public void closeInventory(EntityPlayer player) {
        // TODO Auto-generated method stub

    }

    public int getField(int id) {
        switch (id) {
        case 0:
            return this._power;
        default:
            return 0;
        }
    }

    public void setField(int id, int value) {
        switch (id) {
        case 0:
            this._power = value;
            break;
        }
    }

    public int getFieldCount() {
        return 1;
    }

    @Override
    public void clear() {
        // TODO Auto-generated method stub

    }

    @Override
    public int[] getSlotsForFace(EnumFacing side) {
        // TODO Auto-generated method stub
        return side.UP != null ? slots_top : side.DOWN != null ? slots_bottom : slots_sides;
    }

    @Override
    public boolean canInsertItem(int index, ItemStack itemStackIn, EnumFacing direction) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public boolean canExtractItem(int index, ItemStack stack, EnumFacing direction) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public boolean hasPower() {
        return this._power > 0;
    }

    @Override
    public int getPower() {
        return _power;
    }

    @Override
    public int getMaxSend() {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public int getMaxReceive() {
        // TODO Auto-generated method stub
        return 0;
    }

}
 

 

PacketCapacitorBasic

Spoiler

package com.zeher.orecraft.core.net.packet;

import com.zeher.orecraft.client.tileentity.capacitor.TileEntityCapacitorBasic;
import com.zeher.orecraft.core.block.capacitor.BlockCapacitorBasic;

import io.netty.buffer.ByteBuf;
import net.minecraft.util.IThreadListener;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.WorldServer;
import net.minecraftforge.fml.common.network.ByteBufUtils;
import net.minecraftforge.fml.common.network.simpleimpl.IMessage;
import net.minecraftforge.fml.common.network.simpleimpl.IMessageHandler;
import net.minecraftforge.fml.common.network.simpleimpl.MessageContext;

public class PacketCapacitorBasic implements IMessage {
    
    private static String side;
    private static BlockPos pos;
    private static boolean cycle;
    private static int value;
    
    public PacketCapacitorBasic(){
        
    }
    
    public PacketCapacitorBasic(String side, BlockPos pos){
        this.side = side;
        this.pos = pos;
        cycle = true;
    }
    
    public PacketCapacitorBasic(String side, BlockPos pos, boolean cycle, int value){
        this.side = side;
        this.pos = pos;
        this.cycle = cycle;
        this.value = value;
    }
    
    @Override
    public void fromBytes(ByteBuf buf) {
        side = ByteBufUtils.readUTF8String(buf);
    }

    @Override
    public void toBytes(ByteBuf buf) {
        ByteBufUtils.writeUTF8String(buf, side);
    }
    
    public static class Handler implements IMessageHandler<PacketCapacitorBasic, IMessage> {

        @SuppressWarnings("unused")
        @Override
        public IMessage onMessage(final PacketCapacitorBasic message, final MessageContext ctx) {
            TileEntityCapacitorBasic tileEntity = (TileEntityCapacitorBasic)ctx.getServerHandler().playerEntity.world.getTileEntity(pos);
            BlockCapacitorBasic block = (BlockCapacitorBasic)ctx.getServerHandler().playerEntity.world.getBlockState(pos).getBlock();
            System.out.println("Packet Received: " + tileEntity.getName() + " BlockPos: " + pos);
            if(tileEntity != null){
                System.out.println("TileEntity is not null: " + "Cycle side: " + side);
                if(cycle){
                    tileEntity.cycleSide(side);
                    //tileEntity.getUpdatePacket();
                    //ctx.getServerHandler().playerEntity.world.setBlockState(pos, ctx.getServerHandler().playerEntity.world.getBlockState(pos));
                } else {
                    tileEntity.setSideNBT(side, value);
                }
            } 
            ctx.getServerHandler().playerEntity.world.markBlockRangeForRenderUpdate(pos, pos);
            if(tileEntity == null){
            System.out.println("TileEntity is NULL!. Report to mod author!");
            }
            return null;
        }
    
    }

}
 

 If theres anything else you want to see please ask :P 

Link to comment
Share on other sites

2 hours ago, Zeher_Monkey said:

Also setBlock()? What for...

I said you need to call something setBlock/setBlockStatee calls.

Okay, if you can't understand.. I meant World::notifyBlockUpdate.

Edited by Abastro

I. Stellarium for Minecraft: Configurable Universe for Minecraft! (WIP)

II. Stellar Sky, Better Star Rendering&Sky Utility mod, had separated from Stellarium.

Link to comment
Share on other sites

The nature of this issue as you describe it sounds like you aren't calling markDirty on the TileEntity when you need to. The purpose of that method is to tell the game that the TE needs to be saved to NBT, so it needs to be called anywhere that the TE's data gets changed. But it looks like you're not calling it in cycleSide, which changes the ints in the TE.

Link to comment
Share on other sites

4 minutes ago, Abastro said:

I said you need to call something setBlock/setBlockStatee calls.

Okay, if you can't understand.. I meant World::notifyBlockUpdate.

Yeah i figured that out after, dont worry :P 

 

3 minutes ago, Jay Avery said:

The nature of this issue as you describe it sounds like you aren't calling markDirty on the TileEntity when you need to. The purpose of that method is to tell the game that the TE needs to be saved to NBT, so it needs to be called anywhere that the TE's data gets changed. But it looks like you're not calling it in cycleSide, which changes the ints in the TE.

Ill try that thanks :) 

 

--Edit--

So simple. I don't know how i missed that :D. Thanks Jay. I think its sorted for now, until i mess up again :P Cheers everyone who helped out :) 

Edited by Zeher_Monkey
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.