Fix loading game rules

This commit is contained in:
Daniel Saukel 2020-03-19 15:14:07 +01:00
parent 0212a865f8
commit d0042949fc
7 changed files with 49 additions and 54 deletions

View File

@ -144,13 +144,6 @@ public interface Dungeon {
*/ */
GameRuleContainer getOverrideValues(); GameRuleContainer getOverrideValues();
/**
* Sets the game rule container whose values override all values of the game rule containers of the dungeon's maps.
*
* @param rules the override values
*/
void setOverrideValues(GameRuleContainer rules);
/** /**
* The values from this game rule container will be overriden by values of the game rule containers of the dungeon's maps. They will however still override * The values from this game rule container will be overriden by values of the game rule containers of the dungeon's maps. They will however still override
* the values from the main config. * the values from the main config.
@ -159,14 +152,6 @@ public interface Dungeon {
*/ */
GameRuleContainer getDefaultValues(); GameRuleContainer getDefaultValues();
/**
* Sets the game rule container whose values will be overriden by values of the game rule containers of the dungeon's maps. They will however still override
* the values from the main config.
*
* @param rules the default values
*/
void setDefaultValues(GameRuleContainer rules);
/** /**
* Returns true if the floor is either in the floors list or the start / end floor. * Returns true if the floor is either in the floors list or the start / end floor.
* *

View File

@ -309,7 +309,7 @@ public class GameRule<V> {
/** /**
* Messages; also to be created with /dxl msg * Messages; also to be created with /dxl msg
*/ */
public static final GameRule<Map<Integer, String>> MESSAGES = new MapGameRule<>("msgs", new HashMap<>(), (api, value) -> { public static final GameRule<Map<Integer, String>> MESSAGES = new MapGameRule<>("messages", new HashMap<>(), (api, value) -> {
if (!(value instanceof ConfigurationSection)) { if (!(value instanceof ConfigurationSection)) {
return null; return null;
} }
@ -463,4 +463,9 @@ public class GameRule<V> {
writeTo.setState(this, overridingValue != null ? overridingValue : subsidiaryValue); writeTo.setState(this, overridingValue != null ? overridingValue : subsidiaryValue);
} }
@Override
public String toString() {
return "GameRule{key=" + key + "}";
}
} }

View File

@ -116,4 +116,9 @@ public class GameRuleContainer {
} }
} }
@Override
public String toString() {
return "GameRuleContainer{" + rules + "}";
}
} }

View File

