Jump to content

onBlockAdded and EntityPlayer


Fifou_BE

Recommended Posts

Hello, i'm trying to do a personal chest in 1.11.2 , it means that when added in world by the player it's his chest, and there is only him who can access it, to do that I apply tag to the tileentity of the personal chest. Everything works fine in single player but I'm having trouble for the multiplayer side and with this function :

 

	@Override
	public void onBlockAdded(World worldIn, BlockPos pos, IBlockState state)
	    {
	        this.setDefaultFacing(worldIn, pos, state);
	    
		    			TileEntity te = worldIn.getTileEntity(pos);
		    			EntityPlayer playerIn = Minecraft.getMinecraft().player;
		    			System.out.println(playerIn);
				    	if(te != null)
				    	{
				    		te.getTileData().setString("Owner", playerIn.getUniqueID().toString());
				    		te.getTileData().setString("ownerS", playerIn.getName());
				    		int x = te.getPos().getX();
				    		int y = te.getPos().getY();
				    		int z = te.getPos().getZ();
				    		ModEconomy.network.sendToServer(new PacketVaultCreated(playerIn.getName(),playerIn.getUniqueID().toString(),x,y,z));
				    		if(te.getTileData().hasKey("Owner"))
				    		{
					    		this.setBlockUnbreakable();
					    		this.setResistance(20000.0F);
				    		}
		    		}	    	
	    	}

 

But as you can see I'm using 

EntityPlayer playerIn = Minecraft.getMinecraft().player;

 which does not exist in server side, so I searched a bit on google but I can't find a way to recover the EntityPlayer /:

 

So I tought I should recover the entity player of the player who added the block, but I don't know how to do it correctly /: Do you have any hint for me ? :( 

 

Thank you :)

Edited by Fifou_BE
Link to comment
Share on other sites

If you use Block#onBlockPlacedBy, you'll get the EntityLivingBase who placed the Block as a variable.

Don't PM me with questions. They will be ignored! Make a thread on the appropriate board for support.

 

1.12 -> 1.13 primer by williewillus.

 

1.7.10 and older versions of Minecraft are no longer supported due to it's age! Update to the latest version for support.

 

http://www.howoldisminecraft1710.today/

Link to comment
Share on other sites

 

30 minutes ago, Fifou_BE said:

te.getTileData()

Why are you using custom tile data on your own TileEntity?

 

30 minutes ago, Fifou_BE said:

this.setBlockUnbreakable(); this.setResistance(20000.0F);

Blocks are singletons. When this piece of code is executed all your chests will become unbreakable. Even those which are not placed yet. Block::getPlayerRelativeBlockHardness allows you to handle block hardness in cases like this.

 

30 minutes ago, Fifou_BE said:

if(te.getTileData().hasKey("Owner"))

This condition is redundant as it is always true due to this line above

30 minutes ago, Fifou_BE said:

te.getTileData().setString("Owner", playerIn.getUniqueID().toString());

 

30 minutes ago, Fifou_BE said:

sendToServer

Why are you sending anything to the server at this stage? Both Block::onBlockAdded and the suggested Block::onBlockPlacedBy are common code. You do not need to send anything to the server - the server should already have all the data it needs.

 

 

Edited by V0idWa1k3r
Link to comment
Share on other sites

1 minute ago, V0idWa1k3r said:

 

Why are you using custom tile data on your own TileEntity?

 

Blocks are singletons. When this piece of code is executed all your chests will become unbreakable. Even those which are not placed yet. Block::getPlayerRelativeBlockHardness allows you to handle block hardness in cases like this.

 

This condition is redundant as it is always true due to this line above

 

Why are you sending anything to the server at this stage? Both Block::onBlockAdded and the suggested Block::onBlockPlacedBy are common code. You do not need to send anything to the server - the server should already have all the data it needs.

 

 

When my block is added on world, it add data to the tile entity in world with some tags such as the owner. It allows only the owner of the tile entity to access it, and destroy it. So if I don't send data to server, it don't link client/server, so the server don't know this tile entity is my tile entity(owner). Hope it's clear ^^ 

Link to comment
Share on other sites

1 minute ago, Fifou_BE said:

When my block is added on world, it add data to the tile entity in world with some tags such as the owner. It allows only the owner of the tile entity to access it, and destroy it.

