Fix unloaded protections

This commit is contained in:
Daniel Saukel 2022-02-17 17:03:01 +01:00
parent 5f607a9d3b
commit 2b0f0354aa
5 changed files with 68 additions and 21 deletions

View File

@ -367,6 +367,7 @@ public class DungeonsXL extends DREPlugin implements DungeonsAPI {
commandScriptRegistry.add(cmd.getName(), cmd);
}
protections = new GlobalProtectionCache(this);
protections.loadAll();
/* Integrations */
if (LWCUtil.isLWCLoaded()) {

View File

@ -88,6 +88,17 @@ public abstract class GlobalProtection {
public abstract void save(ConfigurationSection config);
public UnloadedProtection unload() {
String path = getDataPath() + "." + getWorld().getName() + "." + getId();
ConfigurationSection config;
if (!plugin.getGlobalProtectionCache().getConfig().contains(path)) {
config = plugin.getGlobalProtectionCache().getConfig().createSection(path);
} else {
config = plugin.getGlobalProtectionCache().getConfig().getConfigurationSection(path);
}
return UnloadedProtection.create(plugin, getClass(), getWorld().getName(), getId(), config);
}
/**
* @return a collection of all blocks covered by this protection
*/

View File

@ -58,7 +58,10 @@ public class GlobalProtectionCache {
}
}
config = YamlConfiguration.loadConfiguration(file);
loadAll();
}
public FileConfiguration getConfig() {
return config;
}
/**
@ -141,7 +144,8 @@ public class GlobalProtectionCache {
if (world != null) {
addProtection(new GameSign(plugin, world, NumberUtil.parseInt(entry.getKey()), ws.getConfigurationSection(entry.getKey())));
} else {
unloaded.put(new UnloadedProtection<>(plugin, GameSign.class, worldName, NumberUtil.parseInt(entry.getKey()), ws.getConfigurationSection(entry.getKey())), worldName);
UnloadedProtection protection = UnloadedProtection.create(plugin, GameSign.class, worldName, NumberUtil.parseInt(entry.getKey()), ws.getConfigurationSection(entry.getKey()));
unloaded.put(protection, worldName);
}
}
}
@ -158,7 +162,8 @@ public class GlobalProtectionCache {
if (world != null) {
addProtection(new GroupSign(plugin, world, NumberUtil.parseInt(entry.getKey()), ws.getConfigurationSection(entry.getKey())));
} else {
unloaded.put(new UnloadedProtection<>(plugin, GroupSign.class, worldName, NumberUtil.parseInt(entry.getKey()), ws.getConfigurationSection(entry.getKey())), worldName);
UnloadedProtection protection = UnloadedProtection.create(plugin, GroupSign.class, worldName, NumberUtil.parseInt(entry.getKey()), ws.getConfigurationSection(entry.getKey()));
unloaded.put(protection, worldName);
}
}
}
@ -175,7 +180,8 @@ public class GlobalProtectionCache {
if (world != null) {
addProtection(new LeaveSign(plugin, world, NumberUtil.parseInt(entry.getKey()), ws.getConfigurationSection(entry.getKey())));
} else {
unloaded.put(new UnloadedProtection<>(plugin, LeaveSign.class, worldName, NumberUtil.parseInt(entry.getKey()), ws.getConfigurationSection(entry.getKey())), worldName);
UnloadedProtection protection = UnloadedProtection.create(plugin, LeaveSign.class, worldName, NumberUtil.parseInt(entry.getKey()), ws.getConfigurationSection(entry.getKey()));
unloaded.put(protection, worldName);
}
}
}
@ -192,7 +198,8 @@ public class GlobalProtectionCache {
if (world != null) {
addProtection(new DPortal(plugin, world, NumberUtil.parseInt(entry.getKey()), ws.getConfigurationSection(entry.getKey())));
} else {
unloaded.put(new UnloadedProtection<>(plugin, DPortal.class, worldName, NumberUtil.parseInt(entry.getKey()), ws.getConfigurationSection(entry.getKey())), worldName);
UnloadedProtection protection = UnloadedProtection.create(plugin, DPortal.class, worldName, NumberUtil.parseInt(entry.getKey()), ws.getConfigurationSection(entry.getKey()));
unloaded.put(protection, worldName);
}
}
}
@ -207,9 +214,11 @@ public class GlobalProtectionCache {
exception.printStackTrace();
}
}
config.set("protections", null);
for (GlobalProtection protection : protections) {
protection.save(config.createSection(protection.getDataPath() + "." + protection.getWorld().getName() + "." + protection.getId()));
String path = protection.getDataPath() + "." + protection.getWorld().getName() + "." + protection.getId();
if (!config.contains(path)) {
protection.save(config.createSection(path));
}
}
try {
config.save(file);

View File

@ -23,9 +23,11 @@ import de.erethon.dungeonsxl.config.DMessage;
import de.erethon.dungeonsxl.player.DGlobalPlayer;
import de.erethon.dungeonsxl.player.DPermission;
import de.erethon.dungeonsxl.player.DPlayerListener;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Map.Entry;
import java.util.Set;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
@ -46,6 +48,7 @@ import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.event.player.PlayerMoveEvent;
import org.bukkit.event.player.PlayerPortalEvent;
import org.bukkit.event.world.WorldLoadEvent;
import org.bukkit.event.world.WorldUnloadEvent;
import org.bukkit.inventory.ItemStack;
/**
@ -299,9 +302,29 @@ public class GlobalProtectionListener implements Listener {
@EventHandler
public void onWorldLoad(WorldLoadEvent event) {
World world = event.getWorld();
for (Entry<UnloadedProtection, String> entry : new HashSet<>(plugin.getGlobalProtectionCache().getUnloadedProtections().entrySet())) {
plugin.log("New world detected.");
Set<Entry<UnloadedProtection, String>> protections = plugin.getGlobalProtectionCache().getUnloadedProtections().entrySet();
for (Entry<UnloadedProtection, String> entry : protections.toArray(new Entry[protections.size()])) {
plugin.log("Checking unloaded protection " + entry);
if (world.getName().equals(entry.getValue())) {
entry.getKey().load(world);
plugin.log("New world has global DXL data: " + entry);
plugin.getGlobalProtectionCache().addProtection(entry.getKey().load(world));
}
}
}
@EventHandler
public void onWorldUnload(WorldUnloadEvent event) {
World world = event.getWorld();
if (world.getName().startsWith("DXL_")) {
return;
}
plugin.log("Unloaded world detected.");
Collection<GlobalProtection> protections = plugin.getGlobalProtectionCache().getProtections();
for (GlobalProtection protection : protections.toArray(new GlobalProtection[protections.size()])) {
if (protection.getWorld().getName().equals(world.getName())) {
protection.delete();
plugin.getGlobalProtectionCache().getUnloadedProtections().put(protection.unload(), world.getName());
}
}
}

View File

@ -36,19 +36,22 @@ public class UnloadedProtection<T extends GlobalProtection> {
private int id;
private ConfigurationSection config;
public UnloadedProtection(DungeonsXL plugin, Class<T> type, String worldName, int id, ConfigurationSection config) {
this.plugin = plugin;
try {
constructor = type.getConstructor(DungeonsXL.class, World.class, int.class, ConfigurationSection.class);
} catch (NoSuchMethodException | SecurityException exception) {
// Don't register
return;
}
this.worldName = worldName;
this.id = id;
this.config = config;
private UnloadedProtection() {}
cache = plugin.getGlobalProtectionCache();
public static <T extends GlobalProtection> UnloadedProtection create(DungeonsXL plugin, Class<T> type, String worldName, int id, ConfigurationSection config) {
UnloadedProtection protection = new UnloadedProtection();
protection.plugin = plugin;
try {
protection.constructor = type.getConstructor(DungeonsXL.class, World.class, int.class, ConfigurationSection.class);
} catch (NoSuchMethodException | SecurityException exception) {
return null;
}
protection.worldName = worldName;
protection.id = id;
protection.config = config;
protection.cache = plugin.getGlobalProtectionCache();
return protection;
}
public T load(World world) {