Jump to content

Recommended Posts

Posted

I am having an issue with my mod where there will be chat messages that repeat, though I can't seem to find it. There's no issue in single player, but on multiplayer, this happens:

cftfZgS.png

This server has an anti-spam, where 2 messages in a row can't be the same, therefore it's repeating messages. However, I can't figure out where in the code it would do that (I'm probably missing something obvious).

[spoiler=Main]package com.wuble.chattranslate;

 

import com.wuble.chattranslate.handler.ChatHandler;

import com.wuble.chattranslate.handler.ConfigurationHandler;

import com.wuble.chattranslate.proxy.IProxy;

import com.wuble.chattranslate.reference.ClassReference;

import com.wuble.chattranslate.reference.NameReference;

import net.minecraftforge.common.MinecraftForge;

import net.minecraftforge.common.config.Configuration;

import net.minecraftforge.fml.common.FMLCommonHandler;

import net.minecraftforge.fml.common.Mod;

import net.minecraftforge.fml.common.SidedProxy;

import net.minecraftforge.fml.common.event.FMLInitializationEvent;

import net.minecraftforge.fml.common.event.FMLPostInitializationEvent;

import net.minecraftforge.fml.common.event.FMLPreInitializationEvent;

import com.memetix.mst.translate.Translate;

 

 

@Mod(modid = NameReference.MODID, name = NameReference.MODNAME, version = NameReference.VERSION)

public class ChatTranslate {

 

    public static String langDefault, clientId, clientSecret;

    @Mod.Instance(NameReference.MODID)

    public static ChatTranslate instance;

 

    @SidedProxy(clientSide = ClassReference.CLIENT_PROXY_CLASS, serverSide =  ClassReference.SERVER_PROXY_CLASS)

    public static IProxy proxy;

 

    @Mod.EventHandler

    public static void preInit (FMLPreInitializationEvent event){

        ConfigurationHandler.init(event.getSuggestedConfigurationFile());

        FMLCommonHandler.instance().bus().register(new ConfigurationHandler());

        try{ initializeTranslate(); } catch (Exception e) {e.printStackTrace(); }

        MinecraftForge.EVENT_BUS.register(new ChatHandler());

 

 

    }

 

    @Mod.EventHandler

    public static void init(FMLInitializationEvent event){

 

    }

 

    @Mod.EventHandler

    public static void postInit(FMLPostInitializationEvent event){

 

    }

    public static void initializeTranslate() throws Exception {

        Translate.setClientId(ConfigurationHandler.clientId);

        Translate.setClientSecret(ConfigurationHandler.clientSecret);

    }

 

}

 

 

[spoiler=Thread]package com.wuble.chattranslate.thread;

 

import com.memetix.mst.detect.Detect;

import com.memetix.mst.language.Language;

import com.memetix.mst.translate.Translate;

import com.wuble.chattranslate.handler.ChatHandler;

import com.wuble.chattranslate.handler.ConfigurationHandler;

import net.minecraft.client.Minecraft;

import net.minecraft.util.ChatComponentText;

import net.minecraft.util.EnumChatFormatting;

 

public class TranslateThread extends Thread {

    public static String receivedMessage, receivedFormatted;

    private static Language from, langDefault;

 

    @Override

    public void run(){

        receivedMessage = ChatHandler.receivedMessage;

        receivedFormatted = ChatHandler.receivedFormatted;

        langDefault = Language.fromString(ConfigurationHandler.langDefault);

        try {from = Detect.execute(receivedMessage);} catch(Exception e){e.printStackTrace();}

        if (from == langDefault){

            Minecraft.getMinecraft().thePlayer.addChatComponentMessage(new ChatComponentText(receivedFormatted));

        }

        else {

            try {Minecraft.getMinecraft().thePlayer.addChatComponentMessage(new ChatComponentText(Translate.execute(receivedMessage,

                from, langDefault) + EnumChatFormatting.GRAY + EnumChatFormatting.ITALIC + " [From: " + from.getName(langDefault) + "]" ).setChatStyle(ChatHandler.clickableChat)); } catch(Exception e) { e.printStackTrace(); }

        }

    }

}

 

 

[spoiler=ChatHandler]package com.wuble.chattranslate.handler;

 

import com.wuble.chattranslate.thread.TranslateThread;

import net.minecraft.event.HoverEvent;

import net.minecraft.util.ChatComponentText;

import net.minecraft.util.ChatStyle;

import net.minecraftforge.client.event.ClientChatReceivedEvent;

import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;

 

 

public class ChatHandler {

 

  public static String receivedMessage, receivedFormatted;

    public static ChatStyle clickableChat;

 

    @SubscribeEvent

    public void onClientChatReceived(ClientChatReceivedEvent event){

        receivedFormatted = event.message.getFormattedText();

        receivedMessage = event.message.getUnformattedText();

        HoverEvent chatHover = new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ChatComponentText(receivedFormatted));

        clickableChat = new ChatStyle().setChatHoverEvent(chatHover);

        event.setCanceled(true);

        new TranslateThread().start();

 

 

    }

 

}

 

 

 

This will break horribly, you cannot add chat messages from a different thread.

^^ This appears to have been the issue.

Posted

First, don't use static fields. Just pass the variables to the Thread.

I. Stellarium for Minecraft: Configurable Universe for Minecraft! (WIP)

II. Stellar Sky, Better Star Rendering&Sky Utility mod, had separated from Stellarium.

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.

Announcements



×
×
  • Create New...

Important Information

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