9
0
mirror of https://github.com/Xiao-MoMi/Custom-Crops.git synced 2025-12-21 16:09:21 +00:00

world settings

This commit is contained in:
XiaoMoMi
2024-03-10 04:55:55 +08:00
parent 0f18904de0
commit 1b05db11c9
4 changed files with 78 additions and 74 deletions

View File

@@ -49,7 +49,7 @@ public class ConfigManagerImpl extends ConfigManager {
private boolean metrics; private boolean metrics;
private boolean legacyColorSupport; private boolean legacyColorSupport;
private boolean protectLore; private boolean protectLore;
private String[] itemDetectionOrder; private String[] itemDetectionOrder = new String[0];
private boolean checkUpdate; private boolean checkUpdate;
private boolean disableMoisture; private boolean disableMoisture;
private boolean preventTrampling; private boolean preventTrampling;

View File

@@ -450,8 +450,10 @@ public class ActionManagerImpl implements ActionManager {
private void registerVariationAction() { private void registerVariationAction() {
registerAction("variation", (args, chance) -> { registerAction("variation", (args, chance) -> {
if (args instanceof ConfigurationSection section) { if (args instanceof ConfigurationSection section) {
boolean ignore = section.getBoolean("ignore-fertilizer", false);
ArrayList<VariationCrop> variationCrops = new ArrayList<>(); ArrayList<VariationCrop> variationCrops = new ArrayList<>();
for (String inner_key : section.getKeys(false)) { for (String inner_key : section.getKeys(false)) {
if (inner_key.equals("ignore-fertilizer")) continue;
VariationCrop variationCrop = new VariationCrop( VariationCrop variationCrop = new VariationCrop(
section.getString(inner_key + ".item"), section.getString(inner_key + ".item"),
ItemCarrier.valueOf(section.getString(inner_key + ".type", "TripWire").toUpperCase(Locale.ENGLISH)), ItemCarrier.valueOf(section.getString(inner_key + ".type", "TripWire").toUpperCase(Locale.ENGLISH)),
@@ -463,11 +465,13 @@ public class ActionManagerImpl implements ActionManager {
return state -> { return state -> {
if (Math.random() > chance) return; if (Math.random() > chance) return;
double bonus = 0; double bonus = 0;
Optional<WorldPot> pot = plugin.getWorldManager().getPotAt(SimpleLocation.of(state.getLocation().clone().subtract(0,1,0))); if (!ignore) {
if (pot.isPresent()) { Optional<WorldPot> pot = plugin.getWorldManager().getPotAt(SimpleLocation.of(state.getLocation().clone().subtract(0,1,0)));
Fertilizer fertilizer = pot.get().getFertilizer(); if (pot.isPresent()) {
if (fertilizer instanceof Variation variation) { Fertilizer fertilizer = pot.get().getFertilizer();
bonus += variation.getChanceBonus(); if (fertilizer instanceof Variation variation) {
bonus += variation.getChanceBonus();
}
} }
} }
for (VariationCrop variationCrop : variations) { for (VariationCrop variationCrop : variations) {
@@ -638,20 +642,24 @@ public class ActionManagerImpl implements ActionManager {
crop.trigger(ActionTrigger.REACH_LIMIT, state); crop.trigger(ActionTrigger.REACH_LIMIT, state);
return; return;
} }
// fire event plugin.getScheduler().runTaskSync(() -> {
CropPlantEvent plantEvent = new CropPlantEvent(state.getPlayer(), state.getItemInHand(), location, crop, 0); // fire event
if (EventUtils.fireAndCheckCancel(plantEvent)) { if (state.getPlayer() != null) {
return; CropPlantEvent plantEvent = new CropPlantEvent(state.getPlayer(), state.getItemInHand(), location, crop, 0);
} if (EventUtils.fireAndCheckCancel(plantEvent)) {
// place the crop return;
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;
} }
} // place the crop
plugin.getWorldManager().addCropAt(new MemoryCrop(SimpleLocation.of(location), crop.getKey(), point), SimpleLocation.of(location)); 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 { } else {
LogUtils.warn("Illegal value format found at action: " + name); 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); boolean arg = (boolean) (args == null ? true : args);
return state -> { return state -> {
if (Math.random() > chance) return; if (Math.random() > chance) return;
Optional<CustomCropsBlock> removed = plugin.getWorldManager().getBlockAt(SimpleLocation.of(state.getLocation())); plugin.getScheduler().runTaskSync(() -> {
if (removed.isPresent()) { Optional<CustomCropsBlock> removed = plugin.getWorldManager().getBlockAt(SimpleLocation.of(state.getLocation()));
switch (removed.get().getType()) { if (removed.isPresent()) {
case SPRINKLER -> { switch (removed.get().getType()) {
WorldSprinkler sprinkler = (WorldSprinkler) removed.get(); case SPRINKLER -> {
SprinklerBreakEvent event = new SprinklerBreakEvent(state.getPlayer(), state.getLocation(), sprinkler); WorldSprinkler sprinkler = (WorldSprinkler) removed.get();
if (EventUtils.fireAndCheckCancel(event)) SprinklerBreakEvent event = new SprinklerBreakEvent(state.getPlayer(), state.getLocation(), sprinkler);
return; if (EventUtils.fireAndCheckCancel(event))
if (arg) sprinkler.getConfig().trigger(ActionTrigger.BREAK, state); return;
plugin.getItemManager().removeAnythingAt(state.getLocation()); if (arg) sprinkler.getConfig().trigger(ActionTrigger.BREAK, state);
plugin.getWorldManager().removeAnythingAt(SimpleLocation.of(state.getLocation())); plugin.getItemManager().removeAnythingAt(state.getLocation());
} plugin.getWorldManager().removeAnythingAt(SimpleLocation.of(state.getLocation()));
case CROP -> { }
WorldCrop crop = (WorldCrop) removed.get(); case CROP -> {
CropBreakEvent event = new CropBreakEvent(state.getPlayer(), state.getLocation(), crop); WorldCrop crop = (WorldCrop) removed.get();
if (EventUtils.fireAndCheckCancel(event)) CropBreakEvent event = new CropBreakEvent(state.getPlayer(), state.getLocation(), crop);
return; if (EventUtils.fireAndCheckCancel(event))
Crop cropConfig = crop.getConfig(); return;
if (arg) { Crop cropConfig = crop.getConfig();
cropConfig.trigger(ActionTrigger.BREAK, state); if (arg) {
cropConfig.getStageByItemID(cropConfig.getStageItemByPoint(crop.getPoint())).trigger(ActionTrigger.BREAK, state); 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 { }, state.getLocation());
plugin.getItemManager().removeAnythingAt(state.getLocation());
}
}; };
}); });
} }

View File

@@ -41,10 +41,7 @@ import org.bukkit.event.world.ChunkLoadEvent;
import org.bukkit.event.world.ChunkUnloadEvent; import org.bukkit.event.world.ChunkUnloadEvent;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import java.util.Collection; import java.util.*;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
public class WorldManagerImpl implements WorldManager, Listener { public class WorldManagerImpl implements WorldManager, Listener {
@@ -89,6 +86,7 @@ public class WorldManagerImpl implements WorldManager, Listener {
@Override @Override
public void unload() { public void unload() {
this.unregisterListener(); this.unregisterListener();
this.worldSettingMap.clear();
} }
@Override @Override
@@ -130,13 +128,21 @@ public class WorldManagerImpl implements WorldManager, Listener {
return; return;
} }
ConfigurationSection defaultSchedulerSection = settingSection.getConfigurationSection("_DEFAULT_"); ConfigurationSection defaultSection = settingSection.getConfigurationSection("_DEFAULT_");
if (defaultSchedulerSection == null) { if (defaultSection == null) {
LogUtils.severe("worlds.settings._DEFAULT_ section should not be null"); LogUtils.severe("worlds.settings._DEFAULT_ section should not be null");
return; return;
} }
this.defaultWorldSetting = ConfigUtils.getWorldSettingFromSection(defaultSchedulerSection); this.defaultWorldSetting = ConfigUtils.getWorldSettingFromSection(defaultSection);
ConfigurationSection worldSection = settingSection.getConfigurationSection("_WORLDS_");
if (worldSection != null) {
for (Map.Entry<String, Object> entry : worldSection.getValues(false).entrySet()) {
if (entry.getValue() instanceof ConfigurationSection inner) {
this.worldSettingMap.put(entry.getKey(), ConfigUtils.getWorldSettingFromSection(inner));
}
}
}
} }
private void registerListener() { private void registerListener() {

View File

@@ -13,9 +13,7 @@ update-checker: true
# Language # Language
lang: english lang: english
# NOTE: This section requires a restart to apply # World settings
# NOTE: This section requires a restart to apply
# NOTE: This section requires a restart to apply
worlds: worlds:
# This is designed for servers that using an independent folder for worlds # This is designed for servers that using an independent folder for worlds
# Especially for realm systems # Especially for realm systems
@@ -74,16 +72,6 @@ worlds:
tick-interval: 2 tick-interval: 2
# Limit the max amount of sprinklers in one chunk # Limit the max amount of sprinklers in one chunk
max-per-chunk: 20 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 # You can override the default settings for worlds here
_WORLDS_: _WORLDS_:
world_nether: world_nether: