Jump to content

[1.12.2] Blockstate


Zeher_Monkey

Recommended Posts

So i want to display on the block the power level with different textures. I have the textures, the blockstates and everything registered and ready to go. If i manually enter the different values into 

getActualState(IBlockState state, IBlockAccess world, BlockPos pos);

Then the block switches the textures over no problem.

However, i am using the RedstoneFlux API, and when i try to access the power level from the

getActualState(IBlockState state, IBlockAccess world, BlockPos pos);

method, it always returns 0. No matter what i try. I have also tried using the

getExtendedState(IBlockState state, IBlockAccess world, BlockPos pos);

method as well to no avail.

 

TileEntitySynthesiser.java

Spoiler

private EnergyStorage storage = new EnergyStorage(140000, 2000);

 


    @Override
    public int getEnergyStored(EnumFacing from) {
        return this.storage.getEnergyStored();
    }

    @Override
    public int getMaxEnergyStored(EnumFacing from) {
        return this.storage.getMaxEnergyStored();
    }

    @Override
    public boolean canConnectEnergy(EnumFacing from) {
        if (from.equals(EnumFacing.DOWN)) {
            return true;
        }
        return false;
    }

    @Override
    public int extractEnergy(EnumFacing from, int maxExtract, boolean simulate) {
        return this.storage.extractEnergy(maxExtract, simulate);
    }

    @Override
    public int receiveEnergy(EnumFacing from, int maxReceive, boolean simulate) {
        this.markDirty();
        return this.storage.receiveEnergy(maxReceive, simulate);
    }

 

 

 

BlockSynthesiser.java

Spoiler

package com.zeher.zehercraft.processing.core.block;

import com.zeher.zehercraft.processing.core.tile.TileEntitySynthesiser;
import com.zeher.zeherlib.mod.block.ModBlockContainer;

import net.minecraft.block.material.Material;
import net.minecraft.block.properties.IProperty;
import net.minecraft.block.properties.PropertyInteger;
import net.minecraft.block.state.BlockStateContainer;
import net.minecraft.block.state.IBlockState;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.SoundEvents;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.EnumBlockRenderType;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.EnumHand;
import net.minecraft.util.SoundCategory;
import net.minecraft.util.math.AxisAlignedBB;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.IBlockAccess;
import net.minecraft.world.World;

public class BlockSynthesiser extends ModBlockContainer {
    
    public static final PropertyInteger ENERGY = PropertyInteger.create("energy", 0, 14);
    
    public BlockSynthesiser(String name, Material material, String tool, int harvest, int hardness, int resistance, CreativeTabs tab) {
        super(name, material, tool, harvest, hardness, resistance, tab);
        
        this.setDefaultState(this.blockState.getBaseState().withProperty(ENERGY, 0));
    }

    @Override
    public boolean isFullCube(IBlockState state) {
        return false;
    }

    @Override
    public boolean isOpaqueCube(IBlockState state) {
        return false;
    }

    @Override
    public void onBlockClicked(World worldIn, BlockPos pos, EntityPlayer playerIn) {
        TileEntitySynthesiser tile = (TileEntitySynthesiser) worldIn.getTileEntity(pos);
        if (!tile.getStackInSlot(0).isEmpty()) {
            playerIn.inventory.addItemStackToInventory(tile.getStackInSlot(0));
            worldIn.playSound(pos.getX(), pos.getY(), pos.getZ(), SoundEvents.ENTITY_ITEM_PICKUP, SoundCategory.BLOCKS, 0.2F, 2F, false);
            
            System.out.println("__" + tile.getEnergyStored(EnumFacing.DOWN));
        }
    }

    @Override
    public boolean onBlockActivated(World worldIn, BlockPos pos, IBlockState state, EntityPlayer playerIn, EnumHand hand, EnumFacing facing, float hitX, float hitY, float hitZ) {
        TileEntitySynthesiser tile = (TileEntitySynthesiser) worldIn.getTileEntity(pos);
        if ((!playerIn.getHeldItem(hand).isEmpty()) && (tile.getStackInSlot(0).isEmpty())) {
            ItemStack stack = playerIn.getHeldItem(hand);

            worldIn.playSound(pos.getX(), pos.getY(), pos.getZ(), SoundEvents.ENTITY_ITEM_PICKUP, SoundCategory.BLOCKS, 0.2F, 0F, false);
            
            tile.setInventorySlotContents(0, stack.copy());
            stack.shrink(1);
        }
        return true;
    }

