Jump to content

Double code execution on event


Lex023

Recommended Posts

I'm trying to write a test mod, I can't solve the problem. When I right-click with a stick in my hands, I expect to send one chat message to the player, but I get two

How to solve this problem?

 

 

Test.java

package com.alexstank.test;

import com.alexstank.test.events.ModClientEvents;
import net.minecraft.block.Blocks;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.event.lifecycle.FMLClientSetupEvent;
import net.minecraftforge.fml.event.lifecycle.FMLCommonSetupEvent;
import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

@Mod("test")
public class Test
{
    public static final Logger LOGGER = LogManager.getLogger();
    public Test() {
        FMLJavaModLoadingContext.get().getModEventBus().addListener(this::setup);
        FMLJavaModLoadingContext.get().getModEventBus().addListener(this::doClientStuff);
        MinecraftForge.EVENT_BUS.register(ModClientEvents.class);
    }

    private void setup(final FMLCommonSetupEvent event)
    {
        LOGGER.info("HELLO FROM PREINIT");
        LOGGER.info("DIRT BLOCK >> {}", Blocks.DIRT.getRegistryName());
    }

    private void doClientStuff(final FMLClientSetupEvent event) {
        LOGGER.info("Got game settings {}", event.getMinecraftSupplier().get().gameSettings);
    }

}

 

ModClientEvents.java

package com.alexstank.test.events;


import com.alexstank.test.Test;
import net.minecraft.entity.LivingEntity;
import net.minecraft.item.Items;
import net.minecraft.util.text.ITextComponent;
import net.minecraftforge.client.event.ColorHandlerEvent;
import net.minecraftforge.event.entity.player.PlayerInteractEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;


public class ModClientEvents {
    @SubscribeEvent
    public static void OnRightMouseClick(PlayerInteractEvent.RightClickItem event) {
        LivingEntity player = event.getEntityLiving();
        if(player.getHeldItemMainhand().getItem() == Items.STICK) {
            System.out.println("help me please");
            player.sendMessage(ITextComponent.func_244388_a("help me please!"), player.getUniqueID());
            Test.LOGGER.info("double execution");
        }
    }
}

 

Log

[19:00:10] [Render thread/INFO] [STDOUT/]: [com.alexstank.test.events.ModClientEvents:OnRightMouseClick:18]: help me please
[19:00:10] [Server thread/INFO] [STDOUT/]: [com.alexstank.test.events.ModClientEvents:OnRightMouseClick:18]: help me please
[19:00:10] [Server thread/INFO] [co.al.te.Test/]: double execution
[19:00:10] [Render thread/INFO] [minecraft/NewChatGui]: [CHAT] help me please!
[19:00:10] [Render thread/INFO] [co.al.te.Test/]: double execution
[19:00:10] [Render thread/INFO] [minecraft/NewChatGui]: [CHAT] help me please!

 

minecraft.png

Edited by Lex023
Link to comment
Share on other sites

2 minutes ago, Lex023 said:

[Render thread/INFO] [STDOUT/]: [com.alexstank.test.events.ModClientEvents:OnRightMouseClick:18]: help me please [19:00:10] [Server thread/INFO] [STDOUT/]: [com.alexstank.test.events.ModClientEvents:OnRightMouseClick:18]: help me please

It's called on both threads. You should make the code execute on a specific side.

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.



×
×
  • Create New...

Important Information

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