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

Add server broadcast event #5617

Open
wants to merge 21 commits into
base: dev/feature
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
804b7ab
Add server broadcast event
TheLimeGlass Apr 19, 2023
0506270
Update description
TheLimeGlass Apr 19, 2023
5d1258d
Update src/main/java/ch/njol/skript/expressions/ExprChatRecipients.java
TheLimeGlass Jul 30, 2023
85167f5
Merge branch 'master' into feature/broadcast-event
TheLimeGlass Jul 30, 2023
10b7621
Update src/main/java/ch/njol/skript/expressions/ExprChatRecipients.java
TheLimeGlass Jul 30, 2023
9140798
Address reviews
TheLimeGlass Jul 30, 2023
99c4a10
Update src/main/java/ch/njol/skript/events/SimpleEvents.java
TheLimeGlass Jul 30, 2023
087ad0d
Apply suggestions from code review
TheLimeGlass Jul 30, 2023
560f226
Update src/main/java/ch/njol/skript/events/SimpleEvents.java
TheLimeGlass Jul 30, 2023
b2edd26
Merge branch 'dev/feature' into feature/broadcast-event
TheLimeGlass Oct 17, 2023
bb5b5cf
Update ExprChatRecipients.java
TheLimeGlass Oct 28, 2023
9715640
Update src/main/java/ch/njol/skript/expressions/ExprMessage.java
TheLimeGlass Oct 28, 2023
fe4c87c
Merge branch 'dev/feature' into feature/broadcast-event
sovdeeth Dec 30, 2023
a6e8e61
Update src/main/java/ch/njol/skript/events/SimpleEvents.java
TheLimeGlass Jan 1, 2024
c036074
Merge branch 'dev/feature' into feature/broadcast-event
sovdeeth Jun 28, 2024
5664351
Update src/main/java/ch/njol/skript/expressions/ExprChatRecipients.java
TheLimeGlass Sep 7, 2024
283b2d5
Update src/main/java/ch/njol/skript/expressions/ExprChatRecipients.java
TheLimeGlass Sep 7, 2024
81141eb
Update src/main/java/ch/njol/skript/expressions/ExprChatRecipients.java
TheLimeGlass Sep 7, 2024
11fc732
Update src/main/java/ch/njol/skript/events/SimpleEvents.java
TheLimeGlass Sep 7, 2024
5515432
Merge branch 'dev/feature' into feature/broadcast-event
Moderocky Sep 8, 2024
6fb4b2b
Update src/main/java/ch/njol/skript/expressions/ExprMessage.java
Moderocky Sep 8, 2024
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
19 changes: 13 additions & 6 deletions src/main/java/ch/njol/skript/events/SimpleEvents.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,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;
Expand Down Expand Up @@ -120,9 +121,6 @@
import ch.njol.skript.SkriptEventHandler;
import ch.njol.skript.lang.util.SimpleEvent;

/**
* @author Peter Güttinger
*/
public class SimpleEvents {
static {
Skript.registerEvent("Can Build Check", SimpleEvent.class, BlockCanBuildEvent.class, "[block] can build check")
Expand Down Expand Up @@ -725,9 +723,18 @@ 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 {_gui}:",
TheLimeGlass marked this conversation as resolved.
Show resolved Hide resolved
"\t\tsend \"You can't drag your items here!\" to player",
"\t\tcancel event"
)
.since("INSERT VERSION");

Skript.registerEvent("Server Broadcast", SimpleEvent.class, BroadcastMessageEvent.class, "[server] broadcast")
TheLimeGlass marked this conversation as resolved.
Show resolved Hide resolved
.description("Called when the server broadcasts messages.")
TheLimeGlass marked this conversation as resolved.
Show resolved Hide resolved
.examples(
"on server broadcast",
TheLimeGlass marked this conversation as resolved.
Show resolved Hide resolved
"\tbroadcast-message contains \"example\"",
"\tcancel event"
)
.since("INSERT VERSION");

Expand Down
97 changes: 57 additions & 40 deletions src/main/java/ch/njol/skript/expressions/ExprChatRecipients.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<Player> {
@Since("2.2-Fixes-v7, 2.2-dev35 (clearing recipients), INSERT VERSION (broadcast event)")
public class ExprChatRecipients extends SimpleExpression<CommandSender> {

static {
Skript.registerExpression(ExprChatRecipients.class, Player.class, ExpressionType.SIMPLE, "[chat][( |-)]recipients");
}

@Override
public boolean isSingle() {
return false;
}

@Override
public Class<Player> 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");
TheLimeGlass marked this conversation as resolved.
Show resolved Hide resolved
}

@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 server broadcast event");
TheLimeGlass marked this conversation as resolved.
Show resolved Hide resolved
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) {
return ((AsyncPlayerChatEvent) event).getRecipients().toArray(new Player[0]);
} else if (event instanceof BroadcastMessageEvent) {
return ((BroadcastMessageEvent) event).getRecipients().toArray(new CommandSender[0]);
}
Copy link
Member

Choose a reason for hiding this comment

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

Could use instanceof patterns since we now support Java 17

TheLimeGlass marked this conversation as resolved.
Show resolved Hide resolved
return null;
}

@Override
@Nullable
protected Player[] get(Event event) {
if (!(event instanceof AsyncPlayerChatEvent))
return null;

AsyncPlayerChatEvent ae = (AsyncPlayerChatEvent) event;
Set<Player> 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))
TheLimeGlass marked this conversation as resolved.
Show resolved Hide resolved
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((Player) sender);
TheLimeGlass marked this conversation as resolved.
Show resolved Hide resolved
}
break;
case SET:
change(event, delta, ChangeMode.DELETE);
Expand All @@ -113,8 +110,28 @@ public void change(Event event, @Nullable Object[] delta, ChangeMode mode) {
case REMOVE_ALL:
Copy link
Member

Choose a reason for hiding this comment

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

I know that this is the current behavior, but why does REMOVE_ALL as RESET and DELETE? remove all {_x} from the chat recipients shouldn't just remove all the recipients. I don't believe it should be supported here at all anyway, as there can only be one of a recipient, not more.

Also shouldn't resetting the recipients add all players back in, rather than clearing them? Because that's what I interpret from the RESET change mode - to reset the value to how it originally is; in this case all the players

Copy link
Member

Choose a reason for hiding this comment

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

Sorry, why did you mark this as resolved?

Copy link
Member

Choose a reason for hiding this comment

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

Not resolved

case RESET:
case DELETE:
((AsyncPlayerChatEvent) event).getRecipients().clear();
if (event instanceof AsyncPlayerChatEvent) {
((AsyncPlayerChatEvent) event).getRecipients().clear();
} else {
((BroadcastMessageEvent) event).getRecipients().clear();
}
Comment on lines +113 to +117
Copy link
Member

Choose a reason for hiding this comment

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

Could use instanceof pattern

break;
}
}

@Override
public boolean isSingle() {
return false;
}

@Override
public Class<CommandSender> getReturnType() {
return CommandSender.class;
}

@Override
public String toString(@Nullable Event event, boolean debug) {
return "chat recipients";
TheLimeGlass marked this conversation as resolved.
Show resolved Hide resolved
}

}
Loading