Jump to content
  • Home
  • Files
  • Docs
  • Merch
Topics
  • All Content

  • This Topic
  • This Forum

  • Advanced Search
  • Existing user? Sign In  

    Sign In



    • Not recommended on shared computers


    • Forgot your password?

  • Sign Up
  • All Activity
  • Home
  • Mod Developer Central
  • Modder Support
  • LivingAttackEvent called twice, on Client Side
1.13 Update Notes for Mod Creators
Sign in to follow this  
Followers 1
skeeter144

LivingAttackEvent called twice, on Client Side

By skeeter144, August 15, 2015 in Modder Support

  • Reply to this topic
  • Start new topic

Recommended Posts

skeeter144    1

skeeter144

skeeter144    1

  • Tree Puncher
  • skeeter144
  • Members
  • 1
  • 15 posts
Posted August 15, 2015

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.

 

 

 

  • Quote

Share this post


Link to post
Share on other sites

delpi    71

delpi

delpi    71

  • Dragon Slayer
  • delpi
  • Forge Modder
  • 71
  • 840 posts
Posted August 16, 2015

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.

  • Quote

Share this post


Link to post
Share on other sites

skeeter144    1

skeeter144

skeeter144    1

  • Tree Puncher
  • skeeter144
  • Members
  • 1
  • 15 posts
Posted August 16, 2015

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?

  • Quote

Share this post


Link to post
Share on other sites

delpi    71

delpi

delpi    71

  • Dragon Slayer
  • delpi
  • Forge Modder
  • 71
  • 840 posts
Posted August 16, 2015

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.

  • Quote

Share this post


Link to post
Share on other sites

skeeter144    1

skeeter144

skeeter144    1

  • Tree Puncher
  • skeeter144
  • Members
  • 1
  • 15 posts
Posted August 16, 2015

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.

  • Quote

Share this post


Link to post
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.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  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.

  • Insert image from URL
×
  • Desktop
  • Tablet
  • Phone
Sign in to follow this  
Followers 1
Go To Topic Listing



  • Recently Browsing

    No registered users viewing this page.

  • Posts

    • darkgreenminer
      [1.12.2] Multiple Structure Generation

      By darkgreenminer · Posted 43 minutes ago

      Someone else had that same problem of their mod only spawning the last structure added to WorldGenCustomStructures, and I remembered that the solution I found was what a commentor named Redstone Tim mentioned, that in WorldGenStructure you have to remove 'static'.  I'm happy to email my version of these two classes to you if you want to have a look.  It took me hours and hours to get them working.  I have no idea what might cause the cascading gen lag, but fixing the multiple structure problem might help.    
    • troyvs
      problems starting with modding

      By troyvs · Posted 1 hour ago

      what command did you run to set up?  
    • MightyAhmed
      Immediate Crash On Any Version Of Forge

      By MightyAhmed · Posted 2 hours ago

      ok so its been a while but it was workling fine before somehow but now minecraft still works it just freezes and lagspikes every 5 seconds please help me on this issue i cant find anything on the internet also ihave 4GB ram total in my computer and i have dedicated 2gb ram to minecraft in the JVM arguments section also i have 125 mods installed.
    • deanvangreunen
      Custom Armor Item - Help - MC/Forge 1.14.4/1.14.3

      By deanvangreunen · Posted 2 hours ago

      Hello, I'm in progress of making a Minecraft 1.14.4 Mod using Forge. Needing some help, Could you please look at the following Class File and Explain what I'm doing wrong or what I should be doing?.   The "OnArmorTick" and other ".....Tick" functions don't work.   My intent: - if water is below and near player by 1 block while the boots are on then turn the water into ice. I'm trying to implement "Frost Walking Boots"   Code: - FrostBootsItem.java <- File I need help with - My Project Repo  <- Repo, So if you want to see how my mod is setup. (includes my world saves, etc)   Dev Details: - Minecraft Version: 1.14.4 - Minecraft Snapshot: 20191020-1.14.3 - Forge Version: 1.14.4-28.1.61   Notes: - I've followed a tutorial for 1.14.4 modding by MCJty on youtube (The author of RFTools) - I'm new to minecraft modding. I have expeirenced as a software developer/engineer.   ❤️❤️❤️❤️❤️❤️❤️❤️❤️  ❤️  .Thanks in advance. ❤️  ❤️❤️❤️❤️❤️❤️❤️❤️❤️ 
    • DragonITA
      [1.14.4] How to get Minecraft Horse model/texture to make a custom unicorn?

      By DragonITA · Posted 2 hours ago

      please see the screenshoot above.
  • Topics

    • Merthew
      8
      [1.12.2] Multiple Structure Generation

      By Merthew
      Started November 7, 2018

    • coolian
      1
      problems starting with modding

      By coolian
      Started October 9

    • MightyAhmed
      83
      Immediate Crash On Any Version Of Forge

      By MightyAhmed
      Started November 10

    • deanvangreunen
      0
      Custom Armor Item - Help - MC/Forge 1.14.4/1.14.3

      By deanvangreunen
      Started 2 hours ago

    • DragonITA
      48
      [1.14.4] How to get Minecraft Horse model/texture to make a custom unicorn?

      By DragonITA
      Started December 9

  • Who's Online (See full list)

    • dylandmrl
    • Yanny7
    • jan
    • LTNightshade
    • ignaciro
    • Zero
    • troyvs
    • CrackThrough
    • A-Game
  • All Activity
  • Home
  • Mod Developer Central
  • Modder Support
  • LivingAttackEvent called twice, on Client Side
  • Theme
  • Contact Us
  • Discord

Copyright © 2019 ForgeDevelopment LLC · Ads by Curse Powered by Invision Community