Jump to content

More NBT Issues (Clearing on Death)


MistPhizzle
 Share

Recommended Posts

Yet another NBT issue. This time I'm running on the Server.

 

Anywho, I have a custom NBT Tag for a player's element (That they choose after signing in), it works great and all. Once the player dies, the custom NBT tag is cleared.

 

The Methods that set the NBT Tag initially:

 

 

package com.etriacraft.bending;

import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.util.Arrays;

import com.etriacraft.bending.Utils.Element;

import cpw.mods.fml.common.FMLCommonHandler;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;

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 BendingMethods {

public static int AIR = 0;
public static int WATER = 1;
public static int EARTH = 2;
public static int FIRE = 3;
public static int CHI = 3;

static Side side = FMLCommonHandler.instance().getEffectiveSide();

public static void setElement(EntityPlayer player, Element element) {
	NBTTagCompound nbt = player.getEntityData();
	nbt.setString("Element", element.toString());
}

public static boolean hasElement(EntityPlayer player) {
	if (player.getEntityData().hasKey("Element")) {
		return true;
	}
	return false;

	//		if (isBender(player, "air")) return true;
	////		if (isBender(player, "water")) return true;
	//		if (isBender(player, "earth")) return true;
	//		if (isBender(player, "fire")) return true;
	//		if (isBender(player, "chi")) return true;
}

public static Element getElement(EntityPlayer player) {
	NBTTagCompound nbt = player.getEntityData();
	return Element.elementFromString(nbt.getString("Element"));
}

public static boolean isBender(EntityPlayer player, Element element) {
	if (element == Element.Air) {
		if (player.getEntityData().getString("Element").equalsIgnoreCase(Element.Air.toString())) {
			return true;
		}
		return false;

	}
	if (element == Element.Water) {
		if (player.getEntityData().getString("Element").equalsIgnoreCase(Element.Water.toString())) {
			return true;
		}
		return false;
	}
	if (element == Element.Earth) {
		if (player.getEntityData().getString("Element").equalsIgnoreCase(Element.Earth.toString())) {
			return true;
		}
		return false;
	}
	if (element == Element.Fire) {
		if (player.getEntityData().getString("Element").equalsIgnoreCase(Element.Fire.toString())) {
			return true;
		}
		return false;
	}
	if (element == Element.Chi) {
		if (player.getEntityData().getString("Element").equalsIgnoreCase(Element.Chi.toString())) {
			return true;
		}
		return false;
	}
	return false;

}

}

 

 

 

The Command Class (The one that calls the setElement method, the only one)

 

 

package com.etriacraft.bending;

import java.util.Arrays;

import com.etriacraft.bending.ChiBlocking.BlockChi;
import com.etriacraft.bending.Utils.Element;

import cpw.mods.fml.common.FMLCommonHandler;
import cpw.mods.fml.relauncher.Side;

import net.minecraft.command.CommandBase;
import net.minecraft.command.ICommandSender;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.ChatMessageComponent;

