Created
June 17, 2022 09:21
-
-
Save LOOHP/42a4e9bf39f8b2aa3de2c46fcea2f317 to your computer and use it in GitHub Desktop.
VentureChat 1.19 workaround
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.loohp.venturechatworkaround; | |
import com.comphenix.protocol.PacketType; | |
import com.comphenix.protocol.ProtocolLibrary; | |
import com.comphenix.protocol.events.ListenerPriority; | |
import com.comphenix.protocol.events.PacketAdapter; | |
import com.comphenix.protocol.events.PacketAdapter.AdapterParameteters; | |
import com.comphenix.protocol.events.PacketContainer; | |
import com.comphenix.protocol.events.PacketEvent; | |
import com.google.gson.Gson; | |
import com.google.gson.JsonElement; | |
import com.google.gson.JsonObject; | |
import com.google.gson.JsonParser; | |
import net.md_5.bungee.api.ChatColor; | |
import org.bukkit.plugin.java.JavaPlugin; | |
import java.util.Iterator; | |
import java.util.Map.Entry; | |
public class VentureChatWorkaround extends JavaPlugin { | |
@Override | |
public void onEnable() { | |
ProtocolLibrary.getProtocolManager().addPacketListener(new PacketAdapter(new AdapterParameteters().plugin(this).listenerPriority(ListenerPriority.LOWEST).optionAsync().types(PacketType.Play.Server.SYSTEM_CHAT)) { | |
@Override | |
public void onPacketSending(PacketEvent event) { | |
PacketContainer packet = event.getPacket(); | |
String json = packet.getStrings().read(0); | |
if (json == null) { | |
return; | |
} | |
JsonElement root = JsonParser.parseString(json); | |
filterInvalidEvents(root); | |
String result = new Gson().toJson(root); | |
packet.getStrings().write(0, result); | |
} | |
}); | |
getServer().getConsoleSender().sendMessage(ChatColor.GREEN + "VentureChatWorkaround has been enabled!"); | |
} | |
@Override | |
public void onDisable() { | |
getServer().getConsoleSender().sendMessage(ChatColor.RED + "VentureChatWorkaround had been disabled!"); | |
} | |
public void filterInvalidEvents(JsonElement jsonElement) { | |
if (jsonElement.isJsonObject()) { | |
JsonObject jsonObject = jsonElement.getAsJsonObject(); | |
Iterator<Entry<String, JsonElement>> itr = jsonObject.entrySet().iterator(); | |
while (itr.hasNext()) { | |
Entry<String, JsonElement> entry = itr.next(); | |
String key = entry.getKey(); | |
JsonElement value = entry.getValue(); | |
if (key.equals("clickEvent")) { | |
JsonObject clickEvent = value.getAsJsonObject(); | |
if (clickEvent.get("action").getAsString().equals("")) { | |
itr.remove(); | |
} | |
} else if (key.equals("hoverEvent")) { | |
JsonObject hoverEvent = value.getAsJsonObject(); | |
if (hoverEvent.get("action").getAsString().equals("")) { | |
itr.remove(); | |
} | |
} else { | |
filterInvalidEvents(value); | |
} | |
} | |
} else if (jsonElement.isJsonArray()) { | |
for (JsonElement element : jsonElement.getAsJsonArray()) { | |
filterInvalidEvents(element); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment