Jump to content

[Solved]Problem with unsaving ItemBlock's NBT


3059673

Recommended Posts

I have created an ItemBlock to save NBT data to it then someone breaks its block.

Block :

public class ChairBlock extends BlockContainer{


int renderType ;
private int x,y,z;

public ChairBlock(int par1, Material par2Material ) {
	super(par1, par2Material);
	this.setResistance(120F);
	this.setHardness(120F);
	this.setBlockUnbreakable();
}

@Override
public boolean renderAsNormalBlock() {
	return false;
}



public void registerIcons(IconRegister par1IconRegister)
    {
        this.blockIcon = par1IconRegister.registerIcon("rs:Peripheral");
    }

public boolean hasTileEntity(){
	return true;

}

public boolean isOpaqueCube(){
	return false;

}


@Override
public boolean isBlockSolid(IBlockAccess par1IBlockAccess, int par2, int par3, int par4, int par5) {
	return true;
}
@Override
public int getRenderType() {
	return renderType;
}

public void setRenderType(int id){
	this.renderType = id;
}


@Override
    public boolean onBlockActivated(World world, int x, int y, int z, EntityPlayer player, int side, float hitX, float hitY, float hitZ)
    {
        if(world.isRemote)
            return true;
        
        MovingObjectPosition hit = RayTracer.retraceBlock(world, player, x, y, z);
        RussyPeripheral tile = (RussyPeripheral) world.getBlockTileEntity(x, y, z);
        if(!player.isSneaking()){
        	if(hit == null)
                return false;
            if(Utils.isHoldingUsableWrench(player) && tile.Username.equals(player.username)){
            	if(tile.blockMetadata + 1 > 3){
            		world.setBlockMetadataWithNotify(x, y, z, 0, 2);
            	}else{
            		world.setBlockMetadataWithNotify(x, y, z, tile.blockMetadata+1, 2);
            	}
            	return true;
            }
            if(hit.subHit == 1 && tile.getInstalledUpgrades().contains(tile.RVALVE_UPGRADE)){
            	tile.setValve1Activated();
            }
            
        }else{
        	if(Utils.isHoldingUsableWrench(player) && tile.Username.equals(player.username) && !player.capabilities.isCreativeMode){
        		this.removeBlockByPlayer(world, player, x, y, z);
        	}
        }
        
        
        return true;
    }

@Override
    public ItemStack getPickBlock(MovingObjectPosition mop, World world, int x, int y, int z)
    {
	RussyPeripheral tile = (RussyPeripheral) world.getBlockTileEntity(x, y, z);
	ItemStack stack = new ItemStack(this,1);
	if(!stack.hasTagCompound())
            stack.setTagCompound(new NBTTagCompound());
        stack.getTagCompound().setString("owner", tile.Username);
        stack.getTagCompound().setBoolean("canWaitForKey",tile.canWaitForKey);
        if(tile.eniquepass != null) stack.getTagCompound().setString("pass", tile.eniquepass);
        stack.getTagCompound().setInteger("valve1Active", tile.valve1state);
        stack.getTagCompound().setBoolean(tile.RVALVE_UPGRADE, tile.rightValveUpgrade);
	for(int i = 0;i<MyTickHandler.loadedRussyTiles.size();i++){
		if(MyTickHandler.loadedRussyTiles.get(i).xCoord == tile.xCoord
				&& MyTickHandler.loadedRussyTiles.get(i).yCoord == tile.yCoord
				&& MyTickHandler.loadedRussyTiles.get(i).zCoord == tile.zCoord){
			MyTickHandler.loadedRussyTiles.remove(i);
		}
	}
	return stack;
    }
    

@Override
public TileEntity createNewTileEntity(World world){
	RussyPeripheral tile = new RussyPeripheral();
	tile.setUsername("NONE");
	MyTickHandler.loadedRussyTiles.add(tile);
	return tile;

}

public TileEntity createTileEntity(World world, int metadata)
    {
	RussyPeripheral tile = new RussyPeripheral();
	tile.setUsername("NONE");
	MyTickHandler.loadedRussyTiles.add(tile);
	return tile;
    }


@Override
    public boolean removeBlockByPlayer(World world, EntityPlayer player, int x, int y, int z)
    {
        if(!player.capabilities.isCreativeMode && !world.isRemote)
            dropBlockAsItem(world, x, y, z, world.getBlockMetadata(x, y, z), 0);
        
        world.setBlockToAir(x, y, z);
        return true;
    }


public void onBlockPlacedBy(World par1World, int x, int y, int z, EntityLivingBase par5EntityLiving, ItemStack stack){

    
    
}




public void addCollisionBoxesToList(World par1World, int par2, int par3, int par4, AxisAlignedBB par5AxisAlignedBB, List par6List, Entity par7Entity)
    {
	RussyPeripheral tile = (RussyPeripheral) par1World.getBlockTileEntity(par2, par3, par4);
	int ro = 0;
	if(tile.worldObj != null){
	    ro = tile.getBlockMetadata();
	}

	float xMin,yMin,zMin,xMax,yMax,zMax;
	xMin = 0.12500001F;
	yMin = 0.0F;
	zMin = 0.10500001F;
	xMax = 0.9049993F;
	yMax = 0.8949993F;
	zMax = 0.88999933F;
	if(ro == 1){
		this.setBlockBounds(0.12500001F, 0.0F,0.10500001F, 0.9049993F, 0.8949993F, 0.88999933F);
		super.addCollisionBoxesToList(par1World, par2, par3, par4, par5AxisAlignedBB, par6List, par7Entity);
		if(tile.getInstalledUpgrades().contains(tile.RVALVE_UPGRADE)){
			this.setBlockBounds(0.034999996F, 0.17499997F, 0.54999965F, 0.13500002F, 0.39999977F, 0.7949994F);
			super.addCollisionBoxesToList(par1World, par2, par3, par4, par5AxisAlignedBB, par6List, par7Entity);
		}
	}else if(ro == 2){
		this.setBlockBounds(0.105000004F, 0.0F, 0.13000001F, 0.88999933F, 0.8949993F, 0.9149993F);
		super.addCollisionBoxesToList(par1World, par2, par3, par4, par5AxisAlignedBB, par6List, par7Entity);
		if(tile.getInstalledUpgrades().contains(tile.RVALVE_UPGRADE)){
		    this.setBlockBounds(0.19999994F, 0.16499998F, 0.044999994F, 0.44999975F, 0.39999977F, 0.145000001F);
		    super.addCollisionBoxesToList(par1World, par2, par3, par4, par5AxisAlignedBB, par6List, par7Entity);
		}
	}else if(ro == 3){
		this.setBlockBounds(0.085F, 0.0F, 0.10500001F, 0.87499934F, 0.8949993F, 0.8949993F);
		super.addCollisionBoxesToList(par1World, par2, par3, par4, par5AxisAlignedBB, par6List, par7Entity);
		if(tile.getInstalledUpgrades().contains(tile.RVALVE_UPGRADE)){
		    this.setBlockBounds(0.85499936F, 0.16499998F, 0.19999999F, 0.95499927F, 0.3949998F, 0.44999975F);
		    super.addCollisionBoxesToList(par1World, par2, par3, par4, par5AxisAlignedBB, par6List, par7Entity);
		}
	}else if(ro == 0){
		this.setBlockBounds(0.11000001F, 0.0F, 0.095F, 0.88999933F, 0.8949993F, 0.87999934F);
		super.addCollisionBoxesToList(par1World, par2, par3, par4, par5AxisAlignedBB, par6List, par7Entity);
		if(tile.getInstalledUpgrades().contains(tile.RVALVE_UPGRADE)){
		    this.setBlockBounds(0.54999965F, 0.16499998F, 0.86499935F, 0.7999994F, 0.39999977F, 0.96499926F);
		    super.addCollisionBoxesToList(par1World, par2, par3, par4, par5AxisAlignedBB, par6List, par7Entity);
		}
	}


	this.setBlockBounds(0, 0, 0, 1, 1, 1);
    }


public MovingObjectPosition collisionRayTrace(World world, int x, int y, int z, Vec3 start, Vec3 end)
  {
    RussyPeripheral theTile = (RussyPeripheral)world.getBlockTileEntity(x, y, z);
    if (theTile == null) {
      return null;
    }
    List cuboids = new LinkedList();
    theTile.addTraceableCuboids(cuboids);
    return RayTracer.instance().rayTraceCuboids(new Vector3(start), new Vector3(end), cuboids, new BlockCoord(x, y, z), this);
  }

