Skip to content

Commit

Permalink
1.19 message receive fix
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmonkey4eva committed Jun 17, 2022
1 parent fd40644 commit ca35a9c
Show file tree
Hide file tree
Showing 12 changed files with 152 additions and 172 deletions.
@@ -1,5 +1,7 @@
package com.denizenscript.denizen.events.player;

import net.md_5.bungee.api.ChatColor;

public class PlayerReceivesActionbarScriptEvent extends PlayerReceivesMessageScriptEvent {

// <--[event]
Expand Down Expand Up @@ -32,8 +34,12 @@ public PlayerReceivesActionbarScriptEvent() {
instance = this;
}

@Override
public ChatColor baseColor() {
return ChatColor.WHITE;
}

public static PlayerReceivesActionbarScriptEvent instance;
public boolean modified;

@Override
public boolean couldMatch(ScriptPath path) {
Expand Down
@@ -1,15 +1,16 @@
package com.denizenscript.denizen.events.player;

import com.denizenscript.denizen.objects.PlayerTag;
import com.denizenscript.denizen.utilities.FormattedTextHelper;
import com.denizenscript.denizen.utilities.implementation.BukkitScriptEntryData;
import com.denizenscript.denizen.events.BukkitScriptEvent;
import com.denizenscript.denizen.utilities.packets.NetworkInterceptHelper;
import com.denizenscript.denizencore.objects.core.ElementTag;
import com.denizenscript.denizencore.objects.ObjectTag;
import com.denizenscript.denizencore.scripts.ScriptEntryData;
import com.denizenscript.denizencore.utilities.CoreUtilities;

import java.util.function.Consumer;
import net.md_5.bungee.api.ChatColor;
import net.md_5.bungee.chat.ComponentSerializer;

public class PlayerReceivesMessageScriptEvent extends BukkitScriptEvent {

Expand All @@ -25,7 +26,7 @@ public class PlayerReceivesMessageScriptEvent extends BukkitScriptEvent {
//
// @Warning Using this will forcibly sync the chat thread.
//
// @Triggers when a player receives any chat message from the server.
// @Triggers when a player receives any chat message from the server. This does not normally include *player* chat, instead prefer <@link event player chats> for that.
//
// @Context
// <context.message> returns an ElementTag of the message.
Expand All @@ -48,16 +49,21 @@ public PlayerReceivesMessageScriptEvent() {
public ElementTag message;
public ElementTag rawJson;
public ElementTag system;
public boolean modified;
public PlayerTag player;
public boolean loaded;

public Consumer<String> modifyMessage;
public Consumer<String> modifyRawJson;
public Consumer<Boolean> modifyCancellation;
public ChatColor baseColor() {
return ChatColor.BLACK;
}

@Override
public void cancellationChanged() {
modifyCancellation.accept(cancelled);
public void reset() {
player = null;
message = null;
rawJson = null;
system = null;
cancelled = false;
modified = false;
}

@Override
Expand Down Expand Up @@ -87,13 +93,15 @@ public boolean applyDetermination(ScriptPath path, ObjectTag determinationObj) {
String determination = determinationObj.toString();
String lower = CoreUtilities.toLowerCase(determination);
if (lower.startsWith("message:")) {
message = new ElementTag(determination.substring("message:".length()));
modifyMessage.accept(message.asString());
message = new ElementTag(determination.substring("message:".length()), true);
rawJson = new ElementTag(ComponentSerializer.toString(FormattedTextHelper.parse(message.asString(), baseColor())), true);
modified = true;
return true;
}
if (lower.startsWith("raw_json:")) {
rawJson = new ElementTag(determination.substring("raw_json:".length()));
modifyRawJson.accept(rawJson.asString());
message = new ElementTag(FormattedTextHelper.stringify(ComponentSerializer.parse(rawJson.asString()), baseColor()), true);
modified = true;
return true;
}
}
Expand Down
Expand Up @@ -6,15 +6,9 @@ public interface PacketOutChat {

boolean isActionbar();

int getPosition();

String getMessage();

String getRawJson();

void setPosition(int position);

void setMessage(String message);

void setRawJson(String rawJson);
}
Expand Up @@ -4,14 +4,14 @@
import com.denizenscript.denizen.events.player.*;
import com.denizenscript.denizen.nms.NMSHandler;
import com.denizenscript.denizen.nms.NMSVersion;
import com.denizenscript.denizen.utilities.implementation.DenizenCoreImplementation;
import com.denizenscript.denizen.nms.interfaces.packets.*;
import com.denizenscript.denizen.utilities.debugging.Debug;
import com.denizenscript.denizen.objects.EntityTag;
import com.denizenscript.denizen.objects.PlayerTag;
import com.denizenscript.denizen.scripts.commands.player.GlowCommand;
import com.denizenscript.denizen.scripts.commands.server.ExecuteCommand;
import com.denizenscript.denizencore.DenizenCore;
import com.denizenscript.denizencore.events.ScriptEvent;
import com.denizenscript.denizencore.objects.core.ElementTag;
import org.bukkit.Bukkit;
import org.bukkit.Material;
Expand Down Expand Up @@ -114,43 +114,45 @@ public boolean shouldInterceptChatPacket() {
|| PlayerReceivesActionbarScriptEvent.instance.loaded;
}

public boolean sendPacket(final Player player, final PacketOutChat chat) {
public PlayerReceivesMessageScriptEvent sendPacket(final Player player, final PacketOutChat chat) {
if (!chat.isActionbar() && ExecuteCommand.silencedPlayers.contains(player.getUniqueId())) {
return true;
PlayerReceivesMessageScriptEvent event = (PlayerReceivesMessageScriptEvent) PlayerReceivesMessageScriptEvent.instance.clone();
event.cancelled = true;
return event;
}
if (chat.getMessage() == null) {
return false;
return null;
}
final PlayerReceivesMessageScriptEvent event = chat.isActionbar() ? PlayerReceivesActionbarScriptEvent.instance : PlayerReceivesMessageScriptEvent.instance;
if (event.loaded) {
Callable<Boolean> eventCall = () -> {
Callable<PlayerReceivesMessageScriptEvent> eventCall = () -> {
event.reset();
event.message = new ElementTag(chat.getMessage());
event.rawJson = new ElementTag(chat.getRawJson());
event.system = new ElementTag(chat.isSystem());
event.player = PlayerTag.mirrorBukkitPlayer(player);
event.modifyMessage = chat::setMessage;
event.modifyRawJson = chat::setRawJson;
event.cancelled = false;
event.modifyCancellation = (c) -> event.cancelled = c;
event.fire();
return event.cancelled;
PlayerReceivesMessageScriptEvent result = (PlayerReceivesMessageScriptEvent) event.fire();
if (result.modified) {
chat.setRawJson(result.rawJson.asString());
}
return result;
};
try {
if (DenizenCore.implementation.isSafeThread()) {
return eventCall.call();
}
else {
FutureTask<Boolean> futureTask = new FutureTask<>(eventCall);
FutureTask<PlayerReceivesMessageScriptEvent> futureTask = new FutureTask<>(eventCall);
Bukkit.getScheduler().runTask(Denizen.getInstance(), futureTask);
return futureTask.get();
}
}
catch (Exception e) {
Debug.echoError(e);
return false;
return null;
}
}
return false;
return null;
}

public boolean shouldInterceptMetadata() {
Expand Down
@@ -1,5 +1,6 @@
package com.denizenscript.denizen.nms.v1_16.impl.network.handlers;

import com.denizenscript.denizen.events.player.PlayerReceivesMessageScriptEvent;
import com.denizenscript.denizen.nms.abstracts.BlockLight;
import com.denizenscript.denizen.nms.v1_16.Handler;
import com.denizenscript.denizen.nms.v1_16.impl.ProfileEditorImpl;
Expand Down Expand Up @@ -819,7 +820,9 @@ public boolean processMirrorForPacket(Packet<?> packet) {

public boolean processPacketHandlerForPacket(Packet<?> packet) {
if (packet instanceof PacketPlayOutChat && DenizenPacketHandler.instance.shouldInterceptChatPacket()) {
return DenizenPacketHandler.instance.sendPacket(player.getBukkitEntity(), new PacketOutChatImpl((PacketPlayOutChat) packet));
PacketOutChatImpl packetHelper = new PacketOutChatImpl((PacketPlayOutChat) packet);
PlayerReceivesMessageScriptEvent result = DenizenPacketHandler.instance.sendPacket(player.getBukkitEntity(), packetHelper);
return result != null && result.cancelled;
}
else if (packet instanceof PacketPlayOutEntityMetadata && DenizenPacketHandler.instance.shouldInterceptMetadata()) {
return DenizenPacketHandler.instance.sendPacket(player.getBukkitEntity(), new PacketOutEntityMetadataImpl((PacketPlayOutEntityMetadata) packet));
Expand Down
Expand Up @@ -54,11 +54,6 @@ public boolean isActionbar() {
return position == ChatMessageType.GAME_INFO;
}

@Override
public int getPosition() {
return position.ordinal();
}

@Override
public String getMessage() {
return message;
Expand All @@ -69,31 +64,6 @@ public String getRawJson() {
return rawJson;
}

@Override
public void setPosition(int position) {
try {
POSITION.set(internal, position);
}
catch (Exception e) {
Debug.echoError(e);
}
}

@Override
public void setMessage(String message) {
try {
if (!bungee) {
MESSAGE.set(internal, Handler.componentToNMS(FormattedTextHelper.parse(message, ChatColor.WHITE)));
}
else {
internal.components = FormattedTextHelper.parse(message, ChatColor.WHITE);
}
}
catch (Exception e) {
Debug.echoError(e);
}
}

@Override
public void setRawJson(String rawJson) {
try {
Expand Down
Expand Up @@ -2,6 +2,7 @@

import com.denizenscript.denizen.events.player.PlayerHearsSoundScriptEvent;
import com.denizenscript.denizen.events.player.PlayerReceivesActionbarScriptEvent;
import com.denizenscript.denizen.events.player.PlayerReceivesMessageScriptEvent;
import com.denizenscript.denizen.nms.abstracts.BlockLight;
import com.denizenscript.denizen.nms.v1_17.Handler;
import com.denizenscript.denizen.nms.v1_17.ReflectionMappingsInfo;
Expand All @@ -23,6 +24,7 @@
import com.denizenscript.denizen.utilities.entity.HideEntitiesHelper;
import com.denizenscript.denizen.utilities.packets.DenizenPacketHandler;
import com.denizenscript.denizen.utilities.packets.HideParticles;
import com.denizenscript.denizencore.events.ScriptEvent;
import com.denizenscript.denizencore.objects.core.ElementTag;
import com.denizenscript.denizencore.utilities.CoreConfiguration;
import com.mojang.datafixers.util.Pair;
Expand Down Expand Up @@ -271,27 +273,17 @@ public boolean processActionbarPacket(Packet<?> packet, GenericFutureListener<?
ClientboundSetActionBarTextPacket actionbarPacket = (ClientboundSetActionBarTextPacket) packet;
PlayerReceivesActionbarScriptEvent event = PlayerReceivesActionbarScriptEvent.instance;
Component baseComponent = actionbarPacket.getText();
event.reset();
event.message = new ElementTag(FormattedTextHelper.stringify(Handler.componentToSpigot(baseComponent), ChatColor.WHITE));
event.rawJson = new ElementTag(Component.Serializer.toJson(baseComponent));
event.system = new ElementTag(false);
event.player = PlayerTag.mirrorBukkitPlayer(player.getBukkitEntity());
event.modifyMessage = (msg) -> {
event.message = new ElementTag(msg);
event.modified = true;
};
event.modifyRawJson = (json) -> {
event.message = new ElementTag(FormattedTextHelper.stringify(ComponentSerializer.parse(json), ChatColor.WHITE));
event.modified = true;
};
event.modifyCancellation = (c) -> event.cancelled = c;
event.cancelled = false;
event.modified = false;
event.fire();
event = (PlayerReceivesActionbarScriptEvent) event.fire();
if (event.cancelled) {
return true;
}
if (event.modified) {
Component component = Handler.componentToNMS(FormattedTextHelper.parse(event.message.asString(), ChatColor.WHITE));
Component component = Handler.componentToNMS(ComponentSerializer.parse(event.rawJson.asString()));
ClientboundSetActionBarTextPacket newPacket = new ClientboundSetActionBarTextPacket(component);
oldManager.send(newPacket, genericfuturelistener);
return true;
Expand Down Expand Up @@ -951,7 +943,9 @@ public boolean processMirrorForPacket(Packet<?> packet) {

public boolean processPacketHandlerForPacket(Packet<?> packet) {
if (packet instanceof ClientboundChatPacket && DenizenPacketHandler.instance.shouldInterceptChatPacket()) {
return DenizenPacketHandler.instance.sendPacket(player.getBukkitEntity(), new PacketOutChatImpl((ClientboundChatPacket) packet));
PacketOutChatImpl packetHelper = new PacketOutChatImpl((ClientboundChatPacket) packet);
PlayerReceivesMessageScriptEvent result = DenizenPacketHandler.instance.sendPacket(player.getBukkitEntity(), packetHelper);
return result != null && result.cancelled;
}
else if (packet instanceof ClientboundSetEntityDataPacket && DenizenPacketHandler.instance.shouldInterceptMetadata()) {
return DenizenPacketHandler.instance.sendPacket(player.getBukkitEntity(), new PacketOutEntityMetadataImpl((ClientboundSetEntityDataPacket) packet));
Expand Down
Expand Up @@ -53,11 +53,6 @@ public boolean isActionbar() {
return position == ChatType.GAME_INFO;
}

@Override
public int getPosition() {
return position.ordinal();
}

@Override
public String getMessage() {
return message;
Expand All @@ -68,31 +63,6 @@ public String getRawJson() {
return rawJson;
}

@Override
public void setPosition(int position) {
try {
POSITION.set(internal, position);
}
catch (Exception e) {
Debug.echoError(e);
}
}

@Override
public void setMessage(String message) {
try {
if (!bungee) {
MESSAGE.set(internal, Handler.componentToNMS(FormattedTextHelper.parse(message, ChatColor.WHITE)));
}
else {
internal.components = FormattedTextHelper.parse(message, ChatColor.WHITE);
}
}
catch (Exception e) {
Debug.echoError(e);
}
}

@Override
public void setRawJson(String rawJson) {
try {
Expand Down

0 comments on commit ca35a9c

Please sign in to comment.