Jump to content

How to update a clients GuiOverlay rendering a player's IExtenedEntityProperties


Dreadfrost

Recommended Posts

I was following along with this mana bar tutorial.

 

http://www.minecraftforum.net/forums/mapping-and-modding/mapping-and-modding-tutorials/1571567-forge-1-6-4-1-8-eventhandler-and

 

I completed the server side configuration because I can empty and refill mana no problem. Next I created a custom GuiOverlay and now I need the client to update when using the items. The tutorial says this involves PacketHandeling but when I follow the link about PacketHandeling under updating to 1.7.2 the page http://www.minecraftforge.net/wiki/Netty_Packet_Handling says the info is out of date and to not use it. I imagine that's in reference to the 1.8 update.

 

Debugging I can clearly see the server detects that mana changes when using items and restoring mana via the console. How can I send the changes to the client so It can update the GuiOverlay?

 

Player property registration methods.

package com.example.tutorial.entity.player;

import net.minecraft.entity.player.EntityPlayer;
import net.minecraftforge.event.entity.EntityEvent.EntityConstructing;
import net.minecraftforge.fml.common.eventhandler.EventPriority;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;

public class PlayerPropertyRegister
{
@SubscribeEvent(priority = EventPriority.NORMAL)
public void onEntityConstructing(EntityConstructing event)
{
	if (event.entity instanceof EntityPlayer && EntityPlayerManaProperty.get((EntityPlayer) event.entity) == null)
	{
		EntityPlayerManaProperty.register((EntityPlayer) event.entity);
	}
}
}

 

Player Property class

package com.example.tutorial.entity.player;

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

public class EntityPlayerManaProperty implements IExtendedEntityProperties
{

public final static String EXT_PROP_NAME = "ExtendedPlayerTest";
private final EntityPlayer player;
private int currentMana, maxMana;


public EntityPlayerManaProperty(EntityPlayer player)
{
	this.player = player;
	this.currentMana = this.maxMana = 50;
}

public static final void register(EntityPlayer player)
{
	player.registerExtendedProperties(EntityPlayerManaProperty.EXT_PROP_NAME, new EntityPlayerManaProperty(player));
}

public static final EntityPlayerManaProperty get(EntityPlayer player)
{
	return (EntityPlayerManaProperty) player.getExtendedProperties(EXT_PROP_NAME);
}

// Save any custom data that needs saving here
@Override
public void saveNBTData(NBTTagCompound compound)
{
	// We need to create a new tag compound that will save everything for our Extended Properties
	NBTTagCompound properties = new NBTTagCompound();

	// We only have 2 variables currently; save them both to the new tag
	properties.setInteger("CurrentMana", this.currentMana);
	properties.setInteger("MaxMana", this.maxMana);

	compound.setTag(EXT_PROP_NAME, properties);
}

// Load whatever data you saved
@Override
public void loadNBTData(NBTTagCompound compound)
{
	// Here we fetch the unique tag compound we set for this class of Extended Properties
	NBTTagCompound properties = (NBTTagCompound) compound.getTag(EXT_PROP_NAME);

	// Get our data from the custom tag compound
	this.currentMana = properties.getInteger("CurrentMana");
	this.maxMana = properties.getInteger("MaxMana");

	System.out.println("[TUT PROPS] Mana from NBT: " + this.currentMana + "/" + this.maxMana);
}

@Override
public void init(Entity entity, World world) { }

/**
* Returns true if the amount of mana was consumed or false
* if the player's current mana was insufficient
*/
public boolean consumeMana(int amount)
{
	// Does the player have enough mana?
	boolean sufficient = amount <= this.currentMana;
	// Consume the amount anyway; if it's more than the player's current mana,
	// mana will be set to 0
	this.currentMana -= (amount < this.currentMana ? amount : this.currentMana);
	// Return if the player had enough mana
	return sufficient;
}

/**
* Simple method sets current mana to max mana
*/
public void replenishMana()
{
	this.currentMana = this.maxMana;
}

public int getCurrentMana()
{
	return this.currentMana;
}

public int getMaxMana()
{
	return this.maxMana;
}
}

 

and the mana bar GuiOverlay

 

package com.example.tutorial.client.gui;

import org.lwjgl.opengl.GL11;

import com.example.tutorial.entity.player.EntityPlayerManaProperty;

import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.Gui;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.client.event.RenderGameOverlayEvent;
import net.minecraftforge.client.event.RenderGameOverlayEvent.ElementType;
import net.minecraftforge.fml.common.eventhandler.EventPriority;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;