public class BendingCommand extends CommandBase {

//Command Aliases
public static String[] chooseAliases = {"choose", "ch", "c"};

public final static String[] airbendingAliases = { "air", "a", "airbender",
	"airbending", "airbend" };
public final static String[] earthbendingAliases = { "earth", "e", "earthbender",
	"earthbending", "earthbend" };
public final static String[] firebendingAliases = { "fire", "f", "firebender",
	"firebending", "firebend" };
public final static String[] waterbendingAliases = { "water", "w", "waterbender",
	"waterbending", "waterbend" };
public final static String[] chiblockingAliases = { "chi", "c", "chiblock",
	"chiblocker", "chiblocking" };
@Override
public String getCommandName() {
	return "bending";
}

@Override
public String getCommandUsage(ICommandSender icommandsender) {
	// TODO Auto-generated method stub
	return null;
}

@Override
public void processCommand(ICommandSender icommandsender, String[] args) {
	Side side = FMLCommonHandler.instance().getEffectiveSide();

	if (!(icommandsender instanceof EntityPlayerMP)) {
		return;
	}
	EntityPlayer player = (EntityPlayerMP) icommandsender;
	NBTTagCompound nbt = player.getEntityData();

	if (args.length == 0) {
		player.addChatMessage("§4/bending choose [element] §f- Picks an element.");
		player.addChatMessage("§4/bending who [Player] §f- Show a player's element.");
		return;
	}

	if (side.isServer()) {
		if (Arrays.asList(chooseAliases).contains(args[0].toLowerCase())) {
			if (args.length != 2) {
				player.addChatMessage("§eProper Usage: /bending choose [Air|Water|Earth|Fire|Chi]");
				return;
			}
			if (BendingMethods.hasElement(player)) {
				player.addChatMessage("§eYou already have an element.");
				return;
			}
			if (Arrays.asList(airbendingAliases).contains(args[1].toLowerCase())) {
				//					BendingMethods.setElement(player, "air");
				player.addChatMessage("§eYou are now an §7airbender.");
				BendingPlayerTracker.benders.put(player.username, Element.Air);
				BendingMethods.setElement(player, Element.Air);
				return;
			} 
			if (Arrays.asList(waterbendingAliases).contains(args[1].toLowerCase())) {
				//					BendingMethods.setElement(player, "water");
				player.addChatMessage("§eYou are now a §bwaterbender.");
				BendingPlayerTracker.benders.put(player.username, Element.Water);
				BendingMethods.setElement(player, Element.Water);
				return;
			}
			if (Arrays.asList(earthbendingAliases).contains(args[1].toLowerCase())) {
				//					BendingMethods.setElement(player, "earth");
				player.addChatMessage("§eYou are now an §aearthbender.");
				BendingPlayerTracker.benders.put(player.username, Element.Earth);
				BendingMethods.setElement(player, Element.Earth);

				return;
			}
			if (Arrays.asList(firebendingAliases).contains(args[1].toLowerCase())) {
				//					BendingMethods.setElement(player, "fire");
				player.addChatMessage("§eYou are now a §cfirebender.");
				BendingPlayerTracker.benders.put(player.username, Element.Fire);
				BendingMethods.setElement(player, Element.Fire);
				return;
			}
			if (Arrays.asList(chiblockingAliases).contains(args[1].toLowerCase())) {
				//					BendingMethods.setElement(player, "chi");
				player.addChatMessage("§eYou are now a §6chiblocker.");
				BendingPlayerTracker.benders.put(player.username, Element.Chi);
				BendingMethods.setElement(player, Element.Chi);
			} else {
				player.addChatMessage("§eProper Usage: /bending choose Air|Water|Earth|Fire|Chi");
				return;
			}
		}
	}
}
} 

 

 

 

Here's my Player Tracker:

 

 

package com.etriacraft.bending;

import java.util.HashMap;

import com.etriacraft.bending.Utils.Element;

import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
import cpw.mods.fml.common.FMLCommonHandler;
import cpw.mods.fml.common.IPlayerTracker;
import cpw.mods.fml.relauncher.Side;

public class BendingPlayerTracker implements IPlayerTracker {

public static HashMap<String, Element> benders = new HashMap<String, Element>();

Side side = FMLCommonHandler.instance().getEffectiveSide();

@Override
public void onPlayerLogin(EntityPlayer player) {
	if (side.isServer()) {
		if (BendingMethods.hasElement(player)) {
			player.addChatMessage("Element: " + BendingMethods.getElement(player).toString());
			benders.put(player.username, BendingMethods.getElement(player));
		}
	}
}

@Override
public void onPlayerLogout(EntityPlayer player) {
	if (side.isServer()) {
		if (benders.containsKey(player.username)) {
			benders.remove(player.username); // Removes the player from the HashMap.
		}
	}

}

@Override
public void onPlayerChangedDimension(EntityPlayer player) {
	// TODO Auto-generated method stub

}

@Override
public void onPlayerRespawn(EntityPlayer player) {
	// TODO Auto-generated method stub

}

}

 

 

 

And lastly, the Element class:

 

 

package com.etriacraft.bending.Utils;

import java.util.Arrays;

import com.etriacraft.bending.BendingCommand;

public enum Element {

Air, Water, Earth, Fire, Chi;

public static Element getElement(String string) {
	for (Element type: Element.values()) {
		if (type.toString().equalsIgnoreCase(string));
		return type;
	}
	return null;
}

public static Element elementFromString(String elementAsString) {
	if (Arrays.asList(BendingCommand.airbendingAliases).contains(elementAsString.toLowerCase())) return Air;
	if (Arrays.asList(BendingCommand.waterbendingAliases).contains(elementAsString.toLowerCase())) return Water;
	if (Arrays.asList(BendingCommand.earthbendingAliases).contains(elementAsString.toLowerCase())) return Earth;
	if (Arrays.asList(BendingCommand.firebendingAliases).contains(elementAsString.toLowerCase())) return Fire;
	if (Arrays.asList(BendingCommand.chiblockingAliases).contains(elementAsString.toLowerCase())) return Chi;

	return null;

}

}

 

 

 

I think that's all the code that needs to be shown here, I can show more if needed. Thanks in advance.

Link to comment
Share on other sites

Using PersistedNBTTag, now it isn't returning anything on my hasElement Method.

 

Updated Code:

 

 

package com.etriacraft.bending;

import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.util.Arrays;

import com.etriacraft.bending.Utils.Element;

import cpw.mods.fml.common.FMLCommonHandler;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;

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 BendingMethods {

static Side side = FMLCommonHandler.instance().getEffectiveSide();

public static void setElement(EntityPlayer player, Element element) {
	NBTTagCompound nbt = player.getEntityData().getCompoundTag(player.PERSISTED_NBT_TAG);
	nbt.setString("Element", element.toString());
}

public static boolean hasElement(EntityPlayer player) {
	if (player.getEntityData().getCompoundTag(player.PERSISTED_NBT_TAG).hasKey("Element")) {
		return true;
	}
	return false;

	//		if (isBender(player, "air")) return true;
	////		if (isBender(player, "water")) return true;
	//		if (isBender(player, "earth")) return true;
	//		if (isBender(player, "fire")) return true;
	//		if (isBender(player, "chi")) return true;
}

public static Element getElement(EntityPlayer player) {
	NBTTagCompound nbt = player.getEntityData().getCompoundTag(player.PERSISTED_NBT_TAG);
	return Element.elementFromString(nbt.getString("Element"));
}

public static boolean isBender(EntityPlayer player, Element element) {
	NBTTagCompound nbt = player.getEntityData().getCompoundTag(player.PERSISTED_NBT_TAG);
	if (element == Element.Air) {
		if (nbt.getString("Element").equalsIgnoreCase(Element.Air.toString())) {
			return true;
		}
		return false;

	}
	if (element == Element.Water) {
		if (nbt.getString("Element").equalsIgnoreCase(Element.Water.toString())) {
			return true;
		}
		return false;
	}
	if (element == Element.Earth) {
		if (nbt.getString("Element").equalsIgnoreCase(Element.Earth.toString())) {
			return true;
		}
		return false;
	}
	if (element == Element.Fire) {
		if (nbt.getString("Element").equalsIgnoreCase(Element.Fire.toString())) {
			return true;
		}
		return false;
	}
	if (element == Element.Chi) {
		if (nbt.getString("Element").equalsIgnoreCase(Element.Chi.toString())) {
			return true;
		}
		return false;
	}
	return false;

}

}

 

 

Link to comment
Share on other sites

If you call getCompoundTag and the tag doesn't yet exist (which is most likely the case with the persisted tag) you will just get a new NBTTagCompound which is not being added to the parent (which is stupid, but whatever).

You have to check for hasKey() on the parent, and if it doesn't have it, add it in manually.

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
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.

 Share



×
×
  • Create New...

Important Information

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