Jump to content

[1.7.10] [SOLVED] need a few tips about IExtendedEntityProperties


Recommended Posts

Posted

my implementation of IExtendedEntityProperties adds 3 extra equipment slots.

 

I need to know for sure a few things first. I will probably have more questions latter, but I need the answer for these in order to realize the others:

 

when exactly is the EntityConstructing event raised? Does it get raised more than once for each player during a session?

WIP mods: easyautomation, easyenergy, easyelectronics, easymoney, easytrasportation, easysecurity, easymultiverse, easyfactions, easymagick, easyalchemy, easyseasons

Posted

Use your IDE's Find Usages/Call Hierarchy feature to look for usages of

EntityConstructingEvent

and you'll see that it's fired from the

Entity

constructor.

 

If you look for usages of

EntityPlayerMP

(the server-side player class), you'll see a new instance is created when a player logs into the server (

ServerConfigurationManager#createPlayerForUser

) or respawns after dying or conquering the End (

ServerConfigurationManager#respawnPlayer

).

Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.

Posted

Use your IDE's Find Usages/Call Hierarchy feature to look for usages of

EntityConstructingEvent

and you'll see that it's fired from the

Entity

constructor.

 

If you look for usages of

EntityPlayerMP

(the server-side player class), you'll see a new instance is created when a player logs into the server (

ServerConfigurationManager#createPlayerForUser

) or respawns after dying or conquering the End (

ServerConfigurationManager#respawnPlayer

).

 

thanks. it's very confusing to me because the type hierarchy of

EntityPlayer

is a bit complex, and the call hierarchy of the methods that instantiate new players is also confusing and undocumented and goes through methods without human friendly names.

 

So I can safely assume a new EntityPlayerMP will only be instantiated when one of these two FML events are raised:

 

1)

PlayerEvent.PlayerLoggedInEvent

2)

PlayerEvent.PlayerRespawnEvent

 

what about client side?

WIP mods: easyautomation, easyenergy, easyelectronics, easymoney, easytrasportation, easysecurity, easymultiverse, easyfactions, easymagick, easyalchemy, easyseasons

Posted

No, you cannot safely assume that. If you want to find out when a new player is instantiated, EntityConstructingEvent is the only choice you have.

 

then I will have to use some sort of 2 step initialization of my

IExtendedEntityProperties

, because I need access to fields from

EntityPlayer

which of course are all uninitialized during

EntityConstructing

due to the fact that it is raised from inside

Entity.<init>

. I will instantiate it during

EntityConstructing

and will have to initialize it at some point latter.

WIP mods: easyautomation, easyenergy, easyelectronics, easymoney, easytrasportation, easysecurity, easymultiverse, easyfactions, easymagick, easyalchemy, easyseasons

Posted

Can I make these two assumptions?

 

1) while Minecraft client is running during gameplay, it has only one instance of

EntityClientPlayerMP

, corresponding to the player.

2) for each client connected, there is one corresponding

EntityPlayerMP

instance in the server, and that instance is disposed off when that client's connection is closed.

WIP mods: easyautomation, easyenergy, easyelectronics, easymoney, easytrasportation, easysecurity, easymultiverse, easyfactions, easymagick, easyalchemy, easyseasons

Posted

I do the following to ensure that everytime a player joins the world they get an istance of my IEEP class. This event seems to cover allc ases (I havent found any cases where my IEEP isnt initialized.

 

@SubscribeEvent
public void onEntityConstructing(EntityConstructing event) {

	if (event.entity instanceof EntityPlayer) {
		if (CustomExtendedPlayerProperties.get((EntityPlayer) event.entity) == null) {
			CustomExtendedPlayerProperties.register((EntityPlayer) event.entity);
		}

	}
}

 

Then inside my IEEP

 

public static final CustomExtendedPlayerProperties get(EntityPlayer player) {
	return (CustomExtendedPlayerProperties) player.getExtendedProperties("CustomExtendedPlayerProperties");
}

 

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

 

Posted

I do the following to ensure that everytime a player joins the world they get an istance of my IEEP class. This event seems to cover all cases (I havent found any cases where my IEEP isnt initialized.

 

that's the essence of the first part my solution, but since I need access to the players inventory to initialize my IEEP, and since that inventory is still null during the EntityConstructing event, that part of the solution alone wasn't enough. bellow is a simplified version of my code.

 

public class EventHandler
{
       @SubscribeEvent
public void onEntityConstructing(EntityConstructing event)
{
	if (event.entity instanceof EntityPlayerMP && XServerPlayer.get((EntityPlayerMP) event.entity) == null)
	{
		XServerPlayer.register((EntityPlayerMP)event.entity);
	}
	else if(event.entity instanceof EntityClientPlayerMP && XClientPlayer.get((EntityClientPlayerMP)event.entity) == null)
	{
		XClientPlayer.register((EntityClientPlayerMP) event.entity);
	}
}
}


public class XServerPlayer //(same code is valid for XClientPlayer)
{
        public static void register(EntityPlayerMP player)
{
	XServerPlayer xPlayer = new XServerPlayer(player);
	player.registerExtendedProperties("propertyName", xPlayer);

	//register for the first player tick only
	FMLCommonHandler.instance().bus().register(xPlayer);
}

        public void onPlayerFirstTick(PlayerTickEvent event)
{
	this.initialize();
	//unregister to stop receiving player tick events
	FMLCommonHandler.instance().bus().unregister(this);
}
}

WIP mods: easyautomation, easyenergy, easyelectronics, easymoney, easytrasportation, easysecurity, easymultiverse, easyfactions, easymagick, easyalchemy, easyseasons

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.