Jump to content

[SOLVED] GUI not updating values


Melonslise

Recommended Posts

I created a class extending IExtendedEntityProperties:

 

 

package melonslise.runicinscription;

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 ManaProperties implements IExtendedEntityProperties
{
public final static String EXT_PROP_MANA = "HandlerManaSystem";

private final EntityPlayer player;
private int currentMana;
private int maxMana;



public ManaProperties(EntityPlayer player)
{
	this.player = player;
	this.currentMana = 30;
	this.maxMana = 30;
}

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

public static final ManaProperties get(EntityPlayer player)
{
	return (ManaProperties) player.getExtendedProperties(EXT_PROP_MANA);
}

@Override
public void saveNBTData(NBTTagCompound compound)
{
	NBTTagCompound properties = new NBTTagCompound();

	properties.setInteger("CurrentMana", this.currentMana);
	properties.setInteger("MaxMana", this.maxMana);

	compound.setTag(EXT_PROP_MANA, properties);

}

@Override
public void loadNBTData(NBTTagCompound compound)
{
	NBTTagCompound properties = (NBTTagCompound) compound.getTag(EXT_PROP_MANA);

	this.currentMana = properties.getInteger("CurrentMana");
	this.maxMana = properties.getInteger("MaxMana");

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

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

}

public boolean consumeMana(int amount)
{
	if(this.currentMana >= amount)
	{
		currentMana = currentMana - amount;
		return true;
	}

	return false;
}

public void replenishMana()
{
	this.currentMana = this.maxMana;
}

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

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

 

 

 

And here's it's handler:

 

 

package melonslise.runicinscription.Handler;

import cpw.mods.fml.common.eventhandler.SubscribeEvent;
import melonslise.runicinscription.ManaProperties;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraftforge.event.entity.EntityEvent.EntityConstructing;
import net.minecraftforge.event.entity.player.PlayerEvent;

public class HandlerManaEvents
{
@SubscribeEvent
public void onEntityConstructing(EntityConstructing event)
{
	if (event.entity instanceof EntityPlayer && ManaProperties.get((EntityPlayer) event.entity) == null)
		ManaProperties.register((EntityPlayer) event.entity);

	if (event.entity instanceof EntityPlayer && event.entity.getExtendedProperties(ManaProperties.EXT_PROP_MANA) == null)
		event.entity.registerExtendedProperties(ManaProperties.EXT_PROP_MANA, new ManaProperties((EntityPlayer) event.entity));
}

}

 

 

 

And here's the GUI

 

 

package melonslise.runicinscription.GUI;

import cpw.mods.fml.common.eventhandler.EventPriority;
import cpw.mods.fml.common.eventhandler.SubscribeEvent;
import melonslise.runicinscription.ManaProperties;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.Gui;
import net.minecraftforge.client.event.RenderGameOverlayEvent;
import net.minecraftforge.client.event.RenderGameOverlayEvent.ElementType;

public class GUIManaBar extends Gui
{
public GUIManaBar(Minecraft mc)
{	
	int xPos = 2;
	int yPos = 2;

	ManaProperties props = ManaProperties.get(mc.thePlayer);
	mc.fontRenderer.drawStringWithShadow("§1" + props.getCurrentMana() + "/" + props.getMaxMana(), xPos, yPos, );
}
}

 

 

 

And it's hander:

 

 

package melonslise.runicinscription.Handler;

import cpw.mods.fml.client.GuiNotification;
import cpw.mods.fml.common.eventhandler.SubscribeEvent;
import melonslise.runicinscription.GUI.GUIManaBar;
import net.minecraft.client.Minecraft;
import net.minecraftforge.client.event.RenderGameOverlayEvent;
import net.minecraftforge.client.event.RenderGameOverlayEvent.ElementType;

public class HandlerGUI
{
   @SubscribeEvent
   public void onRenderGUI(RenderGameOverlayEvent event)
   {
	   if (event.type != ElementType.EXPERIENCE) return;
	   new melonslise.runicinscription.GUI.GUIManaBar(Minecraft.getMinecraft());
   }
}

 

 

 

And both are registered in the mod class

 

 

@EventHandler
public void init(FMLInitializationEvent event)
{
	MinecraftForge.EVENT_BUS.register(new HandlerManaEvents());
                MinecraftForge.EVENT_BUS.register(new HandlerGUI());
}

 

 

 

Here's an item's onItemRightClick code:

 

 

			if (props.consumeMana(15))
			{
				System.out.println("Player had enough mana. Do something awesome!");
			}
			else
			{
				System.out.println("Player ran out of mana. Sad face.");
				props.replenishMana();
			}

 

 

 

And when right clicked the gui values don't change

Link to comment
Share on other sites

  • Replies 68
  • Created
  • Last Reply

Top Posters In This Topic

Top Posters In This Topic

  • Why are you still using IExtendedEntityProperties? Update and use Capabilities, IEEP is gone in newer forge versions.
  • Why do you have basically the same code twice in the entity constructing event? Why do you even check if your properties are already there? If they would be, something would be seriously wrong and you should not just ignore and carry on.
  • Your IEEP identifier should include your ModID.
  • You should choose one of
    RenderGameOverlayEvent.Pre

    or

    RenderGameOverlayEvent.Post

    , don't use both.

  • Why are you drawing in the constructor of
    GUIManaBar

    ? I have seen this style before, what tutorial did you get it from? The maker needs to be properly yelled at...

  • GUIs are entirely client-side, but modifying your IEEP happens on the server. This does not magically sync, you need to send packets to keep your IEEP in sync whenever it changes on the server.

 

  • I am making a mod for 1.7.10. (Just incase you ask, I will be updating it to 1.8 and don't ask why I am using 1.7.10)
  • I don't know why. I was following coolAlias' tutorial on this thing. What should I have in the handler then?
  • Where do I put my mod id then? (As I said I was following coolAlias' tutorial on IEEP)
  • Ok I'll change  that
  • How should I draw it then? I followed this tutorial : https://emxtutorials.wordpress.com/simple-in-game-gui-overlay/
  • Where can I find a good and detailed tutorial on packets in 1.7.10?

Link to comment
Share on other sites

I am making a mod for 1.7.10. (Just incase you ask, I will be updating it to 1.8 and don't ask why I am using 1.7.10)
No, I won't ask. But I will say: stop.

I don't know why. I was following coolAlias' tutorial on this thing. What should I have in the handler then?
Never have code in your mod (or any other programming project) that you don't understand. You should simply register your IEEP. Nothing more.

Where do I put my mod id then? (As I said I was following coolAlias' tutorial on IEEP)
You put it in the identifier. Like I said.

How should I draw it then? I followed this tutorial : https://emxtutorials.wordpress.com/simple-in-game-gui-overlay/

Not in the constructor.

Where can I find a good and detailed tutorial on packets in 1.7.10?
I don't know, I don't keep track of 1.7.10 tutorials.

 

Alright, So I created two messages and two handlers for them:

 

Message about current mana:

 

 

package melonslise.runicinscription.Network;

import cpw.mods.fml.common.network.simpleimpl.IMessage;
import io.netty.buffer.ByteBuf;

public class CurrentManaMessage implements IMessage
{
private int currentMana;

public CurrentManaMessage()
{

}

public CurrentManaMessage(int currentMana)
{
	this.currentMana = currentMana;
}

@Override
public void toBytes(ByteBuf buf)
{
	buf.writeInt(currentMana);
}

@Override
public void fromBytes(ByteBuf buf)
{
	currentMana = buf.readInt();
}

public int getCurrentMana()
{
	return currentMana;
}
}

 

 

 

and it's handler

 

 

package melonslise.runicinscription.Handler;

import cpw.mods.fml.common.network.simpleimpl.IMessage;
import cpw.mods.fml.common.network.simpleimpl.IMessageHandler;
import cpw.mods.fml.common.network.simpleimpl.MessageContext;
import melonslise.runicinscription.Network.CurrentManaMessage;
import net.minecraft.entity.player.EntityPlayerMP;

public class HandlerCurrentManaMessage implements IMessageHandler<CurrentManaMessage, IMessage>
{
@Override
public IMessage onMessage(CurrentManaMessage message, MessageContext ctx) 
{
	EntityPlayerMP serverPlayer = ctx.getServerHandler().playerEntity;

	int currentMana = message.getCurrentMana();

	return null;
}

}

 

 

 

Message about max mana:

 

 

package melonslise.runicinscription.Network;

import cpw.mods.fml.common.network.simpleimpl.IMessage;
import io.netty.buffer.ByteBuf;

public class MaxManaMessage implements IMessage
{
private int maxMana;

public MaxManaMessage()
{
	  
}
  
public MaxManaMessage(int maxMana)
{
	this.maxMana = maxMana;
}

@Override
public void toBytes(ByteBuf buf)
{
	buf.writeInt(maxMana);
}

@Override
public void fromBytes(ByteBuf buf)
{
	maxMana = buf.readInt();
}

public int getMaxMana()
{
	return maxMana;
}
}

 

 

 

and it's handler:

 

 

package melonslise.runicinscription.Handler;

import cpw.mods.fml.common.network.simpleimpl.IMessage;
import cpw.mods.fml.common.network.simpleimpl.IMessageHandler;
import cpw.mods.fml.common.network.simpleimpl.MessageContext;
import melonslise.runicinscription.Network.MaxManaMessage;
import net.minecraft.entity.player.EntityPlayerMP;

public class HandlerMaxManaMessage implements IMessageHandler<MaxManaMessage, IMessage>
{
@Override
public IMessage onMessage(MaxManaMessage message, MessageContext ctx)
{
	EntityPlayerMP serverPlayer = ctx.getServerHandler().playerEntity;

	int amount = message.getMaxMana();

	return null;
}
}

 

 

 

So how do I integrate these into my GUI and IEEP?

Link to comment
Share on other sites

If hander is supposed to receive on client, why are your using server side?

 

EntityPlayer player = ctx.getClientHandler().playerEntity;

or

Minecraft#thePlayer

 

When registering packet you need to use Side.CLIENT (its the side of handler).

 

Then when you have client-side player, you simlpy get his IEEP and update it.

1.7.10 is no longer supported by forge, you are on your own.

Link to comment
Share on other sites

If hander is supposed to receive on client, why are your using server side?

 

EntityPlayer player = ctx.getClientHandler().playerEntity;

or

Minecraft#thePlayer

 

When registering packet you need to use Side.CLIENT (its the side of handler).

 

Then when you have client-side player, you simlpy get his IEEP and update it.

 

I though the receiving end was registered here:

network.registerMessage(HandlerCurrentManaMessage.class, CurrentManaMessage.class, 1, Side.CLIENT);

 

But I did change to this:

EntityPlayer player = ctx.getClientHandler().playerEntity;

 

and it's saying that playerEntity cannot be resolved

Link to comment
Share on other sites

I though the receiving end was registered here:

network.registerMessage(HandlerCurrentManaMessage.class, CurrentManaMessage.class, 1, Side.CLIENT);

 

This is correct, I just pointed that out since i didn't see you register it. (missed it or you didn't provide code).

 

Anyway - handler is the receiver so it is logical that if receiver is client you can (should) use client-only stuff, in this case - use client player.

 

EDIT

 

In case you would want to update client's data about entity that is NOT Minecraft#thePlayer, you will need to send Entity#entityId (via packet) and use Minecraft#theWorld#getEntityById(entityId) to retrieve it.

1.7.10 is no longer supported by forge, you are on your own.

Link to comment
Share on other sites

I though the receiving end was registered here:

network.registerMessage(HandlerCurrentManaMessage.class, CurrentManaMessage.class, 1, Side.CLIENT);

 

This is correct, I just pointed that out since i didn't see you register it. (missed it or you didn't provide code).

 

Anyway - handler is the receiver so it is logical that if receiver is client you can (should) use client-only stuff, in this case - use client player.

 

EDIT

 

In case you would want to update client's data about entity that is NOT Minecraft#thePlayer, you will need to send Entity#entityId (via packet) and use Minecraft#theWorld#getEntityById(entityId) to retrieve it.

 

 

But I did change to this:

EntityPlayer player = ctx.getClientHandler().playerEntity;

 

and it's saying that playerEntity cannot be resolved

Link to comment
Share on other sites

Because there is no such thing (that was my wild guess / pseudo code). Use Minecraft#thePlayer.

 

Note - As Minecraft is @SideOnly class, referencing it from handler should crash on server (dedic).

There are few ways to resolve it, one of them is to make a Proxy getter for Minecraft#thePlayer that returns Minecraft#thePlayer on client and null on server.

1.7.10 is no longer supported by forge, you are on your own.

Link to comment
Share on other sites

Because there is no such thing (that was my wild guess / pseudo code). Use Minecraft#thePlayer.

 

Note - As Minecraft is @SideOnly class, referencing it from handler should crash on server (dedic).

There are few ways to resolve it, one of them is to make a Proxy getter for Minecraft#thePlayer that returns Minecraft#thePlayer on client and null on server.

So how exactly would I make this proxy? As in a new class? How would the getter look like? A small snippet of code would help me understand better.

Link to comment
Share on other sites

CommonProxy#getClientPlayer() - abstract/interface OR return null;

ClientProxy#getClientPlayer() return Minecraft#thePlayer;

ServerProxy#getClientPlayer() return null; (if you alredy return null in Common, ServerProxy is not even needed (Common will be server))

 

Then you have @SidedProxy CommonProxy in your main mod class, ay?

Now when you are on client.jar you will have Minecraft#thePlayer returned, and on server - null (where on server should never even be reached).

 

http://mcforge.readthedocs.io/en/latest/concepts/sides/

 

1.7.10 is no longer supported by forge, you are on your own.

Link to comment
Share on other sites

CommonProxy#getClientPlayer() - abstract/interface OR return null;

ClientProxy#getClientPlayer() return Minecraft#thePlayer;

ServerProxy#getClientPlayer() return null; (if you alredy return null in Common, ServerProxy is not even needed (Common will be server))

 

Then you have @SidedProxy CommonProxy in your main mod class, ay?

Now when you are on client.jar you will have Minecraft#thePlayer returned, and on server - null (where on server should never even be reached).

 

http://mcforge.readthedocs.io/en/latest/concepts/sides/

 

I started off creating getClientPlayer() in the client proxy, but now it's telling me that it cannot create a static reference to a non-static field /:

Link to comment
Share on other sites

Do you know Java? You got everything you need to make it work, only thing that can stop you is lack of java knowledge which won't really be found here - google said error or just read what static means.

 

Hint:

#getClientPlayer should be a member method (not static).

1.7.10 is no longer supported by forge, you are on your own.

Link to comment
Share on other sites

Do you know Java? You got everything you need to make it work, only thing that can stop you is lack of java knowledge which won't really be found here - google said error or just read what static means.

 

Hint:

#getClientPlayer should be a member method (not static).

Alright I found the solution, but I did google around a little and found this:

EntityPlayer thePlayer = (ctx.side.isClient() ? Minecraft.getMinecraft().thePlayer : ctx.getServerHandler().playerEntity);

 

wouldn't this be better than calling methods from the proxies?

Link to comment
Share on other sites

No it wouldn't be better (tho, it might work, still - it is bad way).

 

Anything marked by @SideOnly is NOT PRESENT on other side. Minecraft is @SideOnly(Side.CLIENT) which means it is not loaded by VM when you fire mod on dedicated server. Because of that if you try to reference Minecraft class from common code - it will crash (in this case - on dedicated server).

 

Whenever you need sided access - you need proxy.

 

You will need proxies at some point, just make them now. God, look into some open sources.

1.7.10 is no longer supported by forge, you are on your own.

Link to comment
Share on other sites

No it wouldn't be better (tho, it might work, still - it is bad way).

 

Anything marked by @SideOnly is NOT PRESENT on other side. Minecraft is @SideOnly(Side.CLIENT) which means it is not loaded by VM when you fire mod on dedicated server. Because of that if you try to reference Minecraft class from common code - it will crash (in this case - on dedicated server).

 

Whenever you need sided access - you need proxy.

 

You will need proxies at some point, just make them now. God, look into some open sources.

There's still one thing that I don't understand. What do I return in my common proxy?

public EntityClientPlayerMP getClientPlayer()
{
	return ;
}

Link to comment
Share on other sites

3mLydMU.png

 

abstract CommonProxy
{
    public abstract getClientPlayer();
}

ClientProxy extends CommonProxy
{
    public getClientPlayer()
    {
        return Minecraft#thePlayer;
    }
}

ServerProxy extends CommonProxy
{
    public getClientPlayer()
    {
        return null;
    }
}

 

Main
{
    @SidedProxy(clientSide="packages.ClientProxy", serverSide="packages.ServerProxy")
    public static CommonProxy proxy;
}

 

PacketHandler
{
    onMessage(...)
    {
        Main.proxy#getClientPlayer();
    }
}

 

PLEASE, use google sometimes. :)

1.7.10 is no longer supported by forge, you are on your own.

Link to comment
Share on other sites

abstract CommonProxy
{
    public abstract getClientPlayer();
}

ClientProxy extends CommonProxy
{
    public abstract getClientPlayer()
    {
        return Minecraft#thePlayer;
    }
}

ServerProxy extends CommonProxy
{
    public abstract getClientPlayer()
    {
        return null;
    }
}

 

Main
{
    @SidedProxy(clientSide="packages.ClientProxy", serverSide="packages.ServerProxy")
    public static CommonProxy proxy;
}

 

PacketHandler
{
    onMessage(...)
    {
        Main.proxy#getClientPlayer();
    }
}

 

PLEASE, use google sometimes. :)

Ahahah. Dat meme.

 

In the common proxy it's telling me to change getClientPlayer to void which I cannot do. I also can't change client proxy's getClientPlayer to abstract (I don't think). And I had registered the proxies in the main mod class a long time ago.

 

EDIT: Where did the meme go? ;(

Link to comment
Share on other sites

be7.jpg

 

DUDE, that was pseudocode!!! #getClientPlayer() will obviously have EntityPlayer return.

 

abstract CommonProxy
{
    public abstract EntityPlayer getClientPlayer();
}

1.7.10 is no longer supported by forge, you are on your own.

Link to comment
Share on other sites

You need to send the packet whenever the value changes.

SO would this be the correct way of sending the packet? (this is located in my IEEP)

public final void syncCurrentMana()
{
	RunicInscription.network.sendTo(new CurrentManaMessage(this.currentMana), (EntityPlayerMP) player);
}

 

The method is being called in the IEEP's handler

@SubscribeEvent
public void onUpdatePlayer(LivingUpdateEvent event)
{
	ManaProperties.get((EntityPlayer) event.entity).syncCurrentMana();
}

Link to comment
Share on other sites

And if the mana is full...?

Sorry for my retardedness, but here all my code so far and the GUI is still not being updated:

 

Here's my IEEP:

 

package melonslise.runicinscription.Network;

import melonslise.runicinscription.RunicInscription;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.world.World;
import net.minecraftforge.common.IExtendedEntityProperties;
import net.minecraftforge.common.util.Constants;

public class ManaProperties implements IExtendedEntityProperties
{
    public final static String EXT_PROP_MANA = RunicInscription.MODID + "_Mana";
    
    private final EntityPlayer player;
    private int currentMana;
    private int maxMana;
    
    
    
    public ManaProperties(EntityPlayer player)
    {
        this.player = player;
        this.currentMana = 30;
        this.maxMana = 30;
    }
    
    public static final void register(EntityPlayer player)
    {
        player.registerExtendedProperties(ManaProperties.EXT_PROP_MANA, new ManaProperties(player));
    }
    
    public static final ManaProperties get(EntityPlayer player)
    {
        return (ManaProperties) player.getExtendedProperties(EXT_PROP_MANA);
    }
    
    @Override
    public void saveNBTData(NBTTagCompound compound)
    {
        NBTTagCompound properties = new NBTTagCompound();
        
        properties.setInteger("CurrentMana", this.currentMana);
        properties.setInteger("MaxMana", this.maxMana);
        
        compound.setTag(EXT_PROP_MANA, properties);    
    }
    
    @Override
    public void loadNBTData(NBTTagCompound compound)
    {
        if(compound.hasKey(EXT_PROP_MANA, Constants.NBT.TAG_COMPOUND))
        {
            NBTTagCompound properties = compound.getCompoundTag(EXT_PROP_MANA);
            
            this.currentMana = properties.getInteger("CurrentMana");
            this.maxMana = properties.getInteger("MaxMana");
        }
    }
    
    @Override
    public void init(Entity entity, World world)
    {
        
    }
    
    public boolean consumeMana(int amount)
    {
        if(this.currentMana >= amount)
        {
            currentMana = currentMana - amount;
            return true;
        }
        
        this.syncMana();
        
        return false;
    }
    
    public void replenishMana()
    {
        this.currentMana = this.maxMana;
        
        this.syncMana();
    }
    
    public int getMaxMana() 
    {
        return this.maxMana;
    }
    
    public int getCurrentMana()
    {
        return this.maxMana;
    }
    
    public final void syncMana()
    {
        RunicInscription.network.sendTo(new MaxManaMessage(this.maxMana), (EntityPlayerMP) player);
        RunicInscription.network.sendTo(new CurrentManaMessage(this.currentMana), (EntityPlayerMP) player);
    }
}

 

 

 

 

 

and it's handler:

 

 


package melonslise.runicinscription.Handler;

import cpw.mods.fml.common.eventhandler.SubscribeEvent;
import melonslise.runicinscription.Network.ManaProperties;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraftforge.event.entity.EntityEvent.EntityConstructing;
import net.minecraftforge.event.entity.EntityJoinWorldEvent;
import net.minecraftforge.event.entity.living.LivingEvent.LivingUpdateEvent;
import net.minecraftforge.event.entity.player.PlayerEvent.Clone;

public class HandlerManaEvents
{
@SubscribeEvent
public void onEntityConstructing(EntityConstructing event)
{
if (event.entity instanceof EntityPlayer)
{
event.entity.registerExtendedProperties(ManaProperties.EXT_PROP_MANA, new ManaProperties((EntityPlayer) event.entity));
}
}

@SubscribeEvent
public void onClonePlayer(Clone event)
{
if(event.wasDeath)
{
NBTTagCompound compound = new NBTTagCompound();
ManaProperties.get(event.original).saveNBTData(compound);
ManaProperties.get(event.entityPlayer).loadNBTData(compound);
}
}

@SubscribeEvent
public void onEntityJoinWorld(EntityJoinWorldEvent event)
{
if (!event.entity.worldObj.isRemote && event.entity instanceof EntityPlayer)
ManaProperties.get((EntityPlayer) event.entity).syncMana();
}
}

 

 

which is registered in the main mod class:

 

 


@EventHandler
public void init(FMLInitializationEvent event)
{
MinecraftForge.EVENT_BUS.register(new HandlerManaEvents());

 

 

and here's the GUI:

 

 


package melonslise.runicinscription.GUI;

import cpw.mods.fml.common.eventhandler.EventPriority;
import cpw.mods.fml.common.eventhandler.SubscribeEvent;
import melonslise.runicinscription.Network.ManaProperties;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.Gui;
import net.minecraftforge.client.event.RenderGameOverlayEvent;
import net.minecraftforge.client.event.RenderGameOverlayEvent.ElementType;

public class GUIManaBar extends Gui
{
private Minecraft mc;

public GUIManaBar(Minecraft mc)
{
super();

this.mc = mc;
}

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

ManaProperties props = ManaProperties.get(this.mc.thePlayer);

if (props == null)
{
return;
}

System.out.print("Drawn");

mc.fontRenderer.drawStringWithShadow("§1" + props.getCurrentMana() + "/" + props.getMaxMana(), 2, 2, 1);
}
}

 

 

and here's it's handler:

 

 


package melonslise.runicinscription.Handler;

import cpw.mods.fml.client.GuiNotification;
import cpw.mods.fml.common.eventhandler.SubscribeEvent;
import melonslise.runicinscription.GUI.GUIManaBar;
import net.minecraft.client.Minecraft;
import net.minecraftforge.client.event.RenderGameOverlayEvent;
import net.minecraftforge.client.event.RenderGameOverlayEvent.ElementType;

public class HandlerGUI
{
@SubscribeEvent
public void onRenderGUI(RenderGameOverlayEvent.Post event)
{

}
}

 

 

And here's the handler and the GUI in the main class:

 

 

 @EventHandler
public void init(FMLInitializationEvent event)
{
MinecraftForge.EVENT_BUS.register(new HandlerGUI());
}

@EventHandler
public void postInit(FMLPostInitializationEvent event)
{
{
if (FMLCommonHandler.instance().getEffectiveSide().isClient())
MinecraftForge.EVENT_BUS.register(new GUIManaBar(Minecraft.getMinecraft()));
}
}

 

 

And here's a packet for the players' max mana

 

 

 package melonslise.runicinscription.Network;

import cpw.mods.fml.common.network.simpleimpl.IMessage;
import io.netty.buffer.ByteBuf;

public class MaxManaMessage implements IMessage
{
private int maxMana;

public MaxManaMessage()
{

}

public MaxManaMessage(int maxMana)
{
this.maxMana = maxMana;
}

@Override
public void toBytes(ByteBuf buf)
{
buf.writeInt(maxMana);
}

@Override
public void fromBytes(ByteBuf buf)
{
maxMana = buf.readInt();
}

public int getMaxMana()
{
return maxMana;
}
}

 

 

and it's handler:

 

 

package melonslise.runicinscription.Handler;

import cpw.mods.fml.common.network.simpleimpl.IMessage;
import cpw.mods.fml.common.network.simpleimpl.IMessageHandler;
import cpw.mods.fml.common.network.simpleimpl.MessageContext;
import melonslise.runicinscription.RunicInscription;
import melonslise.runicinscription.Network.CurrentManaMessage;
import melonslise.runicinscription.Proxy.CommonProxy;
import net.minecraft.entity.player.EntityPlayer;

public class HandlerCurrentManaMessage implements IMessageHandler<CurrentManaMessage, IMessage>
{
@Override
public IMessage onMessage(CurrentManaMessage message, MessageContext ctx)
{
EntityPlayer player = RunicInscription.proxy.getClientPlayer();

int currentMana = message.getCurrentMana();

return null;
}

}

 

 

and here's the packet for the players' current mana:

 

package melonslise.runicinscription.Network;

import cpw.mods.fml.common.network.simpleimpl.IMessage;
import io.netty.buffer.ByteBuf;

public class CurrentManaMessage implements IMessage
{
private int currentMana;

public CurrentManaMessage()
{

}

public CurrentManaMessage(int currentMana)
{
this.currentMana = currentMana;
}

@Override
public void toBytes(ByteBuf buf)
{
buf.writeInt(currentMana);
}

@Override
public void fromBytes(ByteBuf buf)
{
currentMana = buf.readInt();
}

public int getCurrentMana()
{
return currentMana;
}
}

 

 

and it's handler:

 

 

 package melonslise.runicinscription.Handler;

import cpw.mods.fml.common.network.simpleimpl.IMessage;
import cpw.mods.fml.common.network.simpleimpl.IMessageHandler;
import cpw.mods.fml.common.network.simpleimpl.MessageContext;
import melonslise.runicinscription.RunicInscription;
import melonslise.runicinscription.Network.MaxManaMessage;
import net.minecraft.entity.player.EntityPlayer;

public class HandlerMaxManaMessage implements IMessageHandler<MaxManaMessage, IMessage>
{
@Override
public IMessage onMessage(MaxManaMessage message, MessageContext ctx)
{
EntityPlayer player = RunicInscription.proxy.getClientPlayer();

int amount = message.getMaxMana();

return null;
}
}

 

 

And both are registered in the main class:

 

public static SimpleNetworkWrapper network;[/p]

network = NetworkRegistry.INSTANCE.newSimpleChannel("RunicInscription");
        
        network.registerMessage(HandlerCurrentManaMessage.class, CurrentManaMessage.class, 1, Side.CLIENT);
        network.registerMessage(HandlerMaxManaMessage.class, MaxManaMessage.class, 2, Side.CLIENT);

 

 

And there's a method in the client proxy and server proxy which return either the player(for clients) or null(for servers) which is used in the packet handlers:

 

 

ClientProxy

 


public EntityClientPlayerMP getClientPlayer()
{
return Minecraft.getMinecraft().thePlayer;
}

 

ServerProxy

 

    public EntityClientPlayerMP getClientPlayer()
    {
        return null;
    }

 

 

 

And here's a snippet of code from an item which decreases the player's mana:

 

 

 if (props.consumeMana(15))
{
System.out.println("Player had enough mana. Do something awesome!");
}
else
{
System.out.println("Player ran out of mana. Sad face.");
props.replenishMana();
}

 

Link to comment
Share on other sites

Top->bottom:

 

1. Let's make utility method for registering and then not use it...

public static final void register(EntityPlayer player)

Why does it exist if yo uare not using if (in constructing event)?

 

2. This is useless:

public GUIManaBar(Minecraft mc)
{
super();

* super() is empty, no need to call it.

* having constructor is close to useless - seriosuly, you don't have to make additional reference.

 

3. Why does this class even exist? HandlerGUI

 

4. What the fuck? How many init events do you have?

1st you post:

@EventHandler
public void init(FMLInitializationEvent event)
{
MinecraftForge.EVENT_BUS.register(new HandlerManaEvents());

Then:

@EventHandler
public void init(FMLInitializationEvent event)
{
MinecraftForge.EVENT_BUS.register(new HandlerGUI());
}

You should have one...

 

5. PostInit is NOT for constructing your mod... registering events should be in pre or init.

 

6. Whet the fuck is this?

if (FMLCommonHandler.instance().getEffectiveSide().isClient())

 

I just taught you (yesterday) how to use proxies - use them!

 

7. What the hell is this class? "EntityClientPlayerMP" - "EntityPlayer" if any... (proxies).

 

8. How do even expect those packet handlers to work - you literally do nothing in them!

 

@Override
public IMessage onMessage(CurrentManaMessage message, MessageContext ctx)
{
EntityPlayer player = RunicInscription.proxy.getClientPlayer();

int currentMana = message.getCurrentMana();

Properties.get(player).setMana(currentMana); // THIS IS LIKE SO OBVIOUS!!

return null;
}

 

9. There are so many wrong things here... have one event class, not shitload, besides - you don't need to learn java, you need to learn programming.

 

1.7.10 is no longer supported by forge, you are on your own.

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




×
×
  • Create New...

Important Information

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