Recode global data loading; resolves #122

This commit is contained in:
Daniel Saukel 2018-09-24 01:21:04 +02:00
parent cc19684407
commit ddb955a8c1
5 changed files with 86 additions and 27 deletions

View File

@ -212,7 +212,7 @@ public class DungeonsXL extends DREPlugin {
public void loadConfig() {
messageConfig = new MessageConfig(DMessage.class, new File(LANGUAGES, "english.yml"));
globalData = new GlobalData(new File(getDataFolder(), "data.yml"));
globalData = new GlobalData(this, new File(getDataFolder(), "data.yml"));
mainConfig = new MainConfig(this, new File(getDataFolder(), "config.yml"));
messageConfig = new MessageConfig(DMessage.class, new File(LANGUAGES, mainConfig.getLanguage() + ".yml"));
}

View File

@ -29,7 +29,7 @@ import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.block.Sign;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.entity.Player;
import org.bukkit.event.block.SignChangeEvent;
import org.bukkit.material.Attachable;
@ -49,6 +49,10 @@ public class GameSign extends JoinSign {
super(plugin, id, startSign, identifier, maxGroupsPerGame, startIfElementsAtLeast);
}
public GameSign(DungeonsXL plugin, World world, int id, ConfigurationSection config) {
super(plugin, world, id, config);
}
/**
* @return the attached game
*/
@ -117,20 +121,6 @@ public class GameSign extends JoinSign {
sign.update();
}
@Override
public void save(FileConfiguration config) {
String preString = "protections.gameSigns." + getWorld().getName() + "." + getId();
config.set(preString + ".x", startSign.getX());
config.set(preString + ".y", startSign.getY());
config.set(preString + ".z", startSign.getZ());
if (dungeon != null) {
config.set(preString + ".dungeon", dungeon.getName());
}
config.set(preString + ".maxGroupsPerGame", maxElements);
config.set(preString + ".startIfElementsAtLeast", startIfElementsAtLeast);
}
public void onPlayerInteract(Block block, Player player) {
DGroup dGroup = DGroup.getByPlayer(player);
if (dGroup == null) {

View File

@ -17,7 +17,11 @@
package de.erethon.dungeonsxl.global;
import de.erethon.commons.config.DREConfig;
import de.erethon.dungeonsxl.DungeonsXL;
import java.io.File;
import org.bukkit.Bukkit;
import org.bukkit.World;
import org.bukkit.configuration.ConfigurationSection;
/**
* Represents the global data.yml.
@ -26,11 +30,15 @@ import java.io.File;
*/
public class GlobalData extends DREConfig {
private DungeonsXL plugin;
public static final int CONFIG_VERSION = 2;
public GlobalData(File file) {
public GlobalData(DungeonsXL plugin, File file) {
super(file, CONFIG_VERSION);
this.plugin = plugin;
if (initialize) {
initialize();
}
@ -44,7 +52,27 @@ public class GlobalData extends DREConfig {
@Override
public void load() {
// Load?
for (World world : Bukkit.getWorlds()) {
ConfigurationSection gameSigns = config.getConfigurationSection("protections.gameSigns." + world.getName());
ConfigurationSection groupSigns = config.getConfigurationSection("protections.groupSigns." + world.getName());
if (gameSigns == null && groupSigns == null) {
continue;
}
int i = 0;
while (true) {
String key = String.valueOf("i");
if (gameSigns != null && gameSigns.contains(key)) {
new GameSign(plugin, world, i, gameSigns.getConfigurationSection(key));
}
if (groupSigns != null && groupSigns.contains(key)) {
new GroupSign(plugin, world, i, groupSigns.getConfigurationSection(key));
} else if ((gameSigns == null || !gameSigns.contains(key)) && (groupSigns == null || !groupSigns.contains(key))) {
break;
}
}
}
}
}

View File

@ -28,6 +28,7 @@ import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.block.Sign;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.event.block.SignChangeEvent;
@ -50,6 +51,11 @@ public class GroupSign extends JoinSign {
this.groupName = groupName;
}
public GroupSign(DungeonsXL plugin, World world, int id, ConfigurationSection config) {
super(plugin, world, id, config);
groupName = config.getString("groupName");
}
/**
* @return the attached group
*/
@ -120,17 +126,9 @@ public class GroupSign extends JoinSign {
@Override
public void save(FileConfiguration config) {
super.save(config);
String preString = "protections.groupSigns." + getWorld().getName() + "." + getId();
config.set(preString + ".x", startSign.getX());
config.set(preString + ".y", startSign.getY());
config.set(preString + ".z", startSign.getZ());
if (dungeon != null) {
config.set(preString + ".dungeon", dungeon.getName());
}
config.set(preString + ".groupName", groupName);
config.set(preString + ".maxPlayersPerGroup", maxElements);
config.set(preString + ".startIfElementsAtLeast", startIfElementsAtLeast);
}
public void onPlayerInteract(Block block, Player player) {

View File

@ -22,8 +22,10 @@ import de.erethon.dungeonsxl.dungeon.Dungeon;
import de.erethon.dungeonsxl.world.DResourceWorld;
import java.util.HashSet;
import java.util.Set;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.block.Sign;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.FileConfiguration;
/**
@ -61,6 +63,37 @@ public class JoinSign extends GlobalProtection {
update();
}
protected JoinSign(DungeonsXL plugin, World world, int id, ConfigurationSection config) {
super(plugin, world, id);
startSign = world.getBlockAt(config.getInt("x"), config.getInt("y"), config.getInt("z"));
String identifier = config.getString("dungeon");
dungeon = plugin.getDungeonCache().getByName(identifier);
if (dungeon == null) {
DResourceWorld resource = plugin.getDWorldCache().getResourceByName(identifier);
if (resource != null) {
dungeon = new Dungeon(plugin, resource);
}
}
verticalSigns = (int) Math.ceil((float) (1 + maxElements) / 4);
// LEGACY
if (config.contains("maxElements")) {
maxElements = config.getInt("maxElements");
} else if (config.contains("maxGroupsPerGame")) {
maxElements = config.getInt("maxGroupsPerGame");
} else if (config.contains("maxPlayersPerGroup")) {
maxElements = config.getInt("maxPlayersPerGroup");
}
if (startIfElementsAtLeast > 0 && startIfElementsAtLeast <= maxElements) {
startIfElementsAtLeast = config.getInt("startIfElementsAtLeast");
}
update();
}
/**
* @return the dungeon
*/
@ -159,6 +192,16 @@ public class JoinSign extends GlobalProtection {
@Override
public void save(FileConfiguration config) {
String preString = "protections.groupSigns." + getWorld().getName() + "." + getId();
config.set(preString + ".x", startSign.getX());
config.set(preString + ".y", startSign.getY());
config.set(preString + ".z", startSign.getZ());
if (dungeon != null) {
config.set(preString + ".dungeon", dungeon.getName());
}
config.set(preString + ".maxElements", maxElements);
config.set(preString + ".startIfElementsAtLeast", startIfElementsAtLeast);
}
}