Jump to content

How to use NBTTagCompounds


Mew

Recommended Posts

Like the title states, how? I want to set my own .dat file or something like that with custom information stored in it. How would I go about doing this?

I am Mew. The Legendary Psychic. I behave oddly and am always playing practical jokes.

 

I have also found that I really love making extremely long and extremely but sometimes not so descriptive variables. Sort of like what I just did there xD

Link to comment
Share on other sites

You don't need to. Just use Entity.getEntityData() to get an NBTTagCompound. Then you can set your own custom tags (data) by calling methods on that object, like setString for example. Then you can use getString (or getInteger, or getIntArray, etc.) to get your custom data back.

 

For another way to do it, check out this: http://www.minecraftforge.net/wiki/How_to_use_NBT_Tag_Compound

Link to comment
Share on other sites

You don't need to. Just use Entity.getEntityData() to get an NBTTagCompound. Then you can set your own custom tags (data) by calling methods on that object, like setString for example. Then you can use getString (or getInteger, or getIntArray, etc.) to get your custom data back.

 

For another way to do it, check out this: http://www.minecraftforge.net/wiki/How_to_use_NBT_Tag_Compound

 

Thanks! But what I really wanted was something to do with IExtendedEntityProperties... Is there a tutorial on that?

I am Mew. The Legendary Psychic. I behave oddly and am always playing practical jokes.

 

I have also found that I really love making extremely long and extremely but sometimes not so descriptive variables. Sort of like what I just did there xD

Link to comment
Share on other sites

IExtendedEntityProperties allows you to easily attach data to any entity without the overhead of NBTTagCompounds. Especially if you're storing quite a bit of data getEntityData can be a hassle (and also a bit performance intensive since every single get operation causes a HashMap lookup).

 

How to use it:

1. Make a new class implementing IExtendedEntityProperties. Store any data you want via fields in that class. Write them in writeToNbt and read them in loadFromNbt. These are called automatically at the appropriate time.

2. Subscribe to EntityEvent.EntityConstructing. Check if the entity being constructed is the type of entity you want to attach data to.

3. In the event handler use entity.registerExtendedProperties("someUniqueIdentifierEgModid", new YourDataClass()) to register your properties.

4. From now on whenever you need the data back use (YourDataClass)entity.getExtendedProperties("someUniqueIdentifierEgModid") and then access it's data.

 

(Method names might not be 100% correct as I wrote this without eclipse open).

 

hope that helps :)

 

Well it helps ALOT, except for the fact, how do I then call a method I have created that gets a field? Such as I have the method, getRandomString(); that return a string. How do I call that method?

 

I thought something like this:

(YourDataClass)entity.getExtendedProperties("someUniqueIdentifierEgModid").getRandomString(); but it didn't recognise the method. Do I have to make the methods static?

I am Mew. The Legendary Psychic. I behave oddly and am always playing practical jokes.

 

I have also found that I really love making extremely long and extremely but sometimes not so descriptive variables. Sort of like what I just did there xD

Link to comment
Share on other sites

You need more parentheses. ((YourDataClass)entity.getExtendedProperties("identifier")).getRandomString();

 

How would I do this in this method?

@Override
    public void playerLoggedIn(Player player, NetHandler netHandler,
            INetworkManager manager) {
        
    }

 

What I have at the moment ALWAYS has the players class string set as as "". Even though I know it's saving the data to the NBT.

