Skip to content

Commit

Permalink
v3.0.4-SNAPSHOT - queue fix, duelzone changes, cancel-if-moved fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Realizedd committed Jul 6, 2018
1 parent 70f20c1 commit f457105
Show file tree
Hide file tree
Showing 11 changed files with 91 additions and 26 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ buildscript {

allprojects {
group 'me.realized'
version '3.0.3-SNAPSHOT'
version '3.0.4-SNAPSHOT'
}

subprojects {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public class Config extends AbstractConfiguration<DuelsPlugin> {
@Getter
private boolean duelzoneEnabled;
@Getter
private List<String> duelzoneRegions;
private List<String> duelzones;

@Getter
private boolean requiresClearedInventory;
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand All @@ -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(() -> {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<String> allowedRegions = config.getDuelzoneRegions();
return plugin.getRegionManager(player.getWorld())
.getApplicableRegions(player.getLocation()).getRegions()
.stream().anyMatch(region -> allowedRegions.contains(region.getId()));
final Collection<String> 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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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;
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -14,6 +16,7 @@ public class Settings {

private final DuelsPlugin plugin;
private final SettingsGui gui;

@Getter
private UUID target;
@Getter
Expand All @@ -29,7 +32,7 @@ public class Settings {
@Setter
private boolean itemBetting;
@Getter
private Location[] locations = new Location[2];
private Map<UUID, LocationInfo> locations = new HashMap<>();

public Settings(final DuelsPlugin plugin, final Player player) {
this.plugin = plugin;
Expand Down Expand Up @@ -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);
Expand All @@ -77,4 +108,11 @@ public Settings lightCopy() {
copy.locations = locations;
return copy;
}

private class LocationInfo {

private Location location;
private String duelzone;

}
}
2 changes: 1 addition & 1 deletion duels-plugin/src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
3 changes: 2 additions & 1 deletion duels-plugin/src/main/resources/lang.yml
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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%.'
Expand Down

0 comments on commit f457105

Please sign in to comment.