diff --git a/src/main/java/de/erethon/dungeonsxl/global/GameSign.java b/src/main/java/de/erethon/dungeonsxl/global/GameSign.java index 57dc1570..62290641 100644 --- a/src/main/java/de/erethon/dungeonsxl/global/GameSign.java +++ b/src/main/java/de/erethon/dungeonsxl/global/GameSign.java @@ -45,8 +45,8 @@ public class GameSign extends JoinSign { private Game game; - public GameSign(DungeonsXL plugin, int id, Block startSign, String identifier, int maxGroupsPerGame) { - super(plugin, id, startSign, identifier, maxGroupsPerGame); + public GameSign(DungeonsXL plugin, int id, Block startSign, String identifier, int maxGroupsPerGame, int startIfElementsAtLeast) { + super(plugin, id, startSign, identifier, maxGroupsPerGame, startIfElementsAtLeast); } /** @@ -76,11 +76,17 @@ public void update() { Sign sign = (Sign) startSign.getState(); if (game == null || game.getDGroups().isEmpty()) { + loadedWorld = false; sign.setLine(0, DMessage.SIGN_GLOBAL_NEW_GAME.getMessage()); sign.update(); return; } + if (game.getDGroups().size() >= startIfElementsAtLeast && startIfElementsAtLeast != -1) { + loadedWorld = true; + game.getDGroups().forEach(g -> g.teleport()); + } + if (game.getDGroups().get(0).isPlaying()) { sign.setLine(0, DMessage.SIGN_GLOBAL_IS_PLAYING.getMessage()); @@ -122,6 +128,7 @@ public void save(FileConfiguration config) { config.set(preString + ".dungeon", dungeon.getName()); } config.set(preString + ".maxGroupsPerGame", maxElements); + config.set(preString + ".startIfElementsAtLeast", startIfElementsAtLeast); } public void onPlayerInteract(Block block, Player player) { @@ -209,12 +216,17 @@ public static GameSign tryToCreate(DungeonsXL plugin, SignChangeEvent event) { } String identifier = event.getLine(2); - int maxGroupsPerGame = NumberUtil.parseInt(event.getLine(3), 1); + String[] data = event.getLine(3).split(","); + int maxGroupsPerGame = NumberUtil.parseInt(data[0], 1); + int startIfElementsAtLeast = -1; + if (data.length > 1) { + startIfElementsAtLeast = NumberUtil.parseInt(data[1], -1); + } - return tryToCreate(plugin, event.getBlock(), identifier, maxGroupsPerGame); + return tryToCreate(plugin, event.getBlock(), identifier, maxGroupsPerGame, startIfElementsAtLeast); } - public static GameSign tryToCreate(DungeonsXL plugin, Block startSign, String identifier, int maxGroupsPerGame) { + public static GameSign tryToCreate(DungeonsXL plugin, Block startSign, String identifier, int maxGroupsPerGame, int startIfElementsAtLeast) { World world = startSign.getWorld(); BlockFace facing = ((Attachable) startSign.getState().getData()).getAttachedFace().getOppositeFace(); int x = startSign.getX(), y = startSign.getY(), z = startSign.getZ(); @@ -231,7 +243,7 @@ public static GameSign tryToCreate(DungeonsXL plugin, Block startSign, String id verticalSigns--; } - GameSign sign = new GameSign(plugin, plugin.getGlobalProtectionCache().generateId(GameSign.class, world), startSign, identifier, maxGroupsPerGame); + GameSign sign = new GameSign(plugin, plugin.getGlobalProtectionCache().generateId(GameSign.class, world), startSign, identifier, maxGroupsPerGame, startIfElementsAtLeast); LWCUtil.removeProtection(startSign); diff --git a/src/main/java/de/erethon/dungeonsxl/global/GlobalProtectionCache.java b/src/main/java/de/erethon/dungeonsxl/global/GlobalProtectionCache.java index 04a3266f..e8818c65 100644 --- a/src/main/java/de/erethon/dungeonsxl/global/GlobalProtectionCache.java +++ b/src/main/java/de/erethon/dungeonsxl/global/GlobalProtectionCache.java @@ -185,9 +185,10 @@ public void loadAll() { if (data.contains(preString)) { String mapName = data.getString(preString + ".dungeon"); int maxGroupsPerGame = data.getInt(preString + ".maxGroupsPerGame"); + int startIfElementsAtLeast = data.getInt(preString + ".startIfElementsAtLeast"); Block startSign = world.getBlockAt(data.getInt(preString + ".x"), data.getInt(preString + ".y"), data.getInt(preString + ".z")); - new GameSign(plugin, id, startSign, mapName, maxGroupsPerGame); + new GameSign(plugin, id, startSign, mapName, maxGroupsPerGame, startIfElementsAtLeast); } } while (data.contains(preString)); @@ -205,9 +206,10 @@ public void loadAll() { String mapName = data.getString(preString + ".dungeon"); String groupName = data.getString(preString + ".groupName"); int maxPlayersPerGroup = data.getInt(preString + ".maxPlayersPerGroup"); + int startIfElementsAtLeast = data.getInt(preString + ".startIfElementsAtLeast"); Block startSign = world.getBlockAt(data.getInt(preString + ".x"), data.getInt(preString + ".y"), data.getInt(preString + ".z")); - new GroupSign(plugin, id, startSign, mapName, maxPlayersPerGroup, groupName); + new GroupSign(plugin, id, startSign, mapName, maxPlayersPerGroup, startIfElementsAtLeast, groupName); } } while (data.contains(preString)); } diff --git a/src/main/java/de/erethon/dungeonsxl/global/GroupSign.java b/src/main/java/de/erethon/dungeonsxl/global/GroupSign.java index 5142fc49..5f69a457 100644 --- a/src/main/java/de/erethon/dungeonsxl/global/GroupSign.java +++ b/src/main/java/de/erethon/dungeonsxl/global/GroupSign.java @@ -45,8 +45,8 @@ public class GroupSign extends JoinSign { private String groupName; private DGroup group; - public GroupSign(DungeonsXL plugin, int id, Block startSign, String identifier, int maxPlayersPerGroup, String groupName) { - super(plugin, id, startSign, identifier, maxPlayersPerGroup); + public GroupSign(DungeonsXL plugin, int id, Block startSign, String identifier, int maxPlayersPerGroup, int startIfElementsAtLeast, String groupName) { + super(plugin, id, startSign, identifier, maxPlayersPerGroup, startIfElementsAtLeast); this.groupName = groupName; } @@ -77,11 +77,17 @@ public void update() { Sign sign = (Sign) startSign.getState(); if (group == null) { + loadedWorld = false; sign.setLine(0, DMessage.SIGN_GLOBAL_NEW_GROUP.getMessage()); sign.update(); return; } + if (group.getPlayers().size() >= startIfElementsAtLeast && startIfElementsAtLeast != -1 && !loadedWorld) { + loadedWorld = true; + group.teleport(); + } + if (group.isPlaying()) { sign.setLine(0, DMessage.SIGN_GLOBAL_IS_PLAYING.getMessage()); @@ -124,6 +130,7 @@ public void save(FileConfiguration config) { } config.set(preString + ".groupName", groupName); config.set(preString + ".maxPlayersPerGroup", maxElements); + config.set(preString + ".startIfElementsAtLeast", startIfElementsAtLeast); } public void onPlayerInteract(Block block, Player player) { @@ -193,17 +200,21 @@ public static GroupSign tryToCreate(DungeonsXL plugin, SignChangeEvent event) { String[] data = event.getLine(3).split(","); int maxPlayersPerGroup = 1; String groupName = null; + int startIfElementsAtLeast = -1; if (data.length >= 1) { maxPlayersPerGroup = NumberUtil.parseInt(data[0], 1); } - if (data.length == 2) { + if (data.length >= 2) { groupName = data[1]; } + if (data.length == 3) { + startIfElementsAtLeast = NumberUtil.parseInt(data[2], -1); + } - return tryToCreate(plugin, event.getBlock(), identifier, maxPlayersPerGroup, groupName); + return tryToCreate(plugin, event.getBlock(), identifier, maxPlayersPerGroup, startIfElementsAtLeast, groupName); } - public static GroupSign tryToCreate(DungeonsXL plugin, Block startSign, String identifier, int maxPlayersPerGroup, String groupName) { + public static GroupSign tryToCreate(DungeonsXL plugin, Block startSign, String identifier, int maxPlayersPerGroup, int startIfElementsAtLeast, String groupName) { World world = startSign.getWorld(); BlockFace facing = ((Attachable) startSign.getState().getData()).getAttachedFace().getOppositeFace(); int x = startSign.getX(), y = startSign.getY(), z = startSign.getZ(); @@ -220,7 +231,8 @@ public static GroupSign tryToCreate(DungeonsXL plugin, Block startSign, String i verticalSigns--; } - GroupSign sign = new GroupSign(plugin, plugin.getGlobalProtectionCache().generateId(GroupSign.class, world), startSign, identifier, maxPlayersPerGroup, groupName); + GroupSign sign = new GroupSign(plugin, plugin.getGlobalProtectionCache().generateId(GroupSign.class, world), startSign, identifier, maxPlayersPerGroup, + startIfElementsAtLeast, groupName); LWCUtil.removeProtection(startSign); diff --git a/src/main/java/de/erethon/dungeonsxl/global/JoinSign.java b/src/main/java/de/erethon/dungeonsxl/global/JoinSign.java index 1e8f3b8e..236bf3bc 100644 --- a/src/main/java/de/erethon/dungeonsxl/global/JoinSign.java +++ b/src/main/java/de/erethon/dungeonsxl/global/JoinSign.java @@ -33,11 +33,13 @@ public class JoinSign extends GlobalProtection { protected Dungeon dungeon; protected int maxElements; + protected int startIfElementsAtLeast = -1; protected Block startSign; protected int verticalSigns; protected Set blocks; + protected boolean loadedWorld; - protected JoinSign(DungeonsXL plugin, int id, Block startSign, String identifier, int maxElements) { + protected JoinSign(DungeonsXL plugin, int id, Block startSign, String identifier, int maxElements, int startIfElementsAtLeast) { super(plugin, startSign.getWorld(), id); this.startSign = startSign; @@ -52,6 +54,9 @@ protected JoinSign(DungeonsXL plugin, int id, Block startSign, String identifier verticalSigns = (int) Math.ceil((float) (1 + maxElements) / 4); this.maxElements = maxElements; + if (startIfElementsAtLeast > 0 && startIfElementsAtLeast <= maxElements) { + this.startIfElementsAtLeast = startIfElementsAtLeast; + } update(); } @@ -78,10 +83,33 @@ public int getMaxElements() { } /** - * @param maxElements the maximum element count per sign + * @param amount the maximum element count per sign */ - public void setMaxElements(int maxElements) { - this.maxElements = maxElements; + public void setMaxElements(int amount) { + maxElements = amount; + } + + /** + * Returns the minimum amount of elements required to start the dungeon countdown + * + * @return the minimum amount of elements required to start the dungeon countdown;
+ * -1 if the dungeon is not instantiated only through the sign. + */ + public int getStartIfElementsAtLeastAmount() { + return startIfElementsAtLeast; + } + + /** + * Sets the minimum amount of elements required to start the dungeon countdown + * + * @param amount the amount to set + */ + public void setStartIfElementsAtLeastAmount(int amount) { + if ((amount > 0 || amount == -1) && amount < maxElements) { + startIfElementsAtLeast = amount; + } else { + throw new IllegalArgumentException("startIfElementsAtLeastAmount is < 0 or < maxElements"); + } } @Override diff --git a/src/main/java/de/erethon/dungeonsxl/player/DGroup.java b/src/main/java/de/erethon/dungeonsxl/player/DGroup.java index 9068d7c5..bd09906e 100644 --- a/src/main/java/de/erethon/dungeonsxl/player/DGroup.java +++ b/src/main/java/de/erethon/dungeonsxl/player/DGroup.java @@ -44,6 +44,7 @@ import java.util.Set; import java.util.UUID; import org.bukkit.Bukkit; +import org.bukkit.OfflinePlayer; import org.bukkit.entity.Player; import org.bukkit.scheduler.BukkitTask; @@ -609,6 +610,65 @@ public boolean isFinished() { } /* Actions */ + public boolean teleport() { + if (dungeon == null || dungeon.getMap() == null) { + sendMessage(DMessage.ERROR_DUNGEON_NOT_EXIST.getMessage()); + return false; + } + + DGameWorld target = dungeon.getMap().instantiateAsGameWorld(false); + Game game = Game.getByDGroup(this); + + if (target == null && game != null) { + target = game.getWorld(); + } + + if (target == null) { + if (game != null) { + for (DGroup otherTeam : game.getDGroups()) { + if (otherTeam.getGameWorld() != null) { + target = otherTeam.getGameWorld(); + break; + } + } + } + } + + if (target == null && dungeon != null) { + DResourceWorld resource = dungeon.getMap(); + if (resource != null) { + target = resource.instantiateAsGameWorld(false); + if (target == null) { + sendMessage(DMessage.ERROR_TOO_MANY_INSTANCES.getMessage()); + return false; + } + gameWorld = target; + } + } + + if (target == null) { + sendMessage(DMessage.ERROR_DUNGEON_NOT_EXIST.getMessage()); + return false; + } + + if (game == null) { + game = new Game(plugin, this, target); + + } else { + game.setWorld(target); + gameWorld = target; + } + + for (OfflinePlayer offline : players.getOfflinePlayers()) { + if (!offline.isOnline()) { + players.remove(offline); + } + Player player = offline.getPlayer(); + new DGamePlayer(plugin, player, target); + } + return true; + } + /** * The group finishs the dungeon. */