@Override
    public void playerLoggedIn(Player player, NetHandler netHandler,
            INetworkManager manager) {
        PlayerInformation playerInfo = PlayerInformation.forPlayer((EntityPlayerMP)player);
        if (playerInfo.getPlayersClass() == "") {
            ((EntityPlayerMP) player).openGui(RPG.instance,
                    EnumGui.LoreStartingPage.getIndex(),
                    ((EntityPlayerMP) player).worldObj, 0, 0, 0);
        }// else {
            //((EntityPlayerMP) player)
                    //.sendChatToPlayer("<Mysterious Voice> Welcome back master "
                            //+ playerInfo.getPlayersClass());
        //}

        if (SoundLoader.didSoundsLoad == true) {
            ((EntityPlayerMP) player)
                    .sendChatToPlayer("[MinePG Sound Loader] Loaded Sounds Successfully");
        } else if (SoundLoader.didSoundsLoad == false) {
            ((EntityPlayerMP) player)
                    .sendChatToPlayer("[MinePG Sound Loader] Failed to load one or more sounds");
        }
    }

 

So how would I change my method to suit the needs of the if() statement?

I am Mew. The Legendary Psychic. I behave oddly and am always playing practical jokes.

 

I have also found that I really love making extremely long and extremely but sometimes not so descriptive variables. Sort of like what I just did there xD

Link to comment
Share on other sites

No, the playersClass string is in the PlayerInformation class. So how do I compare strings properly?

I am Mew. The Legendary Psychic. I behave oddly and am always playing practical jokes.

 

I have also found that I really love making extremely long and extremely but sometimes not so descriptive variables. Sort of like what I just did there xD

Link to comment
Share on other sites

oops... I am such a fail at looking :/

 

and um, how and when would I send my packet?

I am Mew. The Legendary Psychic. I behave oddly and am always playing practical jokes.

 

I have also found that I really love making extremely long and extremely but sometimes not so descriptive variables. Sort of like what I just did there xD

Link to comment
Share on other sites

Ok. I have where I change the value here:

 

switch (button.id) {
        case 0:
            playerInfo.setPlayersClass("Archer");
            playerInfo.setCurrency(20);
            mc.thePlayer.inventory.addItemStackToInventory(new ItemStack(
                    ArcherWeapons.bowTraining, 1));
            mc.thePlayer.inventory.addItemStackToInventory(new ItemStack(
                    ArcherWeapons.arrowTraining, 32));
            mc.thePlayer.inventory.addItemStackToInventory(new ItemStack(
                    ArcherArmor.tunicTraining, 1));
            mc.thePlayer.inventory.addItemStackToInventory(new ItemStack(
                    ArcherArmor.legsTraining, 1));
            mc.thePlayer.inventory.addItemStackToInventory(new ItemStack(
                    ArcherArmor.bootsTraining, 1));
            break;
        case 1:
            playerInfo.setPlayersClass("Mage");
            playerInfo.setCurrency(20);
            mc.thePlayer.inventory.addItemStackToInventory(new ItemStack(
                    MageWeapons.staffTraining, 1));
            mc.thePlayer.inventory.addItemStackToInventory(new ItemStack(
                    MageArmor.hatTraining, 1));
            mc.thePlayer.inventory.addItemStackToInventory(new ItemStack(
                    MageArmor.robeTraining, 1));
            mc.thePlayer.inventory.addItemStackToInventory(new ItemStack(
                    MageArmor.sandalsTraining, 1));
            break;
        case 2:
            playerInfo.setPlayersClass("Warrior");
            playerInfo.setCurrency(20);
            mc.thePlayer.inventory.addItemStackToInventory(new ItemStack(
                    WarriorWeapons.swordTraining, 1));
            mc.thePlayer.inventory.addItemStackToInventory(new ItemStack(
                    WarriorArmor.helmetTraining, 1));
            mc.thePlayer.inventory.addItemStackToInventory(new ItemStack(
                    WarriorArmor.cuirassTraining, 1));
            mc.thePlayer.inventory.addItemStackToInventory(new ItemStack(
                    WarriorArmor.greavesTraining, 1));
            mc.thePlayer.inventory.addItemStackToInventory(new ItemStack(
                    WarriorArmor.sabatonsTraining, 1));
            break;
        }

 

That is where it is all done. This is inside the onButtonClicked() or whatever its called.

I am Mew. The Legendary Psychic. I behave oddly and am always playing practical jokes.

 

I have also found that I really love making extremely long and extremely but sometimes not so descriptive variables. Sort of like what I just did there xD

Link to comment
Share on other sites

Uhh, the items ARE usuable. And I have a packet for the playerinformation, but i don't have one for buttons :/ what kind of packet would that be?

 

And, just to make sure that the items are usuable, how do I make it server side?

I am Mew. The Legendary Psychic. I behave oddly and am always playing practical jokes.

 

I have also found that I really love making extremely long and extremely but sometimes not so descriptive variables. Sort of like what I just did there xD

Link to comment
Share on other sites

How do I do it server side?

I am Mew. The Legendary Psychic. I behave oddly and am always playing practical jokes.

 

I have also found that I really love making extremely long and extremely but sometimes not so descriptive variables. Sort of like what I just did there xD

Link to comment
Share on other sites

What I meant was, how do I tell the code to do it server side?

I am Mew. The Legendary Psychic. I behave oddly and am always playing practical jokes.

 

I have also found that I really love making extremely long and extremely but sometimes not so descriptive variables. Sort of like what I just did there xD

Link to comment
Share on other sites

Okay. I think I understand now. I make a packet for the button, and then, depending on the button id, add stuff to the players inventory and stuff like INSIDE the packet? in the execute() method?

I am Mew. The Legendary Psychic. I behave oddly and am always playing practical jokes.

 

I have also found that I really love making extremely long and extremely but sometimes not so descriptive variables. Sort of like what I just did there xD

Link to comment
Share on other sites

This is what is inside a typical packet of mine:

package rpg.network.packet;

import net.minecraft.entity.player.EntityPlayer;

import com.google.common.io.ByteArrayDataInput;
import com.google.common.io.ByteArrayDataOutput;

import cpw.mods.fml.relauncher.Side;


public class PacketChangeClass extends MinePGPacket {

    @Override
    protected void execute(EntityPlayer player, Side side) {
        
    }

    @Override
    protected void readData(ByteArrayDataInput in) {
        
    }

    @Override
    protected void writeData(ByteArrayDataOutput out) {
        
    }

}

 

The execute method is wat actually does stuff...

I am Mew. The Legendary Psychic. I behave oddly and am always playing practical jokes.

 

I have also found that I really love making extremely long and extremely but sometimes not so descriptive variables. Sort of like what I just did there xD

Link to comment
Share on other sites

Well, I tried putting stuff in the execute() method, but it didn't work. I am completely lost...

 

I will reexplain what I am trying to do so that you can give me a detailed explanation of what to do.

 

I am making an RPG mod that has classes (e.g. Archer, Mage, Warrior) and that is set in a GUI that opens when the player opens the world. But my problem is that I can only save the players class for the time the world is open, but as soon as you quit the world, the variable gets reset. So I tired using the IExtendedEntityProperties, and that STILL doesn't seem to be working for saving the variable. I also only want to open the GUI when the world is a new one, and not already loaded once.

 

So my new, revised question is: How would I do above mentioned?

I am Mew. The Legendary Psychic. I behave oddly and am always playing practical jokes.

 

I have also found that I really love making extremely long and extremely but sometimes not so descriptive variables. Sort of like what I just did there xD

Link to comment
Share on other sites

Yeah, sorry. I know I can be a bit of an idiot and a bit slow witted at times, sorry.

 

Here is the code I now have for the GUI in relation to the execute() method:

package rpg.client.gui;

import net.minecraft.client.gui.GuiButton;
import net.minecraft.client.gui.inventory.GuiContainer;
import net.minecraft.item.Item;
import net.minecraftforge.common.MinecraftForge;
import rpg.BaseClassList;
import rpg.client.HudOverlayHandler;
import rpg.gui.ContainerEmpty;
import rpg.network.packet.PacketChoseArcher;
import rpg.network.packet.PacketChoseMage;
import rpg.network.packet.PacketChoseWarrior;
import rpg.playerinfo.PlayerInformation;

