Hi
I need to save a float(Favor) to the world save. I have tried basically every tutorial, and approach to solve this.
I eventually came across some tutorial to save NBT data, which works, but it is attatched/tied to the player, eg across all worlds
This is the code:
package duke_Frans.Isis;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.world.World;
import net.minecraftforge.common.IExtendedEntityProperties;
public class ExtendedPlayer implements IExtendedEntityProperties
{
public final static String EXT_PROP_NAME = "ExtendedPlayer";
private final EntityPlayer player;
public static final int FAVOR_WATCHER = 20;
public ExtendedPlayer(EntityPlayer player) {
this.player = player;
this.player.getDataWatcher().addObject(FAVOR_WATCHER, FavorMeter.favor);
}
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);
}
@Override
public final void saveNBTData(NBTTagCompound compound) {
NBTTagCompound properties = new NBTTagCompound();
properties.setInteger("FavorValue", player.getDataWatcher().getWatchableObjectInt(FAVOR_WATCHER));
compound.setTag(EXT_PROP_NAME, properties);
}
@Override
public final void loadNBTData(NBTTagCompound compound) {
NBTTagCompound properties = (NBTTagCompound) compound.getTag(EXT_PROP_NAME);
player.getDataWatcher().updateObject(FAVOR_WATCHER, properties.getInteger("FavorValue"));
}
@Override
public void init(Entity entity, World world) {}
public final int getFavor() {
return player.getDataWatcher().getWatchableObjectInt(FAVOR_WATCHER);
}
private static final String getSaveKey(EntityPlayer player) {
return player.getCommandSenderName() + ":" + EXT_PROP_NAME;
}
}
package duke_Frans.Isis;
import cpw.mods.fml.common.eventhandler.SubscribeEvent;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraftforge.event.entity.EntityEvent.EntityConstructing;
class FavorEventHandler
{
@SubscribeEvent
public void onEntityConstructing(EntityConstructing event)
{
if (event.entity instanceof EntityPlayer && ExtendedPlayer.get((EntityPlayer) event.entity) == null)
ExtendedPlayer.register((EntityPlayer) event.entity);
if (event.entity instanceof EntityPlayer && event.entity.getExtendedProperties(ExtendedPlayer.EXT_PROP_NAME) == null)
event.entity.registerExtendedProperties(ExtendedPlayer.EXT_PROP_NAME, new ExtendedPlayer((EntityPlayer) event.entity));
}
}
Again as I said, this is basically copy pasted from a tutorial after my own ways of doing it failed, so I could be missing something obvious
Any way to alter this to be saved per world? Or any way I can save data per world? Thanks