Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

Saving data to players on lan


Recommended Posts

Hi, im making a gold coin manager for minecraft 1.6.2 you can add and remove gold. It saves fine for singleplayer and for the player hosting the world over lan, but it doesnt save gold for other players over lan and they have to start again!

 

My question:

How can i save the int gold for a player over lan?

 

Details:

Im using NBTTagCompound to save and load.

 

My GoldHandler class:

 

package ate.handlers;

 

import ate.gui.GuiGold;

import net.minecraft.entity.player.EntityPlayer;

import net.minecraft.nbt.NBTBase;

import net.minecraft.nbt.NBTTagCompound;

import net.minecraft.nbt.NBTTagInt;

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

 

public class GoldHandler {

 

private static int currGold;

 

public GoldHandler(EntityPlayer player) {

NBTTagCompound tag = player.getEntityData();

NBTBase gold = tag.getTag("player" + player.username + "Gold");

if(gold != null) {

currGold = ((NBTTagInt)gold).data;

} else {

currGold = 0;

tag.setInteger("player" + player.username + "Gold", 0);

}

}

 

public static void addGold(EntityPlayer player, int gold) {

int index;

GoldHandler instance;

for(int i=0; i<PlayerHandler.names.size(); i++) {

if(PlayerHandler.names.get(i).equalsIgnoreCase(player.username)) {

index = i;

instance = PlayerHandler.gh.get(i);

break;

}

}

currGold += gold;

NBTTagCompound tag = player.getEntityData();

tag.setInteger("player" + player.username + "Gold", currGold);

}

 

public static void removeGold(EntityPlayer player, int gold) {

int index;

GoldHandler instance;

for(int i=0; i<PlayerHandler.names.size(); i++) {

if(PlayerHandler.names.get(i).equalsIgnoreCase(player.username)) {

index = i;

instance = PlayerHandler.gh.get(i);

break;

}

}

currGold -= gold;

NBTTagCompound tag = player.getEntityData();

tag.setInteger("player" + player.username + "Gold", currGold);

}

 

public static int getCurrentGold(EntityPlayer player) {

NBTTagCompound tag = player.getEntityData();

NBTBase gold = tag.getTag("player" + player.username + "Gold");

if(gold != null) {

return ((NBTTagInt)gold).data;

} else {

return 0;

}

}

 

public static String getCurrentGoldAsString(EntityPlayer player) {

NBTTagCompound tag = player.getEntityData();

NBTBase gold = tag.getTag("player" + player.username + "Gold");

if(gold != null) {

return "" + ((NBTTagInt)gold).data;

} else {

return "0";

}

}

}

 

 

My PlayerHandler class:

 

package ate.handlers;

 

import java.util.ArrayList;

 

import net.minecraft.entity.player.EntityPlayer;

import cpw.mods.fml.common.IPlayerTracker;

 

public class PlayerHandler implements IPlayerTracker {

 

public static ArrayList<GoldHandler> gh = new ArrayList<GoldHandler>();

public static ArrayList<String> names = new ArrayList<String>();

 

@Override

public void onPlayerLogin(EntityPlayer player) {

GoldHandler h = new GoldHandler(player);

gh.add(h);

names.add(player.username);

System.out.println("Player " + player.username + " Joined; GOLD: " + h.getCurrentGoldAsString(player));

}

 

public void onPlayerLogout(EntityPlayer player) {

 

}

 

public void onPlayerChangedDimension(EntityPlayer player) {

 

}

 

public void onPlayerRespawn(EntityPlayer player) {

 

}

}

 

 

Please HELP!!!!    Thanks in advance!

Link to post
Share on other sites

1: i would use IExtendedProperty for that (and EntityConstructingEvent)

2:

 

private static int currGold;

do you know what static means ??? because that would mean that its the SAME amount of gold for everyone

 

and btw theres no difference between LAN and a real server

how to debug 101:http://www.minecraftforge.net/wiki/Debug_101

-hydroflame, author of the forge revolution-

Link to post
Share on other sites

Hi, first of all thanks for replying.

 

I do know what static means and when more that 1 player are on the world they have different gold values so that isnt a problem.

 

Can u pls explain a bit more about the iextendedproperty?

 

Thanks

Link to post
Share on other sites

i can 100% guarantee you that it doesnt work like you want it to

