9
0
mirror of https://github.com/Xiao-MoMi/craft-engine.git synced 2025-12-27 19:09:08 +00:00

添加新方法

This commit is contained in:
XiaoMoMi
2025-05-19 04:22:01 +08:00
parent af5c63e940
commit 15475b0538
17 changed files with 342 additions and 73 deletions

View File

@@ -33,7 +33,7 @@ public class EqualsCondition<CTX extends Context> implements Condition<CTX> {
@Override
public Condition<CTX> create(Map<String, Object> arguments) {
String value1 = ResourceConfigUtils.requireNonEmptyStringOrThrow(arguments.get("value1"), "warning.config.condition.equals.missing_value1");
String value2 = ResourceConfigUtils.requireNonEmptyStringOrThrow(arguments.get("value1"), "warning.config.condition.equals.missing_value2");
String value2 = ResourceConfigUtils.requireNonEmptyStringOrThrow(arguments.get("value2"), "warning.config.condition.equals.missing_value2");
return new EqualsCondition<>(TextProviders.fromString(value1), TextProviders.fromString(value2));
}
}

View File

@@ -51,7 +51,7 @@ public abstract class AbstractConditionalFunction<CTX extends Context> implement
} else if (predicates instanceof Map<?,?> map) {
return List.of(factory.apply(MiscUtils.castToMap(map, false)));
}
throw new IllegalArgumentException("Unsupported condition type: " + predicates.getClass().getSimpleName());
throw new UnsupportedOperationException("Unsupported conditions argument class type: " + predicates.getClass().getSimpleName());
}
}
}

View File

