From 1b05db11c9de11f1fc6bda52bb3a1042bbdf7da7 Mon Sep 17 00:00:00 2001 From: XiaoMoMi <972454774@qq.com> Date: Sun, 10 Mar 2024 04:55:55 +0800 Subject: [PATCH] world settings --- .../manager/ConfigManagerImpl.java | 2 +- .../mechanic/action/ActionManagerImpl.java | 116 ++++++++++-------- .../mechanic/world/WorldManagerImpl.java | 20 +-- plugin/src/main/resources/config.yml | 14 +-- 4 files changed, 78 insertions(+), 74 deletions(-) diff --git a/plugin/src/main/java/net/momirealms/customcrops/manager/ConfigManagerImpl.java b/plugin/src/main/java/net/momirealms/customcrops/manager/ConfigManagerImpl.java index 5ccbba2..84ceca4 100644 --- a/plugin/src/main/java/net/momirealms/customcrops/manager/ConfigManagerImpl.java +++ b/plugin/src/main/java/net/momirealms/customcrops/manager/ConfigManagerImpl.java @@ -49,7 +49,7 @@ public class ConfigManagerImpl extends ConfigManager { private boolean metrics; private boolean legacyColorSupport; private boolean protectLore; - private String[] itemDetectionOrder; + private String[] itemDetectionOrder = new String[0]; private boolean checkUpdate; private boolean disableMoisture; private boolean preventTrampling; diff --git a/plugin/src/main/java/net/momirealms/customcrops/mechanic/action/ActionManagerImpl.java b/plugin/src/main/java/net/momirealms/customcrops/mechanic/action/ActionManagerImpl.java index 14b76f8..8314f2d 100644 --- a/plugin/src/main/java/net/momirealms/customcrops/mechanic/action/ActionManagerImpl.java +++ b/plugin/src/main/java/net/momirealms/customcrops/mechanic/action/ActionManagerImpl.java @@ -450,8 +450,10 @@ public class ActionManagerImpl implements ActionManager { private void registerVariationAction() { registerAction("variation", (args, chance) -> { if (args instanceof ConfigurationSection section) { + boolean ignore = section.getBoolean("ignore-fertilizer", false); ArrayList variationCrops = new ArrayList<>(); for (String inner_key : section.getKeys(false)) { + if (inner_key.equals("ignore-fertilizer")) continue; VariationCrop variationCrop = new VariationCrop( section.getString(inner_key + ".item"), ItemCarrier.valueOf(section.getString(inner_key + ".type", "TripWire").toUpperCase(Locale.ENGLISH)), @@ -463,11 +465,13 @@ public class ActionManagerImpl implements ActionManager { return state -> { if (Math.random() > chance) return; double bonus = 0; - Optional pot = plugin.getWorldManager().getPotAt(SimpleLocation.of(state.getLocation().clone().subtract(0,1,0))); - if (pot.isPresent()) { - Fertilizer fertilizer = pot.get().getFertilizer(); - if (fertilizer instanceof Variation variation) { - bonus += variation.getChanceBonus(); + if (!ignore) { + Optional pot = plugin.getWorldManager().getPotAt(SimpleLocation.of(state.getLocation().clone().subtract(0,1,0))); + if (pot.isPresent()) { + Fertilizer fertilizer = pot.get().getFertilizer(); + if (fertilizer instanceof Variation variation) { + bonus += variation.getChanceBonus(); + } } } for (VariationCrop variationCrop : variations) { @@ -638,20 +642,24 @@ public class ActionManagerImpl implements ActionManager { crop.trigger(ActionTrigger.REACH_LIMIT, state); return; } - // fire event - CropPlantEvent plantEvent = new CropPlantEvent(state.getPlayer(), state.getItemInHand(), location, crop, 0); - if (EventUtils.fireAndCheckCancel(plantEvent)) { - return; - } - // place the crop - switch (crop.getItemCarrier()) { - case ITEM_FRAME, ITEM_DISPLAY, TRIPWIRE -> plugin.getItemManager().placeItem(location, crop.getItemCarrier(), crop.getStageItemByPoint(point)); - default -> { - LogUtils.warn("Unsupported type for crop: " + crop.getItemCarrier().name()); - return; + plugin.getScheduler().runTaskSync(() -> { + // fire event + if (state.getPlayer() != null) { + CropPlantEvent plantEvent = new CropPlantEvent(state.getPlayer(), state.getItemInHand(), location, crop, 0); + if (EventUtils.fireAndCheckCancel(plantEvent)) { + return; + } } - } - plugin.getWorldManager().addCropAt(new MemoryCrop(SimpleLocation.of(location), crop.getKey(), point), SimpleLocation.of(location)); + // place the crop + switch (crop.getItemCarrier()) { + case ITEM_FRAME, ITEM_DISPLAY, TRIPWIRE -> plugin.getItemManager().placeItem(location, crop.getItemCarrier(), crop.getStageItemByPoint(point)); + default -> { + LogUtils.warn("Unsupported type for crop: " + crop.getItemCarrier().name()); + return; + } + } + plugin.getWorldManager().addCropAt(new MemoryCrop(SimpleLocation.of(location), crop.getKey(), point), SimpleLocation.of(location)); + }, state.getLocation()); }; } else { LogUtils.warn("Illegal value format found at action: " + name); @@ -666,44 +674,46 @@ public class ActionManagerImpl implements ActionManager { boolean arg = (boolean) (args == null ? true : args); return state -> { if (Math.random() > chance) return; - Optional removed = plugin.getWorldManager().getBlockAt(SimpleLocation.of(state.getLocation())); - if (removed.isPresent()) { - switch (removed.get().getType()) { - case SPRINKLER -> { - WorldSprinkler sprinkler = (WorldSprinkler) removed.get(); - SprinklerBreakEvent event = new SprinklerBreakEvent(state.getPlayer(), state.getLocation(), sprinkler); - if (EventUtils.fireAndCheckCancel(event)) - return; - if (arg) sprinkler.getConfig().trigger(ActionTrigger.BREAK, state); - plugin.getItemManager().removeAnythingAt(state.getLocation()); - plugin.getWorldManager().removeAnythingAt(SimpleLocation.of(state.getLocation())); - } - case CROP -> { - WorldCrop crop = (WorldCrop) removed.get(); - CropBreakEvent event = new CropBreakEvent(state.getPlayer(), state.getLocation(), crop); - if (EventUtils.fireAndCheckCancel(event)) - return; - Crop cropConfig = crop.getConfig(); - if (arg) { - cropConfig.trigger(ActionTrigger.BREAK, state); - cropConfig.getStageByItemID(cropConfig.getStageItemByPoint(crop.getPoint())).trigger(ActionTrigger.BREAK, state); + plugin.getScheduler().runTaskSync(() -> { + Optional removed = plugin.getWorldManager().getBlockAt(SimpleLocation.of(state.getLocation())); + if (removed.isPresent()) { + switch (removed.get().getType()) { + case SPRINKLER -> { + WorldSprinkler sprinkler = (WorldSprinkler) removed.get(); + SprinklerBreakEvent event = new SprinklerBreakEvent(state.getPlayer(), state.getLocation(), sprinkler); + if (EventUtils.fireAndCheckCancel(event)) + return; + if (arg) sprinkler.getConfig().trigger(ActionTrigger.BREAK, state); + plugin.getItemManager().removeAnythingAt(state.getLocation()); + plugin.getWorldManager().removeAnythingAt(SimpleLocation.of(state.getLocation())); + } + case CROP -> { + WorldCrop crop = (WorldCrop) removed.get(); + CropBreakEvent event = new CropBreakEvent(state.getPlayer(), state.getLocation(), crop); + if (EventUtils.fireAndCheckCancel(event)) + return; + Crop cropConfig = crop.getConfig(); + if (arg) { + cropConfig.trigger(ActionTrigger.BREAK, state); + cropConfig.getStageByItemID(cropConfig.getStageItemByPoint(crop.getPoint())).trigger(ActionTrigger.BREAK, state); + } + plugin.getItemManager().removeAnythingAt(state.getLocation()); + plugin.getWorldManager().removeAnythingAt(SimpleLocation.of(state.getLocation())); + } + case POT -> { + WorldPot pot = (WorldPot) removed.get(); + PotBreakEvent event = new PotBreakEvent(state.getPlayer(), state.getLocation(), pot); + if (EventUtils.fireAndCheckCancel(event)) + return; + if (arg) pot.getConfig().trigger(ActionTrigger.BREAK, state); + plugin.getItemManager().removeAnythingAt(state.getLocation()); + plugin.getWorldManager().removeAnythingAt(SimpleLocation.of(state.getLocation())); } - plugin.getItemManager().removeAnythingAt(state.getLocation()); - plugin.getWorldManager().removeAnythingAt(SimpleLocation.of(state.getLocation())); - } - case POT -> { - WorldPot pot = (WorldPot) removed.get(); - PotBreakEvent event = new PotBreakEvent(state.getPlayer(), state.getLocation(), pot); - if (EventUtils.fireAndCheckCancel(event)) - return; - if (arg) pot.getConfig().trigger(ActionTrigger.BREAK, state); - plugin.getItemManager().removeAnythingAt(state.getLocation()); - plugin.getWorldManager().removeAnythingAt(SimpleLocation.of(state.getLocation())); } + } else { + plugin.getItemManager().removeAnythingAt(state.getLocation()); } - } else { - plugin.getItemManager().removeAnythingAt(state.getLocation()); - } + }, state.getLocation()); }; }); } diff --git a/plugin/src/main/java/net/momirealms/customcrops/mechanic/world/WorldManagerImpl.java b/plugin/src/main/java/net/momirealms/customcrops/mechanic/world/WorldManagerImpl.java index 43ef490..1623b79 100644 --- a/plugin/src/main/java/net/momirealms/customcrops/mechanic/world/WorldManagerImpl.java +++ b/plugin/src/main/java/net/momirealms/customcrops/mechanic/world/WorldManagerImpl.java @@ -41,10 +41,7 @@ import org.bukkit.event.world.ChunkLoadEvent; import org.bukkit.event.world.ChunkUnloadEvent; import org.jetbrains.annotations.NotNull; -import java.util.Collection; -import java.util.HashMap; -import java.util.HashSet; -import java.util.Optional; +import java.util.*; import java.util.concurrent.ConcurrentHashMap; public class WorldManagerImpl implements WorldManager, Listener { @@ -89,6 +86,7 @@ public class WorldManagerImpl implements WorldManager, Listener { @Override public void unload() { this.unregisterListener(); + this.worldSettingMap.clear(); } @Override @@ -130,13 +128,21 @@ public class WorldManagerImpl implements WorldManager, Listener { return; } - ConfigurationSection defaultSchedulerSection = settingSection.getConfigurationSection("_DEFAULT_"); - if (defaultSchedulerSection == null) { + ConfigurationSection defaultSection = settingSection.getConfigurationSection("_DEFAULT_"); + if (defaultSection == null) { LogUtils.severe("worlds.settings._DEFAULT_ section should not be null"); return; } - this.defaultWorldSetting = ConfigUtils.getWorldSettingFromSection(defaultSchedulerSection); + this.defaultWorldSetting = ConfigUtils.getWorldSettingFromSection(defaultSection); + ConfigurationSection worldSection = settingSection.getConfigurationSection("_WORLDS_"); + if (worldSection != null) { + for (Map.Entry entry : worldSection.getValues(false).entrySet()) { + if (entry.getValue() instanceof ConfigurationSection inner) { + this.worldSettingMap.put(entry.getKey(), ConfigUtils.getWorldSettingFromSection(inner)); + } + } + } } private void registerListener() { diff --git a/plugin/src/main/resources/config.yml b/plugin/src/main/resources/config.yml index 40a6034..3c18fc2 100644 --- a/plugin/src/main/resources/config.yml +++ b/plugin/src/main/resources/config.yml @@ -13,9 +13,7 @@ update-checker: true # Language lang: english -# NOTE: This section requires a restart to apply -# NOTE: This section requires a restart to apply -# NOTE: This section requires a restart to apply +# World settings worlds: # This is designed for servers that using an independent folder for worlds # Especially for realm systems @@ -74,16 +72,6 @@ worlds: tick-interval: 2 # Limit the max amount of sprinklers in one chunk max-per-chunk: 20 - # Offline growth is a special mode for crops - # Take server performance into account, enabling this option will not load the unloaded chunks - # On the contrary, CustomCrops get how long crops have grown by comparing the unload and load timestamps - offline-growth: - enable: false - # Maximum offline time recorded in seconds - # Please do not set this option to a value that is too large, - # as it may cause chunks that have been unloaded for a long time - # to consume too much server performance during loading - max-offline-seconds: 1200 # You can override the default settings for worlds here _WORLDS_: world_nether: