Jump to content

[1.7.10] need help with NBT and TileEntity


brsgamer

Recommended Posts

I'm currently making the new year's update for my mod. The update just fixes some bugs and adds a gift block. The idea is simple, when you login into game, you get the gift block and when you place it and break it you get some items, that's the easy part. I want the gift to be given to player only once. I made a tile entity, and read write nbt functions and a boolean. I made the boolean false when the block gets clicked, but the block still spawns in the inventory every time i login. :

 

PlayerLoggedInEvent:

if(TileEntityChristmasGift.canGiveGift){
            e.player.inventory.addItemStackToInventory(new ItemStack(TreeOresBlocks.TreeOresChristmasGift,1,0));
        }

TileEntityChristmasGift:

    public static boolean canGiveGift = true;

    @Override
    public void writeToNBT(NBTTagCompound nbtTagCompound)
    {
        super.writeToNBT(nbtTagCompound);
        nbtTagCompound.setBoolean("canGiveGift", canGiveGift);
    }

    @Override
    public void readFromNBT(NBTTagCompound nbtTagCompound)
    {
        super.readFromNBT(nbtTagCompound);
        this.canGiveGift = nbtTagCompound.getBoolean("canGiveGift");
    }

Block Class:

public TreeOresChristmasGift(Material mat) {
        super(mat);
    }

    public TileEntity createTileEntity(World world, int metadata)
    {
        return new TileEntityChristmasGift();
    }
    public ArrayList<ItemStack> getDrops(World world, int x, int y, int z, int metadata, int fortune) {
        ArrayList<ItemStack> ret = new ArrayList<ItemStack>();

        for(int i = 0; i <4; i++) {
            ret.add(new ItemStack(TreeOresBlocks.TreeOresBossSaplings1,1,i));
            ret.add(new ItemStack(TreeOresBlocks.TreeOresBossSaplings2,1,i));
        }for(int i = 0; i <3; i++) {
            ret.add(new ItemStack(TreeOresBlocks.TreeOresBossSaplings3,1,i));
        }
            return ret;
    }
    public void onBlockClicked(World world, int x, int y, int z, EntityPlayer player) {
        if(world.isRemote){
            TileEntity te = world.getTileEntity(x, y, z); //Get the TileEntity at this location
            if(te instanceof TileEntityChristmasGift){
                ((TileEntityChristmasGift) te).canGiveGift=false;
                world.setTileEntity(x,y,z,te);
            }
        }
    }


    @SideOnly(Side.CLIENT)
    public void registerBlockIcons(IIconRegister iconRegister) {
            this.blockIcon = iconRegister.registerIcon(References.MODID + ":" + this.getTextureName());
    }

Thanks in advance for any help

Link to comment
Share on other sites

Well, considering a TileEntity is apart of a block and not the player, it would be impossible for the game to know if the player got it or not. I would use IExtendedEntityProperties to save a boolean to the player, and check the boolean when the player joins the world. If it is false, give the player the block. If it is true, do not. Again, a TileEntity can't do what you're trying to accomplish.

I am not a cat. I know my profile picture is sexy and amazing beyond anything you could imagine but my cat like features only persist in my fierce eyes. I might be a cat.

Link to comment
Share on other sites

I haven't looked through their code, but I would guess they use some form of IEEP to accomplish that. Here's a tutorial by one of the greats here (coolAlias) about IExtendedEntityProperties:

 

http://www.minecraftforum.net/forums/mapping-and-modding/mapping-and-modding-tutorials/1571567-forge-1-6-4-1-8-eventhandler-and

 

Set yours up, and just do a boolean instead of the int's he sets. Then, it should work as you would like. :)

I am not a cat. I know my profile picture is sexy and amazing beyond anything you could imagine but my cat like features only persist in my fierce eyes. I might be a cat.

Link to comment
Share on other sites

To see where the logic snapped, look back at the "canGiveGift" boolean in the tile entity. It was declared static to make it accessible in the event handler via the class name, but that means that there is one and only one boolean value shared by all of such tile entities, which defeats the purpose of creating a TE instance per person.

 

Since the tile entity instance wouldn't come into existence until the block was finally placed, it would never be available (without some hack) to judge the giving in the first place (and the giving is an item-stack anyway; it doesn't make a block until placed).

 

Happy was right to look at the player for per-player decision-making. I wonder if there might be a way to make the gifting decision based on vanilla player data. You know what date you want to give the gift. You might also set an expiration date when the gift would no longer be given. If you can see the prior login date and server system date, then you might be able to deduce the decision without creating an IEEP. What data is available to you in the login event?

The debugger is a powerful and necessary tool in any IDE, so learn how to use it. You'll be able to tell us more and get better help here if you investigate your runtime problems in the debugger before posting.

Link to comment
Share on other sites

He could use a date to give the player the block, but when they log in and out, they will receive it again. Plus, considering Minecraft runs off of the computer date, a player could abuse this and change his date back between January 1st to get the items again. Honestly, the only concrete way of making sure the player will only get the block once, would be to use IEEP. You could however, go through the TinkersConstruct Github to find out how they did it.

I am not a cat. I know my profile picture is sexy and amazing beyond anything you could imagine but my cat like features only persist in my fierce eyes. I might be a cat.

Link to comment
Share on other sites

OK, If the login event doesn't have the time of the prior login, then that idea won't work.

 

How about using an achievement / statistics mechanism? It might even be possible to make it work for multiple years.

The debugger is a powerful and necessary tool in any IDE, so learn how to use it. You'll be able to tell us more and get better help here if you investigate your runtime problems in the debugger before posting.

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.