Luohuayu / CatServer

高性能和高兼容性的1.12.2/1.16.5/1.18.2版本Forge+Bukkit+Spigot服务端 (A high performance and high compatibility 1.12.2/1.16.5/1.18.2 version Forge+Bukkit+Spigot server)
https://catmc.org
GNU Lesser General Public License v3.0
1.98k stars 211 forks source link

Fix ServerChatEvent not being called (#653) #658

Closed andriihorpenko closed 1 year ago

andriihorpenko commented 1 year ago

This PR fixes the issue when ServerChatEvent is not getting called. This also closes #653.

Cause

The event was not called as it was in the unreachable code segment:

// The "else if" branch is always executed, that's why the "else" branch is unreachable

} else if (true) {
  this.chat(p_244548_1_, true);
  // CraftBukkit end
} else {
  ITextComponent itextcomponent = new TranslationTextComponent("chat.type.text", this.field_147369_b.func_145748_c_(), p_244548_1_);
  ITextComponent itextcomponent = new TranslationTextComponent("chat.type.text", this.field_147369_b.func_145748_c_(), net.minecraftforge.common.ForgeHooks.newChatWithLinks(p_244548_1_));
  itextcomponent = net.minecraftforge.common.ForgeHooks.onServerChatEvent(this, p_244548_1_, itextcomponent);
  if (itextcomponent == null) return;
  this.field_147367_d.func_184103_al().func_232641_a_(itextcomponent, ChatType.CHAT, this.field_147369_b.func_110124_au());
}

Solution

In order to fix this issue, the whole event execution was transferred to the ServerPlayNetHandler#chat function:

+   // CatServer start - Fire the forge event first, and replace the message sent to bukkit, removing the <player>
+   ITextComponent itextcomponent = new TranslationTextComponent("chat.type.text", this.field_147369_b.func_145748_c_(), net.minecraftforge.common.ForgeHooks.newChatWithLinks(s));
+   itextcomponent = net.minecraftforge.common.ForgeHooks.onServerChatEvent(this, s, itextcomponent);
+   if (itextcomponent == null) return;
+   s = itextcomponent.getString().substring(3 + this.field_147369_b.func_145748_c_().getString().length());
+   // CatServer end

Similar approaches

The similar approach may be found here.

Kotori0629 commented 1 year ago

Thank you for your contribution, please wait patiently for the review