public class GuiManaBar extends Gui
{
private Minecraft mc;

private static final ResourceLocation texturepath = new ResourceLocation("tutorialmod", "textures/gui/mana_bar.png");

public GuiManaBar(Minecraft mc)
{
	super();
	this.mc = mc;
}

@SubscribeEvent(priority = EventPriority.NORMAL)
public void onRenderExperienceBar(RenderGameOverlayEvent event)
{
	if (event.isCancelable() || event.type != ElementType.EXPERIENCE)
	{
		return;
	}

	// Get our extended player properties and assign it locally so we can easily access it
	EntityPlayerManaProperty props = EntityPlayerManaProperty.get(this.mc.thePlayer);

	// If for some reason these properties don't exist (perhaps in multiplayer?)
	// or the player doesn't have mana, return.
	if (props == null || props.getMaxMana() == 0)
	{
		return;
	}

	// Starting position for the mana bar - 2 pixels from the top left corner.
	int xPos = (event.resolution.getScaledWidth() + 256) / 2;
	int yPos = 2;

	// The center of the screen can be gotten like this during this event:
	// int xPos = event.resolution.getScaledWidth() / 2;
	// int yPos = event.resolution.getScaledHeight() / 2;

	// Be sure to offset based on your texture size or your texture will not be truly centered:
	// int xPos = (event.resolution.getScaledWidth() + textureWidth) / 2;
	// int yPos = (event.resolution.getScaledHeight() + textureHeight) / 2;

	GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
	GL11.glDisable(GL11.GL_LIGHTING);
	this.mc.getTextureManager().bindTexture(texturepath);

	/*
	The parameters for drawTexturedModalRect are as follows:

	drawTexturedModalRect(int x, int y, int u, int v, int width, int height);

	x and y are the on-screen position at which to render.
	u and v are the coordinates of the most upper-left pixel in your texture file from which to start drawing.
	width and height are how many pixels to render from the start point (u, v)
	*/
	// First draw the background layer. In my texture file, it starts at the upper-
	// left corner (x=0, y=0), ends at 50 pixels (so it's 51 pixels long) and is 4 pixels thick (y value)
	this.drawTexturedModalRect(xPos, yPos, 0, 0, 51, 4);
	// Then draw the foreground; it's located just below the background in my
	// texture file, so it starts at x=0, y=4, is only 2 pixels thick and 49 length
	// Why y=4 and not y=5? Y starts at 0, so 0,1,2,3 = 4 pixels for the background

	// However, we want the length to be based on current mana, so we need a new variable:
	int manabarwidth = (int)((((float)props.getCurrentMana()) / props.getMaxMana()) * 49);
	//System.out.println("[GUI MANA] Current mana bar width: " + manabarwidth);
	// Now we can draw our mana bar at yPos+1 so it centers in the background:
	this.drawTexturedModalRect(xPos, yPos + 1, 0, 4, manabarwidth, 2);
}
}

 

Sry for all the comments I type tutorials out word for word.

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
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

    • im stuck in a problem whit BlockEntities i cannot solve so im trying to work around it but in the less complex way possible  i think is posible to store data to the minecraft world  in older versions people talk about  WordDataSave i dont find anything about it for 1.20.4  i find a tutorial for player capability in 1.19    here is an event      //########## ########## ########## ##########     @SubscribeEvent     public static void onAttachCapabilitiesPlayer(AttachCapabilitiesEvent<Entity> event) {         if(event.getObject() instanceof Player) {             if(!event.getObject().getCapability(PlayerThirstProvider.PLAYER_THIRST).isPresent()) {                 event.addCapability(new ResourceLocation(mercmod.MOD_ID, "properties"), new PlayerThirstProvider());             }         }     }  soo i made this      //########## ########## ########## ##########     @SubscribeEvent     public static void onAttachCapabilitiesLevel(AttachCapabilitiesEvent<Level> event) {     } i think i can do some witchcraft  here  but is many things don't understand  to start whit i dont get yet how to recall the saved data from a BlockEntity  the handler still in progress the plan is to make a Map to store nbt data using the block coordinates as index later store that map as a big nbt      ########## any way the question is if theres alredy a tutorial for 1.20.4 for this ?? or some alredy working code from someone else doing something similar ?           
    • I tried this, and found out that Oculus is the mod that is crashing the game. I have removed it, and it now works fine! Thanks!
    • Hi @DanielderErbauer, I arrived here as I have the "as it does not exist in Block{minecraft:air}" error in a modpack I am trying to start. It's a long-shot, I know, but as the post that led this to being solved is missing, can I ask if you may recall what it said? Thanks in advance
    • Hm yeah sorry, not seeing anything else that's sticking out, I'd need to debug myself probably. Do you have a github or bitbucket repo? I could poke around when I have time to see what the problem might be.
    • looking around for the capabilities thing i found a piece of code and find this piece of code  //########## ########## ########## ########## @SubscribeEvent public static void addCustomTrades(VillagerTradesEvent event) { if(event.getType() == VillagerProfession.FLETCHER) { System.out.println("\n\n### VillagerProfession.FLETCHER \n\n");//<----------- Int2ObjectMap<List<VillagerTrades.ItemListing>> trades = event.getTrades(); ItemStack stack = new ItemStack(ItemInit.ARROW_STEEL.get(), 16); int villagerLevel = 1; trades.get(villagerLevel).add((trader, rand) -> new MerchantOffer( new ItemStack(Items.EMERALD, 2), stack,1,3,0.02F)); } if(event.getType() == VillagerProfession.TOOLSMITH) { System.out.println("\n\n### VillagerProfession.TOOLSMITH \n\n");//<----------- Int2ObjectMap<List<VillagerTrades.ItemListing>> trades = event.getTrades(); ItemStack stack = new ItemStack(ItemInit.INGOT_STEEL.get(), 1); int villagerLevel = 1; trades.get(villagerLevel).add((trader, rand) -> new MerchantOffer( new ItemStack(Items.EMERALD, 2), stack,10,8,0.02F)); } } //########## ########## ########## ########## the event is aknoledge and execute when the maps load  ### VillagerProfession.FLETCHER  ### VillagerProfession.TOOLSMITH  //########## But is not working      is a random piece of code is not working  could it be than this code is old 1.18 and no valid anymore for 1.20.4 or the thing are this numbers  stack,10,8,0.02F));  i dont know what this numbers means    ########## the question is ¿how to add things to the villager traders    thanks for your attention               
  • Topics

×
×
  • Create New...

Important Information

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