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 legacyColorSupport;
private boolean protectLore;
private String[] itemDetectionOrder;
private String[] itemDetectionOrder = new String[0];
private boolean checkUpdate;
private boolean disableMoisture;
private boolean preventTrampling;

View File

@@ -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<VariationCrop> 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<WorldPot> 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<WorldPot> 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<CustomCropsBlock> 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<CustomCropsBlock> 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());
};
});
}

View File

@@ -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<String, Object> entry : worldSection.getValues(false).entrySet()) {
if (entry.getValue() instanceof ConfigurationSection inner) {
this.worldSettingMap.put(entry.getKey(), ConfigUtils.getWorldSettingFromSection(inner));
}
}
}
}
private void registerListener() {

View File

@@ -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: