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

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.

  • Author
17 hours ago, ChampionAsh5357 said:

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

Thanks, all worked well.

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.