Jump to content

[1.7.2] Door unlocking and locking across every block not just the one [SOLVED]


Jacknoshima

Recommended Posts

Okay, so, I'm not entirely sure how best to describe this.

 

I'm making a custom door that's locked and becomes unlocked when a key is used on it.

 

That much I've done no problem, but when I unlock one door, it unlocks every door. I used a boolean  (locked) and when the block is activated with a key it sets the boolean to false. When the block is added to the world, it sets it true.

 

However, whenever I use a key on one door, every door becomes unlocked. When I add a new locked door, every door in the world becomes locked.

 

here is my code


public boolean locked;

public void onBlockAdded(World p_149726_1_, int p_149726_2_, int p_149726_3_, int p_149726_4_)
    {
        super.onBlockAdded(p_149726_1_, p_149726_2_, p_149726_3_, p_149726_4_);
        locked = true;
    }
    /**
     * Called upon block activation (right click on the block.)
     */
    public boolean onBlockActivated(World world, int p_149727_2_, int p_149727_3_, int p_149727_4_, EntityPlayer player, int p_149727_6_, float p_149727_7_, float p_149727_8_, float p_149727_9_)
    {
    	if(world.isRemote)
    	{
    		return true;
    	}
    	else
    	{
        if(player.getHeldItem() != null && player.getHeldItem().getItem() == BaseItem.smallKey)
        {
        	locked = false;
        	if(!player.capabilities.isCreativeMode)
            {
            	player.inventory.consumeInventoryItem(BaseItem.smallKey);
            }
        }
        if(!locked)
        {
            int i1 = this.func_150012_g(world, p_149727_2_, p_149727_3_, p_149727_4_);
            int j1 = i1 & 7;
            j1 ^= 4;

            if ((i1 &  == 0)
            {
            	world.setBlockMetadataWithNotify(p_149727_2_, p_149727_3_, p_149727_4_, j1, 2);
            	world.markBlockRangeForRenderUpdate(p_149727_2_, p_149727_3_, p_149727_4_, p_149727_2_, p_149727_3_, p_149727_4_);
                
            }
            else
            {
            	world.setBlockMetadataWithNotify(p_149727_2_, p_149727_3_ - 1, p_149727_4_, j1, 2);
            	world.markBlockRangeForRenderUpdate(p_149727_2_, p_149727_3_ - 1, p_149727_4_, p_149727_2_, p_149727_3_, p_149727_4_);
                
            }

            world.playAuxSFXAtEntity(player, 1003, p_149727_2_, p_149727_3_, p_149727_4_, 0);
            return true;
        }
        else
        	return false;
    	}
    }

 

I tried making a tile entity and doing it through that, but whenever I did anything to do with the tile entity it threw back a null pointer exception. I'm not very good with tile entities, so I'm not really surprised, but if I do need to use a tile entity I'd love some pointers on how exactly to go about using it.

 

This was the Tile Entity I tried to make

package com.noshmod.block.tileentity;

import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;

public class TileEntityLockedDoor extends TileEntity
{
public boolean locked;

public TileEntityLockedDoor()
{

}

public void writeToNBT(NBTTagCompound p_145841_1_)
    {
        super.writeToNBT(p_145841_1_);
        p_145841_1_.setBoolean("locked", this.locked);;
    }

    public void readFromNBT(NBTTagCompound p_145839_1_)
    {
        super.readFromNBT(p_145839_1_);
        this.locked = p_145839_1_.getBoolean("note");
    }
    
    public void setLocked(boolean par1)
    {
    	par1 = this.locked;
    }
    
    public boolean getLocked()
    {
    	return this.locked;
    }

}

 

obviously the door code above hasn't got this implemented

 

but it was basically just replacing the boolean "locked" in the door with the get and set methods in the tile entity

and the tile entity is definitely registered correctly

 

So... can anyone tell me what exactly is going wrong?

I get a feeling it's something really simple... I just can't see it

Link to comment
Share on other sites

Okay, so, I realised what I was doing wrong with the tile entity implementation

 

I was forgetting to put in null checks

which was stupid of me

 

however

now that it's not giving null pointer exceptions

it's just... not doing anything at all

 

public void onBlockAdded(World world, int x, int y, int z)
    {
        super.onBlockAdded(world, x, y, z);
        TileEntityLockedDoor tileentitynote = (TileEntityLockedDoor)world.getTileEntity(x, y, z);
        
        if(tileentitynote != null)
        {
        	tileentitynote.setLocked(true);
        }

    }

public boolean onBlockActivated(World world, int x, int y, int z, EntityPlayer player, int p_149727_6_, float p_149727_7_, float p_149727_8_, float p_149727_9_)
    {
    	if(world.isRemote)
    	{
    		return true;
    	}
    	else
    	{
            TileEntityLockedDoor tileentitynote = (TileEntityLockedDoor)world.getTileEntity(x, y, z);
    		
        if(player.getHeldItem() != null && player.getHeldItem().getItem() == BaseItem.smallKey)
        {
        	if(tileentitynote != null && tileentitynote.getLocked() == true)
        	{
        		tileentitynote.setLocked(false);
        		
        		if(!player.capabilities.isCreativeMode)
            	{
            		player.inventory.consumeInventoryItem(BaseItem.smallKey);
            		return true;
            	}
        		return true;
        	}
        }
        if(tileentitynote != null && tileentitynote.getLocked() == false)
        {
            int i1 = this.func_150012_g(world, x, y, z);
            int j1 = i1 & 7;
            j1 ^= 4;

            if ((i1 &  == 0)
            {
            	world.setBlockMetadataWithNotify(x, y, z, j1, 2);
            	world.markBlockRangeForRenderUpdate(x, y, z, x, y, z);
                
            }
            else
            {
            	world.setBlockMetadataWithNotify(x, y - 1, z, j1, 2);
            	world.markBlockRangeForRenderUpdate(x, y - 1, z, x, y, z);
                
            }

            world.playAuxSFXAtEntity(player, 1003, x, y, z, 0);
            return true;
        }
        else
        	return false;
    	}
    }

 

is what I've got so far with the tile entity stuff

 

Link to comment
Share on other sites

Hi

 

Some background information on blocks that might be of help.

http://greyminecraftcoder.blogspot.com.au/2013/07/blocks.html

and

http://greyminecraftcoder.blogspot.com.au/2013/10/the-most-important-minecraft-classes_9.html

 

I would suggest: use metadata.  You can store 4 bits of information in it, enough for

1) door locked or unlocked

2) door open or closed

3) door facing east, west, north, or south

 

Tile entities are harder to implement correctly, but not that hard; there are a couple of decent tutorials around that a google should show you pretty easily.

 

-TGG

Link to comment
Share on other sites

To proffer an explanation of your "all doors lock/unlock together" dilemma:

 

The reason your doors all lock and unlock together is because Blocks and Items are all declared as "static", so there is only technically ONE single block (or item!) of any type in all of existence; what this means is that any class variables you use, such as "locked", even if you don't declare it as static, it is still, in a way, static, in that there is only one block instance to use that variable with.

 

In short, NEVER use your Block or Item classes to store any data that needs to change on a unique basis; instead, use metadata (stored individually per block in the world), TileEntity (much more versatile), or, for Items, the NBT tag of the containing ItemStack.

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.