@ -82,6 +82,7 @@ import de.erethon.dungeonsxl.util.PlaceholderUtil;
import de.erethon.dungeonsxl.world.DResourceWorld; import de.erethon.dungeonsxl.world.DResourceWorld;
import de.erethon.dungeonsxl.world.DWorldListener; import de.erethon.dungeonsxl.world.DWorldListener;
import de.erethon.dungeonsxl.world.LWCIntegration; import de.erethon.dungeonsxl.world.LWCIntegration;
import de.erethon.dungeonsxl.world.WorldConfig;
import de.erethon.dungeonsxl.world.WorldUnloadTask; import de.erethon.dungeonsxl.world.WorldUnloadTask;
import de.erethon.vignette.api.VignetteAPI; import de.erethon.vignette.api.VignetteAPI;
import java.io.File; import java.io.File;
@ -155,12 +156,26 @@ public class DungeonsXL extends DREPlugin implements DungeonsAPI {
@Override @Override
public void add(String key, GameRule rule) { public void add(String key, GameRule rule) {
super.add(key, rule); super.add(key, rule);
if (loaded) {
GameRuleContainer.DEFAULT_VALUES.setState(rule, rule.getDefaultValue()); GameRuleContainer.DEFAULT_VALUES.setState(rule, rule.getDefaultValue());
mainConfig.getDefaultWorldConfig().updateGameRule(rule);
for (Dungeon apiDungeon : dungeonRegistry) {
DDungeon dungeon = ((DDungeon) apiDungeon);
if (dungeon.isMultiFloor()) {
dungeon.getConfig().getDefaultValues().updateGameRule(rule);
dungeon.getConfig().getOverrideValues().updateGameRule(rule);
} else {
WorldConfig cfg = ((DResourceWorld) dungeon.getMap()).getConfig(false);
cfg.updateGameRule(rule);
}
}
dungeonRegistry.forEach(Dungeon::setupRules);
}
} }
} }
private boolean loadingWorld; private boolean loaded, loadingWorld;
private GlobalData globalData; private GlobalData globalData;
private MainConfig mainConfig; private MainConfig mainConfig;
@ -287,6 +302,10 @@ public class DungeonsXL extends DREPlugin implements DungeonsAPI {
signRegistry.add("WAVE", WaveSign.class); signRegistry.add("WAVE", WaveSign.class);
Bukkit.getPluginManager().registerEvents(new DSignListener(this), this); Bukkit.getPluginManager().registerEvents(new DSignListener(this), this);
for (GameRule rule : GameRule.VALUES) {
gameRuleRegistry.add(rule.getKey(), rule);
}
// Maps // Maps
for (File file : MAPS.listFiles()) { for (File file : MAPS.listFiles()) {
if (file.isDirectory() && !file.getName().equals(".raw")) { if (file.isDirectory() && !file.getName().equals(".raw")) {
@ -323,10 +342,6 @@ public class DungeonsXL extends DREPlugin implements DungeonsAPI {
globalData = new GlobalData(this, new File(getDataFolder(), "data.yml")); globalData = new GlobalData(this, new File(getDataFolder(), "data.yml"));
globalData.load(); globalData.load();
for (GameRule rule : GameRule.VALUES) {
gameRuleRegistry.add(rule.getKey(), rule);
}
// Mobs - Supported providers // Mobs - Supported providers
for (ExternalMobPlugin externalMobPlugin : ExternalMobPlugin.values()) { for (ExternalMobPlugin externalMobPlugin : ExternalMobPlugin.values()) {
externalMobProviderRegistry.add(externalMobPlugin.getIdentifier(), externalMobPlugin); externalMobProviderRegistry.add(externalMobPlugin.getIdentifier(), externalMobPlugin);
@ -375,6 +390,7 @@ public class DungeonsXL extends DREPlugin implements DungeonsAPI {
} }
dCommands.register(this); dCommands.register(this);
loaded = true;
} }
public void saveData() { public void saveData() {

View File

@ -149,21 +149,11 @@ public class DDungeon implements Dungeon {
return config.getOverrideValues(); return config.getOverrideValues();
} }
@Override
public void setOverrideValues(GameRuleContainer rules) {
config.setOverrideValues(rules);
}
@Override @Override
public GameRuleContainer getDefaultValues() { public GameRuleContainer getDefaultValues() {
return config.getDefaultValues(); return config.getDefaultValues();
} }
@Override
public void setDefaultValues(GameRuleContainer rules) {
config.setDefaultValues(rules);
}
@Override @Override
public GameRuleContainer getRules() { public GameRuleContainer getRules() {
return rules; return rules;

View File

@ -18,7 +18,6 @@ package de.erethon.dungeonsxl.dungeon;
import de.erethon.commons.config.DREConfig; import de.erethon.commons.config.DREConfig;
import de.erethon.dungeonsxl.DungeonsXL; import de.erethon.dungeonsxl.DungeonsXL;
import de.erethon.dungeonsxl.api.dungeon.GameRuleContainer;
import de.erethon.dungeonsxl.api.world.ResourceWorld; import de.erethon.dungeonsxl.api.world.ResourceWorld;
import de.erethon.dungeonsxl.world.WorldConfig; import de.erethon.dungeonsxl.world.WorldConfig;
import java.io.File; import java.io.File;
@ -42,8 +41,8 @@ public class DungeonConfig extends DREConfig {
private List<ResourceWorld> floors = new ArrayList<>(); private List<ResourceWorld> floors = new ArrayList<>();
private int floorCount; private int floorCount;
private boolean removeWhenPlayed; private boolean removeWhenPlayed;
private GameRuleContainer overrideValues; private WorldConfig overrideValues;
private GameRuleContainer defaultValues; private WorldConfig defaultValues;
public DungeonConfig(DungeonsXL plugin, File file) { public DungeonConfig(DungeonsXL plugin, File file) {
super(file, CONFIG_VERSION); super(file, CONFIG_VERSION);
@ -100,22 +99,14 @@ public class DungeonConfig extends DREConfig {
this.removeWhenPlayed = removeWhenPlayed; this.removeWhenPlayed = removeWhenPlayed;
} }
public GameRuleContainer getOverrideValues() { public WorldConfig getOverrideValues() {
return overrideValues; return overrideValues;
} }
public void setOverrideValues(GameRuleContainer worldConfig) { public WorldConfig getDefaultValues() {
overrideValues = worldConfig;
}
public GameRuleContainer getDefaultValues() {
return defaultValues; return defaultValues;
} }
public void setDefaultValues(GameRuleContainer worldConfig) {
defaultValues = worldConfig;
}
public boolean containsFloor(ResourceWorld resource) { public boolean containsFloor(ResourceWorld resource) {
return floors.contains(resource) || startFloor.equals(resource) || endFloor.equals(resource); return floors.contains(resource) || startFloor.equals(resource) || endFloor.equals(resource);
} }

View File

@ -39,6 +39,7 @@ public class WorldConfig extends GameRuleContainer {
private DungeonsXL plugin; private DungeonsXL plugin;
private File file; private File file;
private ConfigurationSection config;
private List<String> invitedPlayers = new ArrayList<>(); private List<String> invitedPlayers = new ArrayList<>();
private Environment worldEnvironment; private Environment worldEnvironment;
@ -51,21 +52,23 @@ public class WorldConfig extends GameRuleContainer {
this(plugin); this(plugin);
this.file = file; this.file = file;
FileConfiguration configFile = YamlConfiguration.loadConfiguration(file); config = YamlConfiguration.loadConfiguration(file);
load(configFile); load();
} }
public WorldConfig(DungeonsXL plugin, ConfigurationSection config) { public WorldConfig(DungeonsXL plugin, ConfigurationSection config) {
this(plugin); this(plugin);
load(config); this.config = config;
load();
} }
// Load & Save public void load() {
public void load(ConfigurationSection config) { plugin.getGameRuleRegistry().forEach(this::updateGameRule);
for (GameRule rule : plugin.getGameRuleRegistry()) {
rule.fromConfig(plugin, this, config);
} }
public void updateGameRule(GameRule rule) {
rule.fromConfig(plugin, this, config);
} }
public void save() { public void save() {
@ -76,7 +79,7 @@ public class WorldConfig extends GameRuleContainer {
if (getState(GameRule.MESSAGES) != null) { if (getState(GameRule.MESSAGES) != null) {
for (int msgs : getState(GameRule.MESSAGES).keySet()) { for (int msgs : getState(GameRule.MESSAGES).keySet()) {
configFile.set("message." + msgs, getState(GameRule.MESSAGES).get(msgs)); configFile.set("messages." + msgs, getState(GameRule.MESSAGES).get(msgs));
} }
} }