Yes and there is no need to use custom data for that. It is your own tile entity. TileEntity::getTileData is for tile entities that you do not own but want to store some data in. If it is your own tile entity you can and should just declare some fields in that tileentity and set/get them when needed.

 

2 minutes ago, Fifou_BE said:

So if I don't send data to server, it don't link client/server, so the server don't know this tile entity is my tile entity(owner).

The placement of the block already happens on the server. The server is already aware of the placer as you set it right there. You are literally setting your data on the server and then sending a server -> server packet. That is not a good idea. The client may be the one you need to notify, not the server.

Link to comment
Share on other sites

1 minute ago, V0idWa1k3r said:

Yes and there is no need to use custom data for that. It is your own tile entity. TileEntity::getTileData is for tile entities that you do not own but want to store some data in. If it is your own tile entity you can and should just declare some fields in that tileentity and set/get them when needed.

 

The placement of the block already happens on the server. The server is already aware of the placer as you set it right there. You are literally setting your data on the server and then sending a server -> server packet. That is not a good idea. The client may be the one you need to notify, not the server.

 

Ok so I removed the 

 ModEconomy.network.sendToServer(new PacketVaultCreated(playerIn.getName(),playerIn.getUniqueID().toString(),x,y,z));

and when I try to access it, it don't work well. Hmm let me add all my class, like that maybe you can see what I'm trying to do.. I don't say you're not right, but just to be sure.

 

package fr.fifou.economy.blocks;

import javax.annotation.Nullable;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import fr.fifou.economy.ModEconomy;
import fr.fifou.economy.blocks.tileentity.TileEntityBlockVault;
import fr.fifou.economy.gui.GuiHandler;
import fr.fifou.economy.packets.PacketCardChange;
import fr.fifou.economy.packets.PacketVaultCreated;

import net.minecraft.block.Block;
import net.minecraft.block.BlockChest;
import net.minecraft.block.BlockContainer;
import net.minecraft.block.BlockHorizontal;
import net.minecraft.block.ITileEntityProvider;
import net.minecraft.block.material.Material;
import net.minecraft.block.properties.IProperty;
import net.minecraft.block.properties.PropertyDirection;
import net.minecraft.block.state.BlockStateContainer;
import net.minecraft.block.state.IBlockState;
import net.minecraft.client.Minecraft;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.init.Blocks;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.server.MinecraftServer;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.tileentity.TileEntityFurnace;
import net.minecraft.util.BlockRenderLayer;
import net.minecraft.util.EnumBlockRenderType;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.EnumHand;
import net.minecraft.util.Mirror;
import net.minecraft.util.Rotation;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.MathHelper;
import net.minecraft.util.text.TextComponentTranslation;
import net.minecraft.world.IBlockAccess;
import net.minecraft.world.World;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import net.minecraftforge.items.CapabilityItemHandler;
import net.minecraftforge.items.IItemHandler;

public class blockVault extends BlockContainer
{
    public static final PropertyDirection FACING = BlockHorizontal.FACING;
    
	public blockVault() 
	{
		super(Material.IRON);
        this.setDefaultState(this.blockState.getBaseState().withProperty(FACING, EnumFacing.NORTH));
		this.setRegistryName("block_vault");
		this.setUnlocalizedName("block_vault");
		this.setCreativeTab(CreativeTabs.BUILDING_BLOCKS);

	}
	
	
	
	  public void onBlockPlacedBy(World worldIn, BlockPos pos, IBlockState state, EntityLivingBase placer, ItemStack stack)
	    {
	    	worldIn.setBlockState(pos, state.withProperty(FACING, placer.getHorizontalFacing()), 2);
  			
	    }
	  
	
	@Override
	public boolean onBlockActivated(World worldIn, BlockPos pos,IBlockState state, EntityPlayer playerIn, EnumHand hand, EnumFacing facing, float hitX, float hitY, float hitZ) 
	{
		TileEntity te = (TileEntityBlockVault)worldIn.getTileEntity(pos);
		if(te != null && te.hasCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, EnumFacing.NORTH))
		{
			if(te.getTileData().hasKey("Owner"))
			{
				String ownerTe = te.getTileData().getString("Owner");
				String ownerWo = playerIn.getUniqueID().toString();
				
				if(ownerWo.equals(ownerTe))
				{
					playerIn.openGui(ModEconomy.instance, GuiHandler.BLOCK_VAULT_NEW, worldIn, pos.getX(), pos.getY(), pos.getZ());
				}
				
			}
		}
         return true;
     }
	
	
	@Override
	public void onBlockClicked(World worldIn, BlockPos pos, EntityPlayer playerIn) 
	{
		TileEntity te = worldIn.getTileEntity(pos);
		if(te.getTileData().hasKey("Owner"))
		{
			String ownerTe = te.getTileData().getString("Owner");
			String ownerWo = playerIn.getUniqueID().toString();
			if(ownerWo.equals(ownerTe))
			{
				this.setResistance(20000.0F);
				this.setHardness(1.0F);
			}
			else
			{
	    		this.setBlockUnbreakable();
	    		this.setResistance(20000.0F);
			}

		}
	}

		
	//FACING
	@Override
	public void onBlockAdded(World worldIn, BlockPos pos, IBlockState state)
	    {
	   	 this.setDefaultFacing(worldIn, pos, state);
	
			TileEntity te = (TileEntityBlockVault) worldIn.getTileEntity(pos);
			EntityPlayer playerIn = Minecraft.getMinecraft().player;
			System.out.println(playerIn);
			 	if(te != null)
			 	{
			 		te.getTileData().setString("Owner", playerIn.getUniqueID().toString());
			 		te.getTileData().setString("ownerS", playerIn.getName());
			 		int x = te.getPos().getX();
			 		int y = te.getPos().getY();
			 		int z = te.getPos().getZ();
			 		ModEconomy.network.sendToServer(new PacketVaultCreated(playerIn.getName(),playerIn.getUniqueID().toString(),x,y,z));
			 		if(te.getTileData().hasKey("Owner"))
			 		{
			 			 	this.setHardness(1.0F);
				    		this.setResistance(20000.0F);
			 		}
				}	    	
	    }

	    private void setDefaultFacing(World worldIn, BlockPos pos, IBlockState state)
	    {
	        if (!worldIn.isRemote)
	        {
	            IBlockState iblockstate = worldIn.getBlockState(pos.north());
	            IBlockState iblockstate1 = worldIn.getBlockState(pos.south());
	            IBlockState iblockstate2 = worldIn.getBlockState(pos.west());
	            IBlockState iblockstate3 = worldIn.getBlockState(pos.east());
	            EnumFacing enumfacing = (EnumFacing)state.getValue(FACING);

	            if (enumfacing == EnumFacing.NORTH && iblockstate.isFullBlock() && !iblockstate1.isFullBlock())
	            {
	                enumfacing = EnumFacing.SOUTH;
	            }
	            else if (enumfacing == EnumFacing.SOUTH && iblockstate1.isFullBlock() && !iblockstate.isFullBlock())
	            {
	                enumfacing = EnumFacing.NORTH;
	            }
	            else if (enumfacing == EnumFacing.WEST && iblockstate2.isFullBlock() && !iblockstate3.isFullBlock())
	            {
	                enumfacing = EnumFacing.EAST;
	            }
	            else if (enumfacing == EnumFacing.EAST && iblockstate3.isFullBlock() && !iblockstate2.isFullBlock())
	            {
	                enumfacing = EnumFacing.WEST;
	            }

	            worldIn.setBlockState(pos, state.withProperty(FACING, enumfacing), 2);
	        }
	    }
	    
	    public static void setState(boolean active, World worldIn, BlockPos pos)
	    {
	        IBlockState iblockstate = worldIn.getBlockState(pos);
	        TileEntity tileentity = worldIn.getTileEntity(pos);

	        if (active)
	        {
	            worldIn.setBlockState(pos, BlocksRegistery.BLOCK_VAULT.getDefaultState().withProperty(FACING, iblockstate.getValue(FACING)), 3);
	            worldIn.setBlockState(pos, BlocksRegistery.BLOCK_VAULT.getDefaultState().withProperty(FACING, iblockstate.getValue(FACING)), 3);
	        }
	        else
	        {
	            worldIn.setBlockState(pos, BlocksRegistery.BLOCK_VAULT.getDefaultState().withProperty(FACING, iblockstate.getValue(FACING)), 3);
	            worldIn.setBlockState(pos, BlocksRegistery.BLOCK_VAULT.getDefaultState().withProperty(FACING, iblockstate.getValue(FACING)), 3);
	        }


	        if (tileentity != null)
	        {
	            tileentity.validate();
	            worldIn.setTileEntity(pos, tileentity);
	        }
	    }
	    
	    public IBlockState getStateForPlacement(World worldIn, BlockPos pos, EnumFacing facing, float hitX, float hitY, float hitZ, int meta, EntityLivingBase placer)
	    {
	        return this.getDefaultState().withProperty(FACING, placer.getHorizontalFacing().getOpposite());
	    }

	    public IBlockState getStateFromMeta(int meta)
	    {
	        EnumFacing enumfacing = EnumFacing.getFront(meta);

	        if (enumfacing.getAxis() == EnumFacing.Axis.Y)
	        {
	            enumfacing = EnumFacing.NORTH;
	        }

	        return this.getDefaultState().withProperty(FACING, enumfacing);
	    }

	    /**
	     * Convert the BlockState into the correct metadata value
	     */
	    public int getMetaFromState(IBlockState state)
	    {
	        return ((EnumFacing)state.getValue(FACING)).getIndex();
	    }

	    /**
	     * Returns the blockstate with the given rotation from the passed blockstate. If inapplicable, returns the passed
	     * blockstate.
	     */
	    public IBlockState withRotation(IBlockState state, Rotation rot)
	    {
	        return state.withProperty(FACING, rot.rotate((EnumFacing)state.getValue(FACING)));
	    }

	    /**
	     * Returns the blockstate with the given mirror of the passed blockstate. If inapplicable, returns the passed
	     * blockstate.
	     */
	    public IBlockState withMirror(IBlockState state, Mirror mirrorIn)
	    {
	        return state.withRotation(mirrorIn.toRotation((EnumFacing)state.getValue(FACING)));
	    }

	    protected BlockStateContainer createBlockState()
	    {
	        return new BlockStateContainer(this, new IProperty[] {FACING});
	    }
	
	//OTHERS

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

	  
	  @Override
	  public EnumBlockRenderType getRenderType(IBlockState state)
	  {
	      return EnumBlockRenderType.MODEL;
	  }
	  
	@Override
	public TileEntity createNewTileEntity(World worldIn, int meta) 
	{
		return new TileEntityBlockVault();
	}
	  
	@Override
	public void breakBlock(World worldIn, BlockPos pos, IBlockState state) 
	{
		TileEntityBlockVault te = (TileEntityBlockVault)worldIn.getTileEntity(pos);
		if(te !=null && te.hasCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, EnumFacing.NORTH))
		{
			IItemHandler inventory = te.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, EnumFacing.NORTH);
			if(inventory != null)
			{
				for(int i=0; i < inventory.getSlots(); i++)
				{
					if(inventory.getStackInSlot(i) != ItemStack.EMPTY)
					{
						EntityItem item = new EntityItem(worldIn, pos.getX() + 0.5, pos.getY()+0.5, pos.getZ() +0.5, inventory.getStackInSlot(i));
						
						float multiplier = 0.1f;
						float motionX = worldIn.rand.nextFloat() - 0.5F;
						float motionY = worldIn.rand.nextFloat() - 0.5F;
						float motionZ = worldIn.rand.nextFloat() - 0.5F;
						
						item.motionX = motionX * multiplier;
						item.motionY = motionY * multiplier;
						item.motionZ = motionZ * multiplier;
						
						worldIn.spawnEntity(item);
					}
				}
			}
		}			
		super.breakBlock(worldIn, pos, state);
	}

}

 

 

