Jump to content

Recommended Posts

Posted

Hello, I have a problem with loading Items from NBT. I have worked with and loaded NBT data before because it is fairly easy to use, but I have a special case. I don't think that my NBT Loading Code is wrong, but I'll post it anyway. So basically I have a special Inventory that is Individualized for each Player like the crafting table. Essentially I made a bank that has around 4212 slots, and no that number is not a random number. I use a tab system and a scroll bar to navigate and I have a special slot numbering system. I am telling you this because if you look through the code you might be confused. So basically i have all of the slot in the Inv get loaded into an Extended Properties Class Linked to the player. From there, when the player logs out, I save the ItemStacks to the players Extended NBT.(I think that this part works fine too) When I go to load this NBT data when the player logs in, I get a message saying that says, "Successfully Loaded ItemStack: Blah  to Slot #: Blah", because i have a println in my code. This told me that it is at least saving and loading correctly, but when I set the Item Stacks to the slots, The inv is completely empty and when I log off It says that is saved the Item that I put in the Inventory originally. My thoughts are that I may need to sync, the server and the client, but how would I do that easily? Is there a method that can sync Invs easily or will I need to send a packet with a bunch of little info such as ItemID, StackSize, MetaData, ect.

 

I will only post my Extended Properties Class for now because that is where everything is basically happering.

Also sorry that my code might be messy, I have been try ideas to solve this problem and frankly I have been too lazy to clean it up.

 

 

 

package mod.xtronius.rc_mod.lib;

 

import java.util.HashMap;

 

import cpw.mods.fml.common.network.FMLOutboundHandler;

import cpw.mods.fml.relauncher.Side;

import mod.xtronius.rc_mod.rc_mod;

import mod.xtronius.rc_mod.container.BankContainer;

import mod.xtronius.rc_mod.handlers.RCTickHandler;

import mod.xtronius.rc_mod.packetHandling.packets.generalPackets.PacketBankInvSync;

import mod.xtronius.rc_mod.packetHandling.packets.generalPackets.PacketInitCombatStyle;

import mod.xtronius.rc_mod.packetHandling.packets.generalPackets.PacketSwitchCombatStyle;

import mod.xtronius.rc_mod.proxy.CommonProxy;

import mod.xtronius.rc_mod.util.enumClasses.MeleCombatStyles;

import net.minecraft.entity.Entity;

import net.minecraft.entity.player.EntityPlayer;

import net.minecraft.item.Item;

import net.minecraft.item.ItemStack;

import net.minecraft.nbt.NBTTagCompound;

import net.minecraft.nbt.NBTTagList;

import net.minecraft.world.World;

import net.minecraftforge.common.IExtendedEntityProperties;

 

public class ExtendedPlayer implements IExtendedEntityProperties

