The Meaning of Life, the Universe, and Everything.
Location:
entityPlayer.getPosition();
Join Date:
12/16/2012
Posts:
57
Minecraft:
nielsstrychi
Xbox:
nielsstrychi
PSN:
nielsstrychi
Member Details
Hi
i try to make a variable that switch between on and off, this to remeber if a spell is already learned or not, but i cant change the integer from NBT, i think this is not triggered for some reason "modTag.setInteger" i checked with a function to write on console to check if it adds + 1 for testing but it looks like its not changing, i also checked with NBT explorer en when i manaul change the interger to 1 for spell learned it keeps triggering integer 0 that stands for not learend yet, i first worked with booleans true/false but they had the same problem, im not used to work with NBT and cant really find it out my self, can i have some tips on how i can fix this or an altarnatieve way to program a variable that i can save with NBT, i used this NBT code to save https://github.com/Mitchellbrine/AdvModdingTutorial/blob/master/src/main/java/mc/Mitchellbrine/advTutorial/AdvancedTutorial.java
public static final String Spell_2 = "test4";
@SubscribeEvent
@Override
public ActionResult<ItemStack> onItemRightClick(ItemStack itemStackIn, World worldIn, EntityPlayer playerIn, EnumHand hand) {
if (!worldIn.isRemote) {
if (itemStackIn.getItem() == (RegisterItemSet_Magic.adana_ani)) {
NBTTagCompound persistTag = null;
if (playerIn.getEntityData().hasKey(playerIn.PERSISTED_NBT_TAG)) {
persistTag = playerIn.getEntityData().getCompoundTag(playerIn.PERSISTED_NBT_TAG);
} else {
persistTag = new NBTTagCompound();
playerIn.getEntityData().setTag(playerIn.PERSISTED_NBT_TAG, persistTag);
}
NBTTagCompound modTag = null;
if (playerIn.getEntityData().hasKey("Spells")) {
modTag = persistTag.getCompoundTag("Spells");
} else {
modTag = new NBTTagCompound();
persistTag.setTag("Spells", modTag);
}
int integer = modTag.getInteger(Spell_2);
System.err.println("Check " + integer);
if (/*!modTag.hasKey(Spell_2) || */ integer == 0) {
--itemStackIn.stackSize;
modTag.setInteger(Spell_2, 1); System.err.println("Check " + integer);
playerIn.addStat(AchievementList.OPEN_INVENTORY, 1);
playerIn.addChatMessage(new TextComponentString("You Learned a New Spell " + TextFormatting.GREEN + Spell_2 + TextFormatting.WHITE + "."));}
if (/*modTag.hasKey(Spell_2) || */integer == 1) { //modTag.setBoolean(Spell_2, false);
playerIn.addChatMessage(new TextComponentString("You Already Learned " + TextFormatting.YELLOW + Spell_2 + TextFormatting.WHITE + "."));}
}
}
return super.onItemRightClick(itemStackIn, worldIn, playerIn, hand);
}
could you post your full updated code? I think your issue might still be related to an incorrect order. Regarding the code above you're not saving anything that's not an empty tag.
could you post your full updated code? I think your issue might still be related to an incorrect order. Regarding the code above you're not saving anything that's not an empty tag.
package com.Strychi.TibiaMCMOD.Items.ItemMagic;
import java.util.List;
import com.Strychi.TibiaMCMOD.TibiaMCMOD;
import com.Strychi.TibiaMCMOD.Items.RegisterItems.RegisterItemSet_Magic;
import com.Strychi.TibiaMCMOD.Items.RegisterItems.RegisterItemSet_Tools;
import com.Strychi.TibiaMCMOD.WORK.var;
import com.Strychi.TibiaMCMOD.WORK.Gui.Inventory;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.stats.AchievementList;
import net.minecraft.util.ActionResult;
import net.minecraft.util.EnumHand;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.TextComponentString;
import net.minecraft.util.text.TextFormatting;
import net.minecraft.world.World;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.entity.living.LivingEvent;
import net.minecraftforge.fml.common.eventhandler.Event;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
public class ItemLearnSpell extends Item {
public ItemLearnSpell() {
super();
}
public static final String Spell_1 = "test";
public static final String Spell_2 = "test4";
public static final String Spell_3 = "test";
public static final String Spell_4 = "test";
public static final String Spell_5 = "test";
public static final String Spell_6 = "test";
public static final String Spell_7 = "test";
public static final String Spell_8 = "test";
public static final String Spell_9 = "test";
public static final String Spell_10 = "test";
public static final String Spell_11 = "test";
@SubscribeEvent
@Override
public ActionResult<ItemStack> onItemRightClick(ItemStack itemStackIn, World worldIn, EntityPlayer playerIn, EnumHand hand) {
if (!worldIn.isRemote) {
if (itemStackIn.getItem() == (RegisterItemSet_Magic.adana_ani)) {
NBTTagCompound persistTag = null;
if (playerIn.getEntityData().hasKey(playerIn.PERSISTED_NBT_TAG)) {
persistTag = playerIn.getEntityData().getCompoundTag(playerIn.PERSISTED_NBT_TAG);
} else {
persistTag = new NBTTagCompound();
playerIn.getEntityData().setTag(playerIn.PERSISTED_NBT_TAG, persistTag);
}
NBTTagCompound modTag = null;
if (playerIn.getEntityData().hasKey("Spells")) {
modTag = persistTag.getCompoundTag("Spells");
persistTag.setTag("Spells", modTag);
} else {
modTag = new NBTTagCompound();
persistTag.setTag("Spells", modTag);
}
System.err.println("Check " + modTag.getInteger(Spell_2));
persistTag = playerIn.getEntityData().getCompoundTag(playerIn.PERSISTED_NBT_TAG);
modTag = persistTag.getCompoundTag("Spells");
int integer = modTag.getInteger(Spell_2);
System.err.println("Check " + integer);
if (/*!modTag.hasKey(Spell_2) || */ integer == 0) {
--itemStackIn.stackSize;
System.err.println("Check " + integer);
integer = 1;
System.err.println("Check " + integer);
persistTag.setTag("Spells", modTag);
modTag.setInteger(Spell_2, integer);
System.err.println("Check " + integer);
playerIn.addStat(AchievementList.OPEN_INVENTORY, 1);
playerIn.addChatMessage(new TextComponentString("You Learned a New Spell " + TextFormatting.GREEN + Spell_2 + TextFormatting.WHITE + "."));}
else if (/*modTag.hasKey(Spell_2) || */integer == 1) { //modTag.setBoolean(Spell_2, false);
playerIn.addChatMessage(new TextComponentString("You Already Learned " + TextFormatting.YELLOW + Spell_2 + TextFormatting.WHITE + "."));}
}
}
return super.onItemRightClick(itemStackIn, worldIn, playerIn, hand);
}
Hi
i try to make a variable that switch between on and off, this to remeber if a spell is already learned or not, but i cant change the integer from NBT, i think this is not triggered for some reason "modTag.setInteger" i checked with a function to write on console to check if it adds + 1 for testing but it looks like its not changing, i also checked with NBT explorer en when i manaul change the interger to 1 for spell learned it keeps triggering integer 0 that stands for not learend yet, i first worked with booleans true/false but they had the same problem, im not used to work with NBT and cant really find it out my self, can i have some tips on how i can fix this or an altarnatieve way to program a variable that i can save with NBT, i used this NBT code to save https://github.com/Mitchellbrine/AdvModdingTutorial/blob/master/src/main/java/mc/Mitchellbrine/advTutorial/AdvancedTutorial.java
Topic of the mod i am working on.
http://www.planetminecraft.com/mod/mmorpg-mod---tibia-minecraft-mod---pre-alpha-mc-110-18/
after editing modTag you don't save it anywhere. call persistTag.setTag() after you've finished editing the tags.
That did't work UpcraftLP :/ same situation and i set it in
i Think the problem is modTag.getInteger(Spell_2); is not working it automaticly set its back to 0?
could you post your full updated code? I think your issue might still be related to an incorrect order. Regarding the code above you're not saving anything that's not an empty tag.
here you go sir, And thank you for your help/time
can somebody help me with this problem Please?
Nobody that can help me to make an interger that i can load and save succesfully? without removing it by player death?
That code is not absolutely wrong, but you need to learn that the right order is important.
You need to save the tags to their parent tags AFTER you've finished modifying them, and you need to start from the bottommost layer.
can you show me the correct order? on my code?
1. get the tag, create it if it doesn't exist
2. get the sub tag from the tag, create it if it doesn't exist
3. modify that sub tag
4. save the sub tag to the root tag
5. save the root tag to the player
Found a solution on my own by writing it my own way and it works fine and doest disapear on player dead.
here is the code that works so other can use it for saving booleans or integers without removal on player dead
take a look at the mod i developing with this code was for
http://www.planetminecraft.com/mod/mmorpg-mod---tibia-minecraft-mod---pre-alpha-mc-110-18/