Skip to content

Commit

Permalink
Seems working. Probably final 1.12.2 update, we will see. Fully enabl…
Browse files Browse the repository at this point in the history
…es world-specific configs. Some testing remains.
  • Loading branch information
ProgrammerDan committed Jul 20, 2018
1 parent fd82fc1 commit d10dee2
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 21 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,12 @@ I'm probably missing some other details but that's it for now.

* Better documentation

* Minecraft 1.13 support (will be 1.5.0)
* Minecraft 1.13 support (will be 1.5.0, and _will_ break your config. It will not be backwards compatible.)

### Feature Augment List:

**v1.4.2** Added full multiple world support. Standard config is used as default for any world that does not have a specific config. A new section, `worlds`
can be specified, each subsection is either the UUID or name of the world with a specific config. A single `blocks` subsection under the world identifier contains all the block configurations for that world. It is configured like the default, within that subsection. Check the configs for examples.
can be specified, each subsection is either the UUID or name of the world with a specific config. A single `blocks` subsection under the world identifier contains all the block configurations for that world. It is configured like the default, but within the world's blocks subsection. Check the configs for examples.

**v1.4.1** Live use of new tracker shows its disk use is much increased. Added a configuration option to explicitly disable it. Added config example of Command "drops" and some fixes.

Expand Down
57 changes: 40 additions & 17 deletions src/main/java/com/github/devotedmc/hiddenore/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.inventory.ItemStack;

import com.github.devotedmc.hiddenore.listeners.ConfigDeferralListener;