{

 

private HashMap<String, Integer> playerLvl = new HashMap<String, Integer>();

private HashMap<String, Double> playerExp = new HashMap<String, Double>();

private HashMap<String, Double> playerExpUntilNextLvl = new HashMap<String, Double>();

 

/*Array of the level names for display**/

public static String AttackStr = "Attack", StrengthStr = "Strength", DefenceStr  = "Defense", RangeStr = "Range", PrayerStr = "Prayer", MageStr = "Mage", RuneCraftingStr = "Rune Crafting", ConstructionStr = "Construction", DungeoneeringStr = "Dungeoneering", ConstitutionStr = "Constitution", AgilityStr = "Agility", HerbloreStr = "Herblore", ThievingStr = "Thieving", CraftingStr = "Crafting", FletchingStr = "Fletching", SlayerStr = "Slayer", HuntingStr = "Hunting", MiningStr = "Mining", SmithingStr = "Smithing", FishingStr = "Fishing", CookingStr = "Cooking", FireMakingStr = "Fire Making", WoodCuttingStr = "Wood Cutting", FarmingStr = "Farming", SummoningStr = "Summoning";

public static String[] SkillsStr = new String[] {AttackStr, StrengthStr, DefenceStr, RangeStr, PrayerStr, MageStr, RuneCraftingStr, ConstructionStr, DungeoneeringStr, ConstitutionStr, AgilityStr, HerbloreStr, ThievingStr, CraftingStr, FletchingStr, SlayerStr, HuntingStr, MiningStr, SmithingStr, FishingStr, CookingStr, FireMakingStr, WoodCuttingStr, FarmingStr, SummoningStr};

 

/*Array of the level names for saving**/

public static String AttackNoSpaceStr = "Attack", StrengthNoSpaceStr = "Strength", DefenceNoSpaceStr = "Defense", RangeNoSpaceStr = "Range", PrayerNoSpaceStr = "Prayer", MageNoSpaceStr = "Mage", RuneCraftingNoSpaceStr = "RuneCrafting", ConstructionNoSpaceStr = "Construction", DungeoneeringNoSpaceStr = "Dungeoneering", ConstitutionNoSpaceStr = "Constitution", AgilityNoSpaceStr = "Agility", HerbloreNoSpaceStr = "Herblore", ThievingNoSpaceStr = "Thieving", CraftingNoSpaceStr = "Crafting", FletchingNoSpaceStr = "Fletching", SlayerNoSpaceStr = "Slayer", HuntingNoSpaceStr = "Hunting", MiningNoSpaceStr = "Mining", SmithingNoSpaceStr = "Smithing", FishingNoSpaceStr = "Fishing", CookingNoSpaceStr = "Cooking", FireMakingNoSpaceStr = "FireMaking", WoodCuttingNoSpaceStr = "WoodCutting", FarmingNoSpaceStr = "Farming", SummoningNoSpaceStr = "Summoning";

public static String[] SkillsNoSpaceStr = new String[] {AttackNoSpaceStr, StrengthNoSpaceStr, DefenceNoSpaceStr, RangeNoSpaceStr, PrayerNoSpaceStr, MageNoSpaceStr, RuneCraftingNoSpaceStr, ConstructionNoSpaceStr, DungeoneeringNoSpaceStr, ConstitutionNoSpaceStr, AgilityNoSpaceStr, HerbloreNoSpaceStr, ThievingNoSpaceStr, CraftingNoSpaceStr, FletchingNoSpaceStr, SlayerNoSpaceStr, HuntingNoSpaceStr, MiningNoSpaceStr, SmithingNoSpaceStr, FishingNoSpaceStr, CookingNoSpaceStr, FireMakingNoSpaceStr, WoodCuttingNoSpaceStr, FarmingNoSpaceStr, SummoningStr};

 

public final static String EXT_PROP_NAME = "ExtendedPlayer";

 

private final EntityPlayer player;

private boolean resetNBT = false;

 

private Enum currentMeleAttackStyle = MeleCombatStyles.ATTACK;

private int meleCombatStyleGuiCoolDown = 0;

 

public int attackCoolDown = 0;

 

public ItemStack[] playerBankStorage = new ItemStack[4212];

 

public ExtendedPlayer(EntityPlayer player) {

this.player = player;

//init values to store

 

for(int lvlID = 0; lvlID < this.SkillsNoSpaceStr.length; lvlID++) {

if(this.playerLvl.get(this.SkillsNoSpaceStr[lvlID] + "Lvl") == null && this.playerExp.get(this.SkillsNoSpaceStr[lvlID] + "Exp") == null && this.playerExpUntilNextLvl.get(this.SkillsNoSpaceStr[lvlID] + "ExpUntilNextLvl") == null) {

this.playerLvl.put(this.SkillsNoSpaceStr[lvlID] + "Lvl", 1);

this.playerExp.put(this.SkillsNoSpaceStr[lvlID] + "Exp", (double) 0);

this.playerExpUntilNextLvl.put(this.SkillsNoSpaceStr[lvlID] + "ExpUntilNextLvl", (double) 83);

}

}

}

 

public static final void register(EntityPlayer player) {

player.registerExtendedProperties(ExtendedPlayer.EXT_PROP_NAME, new ExtendedPlayer(player));

}

 

public static final ExtendedPlayer get(EntityPlayer player) {

return (ExtendedPlayer) player.getExtendedProperties(EXT_PROP_NAME);

}

 

private static String getSaveKey(EntityPlayer player) {

return player.getDisplayName() + ":" + EXT_PROP_NAME;

}

 

@Override

public void saveNBTData(NBTTagCompound compound) {

 

NBTTagCompound properties = new NBTTagCompound();

 

if(!this.player.worldObj.isRemote) {

 

/** Null Check Init */

properties.setString("RC_Mod", "NullCheckForNBT");

 

/** Lvl Saving */

for(int lvlID = 0; lvlID < this.SkillsNoSpaceStr.length; lvlID++) {

properties.setInteger(this.SkillsNoSpaceStr[lvlID] + "Lvl", this.playerLvl.get(this.SkillsNoSpaceStr[lvlID] + "Lvl"));

properties.setDouble(this.SkillsNoSpaceStr[lvlID] + "Exp", this.playerExp.get(this.SkillsNoSpaceStr[lvlID] + "Exp"));

properties.setDouble(this.SkillsNoSpaceStr[lvlID] + "ExpUntilNextLvl", this.playerExpUntilNextLvl.get(this.SkillsNoSpaceStr[lvlID] + "ExpUntilNextLvl"));

}

 

properties.setString("MeleAttackStyle", this.currentMeleAttackStyle.toString());

 

compound.setTag(EXT_PROP_NAME, properties);

 

/** Bank Handling */

    NBTTagList nbttaglist = new NBTTagList();

    System.out.println("Saving");

     

        for (int i = 0; i < this.playerBankStorage.length; ++i)

        {

            if (this.playerBankStorage != null)

            {

            System.out.println("Handling Slot " + i + " With the Stack Contenets Of " + this.playerBankStorage);

                NBTTagCompound nbttagcompound1 = new NBTTagCompound();

                nbttagcompound1.setInteger("BankSlot", i);

                this.playerBankStorage.writeToNBT(nbttagcompound1);

                nbttaglist.appendTag(nbttagcompound1);

            }

        }

 

        compound.setTag("BankItems", nbttaglist);

 

}

}

 

@Override

public void loadNBTData(NBTTagCompound compound) {

NBTTagCompound properties = (NBTTagCompound) compound.getTag(EXT_PROP_NAME);

 

if(!this.player.worldObj.isRemote) {

 

/** Null Check */

if(properties.hasKey("RC_Mod") && this.resetNBT == false) {

 

/** Lvl Loading */

for(int lvlID = 0; lvlID < this.SkillsNoSpaceStr.length; lvlID++) {

this.playerLvl.put(this.SkillsNoSpaceStr[lvlID] + "Lvl", properties.getInteger(this.SkillsNoSpaceStr[lvlID] + "Lvl"));

this.playerExp.put(this.SkillsNoSpaceStr[lvlID] + "Exp", properties.getDouble(this.SkillsNoSpaceStr[lvlID] + "Exp"));

this.playerExpUntilNextLvl.put(this.SkillsNoSpaceStr[lvlID] + "ExpUntilNextLvl", properties.getDouble(this.SkillsNoSpaceStr[lvlID] + "ExpUntilNextLvl"));

}

 

this.currentMeleAttackStyle = Enum.valueOf(MeleCombatStyles.class, properties.getString("MeleAttackStyle"));

 

/** Bank Handling */

NBTTagList nbttaglist = (NBTTagList) compound.getTag("BankItems");

for (int i = 0; i < nbttaglist.tagCount(); ++i) {

NBTTagCompound nbttagcompound1 = nbttaglist.getCompoundTagAt(i);

int j = nbttagcompound1.getInteger("BankSlot");

 

if (j >= 0 && j < this.playerBankStorage.length) {

System.out.println("Loading: Stacks To Stack Array");

this.playerBankStorage = ItemStack.loadItemStackFromNBT(nbttagcompound1);

                System.out.println(ItemStack.loadItemStackFromNBT(nbttagcompound1));

}

}

 

for(int slotIndex = 0; slotIndex < this.playerBankStorage.length; slotIndex++) {

BankContainer bank = RCTickHandler.BankContainerMapSERVER.get(this.player);

ItemStack stack = this.playerBankStorage[slotIndex];

if(bank != null) {

if(stack != null) {

System.out.println(stack);

if(slotIndex < 468)

bank.invBank.setInventorySlotContentsIndexed(0, slotIndex, stack);

if(slotIndex < 468 * 2)

bank.invBank.setInventorySlotContentsIndexed(1, slotIndex, stack);

if(slotIndex < 468 * 3)

bank.invBank.setInventorySlotContentsIndexed(2, slotIndex, stack);

if(slotIndex < 468 * 4)

bank.invBank.setInventorySlotContentsIndexed(3, slotIndex, stack);

if(slotIndex < 468 * 5)

bank.invBank.setInventorySlotContentsIndexed(4, slotIndex, stack);

if(slotIndex < 468 * 6)

bank.invBank.setInventorySlotContentsIndexed(5, slotIndex, stack);

if(slotIndex < 468 * 7)

bank.invBank.setInventorySlotContentsIndexed(6, slotIndex, stack);

if(slotIndex < 468 * 8)

bank.invBank.setInventorySlotContentsIndexed(7, slotIndex, stack);

if(slotIndex < 468 * 9)

bank.invBank.setInventorySlotContentsIndexed(8, slotIndex, stack);

 

System.out.println("Setting Stack: " + this.playerBankStorage[slotIndex] + " To Slot: " + slotIndex);

}

}

}

}

}

}

 

private void syncClient(int slotIndex, ItemStack stack) {

int objID = Item.getIdFromItem(stack.getItem());

int stackSize = stack.stackSize;

int meta = stack.getItemDamage();

 

rc_mod.bankInvSyncPacket.get(Side.SERVER).attr(FMLOutboundHandler.FML_MESSAGETARGET).set(FMLOutboundHandler.OutboundTarget.PLAYER);

rc_mod.bankInvSyncPacket.get(Side.SERVER).attr(FMLOutboundHandler.FML_MESSAGETARGETARGS).set(player);

rc_mod.bankInvSyncPacket.get(Side.SERVER).writeOutbound(new PacketBankInvSync(slotIndex, objID, stackSize, meta, true));

}

 

public static void saveProxyData(EntityPlayer player) {

ExtendedPlayer playerData = ExtendedPlayer.get(player);

NBTTagCompound savedData = new NBTTagCompound();

 

playerData.saveNBTData(savedData);

CommonProxy.storeEntityData(getSaveKey(player), savedData);

}

 

public static void loadProxyData(EntityPlayer player) {

ExtendedPlayer playerData = ExtendedPlayer.get(player);

NBTTagCompound savedData = CommonProxy.getEntityData(getSaveKey(player));

 

if(savedData != null) {

playerData.loadNBTData(savedData);

}

//TODO sync the playerData in the Server and the Client by sending many packets

}

 

public int getMeleCombatStyleInt() {

int result = 0;

if(this.currentMeleAttackStyle != null)

if(this.currentMeleAttackStyle == MeleCombatStyles.ATTACK)

result = 0;

if(this.currentMeleAttackStyle == MeleCombatStyles.STRENGTH)

result = 1;

if(this.currentMeleAttackStyle == MeleCombatStyles.DEFENSE)

result = 2;

if(this.currentMeleAttackStyle == MeleCombatStyles.SHARED)

result = 3;

 

return result;

}

 

public Enum getMeleCombatStyle() { if(this.currentMeleAttackStyle != null) return this.currentMeleAttackStyle; return MeleCombatStyles.ATTACK; }

public void setMeleCombatStyle(Enum style) {if(style != null) this.currentMeleAttackStyle = style;}

 

public int getMeleCombatStyleGuiCoolDown() { return meleCombatStyleGuiCoolDown; }

public void setMeleCombatStyleGuiCoolDown(int meleCombatStyleGuiCoolDown) { this.meleCombatStyleGuiCoolDown = meleCombatStyleGuiCoolDown; }

 

public int getLvl(String lvl) { if(this.playerLvl.get(lvl + "Lvl") != null) return this.playerLvl.get(lvl + "Lvl"); return 0; }

public void setLvl(String lvl, int value) { this.playerLvl.put(lvl + "Lvl", value); }

 

public double getExp(String lvl) { if(this.playerExp.get(lvl + "Exp") != null) return this.playerExp.get(lvl + "Exp"); return 0; }

public void setExp(String lvl, double value) { this.playerExp.put(lvl + "Exp", value); }

 

public double getExpUntilNextLvl(String lvl) { if(this.playerExpUntilNextLvl.get(lvl + "ExpUntilNextLvl") != null) return this.playerExpUntilNextLvl.get(lvl + "ExpUntilNextLvl"); return 0; }

public void setExpUntilNextLvl(String lvl, double value) { this.playerExpUntilNextLvl.put(lvl + "ExpUntilNextLvl", value); }

 

@Override

public void init(Entity entity, World world) {}

}

 

 

Don't be afraid to ask question when modding, there are no stupid question! Unless you don't know java then all your questions are stupid!

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.