Skip to content

Commit

Permalink
New config system: WorldMap 1.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Yeregorix committed May 10, 2021
1 parent f9aeb3e commit c29c8d6
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 79 deletions.
3 changes: 3 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ repositories {
dependencies {
compile 'org.spongepowered:spongecommon:1.12.2-7.3.0:dev'
compile 'net.smoofyuniverse:oreupdater:1.0.1'
compile 'net.smoofyuniverse:worldmap:1.0.0'
}

jar {
Expand All @@ -99,9 +100,11 @@ shadowJar {
dependencies {
include dependency('net.smoofyuniverse:oreapi')
include dependency('net.smoofyuniverse:oreupdater')
include dependency('net.smoofyuniverse:worldmap')
}

relocate 'net.smoofyuniverse.ore', 'net.smoofyuniverse.superpiston.ore'
relocate 'net.smoofyuniverse.map', 'net.smoofyuniverse.autopickup.map'

exclude "dummyThing"
afterEvaluate {
Expand Down
97 changes: 36 additions & 61 deletions src/main/java/net/smoofyuniverse/superpiston/SuperPiston.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,21 @@
package net.smoofyuniverse.superpiston;

import com.google.inject.Inject;
import net.smoofyuniverse.map.WorldMap;
import net.smoofyuniverse.map.WorldMapLoader;
import net.smoofyuniverse.ore.update.UpdateChecker;
import net.smoofyuniverse.superpiston.config.world.WorldConfig;
import net.smoofyuniverse.superpiston.event.WorldEventListener;
import net.smoofyuniverse.superpiston.config.world.WorldConfig.Immutable;
import net.smoofyuniverse.superpiston.event.PistonListener;
import net.smoofyuniverse.superpiston.impl.internal.InternalServer;
import net.smoofyuniverse.superpiston.util.IOUtil;
import ninja.leaping.configurate.ConfigurationNode;
import ninja.leaping.configurate.commented.CommentedConfigurationNode;
import ninja.leaping.configurate.hocon.HoconConfigurationLoader;
import ninja.leaping.configurate.loader.ConfigurationLoader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.spongepowered.api.Game;
import org.spongepowered.api.config.ConfigDir;
import org.spongepowered.api.event.Listener;
import org.spongepowered.api.event.game.GameReloadEvent;
import org.spongepowered.api.event.game.state.GameInitializationEvent;
import org.spongepowered.api.event.game.state.GamePreInitializationEvent;
import org.spongepowered.api.event.game.state.GameStartedServerEvent;
import org.spongepowered.api.plugin.Plugin;
Expand All @@ -47,11 +47,6 @@
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

import static net.smoofyuniverse.superpiston.util.MathUtil.clamp;

@Plugin(id = "superpiston", name = "SuperPiston", version = "1.0.7", authors = "Yeregorix", description = "Allows to modify vanilla pistons")
public class SuperPiston {
Expand All @@ -66,9 +61,8 @@ public class SuperPiston {
@Inject
private PluginContainer container;

private Path worldConfigsDir;

private final Map<String, WorldConfig.Immutable> configs = new HashMap<>();
private WorldMapLoader<Immutable> configMapLoader;
private WorldMap<Immutable> configMap;

public SuperPiston() {
if (instance != null)
Expand All @@ -78,61 +72,46 @@ public SuperPiston() {

@Listener
public void onGamePreInit(GamePreInitializationEvent e) {
this.worldConfigsDir = this.configDir.resolve("worlds");
try {
Files.createDirectories(this.worldConfigsDir);
Files.createDirectories(this.configDir);
} catch (IOException ignored) {
}

this.game.getEventManager().registerListeners(this, new WorldEventListener());

this.game.getEventManager().registerListeners(this, new UpdateChecker(LOGGER, this.container,
createConfigLoader(this.configDir.resolve("update.conf")), "Yeregorix", "SuperPiston"));
this.configMapLoader = new WorldMapLoader<WorldConfig.Immutable>(LOGGER,
IOUtil.createConfigLoader(this.configDir.resolve("map.conf")),
this.configDir.resolve("configs"), WorldConfig.VANILLA) {
@Override
protected WorldConfig.Immutable loadConfig(Path file) throws Exception {
return WorldConfig.load(file).toImmutable();
}
};
}

@Listener
public void onGameReload(GameReloadEvent e) {
this.configs.clear();
this.game.getServer().getWorlds().forEach(this::loadConfig);
}

public void loadConfig(World world) {
String name = world.getName();

LOGGER.info("Loading configuration for world " + name + " ..");
try {
Path file = this.worldConfigsDir.resolve(name + ".conf");
ConfigurationLoader<CommentedConfigurationNode> loader = createConfigLoader(file);

CommentedConfigurationNode root = loader.load();
int version = root.getNode("Version").getInt();
if ((version > WorldConfig.CURRENT_VERSION || version < WorldConfig.MINIMUM__VERSION) && IOUtil.backupFile(file)) {
LOGGER.info("Your config version is not supported. A new one will be generated.");
root = loader.createEmptyNode();
}
public void onGameInit(GameInitializationEvent e) {
loadConfigs();

ConfigurationNode cfgNode = root.getNode("Config");
WorldConfig cfg = cfgNode.getValue(WorldConfig.TOKEN, new WorldConfig());
this.game.getEventManager().registerListeners(this, new PistonListener(this));

if (cfg.blockReactions == null)
cfg.blockReactions = new HashMap<>();

if (cfg.stickyBlocks == null)
cfg.stickyBlocks = new HashMap<>();

cfg.maxBlocks = clamp(cfg.maxBlocks, 1, 500);

version = WorldConfig.CURRENT_VERSION;
root.getNode("Version").setValue(version);
cfgNode.setValue(WorldConfig.TOKEN, cfg);
loader.save(root);
this.game.getEventManager().registerListeners(this, new UpdateChecker(LOGGER, this.container,
IOUtil.createConfigLoader(this.configDir.resolve("update.conf")), "Yeregorix", "SuperPiston"));
}

this.configs.put(name, cfg.toImmutable());
} catch (Exception e) {
LOGGER.error("Failed to load configuration for world " + name, e);
private void loadConfigs() {
if (Files.exists(this.configDir.resolve("worlds")) && Files.notExists(this.configDir.resolve("map.conf"))) {
LOGGER.info("Updating config directory structure ...");
Path worlds = IOUtil.backup(this.configDir).orElse(this.configDir).resolve("worlds");
this.configMap = this.configMapLoader.importWorlds(worlds);
} else {
this.configMap = this.configMapLoader.load();
}
}

@Listener
public void onGameReload(GameReloadEvent e) {
loadConfigs();
}

@Listener
public void onServerStarted(GameStartedServerEvent e) {
if (this.game.getServer() instanceof InternalServer)
Expand All @@ -141,12 +120,8 @@ public void onServerStarted(GameStartedServerEvent e) {
LOGGER.error("!!WARNING!! SuperPiston was not loaded correctly. Be sure that the jar file is at the root of your mods folder!");
}

public ConfigurationLoader<CommentedConfigurationNode> createConfigLoader(Path file) {
return HoconConfigurationLoader.builder().setPath(file).build();
}

public Optional<WorldConfig.Immutable> getConfig(World world) {
return Optional.ofNullable(this.configs.get(world.getName()));
public WorldConfig.Immutable getConfig(World world) {
return this.configMap.get(world.getProperties());
}

public PluginContainer getContainer() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,29 +24,62 @@

import com.google.common.collect.ImmutableMap;
import com.google.common.reflect.TypeToken;
import net.smoofyuniverse.superpiston.SuperPiston;
import net.smoofyuniverse.superpiston.api.structure.calculator.DefaultStructureCalculator.MovementReaction;
import net.smoofyuniverse.superpiston.util.IOUtil;
import ninja.leaping.configurate.ConfigurationNode;
import ninja.leaping.configurate.commented.CommentedConfigurationNode;
import ninja.leaping.configurate.loader.ConfigurationLoader;
import ninja.leaping.configurate.objectmapping.ObjectMappingException;
import ninja.leaping.configurate.objectmapping.Setting;
import ninja.leaping.configurate.objectmapping.serialize.ConfigSerializable;
import org.spongepowered.api.block.BlockState;

import java.io.IOException;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.Map;

import static net.smoofyuniverse.superpiston.util.MathUtil.clamp;

@ConfigSerializable
public class WorldConfig {
public static final int CURRENT_VERSION = 1, MINIMUM__VERSION = 1;
public static final TypeToken<WorldConfig> TOKEN = TypeToken.of(WorldConfig.class);
public static final Immutable VANILLA = new WorldConfig().toImmutable();

@Setting(value = "BlockReactions")
public Map<BlockState, MovementReaction> blockReactions;
public Map<BlockState, MovementReaction> blockReactions = new HashMap<>();
@Setting(value = "StickyBlocks")
public Map<BlockState, Boolean> stickyBlocks;
public Map<BlockState, Boolean> stickyBlocks = new HashMap<>();
@Setting(value = "MaxBlocks")
public int maxBlocks = 12;

public Immutable toImmutable() {
return new Immutable(this.blockReactions, this.stickyBlocks, this.maxBlocks);
}

public static WorldConfig load(Path file) throws IOException, ObjectMappingException {
ConfigurationLoader<CommentedConfigurationNode> loader = IOUtil.createConfigLoader(file);

CommentedConfigurationNode root = loader.load();
int version = root.getNode("Version").getInt();
if ((version > CURRENT_VERSION || version < MINIMUM__VERSION) && IOUtil.backup(file).isPresent()) {
SuperPiston.LOGGER.info("Your config version is not supported. A new one will be generated.");
root = loader.createEmptyNode();
}

ConfigurationNode cfgNode = root.getNode("Config");
WorldConfig cfg = cfgNode.getValue(TOKEN, new WorldConfig());

cfg.maxBlocks = clamp(cfg.maxBlocks, 1, 500);

root.getNode("Version").setValue(CURRENT_VERSION);
cfgNode.setValue(TOKEN, cfg);
loader.save(root);
return cfg;
}

public static class Immutable {
public final Map<BlockState, MovementReaction> blockReactions;
public final Map<BlockState, Boolean> stickyBlocks;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,19 @@
import net.smoofyuniverse.superpiston.impl.calculator.SuperPistonStructureCalculator;
import org.spongepowered.api.event.Listener;
import org.spongepowered.api.event.Order;
import org.spongepowered.api.event.world.LoadWorldEvent;
import org.spongepowered.api.world.World;

public class WorldEventListener {
public class PistonListener {
private final SuperPiston plugin;

@Listener
public void onLoadWorld(LoadWorldEvent e) {
SuperPiston.get().loadConfig(e.getTargetWorld());
public PistonListener(SuperPiston plugin) {
this.plugin = plugin;
}

@Listener(order = Order.FIRST)
public void onPreStructureCalculation(PistonStructureCalculationEvent.Pre e) {
World world = e.getTargetWorld();
SuperPiston.get().getConfig(world).ifPresent(config ->
e.setCalculator(new SuperPistonStructureCalculator(world, e.getPiston(), e.getPistonDirection(), e.getPistonMovement(), config)));
e.setCalculator(new SuperPistonStructureCalculator(world,
e.getPiston(), e.getPistonDirection(), e.getPistonMovement(), this.plugin.getConfig(world)));
}
}
34 changes: 25 additions & 9 deletions src/main/java/net/smoofyuniverse/superpiston/util/IOUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,24 +22,40 @@

package net.smoofyuniverse.superpiston.util;

import net.smoofyuniverse.superpiston.SuperPiston;
import ninja.leaping.configurate.commented.CommentedConfigurationNode;
import ninja.leaping.configurate.hocon.HoconConfigurationLoader;
import ninja.leaping.configurate.loader.ConfigurationLoader;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Optional;

public class IOUtil {

public static boolean backupFile(Path file) throws IOException {
public static ConfigurationLoader<CommentedConfigurationNode> createConfigLoader(Path file) {
return HoconConfigurationLoader.builder().setPath(file).build();
}

public static Optional<Path> backup(Path file) {
if (!Files.exists(file))
return false;
return Optional.empty();

String fn = file.getFileName() + ".backup";
Path backup = null;
for (int i = 0; i < 100; i++) {
backup = file.resolveSibling(fn + i);
if (!Files.exists(backup))
break;
Path backup;
int i = 0;
while (Files.exists(backup = file.resolveSibling(fn + i))) {
i++;
}
Files.move(file, backup);
return true;

try {
Files.move(file, backup);
} catch (IOException e) {
SuperPiston.LOGGER.warn("Failed to backup: " + backup, e);
return Optional.empty();
}

return Optional.of(backup);
}
}

0 comments on commit c29c8d6

Please sign in to comment.