public class GuiChooseClass extends GuiContainer {

    String[] starterList;
    Item item;

    public GuiChooseClass() {
        super(new ContainerEmpty());
        starterList = BaseClassList.getStarterStringList();
    }
   
    @Override
    @SuppressWarnings("unchecked")
    public void initGui() {
        super.initGui();
        buttonList.clear();
        for (int i = 0; i < starterList.length; i++) {
            buttonList.add(new GuiButton(i, width / 3 - 100, height / 6 + i
                    * 20, starterList[i]));
        }
    }
    
    @Override
    public void actionPerformed(GuiButton button) {        
        switch(button.id) {
            case 0:
                new PacketChoseArcher().sendToServer();
                break;
            case 1:
                new PacketChoseMage().sendToServer();
                break;
            case 2:
                new PacketChoseWarrior().sendToServer();
                break;
        }
        PlayerInformation playerInfo = PlayerInformation.forPlayer(mc.thePlayer);
        mc.thePlayer.addExperienceLevel(1);
        mc.thePlayer.sendChatToPlayer("[MinePG] You have chosen the path of the "
                + playerInfo.getPlayersClass());
        mc.thePlayer.sendChatToPlayer("<Mysterious Voice> Take care in this world my Friend...");
        mc.thePlayer.sendChatToPlayer("<Mysterious Voice> Many things lurk here that are better left alone");
        mc.thePlayer.sendChatToPlayer("<Mysterious Voice> ...");
        mc.thePlayer.sendChatToPlayer("<Mysterious Voice> You will need to be equiped");
        mc.thePlayer.sendChatToPlayer("<Mysterious Voice> Have this equipment, learn to use it");
        mc.thePlayer.closeScreen();
        MinecraftForge.EVENT_BUS.register(new HudOverlayHandler());
    }

    @Override
    public void drawGuiContainerBackgroundLayer(float par3, int par1, int par2) {
        drawDefaultBackground();
        drawString(fontRenderer, "Welcome to the world of MinePG!!", 3, 10,
                0xFFFFFF);
        drawString(fontRenderer, "Please pick your desired Class:", 3, 20,
                0xFFFFFF);
    }



    @Override
    public void keyTyped(char i, int i1) {
    }
}

 

And here is an example of one of the packets:

package rpg.network.packet;

import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import rpg.config.base.archer.ArcherArmor;
import rpg.config.base.archer.ArcherWeapons;
import rpg.playerinfo.PlayerInformation;

import com.google.common.io.ByteArrayDataInput;
import com.google.common.io.ByteArrayDataOutput;

import cpw.mods.fml.relauncher.Side;

public class PacketChoseArcher extends MinePGPacket {

    @Override
    protected void execute(EntityPlayer player, Side side) {
        if(side.isServer()) {
            PlayerInformation playerInfo = PlayerInformation.forPlayer(player);
            playerInfo.setPlayersClass("Archer");
            playerInfo.setCurrency(20);
            player.inventory.addItemStackToInventory(new ItemStack(
                    ArcherWeapons.bowTraining, 1));
            player.inventory.addItemStackToInventory(new ItemStack(
                    ArcherWeapons.arrowTraining, 32));
            player.inventory.addItemStackToInventory(new ItemStack(
                    ArcherArmor.tunicTraining, 1));
            player.inventory.addItemStackToInventory(new ItemStack(
                    ArcherArmor.legsTraining, 1));
            player.inventory.addItemStackToInventory(new ItemStack(
                    ArcherArmor.bootsTraining, 1));
        }
    }

    @Override
    protected void readData(ByteArrayDataInput in) {
        
    }

    @Override
    protected void writeData(ByteArrayDataOutput out) {
        
    }

}

I am Mew. The Legendary Psychic. I behave oddly and am always playing practical jokes.

 

