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

    • I want to create block with entity, that will have height of 3 or more(configurable) and I tried to change first VoxelShape by increasing collision box on height I want and for changing block height on visual side i tried to configure BlockModelBuilder:  base.element() .from(0, 0, 0) .to(16f, 48f, 16f) .face(Direction.UP).texture("#top").end() .face(Direction.DOWN).texture("#bottom").end() .face(Direction.NORTH).texture("#side").end() .face(Direction.SOUTH).texture("#side").end() .face(Direction.WEST).texture("#side").end() .face(Direction.EAST).texture("#side").end() .end(); but, getting crash with next error: Position y out of range, must be within [-16, 32]. Found: %d [48.0]; Looks like game wont to block height modified by more than 32. Is there any way to fix that problem?
    • As long as the packets you are sending aren't lost, there's nothing wrong with what you're currently doing. Although, this sounds like something that would benefit from you making your own datapack registry instead of trying to arbitrarily sync static maps. Check out `DataPackRegistryEvent.NewRegistry`.
    • Hey all, I've been working a lot with datapacks lately, and I'm wondering what the most efficient way to get said data from server to client is.  I'm currently using packets, but given that a lot of the data I'm storing involves maps along the lines of Map<ResourceLocation, CustomDataType>, it can easily start to get messy if I need to transmit a lot of that data all at once. Recently I started looking into the ReloadableServerResources class, which is where Minecraft stores its built-ins.  I see you can access it via the server from the server's resources.managers, and it seems like this can be done even from the client to appropriately retrieve data from the server, unless I'm misunderstanding.  However, from what I can tell, this only works via built-in methods such as getRecipeManager() or getLootTables(), etc.  These are all SimpleJsonResourceReloadListeners, just like my datapack entries are, so it seems like it could be possible for me to access my datapack entries similarly?  But I don't see anywhere in ReloadableServerResources that stores loaded modded entries, so either I'm looking in the wrong place or it doesn't seem to be a thing. Are packets really the best way of doing this, or am I missing a method that would let me use ReloadableServerResources or something similar?
    • Hi, everyone! I'm new to minecraft modding stuff and want ask you some questions. 1. I checked forge references and saw there com.mojang and net.minecraft (not net.minecraftforge) and as I understand it's original game packages with all minecraft logic inside including renderers and so on, right? 2. Does it mean that forge has a limited set of instruments which doesn't cover all the aspects of the game? If make my question more specific then does forge provide such instruments that allow me totally change minecraft itself, like base mechanics and etc.? Or I have to use "original game packages" to implement such things? 3. I actively learning basic concepts with forge documentation and tutorials. So in my plans make different inventory system like in diabloids. Is that possible with forge? 4. It is last question related to the second one. So how deeply I can change minecraft with forge? I guess all my questions above because of that I haven't globally understanding what forge is and how it works inside and how it works with minecraft. It would be great if you provide some links or topics about it or explain it by yourself but I guess it's to big to be explained in that post at once. Anyway, thank you all for any help!
    • Im trying add to block a hole in center, just a usual block and in center of it on up side, there is should be a hole. I tried to add it via BlockModelBuilder, but its not working. Problem is that it only can change block size outside. I tried it to do with VoxelShape and its working, but its has been on server side and looks like its just changed collision shape, but for client, there is a texture covering this hole. I tried to use: base.renderType("cutout"); and removed some pixels from texture. I thought its should work, but game optimization makes block inside looks transparent. So, only custom model?
  • Topics

×
×
  • Create New...

Important Information

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