diff --git a/build.gradle b/build.gradle index 4dedcb22..e62e52ce 100644 --- a/build.gradle +++ b/build.gradle @@ -10,7 +10,7 @@ buildscript { allprojects { group 'me.realized' - version '3.0.3-SNAPSHOT' + version '3.0.4-SNAPSHOT' } subprojects { 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 cfb18a53..93f76b48 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,8 +82,10 @@ protected boolean executeFirst(final CommandSender sender, final String label, f return true; } - if (worldGuard != null && !worldGuard.inDuelZone(player)) { - lang.sendMessage(sender, "ERROR.duel.not-in-duelzone", "regions", config.getDuelzoneRegions()); + String duelzone = null; + + if (worldGuard != null && config.isDuelzoneEnabled() && (duelzone = worldGuard.findDuelZone(player)) == null) { + lang.sendMessage(sender, "ERROR.duel.not-in-duelzone", "regions", config.getDuelzones()); return true; } @@ -161,6 +163,8 @@ protected boolean executeFirst(final CommandSender sender, final String label, f } settings.setTarget(target); + settings.setDuelzone(player, duelzone); + settings.setBaseLoc(player); if (config.isUseOwnInventoryEnabled()) { settings.openGui(player); diff --git a/duels-plugin/src/main/java/me/realized/duels/command/commands/duel/subcommands/AcceptCommand.java b/duels-plugin/src/main/java/me/realized/duels/command/commands/duel/subcommands/AcceptCommand.java index 73df1b52..35b67aff 100644 --- a/duels-plugin/src/main/java/me/realized/duels/command/commands/duel/subcommands/AcceptCommand.java +++ b/duels-plugin/src/main/java/me/realized/duels/command/commands/duel/subcommands/AcceptCommand.java @@ -45,8 +45,10 @@ protected void execute(final CommandSender sender, final String label, final Str return; } - if (worldGuard != null && !worldGuard.inDuelZone(player)) { - lang.sendMessage(sender, "ERROR.duel.not-in-duelzone", "regions", config.getDuelzoneRegions()); + String duelzone = null; + + if (worldGuard != null && config.isDuelzoneEnabled() && (duelzone = worldGuard.findDuelZone(player)) == null) { + lang.sendMessage(sender, "ERROR.duel.not-in-duelzone", "regions", config.getDuelzones()); return; } @@ -95,7 +97,8 @@ protected void execute(final CommandSender sender, final String label, final Str "name", player.getName(), "kit", kit, "arena", arena, "bet_amount", bet, "item_betting", itemBetting); if (settings.isItemBetting()) { - settings.getLocations()[1] = player.getLocation().clone(); + settings.setDuelzone(player, duelzone); + settings.setBaseLoc(player); bettingManager.open(settings, target, player); } else { duelManager.startMatch(player, target, settings, null, false); 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 0bbf8fad..2c79fbb2 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 @@ -41,7 +41,7 @@ public class Config extends AbstractConfiguration { @Getter private boolean duelzoneEnabled; @Getter - private List duelzoneRegions; + private List duelzones; @Getter private boolean requiresClearedInventory; @@ -175,7 +175,7 @@ protected void loadValues(FileConfiguration configuration) throws Exception { fuNoPowerLoss = configuration.getBoolean("supported-plugins.FactionsUUID.no-power-loss-in-duel", true); fNoPowerLoss = configuration.getBoolean("supported-plugins.Factions.no-power-loss-in-duel", true); duelzoneEnabled = configuration.getBoolean("supported-plugins.WorldGuard.duelzone.enabled", false); - duelzoneRegions = configuration.getStringList("supported-plugins.WorldGuard.duelzone.regions"); + duelzones = configuration.getStringList("supported-plugins.WorldGuard.duelzone.regions"); requiresClearedInventory = configuration.getBoolean("request.requires-cleared-inventory", true); preventCreativeMode = configuration.getBoolean("request.prevent-creative-mode", false); 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 98ef8b7f..c9dfc4be 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 @@ -25,6 +25,7 @@ import me.realized.duels.hooks.EssentialsHook; import me.realized.duels.hooks.McMMOHook; import me.realized.duels.hooks.VaultHook; +import me.realized.duels.hooks.WorldGuardHook; import me.realized.duels.inventories.InventoryManager; import me.realized.duels.kit.Kit; import me.realized.duels.player.PlayerInfo; @@ -79,6 +80,7 @@ public class DuelManager implements Loadable { private VaultHook vault; private EssentialsHook essentials; private McMMOHook mcMMO; + private WorldGuardHook worldGuard; private int durationCheckTask; public DuelManager(final DuelsPlugin plugin) { @@ -99,6 +101,7 @@ public void handleLoad() { this.vault = plugin.getHookManager().getHook(VaultHook.class); this.essentials = plugin.getHookManager().getHook(EssentialsHook.class); this.mcMMO = plugin.getHookManager().getHook(McMMOHook.class); + this.worldGuard = plugin.getHookManager().getHook(WorldGuardHook.class); if (config.getMaxDuration() > 0) { this.durationCheckTask = plugin.doSyncRepeat(() -> { @@ -296,7 +299,13 @@ public void startMatch(final Player first, final Player second, final Settings s return; } - if (config.isCancelIfMoved() && (notInLoc(first, settings.getLocations()[0]) || notInLoc(second, settings.getLocations()[1]))) { + if (config.isCancelIfMoved() && (notInLoc(first, settings.getBaseLoc(first)) || notInLoc(second, settings.getBaseLoc(second)))) { + lang.sendMessage("DUEL.start-failure.player-moved", first, second); + refundItems(items, first, second); + return; + } + + if (config.isDuelzoneEnabled() && worldGuard != null && (notInDz(first, settings.getDuelzone(first)) || notInDz(second, settings.getDuelzone(second)))) { lang.sendMessage("DUEL.start-failure.player-moved", first, second); refundItems(items, first, second); return; @@ -358,6 +367,10 @@ private boolean notInLoc(final Player player, final Location location) { return !source.getWorld().equals(location.getWorld()) || source.getBlockX() != location.getBlockX() || source.getBlockY() != location.getBlockY() || source.getBlockZ() != location.getBlockZ(); } + private boolean notInDz(final Player player, final String duelzone) { + return duelzone != null && !duelzone.equals(worldGuard.findDuelZone(player)); + } + private int getRating(final Kit kit, final UserData user) { return user != null ? user.getRating(kit) : config.getDefaultRating(); } diff --git a/duels-plugin/src/main/java/me/realized/duels/gui/setting/buttons/RequestSendButton.java b/duels-plugin/src/main/java/me/realized/duels/gui/setting/buttons/RequestSendButton.java index 8ddfb928..9762fa18 100644 --- a/duels-plugin/src/main/java/me/realized/duels/gui/setting/buttons/RequestSendButton.java +++ b/duels-plugin/src/main/java/me/realized/duels/gui/setting/buttons/RequestSendButton.java @@ -41,7 +41,6 @@ public void onClick(final Player player) { } player.closeInventory(); - settings.getLocations()[0] = player.getLocation().clone(); if (!requestManager.send(player, target, settings)) { return; diff --git a/duels-plugin/src/main/java/me/realized/duels/hooks/WorldGuardHook.java b/duels-plugin/src/main/java/me/realized/duels/hooks/WorldGuardHook.java index 0ca20d32..d9604b51 100644 --- a/duels-plugin/src/main/java/me/realized/duels/hooks/WorldGuardHook.java +++ b/duels-plugin/src/main/java/me/realized/duels/hooks/WorldGuardHook.java @@ -1,7 +1,8 @@ package me.realized.duels.hooks; import com.sk89q.worldguard.bukkit.WorldGuardPlugin; -import java.util.List; +import com.sk89q.worldguard.protection.regions.ProtectedRegion; +import java.util.Collection; import me.realized.duels.DuelsPlugin; import me.realized.duels.config.Config; import me.realized.duels.util.hook.PluginHook; @@ -16,15 +17,24 @@ public WorldGuardHook(final DuelsPlugin plugin) { this.config = plugin.getConfiguration(); } - public boolean inDuelZone(Player player) { + public String findDuelZone(final Player player) { if (!config.isDuelzoneEnabled()) { - return true; + return null; } final WorldGuardPlugin plugin = (WorldGuardPlugin) getPlugin(); - final List allowedRegions = config.getDuelzoneRegions(); - return plugin.getRegionManager(player.getWorld()) - .getApplicableRegions(player.getLocation()).getRegions() - .stream().anyMatch(region -> allowedRegions.contains(region.getId())); + final Collection allowedRegions = config.getDuelzones(); + + if (allowedRegions.isEmpty()) { + return null; + } + + for (final ProtectedRegion region : plugin.getRegionManager(player.getWorld()).getApplicableRegions(player.getLocation())) { + if (allowedRegions.contains(region.getId())) { + return region.getId(); + } + } + + return null; } } 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 c6c6468c..a25a9601 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 @@ -100,12 +100,8 @@ public void handleLoad() throws Exception { } for (final Player opponent : queue) { - // opponent is already in a match - if (current.equals(opponent) || remove.contains(opponent)) { - continue; - } - - if (!canFight(sign.getKit(), userManager.get(current), userManager.get(opponent))) { + // opponent is already in a match or the rating difference is too high + if (current.equals(opponent) || remove.contains(opponent) || !canFight(sign.getKit(), userManager.get(current), userManager.get(opponent))) { continue; } @@ -120,6 +116,7 @@ public void handleLoad() throws Exception { lang.sendMessage(current, "SIGN.found-opponent", "name", opponent.getName(), "kit", kit, "bet_amount", sign.getBet()); lang.sendMessage(opponent, "SIGN.found-opponent", "name", current.getName(), "kit", kit, "bet_amount", sign.getBet()); duelManager.startMatch(current, opponent, setting, null, true); + break; } } diff --git a/duels-plugin/src/main/java/me/realized/duels/setting/Settings.java b/duels-plugin/src/main/java/me/realized/duels/setting/Settings.java index 9980a6f8..6e8db1e7 100644 --- a/duels-plugin/src/main/java/me/realized/duels/setting/Settings.java +++ b/duels-plugin/src/main/java/me/realized/duels/setting/Settings.java @@ -1,5 +1,7 @@ package me.realized.duels.setting; +import java.util.HashMap; +import java.util.Map; import java.util.UUID; import lombok.Getter; import lombok.Setter; @@ -14,6 +16,7 @@ public class Settings { private final DuelsPlugin plugin; private final SettingsGui gui; + @Getter private UUID target; @Getter @@ -29,7 +32,7 @@ public class Settings { @Setter private boolean itemBetting; @Getter - private Location[] locations = new Location[2]; + private Map locations = new HashMap<>(); public Settings(final DuelsPlugin plugin, final Player player) { this.plugin = plugin; @@ -66,6 +69,34 @@ public void openGui(final Player player) { gui.open(player); } + public void setBaseLoc(final Player player) { + locations.computeIfAbsent(player.getUniqueId(), result -> new LocationInfo()).location = player.getLocation().clone(); + } + + public Location getBaseLoc(final Player player) { + final LocationInfo info = locations.get(player.getUniqueId()); + + if (info == null) { + return null; + } + + return info.location; + } + + public void setDuelzone(final Player player, final String duelzone) { + locations.computeIfAbsent(player.getUniqueId(), result -> new LocationInfo()).duelzone = duelzone; + } + + public String getDuelzone(final Player player) { + final LocationInfo info = locations.get(player.getUniqueId()); + + if (info == null) { + return null; + } + + return info.duelzone; + } + // Don't copy the gui since it won't be required to start a match public Settings lightCopy() { final Settings copy = new Settings(plugin); @@ -77,4 +108,11 @@ public Settings lightCopy() { copy.locations = locations; return copy; } + + private class LocationInfo { + + private Location location; + private String duelzone; + + } } diff --git a/duels-plugin/src/main/resources/config.yml b/duels-plugin/src/main/resources/config.yml index 02fbbc62..bdf00a51 100644 --- a/duels-plugin/src/main/resources/config.yml +++ b/duels-plugin/src/main/resources/config.yml @@ -48,7 +48,7 @@ supported-plugins: no-power-loss-in-duel: true WorldGuard: duelzone: - # If set to 'true', players will be able to use duel related commands only in the regions listed below. + # If set to 'true', players will be able to use duel only in the regions listed below. # default: false enabled: false diff --git a/duels-plugin/src/main/resources/lang.yml b/duels-plugin/src/main/resources/lang.yml index ed518169..ac8733ff 100644 --- a/duels-plugin/src/main/resources/lang.yml +++ b/duels-plugin/src/main/resources/lang.yml @@ -1,5 +1,5 @@ # DO NOT EDIT THIS VALUE! -config-version: 1 +config-version: 2 # Define placeholders usable in any message below. # Example: Adding 'PREFIX: "[Cool]"' below and then putting '{PREFIX}' in a message will display '[Cool]' when the message is sent in game. @@ -244,6 +244,7 @@ DUEL: start-failure: no-kit-selected: '{FAIL_PREFIX} No kit was selected!' player-moved: '{FAIL_PREFIX} You or your opponent moved after sending/accepting the request!' + not-in-duelzone: '{FAIL_PREFIX} You or your opponent moved out of the duelzone sending/accepting the request!' no-arena-available: '{FAIL_PREFIX} No arena is currently available. Please try again later.' arena-in-use: '{FAIL_PREFIX} That arena is already in use. Please select a different arena.' not-enough-money: '{FAIL_PREFIX} You or your opponent does not have $%bet_amount%.'