Last active
April 8, 2023 12:55
-
-
Save extendedclip/a54def1f5c9a1f0ba8db to your computer and use it in GitHub Desktop.
Chat injector using the new injector API (PlaceholderAPI v2.3.5)
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.extendedclip.papi.injector.chat; | |
import java.util.regex.Matcher; | |
import org.bukkit.Bukkit; | |
import org.bukkit.entity.Player; | |
import org.bukkit.event.EventHandler; | |
import org.bukkit.event.EventPriority; | |
import org.bukkit.event.Listener; | |
import org.bukkit.event.player.AsyncPlayerChatEvent; | |
import com.comphenix.protocol.ProtocolLibrary; | |
import com.comphenix.protocol.events.PacketAdapter; | |
import me.clip.placeholderapi.PlaceholderAPI; | |
import me.clip.placeholderapi.PlaceholderAPIPlugin; | |
import me.clip.placeholderapi.injector.PlaceholderInjector; | |
public class ChatInjector extends PlaceholderInjector implements Listener { | |
private PacketAdapter chat; | |
@Override | |
public boolean canInject() { | |
return Bukkit.getPluginManager().getPlugin("ProtocolLib") != null; | |
} | |
@Override | |
public void inject() { | |
if (PlaceholderAPIPlugin.isSpigot()) { | |
chat = new SpigotChatPacketListener(); | |
} else { | |
chat = new ChatPacketListener(); | |
} | |
} | |
@Override | |
public void eject() { | |
if (chat != null) { | |
ProtocolLibrary.getProtocolManager().removePacketListener(chat); | |
chat = null; | |
} | |
} | |
@Override | |
public String getName() { | |
return "chat"; | |
} | |
@Override | |
public String getVersion() { | |
return "1.0.0"; | |
} | |
//if injector implements listener, the listener will always be registered automatically | |
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) | |
public void onChat(AsyncPlayerChatEvent e) { | |
Player p = e.getPlayer(); | |
String message = e.getMessage(); | |
Matcher matcher = PlaceholderAPI.getBracketPlaceholderPattern().matcher(message); | |
if (p.hasPermission("placeholderapi.injector.chat.bypass")) { | |
if (matcher.find()) { | |
message = PlaceholderAPI.setBracketPlaceholders(p, message); | |
} | |
} else { | |
while (matcher.find()) { | |
message = matcher.replaceAll(""); | |
} | |
} | |
if (message.isEmpty()) { | |
e.setCancelled(true); | |
return; | |
} | |
e.setMessage(message); | |
String format = e.getFormat(); | |
matcher = PlaceholderAPI.getBracketPlaceholderPattern().matcher(format); | |
if (matcher.find()) { | |
format = PlaceholderAPI.setBracketPlaceholders(p, format); | |
e.setFormat(format); | |
} | |
} | |
} |
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.extendedclip.papi.injector.chat; | |
import me.clip.placeholderapi.PlaceholderAPI; | |
import me.clip.placeholderapi.PlaceholderAPIPlugin; | |
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.PacketEvent; | |
import com.comphenix.protocol.reflect.StructureModifier; | |
import com.comphenix.protocol.wrappers.WrappedChatComponent; | |
public class ChatPacketListener extends PacketAdapter { | |
public ChatPacketListener() { | |
super(PlaceholderAPIPlugin.getInstance(), ListenerPriority.HIGHEST, PacketType.Play.Server.CHAT); | |
ProtocolLibrary.getProtocolManager().addPacketListener(this); | |
} | |
@Override | |
public void onPacketSending(PacketEvent e) { | |
if (e.getPlayer() == null) { | |
return; | |
} | |
StructureModifier<WrappedChatComponent> chat = e.getPacket().getChatComponents(); | |
WrappedChatComponent c = chat.read(0); | |
if (c == null) { | |
return; | |
} | |
String msg = c.getJson(); | |
if (msg == null) { | |
return; | |
} | |
if (!PlaceholderAPI.getBracketPlaceholderPattern().matcher(msg).find()) { | |
return; | |
} | |
msg = PlaceholderAPI.setBracketPlaceholders(e.getPlayer(), msg); | |
chat.write(0, WrappedChatComponent.fromJson(msg)); | |
} | |
} |
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.extendedclip.papi.injector.chat; | |
import net.md_5.bungee.api.chat.BaseComponent; | |
import net.md_5.bungee.chat.ComponentSerializer; | |
import me.clip.placeholderapi.PlaceholderAPI; | |
import me.clip.placeholderapi.PlaceholderAPIPlugin; | |
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.PacketEvent; | |
import com.comphenix.protocol.reflect.StructureModifier; | |
import com.comphenix.protocol.wrappers.WrappedChatComponent; | |
/** | |
@author extended_clip, GGhost | |
*/ | |
public class SpigotChatPacketListener extends PacketAdapter { | |
public SpigotChatPacketListener() { | |
super(PlaceholderAPIPlugin.getInstance(), ListenerPriority.HIGHEST, PacketType.Play.Server.CHAT); | |
ProtocolLibrary.getProtocolManager().addPacketListener(this); | |
} | |
@Override | |
public void onPacketSending(PacketEvent e) { | |
if (e.getPlayer() == null) { | |
return; | |
} | |
StructureModifier<WrappedChatComponent> chat = e.getPacket().getChatComponents(); | |
WrappedChatComponent c = chat.read(0); | |
if (c == null) { | |
StructureModifier<BaseComponent[]> modifier = e.getPacket().getSpecificModifier(BaseComponent[].class); | |
BaseComponent[] components = modifier.readSafely(0); | |
if (components == null) { | |
return; | |
} | |
String msg = ComponentSerializer.toString(components); | |
if (msg == null) { | |
return; | |
} | |
if (!PlaceholderAPI.getBracketPlaceholderPattern().matcher(msg).find()) { | |
return; | |
} | |
msg = PlaceholderAPI.setBracketPlaceholders(e.getPlayer(), msg); | |
modifier.write(0, ComponentSerializer.parse(msg)); | |
return; | |
} | |
String msg = c.getJson(); | |
if (msg == null) { | |
return; | |
} | |
if (!PlaceholderAPI.getBracketPlaceholderPattern().matcher(msg).find()) { | |
return; | |
} | |
msg = PlaceholderAPI.setBracketPlaceholders(e.getPlayer(), msg); | |
chat.write(0, WrappedChatComponent.fromJson(msg)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://garuda4dslot.net/