Link to comment
Share on other sites

5 minutes ago, Fifou_BE said:

Ok so I removed the 

 

5 minutes ago, Fifou_BE said:

ModEconomy.network.sendToServer(new PacketVaultCreated(playerIn.getName(),playerIn.getUniqueID().toString(),x,y,z));

No you didn't. Not in the class file you've posted. Honestly it looks like you are in a middle of rewriting some code in the class(for example you've added the Block::onBlockPlacedBy but it is empty). You should do it first and post the final code here if you have any issues left.

 

5 minutes ago, Fifou_BE said:

if(ownerWo.equals(ownerTe)) { this.setResistance(20000.0F); this.setHardness(1.0F); } else { this.setBlockUnbreakable(); this.setResistance(20000.0F); }

As I've said blocks are singletons. Changing their properties like this changes them for all blocks of that type in the world.

Edited by V0idWa1k3r
Link to comment
Share on other sites

Just now, V0idWa1k3r said:

 

No you didn't. Not in the class file you've posted. Honestly it looks like you are in a middle of rewrighting some code in the class(for example you've added the Block::onBlockPlacedBy but it is empty). You should do it first and post the final code here if you have any issues left.

 

As I've said blocks are singletons. Changing their properties like this changes them for all blocks of that type in the world.

I didn't remove it, because if I do my code doesn't work anymore. I've posted what I've done since I came here. Just to see if you understand what I'm trying to do ^^ For the part of changing properties like this, it's only for me, this will be modify after but first I wanted to correct the multiplayer crash because of 

EntityPlayer playerIn = Minecraft.getMinecraft().player;

 

then, for the onBlockPlacedBy it was already there because of facing block 

	
	  public void onBlockPlacedBy(World worldIn, BlockPos pos, IBlockState state, EntityLivingBase placer, ItemStack stack)
	    {
	    	worldIn.setBlockState(pos, state.withProperty(FACING, placer.getHorizontalFacing()), 2);
  			
	    }

 

 

 

Link to comment
Share on other sites

3 minutes ago, Fifou_BE said:

I didn't remove it, because if I do my code doesn't work anymore. I've posted what I've done since I came here. Just to see if you understand what I'm trying to do ^^

We know what you're trying to do. We're trying to tell you that you're doing it wrong and how to fix it, but you keep taking that advice and not doing it.

  • Like 1

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

8 minutes ago, Draco18s said:

We know what you're trying to do. We're trying to tell you that you're doing it wrong and how to fix it, but you keep taking that advice and not doing it.

 

Ok so I removed all you told me was wrong.

 

	  public void onBlockPlacedBy(World worldIn, BlockPos pos, IBlockState state, EntityLivingBase placer, ItemStack stack)
	    {
	    	worldIn.setBlockState(pos, state.withProperty(FACING, placer.getHorizontalFacing()), 2);
  			
	    }
	  
	
	@Override
	public boolean onBlockActivated(World worldIn, BlockPos pos,IBlockState state, EntityPlayer playerIn, EnumHand hand, EnumFacing facing, float hitX, float hitY, float hitZ) 
	{
		TileEntity te = (TileEntityBlockVault)worldIn.getTileEntity(pos);
		if(te != null && te.hasCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, EnumFacing.NORTH))
		{
		}
         return true;
     }
	
	
	@Override
	public void onBlockClicked(World worldIn, BlockPos pos, EntityPlayer playerIn) 
	{

	}

 

So, I need to do that in my tile entity then ?

 

	@Override
	public NBTTagCompound writeToNBT(NBTTagCompound compound) 
	{
		compound.setTag("inventory", inventory.serializeNBT());
		compound.setString("owner", player.getUniqueID().toString());
		return super.writeToNBT(compound);
	}

 

But another problem is that I don't understand how to recover the EntityPlayer player here ?

 

Link to comment
Share on other sites

NBT is for storing data when the world is saved, it's not relevant to this issue. When the block is placed, you need to store the placing player in the tileentity. You have access to the block and the placing player in onBlockPlacedBy, you just need to get the tileentity and store the information in there. Then when someone tries to activate the block, you check the tileentity to see if they are the allowed player. You have access to the block and the activating player in onBlockActivated, so you just need to get the tileentity and check the information to respond accordingly.

  • Like 1
Link to comment
Share on other sites

28 minutes ago, Jay Avery said:

NBT is for storing data when the world is saved, it's not relevant to this issue. When the block is placed, you need to store the placing player in the tileentity. You have access to the block and the placing player in onBlockPlacedBy, you just need to get the tileentity and store the information in there. Then when someone tries to activate the block, you check the tileentity to see if they are the allowed player. You have access to the block and the activating player in onBlockActivated, so you just need to get the tileentity and check the information to respond accordingly.

 

I've tried something basing my self on TileEntityFlowerPot but I need NBT, because if I close game the data of the tile entity is null ..

 

  public void onBlockPlacedBy(World worldIn, BlockPos pos, IBlockState state, EntityLivingBase placer, ItemStack stack)
	    {
	    	worldIn.setBlockState(pos, state.withProperty(FACING, placer.getHorizontalFacing()), 2);
	    	TileEntityBlockVault te = (TileEntityBlockVault)worldIn.getTileEntity(pos);
	    	te.setString(placer.getUniqueID().toString());
	    	System.out.println(te.getOwnerS());
	    	
	    }
	  
	
	@Override
	public boolean onBlockActivated(World worldIn, BlockPos pos,IBlockState state, EntityPlayer playerIn, EnumHand hand, EnumFacing facing, float hitX, float hitY, float hitZ) 
	{
		TileEntityBlockVault te = (TileEntityBlockVault)worldIn.getTileEntity(pos);
		if(te != null && te.hasCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, EnumFacing.NORTH))
		{
			if(te.getOwnerS() != null)
			{
				if(te.getOwnerS().equals(playerIn.getUniqueID()))
				{
					System.out.println(te.getOwnerS());
					playerIn.openGui(ModEconomy.instance, GuiHandler.BLOCK_VAULT_NEW, worldIn, pos.getX(), pos.getY(), pos.getZ());
				}
			}
			else
			{
				System.out.println(te.getOwnerS());
			}
		}
         return true;
     }

 

