Skip to content

Commit

Permalink
fix 'receives message' event for 1.19 (Paper broke it)
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmonkey4eva committed Mar 22, 2023
1 parent ecf8b62 commit 3a74f61
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 16 deletions.
Expand Up @@ -3,6 +3,7 @@
import com.denizenscript.denizen.Denizen;
import com.denizenscript.denizen.nms.NMSHandler;
import com.denizenscript.denizen.nms.NMSVersion;
import com.denizenscript.denizen.nms.interfaces.packets.PacketOutChat;
import com.denizenscript.denizen.objects.EntityTag;
import com.denizenscript.denizen.objects.ItemTag;
import com.denizenscript.denizen.objects.WorldTag;
Expand Down Expand Up @@ -108,6 +109,7 @@ public static void init() {
// Other helpers
Bukkit.getPluginManager().registerEvents(new PaperEventHelpers(), Denizen.getInstance());
PaperAPITools.instance = new PaperAPIToolsImpl();
PacketOutChat.convertComponentToJsonString = (o) -> componentToJson((Component) o);
}

public static Component parseFormattedText(String text, ChatColor baseColor) {
Expand Down
@@ -1,12 +1,16 @@
package com.denizenscript.denizen.nms.interfaces.packets;

public interface PacketOutChat {
import java.util.function.Function;

boolean isSystem();
public abstract class PacketOutChat {

boolean isActionbar();
public abstract boolean isSystem();

String getMessage();
public abstract boolean isActionbar();

String getRawJson();
public abstract String getMessage();

public abstract String getRawJson();

public static Function<Object, String> convertComponentToJsonString;
}
Expand Up @@ -12,7 +12,7 @@

import java.lang.reflect.Field;

public class PacketOutChatImpl implements PacketOutChat {
public class PacketOutChatImpl extends PacketOutChat {

private ClientboundChatPacket internal;
private String message;
Expand Down
Expand Up @@ -12,7 +12,7 @@

import java.lang.reflect.Field;

public class PacketOutChatImpl implements PacketOutChat {
public class PacketOutChatImpl extends PacketOutChat {

private ClientboundChatPacket internal;
private String message;
Expand Down
Expand Up @@ -1171,15 +1171,15 @@ public boolean processPacketHandlerForPacket(Packet<?> packet, PacketSendListene
if (DenizenPacketHandler.instance.shouldInterceptChatPacket()) {
PacketOutChatImpl packetHelper = null;
boolean isActionbar = false;
if (packet instanceof ClientboundSystemChatPacket) {
isActionbar = ((ClientboundSystemChatPacket) packet).overlay();
if (((ClientboundSystemChatPacket) packet).content() == null) { // Makes no sense but this can be null in weird edge cases
if (packet instanceof ClientboundSystemChatPacket chatPacket) {
isActionbar = chatPacket.overlay();
packetHelper = new PacketOutChatImpl(chatPacket);
if (packetHelper.rawJson == null) { // Makes no sense but this can be null in weird edge cases
return false;
}
packetHelper = new PacketOutChatImpl((ClientboundSystemChatPacket) packet);
}
else if (packet instanceof ClientboundPlayerChatPacket) {
packetHelper = new PacketOutChatImpl((ClientboundPlayerChatPacket) packet);
else if (packet instanceof ClientboundPlayerChatPacket playerChatPacket) {
packetHelper = new PacketOutChatImpl(playerChatPacket);
}
if (packetHelper != null) {
PlayerReceivesMessageScriptEvent result = DenizenPacketHandler.instance.sendPacket(player.getBukkitEntity(), packetHelper);
Expand All @@ -1194,8 +1194,8 @@ else if (packet instanceof ClientboundPlayerChatPacket) {
}
}
}
if (packet instanceof ClientboundSetEntityDataPacket && DenizenPacketHandler.instance.shouldInterceptMetadata()) {
return DenizenPacketHandler.instance.sendPacket(player.getBukkitEntity(), new PacketOutEntityMetadataImpl((ClientboundSetEntityDataPacket) packet));
if (packet instanceof ClientboundSetEntityDataPacket dataPacket && DenizenPacketHandler.instance.shouldInterceptMetadata()) {
return DenizenPacketHandler.instance.sendPacket(player.getBukkitEntity(), new PacketOutEntityMetadataImpl(dataPacket));
}
return false;
}
Expand Down
Expand Up @@ -2,21 +2,40 @@

import com.denizenscript.denizen.nms.interfaces.packets.PacketOutChat;
import com.denizenscript.denizen.utilities.FormattedTextHelper;
import com.denizenscript.denizencore.utilities.ReflectionHelper;
import com.denizenscript.denizencore.utilities.debugging.Debug;
import net.md_5.bungee.chat.ComponentSerializer;
import net.minecraft.network.protocol.game.ClientboundPlayerChatPacket;
import net.minecraft.network.protocol.game.ClientboundSystemChatPacket;

public class PacketOutChatImpl implements PacketOutChat {
import java.lang.reflect.Field;

public class PacketOutChatImpl extends PacketOutChat {

public ClientboundPlayerChatPacket playerPacket;
public ClientboundSystemChatPacket systemPacket;
public String message;
public String rawJson;
public boolean isOverlayActionbar;

public static Field paperTextField;

public PacketOutChatImpl(ClientboundSystemChatPacket internal) {
systemPacket = internal;
rawJson = internal.content();
if (rawJson == null && convertComponentToJsonString != null) {
try {
if (paperTextField == null) {
paperTextField = ReflectionHelper.getFields(ClientboundSystemChatPacket.class).get("adventure$content");
}
if (paperTextField != null) {
rawJson = convertComponentToJsonString.apply(paperTextField.get(internal));
}
}
catch (Throwable ex) {
Debug.echoError(ex);
}
}
message = FormattedTextHelper.stringify(ComponentSerializer.parse(rawJson));
isOverlayActionbar = internal.overlay();
}
Expand Down

0 comments on commit 3a74f61

Please sign in to comment.