Jump to content

Recommended Posts

Posted (edited)

Hi all!
I need some help with TileEntity.
I made a block which teleports the player to an adjusted position when the player walks on it.
My problem is that the TileEntity saves the datas (coordinates, dimension etc.) with the NBTTag, and loads them, but for some reason the
variables are empty after I re-enter to the world.
As you can see in the code, I'm writing out the x coordinate.
The problem:
I set the blocks destination, the block works perfectly, teleports the player.
When I pause the game, it saves the NBT tag, because I can read the x position from the console.
I left the world, and re-enter to it. Then I can see the coordinate again, so the NBTTag contains it, and it seems to be loaded.
But the block isn't working, and if I pause the game again, it saves 0 as x position (and as the others).
Does anyone have any ideas? I have some other block whichs code looks like this, and they're working normally.
I wrote the same code when I initialized the block (and the others are working), but I gonna show you that too.

 

TileEntity

package Apocalipty.blocks.portal;

import java.util.ArrayList;

import Apocalipty.entities.friendlies.entity_base_trader;
import Apocalipty.entities.zombies.entity_zombie_frozen;
import Apocalipty.entities.zombies.entity_zombie_normal;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.ITickable;
import net.minecraft.util.math.BlockPos;
import scala.reflect.internal.Trees.This;

public class TileEntityTeleporter extends TileEntity{

	private int x = 0;
	private int y = 0;
	private int z = 0;
	private int dim = 0;
	private boolean enabled = false;
	
	
	@Override
	public void readFromNBT(NBTTagCompound compound)
	{
		super.readFromNBT(compound);
		
		if(compound.hasKey("x")) this.x = compound.getInteger("x");
		if(compound.hasKey("y")) this.y = compound.getInteger("y");
		if(compound.hasKey("z")) this.z = compound.getInteger("z");
		if(compound.hasKey("dim")) this.dim = compound.getInteger("dim");
		if(compound.hasKey("enabled")) this.enabled = compound.getBoolean("enabled");
		System.out.println(this.x);
	}
	
	@Override
	public NBTTagCompound writeToNBT(NBTTagCompound compound)
	{
		super.writeToNBT(compound);
		
		compound.setInteger("x", this.x);
		compound.setInteger("y", this.y);
		compound.setInteger("z", this.z);
		compound.setInteger("dim", this.dim);
		compound.setBoolean("enabled", this.enabled);
		System.out.println(this.x);

		return compound;
	}
	
	public void setCoord(int x, int y, int z, int dim) {
		this.x = x;
		this.y = y;
		this.z = z;
		this.dim = dim;
		this.enabled = true;
	}
	
	public BlockPos getTeleportPos() {
		return new BlockPos(this.x, this.y, this.z);
	}
	
	public Integer getDimension() {
		return this.dim;
	}
	
	public boolean getEnabled() {
		return this.enabled;
	}

}

 

Block

package Apocalipty.blocks.portal;

import java.util.Random;

import Apocalipty.Main;
import Apocalipty.handlers.LootHandler;
import Apocalipty.handlers.SoundsHandler;
import Apocalipty.init.ItemInit;
import net.minecraft.block.Block;
import net.minecraft.block.BlockContainer;
import net.minecraft.block.BlockHorizontal;
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.creativetab.CreativeTabs;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Blocks;
import net.minecraft.init.Items;
import net.minecraft.inventory.InventoryHelper;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
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.NonNullList;
import net.minecraft.util.Rotation;
import net.minecraft.util.SoundCategory;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.text.TextComponentString;
import net.minecraft.util.text.TextFormatting;
import net.minecraft.world.IBlockAccess;
import net.minecraft.world.World;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;

public class Teleporter extends BlockContainer
{
	
	public Teleporter(String name, Material material, CreativeTabs tab) 
	{
		super(material);
		setUnlocalizedName(name);
		setRegistryName(name);
		setCreativeTab(tab);
		setHardness(40.0f);
		setHarvestLevel("pickaxe", 2);
		
	}
    
	@SideOnly(Side.CLIENT)
	public BlockRenderLayer getBlockLayer()
	{
		return BlockRenderLayer.SOLID;
	}
  
	@Override
	public boolean isOpaqueCube(IBlockState iBlockState) {
		return true;
	}
  
	@Override
	public boolean isFullCube(IBlockState iBlockState) {
		return true;
	}

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

	@Override
	public TileEntity createNewTileEntity(World worldIn, int meta)
	{
		return new TileEntityTeleporter();
	}
	
	@Override
	public boolean isFullBlock(IBlockState state) 
	{
		return false;
	}
	
	@Override
	public void onEntityWalk(World worldIn, BlockPos pos, Entity entityIn)
    {
		if(entityIn instanceof EntityPlayer && !worldIn.isRemote) {
			TileEntityTeleporter tileEntity = (TileEntityTeleporter) worldIn.getTileEntity(pos);
			EntityPlayer player = (EntityPlayer) entityIn;
			if(tileEntity.getEnabled()) {
				int playerDim = player.dimension;
				int destDim = tileEntity.getDimension();
				if(playerDim != destDim) {
					player.changeDimension(destDim);
				}
				BlockPos p = tileEntity.getTeleportPos();
				player.setPositionAndUpdate(p.getX(), p.getY(), p.getZ());
			}
		}
    }
	
	@Override
    public boolean onBlockActivated(World worldIn, BlockPos pos, IBlockState state, EntityPlayer playerIn, EnumHand hand, EnumFacing facing, float hitX, float hitY, float hitZ)
    {
		if(!worldIn.isRemote) {
			ItemStack held = playerIn.getHeldItem(hand);
			if(held.getItem() == ItemInit.SAVER) {
				NBTTagCompound compound = null;
				if(held.getTagCompound() == null) {
					compound = new NBTTagCompound();
				}else {
					compound = held.getTagCompound();
				}
				
				if(!compound.hasKey("x")) return true;
				
				int x = compound.getInteger("x");
				int y = compound.getInteger("y");
				int z = compound.getInteger("z");
				int dim = compound.getInteger("dim");
				
				TileEntityTeleporter tileEntity = (TileEntityTeleporter) worldIn.getTileEntity(pos);
				tileEntity.setCoord(x, y, z, dim);
				Main.sendMessage(playerIn, "COORDS SET!");
			}
		}
		return true;
    }	
}

 

 

Block Initialization part

GameRegistry.registerTileEntity(TileEntityTeleporter.class, Reference.MODID + ":teleporter_tile_entity");
TELEPORTER = new Teleporter("teleporter", Material.ROCK, Main.tab);
ForgeRegistries.BLOCKS.register(TELEPORTER);
itemTeleporter = new ItemBlock(TELEPORTER);
itemTeleporter.setRegistryName(TELEPORTER.getRegistryName());
ForgeRegistries.ITEMS.register(itemTeleporter);

 

Edited by NErOHUN
Guest
This topic is now closed to further replies.

Announcements



×
×
  • Create New...

Important Information

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