  @SideOnly(Side.CLIENT)
  @ForgeSubscribe
  public void onBlockHighlight(DrawBlockHighlightEvent event) {
    if ((event.target.typeOfHit == EnumMovingObjectType.TILE) && (event.player.worldObj.getBlockTileEntity(event.target.blockX, event.target.blockY, event.target.blockZ) instanceof RussyPeripheral))
    {
      RayTracer.retraceBlock(event.player.worldObj, event.player, event.target.blockX, event.target.blockY, event.target.blockZ);
    }
  }


public AxisAlignedBB getSelectedBoundingBoxFromPool(World par1World, int par2, int par3, int par4)
    {
        return super.getSelectedBoundingBoxFromPool(par1World, par2, par3, par4);
    }






}

 

ItemBlock

public class RussyPeripheralAsItem extends ItemBlock{

public RussyPeripheralAsItem(int par1) {
	super(par1);
	this.setHasSubtypes(false);
	this.setUnlocalizedName(StatCollector.translateToLocal("rs.item.RussyP"));
	this.setFull3D();

}

@Override
    public boolean placeBlockAt(ItemStack stack, EntityPlayer player, World world, int x, int y, int z, int side, float hitX, float hitY, float hitZ, int metadata)
    {
        if(super.placeBlockAt(stack, player, world, x, y, z, side, hitX, hitY, hitZ, metadata))
        {
        	int rotation = MathHelper.floor_double((double)((player.rotationYaw * 4F) / 360F) + 2.5D) & 3;
        	world.setBlock(x, y, z, this.itemID, rotation, 2);
    	    RussyPeripheral tile = (RussyPeripheral) world.getBlockTileEntity(x, y, z);
    	    if(player instanceof EntityPlayer){
    	    	tile.Username = player.username;
    	    }
    	    if(stack.stackTagCompound != null){
    	        tile.readFromNBT(stack.stackTagCompound);
    	    }
    	    player.setCurrentItemOrArmor(0, null);
    	    
        }
        return false;
    }
    
    
@SideOnly(Side.CLIENT)
    @Override
    public void addInformation(ItemStack stack, EntityPlayer player, List list, boolean extended)
    {
        if(!StringHelper.isShiftKeyDown()){
        	list.add(StringHelper.shiftForInfo);
        }else{
        	if(stack.hasTagCompound()){
        		String Username = EnumChatFormatting.YELLOW+stack.stackTagCompound.getString("owner");
                String eniquepass = EnumChatFormatting.YELLOW+stack.stackTagCompound.getString("pass");
                boolean rightValveUpgrade = stack.stackTagCompound.getBoolean(RussyPeripheral.RVALVE_UPGRADE);
            	list.add(EnumChatFormatting.AQUA+"Owner: "+Username);
            	list.add(EnumChatFormatting.AQUA+"Eniquepass: "+eniquepass);
            	list.add(EnumChatFormatting.AQUA+"Installed modules:");
            	if(rightValveUpgrade) list.add(EnumChatFormatting.RED+"   - Right Valve");
        	}
        	
        }
    }

}

 

