Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

[1.8][SOLVED] Attribute Modifier Help


NovaViper
 Share

Recommended Posts

I DID read them all.. I'm still confused on how to actually do it without screwing with the vanilla coding

What? How could you possibly screw with the vanilla coding? It's super simple:

 

1. EntityTameable already handles the owner for you, so you don't need to do anything there.

2. Player's username is a STRING

3. NBT natively supports reading and writing Strings: nbt.writeString("nickname", player's username);

4. Want it on the client for rendering? DataWatcher ALSO supports String by default, just add it and be done.

 

Sorry, but this topic has gone on way too long for something so blatantly simple.

 

It looks like there is a lot of other stuff going on in your Entity, so perhaps you'd be better off by creating a SIMPLE entity that does nothing but handle the nickname stuff. Then you can get just that working without worrying about anything else, figure out exactly what you need, and go from there.

Link to comment
Share on other sites

  • Replies 105
  • Created
  • Last Reply

Top Posters In This Topic

*Facepalm* I can't believe I didn't think it all the way through.. but I have actually gotten close to the solution, I did exactly what you guys said and got it save (I think) the id and name of the owner, then leave and come back as a new player, open the gui (with no crashes, yay) but now the gui says "()" instead of the previous owner name.

 

IDs

	public static final int ownerName = 13;
public static final int ownerID = 14;

 

EntityInt

		this.dataWatcher.addObject(DataValues.ownerName, new String("")); //Owner Name
	this.dataWatcher.addObject(DataValues.ownerID, new String("")); //Owner Id

 

writeEntityToNBT

		tagCompound.setString("ownerId", this.getOwnerID());
	tagCompound.setString("ownerName", this.getOwnerName());

 

readEntityFromNBT

		if (tagCompound.hasKey("ownerName", ) {
		this.saveOwnerName(tagCompound.getString("ownerName"));
	}

	if (tagCompound.hasKey("ownerId", ) {
		this.saveOwnerID(tagCompound.getString("ownerId"));
	}

 

Methods

	public String getOwnerName() {
	return this.dataWatcher.getWatchableObjectString(DataValues.ownerName);
}

public void saveOwnerName(String name) {
	this.dataWatcher.updateObject(DataValues.ownerName, name);
}

public String getOwnerID() {
	return this.dataWatcher.getWatchableObjectString(DataValues.ownerID);
}

public void saveOwnerID(String id) {
	this.dataWatcher.updateObject(DataValues.ownerID, id);
}

Main Developer and Owner of Zero Quest

Visit the Wiki for more information

If I helped anyone, please give me a applaud and a thank you!

Link to comment
Share on other sites

Here ya go

 

package common.zeroquest.client.gui;

import java.io.IOException;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

import net.minecraft.client.gui.FontRenderer;
import net.minecraft.client.gui.GuiButton;
import net.minecraft.client.gui.GuiScreen;
import net.minecraft.client.gui.GuiTextField;
import net.minecraft.client.gui.ScaledResolution;
import net.minecraft.client.renderer.RenderHelper;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.util.EnumChatFormatting;
import net.minecraft.util.StatCollector;

import org.apache.commons.lang3.StringUtils;
import org.lwjgl.input.Keyboard;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL12;

import common.zeroquest.api.interfaces.ITalent;
import common.zeroquest.api.registry.TalentRegistry;
import common.zeroquest.entity.util.ModeUtil.EnumMode;
import common.zeroquest.entity.zertum.EntityZertumEntity;
import common.zeroquest.lib.Constants;
import common.zeroquest.network.PacketHandler;
import common.zeroquest.network.imessage.ZertumMode;
import common.zeroquest.network.imessage.ZertumName;
import common.zeroquest.network.imessage.ZertumObey;
import common.zeroquest.network.imessage.ZertumTalents;

/**
* @author ProPercivalalb
*/
public class GuiDogInfo extends GuiScreen {

public EntityZertumEntity dog;
public EntityPlayer player;
private ScaledResolution resolution;
private final List<GuiTextField> textfieldList = new ArrayList<GuiTextField>();
private GuiTextField nameTextField;
private int currentPage = 0;
private int maxPages = 1;
public int btnPerPages = 0;
private final DecimalFormat dfShort = new DecimalFormat("0.00");

public GuiDogInfo(EntityZertumEntity dog, EntityPlayer player) {
	this.dog = dog;
	this.player = player;
}

@Override
public void initGui() {
	super.initGui();
	this.buttonList.clear();
	this.labelList.clear();
	this.textfieldList.clear();
	this.resolution = new ScaledResolution(this.mc, this.mc.displayWidth, this.mc.displayHeight);
	Keyboard.enableRepeatEvents(true);
	int topX = this.width / 2;
	int topY = this.height / 2;
	GuiTextField nameTextField = new GuiTextField(0, this.fontRendererObj, topX - 100, topY + 50, 200, 20) {
		@Override
		public boolean textboxKeyTyped(char character, int keyId) {
			boolean typed = super.textboxKeyTyped(character, keyId);
			if (typed) {
				PacketHandler.sendToServer(new ZertumName(dog.getEntityId(), this.getText()));
			}
			return typed;
		}
	};
	nameTextField.setFocused(false);
	nameTextField.setMaxStringLength(32);
	nameTextField.setText(this.dog.getPetName());
	this.nameTextField = nameTextField;

	this.textfieldList.add(nameTextField);

	int size = TalentRegistry.getTalents().size();

	int temp = 0;
	while ((temp + 2) * 21 + 10 < this.resolution.getScaledHeight()) {
		temp += 1;
	}

	this.btnPerPages = temp;

	if (temp < size) {
		this.buttonList.add(new GuiButton(-1, 25, temp * 21 + 10, 20, 20, "<"));
		this.buttonList.add(new GuiButton(-2, 48, temp * 21 + 10, 20, 20, ">"));
	}

	if (this.btnPerPages < 1) {
		this.btnPerPages = 1;
	}

	this.maxPages = (int) Math.ceil((double) TalentRegistry.getTalents().size() / (double) this.btnPerPages);

	if (this.currentPage >= this.maxPages) {
		this.currentPage = 0;
	}

	for (int i = 0; i < this.btnPerPages; ++i) {
		if ((this.currentPage * this.btnPerPages + i) >= TalentRegistry.getTalents().size()) {
			continue;
		}
		this.buttonList.add(new GuiButton(1 + this.currentPage * this.btnPerPages + i, 25, 10 + i * 21, 20, 20, "+"));
	}
	if (this.dog.isOwner(this.player)) {
		this.buttonList.add(new GuiButton(-5, this.width - 64, topY + 65, 42, 20, String.valueOf(this.dog.willObeyOthers())));
	}

	this.buttonList.add(new GuiButton(-6, topX + 40, topY + 25, 60, 20, this.dog.mode.getMode().modeName()));
}

@Override
public void drawScreen(int xMouse, int yMouse, float partialTickTime) {
	this.drawDefaultBackground();
	// Background
	int topX = this.width / 2;
	int topY = this.height / 2;
	String health = dfShort.format(this.dog.getHealth());
	String healthMax = dfShort.format(this.dog.getMaxHealth());
	String healthRel = dfShort.format(this.dog.getHealthRelative() * 100);
	String healthState = health + "/" + healthMax + "(" + healthRel + "%)";
	String damageValue = dfShort.format(this.dog.getAIAttackDamage());
	String damageState = damageValue;
	String speedValue = dfShort.format(this.dog.getAIMoveSpeed());
	String speedState = speedValue;

	String tamedString = null;
	if (this.dog.isTamed()) {
		if (this.dog.getOwnerName() == this.player.getName()) {
			tamedString = "Yes (You)";
		}
		else {
			tamedString = "Yes (" + StringUtils.abbreviate(this.dog.getOwnerName(), 22) + ")";
		}
	}

	String evoString = null;
	if (!this.dog.hasEvolved() && !this.dog.isChild() && this.dog.levels.getLevel() < Constants.maxLevel) {
		evoString = "Not at Alpha Level!";
	}
	else if (!this.dog.hasEvolved() && this.dog.isChild() && this.dog.levels.getLevel() < Constants.maxLevel) {
		evoString = "Too Young!";
	}
	else if (!this.dog.hasEvolved() && !this.dog.isChild() && this.dog.levels.getLevel() >= Constants.maxLevel) {
		evoString = "Ready!";
	}
	else if (this.dog.hasEvolved() && !this.dog.isChild()) {
		evoString = "Already Evolved!";
	}

	this.fontRendererObj.drawString("New name:", topX - 100, topY + 38, 4210752);
	this.fontRendererObj.drawString("Level: " + this.dog.levels.getLevel(), topX - 75, topY + 75, 0xFF10F9);
	this.fontRendererObj.drawString("Points Left: " + this.dog.spendablePoints(), topX, topY + 75, 0xFFFFFF);
	this.fontRendererObj.drawString("Health: " + healthState, topX + 190, topY - 170, 0xFFFFFF);
	this.fontRendererObj.drawString("Damage: " + damageState, topX + 190, topY - 160, 0xFFFFFF);
	this.fontRendererObj.drawString("Speed: " + speedState, topX + 190, topY - 150, 0xFFFFFF);
	this.fontRendererObj.drawString("Tamed: " + tamedString, topX + 190, topY - 140, 0xFFFFFF);
	this.fontRendererObj.drawString("State: " + evoString, topX + 190, topY - 130, 0xFFFFFF);
	if (this.dog.isOwner(this.player)) {
		this.fontRendererObj.drawString("Obey Others?", this.width - 76, topY + 55, 0xFFFFFF);
	}

	for (int i = 0; i < this.btnPerPages; ++i) {
		if ((this.currentPage * this.btnPerPages + i) >= TalentRegistry.getTalents().size()) {
			continue;
		}
		this.fontRendererObj.drawString(TalentRegistry.getTalent(this.currentPage * this.btnPerPages + i).getLocalisedName(), 50, 17 + i * 21, 0xFFFFFF);
	}

	for (GuiTextField field : this.textfieldList) {
		field.drawTextBox();
	}
	GL11.glDisable(GL12.GL_RESCALE_NORMAL);
	RenderHelper.disableStandardItemLighting();
	GL11.glDisable(GL11.GL_LIGHTING);
	GL11.glDisable(GL11.GL_DEPTH_TEST);
	super.drawScreen(xMouse, yMouse, partialTickTime);
	RenderHelper.enableGUIStandardItemLighting();

	// Foreground

	GL11.glPushMatrix();
	GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
	for (int k = 0; k < this.buttonList.size(); ++k) {
		GuiButton button = (GuiButton) this.buttonList.get(k);
		if (button.mousePressed(this.mc, xMouse, yMouse)) {
			List list = new ArrayList();
			if (button.id >= 1 && button.id <= TalentRegistry.getTalents().size()) {
				ITalent talent = TalentRegistry.getTalent(button.id - 1);

				list.add(EnumChatFormatting.GREEN + talent.getLocalisedName());
				list.add("Level: " + this.dog.talents.getLevel(talent));
				list.add(EnumChatFormatting.GRAY + "--------------------------------");
				list.addAll(this.splitInto(talent.getLocalisedInfo(), 200, this.mc.fontRendererObj));
			}
			else if (button.id == -1) {
				list.add(EnumChatFormatting.ITALIC + "Previous Page");
			}
			else if (button.id == -2) {
				list.add(EnumChatFormatting.ITALIC + "Next Page");
			}
			else if (button.id == -6) {
				String str = StatCollector.translateToLocal("modeinfo." + button.displayString.toLowerCase());
				list.addAll(splitInto(str, 150, this.mc.fontRendererObj));
			}

			this.drawHoveringText(list, xMouse, yMouse, this.mc.fontRendererObj);
		}
	}
	GL11.glPopMatrix();
}

@Override
protected void actionPerformed(GuiButton button) {

	if (button.id >= 1 && button.id <= TalentRegistry.getTalents().size()) {
		ITalent talent = TalentRegistry.getTalent(button.id - 1);
		int level = this.dog.talents.getLevel(talent);

		if (level < talent.getHighestLevel(this.dog) && this.dog.spendablePoints() >= talent.getCost(this.dog, level + 1)) {
			PacketHandler.sendToServer(new ZertumTalents(this.dog.getEntityId(), TalentRegistry.getTalent(button.id - 1).getKey()));
		}

	}
	else if (button.id == -1) {
		if (this.currentPage > 0) {
			this.currentPage -= 1;
			this.initGui();
		}
	}
	else if (button.id == -2) {
		if (this.currentPage + 1 < this.maxPages) {
			this.currentPage += 1;
			this.initGui();
		}
	}
	if (button.id == -5) {
		if (!this.dog.willObeyOthers()) {
			button.displayString = "true";
			PacketHandler.sendToServer(new ZertumObey(this.dog.getEntityId(), true));

		}
		else {
			button.displayString = "false";
			PacketHandler.sendToServer(new ZertumObey(this.dog.getEntityId(), false));
		}
	}

	if (button.id == -6) {
		int newMode = (dog.mode.getMode().ordinal() + 1) % EnumMode.values().length;
		EnumMode mode = EnumMode.values()[newMode];
		button.displayString = mode.modeName();
		PacketHandler.sendToServer(new ZertumMode(this.dog.getEntityId(), newMode));
	}
}

@Override
public void updateScreen() {
	for (GuiTextField field : this.textfieldList) {
		field.updateCursorCounter();
	}
}

@Override
public void mouseClicked(int xMouse, int yMouse, int mouseButton) throws IOException {
	super.mouseClicked(xMouse, yMouse, mouseButton);
	for (GuiTextField field : this.textfieldList) {
		field.mouseClicked(xMouse, yMouse, mouseButton);
	}
}

@Override
public void keyTyped(char character, int keyId) {
	for (GuiTextField field : this.textfieldList) {
		field.textboxKeyTyped(character, keyId);
	}

	if (keyId == Keyboard.KEY_ESCAPE) {
		this.mc.thePlayer.closeScreen();
	}
}

@Override
public void onGuiClosed() {
	Keyboard.enableRepeatEvents(false);
}

@Override
public boolean doesGuiPauseGame() {
	return false;
}

public List splitInto(String text, int maxLength, FontRenderer font) {
	List list = new ArrayList();

	String temp = "";
	String[] split = text.split(" ");

	for (int i = 0; i < split.length; ++i) {
		String str = split[i];
		int length = font.getStringWidth(temp + str);

		if (length > maxLength) {
			list.add(temp);
			temp = "";
		}

		temp += str + " ";

		if (i == split.length - 1) {
			list.add(temp);
		}
	}

	return list;
}
}

Main Developer and Owner of Zero Quest

Visit the Wiki for more information

If I helped anyone, please give me a applaud and a thank you!

Link to comment
Share on other sites

Hm. Well, first step is to debug by printing out the values of the owner name and ID when loaded from NBT.

 

Also, you should be aware that using '==' operator to compare Strings is not generally a good idea, as it compares identity, not content. You should use 'equals' method instead:

if (this.dog.getOwnerName().equals(this.player.getName())) {
// player viewing GUI has the same name as the owner of the dog
}

Furthermore, shouldn't you be using player.getDisplayNameString() instead of getName() ? That's the method that returns the nickname, if I'm not mistaken. Though it looks like getName() returns the GameProfile name... Dunno.

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