Jump to content

Setting Variable Values When Using A Class That Extends IExtendEntityProperties


Recommended Posts

Posted

As the title states, I can't seem to do this. Either I am extremely tired and should try later, or I have just gone noob mode. I hope its the first option. And before you do start on the latter, I have getter/setter methods for the variables and they don't seem to work.

 

Therefore I am lost. Thanks for any help that may or may not come.

 

PlayerInformation class, the one that extends IExtendedEntityProperties:

package rpg.playerinfo;

import net.minecraft.entity.Entity;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.world.World;
import net.minecraftforge.common.IExtendedEntityProperties;

public final class PlayerInformation implements IExtendedEntityProperties {
    
public static final String IDENTIFIER = "minpg_playerinfo";

public static PlayerInformation forPlayer(Entity player) {
	return (PlayerInformation)player.getExtendedProperties(IDENTIFIER);
}

// called by the ASM hook in EntityPlayer.clonePlayer
public static void handlePlayerClone(EntityPlayer source, EntityPlayer target) {
	target.registerExtendedProperties(IDENTIFIER, source.getExtendedProperties(IDENTIFIER));
}

public static final int MAX_KARMA_VALUE = 99999999;

public boolean dirty = true;
public boolean hasClassBeenChosen = false;
public float karma = 0;
public byte[] eventAmounts = new byte[PlayerInformation.CountableKarmaEvent.values().length];
public String playersClass;
public int danris = 0;

private final EntityPlayer player;

public PlayerInformation(EntityPlayer player) {
	this.player = player;
}

@Override
public void init(Entity entity, World world) {
	// nothing for now
}

@Override
public void saveNBTData(NBTTagCompound nbtPlayer) {
	NBTTagCompound nbt = new NBTTagCompound();

	nbt.setString("playersClass", playersClass);
	nbt.setBoolean("hasClassBeenChosen", hasClassBeenChosen);
	nbt.setInteger("danris", danris);
	nbt.setFloat("karma", karma);

	NBTTagList eventList = new NBTTagList();
	for (int i = 0; i < eventAmounts.length; i++) {
		NBTTagCompound evtInfo = new NBTTagCompound();
		evtInfo.setByte("id", (byte)i);
		evtInfo.setByte("value", eventAmounts[i]);
		eventList.appendTag(evtInfo);
	}
	nbt.setTag("events", eventList);

	//nbtPlayer.setCompoundTag(IDENTIFIER, nbt);
	nbtPlayer.setCompoundTag(IDENTIFIER, player.getEntityData());
}

@Override
public void loadNBTData(NBTTagCompound playerNbt) {
	NBTTagCompound nbt = playerNbt.getCompoundTag(IDENTIFIER);

	playersClass = nbt.getString("playersClass");
	hasClassBeenChosen = nbt.getBoolean("hasClassBeenChosen");
	danris = nbt.getInteger("danris");
	karma = nbt.getFloat("karma");

	NBTTagList eventList = nbt.getTagList("events");
	for (int i = 0; i < eventList.tagCount(); i++) {
		NBTTagCompound evtInfo = (NBTTagCompound)eventList.tagAt(i);
		byte eventId = evtInfo.getByte("id");
		if (eventId >= 0 && eventId < eventAmounts.length) {
			eventAmounts[eventId] = evtInfo.getByte("value");
		}
	}
}

public boolean getHasClassBeenChosen() {
	return hasClassBeenChosen;
}

public boolean setHasClassBeenChosen(boolean hasClassBeenChosen) {
	if(this.hasClassBeenChosen != hasClassBeenChosen) {
		this.hasClassBeenChosen = hasClassBeenChosen;
		setDirty();
	}
	return this.hasClassBeenChosen;
}

public String getPlayersClass() {
	return playersClass;
}

public String setPlayersClass(String playersClass) {
	if(this.playersClass != playersClass) {
		this.playersClass = playersClass;
		setDirty();
	}

	return this.playersClass;
}

public String modifyPlayersClass(String classChangingTo) {
	return setPlayersClass(classChangingTo);
}

public float getKarma() {
	return karma;
}

public float setKarma(float karma) {
	if (this.karma != karma) {
		this.karma = karma;
		if (this.karma > MAX_KARMA_VALUE) {
			this.karma = MAX_KARMA_VALUE;
		}
		if (this.karma < -MAX_KARMA_VALUE) {
			this.karma = -MAX_KARMA_VALUE;
		}
		setDirty();
	}

	return this.karma;
}

public float modifyKarma(float modifier) {
	player.worldObj.playSoundAtEntity(player, "minepgkarma.karma" + (modifier < 0 ? "down" : "up"), 1, 1);

	return setKarma(karma + modifier);
}

public float modifyKarmaWithMax(float modifier, float max) {
	if (karma < max) {
		modifyKarma(modifier);
	}

	return karma;
}

public float modifyKarmaWithMin(float modifier, float min) {
	if (karma > min) {
		modifyKarma(modifier);
	}

	return karma;
}

public byte getEventAmount(CountableKarmaEvent event) {
	return eventAmounts[event.ordinal()];
}

public boolean setEventAmount(CountableKarmaEvent event, int amount) {
	if (amount < event.getMaxCount() && eventAmounts[event.ordinal()] != amount) {
		eventAmounts[event.ordinal()] = (byte)amount;
		setDirty();
		return true;
	} else {
		return false;
	}
}

public boolean increaseEventAmount(PlayerInformation.CountableKarmaEvent event) {
	return setEventAmount(event, eventAmounts[event.ordinal()] + 1);
}

public static enum CountableKarmaEvent {
	PIGMEN_ATTACK(1), CREATE_SNOWGOLEM(2), CREATE_IRONGOLEM(3);