/**
* Someday it might be nice to refactor this to be a proper object.
*
Expand All @@ -31,6 +33,7 @@ public final class Config {
public boolean listDrops;
public boolean ignoreSilktouch;
public Map<UUID, Map<String, List<BlockConfig>>> blockConfigs;
public Map<String, Map<String, List<BlockConfig>>> preloadBlockConfigs;
public Map<String, NameConfig> prettyNames;
public Map<String, PlayerStateConfig> stateMasterList;

Expand All @@ -51,6 +54,7 @@ public final class Config {

private Config() {
blockConfigs = new HashMap<UUID, Map<String, List<BlockConfig>>>();
preloadBlockConfigs = new HashMap<String, Map<String, List<BlockConfig>>>();
prettyNames = new HashMap<String, NameConfig>();
stateMasterList = new HashMap<String, PlayerStateConfig>();
trackFileName = "tracking.dat";
Expand Down Expand Up @@ -175,7 +179,9 @@ public static void doLoad() {

ConfigurationSection blocks = file.getConfigurationSection("blocks");
if (blocks != null) { // default or legacy
grabBlocks(null, blocks, i);
Map<String, List<BlockConfig>> worldBlockConfigs = new HashMap<String, List<BlockConfig>>();
grabBlocks("default", worldBlockConfigs, blocks, i);
i.blockConfigs.put(null, worldBlockConfigs);
}

ConfigurationSection worlds = file.getConfigurationSection("worlds");
Expand All @@ -197,34 +203,53 @@ public static void doLoad() {
try {
worlduid = HiddenOre.getPlugin().getServer().getWorld(world).getUID();
} catch (Exception f) {
System.out.println("World not defined by Name; unable to match " + world + " with actual world. Skipping.");
continue;
System.out.println("World not defined by Name; unable to match " + world + " with loaded world.");
}
}

Map<String, List<BlockConfig>> worldBlockConfigs = null;

if (worlduid == null) {
System.err.println("Unable to match world " + world + " with actual world. Skipping.");
System.err.println("Unable to match world " + world + " with loaded world, registering for post-load.");
worldBlockConfigs = i.preloadBlockConfigs.get(world);
} else {
ConfigurationSection worldConfig = worlds.getConfigurationSection(world);
if (worldConfig != null) {
ConfigurationSection worldBlocks = worldConfig.getConfigurationSection("blocks");
if (worldBlocks != null) {
grabBlocks(worlduid, worldBlocks, i);
}
worldBlockConfigs = i.blockConfigs.get(worlduid);
}

if (worldBlockConfigs == null) {
worldBlockConfigs = new HashMap<String, List<BlockConfig>>();
}

ConfigurationSection worldConfig = worlds.getConfigurationSection(world);
if (worldConfig != null) {
ConfigurationSection worldBlocks = worldConfig.getConfigurationSection("blocks");
if (worldBlocks != null) {
grabBlocks(world, worldBlockConfigs, worldBlocks, i);
}
}

if (worlduid == null) {
i.preloadBlockConfigs.put(world, worldBlockConfigs);
} else {
i.blockConfigs.put(worlduid, worldBlockConfigs);
}
}
}

if (i.preloadBlockConfigs.size() > 0) {
// some world configs won't immediately resolve!
// register a listener for world init / loading to check if we can resolve them!

HiddenOre.getPlugin().getServer().getPluginManager()
.registerEvents(new ConfigDeferralListener(), HiddenOre.getPlugin());

}

instance = i;
}

private static void grabBlocks(UUID world, ConfigurationSection blocks, Config i) {
private static void grabBlocks(String world, Map<String, List<BlockConfig>> worldBlockConfigs, ConfigurationSection blocks, Config i) {
if (blocks != null) {
Map<String, List<BlockConfig>> worldBlockConfigs = i.blockConfigs.get(world);
if (worldBlockConfigs == null) {
worldBlockConfigs = new HashMap<String, List<BlockConfig>>();
}
for (String sourceBlock : blocks.getKeys(false)) {
HiddenOre.getPlugin().getLogger().info("Loading config for " + sourceBlock + " for world " + world);
ConfigurationSection block = blocks.getConfigurationSection(sourceBlock);
Expand Down Expand Up @@ -290,8 +315,6 @@ private static void grabBlocks(UUID world, ConfigurationSection blocks, Config i

worldBlockConfigs.put(cBlockName, bclist);//sourceBlock, bclist);
}

i.blockConfigs.put(world, worldBlockConfigs);
} else {
HiddenOre.getPlugin().getLogger().info("No blocks specified (Why are you using this plugin?)");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package com.github.devotedmc.hiddenore.listeners;

import java.util.UUID;
import java.util.logging.Level;

import org.bukkit.World;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.world.WorldInitEvent;
import org.bukkit.event.world.WorldLoadEvent;

import com.github.devotedmc.hiddenore.Config;
import com.github.devotedmc.hiddenore.HiddenOre;

/**
* For world ore clearing to work, the plugin has to launch on startup.
* This means however that worlds aren't loaded yet, which breaks the tight-binding
* goal for world-specific configs.
*
* This infrastructure allows soft-binding during first load and eventual resolution of
* binding once the worlds are loaded.
*
* @author ProgrammerDan
*/
public class ConfigDeferralListener implements Listener {

@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
public void handleWorldInitEvent(WorldInitEvent init) {
checkPreLoad(init.getWorld());
}

@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
public void handleWorldLoadEvent(WorldLoadEvent init) {
checkPreLoad(init.getWorld());
}

private void checkPreLoad(World world) {
try {
String found = null;
if (Config.instance.preloadBlockConfigs.size() > 0) {
for (String key : Config.instance.preloadBlockConfigs.keySet()) {
if (world.getName().equals(key)) {
HiddenOre.getPlugin().getLogger().log( Level.INFO, "Found a match during loading for {0}, bound config to world", key);
Config.instance.blockConfigs.put(world.getUID(), Config.instance.preloadBlockConfigs.get(key));
found = key;
break;
}

try {
UUID worldkey = UUID.fromString(key);
if (worldkey != null && world.getUID().equals(worldkey)) {
HiddenOre.getPlugin().getLogger().log( Level.INFO, "Found a match during loading for {0}, bound config to world", key);
Config.instance.blockConfigs.put(world.getUID(), Config.instance.preloadBlockConfigs.get(key));
found = key;
break;
}
} catch (IllegalArgumentException e) {
// no match.
}
}
}

if (found != null) {
Config.instance.preloadBlockConfigs.remove(found);
}

} catch (Exception e) {
HiddenOre.getPlugin().getLogger().log(Level.WARNING, "Tried to check for dangling preloaded configs, failed", e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public WorldGenerationListener(ConfigurationSection config) {
*/
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void postGenerationOreClear(ChunkPopulateEvent event) {
if (toReplace == null || replaceWith == null || worldName == null) {
if (toReplace == null || replaceWith == null || (worldName == null && worldUUID == null) ) {
return;
}

Expand Down Expand Up @@ -165,7 +165,7 @@ private void clear(Chunk chunk) {
HiddenOre.getPlugin().getLogger().log(Level.INFO, "Replaced {0} blocks at {1}, {2}", new Object[]{rep, chunk.getX(), chunk.getZ()});
}
}

static BlockFace[] faces = new BlockFace[] {BlockFace.UP,BlockFace.DOWN,BlockFace.NORTH,BlockFace.SOUTH,BlockFace.EAST,BlockFace.WEST};
private void generateCaveOres(Chunk chunk) {
UUID world = chunk.getWorld().getUID();
Expand Down

0 comments on commit d10dee2

Please sign in to comment.