Jump to content

[1.7] Persistence with IExtendedEntityProperties


MrRiegel

Recommended Posts

hello,

 

I want to save data to player and I use IEEP. So far, so good.

The data survive logout and death.

But when I go from death screen directly to title screen the data is lost.

How can I fix this?

 

IEEP:

public class PlayerInformation implements IExtendedEntityProperties {

public final static String EXT_PROP_NAME = "PlayerInformation";

private final EntityPlayer player;
private HashMap<String, Boolean> layerBools = new HashMap<String, Boolean>();
private HashMap<String, Boolean> questBools = new HashMap<String, Boolean>();
private HashMap<String, Integer> questNums = new HashMap<String, Integer>();

public PlayerInformation(EntityPlayer player) {
	this.player = player;
	for (String s : BlockedLayers.names) {
		questBools.put(s, false);
		questNums.put(s + "Num", 0);
	}
	for (String s : BlockedLayers.layer) {
		layerBools.put(s, false);
	}

}

public static final void register(EntityPlayer player) {
	player.registerExtendedProperties(PlayerInformation.EXT_PROP_NAME,
			new PlayerInformation(player));
}

public static final PlayerInformation get(EntityPlayer player) {
	return (PlayerInformation) player.getExtendedProperties(EXT_PROP_NAME);
}

@Override
public void saveNBTData(NBTTagCompound compound) {
	NBTTagCompound properties = new NBTTagCompound();

	for (Entry<String, Boolean> entry : layerBools.entrySet()) {
		properties.setBoolean(entry.getKey(), entry.getValue());
	}

	for (Entry<String, Boolean> entry : questBools.entrySet()) {
		properties.setBoolean(entry.getKey(), entry.getValue());
	}

	for (Entry<String, Integer> entry : questNums.entrySet()) {
		properties.setInteger(entry.getKey(), entry.getValue());
	}

	compound.setTag(EXT_PROP_NAME, properties);

}

@Override
public void loadNBTData(NBTTagCompound compound) {
	NBTTagCompound properties = (NBTTagCompound) compound
			.getTag(EXT_PROP_NAME);

	for (Entry<String, Boolean> entry : layerBools.entrySet()) {
		entry.setValue(properties.getBoolean(entry.getKey()));
	}
	for (Entry<String, Boolean> entry : questBools.entrySet()) {
		entry.setValue(properties.getBoolean(entry.getKey()));
	}
	for (Entry<String, Integer> entry : questNums.entrySet()) {
		entry.setValue(properties.getInteger(entry.getKey()));
	}

}

private static final String getSaveKey(EntityPlayer player) {
	return player.getCommandSenderName() + ":" + EXT_PROP_NAME;
}

public static final void loadProxyData(EntityPlayer player) {
	PlayerInformation playerData = PlayerInformation.get(player);
	NBTTagCompound savedData = ServerProxy
			.getEntityData(getSaveKey(player));
	if (savedData != null) {
		playerData.loadNBTData(savedData);
	}
}

public static final void saveProxyData(EntityPlayer player) {
	PlayerInformation playerData = PlayerInformation.get(player);
	NBTTagCompound savedData = new NBTTagCompound();

	playerData.saveNBTData(savedData);
	ServerProxy.storeEntityData(getSaveKey(player), savedData);
}

@Override
public void init(Entity entity, World world) {

}

public HashMap<String, Boolean> getLayerBools() {
	return layerBools;
}

public HashMap<String, Boolean> getQuestBools() {
	return questBools;
}

public HashMap<String, Integer> getQuestNums() {
	return questNums;
}

}

 

SyncHandler:

public class SyncHandler {

@SubscribeEvent
public void onLivingDeathEvent(LivingDeathEvent event) {
	if (!event.entity.worldObj.isRemote
			&& event.entity instanceof EntityPlayer) {
		EntityPlayer player = (EntityPlayer) event.entity;
		NBTTagCompound playerData = new NBTTagCompound();
		((PlayerInformation) (player
				.getExtendedProperties(PlayerInformation.EXT_PROP_NAME)))
				.saveNBTData(playerData);
		ServerProxy.storeEntityData(player.getCommandSenderName(),
				playerData);
		PlayerInformation.saveProxyData((EntityPlayer) event.entity);
	}
}

@SubscribeEvent
public void onEntityConstructing(EntityConstructing event) {
	if (event.entity instanceof EntityPlayer) {
		EntityPlayer player = (EntityPlayer) event.entity;
		if (PlayerInformation.get((EntityPlayer) event.entity) == null) {
			PlayerInformation.register(player);
		}
	}
}

@SubscribeEvent
public void respawn(EntityJoinWorldEvent event) {
	if (!event.entity.worldObj.isRemote
			&& event.entity instanceof EntityPlayer && !event.entity.isDead) {
		EntityPlayer player = (EntityPlayer) event.entity;

		NBTTagCompound playerData = ServerProxy.getEntityData(player
				.getCommandSenderName());
		if (playerData != null) {
			(player.getExtendedProperties(PlayerInformation.EXT_PROP_NAME))
					.loadNBTData(playerData);
		}
		PlayerInformation props = PlayerInformation.get(player);

	}

}
}

 

Proxy:

public class ServerProxy extends CommonProxy implements IGuiHandler {

private static final Map<String, NBTTagCompound> extendedEntityData = new HashMap<String, NBTTagCompound>();

@Override
public Object getServerGuiElement(int ID, EntityPlayer player, World world,
		int x, int y, int z) {
	return null;
}

@Override
public Object getClientGuiElement(int ID, EntityPlayer player, World world,
		int x, int y, int z) {
	return null;
}

public static void storeEntityData(String name, NBTTagCompound compound) {
	extendedEntityData.put(name, compound);
}

public static NBTTagCompound getEntityData(String name) {
	return extendedEntityData.remove(name);
}
}

Link to comment
Share on other sites

these-are-not-the-droids.jpg

 

In simpler words: You are outdated.

 

* IEEP class is cool.

* Clonning data is not cool.

 

What you need:

IEEP = hold data.

EntityConstructing = assign data.

PlayerEvent.Clone = clone data on player re-creation (e.g death).

 

Example:

@SubscribeEvent
public void onPlayerClonning(PlayerEvent.Clone event)
{
IEEP ieepNew = IEEP.get(event.entityPlayer);
IEEP ieepOld = IEEP.get(event.original);

ieepNew.setSomething(ieepOld.getSomething());
}

 

Everything else is old "technique" and you don't need it.

In case you need synchronization - you might wanna look at FML's PlayerEvent#, like: PlayerRespawnEvent, PlayerChangedDimensionEvent.

1.7.10 is no longer supported by forge, you are on your own.

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



×
×
  • Create New...

Important Information

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