9
0
mirror of https://github.com/Xiao-MoMi/Custom-Fishing.git synced 2025-12-29 20:09:14 +00:00

refactor config parsers

This commit is contained in:
XiaoMoMi
2024-07-24 20:45:06 +08:00
parent 17693470cf
commit 6ec825fb36
11 changed files with 301 additions and 32 deletions

View File

@@ -50,6 +50,7 @@ import org.bukkit.inventory.ItemStack;
import org.bukkit.persistence.PersistentDataType;
import org.bukkit.util.Vector;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.*;
import java.util.concurrent.ThreadLocalRandom;
@@ -88,6 +89,18 @@ public class BukkitBlockManager implements BlockManager, Listener {
});
}
@Nullable
@Override
public BlockDataModifierFactory getBlockDataModifierFactory(@NotNull String id) {
return dataFactories.get(id);
}
@Nullable
@Override
public BlockStateModifierFactory getBlockStateModifierFactory(@NotNull String id) {
return stateFactories.get(id);
}
@Override
public void load() {
Bukkit.getPluginManager().registerEvents(this, plugin.getBoostrap());
@@ -235,8 +248,12 @@ public class BukkitBlockManager implements BlockManager, Listener {
PersistentDataType.STRING,
id + ";" + context.getHolder().getName()
);
Vector vector = playerLocation.subtract(hookLocation).toVector().multiply(1.2 - 1);
vector = vector.setY((vector.getY() + 0.2) * 1.2);
double d0 = playerLocation.getX() - hookLocation.getX();
double d1 = playerLocation.getY() - hookLocation.getY();
double d2 = playerLocation.getZ() - hookLocation.getZ();
double d3 = config.horizontalVector().evaluate(context);
double d4 = config.verticalVector().evaluate(context);
Vector vector = new Vector(d0 * 0.1D * d3, d1 * 0.1D + Math.sqrt(Math.sqrt(d0 * d0 + d1 * d1 + d2 * d2)) * 0.08D * d4, d2 * 0.1D * d3);
fallingBlock.setVelocity(vector);
return fallingBlock;
}

View File