I have also found that I really love making extremely long and extremely but sometimes not so descriptive variables. Sort of like what I just did there xD

Link to comment
Share on other sites

Same problem as before, it does not save the data that it is supposed to. If I close the world, then reopen it, it relaunches the GUI that should only launch if the playersClass string in the PlayerInformation file (found here: https://github.com/ModderPenguin/MinePG/blob/master/source/minepg/rpg_common/rpg/playerinfo/PlayerInformation.java) is "" or nothing pretty much. (if it was null it would crash. Or so I have been told.)

 

So why is my data, A) not saving/writing (not an issue. I am almost certain) or B) being reset.

 

And is there a better way to launch the GUI?

 

what I have atm:

@Override
    public void playerLoggedIn(Player player, NetHandler netHandler,
            INetworkManager manager) {
        PlayerInformation playerInfo = PlayerInformation
                .forPlayer((EntityPlayerMP) player);
        if (playerInfo.getPlayersClass().equals("")) {
            ((EntityPlayerMP) player).openGui(RPG.instance,
                    EnumGui.LoreStartingPage.getIndex(),
                    ((EntityPlayerMP) player).worldObj, 0, 0, 0);
        } else {
            ((EntityPlayerMP) player)
                    .sendChatToPlayer("<Mysterious Voice> Welcome back master "
                            + playerInfo.getPlayersClass());
        }

        if (SoundLoader.didSoundsLoad == true) {
            ((EntityPlayerMP) player)
                    .sendChatToPlayer("[MinePG Sound Loader] Loaded Sounds Successfully");
        } else if (SoundLoader.didSoundsLoad == false) {
            ((EntityPlayerMP) player)
                    .sendChatToPlayer("[MinePG Sound Loader] Failed to load one or more sounds");
        }
    }

I am Mew. The Legendary Psychic. I behave oddly and am always playing practical jokes.

 

I have also found that I really love making extremely long and extremely but sometimes not so descriptive variables. Sort of like what I just did there xD

Link to comment
Share on other sites

I think I see it...

 

I'm thinking either put it near the BEGINNING, or but a load thing in the loadNBT mehtod?

 

[EDIT]

 

Or is it to do with the fact that I used the variable "compound" to set the tag, but "nbt" for everything else?

I am Mew. The Legendary Psychic. I behave oddly and am always playing practical jokes.

 

I have also found that I really love making extremely long and extremely but sometimes not so descriptive variables. Sort of like what I just did there xD

Link to comment
Share on other sites

So how would I fix this? I know you say to understand, but I only understand by someone explaining it to me. Then I can understand.

 

 

Sorry for being annoying if I seem that way. I am just a bit thick at times :/

I am Mew. The Legendary Psychic. I behave oddly and am always playing practical jokes.

 

I have also found that I really love making extremely long and extremely but sometimes not so descriptive variables. Sort of like what I just did there xD

Link to comment
Share on other sites

I FIXED IT!!!!  ;D ;D ;D ;D

 

But now I have the problem of some other things not saving... Like the level of the player. The players level gets set DOWN one level on world reloaded. What am I doing wrong? same code as before except I changed

compound.setCompoundTag(IDENTIFIER, player.getEntityData());

to:

compound.setCompoundTag(IDENTIFIER, nbt);

 

Nothing has changed except that. So what am I doing wrong to have it stuff up the players level?

I am Mew. The Legendary Psychic. I behave oddly and am always playing practical jokes.

 

