diff --git a/src/main/java/ch/njol/skript/events/SimpleEvents.java b/src/main/java/ch/njol/skript/events/SimpleEvents.java
index 36153ae75cb..9ea6c664d0a 100644
--- a/src/main/java/ch/njol/skript/events/SimpleEvents.java
+++ b/src/main/java/ch/njol/skript/events/SimpleEvents.java
@@ -104,6 +104,7 @@
import org.bukkit.event.player.PlayerToggleFlightEvent;
import org.bukkit.event.player.PlayerToggleSneakEvent;
import org.bukkit.event.player.PlayerToggleSprintEvent;
+import org.bukkit.event.server.BroadcastMessageEvent;
import org.bukkit.event.server.ServerListPingEvent;
import org.bukkit.event.vehicle.VehicleCreateEvent;
import org.bukkit.event.vehicle.VehicleDamageEvent;
@@ -118,9 +119,6 @@
import org.bukkit.event.world.PortalCreateEvent;
import org.bukkit.event.world.SpawnChangeEvent;
-/**
- * @author Peter Güttinger
- */
public class SimpleEvents {
static {
Skript.registerEvent("Can Build Check", SimpleEvent.class, BlockCanBuildEvent.class, "[block] can build check")
@@ -744,12 +742,21 @@ public class SimpleEvents {
.description("Called when a player drags an item in their cursor across the inventory.")
.examples(
"on inventory drag:",
- "\tif player's current inventory is {_gui}:",
- "\t\tsend \"You can't drag your items here!\" to player",
- "\t\tcancel event"
+ "\tif player's current inventory is {inventories::custom-gui}:",
+ "\t\tsend \"You can't drag your items here!\" to player",
+ "\t\tcancel event"
)
.since("2.7");
+ Skript.registerEvent("Server Broadcast", SimpleEvent.class, BroadcastMessageEvent.class, "[server] broadcast")
+ .description("Called when the server broadcasts messages. Does not get called when using Skript's broadcast effect.")
+ .examples(
+ "on server broadcast:",
+ "\tbroadcast-message contains \"example\"",
+ "\tcancel event"
+ )
+ .since("INSERT VERSION");
+
if (Skript.classExists("org.bukkit.event.entity.PiglinBarterEvent")) {
Skript.registerEvent("Piglin Barter", SimpleEvent.class, PiglinBarterEvent.class, "piglin (barter[ing]|trad(e|ing))")
.requiredPlugins("Minecraft 1.16+")
@@ -764,6 +771,7 @@ public class SimpleEvents {
)
.since("INSERT VERSION");
}
+
{
final Class extends Event> eventClass;
if (Skript.classExists("org.bukkit.event.block.BellRingEvent")) {
diff --git a/src/main/java/ch/njol/skript/expressions/ExprChatRecipients.java b/src/main/java/ch/njol/skript/expressions/ExprChatRecipients.java
index bda0a00f294..b807a3c520f 100644
--- a/src/main/java/ch/njol/skript/expressions/ExprChatRecipients.java
+++ b/src/main/java/ch/njol/skript/expressions/ExprChatRecipients.java
@@ -18,11 +18,11 @@
*/
package ch.njol.skript.expressions;
-import java.util.Set;
-
+import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.event.Event;
import org.bukkit.event.player.AsyncPlayerChatEvent;
+import org.bukkit.event.server.BroadcastMessageEvent;
import org.eclipse.jdt.annotation.Nullable;
import ch.njol.skript.Skript;
@@ -40,71 +40,68 @@
import ch.njol.util.coll.CollectionUtils;
@Name("Chat Recipients")
-@Description("Recipients of chat events where this is called.")
+@Description("Recipients of chat/broadcast events where this is called.")
@Examples("chat recipients")
-@Since("2.2-Fixes-v7, 2.2-dev35 (clearing recipients)")
-public class ExprChatRecipients extends SimpleExpression {
+@Since("2.2-Fixes-v7, 2.2-dev35 (clearing recipients), INSERT VERSION (broadcast event)")
+public class ExprChatRecipients extends SimpleExpression {
static {
- Skript.registerExpression(ExprChatRecipients.class, Player.class, ExpressionType.SIMPLE, "[chat][( |-)]recipients");
- }
-
- @Override
- public boolean isSingle() {
- return false;
- }
-
- @Override
- public Class getReturnType() {
- return Player.class;
- }
-
- @Override
- public Class>[] acceptChange(final ChangeMode mode) {
- return CollectionUtils.array(Player[].class);
+ Skript.registerExpression(ExprChatRecipients.class, CommandSender.class, ExpressionType.SIMPLE, "[chat( |-)]recipients");
}
@Override
public boolean init(Expression>[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) {
- if (!(getParser().isCurrentEvent(AsyncPlayerChatEvent.class))) {
- Skript.error("Cannot use chat recipients expression outside of a chat event", ErrorQuality.SEMANTIC_ERROR);
+ if (!(getParser().isCurrentEvent(AsyncPlayerChatEvent.class, BroadcastMessageEvent.class))) {
+ Skript.error("Cannot use chat recipients expression outside of a chat or a server broadcast event");
return false;
}
return true;
}
@Override
- public String toString(@Nullable Event event, boolean debug) {
- return "chat recipients";
+ @Nullable
+ protected CommandSender[] get(Event event) {
+ if (event instanceof AsyncPlayerChatEvent async) {
+ return async.getRecipients().toArray(new Player[0]);
+ } else if (event instanceof BroadcastMessageEvent broadcast) {
+ return broadcast.getRecipients().toArray(new CommandSender[0]);
+ }
+ return null;
}
@Override
- @Nullable
- protected Player[] get(Event event) {
- if (!(event instanceof AsyncPlayerChatEvent))
- return null;
-
- AsyncPlayerChatEvent ae = (AsyncPlayerChatEvent) event;
- Set playerSet = ae.getRecipients();
- return playerSet.toArray(new Player[playerSet.size()]);
+ public Class>[] acceptChange(ChangeMode mode) {
+ return CollectionUtils.array(CommandSender[].class);
}
@Override
public void change(Event event, @Nullable Object[] delta, ChangeMode mode) {
- if (!(event instanceof AsyncPlayerChatEvent))
+ if (!(event instanceof AsyncPlayerChatEvent) && !(event instanceof BroadcastMessageEvent))
return;
- final Player[] recipients = (Player[]) delta;
+ CommandSender[] recipients = (CommandSender[]) delta;
switch (mode) {
case REMOVE:
assert recipients != null;
- for (Player player : recipients)
- ((AsyncPlayerChatEvent) event).getRecipients().remove(player);
+ if (event instanceof AsyncPlayerChatEvent) {
+ for (CommandSender sender : recipients)
+ ((AsyncPlayerChatEvent) event).getRecipients().remove(sender);
+ } else {
+ for (CommandSender sender : recipients)
+ ((BroadcastMessageEvent) event).getRecipients().remove(sender);
+ }
break;
case ADD:
assert recipients != null;
- for (Player player : recipients)
- ((AsyncPlayerChatEvent) event).getRecipients().add(player);
+ if (event instanceof AsyncPlayerChatEvent) {
+ for (CommandSender sender : recipients) {
+ if (sender instanceof Player)
+ ((AsyncPlayerChatEvent) event).getRecipients().add((Player) sender);
+ }
+ } else {
+ for (CommandSender sender : recipients)
+ ((BroadcastMessageEvent) event).getRecipients().add(sender);
+ }
break;
case SET:
change(event, delta, ChangeMode.DELETE);
@@ -113,8 +110,28 @@ public void change(Event event, @Nullable Object[] delta, ChangeMode mode) {
case REMOVE_ALL:
case RESET:
case DELETE:
- ((AsyncPlayerChatEvent) event).getRecipients().clear();
+ if (event instanceof AsyncPlayerChatEvent) {
+ ((AsyncPlayerChatEvent) event).getRecipients().clear();
+ } else {
+ ((BroadcastMessageEvent) event).getRecipients().clear();
+ }
break;
}
}
+
+ @Override
+ public boolean isSingle() {
+ return false;
+ }
+
+ @Override
+ public Class getReturnType() {
+ return CommandSender.class;
+ }
+
+ @Override
+ public String toString(@Nullable Event event, boolean debug) {
+ return "chat recipients";
+ }
+
}
diff --git a/src/main/java/ch/njol/skript/expressions/ExprMessage.java b/src/main/java/ch/njol/skript/expressions/ExprMessage.java
index d9342ff1d20..bd9d4d7d441 100644
--- a/src/main/java/ch/njol/skript/expressions/ExprMessage.java
+++ b/src/main/java/ch/njol/skript/expressions/ExprMessage.java
@@ -25,6 +25,7 @@
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerKickEvent;
import org.bukkit.event.player.PlayerQuitEvent;
+import org.bukkit.event.server.BroadcastMessageEvent;
import org.eclipse.jdt.annotation.Nullable;
import ch.njol.skript.Skript;
@@ -42,9 +43,6 @@
import ch.njol.util.Kleenean;
import ch.njol.util.coll.CollectionUtils;
-/**
- * @author Peter Güttinger
- */
@SuppressWarnings("deprecation")
@Name("Message")
@Description("The (chat) message of a chat event, the join message of a join event, the quit message of a quit event, or the death message on a death event. This expression is mostly useful for being changed.")
@@ -66,12 +64,15 @@
"\telse:",
"\t\tset quit message to \"%player% left this awesome server!\"",
"",
+ "on server broadcast:",
+ "\tset the broadcast message to \"something else!\"",
+ "",
"on death:",
"\tset the death message to \"%player% died!\""})
-@Since("1.4.6 (chat message), 1.4.9 (join & quit messages), 2.0 (death message), 2.9.0 (clear message)")
+@Since("1.4.6 (chat message), 1.4.9 (join & quit messages), 2.0 (death message), 2.9.0 (clear message), INSERT VERSION (broadcast message)")
@Events({"chat", "join", "quit", "death"})
public class ExprMessage extends SimpleExpression {
-
+
@SuppressWarnings("unchecked")
private static enum MessageType {
CHAT("chat", "[chat( |-)]message", AsyncPlayerChatEvent.class) {
@@ -79,7 +80,7 @@ private static enum MessageType {
String get(Event event) {
return ((AsyncPlayerChatEvent) event).getMessage();
}
-
+
@Override
void set(Event event, String message) {
((AsyncPlayerChatEvent) event).setMessage(message);
@@ -88,131 +89,141 @@ void set(Event event, String message) {
JOIN("join", "(join|log[ ]in)( |-)message", PlayerJoinEvent.class) {
@Override
@Nullable
- String get(final Event e) {
- return ((PlayerJoinEvent) e).getJoinMessage();
+ String get(Event event) {
+ return ((PlayerJoinEvent) event).getJoinMessage();
}
-
+
@Override
- void set(final Event e, final String message) {
- ((PlayerJoinEvent) e).setJoinMessage(message);
+ void set(Event event, String message) {
+ ((PlayerJoinEvent) event).setJoinMessage(message);
}
},
QUIT("quit", "(quit|leave|log[ ]out|kick)( |-)message", PlayerQuitEvent.class, PlayerKickEvent.class) {
@Override
@Nullable
- String get(final Event e) {
- if (e instanceof PlayerKickEvent)
- return ((PlayerKickEvent) e).getLeaveMessage();
+ String get(Event event) {
+ if (event instanceof PlayerKickEvent)
+ return ((PlayerKickEvent) event).getLeaveMessage();
else
- return ((PlayerQuitEvent) e).getQuitMessage();
+ return ((PlayerQuitEvent) event).getQuitMessage();
}
@Override
- void set(final Event e, final String message) {
- if (e instanceof PlayerKickEvent)
- ((PlayerKickEvent) e).setLeaveMessage(message);
+ void set(Event event, String message) {
+ if (event instanceof PlayerKickEvent)
+ ((PlayerKickEvent) event).setLeaveMessage(message);
else
- ((PlayerQuitEvent) e).setQuitMessage(message);
+ ((PlayerQuitEvent) event).setQuitMessage(message);
}
},
DEATH("death", "death( |-)message", EntityDeathEvent.class) {
@Override
@Nullable
- String get(final Event e) {
- if (e instanceof PlayerDeathEvent)
- return ((PlayerDeathEvent) e).getDeathMessage();
+ String get(Event event) {
+ if (event instanceof PlayerDeathEvent)
+ return ((PlayerDeathEvent) event).getDeathMessage();
return null;
}
@Override
- void set(final Event e, final String message) {
- if (e instanceof PlayerDeathEvent)
- ((PlayerDeathEvent) e).setDeathMessage(message);
+ void set(Event event, String message) {
+ if (event instanceof PlayerDeathEvent)
+ ((PlayerDeathEvent) event).setDeathMessage(message);
+ }
+ },
+ BROADCAST("broadcast", "broadcast( |-)message", BroadcastMessageEvent.class) {
+ @Override
+ @Nullable
+ String get(Event event) {
+ return ((BroadcastMessageEvent) event).getMessage();
+ }
+
+ @Override
+ void set(Event event, String message) {
+ ((BroadcastMessageEvent) event).setMessage(message);
}
};
-
+
final String name;
private final String pattern;
final Class extends Event>[] events;
-
- MessageType(final String name, final String pattern, final Class extends Event>... events) {
+
+ MessageType(String name, String pattern, Class extends Event>... events) {
this.name = name;
this.pattern = "[the] " + pattern;
this.events = events;
}
-
+
static String[] patterns;
static {
patterns = new String[values().length];
for (int i = 0; i < patterns.length; i++)
patterns[i] = values()[i].pattern;
}
-
+
@Nullable
- abstract String get(Event e);
-
- abstract void set(Event e, String message);
-
+ abstract String get(Event event);
+
+ abstract void set(Event event, String message);
+
}
-
+
static {
Skript.registerExpression(ExprMessage.class, String.class, ExpressionType.SIMPLE, MessageType.patterns);
}
-
- @SuppressWarnings("null")
+
private MessageType type;
-
- @SuppressWarnings("null")
+
@Override
- public boolean init(final Expression>[] exprs, final int matchedPattern, final Kleenean isDelayed, final ParseResult parseResult) {
+ public boolean init(Expression>[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) {
type = MessageType.values()[matchedPattern];
if (!getParser().isCurrentEvent(type.events)) {
- Skript.error("The " + type.name + " message can only be used in a " + type.name + " event", ErrorQuality.SEMANTIC_ERROR);
+ Skript.error("The " + type.name + " message can only be used in a " + type.name + " event");
return false;
}
return true;
}
-
+
@Override
- protected String[] get(final Event e) {
- for (final Class extends Event> c : type.events) {
- if (c.isInstance(e))
- return new String[] {type.get(e)};
+ protected String[] get(Event event) {
+ for (Class extends Event> c : type.events) {
+ if (c.isInstance(event))
+ return new String[] {type.get(event)};
}
return new String[0];
}
-
+
@Override
@Nullable
- public Class>[] acceptChange(final ChangeMode mode) {
+ public Class>[] acceptChange(ChangeMode mode) {
if (mode == ChangeMode.SET || mode == ChangeMode.DELETE)
return CollectionUtils.array(String.class);
return null;
}
-
+
@Override
- public void change(final Event e, final @Nullable Object[] delta, final ChangeMode mode) {
- assert mode == ChangeMode.SET || mode == ChangeMode.DELETE;
- for (final Class extends Event> c : type.events) {
- if (c.isInstance(e)) {
- type.set(e, (mode == ChangeMode.DELETE) ? "" : delta[0].toString());
- }
+ public void change(Event event, @Nullable Object[] delta, ChangeMode mode) {
+ assert mode == ChangeMode.SET;
+ assert delta != null;
+ for (Class extends Event> c : type.events) {
+ if (c.isInstance(event))
+ type.set(event, (mode == ChangeMode.DELETE || delta[0] == null) ? "" : delta[0].toString());
}
}
-
+
@Override
public boolean isSingle() {
return true;
}
-
+
@Override
public Class extends String> getReturnType() {
return String.class;
}
-
+
@Override
- public String toString(final @Nullable Event e, final boolean debug) {
- return "the " + type.name + " message";
+ public String toString(@Nullable Event event, final boolean debug) {
+ return type.name + " message";
}
-
+
}