but since you're confident i wont talk about it

 

 

for IExtendedProperty, make a class that implements it and attach it to an entity. this way you will be noticed when an entity is saved to the NBT and you can add your gold there

 

 

ps: [lmgtfy]how to use hashmap in java[/lmgtfy], trust me it will help

how to debug 101:http://www.minecraftforge.net/wiki/Debug_101

-hydroflame, author of the forge revolution-

Link to post
Share on other sites

Make a class that implements IExtendedEntityProperties. Put fields for all your data in there (e.g. a private int gold).

Also decide on an identifier for your properties, your mod id should work just fine.

Then subscribe to the EntityEvent.EntityConstructing event, check if it's a player and register a new instance of your properties there (registerExtendedProperties method in the Entity class). Use your identifier here.

Then to get the data back at any given time just use player.getExtendedProperties and pass it your identifier. It will give you the instance of your properties back, you can safely cast it.

Then in your properties class you have methods read and write to nbt which are called automatically at the right times. Just save and load your data in there from the passed in NBTTagCompound which is basically what will be written to the player.dat.

Link to post
Share on other sites

Like this?:      Its not printing the "Testing 123..."    I tried @ForgeSubscribe, that didnt work either!  :(

 

 

package ate.handlers;

 

import com.google.common.eventbus.Subscribe;

 

import net.minecraft.entity.Entity;

import net.minecraft.nbt.NBTTagCompound;

import net.minecraft.world.World;

import net.minecraftforge.common.IExtendedEntityProperties;

import net.minecraftforge.event.ForgeSubscribe;

import net.minecraftforge.event.entity.EntityEvent;

 

public class Test implements IExtendedEntityProperties {

 

@Subscribe

public void onEntityConstructed(EntityEvent.EntityConstructing event) {

System.out.println("Testing 123...");

}

 

@Override

public void saveNBTData(NBTTagCompound compound) {

 

}

 

@Override

public void loadNBTData(NBTTagCompound compound) {

 

}

 

@Override

public void init(Entity entity, World world) {

 

}

}

 

 

Link to post
Share on other sites

am i the only one who actually traced the code and foudn out that the currGold should NOT be static

 

 

1 everythign in thsi class is static except the constructor (but it cant sooo ... )

 

2:

public static void addGold(EntityPlayer player, int gold) {
      int index;
      GoldHandler instance;
      for(int i=0; i<PlayerHandler.names.size(); i++) {
         if(PlayerHandler.names.get(i).equalsIgnoreCase(player.username)) {
            index = i;
            instance = PlayerHandler.gh.get(i);
            break;
         }
      }
      currGold += gold;
      NBTTagCompound tag = player.getEntityData();
      tag.setInteger("player" + player.username + "Gold", currGold);
   }

what exactly is the variable "instance" suppose to do ? same for "index"

this code is the same as:

public static void addGold(EntityPlayer player, int gold) {
      currGold += gold;
      NBTTagCompound tag = player.getEntityData();
      tag.setInteger("player" + player.username + "Gold", currGold);
   }

 

 

same here ? instance and index are never used (actually most IDE would be whining about this)

public static void removeGold(EntityPlayer player, int gold) {
      int index;
      GoldHandler instance;
      for(int i=0; i<PlayerHandler.names.size(); i++) {
         if(PlayerHandler.names.get(i).equalsIgnoreCase(player.username)) {
            index = i;
            instance = PlayerHandler.gh.get(i);
            break;
         }
      }
      currGold -= gold;
      NBTTagCompound tag = player.getEntityData();
      tag.setInteger("player" + player.username + "Gold", currGold);
   }

is the same as:

public static void removeGold(EntityPlayer player, int gold) {
      currGold -= gold;
      NBTTagCompound tag = player.getEntityData();
      tag.setInteger("player" + player.username + "Gold", currGold);
   }

 

if you want to see how it doesnt work

 

do this:

take 2 player, set their gold to 50 each

add 25 gold to one player

remove 10 gold to the OTHER player

 

 

how to debug 101:http://www.minecraftforge.net/wiki/Debug_101

-hydroflame, author of the forge revolution-

Link to post
Share on other sites
Guest
This topic is now closed to further replies.


×
×
  • Create New...

Important Information

By using this site, you agree to our Privacy Policy.