	private final int maxCount;

	private CountableKarmaEvent(int maxCount) {
		this.maxCount = maxCount;
	}

	public int getMaxCount() {
		return maxCount;
	}
}

public int getCurrency() {
	return danris;
}

public int setCurrency(int danris) {
	if(this.danris != danris) {
		this.danris = danris;
		setDirty();
	}
	if(this.danris > 999999) {
		this.danris = 999999;
		setDirty();
	}
	return this.danris;
}

/**
 * marks that this needs to be resend to the client
 */
public void setDirty() {
	dirty = true;
}
}

-Mew

I am Mew. The Legendary Psychic. I behave oddly and am always playing practical jokes.

 

I have also found that I really love making extremely long and extremely but sometimes not so descriptive variables. Sort of like what I just did there xD

Posted

Define "do not work".

Please post some code.

(Without that I think you might have a client/server-sync problem).

 

sorry...

I am Mew. The Legendary Psychic. I behave oddly and am always playing practical jokes.

 

I have also found that I really love making extremely long and extremely but sometimes not so descriptive variables. Sort of like what I just did there xD

Posted

Never mind.. I am such an idiot. I was tired after all. Although now it seems to reset the data on re-entry to the world...

I am Mew. The Legendary Psychic. I behave oddly and am always playing practical jokes.

 

I have also found that I really love making extremely long and extremely but sometimes not so descriptive variables. Sort of like what I just did there xD

Posted

Are your readToNBT / writeToNBT methods writing the stuff that needs to be saved?

 

Yes they are. Check out the github repo at https://github.com/ModderPenguin/MinePG, it has all my code for my mod...

I am Mew. The Legendary Psychic. I behave oddly and am always playing practical jokes.

 

I have also found that I really love making extremely long and extremely but sometimes not so descriptive variables. Sort of like what I just did there xD

Posted

I don't just, "Copy and Paste". I actually build my own (even if it does look similar) and read through accesible files to find out what everything does. but it still doesn't really seem to help me.

I am Mew. The Legendary Psychic. I behave oddly and am always playing practical jokes.

 

I have also found that I really love making extremely long and extremely but sometimes not so descriptive variables. Sort of like what I just did there xD

Posted

I doubt that.

Examples:

1) CountableKarmaEvent: You don't even use that, do you?

2) PlayerInformation.handlePlayerClone: Read the comment on that method. Do you even know what ASM is?

3) MobSpawnerTransformer: 1 to 1 copy.

4) MinePGTransformer: 1 to 1 copy

5) Packet System: 1 to 1 copy

6) Sound System: copied and slightly changed

7) HudOverlayhandler: 1 to 1 copy

[nobbc]8)[/nobbc] Generic/KarmaEventHandler: 1 to 1 copy

9) MinePGUtil: 1 to 1 copy

 

You are totally not copying our code.

 

Yeah, but that won't be released. I am going to change it around. That was to just get me started. And yes, I'm pretty sure I use the Karma event. I have karma in the mod. I'm sorry if seem like I am copying your code, I am just using that as a base to then change and build of to fit my purposes.

 

I am not 100% percent sure what it is, but I am fairly certain it is to do with core mod transformers. Which is another topic I would like to learn about...

I am Mew. The Legendary Psychic. I behave oddly and am always playing practical jokes.

 

I have also found that I really love making extremely long and extremely but sometimes not so descriptive variables. Sort of like what I just did there xD

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.