From aaec6b142fd43b28a83727927a696b90b262c460 Mon Sep 17 00:00:00 2001 From: Jason Penilla <11360596+jpenilla@users.noreply.github.com> Date: Thu, 17 Aug 2023 10:30:11 -0700 Subject: [PATCH] Make y position ignored by default for spawn notification distance checks 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. --- .../wanderingtrades/config/Config.java | 5 +---- .../TraderSpawnNotificationOptions.java | 22 ++++++++++++------- src/main/resources/config.yml | 9 +++++--- 3 files changed, 21 insertions(+), 15 deletions(-) diff --git a/src/main/java/xyz/jpenilla/wanderingtrades/config/Config.java b/src/main/java/xyz/jpenilla/wanderingtrades/config/Config.java index fd40db9..80b84b7 100644 --- a/src/main/java/xyz/jpenilla/wanderingtrades/config/Config.java +++ b/src/main/java/xyz/jpenilla/wanderingtrades/config/Config.java @@ -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 oldTraderSpawnNotificationCommands = config.getStringList(Fields.traderSpawnNotificationCommands); if (oldTraderSpawnNotificationRadius != 0 || !oldTraderSpawnNotificationCommands.isEmpty()) { config.set(Fields.traderSpawnNotificationRadius, null); @@ -68,8 +67,7 @@ public void load() { oldTraderSpawnNotificationRadius != -1, TraderSpawnNotificationOptions.Players.parse(String.valueOf( oldTraderSpawnNotificationRadius == -1 ? 500 : oldTraderSpawnNotificationRadius - ), sphereTraderNotificationRadius), - sphereTraderNotificationRadius, + )), List.of(), oldTraderSpawnNotificationCommands ); @@ -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"; } diff --git a/src/main/java/xyz/jpenilla/wanderingtrades/config/TraderSpawnNotificationOptions.java b/src/main/java/xyz/jpenilla/wanderingtrades/config/TraderSpawnNotificationOptions.java index 5f1a9d7..8badd52 100644 --- a/src/main/java/xyz/jpenilla/wanderingtrades/config/TraderSpawnNotificationOptions.java +++ b/src/main/java/xyz/jpenilla/wanderingtrades/config/TraderSpawnNotificationOptions.java @@ -15,20 +15,17 @@ public record TraderSpawnNotificationOptions( boolean enabled, Players notifyPlayers, - boolean sphereRadius, List commands, List 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); } @@ -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) ); @@ -63,16 +59,26 @@ public Collection 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."); } diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index cbd4150..a2ad390 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -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"