Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Featured Replies

Posted

I have an event handler that I use to tell whenever an entity gets attacked, and do some other stuff with that.  The problem is, the event is getting called twice per attack, on the client side.

 

I have checks that if the server is dedicated, it will only get called on the server, which works. But when the server is integrated, it only gets called on the client, and is called twice.

 

Here's my code for the attack handler. The LivingDeathEvent works properly, it's only the LivingAttackEvent

 

 

public class AttackHandler {

@SubscribeEvent
public void entityAttacked(LivingAttackEvent e) {

	if (MinecraftServer.getServer().isDedicatedServer()) {
		if (!IslandSurvival.proxy.isRemote()) {
			return;
		}
	}

	EntityPlayer player;
	if (e.source.getSourceOfDamage() instanceof EntityPlayer) {
		player = (EntityPlayer) e.source.getSourceOfDamage();
	} else {
		return;
	}

	Integer level = SkillAttack.getLevelForWeapon(player.getHeldItem().getItem());
	if (level == null)
		return;
	int attackLevel = IslandSurvival.instance.getPlayerLevelsDatabase().getPlayerLevels(player.getPersistentID())
			.getAttackLevel();
	if (attackLevel < level) {
		player.addChatMessage(new ChatComponentTranslation(EnumChatFormatting.RED + "You need level " + level
				+ " Attack to use the " + player.getHeldItem().getDisplayName() + "!"));
		e.setCanceled(true);
	}
}

@SubscribeEvent
public void onLivingDeath(LivingDeathEvent e) {

	EntityPlayer player;
	if (e.source.getSourceOfDamage() instanceof EntityPlayer) {
		player = (EntityPlayer) e.source.getSourceOfDamage();
		if (player.getHeldItem() == null)
			return;

		Integer exp = 0;
		if (e.entity instanceof EntityPlayer) {
			exp = 10;
		} else {

			exp = SkillAttack.getExpForMob((EntityLiving) e.entity);

		}
		System.out.println(exp);

		IslandSurvival.instance.getPlayerLevelsDatabase().getPlayerLevels(player.getPersistentID())
				.addAttackExp(exp, player);
		return;

	}
	return;

}

}

 

 

 

And where I register the AttackHandler class.

 

 

@Mod.EventHandler
public void preInit(FMLPreInitializationEvent e) {
	// Item/block init and registering

	ISItems.init();
	ISTools.init();
	ISBlocks.init();

	SkillsMain.init();
	SkillMining.init();
	SkillCrafting.init();

	network = NetworkRegistry.INSTANCE.newSimpleChannel("MyChannel");
	network.registerMessage(Handler.class, CraftingMessage.class, 0, Side.SERVER);

	// Config handling
	loadDatabase();

	//handlers
	bbHandler = new BlockBreakingHandler();

	MinecraftForge.EVENT_BUS.register(new ConnectionHandler());
	MinecraftForge.EVENT_BUS.register(new AttackHandler());

	if (!proxy.isRemote()) {
		KeyBindings.init();
		FMLCommonHandler.instance().bus().register(new KeyInputHandler());

	} else {

	}

}

 

 

 

And here is the console output:

 

 

 

[17:41:24] [Client thread/INFO]: [CHAT] §cYou need level 20 Attack to use the Diamond Sword!

[17:41:24] [Client thread/INFO]: [CHAT] §cYou need level 20 Attack to use the Diamond Sword!

[17:41:25] [Client thread/INFO]: [CHAT] §cYou need level 20 Attack to use the Diamond Sword!

[17:41:25] [Client thread/INFO]: [CHAT] §cYou need level 20 Attack to use the Diamond Sword!

[17:41:28] [Client thread/INFO]: [CHAT] §cYou need level 20 Attack to use the Diamond Sword!

[17:41:28] [Client thread/INFO]: [CHAT] §cYou need level 20 Attack to use the Diamond Sword!

 

 

Any help would be much appreciated! Not sure if this is a bug or what.

 

 

 

I'd suggest using proxies.

 

Only put your client stuff you are interested for that handler on the client side and vice versa for the server.

 

Will make it easier to troubleshoot.  If I had to wager, by the time you do that, I'm betting the issue goes away.

Long time Bukkit & Forge Programmer

Happy to try and help

  • Author

Not sure I completely follow.  I'm checking "proxy.isRemote()," is that not what you mean?  Would you recommend putting the event handler in the proxy classes?

I put all my events in a side specific proxy section.  I'm very deliberate in separating the client/server.  It tends to avoid confusion.  I looked briefly through your stuff and there were a few things in there that make me think you could benefit from the same.  However, that is my opinion, not gospel.

Long time Bukkit & Forge Programmer

Happy to try and help

  • Author

That's a good idea! I can't believe I had never thought about that! Thanks for the tip, I'll try it out. And even if it doesn't work, that seems like a really good practice.

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

Important Information

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

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.