From d1528838bd76a732992e530503acfb3442a03494 Mon Sep 17 00:00:00 2001 From: Daniel Saukel Date: Fri, 10 Aug 2018 19:51:05 +0200 Subject: [PATCH] Fix maxInstances setting --- .../announcer/AnnouncerStartGameTask.java | 17 ++++++++++++++++- .../erethon/dungeonsxl/command/EditCommand.java | 7 ++++++- .../erethon/dungeonsxl/command/PlayCommand.java | 8 +++++++- .../erethon/dungeonsxl/command/TestCommand.java | 6 +++++- .../de/erethon/dungeonsxl/global/DPortal.java | 6 +++++- .../de/erethon/dungeonsxl/global/GameSign.java | 5 ----- .../de/erethon/dungeonsxl/player/DGroup.java | 2 +- .../dungeonsxl/world/DResourceWorld.java | 13 +++++++++++-- 8 files changed, 51 insertions(+), 13 deletions(-) diff --git a/src/main/java/de/erethon/dungeonsxl/announcer/AnnouncerStartGameTask.java b/src/main/java/de/erethon/dungeonsxl/announcer/AnnouncerStartGameTask.java index 52893f47..b89f2573 100644 --- a/src/main/java/de/erethon/dungeonsxl/announcer/AnnouncerStartGameTask.java +++ b/src/main/java/de/erethon/dungeonsxl/announcer/AnnouncerStartGameTask.java @@ -17,10 +17,13 @@ package de.erethon.dungeonsxl.announcer; import de.erethon.dungeonsxl.DungeonsXL; +import de.erethon.dungeonsxl.config.DMessage; import de.erethon.dungeonsxl.game.Game; import de.erethon.dungeonsxl.player.DGamePlayer; import de.erethon.dungeonsxl.player.DGroup; import de.erethon.dungeonsxl.util.ProgressBar; +import de.erethon.dungeonsxl.world.DGameWorld; +import de.erethon.dungeonsxl.world.DResourceWorld; import java.util.HashSet; import org.bukkit.entity.Player; import org.bukkit.scheduler.BukkitRunnable; @@ -71,7 +74,19 @@ public class AnnouncerStartGameTask extends BukkitRunnable { } if (game == null) { - game = new Game(dGroup, DungeonsXL.getInstance().getDWorlds().getResourceByName(announcer.getMapName())); + DResourceWorld resource = DungeonsXL.getInstance().getDWorlds().getResourceByName(announcer.getMapName()); + if (resource == null) { + dGroup.sendMessage(DMessage.ERROR_NO_SUCH_MAP.getMessage(announcer.getMapName())); + cancel(); + return; + } + DGameWorld gameWorld = resource.instantiateAsGameWorld(false); + if (gameWorld == null) { + dGroup.sendMessage(DMessage.ERROR_TOO_MANY_INSTANCES.getMessage()); + cancel(); + return; + } + game = new Game(dGroup, gameWorld); } else { game.getDGroups().add(dGroup); } diff --git a/src/main/java/de/erethon/dungeonsxl/command/EditCommand.java b/src/main/java/de/erethon/dungeonsxl/command/EditCommand.java index a707c004..53cd3c84 100644 --- a/src/main/java/de/erethon/dungeonsxl/command/EditCommand.java +++ b/src/main/java/de/erethon/dungeonsxl/command/EditCommand.java @@ -67,7 +67,12 @@ public class EditCommand extends DRECommand { return; } - DEditWorld editWorld = resource.instantiateAsEditWorld(); + DEditWorld editWorld = resource.instantiateAsEditWorld(false); + if (editWorld == null) { + MessageUtil.sendMessage(player, DMessage.ERROR_TOO_MANY_INSTANCES.getMessage()); + return; + } + DGroup dGroup = DGroup.getByPlayer(player); DGlobalPlayer dPlayer = DungeonsXL.getInstance().getDPlayers().getByPlayer(player); diff --git a/src/main/java/de/erethon/dungeonsxl/command/PlayCommand.java b/src/main/java/de/erethon/dungeonsxl/command/PlayCommand.java index 478c41f4..1a4b93cb 100644 --- a/src/main/java/de/erethon/dungeonsxl/command/PlayCommand.java +++ b/src/main/java/de/erethon/dungeonsxl/command/PlayCommand.java @@ -28,6 +28,7 @@ import de.erethon.dungeonsxl.player.DGlobalPlayer; import de.erethon.dungeonsxl.player.DGroup; import de.erethon.dungeonsxl.player.DInstancePlayer; import de.erethon.dungeonsxl.player.DPermission; +import de.erethon.dungeonsxl.world.DGameWorld; import de.erethon.dungeonsxl.world.DResourceWorld; import org.bukkit.Bukkit; import org.bukkit.command.CommandSender; @@ -89,7 +90,12 @@ public class PlayCommand extends DRECommand { } dGroup.setDungeon(dungeon); - new Game(dGroup, dungeon.getMap()); + DGameWorld gameWorld = dungeon.getMap().instantiateAsGameWorld(false); + if (gameWorld == null) { + MessageUtil.sendMessage(player, DMessage.ERROR_TOO_MANY_INSTANCES.getMessage()); + return; + } + new Game(dGroup, gameWorld); for (Player groupPlayer : dGroup.getPlayers().getOnlinePlayers()) { DGamePlayer.create(groupPlayer, dGroup.getGameWorld()); } diff --git a/src/main/java/de/erethon/dungeonsxl/command/TestCommand.java b/src/main/java/de/erethon/dungeonsxl/command/TestCommand.java index 1a0948c9..beec9bb0 100644 --- a/src/main/java/de/erethon/dungeonsxl/command/TestCommand.java +++ b/src/main/java/de/erethon/dungeonsxl/command/TestCommand.java @@ -86,7 +86,11 @@ public class TestCommand extends DRECommand { editPlayer.leave(); DResourceWorld resource = editPlayer.getEditWorld().getResource(); Dungeon dungeon = new Dungeon(resource); - DGameWorld instance = resource.instantiateAsGameWorld(); + DGameWorld instance = resource.instantiateAsGameWorld(false); + if (instance == null) { + MessageUtil.sendMessage(player, DMessage.ERROR_TOO_MANY_INSTANCES.getMessage()); + return; + } Game game = new Game(new DGroup(player, dungeon), GameTypeDefault.TEST, instance); DGamePlayer.create(player, game.getWorld(), GameTypeDefault.TEST); } diff --git a/src/main/java/de/erethon/dungeonsxl/global/DPortal.java b/src/main/java/de/erethon/dungeonsxl/global/DPortal.java index da95b1f5..3792f43d 100644 --- a/src/main/java/de/erethon/dungeonsxl/global/DPortal.java +++ b/src/main/java/de/erethon/dungeonsxl/global/DPortal.java @@ -218,7 +218,11 @@ public class DPortal extends GlobalProtection { if (target == null && dGroup.getDungeon() != null) { DResourceWorld resource = dGroup.getDungeon().getMap(); if (resource != null) { - target = resource.instantiateAsGameWorld(); + target = resource.instantiateAsGameWorld(false); + if (target == null) { + MessageUtil.sendMessage(player, DMessage.ERROR_TOO_MANY_INSTANCES.getMessage()); + return; + } dGroup.setGameWorld(target); } } diff --git a/src/main/java/de/erethon/dungeonsxl/global/GameSign.java b/src/main/java/de/erethon/dungeonsxl/global/GameSign.java index 6eddb002..a47cdfb4 100644 --- a/src/main/java/de/erethon/dungeonsxl/global/GameSign.java +++ b/src/main/java/de/erethon/dungeonsxl/global/GameSign.java @@ -125,11 +125,6 @@ public class GameSign extends JoinSign { } public void onPlayerInteract(Block block, Player player) { - if (DungeonsXL.getInstance().getDWorlds().getGameWorlds().size() >= DungeonsXL.getInstance().getMainConfig().getMaxInstances()) { - MessageUtil.sendMessage(player, DMessage.ERROR_TOO_MANY_INSTANCES.getMessage()); - return; - } - DGroup dGroup = DGroup.getByPlayer(player); if (dGroup == null) { MessageUtil.sendMessage(player, DMessage.ERROR_JOIN_GROUP.getMessage()); diff --git a/src/main/java/de/erethon/dungeonsxl/player/DGroup.java b/src/main/java/de/erethon/dungeonsxl/player/DGroup.java index 9c6407f1..acc28b60 100644 --- a/src/main/java/de/erethon/dungeonsxl/player/DGroup.java +++ b/src/main/java/de/erethon/dungeonsxl/player/DGroup.java @@ -704,7 +704,7 @@ public class DGroup { Game game = gameWorld.getGame(); removeUnplayedFloor(gameWorld.getResource(), false); - DGameWorld gameWorld = newFloor.instantiateAsGameWorld(); + DGameWorld gameWorld = newFloor.instantiateAsGameWorld(true); gameWorld.setType(type); this.gameWorld = gameWorld; game.setWorld(gameWorld); diff --git a/src/main/java/de/erethon/dungeonsxl/world/DResourceWorld.java b/src/main/java/de/erethon/dungeonsxl/world/DResourceWorld.java index 336d5dd3..db3f0aa0 100644 --- a/src/main/java/de/erethon/dungeonsxl/world/DResourceWorld.java +++ b/src/main/java/de/erethon/dungeonsxl/world/DResourceWorld.java @@ -262,9 +262,14 @@ public class DResourceWorld { } /** + * @param ignoreLimit if the global instance limit shall be ignored * @return an old or a new instance of this world. */ - public DEditWorld instantiateAsEditWorld() { + public DEditWorld instantiateAsEditWorld(boolean ignoreLimit) { + if (!ignoreLimit && plugin.getMainConfig().getMaxInstances() <= worlds.getInstances().size()) { + return null; + } + for (DEditWorld instance : worlds.getEditWorlds()) { if (instance.getName().equals(getName())) { return instance; @@ -276,9 +281,13 @@ public class DResourceWorld { } /** + * @param ignoreLimit if the global instance limit shall be ignored * @return a new instance of this world */ - public DGameWorld instantiateAsGameWorld() { + public DGameWorld instantiateAsGameWorld(boolean ignoreLimit) { + if (!ignoreLimit && plugin.getMainConfig().getMaxInstances() <= worlds.getInstances().size()) { + return null; + } return (DGameWorld) instantiate(true); }