#94: Resolved all errors due to changes

This commit is contained in:
Daniel Saukel 2016-06-23 18:22:47 +02:00
parent 8bb9fe0eac
commit 141a35f5e7
30 changed files with 493 additions and 733 deletions

View File

@ -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 */
/**

View File

@ -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());

View File

@ -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());
}
}

View File

@ -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)) {

View File

@ -58,7 +58,7 @@ public class EscapeCommand extends BRCommand {
}
if (editWorld.getWorld().getPlayers().isEmpty()) {
editWorld.deleteNoSave();
editWorld.delete(false);
}
} else {

View File

@ -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,9 +45,17 @@ public class InviteCommand extends BRCommand {
@Override
public void onExecute(String[] args, CommandSender sender) {
if (EditWorld.addInvitedPlayer(args[2], UUIDUtil.getUniqueIdFromName(args[1]))) {
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]));
}

View File

@ -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<String> 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<String> 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);

View File

@ -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 = "";

View File

@ -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());

View File

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

View File

@ -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 = "";

View File

@ -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]));
}
}
}

View File

@ -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();
}

View File

@ -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);
}

View File

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

View File

@ -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));

View File

@ -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());
}

View File

@ -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());

View File

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

View File

@ -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

View File

@ -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);
}

View File

@ -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();
}

View File

@ -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);
}

View File

@ -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);
}
}
}

View File

@ -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<String> invitedPlayers = new CopyOnWriteArrayList<>();
private CopyOnWriteArrayList<Block> 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<String> getInvitedPlayers() {
return invitedPlayers;
}
/**
* @param invitedPlayers
* the invitedPlayers to set
*/
public void setInvitedPlayers(CopyOnWriteArrayList<String> 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;
}
world = plugin.getServer().createWorld(creator);
if (lines[0].equalsIgnoreCase("[lobby]")) {
setLobbyLocation(block.getLocation());
}
}
}
/**
* 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);
}
/**
* 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);
if (save) {
plugin.getServer().unloadWorld(getWorld(), true);
}
public void deleteNoSave() {
EditWorldUnloadEvent event = new EditWorldUnloadEvent(this, false);
plugin.getServer().getPluginManager().callEvent(event);
FileUtil.copyDirectory(getFolder(), getResource().getFolder(), DungeonsXL.EXCLUDED_FILES);
FileUtil.deleteUnusedFiles(getResource().getFolder());
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();
}
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;
}
}

View File

@ -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<GamePlaceableBlock> placeableBlocks = new CopyOnWriteArrayList<>();
private World world;
private String mapName;
private Location locLobby;
private boolean isPlaying = false;
private int id;
private List<ItemStack> secureObjects = new ArrayList<>();
private CopyOnWriteArrayList<Chunk> loadedChunks = new CopyOnWriteArrayList<>();
private CopyOnWriteArrayList<Sign> signClass = new CopyOnWriteArrayList<>();
// TO DO: Which lists actually need to be CopyOnWriteArrayLists?
private CopyOnWriteArrayList<GamePlaceableBlock> placeableBlocks = new CopyOnWriteArrayList<>();
private List<ItemStack> secureObjects = new CopyOnWriteArrayList<>();
private CopyOnWriteArrayList<Chunk> loadedChunks = new CopyOnWriteArrayList<>();
private CopyOnWriteArrayList<Sign> classesSigns = new CopyOnWriteArrayList<>();
private CopyOnWriteArrayList<DMob> dMobs = new CopyOnWriteArrayList<>();
// TODO: Killed mobs
private CopyOnWriteArrayList<RewardChest> rewardChests = new CopyOnWriteArrayList<>();
private CopyOnWriteArrayList<DSign> dSigns = new CopyOnWriteArrayList<>();
private CopyOnWriteArrayList<Trigger> 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<GamePlaceableBlock> getPlaceableBlocks() {
return placeableBlocks;
public boolean isPlaying() {
return isPlaying;
}
/**
* @param placeableBlocks
* the placeableBlocks to set
* @param isPlaying
* the isPlaying to set
*/
public void setPlaceableBlocks(CopyOnWriteArrayList<GamePlaceableBlock> 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<GamePlaceableBlock> 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<GamePlaceableBlock> placeableBlocks) {
this.placeableBlocks = placeableBlocks;
}
/**
@ -295,18 +196,18 @@ public class GameWorld {
}
/**
* @return the signClass
* @return the classes signs
*/
public CopyOnWriteArrayList<Sign> getSignClass() {
return signClass;
public CopyOnWriteArrayList<Sign> getClassesSigns() {
return classesSigns;
}
/**
* @param signClass
* the signClass to set
* @param classes signs
* the classes signs to set
*/
public void setSignClass(CopyOnWriteArrayList<Sign> signClass) {
this.signClass = signClass;
public void setClasses(CopyOnWriteArrayList<Sign> 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,87 +430,22 @@ 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());
if (instance instanceof GameWorld) {
return (GameWorld) instance;
} else {
return null;
}
public static void deleteAll() {
for (GameWorld gameWorld : plugin.getGameWorlds()) {
gameWorld.delete();
}
}
}

View File

@ -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();
}

View File

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

View File

@ -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<GameWorld> getGameWorlds() {
Set<GameWorld> gameWorlds = new HashSet<>();
for (InstanceWorld instance : instances) {
if (instance instanceof GameWorld) {
gameWorlds.add((GameWorld) instance);
}
}
return gameWorlds;
}
/**
* @return the loaded EditWorlds
*/
public Set<EditWorld> getEditWorlds() {
Set<EditWorld> 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();
}
}

View File

@ -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());
}