From 141a35f5e79359e419221ed7f87d97510f5f4b9e Mon Sep 17 00:00:00 2001 From: Daniel Saukel Date: Thu, 23 Jun 2016 18:22:47 +0200 Subject: [PATCH] #94: Resolved all errors due to changes --- .../github/dre2n/dungeonsxl/DungeonsXL.java | 35 +- .../dungeonsxl/command/CreateCommand.java | 11 +- .../dre2n/dungeonsxl/command/EditCommand.java | 30 +- .../dungeonsxl/command/EnterCommand.java | 2 +- .../dungeonsxl/command/EscapeCommand.java | 2 +- .../dungeonsxl/command/InviteCommand.java | 17 +- .../dre2n/dungeonsxl/command/ListCommand.java | 8 +- .../dre2n/dungeonsxl/command/MainCommand.java | 2 +- .../dre2n/dungeonsxl/command/MsgCommand.java | 11 +- .../dre2n/dungeonsxl/command/PlayCommand.java | 2 +- .../dungeonsxl/command/ReloadCommand.java | 2 +- .../dungeonsxl/command/UninviteCommand.java | 16 +- .../io/github/dre2n/dungeonsxl/game/Game.java | 6 +- .../dre2n/dungeonsxl/global/DPortal.java | 2 +- .../dre2n/dungeonsxl/global/GameSign.java | 2 +- .../dungeonsxl/listener/BlockListener.java | 2 +- .../dungeonsxl/listener/PlayerListener.java | 6 +- .../dre2n/dungeonsxl/player/DGamePlayer.java | 9 +- .../dre2n/dungeonsxl/player/DGroup.java | 3 +- .../dre2n/dungeonsxl/sign/ClassesSign.java | 2 +- .../dre2n/dungeonsxl/sign/ReadySign.java | 2 +- .../dre2n/dungeonsxl/task/LazyUpdateTask.java | 2 +- .../dungeonsxl/task/RedstoneEventTask.java | 2 +- .../dungeonsxl/task/WorldUnloadTask.java | 6 +- .../dre2n/dungeonsxl/world/EditWorld.java | 443 +++--------------- .../dre2n/dungeonsxl/world/GameWorld.java | 302 +++--------- .../dre2n/dungeonsxl/world/InstanceWorld.java | 64 ++- .../dre2n/dungeonsxl/world/ResourceWorld.java | 115 ++++- .../github/dre2n/dungeonsxl/world/Worlds.java | 116 +++++ .../dungeonsxl/listener/DGroupListener.java | 4 +- 30 files changed, 493 insertions(+), 733 deletions(-) diff --git a/src/main/java/io/github/dre2n/dungeonsxl/DungeonsXL.java b/src/main/java/io/github/dre2n/dungeonsxl/DungeonsXL.java index 81614108..181b0f69 100644 --- a/src/main/java/io/github/dre2n/dungeonsxl/DungeonsXL.java +++ b/src/main/java/io/github/dre2n/dungeonsxl/DungeonsXL.java @@ -23,7 +23,6 @@ import io.github.dre2n.commons.compatibility.Version; import io.github.dre2n.commons.config.MessageConfig; import io.github.dre2n.commons.javaplugin.BRPlugin; import io.github.dre2n.commons.javaplugin.BRPluginSettings; -import io.github.dre2n.commons.util.FileUtil; import io.github.dre2n.dungeonsxl.announcer.Announcers; import io.github.dre2n.dungeonsxl.command.*; import io.github.dre2n.dungeonsxl.config.DMessages; @@ -53,8 +52,6 @@ import io.github.dre2n.dungeonsxl.task.SecureModeTask; import io.github.dre2n.dungeonsxl.task.UpdateTask; import io.github.dre2n.dungeonsxl.task.WorldUnloadTask; import io.github.dre2n.dungeonsxl.trigger.TriggerTypes; -import io.github.dre2n.dungeonsxl.world.EditWorld; -import io.github.dre2n.dungeonsxl.world.GameWorld; import io.github.dre2n.dungeonsxl.world.Worlds; import io.github.dre2n.itemsxl.ItemsXL; import java.io.File; @@ -204,10 +201,7 @@ public class DungeonsXL extends BRPlugin { dGroups.clear(); // Delete Worlds - GameWorld.deleteAll(); - gameWorlds.clear(); - EditWorld.deleteAll(); - editWorlds.clear(); + worlds.deleteAllInstances(); // Disable listeners HandlerList.unregisterAll(this); @@ -272,38 +266,17 @@ public class DungeonsXL extends BRPlugin { public void saveData() { protections.saveAll(); DSavePlayer.save(); - for (EditWorld editWorld : editWorlds) { - editWorld.save(); - } + worlds.saveAll(); } public void loadAll() { protections.loadAll(); dPlayers.loadAll(); DSavePlayer.load(); - checkWorlds(); + worlds.check(); } - public void checkWorlds() { - File serverDir = new File("."); - - for (File file : serverDir.listFiles()) { - if (file.getName().contains("DXL_Edit_") && file.isDirectory()) { - for (File dungeonFile : file.listFiles()) { - if (dungeonFile.getName().contains(".id_")) { - String dungeonName = dungeonFile.getName().substring(4); - FileUtil.copyDirectory(file, new File(getDataFolder(), "/maps/" + dungeonName), EXCLUDED_FILES); - FileUtil.deleteUnusedFiles(new File(getDataFolder(), "/maps/" + dungeonName)); - } - } - - FileUtil.removeDirectory(file); - - } else if (file.getName().contains("DXL_Game_") && file.isDirectory()) { - FileUtil.removeDirectory(file); - } - } - } + /* Getters and loaders */ /** diff --git a/src/main/java/io/github/dre2n/dungeonsxl/command/CreateCommand.java b/src/main/java/io/github/dre2n/dungeonsxl/command/CreateCommand.java index 3a6cc1f5..8b47d850 100644 --- a/src/main/java/io/github/dre2n/dungeonsxl/command/CreateCommand.java +++ b/src/main/java/io/github/dre2n/dungeonsxl/command/CreateCommand.java @@ -24,6 +24,7 @@ import io.github.dre2n.dungeonsxl.player.DEditPlayer; import io.github.dre2n.dungeonsxl.player.DGamePlayer; import io.github.dre2n.dungeonsxl.player.DPermissions; import io.github.dre2n.dungeonsxl.world.EditWorld; +import io.github.dre2n.dungeonsxl.world.ResourceWorld; import java.io.File; import org.bukkit.command.CommandSender; import org.bukkit.command.ConsoleCommandSender; @@ -66,9 +67,8 @@ public class CreateCommand extends BRCommand { MessageUtil.log(plugin, DMessages.LOG_GENERATE_NEW_WORLD.getMessage()); // Create World - EditWorld editWorld = new EditWorld(); - editWorld.generate(); - editWorld.setMapName(name); + ResourceWorld resource = new ResourceWorld(name); + EditWorld editWorld = resource.generate(); editWorld.save(); editWorld.delete(); @@ -88,9 +88,8 @@ public class CreateCommand extends BRCommand { MessageUtil.log(plugin, DMessages.LOG_GENERATE_NEW_WORLD.getMessage()); // Create World - EditWorld editWorld = new EditWorld(); - editWorld.generate(); - editWorld.setMapName(name); + ResourceWorld resource = new ResourceWorld(name); + EditWorld editWorld = resource.generate(); // MSG Done MessageUtil.log(plugin, DMessages.LOG_WORLD_GENERATION_FINISHED.getMessage()); diff --git a/src/main/java/io/github/dre2n/dungeonsxl/command/EditCommand.java b/src/main/java/io/github/dre2n/dungeonsxl/command/EditCommand.java index 9550a356..9b5ea793 100644 --- a/src/main/java/io/github/dre2n/dungeonsxl/command/EditCommand.java +++ b/src/main/java/io/github/dre2n/dungeonsxl/command/EditCommand.java @@ -21,10 +21,13 @@ import io.github.dre2n.commons.util.messageutil.MessageUtil; import io.github.dre2n.dungeonsxl.DungeonsXL; import io.github.dre2n.dungeonsxl.config.DMessages; import io.github.dre2n.dungeonsxl.player.DEditPlayer; -import io.github.dre2n.dungeonsxl.player.DGamePlayer; +import io.github.dre2n.dungeonsxl.player.DGlobalPlayer; import io.github.dre2n.dungeonsxl.player.DGroup; +import io.github.dre2n.dungeonsxl.player.DInstancePlayer; import io.github.dre2n.dungeonsxl.player.DPermissions; import io.github.dre2n.dungeonsxl.world.EditWorld; +import io.github.dre2n.dungeonsxl.world.ResourceWorld; +import io.github.dre2n.dungeonsxl.world.Worlds; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; @@ -34,6 +37,7 @@ import org.bukkit.entity.Player; public class EditCommand extends BRCommand { DungeonsXL plugin = DungeonsXL.getInstance(); + Worlds worlds = plugin.getWorlds(); public EditCommand() { setCommand("edit"); @@ -46,18 +50,24 @@ public class EditCommand extends BRCommand { @Override public void onExecute(String[] args, CommandSender sender) { Player player = (Player) sender; - String mapName = args[1]; - EditWorld editWorld = EditWorld.load(mapName); - DGroup dGroup = DGroup.getByPlayer(player); - DGamePlayer dPlayer = DGamePlayer.getByPlayer(player); - if (!(EditWorld.isInvitedPlayer(mapName, player.getUniqueId(), player.getName()) || DPermissions.hasPermission(player, DPermissions.EDIT))) { + if (!worlds.exists(mapName)) { + MessageUtil.sendMessage(player, DMessages.ERROR_DUNGEON_NOT_EXIST.getMessage(mapName)); + return; + } + + ResourceWorld resource = worlds.getResourceByName(mapName); + EditWorld editWorld = resource.instantiateAsEditWorld(); + DGroup dGroup = DGroup.getByPlayer(player); + DGlobalPlayer dPlayer = plugin.getDPlayers().getByPlayer(player); + + if (!(resource.isInvitedPlayer(player) || DPermissions.hasPermission(player, DPermissions.EDIT))) { MessageUtil.sendMessage(player, DMessages.ERROR_NO_PERMISSIONS.getMessage()); return; } - if (dPlayer != null) { + if (dPlayer instanceof DInstancePlayer) { MessageUtil.sendMessage(player, DMessages.ERROR_LEAVE_DUNGEON.getMessage()); return; } @@ -67,13 +77,7 @@ public class EditCommand extends BRCommand { return; } - if (editWorld == null) { - MessageUtil.sendMessage(player, DMessages.ERROR_DUNGEON_NOT_EXIST.getMessage(mapName)); - return; - } - new DEditPlayer(player, editWorld.getWorld()); - } } diff --git a/src/main/java/io/github/dre2n/dungeonsxl/command/EnterCommand.java b/src/main/java/io/github/dre2n/dungeonsxl/command/EnterCommand.java index 9925fe09..8920bf95 100644 --- a/src/main/java/io/github/dre2n/dungeonsxl/command/EnterCommand.java +++ b/src/main/java/io/github/dre2n/dungeonsxl/command/EnterCommand.java @@ -68,7 +68,7 @@ public class EnterCommand extends BRCommand { } if (joining == null) { - joining = new DGroup(captain, game.getWorld().getMapName(), game.getDungeon() != null); + joining = new DGroup(captain, game.getWorld().getName(), game.getDungeon() != null); } if (joining.getCaptain() != captain && !DPermissions.hasPermission(sender, DPermissions.BYPASS)) { diff --git a/src/main/java/io/github/dre2n/dungeonsxl/command/EscapeCommand.java b/src/main/java/io/github/dre2n/dungeonsxl/command/EscapeCommand.java index 63a1d265..187061b6 100644 --- a/src/main/java/io/github/dre2n/dungeonsxl/command/EscapeCommand.java +++ b/src/main/java/io/github/dre2n/dungeonsxl/command/EscapeCommand.java @@ -58,7 +58,7 @@ public class EscapeCommand extends BRCommand { } if (editWorld.getWorld().getPlayers().isEmpty()) { - editWorld.deleteNoSave(); + editWorld.delete(false); } } else { diff --git a/src/main/java/io/github/dre2n/dungeonsxl/command/InviteCommand.java b/src/main/java/io/github/dre2n/dungeonsxl/command/InviteCommand.java index 8f91bceb..4293e49f 100644 --- a/src/main/java/io/github/dre2n/dungeonsxl/command/InviteCommand.java +++ b/src/main/java/io/github/dre2n/dungeonsxl/command/InviteCommand.java @@ -17,12 +17,13 @@ package io.github.dre2n.dungeonsxl.command; import io.github.dre2n.commons.command.BRCommand; -import io.github.dre2n.commons.util.UUIDUtil; import io.github.dre2n.commons.util.messageutil.MessageUtil; import io.github.dre2n.dungeonsxl.DungeonsXL; import io.github.dre2n.dungeonsxl.config.DMessages; import io.github.dre2n.dungeonsxl.player.DPermissions; -import io.github.dre2n.dungeonsxl.world.EditWorld; +import io.github.dre2n.dungeonsxl.world.ResourceWorld; +import org.bukkit.Bukkit; +import org.bukkit.OfflinePlayer; import org.bukkit.command.CommandSender; /** @@ -44,8 +45,16 @@ public class InviteCommand extends BRCommand { @Override public void onExecute(String[] args, CommandSender sender) { - if (EditWorld.addInvitedPlayer(args[2], UUIDUtil.getUniqueIdFromName(args[1]))) { - MessageUtil.sendMessage(sender, DMessages.CMD_INVITE_SUCCESS.getMessage(args[1], args[2])); + ResourceWorld resource = plugin.getWorlds().getResourceByName(args[2]); + OfflinePlayer player = Bukkit.getOfflinePlayer(args[2]); + + if (resource != null) { + if (player != null) { + MessageUtil.sendMessage(sender, DMessages.CMD_INVITE_SUCCESS.getMessage(args[1], args[2])); + + } else { + MessageUtil.sendMessage(sender, DMessages.ERROR_NO_SUCH_PLAYER.getMessage(args[2])); + } } else { MessageUtil.sendMessage(sender, DMessages.ERROR_DUNGEON_NOT_EXIST.getMessage(args[2])); diff --git a/src/main/java/io/github/dre2n/dungeonsxl/command/ListCommand.java b/src/main/java/io/github/dre2n/dungeonsxl/command/ListCommand.java index 81e7d338..90d52506 100644 --- a/src/main/java/io/github/dre2n/dungeonsxl/command/ListCommand.java +++ b/src/main/java/io/github/dre2n/dungeonsxl/command/ListCommand.java @@ -26,6 +26,7 @@ import io.github.dre2n.dungeonsxl.dungeon.Dungeon; import io.github.dre2n.dungeonsxl.player.DPermissions; import io.github.dre2n.dungeonsxl.world.EditWorld; import io.github.dre2n.dungeonsxl.world.GameWorld; +import io.github.dre2n.dungeonsxl.world.Worlds; import java.io.File; import java.util.ArrayList; import org.bukkit.command.CommandSender; @@ -37,6 +38,7 @@ import org.bukkit.entity.Player; public class ListCommand extends BRCommand { DungeonsXL plugin = DungeonsXL.getInstance(); + Worlds worlds = plugin.getWorlds(); public ListCommand() { setCommand("list"); @@ -61,10 +63,10 @@ public class ListCommand extends BRCommand { mapList.add(file.getName()); } ArrayList loadedList = new ArrayList<>(); - for (EditWorld editWorld : plugin.getEditWorlds()) { + for (EditWorld editWorld : worlds.getEditWorlds()) { loadedList.add(editWorld.getWorld().getWorldFolder().getName()); } - for (GameWorld gameWorld : plugin.getGameWorlds()) { + for (GameWorld gameWorld : worlds.getGameWorlds()) { loadedList.add(gameWorld.getWorld().getWorldFolder().getName()); } ArrayList toSend = new ArrayList<>(); @@ -130,7 +132,7 @@ public class ListCommand extends BRCommand { for (String map : toSend) { boolean invited = false; if (sender instanceof Player) { - invited = EditWorld.isInvitedPlayer(map, ((Player) sender).getUniqueId(), sender.getName()); + invited = worlds.getResourceByName(map).isInvitedPlayer((Player) sender); } MessageUtil.sendMessage(sender, "&b" + map + "&7 | &e" + invited); diff --git a/src/main/java/io/github/dre2n/dungeonsxl/command/MainCommand.java b/src/main/java/io/github/dre2n/dungeonsxl/command/MainCommand.java index 0affa731..a0f05bb3 100644 --- a/src/main/java/io/github/dre2n/dungeonsxl/command/MainCommand.java +++ b/src/main/java/io/github/dre2n/dungeonsxl/command/MainCommand.java @@ -50,7 +50,7 @@ public class MainCommand extends BRCommand { int maps = new File(plugin.getDataFolder() + "/maps").listFiles().length; int dungeons = new File(plugin.getDataFolder() + "/dungeons").listFiles().length; - int loaded = plugin.getEditWorlds().size() + plugin.getGameWorlds().size(); + int loaded = plugin.getWorlds().getEditWorlds().size() + plugin.getWorlds().getGameWorlds().size(); int players = plugin.getDPlayers().getDGamePlayers().size(); Internals internals = CompatibilityHandler.getInstance().getInternals(); String vault = ""; diff --git a/src/main/java/io/github/dre2n/dungeonsxl/command/MsgCommand.java b/src/main/java/io/github/dre2n/dungeonsxl/command/MsgCommand.java index 0980abdf..4bfc689c 100644 --- a/src/main/java/io/github/dre2n/dungeonsxl/command/MsgCommand.java +++ b/src/main/java/io/github/dre2n/dungeonsxl/command/MsgCommand.java @@ -24,7 +24,6 @@ import io.github.dre2n.dungeonsxl.config.DMessages; import io.github.dre2n.dungeonsxl.config.WorldConfig; import io.github.dre2n.dungeonsxl.player.DPermissions; import io.github.dre2n.dungeonsxl.world.EditWorld; -import java.io.File; import org.bukkit.ChatColor; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; @@ -63,10 +62,10 @@ public class MsgCommand extends BRCommand { try { int id = NumberUtil.parseInt(args[1]); - WorldConfig confreader = new WorldConfig(new File(plugin.getDataFolder() + "/maps/" + editWorld.getMapName(), "config.yml")); + WorldConfig config = editWorld.getResource().getConfig(); if (args.length == 2) { - String msg = confreader.getMsg(id, true); + String msg = config.getMsg(id, true); if (msg != null) { MessageUtil.sendMessage(player, ChatColor.WHITE + msg); @@ -89,7 +88,7 @@ public class MsgCommand extends BRCommand { if (splitMsg.length > 1) { msg = splitMsg[1]; - String old = confreader.getMsg(id, false); + String old = config.getMsg(id, false); if (old == null) { MessageUtil.sendMessage(player, DMessages.CMD_MSG_ADDED.getMessage(String.valueOf(id))); @@ -97,8 +96,8 @@ public class MsgCommand extends BRCommand { MessageUtil.sendMessage(player, DMessages.CMD_MSG_UPDATED.getMessage(String.valueOf(id))); } - confreader.setMsg(msg, id); - confreader.save(); + config.setMsg(msg, id); + config.save(); } else { MessageUtil.sendMessage(player, DMessages.ERROR_MSG_FORMAT.getMessage()); diff --git a/src/main/java/io/github/dre2n/dungeonsxl/command/PlayCommand.java b/src/main/java/io/github/dre2n/dungeonsxl/command/PlayCommand.java index 37978851..1a128290 100644 --- a/src/main/java/io/github/dre2n/dungeonsxl/command/PlayCommand.java +++ b/src/main/java/io/github/dre2n/dungeonsxl/command/PlayCommand.java @@ -84,7 +84,7 @@ public class PlayCommand extends BRCommand { } } - if (!multiFloor && !EditWorld.exists(identifier)) { + if (!multiFloor && !plugin.getWorlds().exists(identifier)) { MessageUtil.sendMessage(player, DMessages.ERROR_DUNGEON_NOT_EXIST.getMessage(identifier)); return; } diff --git a/src/main/java/io/github/dre2n/dungeonsxl/command/ReloadCommand.java b/src/main/java/io/github/dre2n/dungeonsxl/command/ReloadCommand.java index 0edeac21..d3d90501 100644 --- a/src/main/java/io/github/dre2n/dungeonsxl/command/ReloadCommand.java +++ b/src/main/java/io/github/dre2n/dungeonsxl/command/ReloadCommand.java @@ -51,7 +51,7 @@ public class ReloadCommand extends BRCommand { int maps = new File(plugin.getDataFolder() + "/maps").listFiles().length; int dungeons = new File(plugin.getDataFolder() + "/dungeons").listFiles().length; - int loaded = plugin.getEditWorlds().size() + plugin.getGameWorlds().size(); + int loaded = plugin.getWorlds().getEditWorlds().size() + plugin.getWorlds().getGameWorlds().size(); int players = plugin.getDPlayers().getDGamePlayers().size(); Internals internals = CompatibilityHandler.getInstance().getInternals(); String vault = ""; diff --git a/src/main/java/io/github/dre2n/dungeonsxl/command/UninviteCommand.java b/src/main/java/io/github/dre2n/dungeonsxl/command/UninviteCommand.java index 9730e64b..8e74a902 100644 --- a/src/main/java/io/github/dre2n/dungeonsxl/command/UninviteCommand.java +++ b/src/main/java/io/github/dre2n/dungeonsxl/command/UninviteCommand.java @@ -17,12 +17,13 @@ package io.github.dre2n.dungeonsxl.command; import io.github.dre2n.commons.command.BRCommand; -import io.github.dre2n.commons.util.UUIDUtil; import io.github.dre2n.commons.util.messageutil.MessageUtil; import io.github.dre2n.dungeonsxl.DungeonsXL; import io.github.dre2n.dungeonsxl.config.DMessages; import io.github.dre2n.dungeonsxl.player.DPermissions; -import io.github.dre2n.dungeonsxl.world.EditWorld; +import io.github.dre2n.dungeonsxl.world.ResourceWorld; +import org.bukkit.Bukkit; +import org.bukkit.OfflinePlayer; import org.bukkit.command.CommandSender; /** @@ -44,12 +45,15 @@ public class UninviteCommand extends BRCommand { @Override public void onExecute(String[] args, CommandSender sender) { - if (EditWorld.removeInvitedPlayer(args[2], UUIDUtil.getUniqueIdFromName(args[1]), args[1])) { - MessageUtil.sendMessage(sender, DMessages.CMD_UNINVITE_SUCCESS.getMessage(args[1], args[2])); - - } else { + ResourceWorld resource = plugin.getWorlds().getResourceByName(args[2]); + if (resource == null) { MessageUtil.sendMessage(sender, DMessages.ERROR_DUNGEON_NOT_EXIST.getMessage(args[2])); } + + OfflinePlayer player = Bukkit.getOfflinePlayer(args[1]); + if (resource.removeInvitedPlayer(player)) { + MessageUtil.sendMessage(sender, DMessages.CMD_UNINVITE_SUCCESS.getMessage(args[1], args[2])); + } } } diff --git a/src/main/java/io/github/dre2n/dungeonsxl/game/Game.java b/src/main/java/io/github/dre2n/dungeonsxl/game/Game.java index 07d1fef4..194375c8 100644 --- a/src/main/java/io/github/dre2n/dungeonsxl/game/Game.java +++ b/src/main/java/io/github/dre2n/dungeonsxl/game/Game.java @@ -28,6 +28,7 @@ import io.github.dre2n.dungeonsxl.sign.DSign; import io.github.dre2n.dungeonsxl.sign.MobSign; import io.github.dre2n.dungeonsxl.trigger.ProgressTrigger; import io.github.dre2n.dungeonsxl.world.GameWorld; +import io.github.dre2n.dungeonsxl.world.ResourceWorld; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; @@ -77,9 +78,10 @@ public class Game { dGroups.add(dGroup); started = false; - world = new GameWorld(); + // TO DO world = new GameWorld(); + ResourceWorld resource = plugin.getWorlds().getResourceByName(worldName); dGroup.setGameWorld(world); - world.load(worldName); + resource.instantiateAsGameWorld(); fetchRules(); } diff --git a/src/main/java/io/github/dre2n/dungeonsxl/global/DPortal.java b/src/main/java/io/github/dre2n/dungeonsxl/global/DPortal.java index 27e428fd..2ec333bd 100644 --- a/src/main/java/io/github/dre2n/dungeonsxl/global/DPortal.java +++ b/src/main/java/io/github/dre2n/dungeonsxl/global/DPortal.java @@ -186,7 +186,7 @@ public class DPortal extends GlobalProtection { } if (target == null && dGroup.getMapName() != null) { - target = new GameWorld(dGroup.getMapName()); + target = plugin.getWorlds().getResourceByName(dGroup.getMapName()).instantiateAsGameWorld();//TO DO dGroup.setGameWorld(target); } diff --git a/src/main/java/io/github/dre2n/dungeonsxl/global/GameSign.java b/src/main/java/io/github/dre2n/dungeonsxl/global/GameSign.java index 1d4cea42..28c3561b 100644 --- a/src/main/java/io/github/dre2n/dungeonsxl/global/GameSign.java +++ b/src/main/java/io/github/dre2n/dungeonsxl/global/GameSign.java @@ -469,7 +469,7 @@ public class GameSign extends GlobalProtection { return false; } - if (plugin.getGameWorlds().size() >= plugin.getMainConfig().getMaxInstances()) { + if (plugin.getWorlds().getGameWorlds().size() >= plugin.getMainConfig().getMaxInstances()) { MessageUtil.sendMessage(player, DMessages.ERROR_TOO_MANY_INSTANCES.getMessage()); return true; } diff --git a/src/main/java/io/github/dre2n/dungeonsxl/listener/BlockListener.java b/src/main/java/io/github/dre2n/dungeonsxl/listener/BlockListener.java index dea4fc76..1fe0ddb2 100644 --- a/src/main/java/io/github/dre2n/dungeonsxl/listener/BlockListener.java +++ b/src/main/java/io/github/dre2n/dungeonsxl/listener/BlockListener.java @@ -231,7 +231,7 @@ public class BlockListener implements Listener { } if (dsign.check()) { - editWorld.checkSign(block); + editWorld.registerSign(block); editWorld.getSigns().add(block); MessageUtil.sendMessage(player, plugin.getMessageConfig().getMessage(DMessages.PLAYER_SIGN_CREATED)); diff --git a/src/main/java/io/github/dre2n/dungeonsxl/listener/PlayerListener.java b/src/main/java/io/github/dre2n/dungeonsxl/listener/PlayerListener.java index 4ad92df4..993e2d79 100644 --- a/src/main/java/io/github/dre2n/dungeonsxl/listener/PlayerListener.java +++ b/src/main/java/io/github/dre2n/dungeonsxl/listener/PlayerListener.java @@ -285,7 +285,7 @@ public class PlayerListener implements Listener { } // Class Signs - for (Sign classSign : gameWorld.getSignClass()) { + for (Sign classSign : gameWorld.getClassesSigns()) { if (classSign != null) { if (classSign.getLocation().distance(clickedBlock.getLocation()) < 1) { if (event.getAction() == Action.LEFT_CLICK_BLOCK || event.getAction() == Action.RIGHT_CLICK_BLOCK) { @@ -538,7 +538,7 @@ public class PlayerListener implements Listener { } if (dGroup.getGameWorld() == null) { - dGroup.setGameWorld(new GameWorld(DGroup.getByPlayer(player).getMapName())); + dGroup.setGameWorld(plugin.getWorlds().getResourceByName(DGroup.getByPlayer(player).getMapName()).instantiateAsGameWorld());// TO DO dGroup.getGameWorld().setTutorial(true); } @@ -578,7 +578,7 @@ public class PlayerListener implements Listener { continue; } - if (plugin.getGameWorlds().size() >= config.getMaxInstances()) { + if (plugin.getWorlds().getGameWorlds().size() >= config.getMaxInstances()) { event.setResult(PlayerLoginEvent.Result.KICK_FULL); event.setKickMessage(DMessages.ERROR_TOO_MANY_TUTORIALS.getMessage()); } diff --git a/src/main/java/io/github/dre2n/dungeonsxl/player/DGamePlayer.java b/src/main/java/io/github/dre2n/dungeonsxl/player/DGamePlayer.java index 46e12d60..5038cced 100644 --- a/src/main/java/io/github/dre2n/dungeonsxl/player/DGamePlayer.java +++ b/src/main/java/io/github/dre2n/dungeonsxl/player/DGamePlayer.java @@ -39,6 +39,7 @@ import io.github.dre2n.dungeonsxl.reward.DLootInventory; import io.github.dre2n.dungeonsxl.reward.Reward; import io.github.dre2n.dungeonsxl.trigger.DistanceTrigger; import io.github.dre2n.dungeonsxl.world.GameWorld; +import io.github.dre2n.dungeonsxl.world.ResourceWorld; import java.io.File; import java.util.concurrent.CopyOnWriteArrayList; import org.bukkit.ChatColor; @@ -698,8 +699,14 @@ public class DGamePlayer extends DInstancePlayer { dGroup.removeUnplayedFloor(dGroup.getMapName()); dGroup.setMapName(newFloor); - GameWorld gameWorld = new GameWorld(newFloor); + + ResourceWorld resource = plugin.getWorlds().getResourceByName(newFloor); + GameWorld gameWorld = null; + if (resource != null) { + gameWorld = resource.instantiateAsGameWorld(); + } dGroup.setGameWorld(gameWorld); + for (Player player : dGroup.getPlayers()) { DGamePlayer dPlayer = getByPlayer(player); dPlayer.setWorld(gameWorld.getWorld()); diff --git a/src/main/java/io/github/dre2n/dungeonsxl/player/DGroup.java b/src/main/java/io/github/dre2n/dungeonsxl/player/DGroup.java index 94c17a3a..004c516c 100644 --- a/src/main/java/io/github/dre2n/dungeonsxl/player/DGroup.java +++ b/src/main/java/io/github/dre2n/dungeonsxl/player/DGroup.java @@ -34,7 +34,6 @@ import io.github.dre2n.dungeonsxl.requirement.Requirement; import io.github.dre2n.dungeonsxl.reward.Reward; import io.github.dre2n.dungeonsxl.task.TimeIsRunningTask; import io.github.dre2n.dungeonsxl.world.GameWorld; -import io.github.dre2n.dungeonsxl.world.Worlds; import java.util.ArrayList; import java.util.Arrays; import java.util.HashSet; @@ -422,7 +421,7 @@ public class DGroup { * the name to set */ public void setMapName(String name) { - if (Worlds.exists(name)) { + if (plugin.getWorlds().exists(name)) { mapName = name; } } diff --git a/src/main/java/io/github/dre2n/dungeonsxl/sign/ClassesSign.java b/src/main/java/io/github/dre2n/dungeonsxl/sign/ClassesSign.java index b8f886d8..11601c1f 100644 --- a/src/main/java/io/github/dre2n/dungeonsxl/sign/ClassesSign.java +++ b/src/main/java/io/github/dre2n/dungeonsxl/sign/ClassesSign.java @@ -65,7 +65,7 @@ public class ClassesSign extends DSign { getSign().setLine(3, ChatColor.DARK_BLUE + "############"); getSign().update(); - getGameWorld().getSignClass().add(getSign()); + getGameWorld().getClassesSigns().add(getSign()); } @Override diff --git a/src/main/java/io/github/dre2n/dungeonsxl/sign/ReadySign.java b/src/main/java/io/github/dre2n/dungeonsxl/sign/ReadySign.java index fc146470..8b484a10 100644 --- a/src/main/java/io/github/dre2n/dungeonsxl/sign/ReadySign.java +++ b/src/main/java/io/github/dre2n/dungeonsxl/sign/ReadySign.java @@ -155,7 +155,7 @@ public class ReadySign extends DSign { return; } - if (getGameWorld().getSignClass().isEmpty() || dPlayer.getDClass() != null) { + if (getGameWorld().getClassesSigns().isEmpty() || dPlayer.getDClass() != null) { GameType forced = getGameWorld().getConfig().getForcedGameType(); dPlayer.ready(forced == null ? gameType : forced); } diff --git a/src/main/java/io/github/dre2n/dungeonsxl/task/LazyUpdateTask.java b/src/main/java/io/github/dre2n/dungeonsxl/task/LazyUpdateTask.java index a4941578..ad2b2e6d 100644 --- a/src/main/java/io/github/dre2n/dungeonsxl/task/LazyUpdateTask.java +++ b/src/main/java/io/github/dre2n/dungeonsxl/task/LazyUpdateTask.java @@ -30,7 +30,7 @@ public class LazyUpdateTask extends BukkitRunnable { @Override public void run() { - for (GameWorld gameWorld : plugin.getGameWorlds()) { + for (GameWorld gameWorld : plugin.getWorlds().getGameWorlds()) { gameWorld.update(); } diff --git a/src/main/java/io/github/dre2n/dungeonsxl/task/RedstoneEventTask.java b/src/main/java/io/github/dre2n/dungeonsxl/task/RedstoneEventTask.java index 4e138e48..2b0a2904 100644 --- a/src/main/java/io/github/dre2n/dungeonsxl/task/RedstoneEventTask.java +++ b/src/main/java/io/github/dre2n/dungeonsxl/task/RedstoneEventTask.java @@ -35,7 +35,7 @@ public class RedstoneEventTask extends BukkitRunnable { @Override public void run() { - for (GameWorld gameWorld : DungeonsXL.getInstance().getGameWorlds()) { + for (GameWorld gameWorld : DungeonsXL.getInstance().getWorlds().getGameWorlds()) { if (block.getWorld() == gameWorld.getWorld()) { RedstoneTrigger.updateAll(gameWorld); } diff --git a/src/main/java/io/github/dre2n/dungeonsxl/task/WorldUnloadTask.java b/src/main/java/io/github/dre2n/dungeonsxl/task/WorldUnloadTask.java index 84f04b3b..b3f8d0cb 100644 --- a/src/main/java/io/github/dre2n/dungeonsxl/task/WorldUnloadTask.java +++ b/src/main/java/io/github/dre2n/dungeonsxl/task/WorldUnloadTask.java @@ -31,7 +31,7 @@ public class WorldUnloadTask extends BukkitRunnable { @Override public void run() { - for (GameWorld gameWorld : plugin.getGameWorlds()) { + for (GameWorld gameWorld : plugin.getWorlds().getGameWorlds()) { if (gameWorld.getWorld().getPlayers().isEmpty()) { if (DGamePlayer.getByWorld(gameWorld.getWorld()).isEmpty()) { gameWorld.delete(); @@ -39,9 +39,9 @@ public class WorldUnloadTask extends BukkitRunnable { } } - for (EditWorld editWorld : plugin.getEditWorlds()) { + for (EditWorld editWorld : plugin.getWorlds().getEditWorlds()) { if (editWorld.getWorld().getPlayers().isEmpty()) { - editWorld.delete(); + editWorld.delete(true); } } } diff --git a/src/main/java/io/github/dre2n/dungeonsxl/world/EditWorld.java b/src/main/java/io/github/dre2n/dungeonsxl/world/EditWorld.java index 66a10a31..76fff176 100644 --- a/src/main/java/io/github/dre2n/dungeonsxl/world/EditWorld.java +++ b/src/main/java/io/github/dre2n/dungeonsxl/world/EditWorld.java @@ -17,27 +17,14 @@ package io.github.dre2n.dungeonsxl.world; import io.github.dre2n.commons.util.FileUtil; -import io.github.dre2n.commons.util.messageutil.MessageUtil; import io.github.dre2n.dungeonsxl.DungeonsXL; -import io.github.dre2n.dungeonsxl.config.WorldConfig; -import io.github.dre2n.dungeonsxl.event.editworld.EditWorldGenerateEvent; -import io.github.dre2n.dungeonsxl.event.editworld.EditWorldLoadEvent; import io.github.dre2n.dungeonsxl.event.editworld.EditWorldSaveEvent; import io.github.dre2n.dungeonsxl.event.editworld.EditWorldUnloadEvent; -import io.github.dre2n.dungeonsxl.player.DGamePlayer; +import io.github.dre2n.dungeonsxl.player.DEditPlayer; import java.io.File; -import java.io.FileInputStream; -import java.io.FileNotFoundException; -import java.io.FileOutputStream; import java.io.IOException; -import java.io.ObjectInputStream; -import java.io.ObjectOutputStream; -import java.util.UUID; import java.util.concurrent.CopyOnWriteArrayList; -import org.bukkit.Location; import org.bukkit.World; -import org.bukkit.WorldCreator; -import org.bukkit.WorldType; import org.bukkit.block.Block; import org.bukkit.block.Sign; import org.bukkit.entity.Player; @@ -45,148 +32,17 @@ import org.bukkit.entity.Player; /** * @author Frank Baumann, Daniel Saukel */ -public class EditWorld { +public class EditWorld extends InstanceWorld { - static DungeonsXL plugin = DungeonsXL.getInstance(); + static Worlds worlds = plugin.getWorlds(); - // Variables - private World world; - private String owner; - private String name; - private String mapName; - private int id; - private Location lobby; - private CopyOnWriteArrayList invitedPlayers = new CopyOnWriteArrayList<>(); private CopyOnWriteArrayList signs = new CopyOnWriteArrayList<>(); - public EditWorld() { - plugin.getEditWorlds().add(this); - - // ID - id = -1; - int i = -1; - while (id == -1) { - i++; - boolean exist = false; - for (EditWorld editWorld : plugin.getEditWorlds()) { - if (editWorld.id == i) { - exist = true; - break; - } - } - if (!exist) { - id = i; - } - } - - name = "DXL_Edit_" + id; - } - - /** - * @return the world - */ - public World getWorld() { - return world; - } - - /** - * @param world - * the world to set - */ - public void setWorld(World world) { - this.world = world; - } - - /** - * @return the owner - */ - public String getOwner() { - return owner; - } - - /** - * @param owner - * the owner to set - */ - public void setOwner(String owner) { - this.owner = owner; - } - - /** - * @return the name - */ - public String getName() { - return name; - } - - /** - * @param name - * the name to set - */ - public void setName(String name) { - this.name = name; - } - - /** - * @return the mapName - */ - public String getMapName() { - return mapName; - } - - /** - * @param mapName - * the mapName to set - */ - public void setMapName(String mapName) { - this.mapName = mapName; - } - - /** - * @return the id - */ - public int getId() { - return id; - } - - /** - * @param id - * the id to set - */ - public void setId(int id) { - this.id = id; - } - - /** - * @return the location of the lobby - */ - public Location getLobbyLocation() { - return lobby; - } - - /** - * @param lobby - * the lobby to set - */ - public void setLobby(Location lobby) { - this.lobby = lobby; - } - - /** - * @return the invitedPlayers - */ - public CopyOnWriteArrayList getInvitedPlayers() { - return invitedPlayers; - } - - /** - * @param invitedPlayers - * the invitedPlayers to set - */ - public void setInvitedPlayers(CopyOnWriteArrayList invitedPlayers) { - this.invitedPlayers = invitedPlayers; + EditWorld(ResourceWorld resourceWorld, File folder, World world, int id) { + super(resourceWorld, folder, world, id); } + /* Getters and setters */ /** * @return the signs */ @@ -202,21 +58,27 @@ public class EditWorld { this.signs = signs; } - public void generate() { - WorldCreator creator = WorldCreator.name(name); - creator.type(WorldType.FLAT); - creator.generateStructures(false); + /* Actions */ + /** + * Registers the block as a DSign sothat it can later be saved persistently. + * + * @param block + * a DSign block + */ + public void registerSign(Block block) { + if (block.getState() instanceof Sign) { + Sign sign = (Sign) block.getState(); + String[] lines = sign.getLines(); - EditWorldGenerateEvent event = new EditWorldGenerateEvent(this); - plugin.getServer().getPluginManager().callEvent(event); - - if (event.isCancelled()) { - return; + if (lines[0].equalsIgnoreCase("[lobby]")) { + setLobbyLocation(block.getLocation()); + } } - - world = plugin.getServer().createWorld(creator); } + /** + * Saves the sign data and overrides the resource with the changes. + */ public void save() { EditWorldSaveEvent event = new EditWorldSaveEvent(this); plugin.getServer().getPluginManager().callEvent(event); @@ -225,38 +87,29 @@ public class EditWorld { return; } - world.save(); + getWorld().save(); - File dir = new File("DXL_Edit_" + id); - FileUtil.copyDirectory(dir, new File(plugin.getDataFolder(), "/maps/" + mapName), DungeonsXL.EXCLUDED_FILES); - FileUtil.deleteUnusedFiles(new File(plugin.getDataFolder(), "/maps/" + mapName)); + FileUtil.copyDirectory(getFolder(), getResource().getFolder(), DungeonsXL.EXCLUDED_FILES); + FileUtil.deleteUnusedFiles(getResource().getFolder()); try { - ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(new File(plugin.getDataFolder(), "/maps/" + mapName + "/DXLData.data"))); - out.writeInt(signs.size()); - for (Block sign : signs) { - out.writeInt(sign.getX()); - out.writeInt(sign.getY()); - out.writeInt(sign.getZ()); - } - out.close(); - + getResource().getSignData().serializeSigns(signs); } catch (IOException exception) { } } - public void checkSign(Block block) { - if (block.getState() instanceof Sign) { - Sign sign = (Sign) block.getState(); - String[] lines = sign.getLines(); - - if (lines[0].equalsIgnoreCase("[lobby]")) { - lobby = block.getLocation(); - } - } + @Override + public void delete() { + delete(true); } - public void delete() { + /** + * Deletes this edit instance. + * + * @param save + * whether this world should be saved + */ + public void delete(boolean save) { EditWorldUnloadEvent event = new EditWorldUnloadEvent(this, true); plugin.getServer().getPluginManager().callEvent(event); @@ -264,217 +117,51 @@ public class EditWorld { return; } - plugin.getEditWorlds().remove(this); - for (Player player : world.getPlayers()) { - DGamePlayer dPlayer = DGamePlayer.getByPlayer(player); + worlds.getInstances().remove(this); + for (Player player : getWorld().getPlayers()) { + DEditPlayer dPlayer = DEditPlayer.getByPlayer(player); dPlayer.leave(); } - plugin.getServer().unloadWorld(world, true); - File dir = new File("DXL_Edit_" + id); - FileUtil.copyDirectory(dir, new File(plugin.getDataFolder(), "/maps/" + mapName), DungeonsXL.EXCLUDED_FILES); - FileUtil.deleteUnusedFiles(new File(plugin.getDataFolder(), "/maps/" + mapName)); - FileUtil.removeDirectory(dir); - } - - public void deleteNoSave() { - EditWorldUnloadEvent event = new EditWorldUnloadEvent(this, false); - plugin.getServer().getPluginManager().callEvent(event); - - if (event.isCancelled()) { - return; + if (save) { + plugin.getServer().unloadWorld(getWorld(), true); } - plugin.getEditWorlds().remove(this); - for (Player player : world.getPlayers()) { - DGamePlayer dPlayer = DGamePlayer.getByPlayer(player); - dPlayer.leave(); + FileUtil.copyDirectory(getFolder(), getResource().getFolder(), DungeonsXL.EXCLUDED_FILES); + FileUtil.deleteUnusedFiles(getResource().getFolder()); + + if (!save) { + plugin.getServer().unloadWorld(getWorld(), true); } - File dir = new File("DXL_Edit_" + id); - FileUtil.copyDirectory(dir, new File(plugin.getDataFolder(), "/maps/" + mapName), DungeonsXL.EXCLUDED_FILES); - FileUtil.deleteUnusedFiles(new File(plugin.getDataFolder(), "/maps/" + mapName)); - plugin.getServer().unloadWorld(world, true); - FileUtil.removeDirectory(dir); - } - - public void sendMessage(String message) { - for (DGamePlayer dPlayer : DGamePlayer.getByWorld(world)) { - MessageUtil.sendMessage(dPlayer.getPlayer(), message); - } + FileUtil.removeDirectory(getFolder()); } /* Statics */ + /** + * @param world + * the instance + * @return + * the EditWorld that represents the world + */ public static EditWorld getByWorld(World world) { - for (EditWorld editWorld : plugin.getEditWorlds()) { - if (editWorld.world.equals(world)) { - return editWorld; - } - } - - return null; + return getByName(world.getName()); } + /** + * @param world + * the instance name + * @return + * the EditWorld that represents the world + */ public static EditWorld getByName(String name) { - for (EditWorld editWorld : plugin.getEditWorlds()) { - if (editWorld.mapName.equalsIgnoreCase(name)) { - return editWorld; - } - } + InstanceWorld instance = worlds.getInstanceByName(name); - return null; - } - - public static void deleteAll() { - for (EditWorld editWorld : plugin.getEditWorlds()) { - editWorld.delete(); - } - } - - public static EditWorld load(String name) { - EditWorldLoadEvent event = new EditWorldLoadEvent(name); - plugin.getServer().getPluginManager().callEvent(event); - - if (event.isCancelled()) { - return null; - } - - for (EditWorld editWorld : plugin.getEditWorlds()) { - - if (editWorld.mapName.equalsIgnoreCase(name)) { - return editWorld; - } - } - - File file = new File(plugin.getDataFolder(), "/maps/" + name); - - if (file.exists()) { - EditWorld editWorld = new EditWorld(); - editWorld.mapName = name; - // World - FileUtil.copyDirectory(file, new File("DXL_Edit_" + editWorld.id), DungeonsXL.EXCLUDED_FILES); - - // Id File - File idFile = new File("DXL_Edit_" + editWorld.id + "/.id_" + name); - try { - idFile.createNewFile(); - } catch (IOException e) { - e.printStackTrace(); - } - - editWorld.world = plugin.getServer().createWorld(WorldCreator.name("DXL_Edit_" + editWorld.id)); - - try { - ObjectInputStream os = new ObjectInputStream(new FileInputStream(new File(plugin.getDataFolder(), "/maps/" + editWorld.mapName + "/DXLData.data"))); - int length = os.readInt(); - for (int i = 0; i < length; i++) { - int x = os.readInt(); - int y = os.readInt(); - int z = os.readInt(); - Block block = editWorld.world.getBlockAt(x, y, z); - editWorld.checkSign(block); - editWorld.signs.add(block); - } - os.close(); - - } catch (FileNotFoundException e) { - e.printStackTrace(); - } catch (IOException e) { - e.printStackTrace(); - } - - return editWorld; - } - - return null; - } - - public static boolean exists(String name) { - // Cheack Loaded EditWorlds - for (EditWorld editWorld : plugin.getEditWorlds()) { - if (editWorld.mapName.equalsIgnoreCase(name)) { - return true; - } - } - - // Cheack Unloaded Worlds - File file = new File(plugin.getDataFolder(), "/maps/" + name); - - if (file.exists()) { - return true; - } - - return false; - } - - // Invite - public static boolean addInvitedPlayer(String editWorldName, UUID uuid) { - if (!exists(editWorldName)) { - return false; - } - - File file = new File(plugin.getDataFolder() + "/maps/" + editWorldName, "config.yml"); - if (!file.exists()) { - try { - file.createNewFile(); - - } catch (IOException exception) { - exception.printStackTrace(); - return false; - } - } - WorldConfig config = new WorldConfig(file); - config.addInvitedPlayer(uuid.toString()); - config.save(); - - return true; - } - - public static boolean removeInvitedPlayer(String editWorldName, UUID uuid, String name) { - if (!exists(editWorldName)) { - return false; - } - - File file = new File(plugin.getDataFolder() + "/maps/" + editWorldName, "config.yml"); - if (!file.exists()) { - return false; - } - WorldConfig config = new WorldConfig(file); - config.removeInvitedPlayers(uuid.toString(), name.toLowerCase()); - config.save(); - - // Kick Player - EditWorld editWorld = EditWorld.getByName(editWorldName); - if (editWorld != null) { - DGamePlayer player = DGamePlayer.getByName(name); - - if (player != null) { - if (editWorld.world.getPlayers().contains(player.getPlayer())) { - player.leave(); - } - } - } - - return true; - } - - public static boolean isInvitedPlayer(String editWorldName, UUID uuid, String name) { - if (!exists(editWorldName)) { - return false; - } - - File file = new File(plugin.getDataFolder() + "/maps/" + editWorldName, "config.yml"); - if (!file.exists()) { - return false; - } - - WorldConfig config = new WorldConfig(file); - // get player from both a 0.9.1 and lower and 0.9.2 and higher file - if (config.getInvitedPlayers().contains(name.toLowerCase()) || config.getInvitedPlayers().contains(uuid.toString())) { - return true; + if (instance instanceof EditWorld) { + return (EditWorld) instance; } else { - return false; + return null; } } diff --git a/src/main/java/io/github/dre2n/dungeonsxl/world/GameWorld.java b/src/main/java/io/github/dre2n/dungeonsxl/world/GameWorld.java index 9592f82f..10ae4032 100644 --- a/src/main/java/io/github/dre2n/dungeonsxl/world/GameWorld.java +++ b/src/main/java/io/github/dre2n/dungeonsxl/world/GameWorld.java @@ -17,18 +17,14 @@ package io.github.dre2n.dungeonsxl.world; import io.github.dre2n.commons.util.FileUtil; -import io.github.dre2n.commons.util.messageutil.MessageUtil; import io.github.dre2n.dungeonsxl.DungeonsXL; import io.github.dre2n.dungeonsxl.config.DungeonConfig; -import io.github.dre2n.dungeonsxl.config.WorldConfig; import io.github.dre2n.dungeonsxl.dungeon.Dungeon; -import io.github.dre2n.dungeonsxl.event.gameworld.GameWorldLoadEvent; import io.github.dre2n.dungeonsxl.event.gameworld.GameWorldStartGameEvent; import io.github.dre2n.dungeonsxl.event.gameworld.GameWorldUnloadEvent; import io.github.dre2n.dungeonsxl.game.Game; import io.github.dre2n.dungeonsxl.game.GamePlaceableBlock; import io.github.dre2n.dungeonsxl.mob.DMob; -import io.github.dre2n.dungeonsxl.player.DGamePlayer; import io.github.dre2n.dungeonsxl.player.DGroup; import io.github.dre2n.dungeonsxl.reward.RewardChest; import io.github.dre2n.dungeonsxl.sign.DSign; @@ -42,19 +38,12 @@ import io.github.dre2n.dungeonsxl.trigger.Trigger; import io.github.dre2n.dungeonsxl.trigger.TriggerType; import io.github.dre2n.dungeonsxl.trigger.TriggerTypeDefault; import java.io.File; -import java.io.FileInputStream; -import java.io.FileNotFoundException; -import java.io.IOException; -import java.io.ObjectInputStream; import java.util.ArrayList; import java.util.List; import java.util.concurrent.CopyOnWriteArrayList; -import org.bukkit.Bukkit; import org.bukkit.Chunk; import org.bukkit.Location; import org.bukkit.World; -import org.bukkit.WorldCreator; -import org.bukkit.block.Block; import org.bukkit.block.Sign; import org.bukkit.entity.Entity; import org.bukkit.entity.EntityType; @@ -65,54 +54,26 @@ import org.bukkit.inventory.ItemStack; /** * @author Frank Baumann, Milan Albrecht, Daniel Saukel */ -public class GameWorld { +public class GameWorld extends InstanceWorld { static DungeonsXL plugin = DungeonsXL.getInstance(); // Variables private boolean tutorial; - - private CopyOnWriteArrayList placeableBlocks = new CopyOnWriteArrayList<>(); - private World world; - private String mapName; - private Location locLobby; private boolean isPlaying = false; - private int id; - private List secureObjects = new ArrayList<>(); - private CopyOnWriteArrayList loadedChunks = new CopyOnWriteArrayList<>(); - private CopyOnWriteArrayList signClass = new CopyOnWriteArrayList<>(); + // TO DO: Which lists actually need to be CopyOnWriteArrayLists? + private CopyOnWriteArrayList placeableBlocks = new CopyOnWriteArrayList<>(); + private List secureObjects = new CopyOnWriteArrayList<>(); + private CopyOnWriteArrayList loadedChunks = new CopyOnWriteArrayList<>(); + private CopyOnWriteArrayList classesSigns = new CopyOnWriteArrayList<>(); private CopyOnWriteArrayList dMobs = new CopyOnWriteArrayList<>(); - // TODO: Killed mobs private CopyOnWriteArrayList rewardChests = new CopyOnWriteArrayList<>(); private CopyOnWriteArrayList dSigns = new CopyOnWriteArrayList<>(); private CopyOnWriteArrayList triggers = new CopyOnWriteArrayList<>(); - private WorldConfig worldConfig; - public GameWorld() { - plugin.getGameWorlds().add(this); - - // ID - id = -1; - int i = -1; - while (id == -1) { - i++; - boolean exist = false; - for (GameWorld gameWorld : plugin.getGameWorlds()) { - if (gameWorld.id == i) { - exist = true; - break; - } - } - if (!exist) { - id = i; - } - } - } - - public GameWorld(String name) { - this(); - load(name); + GameWorld(ResourceWorld resourceWorld, File folder, World world, int id) { + super(resourceWorld, folder, world, id); } /** @@ -145,63 +106,18 @@ public class GameWorld { } /** - * @return the placeableBlocks + * @return the isPlaying */ - public CopyOnWriteArrayList getPlaceableBlocks() { - return placeableBlocks; + public boolean isPlaying() { + return isPlaying; } /** - * @param placeableBlocks - * the placeableBlocks to set + * @param isPlaying + * the isPlaying to set */ - public void setPlaceableBlocks(CopyOnWriteArrayList placeableBlocks) { - this.placeableBlocks = placeableBlocks; - } - - /** - * @return the world - */ - public World getWorld() { - return world; - } - - /** - * @param world - * the world to set - */ - public void setWorld(World world) { - this.world = world; - } - - /** - * @return the mapName - */ - public String getMapName() { - return mapName; - } - - /** - * @param mapName - * the mapName to set - */ - public void setMapName(String mapName) { - this.mapName = mapName; - } - - /** - * @return the location of the lobby - */ - public Location getLobbyLocation() { - return locLobby; - } - - /** - * @param location - * the location of the lobby to set - */ - public void setLobbyLocation(Location location) { - this.locLobby = location; + public void setPlaying(boolean isPlaying) { + this.isPlaying = isPlaying; } /** @@ -227,41 +143,26 @@ public class GameWorld { } // Lobby location as fallback - if (locLobby != null) { - return locLobby; + if (getLobbyLocation() != null) { + return getLobbyLocation(); } - return world.getSpawnLocation(); + return getWorld().getSpawnLocation(); } /** - * @return the isPlaying + * @return the placeableBlocks */ - public boolean isPlaying() { - return isPlaying; + public CopyOnWriteArrayList getPlaceableBlocks() { + return placeableBlocks; } /** - * @param isPlaying - * the isPlaying to set + * @param placeableBlocks + * the placeableBlocks to set */ - public void setPlaying(boolean isPlaying) { - this.isPlaying = isPlaying; - } - - /** - * @return the id - */ - public int getId() { - return id; - } - - /** - * @param id - * the id to set - */ - public void setId(int id) { - this.id = id; + public void setPlaceableBlocks(CopyOnWriteArrayList placeableBlocks) { + this.placeableBlocks = placeableBlocks; } /** @@ -295,18 +196,18 @@ public class GameWorld { } /** - * @return the signClass + * @return the classes signs */ - public CopyOnWriteArrayList getSignClass() { - return signClass; + public CopyOnWriteArrayList getClassesSigns() { + return classesSigns; } /** - * @param signClass - * the signClass to set + * @param classes signs + * the classes signs to set */ - public void setSignClass(CopyOnWriteArrayList signClass) { - this.signClass = signClass; + public void setClasses(CopyOnWriteArrayList signs) { + classesSigns = signs; } /** @@ -437,32 +338,13 @@ public class GameWorld { return mobCount; } - /** - * @return the worldConfig - */ - public WorldConfig getConfig() { - if (worldConfig == null) { - return plugin.getMainConfig().getDefaultWorldConfig(); - } - - return worldConfig; - } - - /** - * @param worldConfig - * the worldConfig to set - */ - public void setConfig(WorldConfig worldConfig) { - this.worldConfig = worldConfig; - } - /** * @return the Dungeon that contains the GameWorld */ public Dungeon getDungeon() { for (Dungeon dungeon : plugin.getDungeons().getDungeons()) { DungeonConfig dungeonConfig = dungeon.getConfig(); - if (dungeonConfig.getFloors().contains(mapName) || dungeonConfig.getStartFloor().equals(mapName) || dungeonConfig.getEndFloor().equals(mapName)) { + if (dungeonConfig.getFloors().contains(getName()) || dungeonConfig.getStartFloor().equals(getName()) || dungeonConfig.getEndFloor().equals(getName())) { return dungeon; } } @@ -470,13 +352,9 @@ public class GameWorld { return null; } - public void checkSign(Block block) { - if (block.getState() instanceof Sign) { - Sign sign = (Sign) block.getState(); - dSigns.add(DSign.create(sign, this)); - } - } - + /** + * Set up the instance for the game + */ public void startGame() { GameWorldStartGameEvent event = new GameWorldStartGameEvent(this, getGame()); plugin.getServer().getPluginManager().callEvent(event); @@ -508,12 +386,10 @@ public class GameWorld { } } - public void sendMessage(String message) { - for (DGamePlayer dPlayer : DGamePlayer.getByWorld(world)) { - MessageUtil.sendMessage(dPlayer.getPlayer(), message); - } - } - + /** + * Delete this instance. + */ + @Override public void delete() { GameWorldUnloadEvent event = new GameWorldUnloadEvent(this); plugin.getServer().getPluginManager().callEvent(event); @@ -522,12 +398,14 @@ public class GameWorld { return; } - plugin.getGameWorlds().remove(this); - plugin.getServer().unloadWorld(world, true); - File dir = new File("DXL_Game_" + id); - FileUtil.removeDirectory(dir); + plugin.getWorlds().getInstances().remove(this); + plugin.getServer().unloadWorld(getWorld(), true); + FileUtil.removeDirectory(getFolder()); } + /** + * Ongoing updates + */ public void update() { if (getWorld() == null) { return; @@ -542,6 +420,7 @@ public class GameWorld { continue; } } + for (Entity player : spider.getNearbyEntities(10, 10, 10)) { if (player.getType() == EntityType.PLAYER) { spider.setTarget((LivingEntity) player); @@ -551,86 +430,21 @@ public class GameWorld { } } - public void load(String name) { - GameWorldLoadEvent event = new GameWorldLoadEvent(name); - plugin.getServer().getPluginManager().callEvent(event); - - if (event.isCancelled()) { - return; - } - - File file = new File(plugin.getDataFolder(), "/maps/" + name); - - if (file.exists()) { - mapName = name; - - // Unload empty editWorlds - for (EditWorld editWorld : plugin.getEditWorlds()) { - if (editWorld.getWorld().getPlayers().isEmpty()) { - editWorld.delete(); - } - } - - // Config einlesen - worldConfig = new WorldConfig(new File(plugin.getDataFolder() + "/maps/" + mapName, "config.yml")); - - // Secure Objects - secureObjects = worldConfig.getSecureObjects(); - - if (Bukkit.getWorld("DXL_Game_" + id) == null) { - - // World - FileUtil.copyDirectory(file, new File("DXL_Game_" + id), DungeonsXL.EXCLUDED_FILES); - - // Id File - File idFile = new File("DXL_Game_" + id + "/.id_" + name); - try { - idFile.createNewFile(); - } catch (IOException e) { - e.printStackTrace(); - } - - world = plugin.getServer().createWorld(WorldCreator.name("DXL_Game_" + id)); - - ObjectInputStream os; - try { - os = new ObjectInputStream(new FileInputStream(new File(plugin.getDataFolder() + "/maps/" + mapName + "/DXLData.data"))); - - int length = os.readInt(); - for (int i = 0; i < length; i++) { - int x = os.readInt(); - int y = os.readInt(); - int z = os.readInt(); - Block block = world.getBlockAt(x, y, z); - checkSign(block); - } - - os.close(); - - } catch (FileNotFoundException exception) { - MessageUtil.log(plugin, "Could not find any sign data for the world \"" + name + "\"!"); - - } catch (IOException exception) { - exception.printStackTrace(); - } - } - } - } - /* Statics */ + /** + * @param world + * the instance + * @return + * the EditWorld that represents the world + */ public static GameWorld getByWorld(World world) { - for (GameWorld gameWorld : plugin.getGameWorlds()) { - if (gameWorld.getWorld() != null && gameWorld.getWorld().equals(world)) { - return gameWorld; - } - } + InstanceWorld instance = plugin.getWorlds().getInstanceByName(world.getName()); - return null; - } + if (instance instanceof GameWorld) { + return (GameWorld) instance; - public static void deleteAll() { - for (GameWorld gameWorld : plugin.getGameWorlds()) { - gameWorld.delete(); + } else { + return null; } } diff --git a/src/main/java/io/github/dre2n/dungeonsxl/world/InstanceWorld.java b/src/main/java/io/github/dre2n/dungeonsxl/world/InstanceWorld.java index c1d7f5ae..3baa7301 100644 --- a/src/main/java/io/github/dre2n/dungeonsxl/world/InstanceWorld.java +++ b/src/main/java/io/github/dre2n/dungeonsxl/world/InstanceWorld.java @@ -16,27 +16,34 @@ */ package io.github.dre2n.dungeonsxl.world; +import io.github.dre2n.commons.util.messageutil.MessageUtil; import io.github.dre2n.dungeonsxl.DungeonsXL; import io.github.dre2n.dungeonsxl.config.WorldConfig; +import io.github.dre2n.dungeonsxl.player.DGamePlayer; +import java.io.File; +import java.io.IOException; import org.bukkit.Location; import org.bukkit.World; /** * @author Daniel Saukel */ -public class InstanceWorld { +public abstract class InstanceWorld { - DungeonsXL plugin = DungeonsXL.getInstance(); + protected static DungeonsXL plugin = DungeonsXL.getInstance(); public static String ID_FILE_PREFIX = ".id_"; private ResourceWorld resourceWorld; + private File folder; private World world; + private File idFile; private int id; private Location lobby; - InstanceWorld(ResourceWorld resourceWorld, World world, int id) { + InstanceWorld(ResourceWorld resourceWorld, File folder, World world, int id) { this.resourceWorld = resourceWorld; + this.folder = folder; this.world = world; this.id = id; } @@ -56,6 +63,20 @@ public class InstanceWorld { return resourceWorld.getConfig(); } + /** + * @return the ResourceWorld of that this world is an instance + */ + public ResourceWorld getResource() { + return resourceWorld; + } + + /** + * @return the folder of the instance + */ + public File getFolder() { + return folder; + } + /** * @return the instance */ @@ -63,6 +84,13 @@ public class InstanceWorld { return world; } + /** + * @return the file that stores the ID + */ + public File getIdFile() { + return idFile; + } + /** * @return the unique ID */ @@ -85,11 +113,35 @@ public class InstanceWorld { this.lobby = lobby; } + /* Actions */ /** - * @return the ResourceWorld of that this world is an instance + * Sends a message to all players in the instance. + * + * @param message + * the message to send */ - public ResourceWorld getResource() { - return resourceWorld; + public void sendMessage(String message) { + for (DGamePlayer dPlayer : DGamePlayer.getByWorld(world)) { + MessageUtil.sendMessage(dPlayer.getPlayer(), message); + } } + /** + * @return the ID file + */ + public void generateIdFile() { + try { + idFile = new File(getFolder(), ID_FILE_PREFIX + getName()); + idFile.createNewFile(); + } catch (IOException exception) { + exception.printStackTrace(); + } + } + + /* Abstracts */ + /** + * Deletes this instance. + */ + public abstract void delete(); + } diff --git a/src/main/java/io/github/dre2n/dungeonsxl/world/ResourceWorld.java b/src/main/java/io/github/dre2n/dungeonsxl/world/ResourceWorld.java index c92742fa..c6d7a046 100644 --- a/src/main/java/io/github/dre2n/dungeonsxl/world/ResourceWorld.java +++ b/src/main/java/io/github/dre2n/dungeonsxl/world/ResourceWorld.java @@ -20,11 +20,14 @@ import io.github.dre2n.commons.util.FileUtil; import io.github.dre2n.dungeonsxl.DungeonsXL; import io.github.dre2n.dungeonsxl.config.SignData; import io.github.dre2n.dungeonsxl.config.WorldConfig; +import io.github.dre2n.dungeonsxl.player.DEditPlayer; import java.io.File; import java.io.IOException; import org.bukkit.Bukkit; +import org.bukkit.OfflinePlayer; import org.bukkit.World; import org.bukkit.WorldCreator; +import org.bukkit.WorldType; /** * This class represents unloaded worlds. @@ -34,11 +37,31 @@ import org.bukkit.WorldCreator; public class ResourceWorld { DungeonsXL plugin = DungeonsXL.getInstance(); + Worlds worlds = plugin.getWorlds(); private File folder; private WorldConfig config; private SignData signData; + public ResourceWorld(String name) { + folder = new File(DungeonsXL.MAPS, name); + if (!folder.exists()) { + folder.mkdir(); + } + + File signDataFile = new File(folder, "DXLData.data"); + if (!signDataFile.exists()) { + try { + signDataFile.createNewFile(); + + } catch (IOException exception) { + exception.printStackTrace(); + } + } + + signData = new SignData(signDataFile); + } + public ResourceWorld(File folder) { this.folder = folder; @@ -90,6 +113,53 @@ public class ResourceWorld { return signData; } + /** + * @param player + * the player to invite + */ + public void addInvitedPlayer(OfflinePlayer player) { + if (config == null) { + config = new WorldConfig(); + } + + config.addInvitedPlayer(player.getUniqueId().toString()); + config.save(); + } + + /** + * @param player + * the player to uninvite + */ + public boolean removeInvitedPlayer(OfflinePlayer player) { + if (config == null) { + return false; + } + + config.removeInvitedPlayers(player.getUniqueId().toString(), player.getName().toLowerCase()); + config.save(); + + DEditPlayer editPlayer = DEditPlayer.getByName(player.getName()); + if (editPlayer != null) { + if (EditWorld.getByWorld(editPlayer.getWorld()).getResource() == this) { + editPlayer.leave(); + } + } + + return true; + } + + /** + * @param player + * the player to check + */ + public boolean isInvitedPlayer(OfflinePlayer player) { + if (config == null) { + return false; + } + + return config.getInvitedPlayers().contains(player.getName().toLowerCase()) || config.getInvitedPlayers().contains(player.getUniqueId().toString()); + } + /* Actions */ /** * @param game @@ -97,8 +167,8 @@ public class ResourceWorld { * @return an instance of this world */ public InstanceWorld instantiate(boolean game) { - int id = plugin.getWorlds().getInstances().size(); - String name = "DXL_" + (game ? "Game" : "Edit") + "_" + id; + int id = worlds.generateId(); + String name = worlds.generateName(game); File instanceFolder = new File(Bukkit.getWorldContainer(), name); FileUtil.copyDirectory(folder, instanceFolder, DungeonsXL.EXCLUDED_FILES); @@ -108,19 +178,14 @@ public class ResourceWorld { World world = plugin.getServer().createWorld(WorldCreator.name(name)); - File idFile = new File(name, InstanceWorld.ID_FILE_PREFIX + getName()); - try { - idFile.createNewFile(); - } catch (IOException exception) { - exception.printStackTrace(); - } - - InstanceWorld instance = new InstanceWorld(this, world, id); - + InstanceWorld instance = null; try { if (game) { + new GameWorld(this, instanceFolder, world, id); signData.deserializeSigns((GameWorld) instance); + } else { + new EditWorld(this, instanceFolder, world, id); signData.deserializeSigns((EditWorld) instance); } @@ -145,4 +210,32 @@ public class ResourceWorld { return (GameWorld) instantiate(true); } + /** + * Generate a new ResourceWorld. + * + * @return the automatically created EditWorld instance + */ + public EditWorld generate() { + String name = worlds.generateName(false); + WorldCreator creator = WorldCreator.name(name); + creator.type(WorldType.FLAT); + creator.generateStructures(false); + + /*EditWorldGenerateEvent event = new EditWorldGenerateEvent(this); + plugin.getServer().getPluginManager().callEvent(event); + + if (event.isCancelled()) { + return; + } + */ + int id = worlds.generateId(); + File folder = new File(Bukkit.getWorldContainer(), name); + World world = plugin.getServer().createWorld(creator); + + EditWorld editWorld = new EditWorld(this, folder, world, id); + editWorld.generateIdFile(); + + return editWorld; + } + } diff --git a/src/main/java/io/github/dre2n/dungeonsxl/world/Worlds.java b/src/main/java/io/github/dre2n/dungeonsxl/world/Worlds.java index a4379b8f..341dcfd5 100644 --- a/src/main/java/io/github/dre2n/dungeonsxl/world/Worlds.java +++ b/src/main/java/io/github/dre2n/dungeonsxl/world/Worlds.java @@ -16,9 +16,13 @@ */ package io.github.dre2n.dungeonsxl.world; +import io.github.dre2n.commons.util.FileUtil; import io.github.dre2n.commons.util.NumberUtil; +import io.github.dre2n.dungeonsxl.DungeonsXL; import java.io.File; +import java.util.HashSet; import java.util.Set; +import org.bukkit.Bukkit; /** * @author Daniel Saukel @@ -89,4 +93,116 @@ public class Worlds { return instances; } + /** + * @return the loaded GameWorlds + */ + public Set getGameWorlds() { + Set gameWorlds = new HashSet<>(); + for (InstanceWorld instance : instances) { + if (instance instanceof GameWorld) { + gameWorlds.add((GameWorld) instance); + } + } + return gameWorlds; + } + + /** + * @return the loaded EditWorlds + */ + public Set getEditWorlds() { + Set editWorlds = new HashSet<>(); + for (InstanceWorld instance : instances) { + if (instance instanceof GameWorld) { + editWorlds.add((EditWorld) instance); + } + } + return editWorlds; + } + + /** + * @param name + * the name of the map; can either be the resource name or the instance name + * @return + * if a map with this name exists + */ + public boolean exists(String name) { + for (ResourceWorld resource : resources) { + if (resource.getName().equalsIgnoreCase(name)) { + return true; + } + } + + for (InstanceWorld instance : instances) { + if (instance.getFolder().getName().equalsIgnoreCase(name)) { + return true; + } + } + + return false; + } + + /** + * Check world container for old, remaining instances and delete them. + */ + public void check() { + for (File file : Bukkit.getWorldContainer().listFiles()) { + if (file.getName().startsWith("DXL_Edit_") && file.isDirectory()) { + for (File mapFile : file.listFiles()) { + if (mapFile.getName().startsWith(".id_")) { + String name = mapFile.getName().substring(4); + + FileUtil.copyDirectory(file, new File(DungeonsXL.MAPS, name), DungeonsXL.EXCLUDED_FILES); + FileUtil.deleteUnusedFiles(new File(DungeonsXL.MAPS, name)); + + FileUtil.removeDirectory(file); + } + } + + } else if (file.getName().startsWith("DXL_Game_") && file.isDirectory()) { + FileUtil.removeDirectory(file); + } + } + } + + /** + * Clean up all instances. + */ + public void deleteAllInstances() { + for (InstanceWorld instance : instances) { + instance.delete(); + } + } + + /** + * Saves all EditWorlds. + */ + public void saveAll() { + for (EditWorld editWorld : getEditWorlds()) { + editWorld.save(); + } + } + + /** + * @return an ID for the instance + */ + public int generateId() { + int id = 0; + for (InstanceWorld instance : instances) { + if (instance.getId() >= id) { + id = instance.getId() + 1; + } + } + return id; + } + + /** + * @return a name for the instance + * + * @param game + * whether the instance is a GameWorld + */ + public String generateName(boolean game) { + return "DXL_" + (game ? "Game" : "Edit") + "_" + generateId(); + } + } diff --git a/src/test/java/io/github/dre2n/dungeonsxl/listener/DGroupListener.java b/src/test/java/io/github/dre2n/dungeonsxl/listener/DGroupListener.java index 4fe7f009..3e3c1c90 100644 --- a/src/test/java/io/github/dre2n/dungeonsxl/listener/DGroupListener.java +++ b/src/test/java/io/github/dre2n/dungeonsxl/listener/DGroupListener.java @@ -61,13 +61,13 @@ public class DGroupListener implements Listener { @EventHandler public void onStartFloor(DGroupStartFloorEvent event) { MessageUtil.log(plugin, "&b== " + event.getEventName() + "=="); - MessageUtil.log(plugin, "GameWorld: " + event.getGameWorld().getMapName()); + MessageUtil.log(plugin, "GameWorld: " + event.getGameWorld().getName()); } @EventHandler public void onFinishFloor(DGroupFinishFloorEvent event) { MessageUtil.log(plugin, "&b== " + event.getEventName() + "=="); - MessageUtil.log(plugin, "Finished: " + event.getFinished().getMapName()); + MessageUtil.log(plugin, "Finished: " + event.getFinished().getName()); MessageUtil.log(plugin, "Next: " + event.getNext()); }