Skip to content

Commit d10dee2

Browse files
committed
Seems working. Probably final 1.12.2 update, we will see. Fully enables world-specific configs. Some testing remains.
1 parent fd82fc1 commit d10dee2

File tree

4 files changed

+116
-21
lines changed

4 files changed

+116
-21
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,12 @@ I'm probably missing some other details but that's it for now.
6666

6767
* Better documentation
6868

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

7171
### Feature Augment List:
7272

7373
**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`
74-
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.
74+
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.
7575

7676
**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.
7777

src/main/java/com/github/devotedmc/hiddenore/Config.java

Lines changed: 40 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
import org.bukkit.configuration.file.FileConfiguration;
1414
import org.bukkit.inventory.ItemStack;
1515

16+
import com.github.devotedmc.hiddenore.listeners.ConfigDeferralListener;
17+
1618
/**
1719
* Someday it might be nice to refactor this to be a proper object.
1820
*
@@ -31,6 +33,7 @@ public final class Config {
3133
public boolean listDrops;
3234
public boolean ignoreSilktouch;
3335
public Map<UUID, Map<String, List<BlockConfig>>> blockConfigs;
36+
public Map<String, Map<String, List<BlockConfig>>> preloadBlockConfigs;
3437
public Map<String, NameConfig> prettyNames;
3538
public Map<String, PlayerStateConfig> stateMasterList;
3639

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

5255
private Config() {
5356
blockConfigs = new HashMap<UUID, Map<String, List<BlockConfig>>>();
57+
preloadBlockConfigs = new HashMap<String, Map<String, List<BlockConfig>>>();
5458
prettyNames = new HashMap<String, NameConfig>();
5559
stateMasterList = new HashMap<String, PlayerStateConfig>();
5660
trackFileName = "tracking.dat";
@@ -175,7 +179,9 @@ public static void doLoad() {
175179

176180
ConfigurationSection blocks = file.getConfigurationSection("blocks");
177181
if (blocks != null) { // default or legacy
178-
grabBlocks(null, blocks, i);
182+
Map<String, List<BlockConfig>> worldBlockConfigs = new HashMap<String, List<BlockConfig>>();
183+
grabBlocks("default", worldBlockConfigs, blocks, i);
184+
i.blockConfigs.put(null, worldBlockConfigs);
179185
}
180186

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

210+
Map<String, List<BlockConfig>> worldBlockConfigs = null;
211+
205212
if (worlduid == null) {
206-
System.err.println("Unable to match world " + world + " with actual world. Skipping.");
213+
System.err.println("Unable to match world " + world + " with loaded world, registering for post-load.");
214+
worldBlockConfigs = i.preloadBlockConfigs.get(world);
207215
} else {
208-
ConfigurationSection worldConfig = worlds.getConfigurationSection(world);
209-
if (worldConfig != null) {
210-
ConfigurationSection worldBlocks = worldConfig.getConfigurationSection("blocks");
211-
if (worldBlocks != null) {
212-
grabBlocks(worlduid, worldBlocks, i);
213-
}
216+
worldBlockConfigs = i.blockConfigs.get(worlduid);
217+
}
218+
219+
if (worldBlockConfigs == null) {
220+
worldBlockConfigs = new HashMap<String, List<BlockConfig>>();
221+
}
222+
223+
ConfigurationSection worldConfig = worlds.getConfigurationSection(world);
224+
if (worldConfig != null) {
225+
ConfigurationSection worldBlocks = worldConfig.getConfigurationSection("blocks");
226+
if (worldBlocks != null) {
227+
grabBlocks(world, worldBlockConfigs, worldBlocks, i);
214228
}
215229
}
230+
231+
if (worlduid == null) {
232+
i.preloadBlockConfigs.put(world, worldBlockConfigs);
233+
} else {
234+
i.blockConfigs.put(worlduid, worldBlockConfigs);
235+
}
216236
}
217237
}
218238

239+
if (i.preloadBlockConfigs.size() > 0) {
240+
// some world configs won't immediately resolve!
241+
// register a listener for world init / loading to check if we can resolve them!
242+
243+
HiddenOre.getPlugin().getServer().getPluginManager()
244+
.registerEvents(new ConfigDeferralListener(), HiddenOre.getPlugin());
245+
246+
}
247+
219248
instance = i;
220249
}
221250

222-
private static void grabBlocks(UUID world, ConfigurationSection blocks, Config i) {
251+
private static void grabBlocks(String world, Map<String, List<BlockConfig>> worldBlockConfigs, ConfigurationSection blocks, Config i) {
223252
if (blocks != null) {
224-
Map<String, List<BlockConfig>> worldBlockConfigs = i.blockConfigs.get(world);
225-
if (worldBlockConfigs == null) {
226-
worldBlockConfigs = new HashMap<String, List<BlockConfig>>();
227-
}
228253
for (String sourceBlock : blocks.getKeys(false)) {
229254
HiddenOre.getPlugin().getLogger().info("Loading config for " + sourceBlock + " for world " + world);
230255
ConfigurationSection block = blocks.getConfigurationSection(sourceBlock);
@@ -290,8 +315,6 @@ private static void grabBlocks(UUID world, ConfigurationSection blocks, Config i
290315

291316
worldBlockConfigs.put(cBlockName, bclist);//sourceBlock, bclist);
292317
}
293-
294-
i.blockConfigs.put(world, worldBlockConfigs);
295318
} else {
296319
HiddenOre.getPlugin().getLogger().info("No blocks specified (Why are you using this plugin?)");
297320
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package com.github.devotedmc.hiddenore.listeners;
2+
3+
import java.util.UUID;
4+
import java.util.logging.Level;
5+
6+
import org.bukkit.World;
7+
import org.bukkit.event.EventHandler;
8+
import org.bukkit.event.EventPriority;
9+
import org.bukkit.event.Listener;
10+
import org.bukkit.event.world.WorldInitEvent;
11+
import org.bukkit.event.world.WorldLoadEvent;
12+
13+
import com.github.devotedmc.hiddenore.Config;
14+
import com.github.devotedmc.hiddenore.HiddenOre;
15+
16+
/**
17+
* For world ore clearing to work, the plugin has to launch on startup.
18+
* This means however that worlds aren't loaded yet, which breaks the tight-binding
19+
* goal for world-specific configs.
20+
*
21+
* This infrastructure allows soft-binding during first load and eventual resolution of
22+
* binding once the worlds are loaded.
23+
*
24+
* @author ProgrammerDan
25+
*/
26+
public class ConfigDeferralListener implements Listener {
27+
28+
@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
29+
public void handleWorldInitEvent(WorldInitEvent init) {
30+
checkPreLoad(init.getWorld());
31+
}
32+
33+
@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
34+
public void handleWorldLoadEvent(WorldLoadEvent init) {
35+
checkPreLoad(init.getWorld());
36+
}
37+
38+
private void checkPreLoad(World world) {
39+
try {
40+
String found = null;
41+
if (Config.instance.preloadBlockConfigs.size() > 0) {
42+
for (String key : Config.instance.preloadBlockConfigs.keySet()) {
43+
if (world.getName().equals(key)) {
44+
HiddenOre.getPlugin().getLogger().log( Level.INFO, "Found a match during loading for {0}, bound config to world", key);
45+
Config.instance.blockConfigs.put(world.getUID(), Config.instance.preloadBlockConfigs.get(key));
46+
found = key;
47+
break;
48+
}
49+
50+
try {
51+
UUID worldkey = UUID.fromString(key);
52+
if (worldkey != null && world.getUID().equals(worldkey)) {
53+
HiddenOre.getPlugin().getLogger().log( Level.INFO, "Found a match during loading for {0}, bound config to world", key);
54+
Config.instance.blockConfigs.put(world.getUID(), Config.instance.preloadBlockConfigs.get(key));
55+
found = key;
56+
break;
57+
}
58+
} catch (IllegalArgumentException e) {
59+
// no match.
60+
}
61+
}
62+
}
63+
64+
if (found != null) {
65+
Config.instance.preloadBlockConfigs.remove(found);
66+
}
67+
68+
} catch (Exception e) {
69+
HiddenOre.getPlugin().getLogger().log(Level.WARNING, "Tried to check for dangling preloaded configs, failed", e);
70+
}
71+
}
72+
}

src/main/java/com/github/devotedmc/hiddenore/listeners/WorldGenerationListener.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ public WorldGenerationListener(ConfigurationSection config) {
9191
*/
9292
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
9393
public void postGenerationOreClear(ChunkPopulateEvent event) {
94-
if (toReplace == null || replaceWith == null || worldName == null) {
94+
if (toReplace == null || replaceWith == null || (worldName == null && worldUUID == null) ) {
9595
return;
9696
}
9797

@@ -165,7 +165,7 @@ private void clear(Chunk chunk) {
165165
HiddenOre.getPlugin().getLogger().log(Level.INFO, "Replaced {0} blocks at {1}, {2}", new Object[]{rep, chunk.getX(), chunk.getZ()});
166166
}
167167
}
168-
168+
169169
static BlockFace[] faces = new BlockFace[] {BlockFace.UP,BlockFace.DOWN,BlockFace.NORTH,BlockFace.SOUTH,BlockFace.EAST,BlockFace.WEST};
170170
private void generateCaveOres(Chunk chunk) {
171171
UUID world = chunk.getWorld().getUID();

0 commit comments

Comments
 (0)