Jump to content

[1.6.4] world.getBlockTileEntity(x, y, z); returns new entity?


Recommended Posts

Posted

Hello everyone,

 

First mod, but not my first run in with Java.

I'm trying to get a portal-like object in my world. I've followed a tutorial for creating multiblock objects which helped me out pretty good.

However, there's a weird thing here.

 

My multiblock consists of 3 objects: portalCore, portalDummy and portalPortal (oh, the name)

Anyway, The core block holds all the variables, to where the portal links e.d.

The dummy is just the ring of the portal, which transfers all the functions (redstone) to the core block.

portalPortal is the block that you actually walk trough. It is generated a bit later than the dummy and core. (Only on redstone signal)

 

They are all three BlockContainers, and all three have a tile behind them.

The problem i'm having now is that when i break the portalPortal block, it works perfectly. The block searches the tileEntity needed, which in term searches the core block(because of stored data) telling the core block to turn the portal off.

 

However, when i walk trough the block (onEntityCollidedWithBlock), the exact same function now returns a completely new TilePortalPortal object.

This i know because the coreX, coreY and coreZ are emtpy.

 

I will post the class PortalPortalBlock (the block) and TilePortalPortal (the tileEntityClass) below.

Please excuse my names, i haven't decided on a good naming-directive for my mods.

 

PortalPortalBlock.java

package k4unl.minecraft.portals.blocks;

import java.util.logging.Level;

import k4unl.minecraft.portals.lib.LogHelper;
import k4unl.minecraft.portals.lib.config.ModInfo;
import k4unl.minecraft.portals.lib.config.Names;
import k4unl.minecraft.portals.tiles.TilePortalCore;
import k4unl.minecraft.portals.tiles.TilePortalPortal;
import net.minecraft.block.BlockContainer;
import net.minecraft.block.material.Material;
import net.minecraft.client.entity.EntityPlayerSP;
import net.minecraft.client.renderer.texture.IconRegister;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.AxisAlignedBB;
import net.minecraft.util.Icon;
import net.minecraft.world.IBlockAccess;
import net.minecraft.world.World;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;

public class PortalPortalBlock extends BlockContainer {
private Icon blockIcon;

public PortalPortalBlock(int id) {
	super(id, Material.portal);

	setUnlocalizedName(Names.portalCoreBlock_unlocalized);
	setLightValue(0.6F);
	setHardness(99999F);
}

@Override
public void registerIcons(IconRegister icon) {
	blockIcon = icon.registerIcon(ModInfo.ID.toLowerCase() + ":" + Names.portalPortalBlock_unlocalized);
}


@Override
public Icon getIcon(int side, int metadata) {
	return blockIcon;
}

/**
     * Returns a bounding box from the pool of bounding boxes (this means this box can change after the pool has been
     * cleared to be reused)
     */
    public AxisAlignedBB getCollisionBoundingBoxFromPool(World par1World, int par2, int par3, int par4){
        return null;
    }

    /**
     * Updates the blocks bounds based on its current state. Args: world, x, y, z
     */
    public void setBlockBoundsBasedOnState(IBlockAccess par1IBlockAccess, int par2, int par3, int par4) {
        float f;
        float f1;

        if (par1IBlockAccess.getBlockId(par2 - 1, par3, par4) != this.blockID && par1IBlockAccess.getBlockId(par2 + 1, par3, par4) != this.blockID)
        {
            f = 0.02F;
            f1 = 0.5F;
            this.setBlockBounds(0.5F - f, 0.0F, 0.5F - f1, 0.5F + f, 1.0F, 0.5F + f1);
        }
        else
        {
            f = 0.5F;
            f1 = 0.02F;
            this.setBlockBounds(0.5F - f, 0.0F, 0.5F - f1, 0.5F + f, 1.0F, 0.5F + f1);
        }
    }

    /**
     * Returns which pass should this block be rendered on. 0 for solids and 1 for alpha
     */
    public int getRenderBlockPass(){
        return 1;
    }

