Skip to content

Instantly share code, notes, and snippets.

@Phoenix616
Last active August 30, 2021 12:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Phoenix616/d9d46784f21e7d5274bf1d364bef1cca to your computer and use it in GitHub Desktop.
Save Phoenix616/d9d46784f21e7d5274bf1d364bef1cca to your computer and use it in GitHub Desktop.
/**
* Copyright (C) 2016 Max Lee (https://github.com/Phoenix616/)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
protocolManager = ProtocolLibrary.getProtocolManager();
protocolManager.addPacketListener(
new PacketAdapter(plugin, priority, PacketType.Play.Server.CHAT) {
public void onPacketSending(PacketEvent event) {
if (isServerSpigot()) {
for (int i = 0; i < event.getPacket().getModifier().size(); i++) {
Object modifier = event.getPacket().getModifier().read(i);
if (hasAdventure && modifier instanceof Component) {
String json = GsonComponentSerializer.gson().serialize((Component) modifier);
json = getReplacedJson(event.getPlayer(), json);
if (json != null) {
try {
Component component = GsonComponentSerializer.gson().deserialize(json);
event.getPacket().getModifier().write(i, component);
} catch (JsonParseException e) {
for(int j = 0; j < event.getPacket().getChatComponents().size(); j++) {
event.getPacket().getChatComponents().write(j, WrappedChatComponent.fromJson(json));
}
}
}
} else if (modifier instanceof BaseComponent[]) {
String json = toJson((BaseComponent[]) modifier);
json = getReplacedJson(event.getPlayer(), json);
if (json != null) {
BaseComponent[] components = parseJson(json);
event.getPacket().getModifier().write(i, components);
if (components == null) {
for(int j = 0; j < event.getPacket().getChatComponents().size(); j++) {
event.getPacket().getChatComponents().write(j, WrappedChatComponent.fromJson(json));
}
}
}
}
}
}
for(int i = 0; i < event.getPacket().getChatComponents().size(); i++) {
WrappedChatComponent chatComponent = event.getPacket().getChatComponents().read(i);
if(chatComponent == null)
continue;
try {
chatComponent = replaceInComponent(event.getPlayer(), chatComponent);
if (chatComponent != null) {
event.getPacket().getChatComponents().write(i, chatComponent);
}
} catch (Exception e) {
plugin.getLogger().log(Level.SEVERE, "Error while trying to set component! " + (chatComponent != null ? " Json: " + chatComponent.getJson() : ""), e);
}
}
}
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment