Jump to content

Recommended Posts

Posted

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.

Posted

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;

}

}

 

 

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



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • UPDATE: this seems to be an Arch-specific issue. Switching to Ubuntu server fixed EVERYTHING.
    • Yes, Attapoll offers a $20 Sign-up bonus for new users using a code (AOVCQ). Enter the Attapoll Referral Code “AOVCQ” and submit. Yes, Attapoll offers $20 Referral Code {AOVCQ} For New Customers. If you are who wish to join Attapoll, then you should use this exclusive Attapoll referral code $20 Signup Bonus Use this Code (AOVCQ) and get $20 Welcome Bonus. You can get a $20 Signup bonus use this referral code {AOVCQ}. You can get a $20 Attpoll Referral Code {AOVCQ}. This Attapoll $20 Referral code is specifically for existing customers and can be redeemed to receive a $20. Enter $20 Attapoll Referral Code {AOVCQ} at checkout. Enjoy $20Welcome Bonus. Use the best Attapoll referral code $20 (AOVCQ) to get up to $20 first time survey as a new user. Complete the survey and get $20 credited on your Attapoll account. Plus, refer your friend to earn 10% commission on their earning If you're on the hunt for a little extra cash or some cool rewards without too much hassle, you've landed in the right place. Today, we're diving into the world of Attapoll referral codes, a nifty way to boost your earnings while taking surveys. Use this Attapoll Referral Link or code {{AOVCQ}} to get a $20 sign up bonus.
    • Yes, Attapoll offers a $20 Sign-up bonus for new users using a code (AOVCQ). Enter the Attapoll Referral Code “AOVCQ” and submit. Yes, Attapoll offers $20 Referral Code {AOVCQ} For New Customers. If you are who wish to join Attapoll, then you should use this exclusive Attapoll referral code $20 Signup Bonus Use this Code (AOVCQ) and get $20 Welcome Bonus. You can get a $20 Signup bonus use this referral code {AOVCQ}. You can get a $20 Attpoll Referral Code {AOVCQ}. This Attapoll $20 Referral code is specifically for existing customers and can be redeemed to receive a $20. Enter $20 Attapoll Referral Code {AOVCQ} at checkout. Enjoy $20Welcome Bonus. Use the best Attapoll referral code $20 (AOVCQ) to get up to $20 first time survey as a new user. Complete the survey and get $20 credited on your Attapoll account. Plus, refer your friend to earn 10% commission on their earning If you're on the hunt for a little extra cash or some cool rewards without too much hassle, you've landed in the right place. Today, we're diving into the world of Attapoll referral codes, a nifty way to boost your earnings while taking surveys. Use this Attapoll Referral Link or code {{AOVCQ}} to get a $20 sign up bonus.
    • Yes, Attapoll offers a $20 Sign-up bonus for new users using a code (AOVCQ). Enter the Attapoll Referral Code “AOVCQ” and submit. Yes, Attapoll offers $20 Referral Code {AOVCQ} For New Customers. If you are who wish to join Attapoll, then you should use this exclusive Attapoll referral code $20 Signup Bonus Use this Code (AOVCQ) and get $20 Welcome Bonus. You can get a $20 Signup bonus use this referral code {AOVCQ}. You can get a $20 Attpoll Referral Code {AOVCQ}. This Attapoll $20 Referral code is specifically for existing customers and can be redeemed to receive a $20. Enter $20 Attapoll Referral Code {AOVCQ} at checkout. Enjoy $20Welcome Bonus. Use the best Attapoll referral code $20 (AOVCQ) to get up to $20 first time survey as a new user. Complete the survey and get $20 credited on your Attapoll account. Plus, refer your friend to earn 10% commission on their earning If you're on the hunt for a little extra cash or some cool rewards without too much hassle, you've landed in the right place. Today, we're diving into the world of Attapoll referral codes, a nifty way to boost your earnings while taking surveys. Use this Attapoll Referral Link or code {{AOVCQ}} to get a $20 sign up bonus.
    • Yes, Attapoll offers a $20 Sign-up bonus for new users using a code (AOVCQ). Enter the Attapoll Referral Code “AOVCQ” and submit. Yes, Attapoll offers $20 Referral Code {AOVCQ} For New Customers. If you are who wish to join Attapoll, then you should use this exclusive Attapoll referral code $20 Signup Bonus Use this Code (AOVCQ) and get $20 Welcome Bonus. You can get a $20 Signup bonus use this referral code {AOVCQ}. You can get a $20 Attpoll Referral Code {AOVCQ}. This Attapoll $20 Referral code is specifically for existing customers and can be redeemed to receive a $20. Enter $20 Attapoll Referral Code {AOVCQ} at checkout. Enjoy $20Welcome Bonus. Use the best Attapoll referral code $20 (AOVCQ) to get up to $20 first time survey as a new user. Complete the survey and get $20 credited on your Attapoll account. Plus, refer your friend to earn 10% commission on their earning If you're on the hunt for a little extra cash or some cool rewards without too much hassle, you've landed in the right place. Today, we're diving into the world of Attapoll referral codes, a nifty way to boost your earnings while taking surveys. Use this Attapoll Referral Link or code {{AOVCQ}} to get a $20 sign up bonus.
  • Topics

  • Who's Online (See full list)

    • There are no registered users currently online
×
×
  • Create New...

Important Information

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