    @Override
    public boolean hasTileEntity(IBlockState state) {
        return true;
    }

    @Override
    public TileEntity createNewTileEntity(World worldIn, int meta) {
        return new TileEntitySynthesiser();
    }

    @Override
    public EnumBlockRenderType getRenderType(IBlockState state) {
        return EnumBlockRenderType.MODEL;
    }

    @Override
    public AxisAlignedBB getBoundingBox(IBlockState state, IBlockAccess source, BlockPos pos) {
        return new AxisAlignedBB(0.05D, 0.0D, 0.05D, 0.95D, 1D, 0.95D);
    }
    
    @Override
    public int getMetaFromState(IBlockState state) {
        return 0;
    }
    
    @Override
    protected BlockStateContainer createBlockState() {
        return new BlockStateContainer(this, new IProperty[] { ENERGY });
    }
    
    public IBlockState getExtendedState(IBlockState state, IBlockAccess world, BlockPos pos) {
        TileEntitySynthesiser tile = (TileEntitySynthesiser) world.getTileEntity(pos);
        
        return state.withProperty(ENERGY, (tile.getEnergyStored(EnumFacing.DOWN) / 10000));
    }
}

 

Help would be greatly apprecieted :P 

Link to comment
Share on other sites

Just because you have a TE doesn't mean that its data is automatically sent to the client.

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

9 hours ago, diesieben07 said:

Syncing TE data to the client:

 

Thank you very much. I remembered I have used this previously but did not fully understand it then. I understand it better now, and have made some head-way. I have managed to get the change implemented using a Container and the methods suggested above. However when the

World::notifyBlockUpdate()

is called, I am seeing the update with WAILA (and the F3 menu), but the block itself is still requiring me to actually place a block next to it to update the texture / model.

 

Added to my TE class:

Spoiler

@Override
    public int receiveEnergy(EnumFacing from, int maxReceive, boolean simulate) {
        this.markDirty();
        world.notifyBlockUpdate(pos, world.getBlockState(pos), world.getBlockState(pos), 3);
        return this.storage.receiveEnergy(maxReceive, simulate);
    }
    
    public Container createContainer(InventoryPlayer playerInventory, EntityPlayer playerIn) {
        return new ContainerSynthesiser(playerInventory, this);
    }

 

/**
     * Called on the client with whatever tag was returned from ::getUpdateTag()
     */
    @Override
    public void handleUpdateTag(NBTTagCompound tag) {
        this.storage.readFromNBT(tag);
        
        this.readFromNBT(tag);
    }
    
    /**
     * Returns the data that should be sent to the client on initial chunk load.
     */
    @Override
    public NBTTagCompound getUpdateTag() {
        NBTTagCompound supertag = super.getUpdateTag();
        
        this.storage.writeToNBT(supertag);
        
        return supertag;
    }

    /**
     * Called to produce a subsequent packet for update.
     */
    @Override
    public SPacketUpdateTileEntity getUpdatePacket() {
        NBTTagCompound tag = this.getUpdateTag();
        this.writeToNBT(tag);
        return new SPacketUpdateTileEntity(this.pos, 0, tag);
    }

    @Override
    public void onDataPacket(NetworkManager net, SPacketUpdateTileEntity pkt) {
        NBTTagCompound tag = pkt.getNbtCompound();
        
        this.handleUpdateTag(tag);
    }

 

EDIT: I am using the container because I have added a GUI to the block.

Edited by Zeher_Monkey
Link to comment
Share on other sites

1 minute ago, diesieben07 said:

Writing the energy storage would be fine. But you are sending your entire TE data.

Yeah I just saw that line. I don't think it was having much of an effect, the only thing in that data (at least the read/write to NBT data) was a single ItemStack.

 

