Jump to content

[1.10.2] Java Code Design and Capability/Packets


Recommended Posts

Nevermind, apparently inquiring about a better design is not covered here. Thanks please ignore


[spoiler=Original Post]I know here we don't do "Java" that's something to learn on your own, however I'm not asking for "How do I use Syntax" but a more of "How can I design this to work dynamically"


I'm asking because for syncing data to and from sever/client requires packets, not only that but writing/reading to the NBT. I had created an Attributes Capability which was easy if I just had an Int[] with some static named indexes and tags (which honestly I should make into an Enum that has index + tag)


Now I'm creating seperate MobExtra and PlayerExtra capabilities that will work with the attributes (attributes are generic enough everyone can get them) my challenge is now with the PlayerExtra fields are not stored as Arrays[] but as normal variables. This makes me do something stupid like this:



	public NBTBase writeData()
	NBTTagCompound tag = new NBTTagCompound();

	tag.setInteger("currentClass", currentClass);
	tag.setInteger("currentProfession", currentProfession);
	tag.setInteger("skill1", skill1);
	tag.setInteger("skill2", skill2);
	tag.setInteger("gold", gold);

	tag.setFloat("bonusHp", bonusHp);
	tag.setFloat("bonusMana", bonusMana);
	tag.setFloat("bonusStam", bonusStam);
	tag.setFloat("bonusHpRegen", bonusHpRegen);
	tag.setFloat("bonusManaRegen", bonusManaRegen);
	tag.setFloat("bonusStamRegen", bonusStamRegen);
	tag.setFloat("currentMana", currentMana);
	tag.setFloat("currentStam", currentStam);

	return tag;

public void readData(NBTBase nbt)
	NBTTagCompound tag = (NBTTagCompound)nbt;

	currentClass = tag.getInteger("currentClass");
	currentProfession = tag.getInteger("currentProfession");
	skill1 = tag.getInteger("skill1");
	skill2 = tag.getInteger("skill2");
	gold = tag.getInteger("gold");

	bonusHp = tag.getFloat("bonusHp");
	bonusMana = tag.getFloat("bonusMana");
	bonusStam = tag.getFloat("bonusStam");
	bonusHpRegen = tag.getFloat("bonusHpRegen");
	bonusManaRegen = tag.getFloat("bonusManaRegen");
	bonusStamRegen = tag.getFloat("bonusStamRegen");
	currentMana = tag.getFloat("currentMana");
	currentStam = tag.getFloat("currentStam");


public Object[] compressData()
	Object[] data = new Object[numberOfInts + numberOfFloats];
	int indexer = 0;
	data[indexer++] = currentClass;
	data[indexer++] = currentProfession;
	data[indexer++] = skill1;
	data[indexer++] = skill2;
	data[indexer++] = gold;

	data[indexer++] = bonusHp;
	data[indexer++] = bonusMana;
	data[indexer++] = bonusStam;
	data[indexer++] = bonusHpRegen;
	data[indexer++] = bonusManaRegen;
	data[indexer++] = bonusStamRegen;
	data[indexer++] = currentMana;
	data[indexer++] = currentStam;

	return data;

public void decompressData(Object[] data)
	int indexer = 0;
	currentClass = (int)data[indexer++];
	currentProfession = (int)data[indexer++];
	skill1 = (int)data[indexer++];
	skill2 = (int)data[indexer++];
	gold = (int)data[indexer++];

	bonusHp = (float)data[indexer++];
	bonusMana = (float)data[indexer++];
	bonusStam = (float)data[indexer++];
	bonusHpRegen = (float)data[indexer++];
	bonusManaRegen = (float)data[indexer++];
	bonusStamRegen = (float)data[indexer++];
	currentMana = (float)data[indexer++];
	currentStam = (float)data[indexer++];

public static int dataSize()
	return numberOfInts + numberOfFloats;

	Object[] data = new Object[(CapPlayerExtraData.dataSize())];

public CapPlayerExtraPacket(){}

public CapPlayerExtraPacket(Object[] data)
	this.data = data;

public void fromBytes(ByteBuf buf)
	for (int index = 0; index < CapPlayerExtraData.numberOfInts; index++)
		data[index] = buf.readInt();

	for (int index = CapPlayerExtraData.numberOfInts; index < CapPlayerExtraData.dataSize(); index++)
		data[index] = buf.readFloat();

public void toBytes(ByteBuf buf)
	for (int index = 0; index < CapPlayerExtraData.numberOfInts; index++)
		buf.writeInt((int) data[index]);

	for (int index = CapPlayerExtraData.numberOfInts; index < CapPlayerExtraData.dataSize(); index++)
		buf.writeFloat((float) data[index]);



My TL;DR is If I have over 10 vars that are not in a List<> or Array[] what's and easy way to create a FiFo system that understands what datatype the injected variable is. So when Reading/Writing to NBT or message Buffer I simply just need to use loops to go through, making it so I just have to remember to add/remove from the FiFo System if I make or remove variables.


Link to comment
Share on other sites

IMO if at any point you, as a programmer have to "remember that I did X" you did it wrong.

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.


Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.


DO NOT PM ME WITH PROBLEMS. No help will be given.

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.

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.


  • Create New...

Important Information

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