I have also found that I really love making extremely long and extremely but sometimes not so descriptive variables. Sort of like what I just did there xD

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

    • They were already updated, and just to double check I even did a cleanup and fresh update from that same page. I'm quite sure drivers are not the problem here. 
    • i tried downloading the drivers but it says no AMD graphics hardware has been detected    
    • Update your AMD/ATI drivers - get the drivers from their website - do not update via system  
    • As the title says i keep on crashing on forge 1.20.1 even without any mods downloaded, i have the latest drivers (nvidia) and vanilla minecraft works perfectly fine for me logs: https://pastebin.com/5UR01yG9
    • Hello everyone, I'm making this post to seek help for my modded block, It's a special block called FrozenBlock supposed to take the place of an old block, then after a set amount of ticks, it's supposed to revert its Block State, Entity, data... to the old block like this :  The problem I have is that the system breaks when handling multi blocks (I tried some fix but none of them worked) :  The bug I have identified is that the function "setOldBlockFields" in the item's "setFrozenBlock" function gets called once for the 1st block of multiblock getting frozen (as it should), but gets called a second time BEFORE creating the first FrozenBlock with the data of the 1st block, hence giving the same data to the two FrozenBlock :   Old Block Fields set BlockState : Block{minecraft:black_bed}[facing=east,occupied=false,part=head] BlockEntity : net.minecraft.world.level.block.entity.BedBlockEntity@73681674 BlockEntityData : id:"minecraft:bed",x:3,y:-60,z:-6} Old Block Fields set BlockState : Block{minecraft:black_bed}[facing=east,occupied=false,part=foot] BlockEntity : net.minecraft.world.level.block.entity.BedBlockEntity@6d1aa3da BlockEntityData : {id:"minecraft:bed",x:2,y:-60,z:-6} Frozen Block Entity set BlockState : Block{minecraft:black_bed}[facing=east,occupied=false,part=foot] BlockPos{x=3, y=-60, z=-6} BlockEntity : net.minecraft.world.level.block.entity.BedBlockEntity@6d1aa3da BlockEntityData : {id:"minecraft:bed",x:2,y:-60,z:-6} Frozen Block Entity set BlockState : Block{minecraft:black_bed}[facing=east,occupied=false,part=foot] BlockPos{x=2, y=-60, z=-6} BlockEntity : net.minecraft.world.level.block.entity.BedBlockEntity@6d1aa3da BlockEntityData : {id:"minecraft:bed",x:2,y:-60,z:-6} here is the code inside my custom "freeze" item :    @Override     public @NotNull InteractionResult useOn(@NotNull UseOnContext pContext) {         if (!pContext.getLevel().isClientSide() && pContext.getHand() == InteractionHand.MAIN_HAND) {             BlockPos blockPos = pContext.getClickedPos();             BlockPos secondBlockPos = getMultiblockPos(blockPos, pContext.getLevel().getBlockState(blockPos));             if (secondBlockPos != null) {                 createFrozenBlock(pContext, secondBlockPos);             }             createFrozenBlock(pContext, blockPos);             return InteractionResult.SUCCESS;         }         return super.useOn(pContext);     }     public static void createFrozenBlock(UseOnContext pContext, BlockPos blockPos) {         BlockState oldState = pContext.getLevel().getBlockState(blockPos);         BlockEntity oldBlockEntity = oldState.hasBlockEntity() ? pContext.getLevel().getBlockEntity(blockPos) : null;         CompoundTag oldBlockEntityData = oldState.hasBlockEntity() ? oldBlockEntity.serializeNBT() : null;         if (oldBlockEntity != null) {             pContext.getLevel().removeBlockEntity(blockPos);         }         BlockState FrozenBlock = setFrozenBlock(oldState, oldBlockEntity, oldBlockEntityData);         pContext.getLevel().setBlockAndUpdate(blockPos, FrozenBlock);     }     public static BlockState setFrozenBlock(BlockState blockState, @Nullable BlockEntity blockEntity, @Nullable CompoundTag blockEntityData) {         BlockState FrozenBlock = BlockRegister.FROZEN_BLOCK.get().defaultBlockState();         ((FrozenBlock) FrozenBlock.getBlock()).setOldBlockFields(blockState, blockEntity, blockEntityData);         return FrozenBlock;     }  
  • Topics

×
×
  • Create New...

Important Information

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