Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix message sending on legacy versions #5106

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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<>();
APickledWalrus marked this conversation as resolved.
Show resolved Hide resolved
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));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the point of BungeeConverter when VariableString also stores the MessageComponents? Can't BungeeConverter just be removed entirely if we're not going to use it.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MessageComponent is a custom Skript type. BungeeConverter converters our MessageComponent into Spigot/Bungee's component format

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So why was the old code you deleted in this pull request not used again with the BungeeConverter?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not entirely sure what you mean, but EffBroadcast just hooks into the same behavior as EffMessage now. BungeeConverter is called each time those messages are sent though which probably isn't necessary (it has played-dependent behavior AFAIK)

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);
APickledWalrus marked this conversation as resolved.
Show resolved Hide resolved
}

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