diff --git a/README.md b/README.md index 7e78bd6..af24668 100644 --- a/README.md +++ b/README.md @@ -2,8 +2,9 @@ The [TrueOG Network](https://trueog.net) fork of [Duels](https://github.com/Realizedd/Duels) - a duel plugin for Spigot. Spigot Project Page -Includes the following proposed changes that have yet to be merged into Duels upstream: +Changes from Duels: +- Only start Duels from one world, configurable in config.yml by @NotAlexNoyle - Inventory loss bug fixes by @szumielxd - Essentials API NPE fix by @skbeh - Update deprecated pickup API by @NotAlexNoyle diff --git a/duels-plugin/src/main/java/me/realized/duels/command/commands/duel/DuelCommand.java b/duels-plugin/src/main/java/me/realized/duels/command/commands/duel/DuelCommand.java index 64c6bd0..4c62397 100644 --- a/duels-plugin/src/main/java/me/realized/duels/command/commands/duel/DuelCommand.java +++ b/duels-plugin/src/main/java/me/realized/duels/command/commands/duel/DuelCommand.java @@ -82,7 +82,7 @@ public class DuelCommand extends BaseCommand { return true; } - if (config.getBlacklistedWorlds().contains(player.getWorld().getName())) { + if (!config.isDuelingWorld(player) || config.getBlacklistedWorlds().contains(player.getWorld().getName())) { lang.sendMessage(sender, "ERROR.duel.in-blacklisted-world"); return true; } diff --git a/duels-plugin/src/main/java/me/realized/duels/config/Config.java b/duels-plugin/src/main/java/me/realized/duels/config/Config.java index 44e300d..b894b3f 100644 --- a/duels-plugin/src/main/java/me/realized/duels/config/Config.java +++ b/duels-plugin/src/main/java/me/realized/duels/config/Config.java @@ -122,6 +122,8 @@ public class Config extends AbstractConfiguration { @Getter private boolean cancelIfMoved; @Getter + private String enabledWorld; + @Getter private List blacklistedWorlds; @Getter private boolean teleportToLastLocation; @@ -317,6 +319,10 @@ public class Config extends AbstractConfiguration { preventTpToMatchPlayers = configuration.getBoolean("duel.prevent-teleport-to-match-players", true); forceAllowCombat = configuration.getBoolean("duel.force-allow-combat", true); cancelIfMoved = configuration.getBoolean("duel.cancel-if-moved", false); + final String configuredEnabledWorld = configuration.getString("duel.enabled-world", "world"); + enabledWorld = configuredEnabledWorld != null && !configuredEnabledWorld.trim().isEmpty() + ? configuredEnabledWorld.trim() + : "world"; blacklistedWorlds = configuration.getStringList("duel.blacklisted-worlds"); teleportToLastLocation = configuration.getBoolean("duel.teleport-to-last-location", false); teleportDelay = configuration.getInt("duel.teleport-delay", 5); @@ -414,6 +420,11 @@ public class Config extends AbstractConfiguration { return sounds.keySet(); } + public boolean isDuelingWorld(final Player player) { + return player != null && player.getWorld() != null + && enabledWorld.equalsIgnoreCase(player.getWorld().getName()); + } + public class MessageSound { @Getter diff --git a/duels-plugin/src/main/java/me/realized/duels/duel/DuelManager.java b/duels-plugin/src/main/java/me/realized/duels/duel/DuelManager.java index 6e82014..4d0fdf5 100644 --- a/duels-plugin/src/main/java/me/realized/duels/duel/DuelManager.java +++ b/duels-plugin/src/main/java/me/realized/duels/duel/DuelManager.java @@ -376,7 +376,7 @@ public class DuelManager implements Loadable { } private boolean isBlacklistedWorld(final Player player) { - return config.getBlacklistedWorlds().contains(player.getWorld().getName()); + return !config.isDuelingWorld(player) || config.getBlacklistedWorlds().contains(player.getWorld().getName()); } private boolean isTagged(final Player player) { @@ -712,4 +712,4 @@ public class DuelManager implements Loadable { lang.sendMessage(player, "DUEL.prevent.inventory-open"); } } -} \ No newline at end of file +} diff --git a/duels-plugin/src/main/java/me/realized/duels/player/PlayerInfoManager.java b/duels-plugin/src/main/java/me/realized/duels/player/PlayerInfoManager.java index 61e7958..9c876f4 100644 --- a/duels-plugin/src/main/java/me/realized/duels/player/PlayerInfoManager.java +++ b/duels-plugin/src/main/java/me/realized/duels/player/PlayerInfoManager.java @@ -97,11 +97,13 @@ public class PlayerInfoManager implements Loadable { } } - // If lobby is not found or invalid, use the default world's spawn location for lobby. - if (lobby == null || lobby.getWorld() == null) { - final World world = Bukkit.getWorlds().get(0); - this.lobby = world.getSpawnLocation(); - Log.warn(this, String.format(ERROR_LOBBY_DEFAULT, world.getName())); + // If lobby is not found or points at a disabled/unloaded world, use the + // configured dueling world's spawn location for lobby. + if (!isEnabledWorld(lobby)) { + this.lobby = getFallbackLocation(); + final World world = this.lobby != null ? this.lobby.getWorld() : null; + Log.warn(this, + String.format(ERROR_LOBBY_DEFAULT, world != null ? world.getName() : config.getEnabledWorld())); } } @@ -112,7 +114,7 @@ public class PlayerInfoManager implements Loadable { if (info != null) { player.spigot().respawn(); - teleport.tryTeleport(player, info.getLocation()); + teleport.tryTeleport(player, safeReturnLocation(info.getLocation())); PlayerUtil.reset(player); info.restore(player); } @@ -143,6 +145,10 @@ public class PlayerInfoManager implements Loadable { * @return true if setting lobby was successful, false otherwise */ public boolean setLobby(final Player player) { + if (!config.isDuelingWorld(player)) { + return false; + } + final Location lobby = player.getLocation().clone(); try (final Writer writer = new OutputStreamWriter(new FileOutputStream(lobbyFile), Charsets.UTF_8)) { @@ -176,7 +182,9 @@ public class PlayerInfoManager implements Loadable { final PlayerInfo info = new PlayerInfo(player, excludeInventory); if (!config.isTeleportToLastLocation()) { - info.setLocation(lobby.clone()); + info.setLocation(safeReturnLocation(lobby)); + } else if (!isEnabledWorld(info.getLocation())) { + info.setLocation(safeReturnLocation(info.getLocation())); } cache.put(player.getUniqueId(), info); @@ -201,6 +209,30 @@ public class PlayerInfoManager implements Loadable { return cache.remove(player.getUniqueId()); } + private boolean isEnabledWorld(final Location location) { + return location != null && location.getWorld() != null + && config.getEnabledWorld().equalsIgnoreCase(location.getWorld().getName()); + } + + private Location getFallbackLocation() { + World world = Bukkit.getWorld(config.getEnabledWorld()); + + if (world == null && !Bukkit.getWorlds().isEmpty()) { + world = Bukkit.getWorlds().get(0); + } + + return world != null ? world.getSpawnLocation() : null; + } + + private Location safeReturnLocation(final Location location) { + if (isEnabledWorld(location)) { + return location.clone(); + } + + final Location fallback = getFallbackLocation(); + return fallback != null ? fallback.clone() : null; + } + private class PlayerInfoListener implements Listener { // Handles case of some players causing respawn to skip somehow. @@ -218,7 +250,7 @@ public class PlayerInfoManager implements Loadable { return; } - teleport.tryTeleport(player, info.getLocation()); + teleport.tryTeleport(player, safeReturnLocation(info.getLocation())); info.restore(player); } @@ -231,7 +263,7 @@ public class PlayerInfoManager implements Loadable { return; } - event.setRespawnLocation(info.getLocation()); + event.setRespawnLocation(safeReturnLocation(info.getLocation())); if (essentials != null) { essentials.setBackLocation(player, event.getRespawnLocation()); diff --git a/duels-plugin/src/main/java/me/realized/duels/queue/QueueManager.java b/duels-plugin/src/main/java/me/realized/duels/queue/QueueManager.java index 37fd751..de52b94 100644 --- a/duels-plugin/src/main/java/me/realized/duels/queue/QueueManager.java +++ b/duels-plugin/src/main/java/me/realized/duels/queue/QueueManager.java @@ -361,6 +361,11 @@ public class QueueManager implements Loadable, DQueueManager, Listener { return false; } + if (!config.isDuelingWorld(player) || config.getBlacklistedWorlds().contains(player.getWorld().getName())) { + lang.sendMessage(player, "ERROR.duel.in-blacklisted-world"); + return false; + } + if ((combatTagPlus != null && combatTagPlus.isTagged(player)) || (pvpManager != null && pvpManager.isTagged(player))) { lang.sendMessage(player, "ERROR.duel.is-tagged"); return false; diff --git a/duels-plugin/src/main/resources/config.yml b/duels-plugin/src/main/resources/config.yml index 080f8af..279317d 100644 --- a/duels-plugin/src/main/resources/config.yml +++ b/duels-plugin/src/main/resources/config.yml @@ -231,6 +231,10 @@ duel: # default: false cancel-if-moved: false + # Only allow duels to be started from this world. + # default: world + enabled-world: 'world' + # List of worlds to disable dueling. blacklisted-worlds: - 'world_name_here' @@ -603,4 +607,4 @@ sounds: pitch: 1.9 volume: 1.0 trigger-messages: - - '&7Now in a match against &f%opponent%&7(&a%opponent_rating%&7) with kit &3%kit% &7in arena &e%arena%&7.' \ No newline at end of file + - '&7Now in a match against &f%opponent%&7(&a%opponent_rating%&7) with kit &3%kit% &7in arena &e%arena%&7.'