New code:

Spoiler

@Override
    public void handleUpdateTag(NBTTagCompound tag) {
        this.storage.setEnergyStored(tag.getInteger("energy"));
        
        this.readFromNBT(tag);
    }
    
    @Override
    public NBTTagCompound getUpdateTag() {
        NBTTagCompound supertag = super.getUpdateTag();
        return supertag;
    }
    
    @Override
    public SPacketUpdateTileEntity getUpdatePacket() {
        NBTTagCompound tag = this.getUpdateTag();
        
        tag.setInteger("energy", this.storage.getEnergyStored());
        
        return new SPacketUpdateTileEntity(this.pos, 0, tag);
    }

    @Override
    public void onDataPacket(NetworkManager net, SPacketUpdateTileEntity pkt) {
        NBTTagCompound tag = pkt.getNbtCompound();
        
        world.notifyBlockUpdate(pos, world.getBlockState(pos), world.getBlockState(pos), 3);
        
        this.handleUpdateTag(tag);
    }

 

Link to comment
Share on other sites

7 minutes ago, diesieben07 said:
  • Why are you not returning any data in getUpdateTag?
  • getUpdatePacket and onDataPacket should be symmetrical. onDataPacket should read data in the same way that getUpdatePacket writes it. Your case works, but its a little strange that onDataPacket delegates to handleUpdateTag, but getUpdatePacket does not delegate to getUpdateTag.
  • Same goes for getUpdateTag and handleUpdateTag. They should be symmetrical to make sense.

 

Just updated the code again. Tried to implement these. ^

 

Spoiler

@Override
    public void handleUpdateTag(NBTTagCompound tag) {
        this.storage.setEnergyStored(tag.getInteger("energy"));
        
        this.readFromNBT(tag);
    }
    
    @Override
    public NBTTagCompound getUpdateTag() {
        NBTTagCompound tag = super.getUpdateTag();

        tag.setInteger("energy", this.storage.getEnergyStored());
        
        return tag;
    }
    
    @Override
    public SPacketUpdateTileEntity getUpdatePacket() {
        NBTTagCompound tag = this.getUpdateTag();
        return new SPacketUpdateTileEntity(this.pos, 0, tag);
    }

    @Override
    public void onDataPacket(NetworkManager net, SPacketUpdateTileEntity pkt) {
        NBTTagCompound tag = pkt.getNbtCompound();
        this.updateBlock(0);
        this.handleUpdateTag(tag);
    }

This system is mostly new to me, like I said I have used it before but did not really understand how to fully utilise it.

Edited by Zeher_Monkey
Link to comment
Share on other sites

4 minutes ago, diesieben07 said:

Looks fine now.

Interesting. Because it has just stopped functioning at all. No updates client or server.

 

Edit:

It is flickering when i access the GUI. If i put an item into the slot it disappears  but i can still take it out

Edited by Zeher_Monkey
Link to comment
Share on other sites

4 minutes ago, diesieben07 said:

Please clarify what you mean by "no updates on the server". Have you used the debugger? Are the methods called?

Sorry, the methods are being called, when i access the GUI, the block flickers for less than a second into the correct state. Textures and the ENERGY Property show up. Once inside the GUI, the block returns to the incorrect state of having no energy. So from ENERGY = 14 to ENERGY =  0. Inside the GUI the power bar texture still renders. When inserting an Item, it goes into the slot and disappears from view, but when i click to get it back out of the slot, it pops back into existence. The block also renders the item in-world, that acts the same as the slot.

 

EDIT:

The only thing i really have changed is moving where i set the data from

::getUpdatePacket()

to

::getUpdateTag()

 

Edited by Zeher_Monkey
Link to comment
Share on other sites

8 minutes ago, diesieben07 said:

That is very strange behavior and almost impossible to debug remotely.

Okay, found the culprit.

 

::readFromNBT()

this.storage = storage.readFromNBT(compound);

 and 

::writeToNBT():

this.storage.writeToNBT(compound);

 

To save the data to NBT for regular stuff.

The NBT must have been de-synced because i left these lines in.

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.