Skip to content

Commit

Permalink
Fix message sending on legacy versions (#5106)
Browse files Browse the repository at this point in the history
  • Loading branch information
APickledWalrus authored Oct 11, 2022
1 parent 207ab9b commit 0269fe2
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 35 deletions.
28 changes: 3 additions & 25 deletions src/main/java/ch/njol/skript/effects/EffBroadcast.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,13 @@
import ch.njol.skript.doc.Examples;
import ch.njol.skript.doc.Name;
import ch.njol.skript.doc.Since;
import ch.njol.skript.expressions.ExprColoured;
import ch.njol.skript.lang.Effect;
import ch.njol.skript.lang.Expression;
import ch.njol.skript.lang.ExpressionList;
import ch.njol.skript.lang.SkriptParser.ParseResult;
import ch.njol.skript.lang.VariableString;
import ch.njol.skript.registrations.Classes;
import ch.njol.skript.util.LiteralUtils;
import ch.njol.skript.util.chat.BungeeConverter;
import ch.njol.skript.util.chat.ChatMessages;
import ch.njol.util.Kleenean;
import ch.njol.util.coll.CollectionUtils;
import net.md_5.bungee.api.chat.BaseComponent;
import org.bukkit.Bukkit;
import org.bukkit.World;
import org.bukkit.command.CommandSender;
Expand Down Expand Up @@ -76,33 +70,17 @@ public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelaye
}

@Override
@SuppressWarnings("deprecation")
public void execute(Event e) {
public void execute(Event event) {
List<CommandSender> receivers = new ArrayList<>();
if (worlds == null) {
receivers.addAll(Bukkit.getOnlinePlayers());
receivers.add(Bukkit.getConsoleSender());
} else {
for (World world : worlds.getArray(e))
for (World world : worlds.getArray(event))
receivers.addAll(world.getPlayers());
}

for (Expression<?> message : getMessages()) {
if (message instanceof VariableString) {
BaseComponent[] components = BungeeConverter.convert(((VariableString) message).getMessageComponents(e));
receivers.forEach(receiver -> receiver.spigot().sendMessage(components));
} else if (message instanceof ExprColoured && ((ExprColoured) message).isUnsafeFormat()) { // Manually marked as trusted
for (Object realMessage : message.getArray(e)) {
BaseComponent[] components = BungeeConverter.convert(ChatMessages.parse((String) realMessage));
receivers.forEach(receiver -> receiver.spigot().sendMessage(components));
}
} else {
for (Object messageObject : message.getArray(e)) {
String realMessage = messageObject instanceof String ? (String) messageObject : Classes.toString(messageObject);
receivers.forEach(receiver -> receiver.sendMessage(realMessage));
}
}
}
EffMessage.sendMessage(event, getMessages(), receivers.toArray(new CommandSender[0]), null);
}

private Expression<?>[] getMessages() {
Expand Down
20 changes: 10 additions & 10 deletions src/main/java/ch/njol/skript/effects/EffMessage.java
Original file line number Diff line number Diff line change
Expand Up @@ -104,23 +104,23 @@ public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelaye
}

@Override
protected void execute(Event e) {
Player sender = this.sender != null ? this.sender.getSingle(e) : null;

CommandSender[] commandSenders = recipients.getArray(e);
protected void execute(Event event) {
sendMessage(event, getMessages(), recipients.getArray(event), this.sender != null ? this.sender.getSingle(event) : null);
}

for (Expression<?> message : getMessages()) {
public static void sendMessage(Event event, Expression<?>[] messages, CommandSender[] receivers, @Nullable Player sender) {
for (Expression<?> message : messages) {

Object[] messageArray = null;
List<MessageComponent> messageComponents = null;

for (CommandSender receiver : commandSenders) {
for (CommandSender receiver : receivers) {
if (receiver instanceof Player && message instanceof VariableString) {
if (messageComponents == null)
messageComponents = ((VariableString) message).getMessageComponents(e);
messageComponents = ((VariableString) message).getMessageComponents(event);
} else {
if (messageArray == null)
messageArray = message.getArray(e);
messageArray = message.getArray(event);
}

if (receiver instanceof Player) { // Can use JSON formatting
Expand All @@ -147,7 +147,7 @@ protected void execute(Event e) {
}
}

private void sendMessage(Player receiver, @Nullable Player sender, BaseComponent... components) {
private static void sendMessage(Player receiver, @Nullable Player sender, BaseComponent... components) {
if (SUPPORTS_SENDER && sender != null)
receiver.spigot().sendMessage(sender.getUniqueId(), components);
else
Expand All @@ -161,7 +161,7 @@ private Expression<?>[] getMessages() {
return messages;
}

private String toString(Object object) {
private static String toString(Object object) {
return object instanceof String ? (String) object : Classes.toString(object);
}

Expand Down

0 comments on commit 0269fe2

Please sign in to comment.