diff --git a/api/src/main/java/net/momirealms/customfishing/api/mechanic/config/ConfigManager.java b/api/src/main/java/net/momirealms/customfishing/api/mechanic/config/ConfigManager.java index 3a38cb22..d97f3be6 100644 --- a/api/src/main/java/net/momirealms/customfishing/api/mechanic/config/ConfigManager.java +++ b/api/src/main/java/net/momirealms/customfishing/api/mechanic/config/ConfigManager.java @@ -37,6 +37,7 @@ import net.momirealms.customfishing.api.mechanic.entity.EntityConfig; import net.momirealms.customfishing.api.mechanic.event.EventCarrier; import net.momirealms.customfishing.api.mechanic.hook.HookConfig; import net.momirealms.customfishing.api.mechanic.loot.Loot; +import net.momirealms.customfishing.api.mechanic.loot.operation.WeightOperation; import net.momirealms.customfishing.api.mechanic.requirement.Requirement; import net.momirealms.customfishing.api.mechanic.totem.TotemConfig; import net.momirealms.customfishing.common.config.ConfigLoader; @@ -59,7 +60,6 @@ import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.function.BiConsumer; -import java.util.function.BiFunction; import java.util.function.Consumer; import java.util.function.Function; @@ -471,9 +471,9 @@ public abstract class ConfigManager implements ConfigLoader, Reloadable { return lootFormatFunctions; } - public abstract List, Double, Double>>> parseWeightOperation(List ops); + public abstract List> parseWeightOperation(List ops, Function validator); - public abstract List, Double, Double>>> parseGroupWeightOperation(List gops); + public abstract List> parseGroupWeightOperation(List gops); @Deprecated public Map> getDefaultFormatFunctions() { diff --git a/api/src/main/java/net/momirealms/customfishing/api/mechanic/context/ContextKeys.java b/api/src/main/java/net/momirealms/customfishing/api/mechanic/context/ContextKeys.java index c1eb33f4..67ab8ec0 100644 --- a/api/src/main/java/net/momirealms/customfishing/api/mechanic/context/ContextKeys.java +++ b/api/src/main/java/net/momirealms/customfishing/api/mechanic/context/ContextKeys.java @@ -76,6 +76,7 @@ public class ContextKeys { public static final ContextKeys AMOUNT = of("amount", Integer.class); public static final ContextKeys TOTAL_AMOUNT = of("total_amount", Integer.class); public static final ContextKeys WEIGHT = of("0", Double.class); + public static final ContextKeys TOTAL_WEIGHT = of("1", Double.class); public static final ContextKeys TIME_LEFT = of("time_left", String.class); public static final ContextKeys PROGRESS = of("progress", String.class); public static final ContextKeys RECORD = of("record", Float.class); diff --git a/api/src/main/java/net/momirealms/customfishing/api/mechanic/effect/Effect.java b/api/src/main/java/net/momirealms/customfishing/api/mechanic/effect/Effect.java index cb38d7a6..23757ec6 100644 --- a/api/src/main/java/net/momirealms/customfishing/api/mechanic/effect/Effect.java +++ b/api/src/main/java/net/momirealms/customfishing/api/mechanic/effect/Effect.java @@ -17,13 +17,11 @@ package net.momirealms.customfishing.api.mechanic.effect; -import net.momirealms.customfishing.api.mechanic.context.Context; +import net.momirealms.customfishing.api.mechanic.loot.operation.WeightOperation; import net.momirealms.customfishing.common.util.Pair; -import org.bukkit.entity.Player; import java.util.List; import java.util.Map; -import java.util.function.BiFunction; /** * Represents an effect applied in the fishing. @@ -234,7 +232,7 @@ public interface Effect { * * @return the list of weight operations */ - List, Double, Double>>> weightOperations(); + List> weightOperations(); /** * Adds the list of weight operations. @@ -242,14 +240,14 @@ public interface Effect { * @param weightOperations the list of weight operations to add * @return the effect instance */ - Effect weightOperations(List, Double, Double>>> weightOperations); + Effect weightOperations(List> weightOperations); /** * Gets the list of weight operations that are conditions ignored. * * @return the list of weight operations that are conditions ignored */ - List, Double, Double>>> weightOperationsIgnored(); + List> weightOperationsIgnored(); /** * Adds the list of weight operations that are conditions ignored. @@ -257,7 +255,7 @@ public interface Effect { * @param weightOperations the list of weight operations that are conditions ignored * @return the effect instance */ - Effect weightOperationsIgnored(List, Double, Double>>> weightOperations); + Effect weightOperationsIgnored(List> weightOperations); /** * Combines this effect with another effect. diff --git a/api/src/main/java/net/momirealms/customfishing/api/mechanic/effect/EffectImpl.java b/api/src/main/java/net/momirealms/customfishing/api/mechanic/effect/EffectImpl.java index 4cc1a562..3f693d8b 100644 --- a/api/src/main/java/net/momirealms/customfishing/api/mechanic/effect/EffectImpl.java +++ b/api/src/main/java/net/momirealms/customfishing/api/mechanic/effect/EffectImpl.java @@ -1,14 +1,12 @@ package net.momirealms.customfishing.api.mechanic.effect; -import net.momirealms.customfishing.api.mechanic.context.Context; +import net.momirealms.customfishing.api.mechanic.loot.operation.WeightOperation; import net.momirealms.customfishing.common.util.Pair; -import org.bukkit.entity.Player; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; -import java.util.function.BiFunction; public class EffectImpl implements Effect { @@ -24,8 +22,8 @@ public class EffectImpl implements Effect { private double waitTimeMultiplier = 1; private double difficultyAdder = 0; private double difficultyMultiplier = 1; - private final List, Double, Double>>> weightOperations = new ArrayList<>(); - private final List, Double, Double>>> weightOperationsIgnored = new ArrayList<>(); + private final List> weightOperations = new ArrayList<>(); + private final List> weightOperationsIgnored = new ArrayList<>(); @Override public Map, Object> properties() { @@ -172,23 +170,23 @@ public class EffectImpl implements Effect { } @Override - public List, Double, Double>>> weightOperations() { + public List> weightOperations() { return weightOperations; } @Override - public Effect weightOperations(List, Double, Double>>> weightOperations) { + public Effect weightOperations(List> weightOperations) { this.weightOperations.addAll(weightOperations); return this; } @Override - public List, Double, Double>>> weightOperationsIgnored() { + public List> weightOperationsIgnored() { return weightOperationsIgnored; } @Override - public Effect weightOperationsIgnored(List, Double, Double>>> weightOperations) { + public Effect weightOperationsIgnored(List> weightOperations) { this.weightOperationsIgnored.addAll(weightOperations); return this; } diff --git a/api/src/main/java/net/momirealms/customfishing/api/mechanic/loot/operation/AddWeightOperation.java b/api/src/main/java/net/momirealms/customfishing/api/mechanic/loot/operation/AddWeightOperation.java new file mode 100644 index 00000000..22c73708 --- /dev/null +++ b/api/src/main/java/net/momirealms/customfishing/api/mechanic/loot/operation/AddWeightOperation.java @@ -0,0 +1,38 @@ +/* + * Copyright (C) <2024> + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package net.momirealms.customfishing.api.mechanic.loot.operation; + +import net.momirealms.customfishing.api.mechanic.context.Context; +import net.momirealms.customfishing.api.mechanic.misc.value.MathValue; +import org.bukkit.entity.Player; + +import java.util.Map; + +public class AddWeightOperation implements WeightOperation { + + private final MathValue arg; + + public AddWeightOperation(MathValue arg) { + this.arg = arg; + } + + @Override + public Double apply(Context context, Double weight, Map weights) { + return weight + arg.evaluate(context); + } +} diff --git a/api/src/main/java/net/momirealms/customfishing/api/mechanic/loot/operation/CustomWeightOperation.java b/api/src/main/java/net/momirealms/customfishing/api/mechanic/loot/operation/CustomWeightOperation.java new file mode 100644 index 00000000..a26bf137 --- /dev/null +++ b/api/src/main/java/net/momirealms/customfishing/api/mechanic/loot/operation/CustomWeightOperation.java @@ -0,0 +1,64 @@ +/* + * Copyright (C) <2024> + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package net.momirealms.customfishing.api.mechanic.loot.operation; + +import net.momirealms.customfishing.api.mechanic.context.Context; +import net.momirealms.customfishing.api.mechanic.context.ContextKeys; +import net.momirealms.customfishing.api.mechanic.misc.value.MathValue; +import org.bukkit.entity.Player; + +import java.util.Collection; +import java.util.List; +import java.util.Map; + +public class CustomWeightOperation implements WeightOperation { + + private final MathValue arg; + private final boolean hasTotalWeight; + private final List otherWeights; + + public CustomWeightOperation(MathValue arg, boolean hasTotalWeight, List otherWeights) { + this.arg = arg; + this.hasTotalWeight = hasTotalWeight; + this.otherWeights = otherWeights; + } + + @Override + public Double apply(Context context, Double weight, Map weights) { + context.arg(ContextKeys.WEIGHT, weight); + if (hasTotalWeight) { + context.arg(ContextKeys.TOTAL_WEIGHT, getValidTotalWeight(weights.values())); + } + if (!otherWeights.isEmpty()) { + for (String otherWeight : otherWeights) { + context.arg(ContextKeys.of("loot_" + otherWeight, Double.class), weights.get(otherWeight)); + } + } + return arg.evaluate(context); + } + + private double getValidTotalWeight(Collection weights) { + double totalWeight = 0; + for (Double weight : weights) { + if (weight > 0) { + totalWeight += weight; + } + } + return totalWeight; + } +} diff --git a/api/src/main/java/net/momirealms/customfishing/api/mechanic/loot/operation/DivideWeightOperation.java b/api/src/main/java/net/momirealms/customfishing/api/mechanic/loot/operation/DivideWeightOperation.java new file mode 100644 index 00000000..ca2200d2 --- /dev/null +++ b/api/src/main/java/net/momirealms/customfishing/api/mechanic/loot/operation/DivideWeightOperation.java @@ -0,0 +1,38 @@ +/* + * Copyright (C) <2024> + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package net.momirealms.customfishing.api.mechanic.loot.operation; + +import net.momirealms.customfishing.api.mechanic.context.Context; +import net.momirealms.customfishing.api.mechanic.misc.value.MathValue; +import org.bukkit.entity.Player; + +import java.util.Map; + +public class DivideWeightOperation implements WeightOperation { + + private final MathValue arg; + + public DivideWeightOperation(MathValue arg) { + this.arg = arg; + } + + @Override + public Double apply(Context context, Double weight, Map weights) { + return weight / arg.evaluate(context); + } +} diff --git a/api/src/main/java/net/momirealms/customfishing/api/mechanic/loot/operation/ModuloWeightOperation.java b/api/src/main/java/net/momirealms/customfishing/api/mechanic/loot/operation/ModuloWeightOperation.java new file mode 100644 index 00000000..529d7a55 --- /dev/null +++ b/api/src/main/java/net/momirealms/customfishing/api/mechanic/loot/operation/ModuloWeightOperation.java @@ -0,0 +1,38 @@ +/* + * Copyright (C) <2024> + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package net.momirealms.customfishing.api.mechanic.loot.operation; + +import net.momirealms.customfishing.api.mechanic.context.Context; +import net.momirealms.customfishing.api.mechanic.misc.value.MathValue; +import org.bukkit.entity.Player; + +import java.util.Map; + +public class ModuloWeightOperation implements WeightOperation { + + private final MathValue arg; + + public ModuloWeightOperation(MathValue arg) { + this.arg = arg; + } + + @Override + public Double apply(Context context, Double weight, Map weights) { + return weight % arg.evaluate(context); + } +} diff --git a/api/src/main/java/net/momirealms/customfishing/api/mechanic/loot/operation/MultiplyWeightOperation.java b/api/src/main/java/net/momirealms/customfishing/api/mechanic/loot/operation/MultiplyWeightOperation.java new file mode 100644 index 00000000..757b99dd --- /dev/null +++ b/api/src/main/java/net/momirealms/customfishing/api/mechanic/loot/operation/MultiplyWeightOperation.java @@ -0,0 +1,38 @@ +/* + * Copyright (C) <2024> + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package net.momirealms.customfishing.api.mechanic.loot.operation; + +import net.momirealms.customfishing.api.mechanic.context.Context; +import net.momirealms.customfishing.api.mechanic.misc.value.MathValue; +import org.bukkit.entity.Player; + +import java.util.Map; + +public class MultiplyWeightOperation implements WeightOperation { + + private final MathValue arg; + + public MultiplyWeightOperation(MathValue arg) { + this.arg = arg; + } + + @Override + public Double apply(Context context, Double weight, Map weights) { + return weight * arg.evaluate(context); + } +} diff --git a/api/src/main/java/net/momirealms/customfishing/api/mechanic/loot/operation/ReduceWeightOperation.java b/api/src/main/java/net/momirealms/customfishing/api/mechanic/loot/operation/ReduceWeightOperation.java new file mode 100644 index 00000000..ba440e6c --- /dev/null +++ b/api/src/main/java/net/momirealms/customfishing/api/mechanic/loot/operation/ReduceWeightOperation.java @@ -0,0 +1,38 @@ +/* + * Copyright (C) <2024> + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package net.momirealms.customfishing.api.mechanic.loot.operation; + +import net.momirealms.customfishing.api.mechanic.context.Context; +import net.momirealms.customfishing.api.mechanic.misc.value.MathValue; +import org.bukkit.entity.Player; + +import java.util.Map; + +public class ReduceWeightOperation implements WeightOperation { + + private final MathValue arg; + + public ReduceWeightOperation(MathValue arg) { + this.arg = arg; + } + + @Override + public Double apply(Context context, Double weight, Map weights) { + return weight - arg.evaluate(context); + } +} diff --git a/api/src/main/java/net/momirealms/customfishing/api/mechanic/loot/operation/WeightOperation.java b/api/src/main/java/net/momirealms/customfishing/api/mechanic/loot/operation/WeightOperation.java new file mode 100644 index 00000000..3e9fcd7d --- /dev/null +++ b/api/src/main/java/net/momirealms/customfishing/api/mechanic/loot/operation/WeightOperation.java @@ -0,0 +1,28 @@ +/* + * Copyright (C) <2024> + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package net.momirealms.customfishing.api.mechanic.loot.operation; + +import net.momirealms.customfishing.api.mechanic.context.Context; +import net.momirealms.customfishing.common.util.TriFunction; +import org.bukkit.entity.Player; + +import java.util.Map; + +@FunctionalInterface +public interface WeightOperation extends TriFunction, Double, Map, Double> { +} diff --git a/core/src/main/java/net/momirealms/customfishing/bukkit/BukkitCustomFishingPluginImpl.java b/core/src/main/java/net/momirealms/customfishing/bukkit/BukkitCustomFishingPluginImpl.java index 61c56111..12cf2f1f 100644 --- a/core/src/main/java/net/momirealms/customfishing/bukkit/BukkitCustomFishingPluginImpl.java +++ b/core/src/main/java/net/momirealms/customfishing/bukkit/BukkitCustomFishingPluginImpl.java @@ -184,7 +184,7 @@ public class BukkitCustomFishingPluginImpl extends BukkitCustomFishingPlugin { this.actionManager.reload(); this.requirementManager.reload(); - this.gameManager.reload(); + this.gameManager.unload(); // before ConfigManager this.placeholderManager.reload(); @@ -205,6 +205,7 @@ public class BukkitCustomFishingPluginImpl extends BukkitCustomFishingPlugin { this.eventManager.load(); this.entityManager.load(); this.lootManager.load(); + this.gameManager.load(); this.blockManager.load(); this.effectManager.load(); this.hookManager.load(); diff --git a/core/src/main/java/net/momirealms/customfishing/bukkit/config/BukkitConfigManager.java b/core/src/main/java/net/momirealms/customfishing/bukkit/config/BukkitConfigManager.java index a2da1684..737a6fc7 100644 --- a/core/src/main/java/net/momirealms/customfishing/bukkit/config/BukkitConfigManager.java +++ b/core/src/main/java/net/momirealms/customfishing/bukkit/config/BukkitConfigManager.java @@ -48,6 +48,8 @@ import net.momirealms.customfishing.api.mechanic.effect.EffectProperties; import net.momirealms.customfishing.api.mechanic.event.EventManager; import net.momirealms.customfishing.api.mechanic.item.ItemEditor; import net.momirealms.customfishing.api.mechanic.loot.Loot; +import net.momirealms.customfishing.api.mechanic.loot.operation.*; +import net.momirealms.customfishing.api.mechanic.misc.placeholder.BukkitPlaceholderManager; import net.momirealms.customfishing.api.mechanic.misc.value.MathValue; import net.momirealms.customfishing.api.mechanic.misc.value.TextValue; import net.momirealms.customfishing.api.mechanic.requirement.Requirement; @@ -90,18 +92,20 @@ import java.io.IOException; import java.io.InputStream; import java.util.*; import java.util.function.BiConsumer; -import java.util.function.BiFunction; import java.util.function.Function; public class BukkitConfigManager extends ConfigManager { private static YamlDocument MAIN_CONFIG; private static Particle dustParticle; - public static YamlDocument getMainConfig() { return MAIN_CONFIG; } + private Function lootValidator = (id) -> { + return plugin.getLootManager().getLoot(id).isPresent(); + }; + public BukkitConfigManager(BukkitCustomFishingPlugin plugin) { super(plugin); this.registerBuiltInItemProperties(); @@ -627,7 +631,7 @@ public class BukkitConfigManager extends ConfigManager { })); } case "weight-mod" -> { - var op = parseWeightOperation(section.getStringList("value")); + var op = parseWeightOperation(section.getStringList("value"), lootValidator); return (((effect, context, phase) -> { if (phase == 1) { effect.weightOperations(op); @@ -636,7 +640,7 @@ public class BukkitConfigManager extends ConfigManager { })); } case "weight-mod-ignore-conditions" -> { - var op = parseWeightOperation(section.getStringList("value")); + var op = parseWeightOperation(section.getStringList("value"), lootValidator); return (((effect, context, phase) -> { if (phase == 1) { effect.weightOperationsIgnored(op); @@ -782,63 +786,73 @@ public class BukkitConfigManager extends ConfigManager { } } - private BiFunction, Double, Double> parseWeightOperation(String op) { + private WeightOperation parseWeightOperation(String op) { switch (op.charAt(0)) { case '/' -> { MathValue arg = MathValue.auto(op.substring(1)); - return (context, weight) -> weight / arg.evaluate(context); + return new DivideWeightOperation(arg); } case '*' -> { MathValue arg = MathValue.auto(op.substring(1)); - return (context, weight) -> weight * arg.evaluate(context); + return new MultiplyWeightOperation(arg); } case '-' -> { MathValue arg = MathValue.auto(op.substring(1)); - return (context, weight) -> weight - arg.evaluate(context); + return new ReduceWeightOperation(arg); } case '%' -> { MathValue arg = MathValue.auto(op.substring(1)); - return (context, weight) -> weight % arg.evaluate(context); + return new ModuloWeightOperation(arg); } case '+' -> { MathValue arg = MathValue.auto(op.substring(1)); - return (context, weight) -> weight + arg.evaluate(context); + return new AddWeightOperation(arg); } case '=' -> { - MathValue arg = MathValue.auto(op.substring(1)); - return (context, weight) -> { - context.arg(ContextKeys.WEIGHT, weight); - return arg.evaluate(context); - }; + String expression = op.substring(1); + MathValue arg = MathValue.auto(expression); + List placeholders = BukkitPlaceholderManager.getInstance().resolvePlaceholders(expression); + List otherWeights = new ArrayList<>(); + for (String placeholder : placeholders) { + if (placeholder.startsWith("{loot_")) { + otherWeights.add(placeholder.substring("{loot_".length(), placeholder.length() - 1)); + } + } + return new CustomWeightOperation(arg, expression.contains("{1}"), otherWeights); } default -> throw new IllegalArgumentException("Invalid weight operation: " + op); } } @Override - public List, Double, Double>>> parseWeightOperation(List ops) { - List, Double, Double>>> result = new ArrayList<>(); + public List> parseWeightOperation(List ops, Function validator) { + List> result = new ArrayList<>(); for (String op : ops) { String[] split = op.split(":", 2); if (split.length < 2) { plugin.getPluginLogger().warn("Illegal weight operation: " + op); continue; } - result.add(Pair.of(split[0], parseWeightOperation(split[1]))); + String id = split[0]; + if (!validator.apply(id)) { + plugin.getPluginLogger().warn("Illegal weight operation: " + op + ". Id " + id + " is not valid"); + continue; + } + result.add(Pair.of(id, parseWeightOperation(split[1]))); } return result; } @Override - public List, Double, Double>>> parseGroupWeightOperation(List gops) { - List, Double, Double>>> result = new ArrayList<>(); + public List> parseGroupWeightOperation(List gops) { + List> result = new ArrayList<>(); for (String gop : gops) { String[] split = gop.split(":", 2); if (split.length < 2) { plugin.getPluginLogger().warn("Illegal weight operation: " + gop); continue; } - BiFunction, Double, Double> operation = parseWeightOperation(split[1]); + WeightOperation operation = parseWeightOperation(split[1]); for (String member : plugin.getLootManager().getGroupMembers(split[0])) { result.add(Pair.of(member, operation)); } diff --git a/core/src/main/java/net/momirealms/customfishing/bukkit/game/BukkitGameManager.java b/core/src/main/java/net/momirealms/customfishing/bukkit/game/BukkitGameManager.java index 87f9cb28..ea9d2e3e 100644 --- a/core/src/main/java/net/momirealms/customfishing/bukkit/game/BukkitGameManager.java +++ b/core/src/main/java/net/momirealms/customfishing/bukkit/game/BukkitGameManager.java @@ -27,6 +27,7 @@ import net.momirealms.customfishing.api.mechanic.context.ContextKeys; import net.momirealms.customfishing.api.mechanic.effect.Effect; import net.momirealms.customfishing.api.mechanic.fishing.CustomFishingHook; import net.momirealms.customfishing.api.mechanic.game.*; +import net.momirealms.customfishing.api.mechanic.loot.operation.WeightOperation; import net.momirealms.customfishing.api.mechanic.misc.value.MathValue; import net.momirealms.customfishing.api.mechanic.misc.value.TextValue; import net.momirealms.customfishing.api.mechanic.requirement.ConditionalElement; @@ -52,7 +53,7 @@ public class BukkitGameManager implements GameManager { private final BukkitCustomFishingPlugin plugin; private final Map gameFactoryMap = new HashMap<>(); private final Map gameMap = new HashMap<>(); - private final LinkedHashMap, Double, Double>>>, Player>> gameConditions = new LinkedHashMap<>(); + private final LinkedHashMap>, Player>> gameConditions = new LinkedHashMap<>(); private static final String EXPANSION_FOLDER = "expansions/minigame"; public BukkitGameManager(BukkitCustomFishingPlugin plugin) { @@ -87,23 +88,23 @@ public class BukkitGameManager implements GameManager { this.gameMap.clear(); } - private ConditionalElement, Double, Double>>>, Player> parseGameConditions(Section section) { + private ConditionalElement>, Player> parseGameConditions(Section section) { Section subSection = section.getSection("sub-groups"); if (subSection == null) { return new ConditionalElement<>( - plugin.getConfigManager().parseWeightOperation(section.getStringList("list")), + plugin.getConfigManager().parseWeightOperation(section.getStringList("list"), (id) -> getGame(id).isPresent()), Map.of(), plugin.getRequirementManager().parseRequirements(section.getSection("conditions"), false) ); } else { - HashMap, Double, Double>>>, Player>> subElements = new HashMap<>(); + HashMap>, Player>> subElements = new HashMap<>(); for (Map.Entry entry : subSection.getStringRouteMappedValues(false).entrySet()) { if (entry.getValue() instanceof Section innerSection) { subElements.put(entry.getKey(), parseGameConditions(innerSection)); } } return new ConditionalElement<>( - plugin.getConfigManager().parseWeightOperation(section.getStringList("list")), + plugin.getConfigManager().parseWeightOperation(section.getStringList("list"), (id) -> getGame(id).isPresent()), subElements, plugin.getRequirementManager().parseRequirements(section.getSection("conditions"), false) ); @@ -143,24 +144,24 @@ public class BukkitGameManager implements GameManager { @Nullable @Override public Game getNextGame(Effect effect, Context context) { - HashMap lootWeightMap = new HashMap<>(); - for (ConditionalElement, Double, Double>>>, Player> conditionalElement : gameConditions.values()) { - modifyWeightMap(lootWeightMap, context, conditionalElement); + HashMap gameWeightMap = new HashMap<>(); + for (ConditionalElement>, Player> conditionalElement : gameConditions.values()) { + modifyWeightMap(gameWeightMap, context, conditionalElement); } - String gameID = WeightUtils.getRandom(lootWeightMap); + String gameID = WeightUtils.getRandom(gameWeightMap); return Optional.ofNullable(gameID) .map(id -> getGame(gameID).orElseThrow(() -> new RuntimeException("Could not find game " + gameID))) .orElse(null); } - private void modifyWeightMap(Map weightMap, Context context, ConditionalElement, Double, Double>>>, Player> conditionalElement) { + private void modifyWeightMap(Map weightMap, Context context, ConditionalElement>, Player> conditionalElement) { if (conditionalElement == null) return; if (RequirementManager.isSatisfied(context, conditionalElement.getRequirements())) { - for (Pair, Double, Double>> modifierPair : conditionalElement.getElement()) { + for (Pair modifierPair : conditionalElement.getElement()) { double previous = weightMap.getOrDefault(modifierPair.left(), 0d); - weightMap.put(modifierPair.left(), modifierPair.right().apply(context, previous)); + weightMap.put(modifierPair.left(), modifierPair.right().apply(context, previous, weightMap)); } - for (ConditionalElement, Double, Double>>>, Player> sub : conditionalElement.getSubElements().values()) { + for (ConditionalElement>, Player> sub : conditionalElement.getSubElements().values()) { modifyWeightMap(weightMap, context, sub); } } diff --git a/core/src/main/java/net/momirealms/customfishing/bukkit/integration/BukkitIntegrationManager.java b/core/src/main/java/net/momirealms/customfishing/bukkit/integration/BukkitIntegrationManager.java index 468a8943..5d49b350 100644 --- a/core/src/main/java/net/momirealms/customfishing/bukkit/integration/BukkitIntegrationManager.java +++ b/core/src/main/java/net/momirealms/customfishing/bukkit/integration/BukkitIntegrationManager.java @@ -39,7 +39,6 @@ import net.momirealms.customfishing.bukkit.integration.region.WorldGuardRegion; import net.momirealms.customfishing.bukkit.integration.season.AdvancedSeasonsProvider; import net.momirealms.customfishing.bukkit.integration.season.CustomCropsSeasonProvider; import net.momirealms.customfishing.bukkit.integration.season.RealisticSeasonsProvider; -import net.momirealms.customfishing.bukkit.integration.shop.ShopGUICFItemProvider; import net.momirealms.customfishing.bukkit.integration.shop.ShopGUIHook; import net.momirealms.customfishing.bukkit.item.BukkitItemManager; import net.momirealms.customfishing.common.util.Pair; diff --git a/core/src/main/java/net/momirealms/customfishing/bukkit/loot/BukkitLootManager.java b/core/src/main/java/net/momirealms/customfishing/bukkit/loot/BukkitLootManager.java index f3a7686b..5591c69a 100644 --- a/core/src/main/java/net/momirealms/customfishing/bukkit/loot/BukkitLootManager.java +++ b/core/src/main/java/net/momirealms/customfishing/bukkit/loot/BukkitLootManager.java @@ -24,6 +24,7 @@ import net.momirealms.customfishing.api.mechanic.context.Context; import net.momirealms.customfishing.api.mechanic.effect.Effect; import net.momirealms.customfishing.api.mechanic.loot.Loot; import net.momirealms.customfishing.api.mechanic.loot.LootManager; +import net.momirealms.customfishing.api.mechanic.loot.operation.WeightOperation; import net.momirealms.customfishing.api.mechanic.requirement.ConditionalElement; import net.momirealms.customfishing.api.mechanic.requirement.RequirementManager; import net.momirealms.customfishing.common.util.Pair; @@ -34,7 +35,6 @@ import org.jetbrains.annotations.Nullable; import java.io.File; import java.util.*; -import java.util.function.BiFunction; @SuppressWarnings("DuplicatedCode") public class BukkitLootManager implements LootManager { @@ -42,7 +42,7 @@ public class BukkitLootManager implements LootManager { private final BukkitCustomFishingPlugin plugin; private final HashMap lootMap = new HashMap<>(); private final HashMap> groupMembersMap = new HashMap<>(); - private final LinkedHashMap, Double, Double>>>, Player>> lootConditions = new LinkedHashMap<>(); + private final LinkedHashMap>, Player>> lootConditions = new LinkedHashMap<>(); public BukkitLootManager(BukkitCustomFishingPlugin plugin) { this.plugin = plugin; @@ -73,23 +73,23 @@ public class BukkitLootManager implements LootManager { } } - private ConditionalElement, Double, Double>>>, Player> parseLootConditions(Section section) { + private ConditionalElement>, Player> parseLootConditions(Section section) { Section subSection = section.getSection("sub-groups"); if (subSection == null) { return new ConditionalElement<>( - plugin.getConfigManager().parseWeightOperation(section.getStringList("list")), + plugin.getConfigManager().parseWeightOperation(section.getStringList("list"), (id) -> getLoot(id).isPresent()), Map.of(), plugin.getRequirementManager().parseRequirements(section.getSection("conditions"), false) ); } else { - HashMap, Double, Double>>>, Player>> subElements = new HashMap<>(); + HashMap>, Player>> subElements = new HashMap<>(); for (Map.Entry entry : subSection.getStringRouteMappedValues(false).entrySet()) { if (entry.getValue() instanceof Section innerSection) { subElements.put(entry.getKey(), parseLootConditions(innerSection)); } } return new ConditionalElement<>( - plugin.getConfigManager().parseWeightOperation(section.getStringList("list")), + plugin.getConfigManager().parseWeightOperation(section.getStringList("list"), (id) -> getLoot(id).isPresent()), subElements, plugin.getRequirementManager().parseRequirements(section.getSection("conditions"), false) ); @@ -136,18 +136,18 @@ public class BukkitLootManager implements LootManager { @Override public HashMap getWeightedLoots(Effect effect, Context context) { HashMap lootWeightMap = new HashMap<>(); - for (ConditionalElement, Double, Double>>>, Player> conditionalElement : lootConditions.values()) { + for (ConditionalElement>, Player> conditionalElement : lootConditions.values()) { modifyWeightMap(lootWeightMap, context, conditionalElement); } - for (Pair, Double, Double>> pair : effect.weightOperations()) { + for (Pair pair : effect.weightOperations()) { Double previous = lootWeightMap.get(pair.left()); if (previous != null) { - lootWeightMap.put(pair.left(), pair.right().apply(context, previous)); + lootWeightMap.put(pair.left(), pair.right().apply(context, previous, lootWeightMap)); } } - for (Pair, Double, Double>> pair : effect.weightOperationsIgnored()) { + for (Pair pair : effect.weightOperationsIgnored()) { double previous = lootWeightMap.getOrDefault(pair.left(), 0d); - lootWeightMap.put(pair.left(), pair.right().apply(context, previous)); + lootWeightMap.put(pair.left(), pair.right().apply(context, previous, lootWeightMap)); } return lootWeightMap; } @@ -155,36 +155,36 @@ public class BukkitLootManager implements LootManager { @Nullable @Override public Loot getNextLoot(Effect effect, Context context) { - HashMap lootWeightMap = new HashMap<>(); - for (ConditionalElement, Double, Double>>>, Player> conditionalElement : lootConditions.values()) { - modifyWeightMap(lootWeightMap, context, conditionalElement); + HashMap weightMap = new HashMap<>(); + for (ConditionalElement>, Player> conditionalElement : lootConditions.values()) { + modifyWeightMap(weightMap, context, conditionalElement); } - for (Pair, Double, Double>> pair : effect.weightOperations()) { - Double previous = lootWeightMap.get(pair.left()); + for (Pair pair : effect.weightOperations()) { + Double previous = weightMap.get(pair.left()); if (previous != null) { - lootWeightMap.put(pair.left(), pair.right().apply(context, previous)); + weightMap.put(pair.left(), pair.right().apply(context, previous, weightMap)); } } - for (Pair, Double, Double>> pair : effect.weightOperationsIgnored()) { - double previous = lootWeightMap.getOrDefault(pair.left(), 0d); - lootWeightMap.put(pair.left(), pair.right().apply(context, previous)); + for (Pair pair : effect.weightOperationsIgnored()) { + double previous = weightMap.getOrDefault(pair.left(), 0d); + weightMap.put(pair.left(), pair.right().apply(context, previous, weightMap)); } - plugin.debug(lootWeightMap); - String lootID = WeightUtils.getRandom(lootWeightMap); + plugin.debug(weightMap::toString); + String lootID = WeightUtils.getRandom(weightMap); return Optional.ofNullable(lootID) .map(id -> getLoot(lootID).orElseThrow(() -> new NullPointerException("Could not find loot " + lootID))) .orElse(null); } - private void modifyWeightMap(Map weightMap, Context context, ConditionalElement, Double, Double>>>, Player> conditionalElement) { + private void modifyWeightMap(Map weightMap, Context context, ConditionalElement>, Player> conditionalElement) { if (conditionalElement == null) return; if (RequirementManager.isSatisfied(context, conditionalElement.getRequirements())) { - for (Pair, Double, Double>> modifierPair : conditionalElement.getElement()) { + for (Pair modifierPair : conditionalElement.getElement()) { double previous = weightMap.getOrDefault(modifierPair.left(), 0d); - weightMap.put(modifierPair.left(), modifierPair.right().apply(context, previous)); + weightMap.put(modifierPair.left(), modifierPair.right().apply(context, previous, weightMap)); } - for (ConditionalElement, Double, Double>>>, Player> sub : conditionalElement.getSubElements().values()) { + for (ConditionalElement>, Player> sub : conditionalElement.getSubElements().values()) { modifyWeightMap(weightMap, context, sub); } } diff --git a/gradle.properties b/gradle.properties index 5f349b19..96ae5874 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,6 +1,6 @@ # Project settings # Rule: [major update].[feature update].[bug fix] -project_version=2.2.36 +project_version=2.3.0 config_version=38 project_group=net.momirealms @@ -46,9 +46,9 @@ guava_version=33.3.1-jre lz4_version=1.8.0 # Proxy settings -#systemProp.socks.proxyHost=127.0.0.1 -#systemProp.socks.proxyPort=7890 -#systemProp.http.proxyHost=127.0.0.1 -#systemProp.http.proxyPort=7890 -#systemProp.https.proxyHost=127.0.0.1 -#systemProp.https.proxyPort=7890 \ No newline at end of file +systemProp.socks.proxyHost=127.0.0.1 +systemProp.socks.proxyPort=7890 +systemProp.http.proxyHost=127.0.0.1 +systemProp.http.proxyPort=7890 +systemProp.https.proxyHost=127.0.0.1 +systemProp.https.proxyPort=7890 \ No newline at end of file