TilEntityBlockVault

 

private static String ownerS;
	
	public TileEntityBlockVault()
	{
		
	}
	
	public TileEntityBlockVault(String ownerData)
	{
		 this.ownerS = ownerData;
	}
	
    public void setString(String string)
    {
        this.ownerS = string;
    }
    
    @Nullable
    public String getOwnerS()
    {
        return this.ownerS;
    }

 

Edited by Fifou_BE
Link to comment
Share on other sites

2 minutes ago, Fifou_BE said:

I've tried something basing my self on TileEntityFlowerPot but I need NBT, because if I close game the data of the tile entity is null ..

 

Yes, you need NBT, it's just not relevant to this particular issue of making a tileentity which only a certain player can access. NBT is just something you have to do for any tileentity which stores data of some kind.

Link to comment
Share on other sites

Ok, so is my code correct or do you see some problems ? :)

blockVault


	  public void onBlockPlacedBy(World worldIn, BlockPos pos, IBlockState state, EntityLivingBase placer, ItemStack stack)
	    {
	    	worldIn.setBlockState(pos, state.withProperty(FACING, placer.getHorizontalFacing()), 2);
	    	TileEntityBlockVault te = (TileEntityBlockVault)worldIn.getTileEntity(pos);
	    	te.setString(placer.getUniqueID().toString());
	    	te.getTileData().setString("ownerS", placer.getUniqueID().toString());
	    	System.out.println(te.getOwnerS());
	    	
	    }
	  
	
	@Override
	public boolean onBlockActivated(World worldIn, BlockPos pos,IBlockState state, EntityPlayer playerIn, EnumHand hand, EnumFacing facing, float hitX, float hitY, float hitZ) 
	{
		TileEntityBlockVault te = (TileEntityBlockVault)worldIn.getTileEntity(pos);
		if(te != null && te.hasCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, EnumFacing.NORTH))
		{
			if(te.getTileData().getString("ownerS") != null)
			{
				String checkONBT = te.getTileData().getString("ownerS");
				String checkOBA = playerIn.getUniqueID().toString();
				if(checkONBT.equals(checkOBA))
				{
					playerIn.openGui(ModEconomy.instance, GuiHandler.BLOCK_VAULT_NEW, worldIn, pos.getX(), pos.getY(), pos.getZ());
				}
			}
			else
			{
				System.out.println(te.getOwnerS());
			}
		}
         return true;
     }

 

TileEntityBlockVault

 

	ItemStackHandler inventory = new ItemStackHandler(27);
	private static String ownerS;
	
	public TileEntityBlockVault()
	{
		
	}
	
	public TileEntityBlockVault(String ownerData)
	{
		 this.ownerS = ownerData;
	}
	
    public void setString(String string)
    {
        this.ownerS = string;
    }
    
    @Nullable
    public String getOwnerS()
    {
        return this.ownerS;
    }
	
	@Override
	public NBTTagCompound writeToNBT(NBTTagCompound compound) 
	{
		compound.setTag("inventory", inventory.serializeNBT());
		compound.setString("ownerS", this.ownerS);
		return super.writeToNBT(compound);
	}
	
	
	@Override
	public void readFromNBT(NBTTagCompound compound) 
	{
		super.readFromNBT(compound);
		inventory.deserializeNBT(compound.getCompoundTag("inventory"));
		this.ownerS = compound.getString("ownerS");
	}

 

Sorry for being low to understand things ^^

Edited by Fifou_BE
Link to comment
Share on other sites

Quote

f(te.getTileData().getString("ownerS") != null)

You're still using te.getTileData().

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

14 minutes ago, Jay Avery said:

You already have a much more convenient way of finding out if your code still has problems: try it and see what happens!

That's what I've done, but I prefer to have the advice of someone really qualified ^^ It seems to worked but I should not use te.getTileData().

 

But how am I supposed to get the data from the tile entity then ? I looked trough all the methods and nothing is similar to getTileData() :/ !

Link to comment
Share on other sites

1 minute ago, Fifou_BE said:

That's what I've done, but I prefer to have the advice of someone really qualified ^^ It seems to worked but I should not use te.getTileData().

 

But how am I supposed to get the data from the tile entity then ? I looked trough all the methods and nothing is similar to getTileData() :/ !

You have made a method yourself which gives the data from the tileentity: 

    public String getOwnerS()

 

Also, this field should not be static:

	private static String ownerS;

Static means there is one value for the whole class, rather than one value per instance. If someone places a new tileentity, they will become the owner of ALL tileentities of that type. Presumably that's not what you're going for. You also cannot use this. with static fields - your IDE should give you a warning if you do.

Link to comment
Share on other sites

I've changed all of this, thank you :) I have this prompt on the log but can it be linked to my mod or it's mc ?

 

[18:09:04] [pool-2-thread-1/WARN]: Couldn't look up profile properties for com.mojang.authlib.GameProfile@7bd76e78[id=1180fda0-fbd6-4eaa-a861-61fd3910f6fc,name=Fifou_BE,properties={},legacy=false]
com.mojang.authlib.exceptions.AuthenticationException: The client has sent too many requests within a certain amount of time
	at com.mojang.authlib.yggdrasil.YggdrasilAuthenticationService.makeRequest(YggdrasilAuthenticationService.java:79) ~[YggdrasilAuthenticationService.class:?]
	at com.mojang.authlib.yggdrasil.YggdrasilMinecraftSessionService.fillGameProfile(YggdrasilMinecraftSessionService.java:180) [YggdrasilMinecraftSessionService.class:?]
	at com.mojang.authlib.yggdrasil.YggdrasilMinecraftSessionService$1.load(YggdrasilMinecraftSessionService.java:60) [YggdrasilMinecraftSessionService$1.class:?]
	at com.mojang.authlib.yggdrasil.YggdrasilMinecraftSessionService$1.load(YggdrasilMinecraftSessionService.java:57) [YggdrasilMinecraftSessionService$1.class:?]
	at com.google.common.cache.LocalCache$LoadingValueReference.loadFuture(LocalCache.java:3524) [guava-17.0.jar:?]
	at com.google.common.cache.LocalCache$Segment.loadSync(LocalCache.java:2317) [guava-17.0.jar:?]
	at com.google.common.cache.LocalCache$Segment.lockedGetOrLoad(LocalCache.java:2280) [guava-17.0.jar:?]
	at com.google.common.cache.LocalCache$Segment.get(LocalCache.java:2195) [guava-17.0.jar:?]
	at com.google.common.cache.LocalCache.get(LocalCache.java:3934) [guava-17.0.jar:?]
	at com.google.common.cache.LocalCache.getOrLoad(LocalCache.java:3938) [guava-17.0.jar:?]
	at com.google.common.cache.LocalCache$LocalLoadingCache.get(LocalCache.java:4821) [guava-17.0.jar:?]
	at com.google.common.cache.LocalCache$LocalLoadingCache.getUnchecked(LocalCache.java:4827) [guava-17.0.jar:?]
	at com.mojang.authlib.yggdrasil.YggdrasilMinecraftSessionService.fillProfileProperties(YggdrasilMinecraftSessionService.java:170) [YggdrasilMinecraftSessionService.class:?]
	at net.minecraft.client.Minecraft.getProfileProperties(Minecraft.java:3056) [Minecraft.class:?]
	at net.minecraft.client.resources.SkinManager$3.run(SkinManager.java:138) [SkinManager$3.class:?]
	at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_131]
	at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_131]
	at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) [?:1.8.0_131]
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) [?:1.8.0_131]
	at java.lang.Thread.run(Unknown Source) [?:1.8.0_131]

 

Edited by Fifou_BE
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.