But then I try to break my block, my itemBlock drops well, but its NBT is empty. (I check NBT ingame with NBTEditor)

What am I doing wrong ?

Link to comment
Share on other sites

Well...you don't do anything when someone breaks the block.

 

@Override

    public boolean removeBlockByPlayer(World world, EntityPlayer player, int x, int y, int z)

    {

        if(!player.capabilities.isCreativeMode && !world.isRemote)

            dropBlockAsItem(world, x, y, z, world.getBlockMetadata(x, y, z), 0);

       

        world.setBlockToAir(x, y, z);

        return true;

    }

Link to comment
Share on other sites

Well...you don't do anything when someone breaks the block.

 

@Override

    public boolean removeBlockByPlayer(World world, EntityPlayer player, int x, int y, int z)

    {

        if(!player.capabilities.isCreativeMode && !world.isRemote)

            dropBlockAsItem(world, x, y, z, world.getBlockMetadata(x, y, z), 0);

       

        world.setBlockToAir(x, y, z);

        return true;

    }

 

Hi

 

What do you mean exactly, "ItemBlock NBT"?  Items don't have NBT because they are just a definition of what each Item type is.

Do you mean NBT of the ItemStack that holds your RussyPeripheralAsItem?

 

If so you'll need to copy the NBT data manually when the block is broken, like GotoLink says.

 

-TGG

 

I found the mistake. I used getPickBlock method to specify the output ItemStack

@Override
    public ItemStack getPickBlock(MovingObjectPosition mop, World world, int x, int y, int z)
    {
	RussyPeripheral tile = (RussyPeripheral) world.getBlockTileEntity(x, y, z);
	ItemStack stack = new ItemStack(this,1);
	if(!stack.hasTagCompound())
            stack.setTagCompound(new NBTTagCompound());
        stack.getTagCompound().setString("owner", tile.Username);
        stack.getTagCompound().setBoolean("canWaitForKey",tile.canWaitForKey);
        if(tile.eniquepass != null) stack.getTagCompound().setString("pass", tile.eniquepass);
        stack.getTagCompound().setInteger("valve1Active", tile.valve1state);
        stack.getTagCompound().setBoolean(tile.RVALVE_UPGRADE, tile.rightValveUpgrade);
	for(int i = 0;i<MyTickHandler.loadedRussyTiles.size();i++){
		if(MyTickHandler.loadedRussyTiles.get(i).xCoord == tile.xCoord
				&& MyTickHandler.loadedRussyTiles.get(i).yCoord == tile.yCoord
				&& MyTickHandler.loadedRussyTiles.get(i).zCoord == tile.zCoord){
			MyTickHandler.loadedRussyTiles.remove(i);
		}
	}
	return stack;
    }

 

