dmulloy2 / ProtocolLib

Provides read and write access to the Minecraft protocol with Bukkit.
GNU General Public License v2.0
1.02k stars 262 forks source link

WrappedChatComponent doesn't work on 1.5.2 #139

Closed DenAbr closed 8 years ago

DenAbr commented 8 years ago

Method fromText of WrappedChatComponent throws an exception when I use it on 1.5.2.

        at ru.Den_Abr.ChatGuard.Listeners.PacketsListener$ChatPacketListner.onPa
cketSending(PacketsListener.java:94) // WrappedChatComponent.fromText(message)
        at com.comphenix.protocol.injector.SortedPacketListenerList.invokeSendin
gListener(SortedPacketListenerList.java:195)
        at com.comphenix.protocol.injector.SortedPacketListenerList.invokePacket
Sending(SortedPacketListenerList.java:149)
        at com.comphenix.protocol.injector.PacketFilterManager.handlePacket(Pack
etFilterManager.java:637)
        at com.comphenix.protocol.injector.PacketFilterManager.invokePacketSendi
ng(PacketFilterManager.java:613)
        at com.comphenix.protocol.injector.spigot.SpigotPacketInjector.packetQue
ued(SpigotPacketInjector.java:529)
        at com.comphenix.protocol.injector.spigot.SpigotPacketInjector.packetQue
ued(SpigotPacketInjector.java:509)
        at com.comphenix.protocol.injector.spigot.SpigotPacketInjector$2.interce
pt(SpigotPacketInjector.java:222)
        at org.spigotmc.netty.PacketListener$$EnhancerByCGLIB$$a7e7ea56.packetQu
eued(<generated>)
        at org.spigotmc.netty.PacketListener.callQueued(PacketListener.java:73)
        at org.spigotmc.netty.NettyNetworkManager.queue(NettyNetworkManager.java
:168)
        at net.minecraft.server.v1_5_R3.PlayerConnection.sendPacket(PlayerConnec
tion.java:731)
        at org.bukkit.craftbukkit.v1_5_R3.entity.CraftPlayer.sendRawMessage(Craf
tPlayer.java:131)
        at org.bukkit.craftbukkit.v1_5_R3.entity.CraftPlayer.sendMessage(CraftPl
ayer.java:136)
        at org.bukkit.command.Command.broadcastCommandMessage(Command.java:361)
        at org.bukkit.command.Command.broadcastCommandMessage(Command.java:336)
        at org.bukkit.command.defaults.ReloadCommand.execute(ReloadCommand.java:
25)
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:18
9)
        at org.bukkit.craftbukkit.v1_5_R3.CraftServer.dispatchCommand(CraftServe
r.java:546)
        at org.bukkit.craftbukkit.v1_5_R3.CraftServer.dispatchServerCommand(Craf
tServer.java:535)
        at net.minecraft.server.v1_5_R3.DedicatedServer.an(DedicatedServer.java:
263)
        at net.minecraft.server.v1_5_R3.DedicatedServer.r(DedicatedServer.java:2
28)
        at net.minecraft.server.v1_5_R3.MinecraftServer.q(MinecraftServer.java:4
72)
        at net.minecraft.server.v1_5_R3.MinecraftServer.run(MinecraftServer.java
:404)
        at net.minecraft.server.v1_5_R3.ThreadServerApplication.run(SourceFile:5
73)
Caused by: java.lang.IllegalStateException: Could not find ChatSerializer class.

        at com.comphenix.protocol.utility.MinecraftReflection.getChatSerializerC
lass(MinecraftReflection.java:796)
        at com.comphenix.protocol.wrappers.WrappedChatComponent.<clinit>(Wrapped
ChatComponent.java:17)
        ... 25 more
Caused by: java.lang.RuntimeException: Unable to find ChatSerializer (IChatBaseC
omponent$ChatSerializer)
        at com.comphenix.protocol.utility.MinecraftReflection.getMinecraftClass(
MinecraftReflection.java:1996)
        at com.comphenix.protocol.utility.MinecraftReflection.getChatSerializerC
lass(MinecraftReflection.java:793)
        ... 26 more
Parameters:
  net.minecraft.server.v1_5_R3.Packet3Chat@3a3e8c00[
    message=з7зo[CONSOLE: зaReload complete.]
    c=true
    m=<null>
    timestamp=1453282719685
    lowPriority=false
    packetID=3
  ]```
games647 commented 8 years ago

Try to use getStrings() instead.

DenAbr commented 8 years ago

I know it, but i need to use the same code in different versions

games647 commented 8 years ago

That's impossible if the protocol changed from a string to a json component. If you read the Javadoc description of the method getChatComponents, you see that this method is only for 1.7+.

Retrieves a read/write structure for chat components in Minecraft 1.7.2. *

* This modifier will automatically marshall between WrappedChatComponent and the * internal Minecraft IChatBaseComponent.

Same for the class description.

Represents a chat component added in Minecraft 1.7.2

You can try to detect the server version and then use the methods the server supports. Example:


private boolean supportsJson;

public void sendMessage() {
    String message = ChatColor.DARK_RED + "Test";
    if (supportsJson) {
        WrappedChatComponent jsonComponent = WrappedChatComponent.fromText(message);
        //send packet using .getChatComponents()
    } else {
       //send packet using getStrings()
    }
}
DenAbr commented 8 years ago

Thanks. I thought that there is way to make more graceful solution. So than I continue to use the same method as you described.