Skip to content

Commit

Permalink
Make y position ignored by default for spawn notification distance ch…
Browse files Browse the repository at this point in the history
…ecks

Notifying for example players in caves below where a trader spawns seems like a good default. The old behavior is still accessible by appending 'box' to the distance value. The comments in the config have also been updated to be more specific.
  • Loading branch information
jpenilla committed Aug 17, 2023
1 parent ddb9533 commit aaec6b1
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ public void load() {
this.updateChecker = config.getBoolean(Fields.updateChecker);

final int oldTraderSpawnNotificationRadius = config.getInt(Fields.traderSpawnNotificationRadius);
final boolean sphereTraderNotificationRadius = config.getBoolean(Fields.traderSpawnSphereNotificationRadius);
final List<String> oldTraderSpawnNotificationCommands = config.getStringList(Fields.traderSpawnNotificationCommands);
if (oldTraderSpawnNotificationRadius != 0 || !oldTraderSpawnNotificationCommands.isEmpty()) {
config.set(Fields.traderSpawnNotificationRadius, null);
Expand All @@ -68,8 +67,7 @@ public void load() {
oldTraderSpawnNotificationRadius != -1,
TraderSpawnNotificationOptions.Players.parse(String.valueOf(
oldTraderSpawnNotificationRadius == -1 ? 500 : oldTraderSpawnNotificationRadius
), sphereTraderNotificationRadius),
sphereTraderNotificationRadius,
)),
List.of(),
oldTraderSpawnNotificationCommands
);
Expand Down Expand Up @@ -217,7 +215,6 @@ public static final class Fields {
public static final String refreshCommandTradersMinutes = "refreshCommandTradersMinutes";
public static final String updateChecker = "updateChecker";
public static final String traderSpawnNotificationRadius = "traderSpawnNotificationRadius";
public static final String traderSpawnSphereNotificationRadius = "traderSpawnSphereNotificationRadius";
public static final String traderSpawnNotificationCommands = "traderSpawnNotificationCommands";
public static final String traderSpawnNotifications = "traderSpawnNotifications";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,17 @@
public record TraderSpawnNotificationOptions(
boolean enabled,
Players notifyPlayers,
boolean sphereRadius,
List<String> commands,
List<String> perPlayerCommands
) {
private static final String ENABLED = "enabled";
private static final String NOTIFY_PLAYERS = "notifyPlayers";
private static final String SPHERE_RADIUS = "sphereRadius";
private static final String COMMANDS = "commands";
private static final String PER_PLAYER_COMMANDS = "perPlayerCommands";

void setTo(final DefaultedConfig config, final String path) {
config.set(path + "." + ENABLED, this.enabled);
config.set(path + "." + NOTIFY_PLAYERS, this.notifyPlayers.input());
config.set(path+ "." + SPHERE_RADIUS, this.sphereRadius);
config.set(path + "." + COMMANDS, this.commands);
config.set(path + "." + PER_PLAYER_COMMANDS, this.perPlayerCommands);
}
Expand All @@ -37,8 +34,7 @@ static TraderSpawnNotificationOptions createFrom(final @Nullable ConfigurationSe
Objects.requireNonNull(section, "section");
return new TraderSpawnNotificationOptions(
section.getBoolean(ENABLED),
Players.parse(section.getString(NOTIFY_PLAYERS), section.getBoolean(SPHERE_RADIUS)),
section.getBoolean(SPHERE_RADIUS),
Players.parse(section.getString(NOTIFY_PLAYERS)),
section.getStringList(COMMANDS),
section.getStringList(PER_PLAYER_COMMANDS)
);
Expand All @@ -63,16 +59,26 @@ public Collection<? extends Player> find(final WanderingTrader entity) {
};
}

static Players parse(final @Nullable String value, final boolean sphereRadius) {
static Players parse(final @Nullable String value) {
Objects.requireNonNull(value, "value");
if (value.equalsIgnoreCase("all")) {
return withInput(value, trader -> trader.getServer().getOnlinePlayers());
} else if (value.equalsIgnoreCase("world")) {
return withInput(value, trader -> trader.getWorld().getPlayers());
}
final boolean box = value.endsWith("box");
try {
final int radius = Integer.parseInt(value);
return withInput(value, trader -> trader.getLocation().getWorld().getNearbyEntities(trader.getLocation(), radius, sphereRadius ? radius : trader.getLocation().getWorld().getMaxHeight() - trader.getLocation().getWorld().getMinHeight(), radius, k -> k instanceof Player).stream().map(k -> (Player) k).toList());
final int radius = Integer.parseInt(box ? value.substring(0, value.length() - 3) : value);
return withInput(
value,
trader -> trader.getLocation().getWorld().getNearbyEntities(
trader.getLocation(),
radius,
box ? radius : trader.getLocation().getWorld().getMaxHeight() - trader.getLocation().getWorld().getMinHeight(),
radius,
k -> k instanceof Player
).stream().map(Player.class::cast).toList()
);
} catch (final NumberFormatException ex) {
throw new IllegalArgumentException("Invalid players option, got '" + value + "', expected 'all', 'world', or an integer number for radius.");
}
Expand Down
9 changes: 6 additions & 3 deletions src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,15 @@ traderSpawnNotifications:
# Toggle the notification feature
enabled: false

# Who to notify. Can be 'all' for all online players, 'world' for all players in the world
# the trader spawned in, or a number for a block radius around the spawned trader.
# Who to notify. Can be:
# - 'all': all online players
# - 'world': all players in the world the trader spawned in
# - a number (B), i.e. '500': notify players within B blocks of the spawned trader (on the X and Z (horizontal) axes)
# - a number (B) followed by 'box', i.e. '500box': notify players within B blocks of the spawned trader (on the X, Y, and Z (horizontal and vertical) axes)
#
# Requires 'wanderingtrades.trader-spawn-notifications' permission, which defaults to true
notifyPlayers: 500
sphereRadius: true

# Commands to run when a trader spawns
commands:
- "effect give {trader-uuid} glowing 30"
Expand Down

0 comments on commit aaec6b1

Please sign in to comment.