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.

[1.12] sendMessage(new TextComponentTranslation()); duplicating sent messages

Featured Replies

Posted

I'm trying to test out the sendMessage function with an item that sends a message through chat when you right-click with it. However, for some reason, it's having a bit of an issue: it's sending the message twice. I have no idea why it's happening, but it is. I've got a video of that happening here, and I'll include the code for the item under a spoiler. Anyone have an idea what's happening?

Spoiler

package space.bbkr.aura.item;

import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.util.text.TextComponentTranslation;
import net.minecraft.util.text.TextFormatting;
import net.minecraft.util.EnumActionResult;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.EnumHand;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraft.item.Item;
import space.bbkr.aura.Aura;

public class ItemNote extends ItemBase {


    protected String name;

    public ItemNote(String name) {
        super(name);
        setMaxStackSize(1);
        setCreativeTab(Aura.creativeTab);
    }


    public EnumActionResult onItemUse(EntityPlayer p, World w, BlockPos pos, EnumHand h, EnumFacing f, float x, float y, float z) {
        p.sendMessage(new TextComponentTranslation("aura.note.start"));
        return EnumActionResult.SUCCESS;
    }


}

ItemBase, the class ItemNote extends, is:


package space.bbkr.aura.item;

import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.item.Item;
import space.bbkr.aura.Aura;

public class ItemBase extends Item {

    protected String name;

    public ItemBase(String name) {
        this.name = name;
        setUnlocalizedName(name);
        setRegistryName(name);
    }

    public void registerItemModel() {
        Aura.proxy.registerItemRenderer(this, 0, name);
    }

    @Override
    public ItemBase setCreativeTab(CreativeTabs tab) {
        super.setCreativeTab(tab);
        return this;
    }
}

 

 

Keep in mind that the onItemUse() method will be called twice: once on the server, and once on the client. That's why you're getting two outputs, since both calls are sending a message.

 

You can check which side is calling the method from the World#isRemote member. In your case, the world is a parameter called w, so you can check w.isRemote to see the side. If isRemote is false, it's the server, if it's true, it's the client.

 

For instance, if your code only does things on the server, you can simply bypass it on the client by putting this snippet at the top of your method body:

 

if (w.isRemote) {
	return EnumActionResult.PASS;
}

 

Edited by IceMetalPunk

Whatever Minecraft needs, it is most likely not yet another tool tier.

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.