@@ -5,11 +5,12 @@ import net.momirealms.craftengine.core.util.Key;
public final class CommonFunctions {
private CommonFunctions() {}
public static final Key RUN_ALL = Key.of("craftengine:run_all");
public static final Key RUN = Key.of("craftengine:run");
public static final Key COMMAND = Key.of("craftengine:command");
public static final Key MESSAGE = Key.of("craftengine:message");
public static final Key ACTIONBAR = Key.of("craftengine:actionbar");
public static final Key TITLE = Key.of("craftengine:title");
public static final Key OPEN_WINDOW = Key.of("craftengine:open_window");
public static final Key PARTICLE = Key.of("craftengine:particle");
public static final Key SOUND = Key.of("craftengine:sound");
public static final Key POTION_EFFECT = Key.of("craftengine:potion_effect");

View File

@@ -0,0 +1,77 @@
package net.momirealms.craftengine.core.plugin.context.function;
import net.momirealms.craftengine.core.entity.player.Player;
import net.momirealms.craftengine.core.plugin.CraftEngine;
import net.momirealms.craftengine.core.plugin.context.*;
import net.momirealms.craftengine.core.plugin.context.parameter.DirectContextParameters;
import net.momirealms.craftengine.core.plugin.context.selector.PlayerSelector;
import net.momirealms.craftengine.core.plugin.context.selector.PlayerSelectors;
import net.momirealms.craftengine.core.plugin.context.text.TextProvider;
import net.momirealms.craftengine.core.plugin.context.text.TextProviders;
import net.momirealms.craftengine.core.plugin.gui.GuiType;
import net.momirealms.craftengine.core.plugin.locale.LocalizedResourceConfigException;
import net.momirealms.craftengine.core.util.*;
import javax.annotation.Nullable;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Optional;
public class OpenWindowFunction<CTX extends Context> extends AbstractConditionalFunction<CTX> {
private final PlayerSelector<CTX> selector;
private final GuiType guiType;
private final TextProvider optionalTitle;
public OpenWindowFunction(List<Condition<CTX>> predicates, @Nullable PlayerSelector<CTX> selector, GuiType guiType, TextProvider optionalTitle) {
super(predicates);
this.selector = selector;
this.guiType = guiType;
this.optionalTitle = optionalTitle;
}
@Override
public void runInternal(CTX ctx) {
Optional<Player> owner = ctx.getOptionalParameter(DirectContextParameters.PLAYER);
if (this.selector == null) {
owner.ifPresent(it -> {
CraftEngine.instance().guiManager().openInventory(it, this.guiType);
if (this.optionalTitle != null) {
CraftEngine.instance().guiManager().updateInventoryTitle(it, AdventureHelper.miniMessage().deserialize(this.optionalTitle.get(ctx), ctx.tagResolvers()));
}
});
} else {
for (Player viewer : this.selector.get(ctx)) {
CraftEngine.instance().guiManager().openInventory(viewer, this.guiType);
if (this.optionalTitle != null) {
RelationalContext relationalContext = ViewerContext.of(ctx, PlayerOptionalContext.of(viewer, ContextHolder.EMPTY));
CraftEngine.instance().guiManager().updateInventoryTitle(viewer, AdventureHelper.miniMessage().deserialize(this.optionalTitle.get(relationalContext), relationalContext.tagResolvers()));
}
}
}
}
@Override
public Key type() {
return CommonFunctions.OPEN_WINDOW;
}
public static class FactoryImpl<CTX extends Context> extends AbstractFactory<CTX> {
public FactoryImpl(java.util.function.Function<Map<String, Object>, Condition<CTX>> factory) {
super(factory);
}
@Override
public Function<CTX> create(Map<String, Object> arguments) {
TextProvider title = Optional.ofNullable(arguments.get("title")).map(String::valueOf).map(TextProviders::fromString).orElse(null);
String rawType = ResourceConfigUtils.requireNonEmptyStringOrThrow(arguments.get("gui-type"), "warning.config.function.open_window.missing_gui_type");
try {
GuiType type = GuiType.valueOf(rawType.toUpperCase(Locale.ENGLISH));
return new OpenWindowFunction<>(getPredicates(arguments), PlayerSelectors.fromObject(arguments.get("target"), conditionFactory()), type, title);
} catch (IllegalArgumentException e) {
throw new LocalizedResourceConfigException("warning.config.function.open_window.invalid_gui_type", e, rawType, EnumUtils.toString(GuiType.values()));
}
}
}
}

View File

@@ -0,0 +1,79 @@
package net.momirealms.craftengine.core.plugin.context.function;
import net.momirealms.craftengine.core.plugin.CraftEngine;
import net.momirealms.craftengine.core.plugin.context.Condition;
import net.momirealms.craftengine.core.plugin.context.Context;
import net.momirealms.craftengine.core.plugin.context.number.NumberProvider;
import net.momirealms.craftengine.core.plugin.context.number.NumberProviders;
import net.momirealms.craftengine.core.plugin.context.parameter.DirectContextParameters;
import net.momirealms.craftengine.core.plugin.event.EventFunctions;
import net.momirealms.craftengine.core.util.*;
import net.momirealms.craftengine.core.world.WorldPosition;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
public class RunFunction<CTX extends Context> extends AbstractConditionalFunction<CTX> {
private final List<Function<CTX>> functions;
private final NumberProvider delay;
public RunFunction(List<Function<CTX>> functions, NumberProvider delay, List<Condition<CTX>> predicates) {
super(predicates);
this.functions = functions;
this.delay = delay;
}
@Override
public void runInternal(CTX ctx) {
int delay = this.delay.getInt(ctx);
if (delay <= 0) {
for (Function<CTX> function : functions) {
function.run(ctx);
}
} else {
Optional<WorldPosition> position = ctx.getOptionalParameter(DirectContextParameters.POSITION);
if (!VersionHelper.isFolia() || position.isEmpty()) {
CraftEngine.instance().scheduler().sync().runLater(() -> {
for (Function<CTX> function : functions) {
function.run(ctx);
}
}, delay);
} else {
WorldPosition pos = position.get();
CraftEngine.instance().scheduler().sync().runLater(() -> {
for (Function<CTX> function : functions) {
function.run(ctx);
}
}, delay, pos.world().platformWorld(), MCUtils.fastFloor(pos.x()) >> 4, MCUtils.fastFloor(pos.z()) >> 4);
}
}
}
@Override
public Key type() {
return CommonFunctions.RUN;
}
public static class FactoryImpl<CTX extends Context> extends AbstractFactory<CTX> {
private final java.util.function.Function<Map<String, Object>, Function<CTX>> functionFactory;
public FactoryImpl(java.util.function.Function<Map<String, Object>, Function<CTX>> functionFactory, java.util.function.Function<Map<String, Object>, Condition<CTX>> conditionFactory) {
super(conditionFactory);
this.functionFactory = functionFactory;
}
@Override
public Function<CTX> create(Map<String, Object> arguments) {
NumberProvider delay = NumberProviders.fromObject(arguments.getOrDefault("delay", 0));
@SuppressWarnings("unchecked")
List<Map<String, Object>> functions = (List<Map<String, Object>>) ResourceConfigUtils.requireNonNullOrThrow(arguments.get("functions"), "warning.config.function.run.missing_functions");
List<Function<CTX>> fun = new ArrayList<>();
for (Map<String, Object> function : functions) {
fun.add(this.functionFactory.apply(function));
}
return new RunFunction<>(fun, delay, getPredicates(arguments));
}
}
}

View File

@@ -24,7 +24,9 @@ public class EventFunctions {
register(CommonFunctions.MESSAGE, new MessageFunction.FactoryImpl<>(EventConditions::fromMap));
register(CommonFunctions.ACTIONBAR, new ActionBarFunction.FactoryImpl<>(EventConditions::fromMap));
register(CommonFunctions.TITLE, new TitleFunction.FactoryImpl<>(EventConditions::fromMap));
register(CommonFunctions.OPEN_WINDOW, new OpenWindowFunction.FactoryImpl<>(EventConditions::fromMap));
register(CommonFunctions.CANCEL_EVENT, new CancelEventFunction.FactoryImpl<>(EventConditions::fromMap));
register(CommonFunctions.RUN, new RunFunction.FactoryImpl<>(EventFunctions::fromMap, EventConditions::fromMap));
}
public static void register(Key key, FunctionFactory<PlayerOptionalContext> factory) {

View File

@@ -1,8 +1,14 @@
package net.momirealms.craftengine.core.plugin.gui;
import net.kyori.adventure.text.Component;
import net.momirealms.craftengine.core.entity.player.Player;
import net.momirealms.craftengine.core.plugin.Manageable;
public interface GuiManager extends Manageable {
void openInventory(Player player, GuiType guiType);
void updateInventoryTitle(Player player, Component component);
Inventory createInventory(Gui gui, int size);
}

View File

@@ -0,0 +1,11 @@
package net.momirealms.craftengine.core.plugin.gui;
public enum GuiType {
ANVIL,
CARTOGRAPHY,
ENCHANTMENT,
GRINDSTONE,
LOOM,
SMITHING,
CRAFTING
}

View File

@@ -20,6 +20,10 @@ public interface RegionExecutor<W> extends Executor {
SchedulerTask runAsyncLater(Runnable runnable, long delay);
default SchedulerTask runLater(Runnable runnable, long delay) {
return runLater(runnable, delay, null, 0 ,0);
}
SchedulerTask runLater(Runnable runnable, long delay, W world, int x, int z);
SchedulerTask runRepeating(Runnable runnable, long delay, long period, W world, int x, int z);