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



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • JANGKRIK4D >> SLOT DANA GOPAY OVO | SLOT GACOR GAMPANG MAXWIN X100 X250 X500  << DAFTAR JANGKRIK4D >> JANGKRIK4D SITUS SLOT PGSOFT GAMING TERPERCAYA JANGKRIK4D 🚀 SITUS PGSLOT DAN PGSOFT GAMING TERPERCAYA 🚀 – JANGKRIK4D situs pgslot paling gacor telah hadir bersama JANGKRIK4D yang karena telah memberikan berbagai macam permainan untuk mendapatkan jackpot yang besar karena sudah menjadi pioneer untuk penyedia game paling mujarab beserta jajaran pgsoft gaming terpercaya yang telah bersama dengan JANGKRIK4D menjalin kerja sama resmi serta berlisensi untuk memberikan kemenangan paling besar untuk para player yang mencoba menamatkan permainan yang telah memeberikan kemenangan fantastis saat ini. jangkrik4d slot gacor slot deposit dana demo slot gacor demo slot pg demo pg soft slot deposit gopay slot deposit pulsa slot server jepang
    • LINK DAFTAR KLIK DISINI slot deposit dana 5000 ribu via spaylater  adalah permainan slot dana gacor dengan menyediakan slot server thailand deposit dana. dan slot deposit dana adalah slot terbesar di thailand. anda cukup daftar disini dan mainkan disini slot deposit 5000 ribu via dana sudah dipastikan dengan deposit dana akan membuat akun anda menjadi akun special. situs deposit dana adalah situs yang terpercaya dan terakurat. Slot dana merupakan situs slot deposit dana yang sangat trending dan sangat dikenal luas di masyarakat. dengan adanya MAXWINBET77 anda bisa mendapatkan keuntungan besar setiap hari tanpa adanya potongan  yang pastinya menguntungkan bagi anda. situs slot dana  maxwinbet77 adalah situs slot dana terbaik 2024 dan dijamin mudah maxwin.
    • MELATI88 adalah pilihan terbaik bagi Anda yang ingin bermain slot dengan deposit pulsa tanpa potongan besar sebesar 5000. Dengan berbagai keunggulan yang kami tawarkan, kami siap memberikan Anda pengalaman bermain yang tak terlupakan. Bergabunglah dengan kami sekarang dan mulailah petualangan bermain slot yang seru dan menguntungkan!    
    • SENSASLOT adalah pilihan tepat bagi Anda yang menginginkan pengalaman bermain slot tanpa potongan besar saat deposit menggunakan Dana. Berikut adalah beberapa alasan mengapa Anda harus memilih SENSASLOT: Deposit Dana Tanpa Potongan Kami bangga menjadi salah satu situs slot pertama yang menawarkan deposit menggunakan Dana tanpa potongan besar sebesar 1000 ribu. Ini berarti Anda dapat melakukan deposit sebesar 1000 ribu dan mendapatkan seluruhnya untuk digunakan dalam permainan slot kami. Beragam Pilihan Permainan SENSASLOT menyajikan koleksi permainan slot yang beragam dan menarik dari berbagai provider terkemuka. Mulai dari tema klasik hingga yang paling modern, Anda akan menemukan banyak pilihan permainan yang sesuai dengan selera dan preferensi Anda. Kemudahan Bertransaksi Selain deposit Dana tanpa potongan besar, kami juga menyediakan berbagai metode pembayaran lainnya untuk kenyamanan Anda. Proses deposit dan penarikan dana di SENSASLOT cepat, mudah, dan aman.    
    • GIGASLOT adalah pilihan utama bagi para pemain yang menginginkan pengalaman bermain slot yang menyenangkan dan menguntungkan. Berikut adalah beberapa alasan mengapa Anda harus memilih GIGASLOT: Slot Gacor dengan Maxwin Kami menyajikan koleksi slot gacor terbaik dengan peluang meraih maxwin yang menggiurkan. Dengan berbagai tema permainan dan fitur bonus yang menarik, Anda akan selalu merasa terhibur dan berkesempatan untuk meraih kemenangan besar. Deposit Terjangkau Melalui Bank Permata Kami memahami bahwa kemudahan dalam bertransaksi adalah hal penting bagi para pemain. Oleh karena itu, kami menyediakan layanan deposit melalui Bank Permata dengan nominal deposit yang terjangkau, mulai dari 10K saja! Prosesnya cepat, mudah, dan aman, sehingga Anda dapat langsung memulai petualangan bermain tanpa hambatan. GIGASLOT adalah pilihan terbaik untuk Anda yang ingin menikmati slot gacor dengan maxwin dan melakukan deposit yang terjangkau melalui Bank Permata. Dengan berbagai keunggulan yang kami tawarkan, kami siap memberikan Anda pengalaman bermain .    
  • Topics

×
×
  • Create New...

Important Information

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