But I should have used getBlockDropped method for it .

 

Thank you for your responses ! SOLVED

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



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • They were already updated, and just to double check I even did a cleanup and fresh update from that same page. I'm quite sure drivers are not the problem here. 
    • i tried downloading the drivers but it says no AMD graphics hardware has been detected    
    • Update your AMD/ATI drivers - get the drivers from their website - do not update via system  
    • As the title says i keep on crashing on forge 1.20.1 even without any mods downloaded, i have the latest drivers (nvidia) and vanilla minecraft works perfectly fine for me logs: https://pastebin.com/5UR01yG9
    • Hello everyone, I'm making this post to seek help for my modded block, It's a special block called FrozenBlock supposed to take the place of an old block, then after a set amount of ticks, it's supposed to revert its Block State, Entity, data... to the old block like this :  The problem I have is that the system breaks when handling multi blocks (I tried some fix but none of them worked) :  The bug I have identified is that the function "setOldBlockFields" in the item's "setFrozenBlock" function gets called once for the 1st block of multiblock getting frozen (as it should), but gets called a second time BEFORE creating the first FrozenBlock with the data of the 1st block, hence giving the same data to the two FrozenBlock :   Old Block Fields set BlockState : Block{minecraft:black_bed}[facing=east,occupied=false,part=head] BlockEntity : net.minecraft.world.level.block.entity.BedBlockEntity@73681674 BlockEntityData : id:"minecraft:bed",x:3,y:-60,z:-6} Old Block Fields set BlockState : Block{minecraft:black_bed}[facing=east,occupied=false,part=foot] BlockEntity : net.minecraft.world.level.block.entity.BedBlockEntity@6d1aa3da BlockEntityData : {id:"minecraft:bed",x:2,y:-60,z:-6} Frozen Block Entity set BlockState : Block{minecraft:black_bed}[facing=east,occupied=false,part=foot] BlockPos{x=3, y=-60, z=-6} BlockEntity : net.minecraft.world.level.block.entity.BedBlockEntity@6d1aa3da BlockEntityData : {id:"minecraft:bed",x:2,y:-60,z:-6} Frozen Block Entity set BlockState : Block{minecraft:black_bed}[facing=east,occupied=false,part=foot] BlockPos{x=2, y=-60, z=-6} BlockEntity : net.minecraft.world.level.block.entity.BedBlockEntity@6d1aa3da BlockEntityData : {id:"minecraft:bed",x:2,y:-60,z:-6} here is the code inside my custom "freeze" item :    @Override     public @NotNull InteractionResult useOn(@NotNull UseOnContext pContext) {         if (!pContext.getLevel().isClientSide() && pContext.getHand() == InteractionHand.MAIN_HAND) {             BlockPos blockPos = pContext.getClickedPos();             BlockPos secondBlockPos = getMultiblockPos(blockPos, pContext.getLevel().getBlockState(blockPos));             if (secondBlockPos != null) {                 createFrozenBlock(pContext, secondBlockPos);             }             createFrozenBlock(pContext, blockPos);             return InteractionResult.SUCCESS;         }         return super.useOn(pContext);     }     public static void createFrozenBlock(UseOnContext pContext, BlockPos blockPos) {         BlockState oldState = pContext.getLevel().getBlockState(blockPos);         BlockEntity oldBlockEntity = oldState.hasBlockEntity() ? pContext.getLevel().getBlockEntity(blockPos) : null;         CompoundTag oldBlockEntityData = oldState.hasBlockEntity() ? oldBlockEntity.serializeNBT() : null;         if (oldBlockEntity != null) {             pContext.getLevel().removeBlockEntity(blockPos);         }         BlockState FrozenBlock = setFrozenBlock(oldState, oldBlockEntity, oldBlockEntityData);         pContext.getLevel().setBlockAndUpdate(blockPos, FrozenBlock);     }     public static BlockState setFrozenBlock(BlockState blockState, @Nullable BlockEntity blockEntity, @Nullable CompoundTag blockEntityData) {         BlockState FrozenBlock = BlockRegister.FROZEN_BLOCK.get().defaultBlockState();         ((FrozenBlock) FrozenBlock.getBlock()).setOldBlockFields(blockState, blockEntity, blockEntityData);         return FrozenBlock;     }  
  • Topics

×
×
  • Create New...

Important Information

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