@@ -32,8 +32,13 @@ import net.momirealms.customfishing.api.BukkitCustomFishingPlugin;
import net.momirealms.customfishing.api.mechanic.MechanicType;
import net.momirealms.customfishing.api.mechanic.action.Action;
import net.momirealms.customfishing.api.mechanic.action.ActionTrigger;
import net.momirealms.customfishing.api.mechanic.block.BlockDataModifier;
import net.momirealms.customfishing.api.mechanic.block.BlockDataModifierFactory;
import net.momirealms.customfishing.api.mechanic.block.BlockStateModifier;
import net.momirealms.customfishing.api.mechanic.block.BlockStateModifierFactory;
import net.momirealms.customfishing.api.mechanic.config.ConfigManager;
import net.momirealms.customfishing.api.mechanic.config.ConfigType;
import net.momirealms.customfishing.api.mechanic.config.function.ConfigParserFunction;
import net.momirealms.customfishing.api.mechanic.context.Context;
import net.momirealms.customfishing.api.mechanic.context.ContextKeys;
import net.momirealms.customfishing.api.mechanic.effect.Effect;
@@ -60,6 +65,7 @@ import net.momirealms.customfishing.bukkit.totem.particle.DustParticleSetting;
import net.momirealms.customfishing.bukkit.totem.particle.ParticleSetting;
import net.momirealms.customfishing.bukkit.util.ItemStackUtils;
import net.momirealms.customfishing.bukkit.util.ParticleUtils;
import net.momirealms.customfishing.common.config.node.Node;
import net.momirealms.customfishing.common.dependency.DependencyProperties;
import net.momirealms.customfishing.common.helper.AdventureHelper;
import net.momirealms.customfishing.common.helper.VersionHelper;
@@ -102,6 +108,7 @@ public class BukkitConfigManager extends ConfigManager {
this.registerBuiltInEffectModifierParser();
this.registerBuiltInTotemParser();
this.registerBuiltInHookParser();
this.registerBuiltInBlockParser();
dustParticle = VersionHelper.isVersionNewerThan1_20_5() ? Particle.valueOf("DUST") : Particle.valueOf("REDSTONE");
}
@@ -262,6 +269,7 @@ public class BukkitConfigManager extends ConfigManager {
if (!typeFolder.mkdirs()) return;
plugin.getBoostrap().saveResource("contents" + File.separator + type.path() + File.separator + "default.yml", false);
}
Map<String, Node<ConfigParserFunction>> nodes = type.parser();
fileDeque.push(typeFolder);
while (!fileDeque.isEmpty()) {
File file = fileDeque.pop();
@@ -274,7 +282,7 @@ public class BukkitConfigManager extends ConfigManager {
YamlDocument document = plugin.getConfigManager().loadData(subFile);
for (Map.Entry<String, Object> entry : document.getStringRouteMappedValues(false).entrySet()) {
if (entry.getValue() instanceof Section section) {
type.parse(entry.getKey(), section, formatFunctions);
type.parse(entry.getKey(), section, nodes);
}
}
}
@@ -783,6 +791,41 @@ public class BukkitConfigManager extends ConfigManager {
}, "base-effects", "wait-time-multiplier");
}
private void registerBuiltInBlockParser() {
this.registerBlockParser(object -> {
String block = (String) object;
return builder -> builder.blockID(block);
}, "block");
this.registerBlockParser(object -> {
Section section = (Section) object;
List<BlockDataModifier> dataModifiers = new ArrayList<>();
List<BlockStateModifier> stateModifiers = new ArrayList<>();
for (Map.Entry<String, Object> innerEntry : section.getStringRouteMappedValues(false).entrySet()) {
BlockDataModifierFactory dataModifierFactory = plugin.getBlockManager().getBlockDataModifierFactory(innerEntry.getKey());
if (dataModifierFactory != null) {
dataModifiers.add(dataModifierFactory.process(innerEntry.getValue()));
continue;
}
BlockStateModifierFactory stateModifierFactory = plugin.getBlockManager().getBlockStateModifierFactory(innerEntry.getKey());
if (stateModifierFactory != null) {
stateModifiers.add(stateModifierFactory.process(innerEntry.getValue()));
}
}
return builder -> {
builder.dataModifierList(dataModifiers);
builder.stateModifierList(stateModifiers);
};
}, "properties");
this.registerBlockParser(object -> {
MathValue<Player> mathValue = MathValue.auto(object);
return builder -> builder.horizontalVector(mathValue);
}, "velocity", "horizontal");
this.registerBlockParser(object -> {
MathValue<Player> mathValue = MathValue.auto(object);
return builder -> builder.verticalVector(mathValue);
}, "velocity", "vertical");
}
private void registerBuiltInEntityParser() {
this.registerEntityParser(object -> {
String entity = (String) object;

View File

@@ -146,9 +146,9 @@ public class BukkitMarketManager implements MarketManager, Listener {
this.sellAllSlot = sellAllSection.getString("symbol", "S").charAt(0);
this.sellFishingBag = sellAllSection.getBoolean("fishingbag", true);
this.sellAllIconAllowItem = new SingleItemParser("allow", sellAllSection.getSection("allow-icon"), plugin.getConfigManager().getFormatFunctions()).getItem();
this.sellAllIconDenyItem = new SingleItemParser("deny", sellAllSection.getSection("deny-icon"), plugin.getConfigManager().getFormatFunctions()).getItem();
this.sellAllIconLimitItem = new SingleItemParser("limit", sellAllSection.getSection("limit-icon"), plugin.getConfigManager().getFormatFunctions()).getItem();
this.sellAllIconAllowItem = new SingleItemParser("allow", sellAllSection.getSection("allow-icon"), plugin.getConfigManager().getItemFormatFunctions()).getItem();
this.sellAllIconDenyItem = new SingleItemParser("deny", sellAllSection.getSection("deny-icon"), plugin.getConfigManager().getItemFormatFunctions()).getItem();
this.sellAllIconLimitItem = new SingleItemParser("limit", sellAllSection.getSection("limit-icon"), plugin.getConfigManager().getItemFormatFunctions()).getItem();
this.sellAllAllowActions = plugin.getActionManager().parseActions(sellAllSection.getSection("allow-icon.action"));
this.sellAllDenyActions = plugin.getActionManager().parseActions(sellAllSection.getSection("deny-icon.action"));
@@ -163,9 +163,9 @@ public class BukkitMarketManager implements MarketManager, Listener {
if (sellSection != null) {
this.sellSlot = sellSection.getString("symbol", "B").charAt(0);
this.sellIconAllowItem = new SingleItemParser("allow", sellSection.getSection("allow-icon"), plugin.getConfigManager().getFormatFunctions()).getItem();
this.sellIconDenyItem = new SingleItemParser("deny", sellSection.getSection("deny-icon"), plugin.getConfigManager().getFormatFunctions()).getItem();
this.sellIconLimitItem = new SingleItemParser("limit", sellSection.getSection("limit-icon"), plugin.getConfigManager().getFormatFunctions()).getItem();
this.sellIconAllowItem = new SingleItemParser("allow", sellSection.getSection("allow-icon"), plugin.getConfigManager().getItemFormatFunctions()).getItem();
this.sellIconDenyItem = new SingleItemParser("deny", sellSection.getSection("deny-icon"), plugin.getConfigManager().getItemFormatFunctions()).getItem();
this.sellIconLimitItem = new SingleItemParser("limit", sellSection.getSection("limit-icon"), plugin.getConfigManager().getItemFormatFunctions()).getItem();
this.sellAllowActions = plugin.getActionManager().parseActions(sellSection.getSection("allow-icon.action"));
this.sellDenyActions = plugin.getActionManager().parseActions(sellSection.getSection("deny-icon.action"));
@@ -188,7 +188,7 @@ public class BukkitMarketManager implements MarketManager, Listener {
for (Map.Entry<String, Object> entry : decorativeSection.getStringRouteMappedValues(false).entrySet()) {
if (entry.getValue() instanceof Section innerSection) {
char symbol = Objects.requireNonNull(innerSection.getString("symbol")).charAt(0);
decorativeIcons.put(symbol, new SingleItemParser("gui", innerSection, plugin.getConfigManager().getFormatFunctions()).getItem());
decorativeIcons.put(symbol, new SingleItemParser("gui", innerSection, plugin.getConfigManager().getItemFormatFunctions()).getItem());
}
}
}