Jump to content

EntityJoinWorldEvent fired multiple times


Codemetry

Recommended Posts

2 minutes ago, Codemetry said:

Why is it fired multiple times when a player joins a server (and a world)? Are there any alternative events?

The even is fired on both the client and the server. Make sure to only do your code on the logical server by checking if World#isRemote is false.

VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING

I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect.

Forge and vanilla BlockState generator.

Link to comment
Share on other sites

16 minutes ago, Animefan8888 said:

The even is fired on both the client and the server. Make sure to only do your code on the logical server by checking if World#isRemote is false.

I am new to forge modding, so please forgive me for my basic questions.

 

I am trying to make the player send a chat message when joined a server world. Before ensuring World#isRemote is false, the chat message is sent 3 times every 3 seconds. When I added the check, no chat message is sent.

Edited by Codemetry
Link to comment
Share on other sites

8 minutes ago, Codemetry said:

I am new to forge modding, so please forgive me for my basic questions.

 

I am trying to make the player send a chat message when joined a server world. Before ensuring World#isRemote is false, the chat message is sent 3 times every 3 seconds. When I added the check, no chat message is sent.

Post your code. And this includes how you registered your event handler.

VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING

I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect.

Forge and vanilla BlockState generator.

Link to comment
Share on other sites

What's the current state of the code?

 

7 minutes ago, Codemetry said:

I am trying to make the player send a chat message when joined a server world.

By this do you mean you want to send a message to the player, or send a message via the player?

Fancy 3D Graphing Calculator mod, with many different coordinate systems.

Lightweight 3D/2D position/vector transformations library, also with support for different coordinate systems.

Link to comment
Share on other sites

1 hour ago, SerpentDagger said:

What's the current state of the code?

 

By this do you mean you want to send a message to the player, or send a message via the player?

via the player to the server

1 hour ago, Animefan8888 said:

Post your code. And this includes how you registered your event handler.

	Minecraft mc;
	@EventHandler
	public void preInit(FMLPreInitializationEvent event) {
		mc = Minecraft.getMinecraft();
		MinecraftForge.EVENT_BUS.register(this);
	}
	@SubscribeEvent
	public void onEntityJoinWorld(EntityJoinWorldEvent event) {
		if (!event.world.isRemote) {
			mc.thePlayer.sendChatMessage("hello");
		}
	}

 

Edited by Codemetry
Link to comment
Share on other sites

2 minutes ago, SerpentDagger said:

It would be helpful if you could show the whole classes, as the event system is impacted by that.

package com.example.examplemod;

import net.minecraft.client.Minecraft;
import net.minecraft.client.multiplayer.ServerData;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.entity.EntityJoinWorldEvent;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.common.Mod.EventHandler;
import net.minecraftforge.fml.common.event.FMLPreInitializationEvent;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;

@Mod(modid = ExampleMod.MODID, version = ExampleMod.VERSION)
public class ExampleMod {
	public static final String MODID = "examplemod";
	public static final String VERSION = "1.0";
	Minecraft mc;

	@EventHandler
	public void preInit(FMLPreInitializationEvent event) {
		mc = Minecraft.getMinecraft();
		MinecraftForge.EVENT_BUS.register(this);
	}
	
	@SubscribeEvent
	public void onEntityJoinWorld(EntityJoinWorldEvent event) {
		if (!event.world.isRemote) {
			mc.thePlayer.sendChatMessage("hello");
		}
	}
}

 

Link to comment
Share on other sites

1 hour ago, Codemetry said:

I am on recommended version of 1.8.9 of Forge.

Sorry 1.8.9 is an extremely outdated version and we no longer support it on this forum. Please update to receive support. Once a forum moderator sees this they will lock it.

VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING

I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect.

Forge and vanilla BlockState generator.

Link to comment
Share on other sites

1 hour ago, Codemetry said:

Updated

What version have you updated to and post your current code.

VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING

I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect.

Forge and vanilla BlockState generator.

Link to comment
Share on other sites

  • 3 weeks later...

Bump.

I have updated to 1.14.4 - 28.1.0.

I did some testing and found that:

  • EntityJoinWorldEvent is fired once when joining a server (and a world) from the multiplayer menu
  • EntityJoinWorldEvent is fired multiple times (usually 6 - 10) when switching between worlds on a server or servers on a bungee network
  • The Worlds returned by EntityJoinWorldEvents are not the same instance
  • The EntityJoinWorldEvents are not the same instance (that means I did not register more than once)

I have already added the following check into EntityJoinWorldEvent handler

mc.thePlayer == event.entity

Here is my handler in case:

	@SubscribeEvent
	public void onEntityJoinWorld(EntityJoinWorldEvent event) {
		if (mc.thePlayer == event.entity) {
			mc.thePlayer.addChatMessage(
					new ChatComponentText("hello"));
		}
	}

Does anyone know why this is happening?

Link to comment
Share on other sites

52 minutes ago, Codemetry said:

Bump.

I have updated to 1.14.4 - 28.1.0.

I did some testing and found that:

  • EntityJoinWorldEvent is fired once when joining a server (and a world) from the multiplayer menu
  •  EntityJoinWorldEvent is fired multiple times (usually 6 - 10) when switching between worlds on a server or servers on a bungee network
  • The Worlds returned by EntityJoinWorldEvents are not the same instance
  • The EntityJoinWorldEvents are not the same instance (that means I did not register more than once)

I have already added the following check into EntityJoinWorldEvent handler


mc.thePlayer == event.entity

Here is my handler in case:


	@SubscribeEvent
	public void onEntityJoinWorld(EntityJoinWorldEvent event) {
		if (mc.thePlayer == event.entity) {
			mc.thePlayer.addChatMessage(
					new ChatComponentText("hello"));
		}
	}

Does anyone know why this is happening?

if you updated to 1.14.4: Where did you get the method `addChatMessage` and `ChatComponentText` class?

Link to comment
Share on other sites

19 hours ago, Codemetry said:

if (mc.thePlayer == event.entity) {

mc here is almost certainly the result of calling Minecraft.getMinecraft(), which won't work on a server (it'll crash the server).

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

36 minutes ago, Draco18s said:

mc here is almost certainly the result of calling Minecraft.getMinecraft(), which won't work on a server (it'll crash the server).

I am working on a client side mod.

19 hours ago, MairwunNx said:

if you updated to 1.14.4: Where did you get the method `addChatMessage` and `ChatComponentText` class?

Sorry I was in a rush and copied from my old java file.

Link to comment
Share on other sites

50 minutes ago, Codemetry said:

 Sorry I was in a rush and copied from my old java file.

You should post your new updated code.

VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING

I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect.

Forge and vanilla BlockState generator.

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.