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.