Use static counter for dungeon world IDs

This commit is contained in:
Daniel Saukel 2020-05-02 00:46:20 +02:00
parent df48257ef5
commit 2b1c09d0d7
4 changed files with 12 additions and 26 deletions

View File

@ -44,8 +44,8 @@ public class DEditWorld extends DInstanceWorld implements EditWorld {
private File idFile;
DEditWorld(DungeonsXL plugin, DResourceWorld resourceWorld, File folder, int id) {
super(plugin, resourceWorld, folder, id);
DEditWorld(DungeonsXL plugin, DResourceWorld resourceWorld, File folder) {
super(plugin, resourceWorld, folder);
}
/* Getters and setters */

View File

@ -59,7 +59,6 @@ import java.util.Map;
import java.util.Set;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.block.Sign;
import org.bukkit.entity.Entity;
@ -96,8 +95,8 @@ public class DGameWorld extends DInstanceWorld implements GameWorld {
private boolean readySign;
DGameWorld(DungeonsXL plugin, DResourceWorld resourceWorld, File folder, int id) {
super(plugin, resourceWorld, folder, id);
DGameWorld(DungeonsXL plugin, DResourceWorld resourceWorld, File folder) {
super(plugin, resourceWorld, folder);
caliburn = plugin.getCaliburn();
}

View File

@ -17,7 +17,6 @@
package de.erethon.dungeonsxl.world;
import de.erethon.commons.chat.MessageUtil;
import de.erethon.commons.misc.Registry;
import de.erethon.commons.player.PlayerUtil;
import de.erethon.dungeonsxl.DungeonsXL;
import de.erethon.dungeonsxl.api.DungeonsAPI;
@ -47,6 +46,8 @@ public abstract class DInstanceWorld implements InstanceWorld {
protected DungeonsXL plugin;
protected PlayerCache dPlayers;
static int counter;
protected Map<Block, DungeonSign> signs = new HashMap<>();
private DResourceWorld resourceWorld;
private File folder;
@ -54,13 +55,13 @@ public abstract class DInstanceWorld implements InstanceWorld {
private int id;
private Location lobby;
DInstanceWorld(DungeonsXL plugin, DResourceWorld resourceWorld, File folder, int id) {
DInstanceWorld(DungeonsXL plugin, DResourceWorld resourceWorld, File folder) {
this.plugin = plugin;
dPlayers = plugin.getPlayerCache();
this.resourceWorld = resourceWorld;
this.folder = folder;
this.id = id;
id = counter++;
plugin.getInstanceCache().add(id, this);
}
@ -196,20 +197,6 @@ public abstract class DInstanceWorld implements InstanceWorld {
}
}
/**
* @param instanceCache the used instance cache
* @return an ID for the instance
*/
public static int generateId(Registry<Integer, InstanceWorld> instanceCache) {
int id = 0;
for (InstanceWorld instance : instanceCache) {
if (instance.getId() >= id) {
id = instance.getId() + 1;
}
}
return id;
}
/**
* @return a name for the instance
* @param game whether the instance is a GameWorld

View File

@ -176,7 +176,7 @@ public class DResourceWorld implements ResourceWorld {
public DInstanceWorld instantiate(boolean game) {
plugin.setLoadingWorld(true);
int id = DInstanceWorld.generateId(plugin.getInstanceCache());
int id = DInstanceWorld.counter;
String name = DInstanceWorld.generateName(game, id);
File instanceFolder = new File(Bukkit.getWorldContainer(), name);
@ -197,7 +197,7 @@ public class DResourceWorld implements ResourceWorld {
}
}
DInstanceWorld instance = game ? new DGameWorld(plugin, this, instanceFolder, id) : new DEditWorld(plugin, this, instanceFolder, id);
DInstanceWorld instance = game ? new DGameWorld(plugin, this, instanceFolder) : new DEditWorld(plugin, this, instanceFolder);
FileUtil.copyDir(folder, instanceFolder, DungeonsXL.EXCLUDED_FILES);
instance.world = Bukkit.createWorld(WorldCreator.name(name).environment(getWorldEnvironment())).getName();
@ -271,14 +271,14 @@ public class DResourceWorld implements ResourceWorld {
* @return the automatically created DEditWorld instance
*/
public DEditWorld generate() {
int id = DInstanceWorld.generateId(plugin.getInstanceCache());
int id = DInstanceWorld.counter;
String name = DInstanceWorld.generateName(false, id);
File folder = new File(Bukkit.getWorldContainer(), name);
WorldCreator creator = new WorldCreator(name);
creator.type(WorldType.FLAT);
creator.generateStructures(false);
DEditWorld editWorld = new DEditWorld(plugin, this, folder, id);
DEditWorld editWorld = new DEditWorld(plugin, this, folder);
EditWorldGenerateEvent event = new EditWorldGenerateEvent(editWorld);
Bukkit.getPluginManager().callEvent(event);