diff --git a/CHANGELOG.md b/CHANGELOG.md index 59091cc67..433104e6b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -27,6 +27,7 @@ * Added a [Metrics module](https://github.com/Slimefun/MetricsModule) which allows us to release updates to metrics (bStats) independently from the main plugin * Added "Compressed Carbon -> Carbon" recipe to the Ore Crusher * Added "Carbon -> Coal" recipe to the Ore Crusher +* Added an option to disable the message "Ignoring duplicate block" #### Changes * Refactored and reworked the Generator API diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/core/SlimefunRegistry.java b/src/main/java/io/github/thebusybiscuit/slimefun4/core/SlimefunRegistry.java index 710cd83e4..145a0cb9b 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/core/SlimefunRegistry.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/core/SlimefunRegistry.java @@ -64,10 +64,11 @@ public class SlimefunRegistry { private boolean enableResearches; private boolean freeCreativeResearches; private boolean researchFireworks; + private boolean logDuplicateBlockEntries; private final Set tickers = new HashSet<>(); private final Set radioactive = new HashSet<>(); - private final Set activeChunks = new HashSet<>(); + private final Set activeChunks = ConcurrentHashMap.newKeySet(); private final KeyMap geoResources = new KeyMap<>(); @@ -82,7 +83,7 @@ public class SlimefunRegistry { private final Map, Set> globalItemHandlers = new HashMap<>(); private final Map blockHandlers = new HashMap<>(); - private final Map> activeTickers = new HashMap<>(); + private final Map> activeTickers = new ConcurrentHashMap<>(); private final Map automatedCraftingChamberRecipes = new HashMap<>(); @@ -98,6 +99,7 @@ public class SlimefunRegistry { backwardsCompatibility = cfg.getBoolean("options.backwards-compatibility") || SlimefunPlugin.getMinecraftVersion().isBefore(MinecraftVersion.MINECRAFT_1_14); freeCreativeResearches = cfg.getBoolean("researches.free-in-creative-mode"); researchFireworks = cfg.getBoolean("researches.enable-fireworks"); + logDuplicateBlockEntries = cfg.getBoolean("options.log-duplicate-block-entries"); } /** @@ -264,5 +266,9 @@ public class SlimefunRegistry { public Map getAutomatedCraftingChamberRecipes() { return automatedCraftingChamberRecipes; } + + public boolean logDuplicateBlockEntries() { + return logDuplicateBlockEntries; + } } diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/core/services/github/GitHubConnector.java b/src/main/java/io/github/thebusybiscuit/slimefun4/core/services/github/GitHubConnector.java index fba07dfd3..f168dc322 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/core/services/github/GitHubConnector.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/core/services/github/GitHubConnector.java @@ -54,7 +54,7 @@ abstract class GitHubConnector { writeCacheFile(resp.getBody()); } else { - Slimefun.getLogger().log(Level.WARNING, "Failed to fetch {0}: {1} - {2}", new Object[] {repository + getURLSuffix(), resp.getStatus(), resp.getBody().toString()}); + Slimefun.getLogger().log(Level.WARNING, "Failed to fetch {0}: {1} - {2}", new Object[] {repository + getURLSuffix(), resp.getStatus(), resp.getBody()}); } } catch (UnirestException e) { diff --git a/src/main/java/me/mrCookieSlime/Slimefun/api/BlockStorage.java b/src/main/java/me/mrCookieSlime/Slimefun/api/BlockStorage.java index c8900f942..9e198997a 100644 --- a/src/main/java/me/mrCookieSlime/Slimefun/api/BlockStorage.java +++ b/src/main/java/me/mrCookieSlime/Slimefun/api/BlockStorage.java @@ -16,7 +16,6 @@ import java.util.logging.Level; import java.util.logging.Logger; import org.bukkit.Bukkit; -import org.bukkit.Chunk; import org.bukkit.Location; import org.bukkit.World; import org.bukkit.block.Block; @@ -151,40 +150,8 @@ public class BlockStorage { FileConfiguration cfg = YamlConfiguration.loadConfiguration(file); for (String key : cfg.getKeys(false)) { - Location l = deserializeLocation(key); - String chunkString = locationToChunkString(l); - - try { - totalBlocks++; - String json = cfg.getString(key); - Config blockInfo = parseBlockInfo(l, json); - - if (blockInfo != null && blockInfo.contains("id")) { - if (storage.containsKey(l)) { - // It should not be possible to have two blocks on the same location. Ignore the - // new entry if a block is already present and print an error to the console. - - Slimefun.getLogger().log(Level.INFO, "Ignoring duplicate block @ {0}, {1}, {2}", new Object[] { l.getBlockX(), l.getBlockY(), l.getBlockZ() }); - Slimefun.getLogger().log(Level.INFO, "New: {0} | Old: {1}", new Object[] { key, serializeBlockInfo(storage.get(l)) }); - continue; - } - - storage.put(l, blockInfo); - - if (SlimefunPlugin.getRegistry().getTickerBlocks().contains(file.getName().replace(".sfb", ""))) { - Set locations = SlimefunPlugin.getRegistry().getActiveTickers().getOrDefault(chunkString, new HashSet<>()); - locations.add(l); - SlimefunPlugin.getRegistry().getActiveTickers().put(chunkString, locations); - - if (!SlimefunPlugin.getRegistry().getActiveChunks().contains(chunkString)) { - SlimefunPlugin.getRegistry().getActiveChunks().add(chunkString); - } - } - } - } - catch (Exception x) { - Slimefun.getLogger().log(Level.WARNING, x, () -> "Failed to load " + file.getName() + '(' + key + ") for Slimefun " + SlimefunPlugin.getVersion()); - } + loadBlock(file, cfg, key); + totalBlocks++; } done++; @@ -202,6 +169,48 @@ public class BlockStorage { } } + private void loadBlock(File file, FileConfiguration cfg, String key) { + Location l = deserializeLocation(key); + + if (l == null) { + // That location was malformed, we will skip this one + return; + } + + try { + String chunkString = locationToChunkString(l); + String json = cfg.getString(key); + Config blockInfo = parseBlockInfo(l, json); + + if (blockInfo != null && blockInfo.contains("id")) { + if (storage.containsKey(l)) { + // It should not be possible to have two blocks on the same location. Ignore the + // new entry if a block is already present and print an error to the console. + if (SlimefunPlugin.getRegistry().logDuplicateBlockEntries()) { + Slimefun.getLogger().log(Level.INFO, "Ignoring duplicate block @ {0}, {1}, {2} ({3} -> {4})", new Object[] { l.getBlockX(), l.getBlockY(), l.getBlockZ(), blockInfo.getString("id"), storage.get(l).getString("id") }); + } + + return; + } + + storage.put(l, blockInfo); + + if (SlimefunPlugin.getRegistry().getTickerBlocks().contains(file.getName().replace(".sfb", ""))) { + Set locations = SlimefunPlugin.getRegistry().getActiveTickers().getOrDefault(chunkString, new HashSet<>()); + locations.add(l); + SlimefunPlugin.getRegistry().getActiveTickers().put(chunkString, locations); + + if (!SlimefunPlugin.getRegistry().getActiveChunks().contains(chunkString)) { + SlimefunPlugin.getRegistry().getActiveChunks().add(chunkString); + } + } + } + } + catch (Exception x) { + Slimefun.getLogger().log(Level.WARNING, x, () -> "Failed to load " + file.getName() + '(' + key + ") for Slimefun " + SlimefunPlugin.getVersion()); + } + } + private void loadChunks() { File chunks = new File(PATH_CHUNKS + "chunks.sfc"); @@ -211,7 +220,8 @@ public class BlockStorage { for (String key : cfg.getKeys(false)) { try { if (world.getName().equals(PatternUtils.SEMICOLON.split(key)[0])) { - SlimefunPlugin.getRegistry().getChunks().put(key, new BlockInfoConfig(parseJSON(cfg.getString(key)))); + BlockInfoConfig data = new BlockInfoConfig(parseJSON(cfg.getString(key))); + SlimefunPlugin.getRegistry().getChunks().put(key, data); } } catch (Exception x) { @@ -381,6 +391,7 @@ public class BlockStorage { * * @param block * the block to retrieve the ItemStack from + * * @return the SlimefunItem's ItemStack corresponding to the block if it has one, otherwise null * * @since 4.0 @@ -424,6 +435,7 @@ public class BlockStorage { map.put(entry.getKey(), entry.getValue().getAsString()); } } + return map; } @@ -530,12 +542,14 @@ public class BlockStorage { } else if (!storage.hasInventory(l)) { File file = new File(PATH_INVENTORIES + serializeLocation(l) + ".sfi"); + BlockMenuPreset preset = BlockMenuPreset.getPreset(id); if (file.exists()) { - storage.inventories.put(l, new BlockMenu(BlockMenuPreset.getPreset(id), l, new io.github.thebusybiscuit.cscorelib2.config.Config(file))); + BlockMenu inventory = new BlockMenu(preset, l, new io.github.thebusybiscuit.cscorelib2.config.Config(file)); + storage.inventories.put(l, inventory); } else { - storage.loadInventory(l, BlockMenuPreset.getPreset(id)); + storage.loadInventory(l, preset); } } } @@ -732,15 +746,11 @@ public class BlockStorage { } public static Set getTickingChunks() { - return new HashSet<>(SlimefunPlugin.getRegistry().getActiveChunks()); - } - - public static Set getTickingLocations(Chunk chunk) { - return getTickingLocations(chunk.toString()); + return SlimefunPlugin.getRegistry().getActiveChunks(); } public static Set getTickingLocations(String chunk) { - return new HashSet<>(SlimefunPlugin.getRegistry().getActiveTickers().get(chunk)); + return SlimefunPlugin.getRegistry().getActiveTickers().getOrDefault(chunk, new HashSet<>()); } public BlockMenu loadInventory(Location l, BlockMenuPreset preset) { @@ -769,7 +779,8 @@ public class BlockStorage { } public void loadUniversalInventory(BlockMenuPreset preset) { - SlimefunPlugin.getRegistry().getUniversalInventories().put(preset.getID(), new UniversalBlockMenu(preset)); + UniversalBlockMenu inventory = new UniversalBlockMenu(preset); + SlimefunPlugin.getRegistry().getUniversalInventories().put(preset.getID(), inventory); } public void clearInventory(Location l) { diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index eca44c177..9e787a99b 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -16,6 +16,7 @@ options: legacy-ore-grinder: true language: en enable-translations: true + log-duplicate-block-entries: true guide: default-view-book: false