mirror of
https://github.com/Xiao-MoMi/Custom-Crops.git
synced 2025-12-27 19:09:09 +00:00
Added expression support for chance
This commit is contained in:
@@ -20,6 +20,7 @@ package net.momirealms.customcrops.api.action;
|
||||
import dev.dejvokep.boostedyaml.block.implementation.Section;
|
||||
import net.momirealms.customcrops.api.BukkitCustomCropsPlugin;
|
||||
import net.momirealms.customcrops.api.action.builtin.*;
|
||||
import net.momirealms.customcrops.api.misc.value.MathValue;
|
||||
import net.momirealms.customcrops.common.util.ClassUtils;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
@@ -96,7 +97,7 @@ public abstract class AbstractActionManager<T> implements ActionManager<T> {
|
||||
plugin.getPluginLogger().warn("Action type: " + section.getString("type") + " doesn't exist.");
|
||||
return Action.empty();
|
||||
}
|
||||
return factory.process(section.get("value"), section.getDouble("chance", 1d));
|
||||
return factory.process(section.get("value"), section.contains("chance") ? MathValue.auto(section.get("chance")) : MathValue.plain(1d));
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@@ -122,7 +123,7 @@ public abstract class AbstractActionManager<T> implements ActionManager<T> {
|
||||
plugin.getPluginLogger().warn("Action type: " + type + " doesn't exist.");
|
||||
return Action.empty();
|
||||
}
|
||||
return factory.process(args, 1);
|
||||
return factory.process(args, MathValue.plain(1));
|
||||
}
|
||||
|
||||
@SuppressWarnings({"ResultOfMethodCallIgnored", "unchecked"})
|
||||
|
||||
@@ -17,6 +17,8 @@
|
||||
|
||||
package net.momirealms.customcrops.api.action;
|
||||
|
||||
import net.momirealms.customcrops.api.misc.value.MathValue;
|
||||
|
||||
/**
|
||||
* Interface representing a factory for creating actions.
|
||||
*
|
||||
@@ -30,5 +32,5 @@ public interface ActionFactory<T> {
|
||||
* @param args the args containing the arguments needed to build the action
|
||||
* @return the constructed action
|
||||
*/
|
||||
Action<T> process(Object args, double chance);
|
||||
Action<T> process(Object args, MathValue<T> chance);
|
||||
}
|
||||
|
||||
@@ -20,13 +20,15 @@ package net.momirealms.customcrops.api.action.builtin;
|
||||
import net.momirealms.customcrops.api.BukkitCustomCropsPlugin;
|
||||
import net.momirealms.customcrops.api.action.Action;
|
||||
import net.momirealms.customcrops.api.context.Context;
|
||||
import net.momirealms.customcrops.api.misc.value.MathValue;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
public abstract class AbstractBuiltInAction<T> implements Action<T> {
|
||||
|
||||
protected final BukkitCustomCropsPlugin plugin;
|
||||
protected final double chance;
|
||||
protected final MathValue<T> chance;
|
||||
|
||||
protected AbstractBuiltInAction(BukkitCustomCropsPlugin plugin, double chance) {
|
||||
protected AbstractBuiltInAction(BukkitCustomCropsPlugin plugin, MathValue<T> chance) {
|
||||
this.plugin = plugin;
|
||||
this.chance = chance;
|
||||
}
|
||||
@@ -35,13 +37,13 @@ public abstract class AbstractBuiltInAction<T> implements Action<T> {
|
||||
return plugin;
|
||||
}
|
||||
|
||||
public double chance() {
|
||||
public MathValue<T> chance() {
|
||||
return chance;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void trigger(Context<T> context) {
|
||||
if (Math.random() > chance) return;
|
||||
if (Math.random() > chance.evaluate(context)) return;
|
||||
triggerAction(context);
|
||||
}
|
||||
|
||||
|
||||
@@ -39,7 +39,7 @@ public class ActionActionbarNearby<T> extends AbstractBuiltInAction<T> {
|
||||
public ActionActionbarNearby(
|
||||
BukkitCustomCropsPlugin plugin,
|
||||
Section section,
|
||||
double chance
|
||||
MathValue<T> chance
|
||||
) {
|
||||
super(plugin, chance);
|
||||
this.actionbar = section.getString("actionbar");
|
||||
|
||||
@@ -29,6 +29,7 @@ import net.momirealms.customcrops.api.core.world.CustomCropsBlockState;
|
||||
import net.momirealms.customcrops.api.core.world.CustomCropsWorld;
|
||||
import net.momirealms.customcrops.api.core.world.Pos3;
|
||||
import net.momirealms.customcrops.api.core.wrapper.WrappedBreakEvent;
|
||||
import net.momirealms.customcrops.api.misc.value.MathValue;
|
||||
import net.momirealms.customcrops.api.util.DummyCancellable;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.entity.Player;
|
||||
@@ -46,7 +47,7 @@ public class ActionBreak<T> extends AbstractBuiltInAction<T> {
|
||||
public ActionBreak(
|
||||
BukkitCustomCropsPlugin plugin,
|
||||
Object args,
|
||||
double chance
|
||||
MathValue<T> chance
|
||||
) {
|
||||
super(plugin, chance);
|
||||
this.triggerEvent = Optional.ofNullable(args).map(it -> (boolean) it).orElse(true);
|
||||
|
||||
@@ -21,6 +21,7 @@ import net.kyori.adventure.audience.Audience;
|
||||
import net.momirealms.customcrops.api.BukkitCustomCropsPlugin;
|
||||
import net.momirealms.customcrops.api.context.Context;
|
||||
import net.momirealms.customcrops.api.context.ContextKeys;
|
||||
import net.momirealms.customcrops.api.misc.value.MathValue;
|
||||
import net.momirealms.customcrops.common.helper.AdventureHelper;
|
||||
import net.momirealms.customcrops.common.util.ListUtils;
|
||||
import org.bukkit.Bukkit;
|
||||
@@ -36,7 +37,7 @@ public class ActionBroadcast<T> extends AbstractBuiltInAction<T> {
|
||||
public ActionBroadcast(
|
||||
BukkitCustomCropsPlugin plugin,
|
||||
Object args,
|
||||
double chance
|
||||
MathValue<T> chance
|
||||
) {
|
||||
super(plugin, chance);
|
||||
this.messages = ListUtils.toList(args);
|
||||
|
||||
@@ -22,6 +22,7 @@ import net.momirealms.customcrops.api.BukkitCustomCropsPlugin;
|
||||
import net.momirealms.customcrops.api.action.AbstractActionManager;
|
||||
import net.momirealms.customcrops.api.action.Action;
|
||||
import net.momirealms.customcrops.api.context.Context;
|
||||
import net.momirealms.customcrops.api.misc.value.MathValue;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
@@ -35,7 +36,7 @@ public class ActionChain<T> extends AbstractBuiltInAction<T> {
|
||||
BukkitCustomCropsPlugin plugin,
|
||||
AbstractActionManager<T> manager,
|
||||
Object args,
|
||||
double chance
|
||||
MathValue<T> chance
|
||||
) {
|
||||
super(plugin, chance);
|
||||
this.actions = new ArrayList<>();
|
||||
|
||||
@@ -21,6 +21,7 @@ import net.momirealms.customcrops.api.BukkitCustomCropsPlugin;
|
||||
import net.momirealms.customcrops.api.context.Context;
|
||||
import net.momirealms.customcrops.api.context.ContextKeys;
|
||||
import net.momirealms.customcrops.api.misc.placeholder.BukkitPlaceholderManager;
|
||||
import net.momirealms.customcrops.api.misc.value.MathValue;
|
||||
import net.momirealms.customcrops.common.util.ListUtils;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
@@ -35,7 +36,7 @@ public class ActionCommand<T> extends AbstractBuiltInAction<T> {
|
||||
public ActionCommand(
|
||||
BukkitCustomCropsPlugin plugin,
|
||||
Object args,
|
||||
double chance
|
||||
MathValue<T> chance
|
||||
) {
|
||||
super(plugin, chance);
|
||||
this.commands = ListUtils.toList(args);
|
||||
|
||||
@@ -42,7 +42,7 @@ public class ActionCommandNearby<T> extends AbstractBuiltInAction<T> {
|
||||
public ActionCommandNearby(
|
||||
BukkitCustomCropsPlugin plugin,
|
||||
Section section,
|
||||
double chance
|
||||
MathValue<T> chance
|
||||
) {
|
||||
super(plugin, chance);
|
||||
this.cmd = ListUtils.toList(section.get("command"));
|
||||
|
||||
@@ -22,6 +22,7 @@ import net.momirealms.customcrops.api.BukkitCustomCropsPlugin;
|
||||
import net.momirealms.customcrops.api.action.AbstractActionManager;
|
||||
import net.momirealms.customcrops.api.action.Action;
|
||||
import net.momirealms.customcrops.api.context.Context;
|
||||
import net.momirealms.customcrops.api.misc.value.MathValue;
|
||||
import net.momirealms.customcrops.api.requirement.Requirement;
|
||||
|
||||
public class ActionConditional<T> extends AbstractBuiltInAction<T> {
|
||||
@@ -34,7 +35,7 @@ public class ActionConditional<T> extends AbstractBuiltInAction<T> {
|
||||
AbstractActionManager<T> manager,
|
||||
Class<T> tClass,
|
||||
Section section,
|
||||
double chance
|
||||
MathValue<T> chance
|
||||
) {
|
||||
super(plugin, chance);
|
||||
this.actions = manager.parseActions(section.getSection("actions"));
|
||||
|
||||
@@ -23,6 +23,7 @@ import net.momirealms.customcrops.api.action.AbstractActionManager;
|
||||
import net.momirealms.customcrops.api.action.Action;
|
||||
import net.momirealms.customcrops.api.context.Context;
|
||||
import net.momirealms.customcrops.api.context.ContextKeys;
|
||||
import net.momirealms.customcrops.api.misc.value.MathValue;
|
||||
import org.bukkit.Location;
|
||||
|
||||
import java.util.ArrayList;
|
||||
@@ -40,7 +41,7 @@ public class ActionDelay<T> extends AbstractBuiltInAction<T> {
|
||||
BukkitCustomCropsPlugin plugin,
|
||||
AbstractActionManager<T> manager,
|
||||
Object args,
|
||||
double chance
|
||||
MathValue<T> chance
|
||||
) {
|
||||
super(plugin, chance);
|
||||
this.actions = new ArrayList<>();
|
||||
|
||||
@@ -55,7 +55,7 @@ public class ActionDropItem<T> extends AbstractBuiltInAction<T> {
|
||||
public ActionDropItem(
|
||||
BukkitCustomCropsPlugin plugin,
|
||||
Section section,
|
||||
double chance
|
||||
MathValue<T> chance
|
||||
) {
|
||||
super(plugin, chance);
|
||||
this.ignoreFertilizer = section.getBoolean("ignore-fertilizer", true);
|
||||
@@ -75,6 +75,7 @@ public class ActionDropItem<T> extends AbstractBuiltInAction<T> {
|
||||
player = null;
|
||||
}
|
||||
int random = RandomUtils.generateRandomInt((int) min.evaluate(context), (int) max.evaluate(context));
|
||||
if (random <= 0) return;
|
||||
ItemStack itemStack = generateItem(location, player, random);
|
||||
plugin.getScheduler().sync().run(() -> {
|
||||
DropItemActionEvent actionEvent = new DropItemActionEvent(context, location, item, itemStack);
|
||||
|
||||
@@ -22,6 +22,7 @@ import net.momirealms.customcrops.api.BukkitCustomCropsPlugin;
|
||||
import net.momirealms.customcrops.api.action.Action;
|
||||
import net.momirealms.customcrops.api.action.ActionManager;
|
||||
import net.momirealms.customcrops.api.context.Context;
|
||||
import net.momirealms.customcrops.api.misc.value.MathValue;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
@@ -37,7 +38,7 @@ public class ActionDropItemLegacy<T> extends AbstractBuiltInAction<T> {
|
||||
BukkitCustomCropsPlugin plugin,
|
||||
ActionManager<T> manager,
|
||||
Section section,
|
||||
double chance
|
||||
MathValue<T> chance
|
||||
) {
|
||||
super(plugin, chance);
|
||||
this.actions = new ArrayList<>();
|
||||
@@ -45,13 +46,13 @@ public class ActionDropItemLegacy<T> extends AbstractBuiltInAction<T> {
|
||||
if (otherItemSection != null) {
|
||||
for (Map.Entry<String, Object> entry : otherItemSection.getStringRouteMappedValues(false).entrySet()) {
|
||||
if (entry.getValue() instanceof Section inner) {
|
||||
actions.add(requireNonNull(manager.getActionFactory("drop-item")).process(inner, inner.getDouble("chance", 1D)));
|
||||
actions.add(requireNonNull(manager.getActionFactory("drop-item")).process(inner, section.contains("chance") ? MathValue.auto(section.get("chance")) : MathValue.plain(1d)));
|
||||
}
|
||||
}
|
||||
}
|
||||
Section qualitySection = section.getSection("quality-crops");
|
||||
if (qualitySection != null) {
|
||||
actions.add(requireNonNull(manager.getActionFactory("quality-crops")).process(qualitySection, 1));
|
||||
actions.add(requireNonNull(manager.getActionFactory("quality-crops")).process(qualitySection, MathValue.plain(1)));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -53,7 +53,7 @@ public class ActionFakeItem<T> extends AbstractBuiltInAction<T> {
|
||||
public ActionFakeItem(
|
||||
BukkitCustomCropsPlugin plugin,
|
||||
Section section,
|
||||
double chance
|
||||
MathValue<T> chance
|
||||
) {
|
||||
super(plugin, chance);
|
||||
String itemID = section.getString("item", "");
|
||||
|
||||
@@ -34,7 +34,7 @@ public class ActionHologram<T> extends AbstractBuiltInAction<T> {
|
||||
public ActionHologram(
|
||||
BukkitCustomCropsPlugin plugin,
|
||||
Section section,
|
||||
double chance
|
||||
MathValue<T> chance
|
||||
) {
|
||||
super(plugin, chance);
|
||||
this.text = TextValue.auto(section.getString("text", ""));
|
||||
|
||||
@@ -43,7 +43,7 @@ public class ActionMessageNearby<T> extends AbstractBuiltInAction<T> {
|
||||
public ActionMessageNearby(
|
||||
BukkitCustomCropsPlugin plugin,
|
||||
Section section,
|
||||
double chance
|
||||
MathValue<T> chance
|
||||
) {
|
||||
super(plugin, chance);
|
||||
this.messages = ListUtils.toList(section.get("message"));
|
||||
|
||||
@@ -21,6 +21,7 @@ import dev.dejvokep.boostedyaml.block.implementation.Section;
|
||||
import net.momirealms.customcrops.api.BukkitCustomCropsPlugin;
|
||||
import net.momirealms.customcrops.api.context.Context;
|
||||
import net.momirealms.customcrops.api.context.ContextKeys;
|
||||
import net.momirealms.customcrops.api.misc.value.MathValue;
|
||||
import net.momirealms.customcrops.api.util.ParticleUtils;
|
||||
import org.bukkit.Color;
|
||||
import org.bukkit.Location;
|
||||
@@ -51,7 +52,7 @@ public class ActionParticle<T> extends AbstractBuiltInAction<T> {
|
||||
public ActionParticle(
|
||||
BukkitCustomCropsPlugin plugin,
|
||||
Section section,
|
||||
double chance
|
||||
MathValue<T> chance
|
||||
) {
|
||||
super(plugin, chance);
|
||||
this.particleType = ParticleUtils.getParticle(section.getString("particle", "ASH").toUpperCase(Locale.ENGLISH));
|
||||
|
||||
@@ -35,6 +35,7 @@ import net.momirealms.customcrops.api.core.world.CustomCropsBlockState;
|
||||
import net.momirealms.customcrops.api.core.world.CustomCropsWorld;
|
||||
import net.momirealms.customcrops.api.core.world.Pos3;
|
||||
import net.momirealms.customcrops.api.event.CropPlantEvent;
|
||||
import net.momirealms.customcrops.api.misc.value.MathValue;
|
||||
import net.momirealms.customcrops.api.util.EventUtils;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.entity.Player;
|
||||
@@ -54,7 +55,7 @@ public class ActionPlant<T> extends AbstractBuiltInAction<T> {
|
||||
public ActionPlant(
|
||||
BukkitCustomCropsPlugin plugin,
|
||||
Section section,
|
||||
double chance
|
||||
MathValue<T> chance
|
||||
) {
|
||||
super(plugin, chance);
|
||||
this.point = section.getInt("point", 0);
|
||||
|
||||
@@ -22,6 +22,7 @@ import net.momirealms.customcrops.api.BukkitCustomCropsPlugin;
|
||||
import net.momirealms.customcrops.api.action.AbstractActionManager;
|
||||
import net.momirealms.customcrops.api.action.Action;
|
||||
import net.momirealms.customcrops.api.context.Context;
|
||||
import net.momirealms.customcrops.api.misc.value.MathValue;
|
||||
import net.momirealms.customcrops.api.requirement.Requirement;
|
||||
import net.momirealms.customcrops.common.util.Pair;
|
||||
|
||||
@@ -38,7 +39,7 @@ public class ActionPriority<T> extends AbstractBuiltInAction<T> {
|
||||
AbstractActionManager<T> manager,
|
||||
Class<T> tClass,
|
||||
Section section,
|
||||
double chance
|
||||
MathValue<T> chance
|
||||
) {
|
||||
super(plugin, chance);
|
||||
this.conditionActionPairList = new ArrayList<>();
|
||||
|
||||
@@ -56,7 +56,7 @@ public class ActionQualityCrops<T> extends AbstractBuiltInAction<T> {
|
||||
public ActionQualityCrops(
|
||||
BukkitCustomCropsPlugin plugin,
|
||||
Section section,
|
||||
double chance
|
||||
MathValue<T> chance
|
||||
) {
|
||||
super(plugin, chance);
|
||||
this.min = MathValue.auto(section.get("min"));
|
||||
@@ -76,6 +76,7 @@ public class ActionQualityCrops<T> extends AbstractBuiltInAction<T> {
|
||||
protected void triggerAction(Context<T> context) {
|
||||
Location location = requireNonNull(context.arg(ContextKeys.LOCATION));
|
||||
int random = RandomUtils.generateRandomInt((int) min.evaluate(context), (int) max.evaluate(context));
|
||||
if (random <= 0) return;
|
||||
Player player;
|
||||
if (context.holder() instanceof Player p) {
|
||||
player = p;
|
||||
|
||||
@@ -21,6 +21,7 @@ import net.momirealms.customcrops.api.BukkitCustomCropsPlugin;
|
||||
import net.momirealms.customcrops.api.context.Context;
|
||||
import net.momirealms.customcrops.api.context.ContextKeys;
|
||||
import net.momirealms.customcrops.api.misc.placeholder.BukkitPlaceholderManager;
|
||||
import net.momirealms.customcrops.api.misc.value.MathValue;
|
||||
import net.momirealms.customcrops.common.util.ListUtils;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
@@ -36,7 +37,7 @@ public class ActionRandomCommand<T> extends AbstractBuiltInAction<T> {
|
||||
public ActionRandomCommand(
|
||||
BukkitCustomCropsPlugin plugin,
|
||||
Object args,
|
||||
double chance
|
||||
MathValue<T> chance
|
||||
) {
|
||||
super(plugin, chance);
|
||||
this.commands = ListUtils.toList(args);
|
||||
|
||||
@@ -22,6 +22,7 @@ import net.momirealms.customcrops.api.BukkitCustomCropsPlugin;
|
||||
import net.momirealms.customcrops.api.context.Context;
|
||||
import net.momirealms.customcrops.api.context.ContextKeys;
|
||||
import net.momirealms.customcrops.api.integration.EntityProvider;
|
||||
import net.momirealms.customcrops.api.misc.value.MathValue;
|
||||
import org.bukkit.Location;
|
||||
|
||||
import java.util.HashMap;
|
||||
@@ -37,7 +38,7 @@ public class ActionSpawnEntity<T> extends AbstractBuiltInAction<T> {
|
||||
public ActionSpawnEntity(
|
||||
BukkitCustomCropsPlugin plugin,
|
||||
Section section,
|
||||
double chance
|
||||
MathValue<T> chance
|
||||
) {
|
||||
super(plugin, chance);
|
||||
this.id = section.getString("id");
|
||||
|
||||
@@ -23,6 +23,7 @@ import net.momirealms.customcrops.api.action.AbstractActionManager;
|
||||
import net.momirealms.customcrops.api.action.Action;
|
||||
import net.momirealms.customcrops.api.context.Context;
|
||||
import net.momirealms.customcrops.api.context.ContextKeys;
|
||||
import net.momirealms.customcrops.api.misc.value.MathValue;
|
||||
import net.momirealms.customcrops.common.plugin.scheduler.SchedulerTask;
|
||||
import org.bukkit.Location;
|
||||
|
||||
@@ -41,7 +42,7 @@ public class ActionTimer<T> extends AbstractBuiltInAction<T> {
|
||||
BukkitCustomCropsPlugin plugin,
|
||||
AbstractActionManager<T> manager,
|
||||
Object args,
|
||||
double chance
|
||||
MathValue<T> chance
|
||||
) {
|
||||
super(plugin, chance);
|
||||
this.actions = new ArrayList<>();
|
||||
|
||||
@@ -22,6 +22,7 @@ import net.kyori.adventure.audience.Audience;
|
||||
import net.momirealms.customcrops.api.BukkitCustomCropsPlugin;
|
||||
import net.momirealms.customcrops.api.context.Context;
|
||||
import net.momirealms.customcrops.api.context.ContextKeys;
|
||||
import net.momirealms.customcrops.api.misc.value.MathValue;
|
||||
import net.momirealms.customcrops.api.misc.value.TextValue;
|
||||
import net.momirealms.customcrops.api.util.LocationUtils;
|
||||
import net.momirealms.customcrops.common.helper.AdventureHelper;
|
||||
@@ -42,7 +43,7 @@ public class ActionTitleNearby<T> extends AbstractBuiltInAction<T> {
|
||||
public ActionTitleNearby(
|
||||
BukkitCustomCropsPlugin plugin,
|
||||
Section section,
|
||||
double chance
|
||||
MathValue<T> chance
|
||||
) {
|
||||
super(plugin, chance);
|
||||
this.title = TextValue.auto(section.getString("title"));
|
||||
|
||||
@@ -33,6 +33,7 @@ import net.momirealms.customcrops.api.core.mechanic.fertilizer.FertilizerConfig;
|
||||
import net.momirealms.customcrops.api.core.world.CustomCropsBlockState;
|
||||
import net.momirealms.customcrops.api.core.world.CustomCropsWorld;
|
||||
import net.momirealms.customcrops.api.core.world.Pos3;
|
||||
import net.momirealms.customcrops.api.misc.value.MathValue;
|
||||
import org.bukkit.Location;
|
||||
|
||||
import java.util.*;
|
||||
@@ -47,7 +48,7 @@ public class ActionVariation<T> extends AbstractBuiltInAction<T> {
|
||||
public ActionVariation(
|
||||
BukkitCustomCropsPlugin plugin,
|
||||
Section section,
|
||||
double chance
|
||||
MathValue<T> chance
|
||||
) {
|
||||
super(plugin, chance);
|
||||
ignore = section.getBoolean("ignore-fertilizer", false);
|
||||
|
||||
Reference in New Issue
Block a user