    /**
     * Is this block (a) opaque and (b) a full 1m cube?  This determines whether or not to render the shared face of two
     * adjacent blocks and also whether the player can attach torches, redstone wire, etc to this block.
     */
    public boolean isOpaqueCube(){
        return false;
    }

    /**
     * If this block doesn't render as an ordinary block it will return False (examples: signs, buttons, stairs, etc)
     */
    public boolean renderAsNormalBlock(){
        return false;
    }
    
    public void onEntityCollidedWithBlock(World world, int x, int y, int z, Entity colEntity){
    	//The problem here being that this actually won't return the portal
    	//The coördinates supplied by this function are actually the coördinates
    	//of the entity trying to pass.
    	if(colEntity instanceof EntityPlayerMP || colEntity instanceof EntityPlayerSP){
    		//For now, just players plox
    		TilePortalPortal dummy = (TilePortalPortal)world.getBlockTileEntity(x, y, z);
    		if(dummy != null && dummy.getCore() != null)
    			dummy.getCore().getMasterClass().teleport(colEntity);
    	}
    	
    	super.onEntityCollidedWithBlock(world, x, y, z, colEntity);

    }
    
@Override
public void breakBlock(World world, int x, int y, int z, int oldBlockId, int par6){
	TilePortalPortal dummy = (TilePortalPortal)world.getBlockTileEntity(x, y, z);
	if(dummy != null && dummy.getCore() != null)
		dummy.getCore().getMasterClass().deactivatePortal();

	super.breakBlock(world, x, y, z, oldBlockId, par6);
}
    
    @SideOnly(Side.CLIENT)
    public int idPicked(World par1World, int par2, int par3, int par4){
        return 0;
    }


@Override
public TileEntity createNewTileEntity(World world) {
	return new TilePortalPortal();
}

}

 

TilePortalPortal.java

package k4unl.minecraft.portals.tiles;

import java.util.logging.Level;

import k4unl.minecraft.portals.lib.LogHelper;
import k4unl.minecraft.portals.lib.config.Constants;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;

public class TilePortalPortal extends TileEntity{

TilePortalCore tileCore;
int coreX;
int coreY;
int coreZ;
boolean isRedstonePowered = false;

public void setCore(TilePortalCore core){
	coreX = core.xCoord;
	coreY = core.yCoord;
	coreZ = core.zCoord;
	tileCore = core;
}

public TilePortalCore getCore(){
	if(tileCore == null){
		tileCore = (TilePortalCore)worldObj.getBlockTileEntity(coreX, coreY, coreZ);
		if(tileCore == null){
			TileEntity tEnt = worldObj.getBlockTileEntity(xCoord, yCoord, zCoord);
			LogHelper.log(Level.INFO, "Derp message");
		}
	}
	return tileCore;
}

@Override
public void readFromNBT(NBTTagCompound tagCompound){
	super.readFromNBT(tagCompound);

	coreX = tagCompound.getInteger("CoreX");
	coreY = tagCompound.getInteger("CoreY");
	coreZ = tagCompound.getInteger("CoreZ");
}

@Override
public void writeToNBT(NBTTagCompound tagCompound){
	super.writeToNBT(tagCompound);

	tagCompound.setInteger("CoreX", coreX);
	tagCompound.setInteger("CoreY", coreY);
	tagCompound.setInteger("CoreZ", coreZ);
}
}

 

I thank you in advance!

 

With kind regards,

K-4U

Posted

First of all, thanks for the reply :)

 

This is even before i do the teleport function.

So, i'm not sure what you mean with "they are empty"?

 

The problem is that both "public void breakBlock()" and "public void onEntityCollidedWithBlock()" have the same code:

TilePortalPortal dummy = (TilePortalPortal)world.getBlockTileEntity(x, y, z);
if(dummy != null && dummy.getCore() != null)
dummy.getCore().getMasterClass().teleport(colEntity);

Now somehow, the entityCollided function gives me another (new?) tileEntity back, whilst the breakBlock function gives me exactly what i want: the existing tileEntity on those coördinates.

 

What is the difference between these functions?

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.