mirror of
https://github.com/Xiao-MoMi/craft-engine.git
synced 2025-12-29 11:59:11 +00:00
添加新的条件类型和函数类型
This commit is contained in:
@@ -8,8 +8,8 @@ import net.momirealms.craftengine.core.plugin.config.ConfigParser;
|
||||
import net.momirealms.craftengine.core.util.Key;
|
||||
import org.incendo.cloud.suggestion.Suggestion;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
import java.util.Collection;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
@@ -36,11 +36,14 @@ public interface BlockManager extends Manageable, ModelGenerator {
|
||||
|
||||
int availableAppearances(Key blockType);
|
||||
|
||||
Key getBlockOwnerId(PackedBlockState state);
|
||||
Key getBlockOwnerId(BlockStateWrapper state);
|
||||
|
||||
@NotNull
|
||||
ImmutableBlockState getImmutableBlockStateUnsafe(int stateId);
|
||||
|
||||
@Nullable
|
||||
ImmutableBlockState getImmutableBlockState(int stateId);
|
||||
|
||||
@Nullable
|
||||
BlockStateWrapper createPackedBlockState(String blockState);
|
||||
}
|
||||
|
||||
@@ -1,15 +1,15 @@
|
||||
package net.momirealms.craftengine.core.block;
|
||||
|
||||
public class BlockRegistryMirror {
|
||||
private static PackedBlockState[] customBlockStates;
|
||||
private static PackedBlockState stoneState;
|
||||
private static BlockStateWrapper[] customBlockStates;
|
||||
private static BlockStateWrapper stoneState;
|
||||
|
||||
public static void init(PackedBlockState[] states, PackedBlockState state) {
|
||||
public static void init(BlockStateWrapper[] states, BlockStateWrapper state) {
|
||||
customBlockStates = states;
|
||||
stoneState = state;
|
||||
}
|
||||
|
||||
public static PackedBlockState stateByRegistryId(int vanillaId) {
|
||||
public static BlockStateWrapper stateByRegistryId(int vanillaId) {
|
||||
if (vanillaId < 0) return stoneState;
|
||||
return customBlockStates[vanillaId];
|
||||
}
|
||||
|
||||
@@ -0,0 +1,67 @@
|
||||
package net.momirealms.craftengine.core.block;
|
||||
|
||||
public interface BlockStateWrapper {
|
||||
|
||||
Object handle();
|
||||
|
||||
int registryId();
|
||||
|
||||
boolean isVanillaBlock();
|
||||
|
||||
static BlockStateWrapper vanilla(Object handle, int registryId) {
|
||||
return new VanillaBlockState(handle, registryId);
|
||||
}
|
||||
|
||||
static BlockStateWrapper custom(Object handle, int registryId) {
|
||||
return new CustomBlockState(handle, registryId);
|
||||
}
|
||||
|
||||
static BlockStateWrapper create(Object handle, int registryId, boolean isVanillaBlock) {
|
||||
if (isVanillaBlock) return new VanillaBlockState(handle, registryId);
|
||||
else return new CustomBlockState(handle, registryId);
|
||||
}
|
||||
|
||||
abstract class AbstractBlockState implements BlockStateWrapper {
|
||||
protected final Object handle;
|
||||
protected final int registryId;
|
||||
|
||||
public AbstractBlockState(Object handle, int registryId) {
|
||||
this.handle = handle;
|
||||
this.registryId = registryId;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object handle() {
|
||||
return this.handle;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int registryId() {
|
||||
return this.registryId;
|
||||
}
|
||||
}
|
||||
|
||||
class VanillaBlockState extends AbstractBlockState {
|
||||
|
||||
public VanillaBlockState(Object handle, int registryId) {
|
||||
super(handle, registryId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isVanillaBlock() {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
class CustomBlockState extends AbstractBlockState {
|
||||
|
||||
public CustomBlockState(Object handle, int registryId) {
|
||||
super(handle, registryId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isVanillaBlock() {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -14,14 +14,14 @@ import net.momirealms.sparrow.nbt.CompoundTag;
|
||||
import net.momirealms.sparrow.nbt.NBT;
|
||||
import net.momirealms.sparrow.nbt.Tag;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
import java.util.List;
|
||||
|
||||
public class ImmutableBlockState extends BlockStateHolder {
|
||||
private CompoundTag tag;
|
||||
private PackedBlockState customBlockState;
|
||||
private PackedBlockState vanillaBlockState;
|
||||
private BlockStateWrapper customBlockState;
|
||||
private BlockStateWrapper vanillaBlockState;
|
||||
|
||||
private BlockBehavior behavior;
|
||||
private Integer hashCode;
|
||||
@@ -81,19 +81,19 @@ public class ImmutableBlockState extends BlockStateHolder {
|
||||
return settings.pushReaction;
|
||||
}
|
||||
|
||||
public PackedBlockState customBlockState() {
|
||||
public BlockStateWrapper customBlockState() {
|
||||
return this.customBlockState;
|
||||
}
|
||||
|
||||
public PackedBlockState vanillaBlockState() {
|
||||
public BlockStateWrapper vanillaBlockState() {
|
||||
return this.vanillaBlockState;
|
||||
}
|
||||
|
||||
public void setCustomBlockState(@NotNull PackedBlockState customBlockState) {
|
||||
public void setCustomBlockState(@NotNull BlockStateWrapper customBlockState) {
|
||||
this.customBlockState = customBlockState;
|
||||
}
|
||||
|
||||
public void setVanillaBlockState(@NotNull PackedBlockState vanillaBlockState) {
|
||||
public void setVanillaBlockState(@NotNull BlockStateWrapper vanillaBlockState) {
|
||||
this.vanillaBlockState = vanillaBlockState;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,4 +0,0 @@
|
||||
package net.momirealms.craftengine.core.block;
|
||||
|
||||
public record PackedBlockState(Object handle, int registryId) {
|
||||
}
|
||||
@@ -2,5 +2,5 @@ package net.momirealms.craftengine.core.block;
|
||||
|
||||
import net.momirealms.craftengine.core.util.Key;
|
||||
|
||||
public record VanillaBlock(Key type, String properties, int registryId) {
|
||||
public record VanillaBlockState(Key type, String properties, int registryId) {
|
||||
}
|
||||
@@ -5,8 +5,8 @@ import net.momirealms.craftengine.core.item.context.BlockPlaceContext;
|
||||
import net.momirealms.craftengine.core.util.Direction;
|
||||
import net.momirealms.craftengine.core.util.HorizontalDirection;
|
||||
import net.momirealms.sparrow.nbt.Tag;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@@ -2,8 +2,6 @@ package net.momirealms.craftengine.core.entity.furniture;
|
||||
|
||||
import net.momirealms.craftengine.core.entity.player.Player;
|
||||
import net.momirealms.craftengine.core.util.Key;
|
||||
import net.momirealms.craftengine.core.world.Vec3d;
|
||||
import net.momirealms.craftengine.core.world.World;
|
||||
import net.momirealms.craftengine.core.world.WorldPosition;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.joml.Vector3f;
|
||||
|
||||
@@ -6,8 +6,8 @@ import net.momirealms.craftengine.core.plugin.config.ConfigParser;
|
||||
import net.momirealms.craftengine.core.util.Key;
|
||||
import net.momirealms.craftengine.core.world.WorldPosition;
|
||||
import org.incendo.cloud.suggestion.Suggestion;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
import java.util.Collection;
|
||||
import java.util.Optional;
|
||||
|
||||
|
||||
@@ -36,6 +36,8 @@ public abstract class Player extends AbstractEntity implements NetWorkUser {
|
||||
|
||||
public abstract void abortMiningBlock();
|
||||
|
||||
public abstract void breakBlock(int x, int y, int z);
|
||||
|
||||
public abstract double getCachedInteractionRange();
|
||||
|
||||
public abstract void onSwingHand();
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package net.momirealms.craftengine.core.font;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class Emoji {
|
||||
|
||||
@@ -11,8 +11,8 @@ import net.momirealms.craftengine.core.plugin.config.ConfigParser;
|
||||
import net.momirealms.craftengine.core.registry.Holder;
|
||||
import net.momirealms.craftengine.core.util.Key;
|
||||
import org.incendo.cloud.suggestion.Suggestion;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
import java.util.*;
|
||||
|
||||
public interface ItemManager<T> extends Manageable, ModelGenerator {
|
||||
|
||||
@@ -4,8 +4,8 @@ import it.unimi.dsi.fastutil.ints.IntArrayList;
|
||||
import it.unimi.dsi.fastutil.ints.IntList;
|
||||
import it.unimi.dsi.fastutil.objects.Reference2IntMap;
|
||||
import it.unimi.dsi.fastutil.objects.Reference2IntOpenHashMap;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
import java.util.ArrayList;
|
||||
import java.util.BitSet;
|
||||
import java.util.List;
|
||||
|
||||
@@ -33,6 +33,7 @@ public class LootConditions {
|
||||
register(CommonConditions.PERMISSION, new PermissionCondition.FactoryImpl<>());
|
||||
register(CommonConditions.EQUALS, new EqualsCondition.FactoryImpl<>());
|
||||
register(CommonConditions.EXPRESSION, new ExpressionCondition.FactoryImpl<>());
|
||||
register(CommonConditions.IS_NULL, new IsNullCondition.FactoryImpl<>());
|
||||
}
|
||||
|
||||
public static void register(Key key, ConditionFactory<LootContext> factory) {
|
||||
|
||||
@@ -5,8 +5,7 @@ import net.momirealms.craftengine.core.plugin.context.ContextHolder;
|
||||
import net.momirealms.craftengine.core.plugin.context.PlayerOptionalContext;
|
||||
import net.momirealms.craftengine.core.world.World;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
public class LootContext extends PlayerOptionalContext {
|
||||
private final World world;
|
||||
|
||||
@@ -10,8 +10,8 @@ import net.momirealms.craftengine.core.pack.host.ResourcePackHosts;
|
||||
import net.momirealms.craftengine.core.plugin.CraftEngine;
|
||||
import net.momirealms.craftengine.core.plugin.locale.LocalizedException;
|
||||
import net.momirealms.craftengine.core.util.*;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
package net.momirealms.craftengine.core.plugin.context;
|
||||
|
||||
import com.google.common.collect.ImmutableMap;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
@@ -20,4 +20,5 @@ public final class CommonConditions {
|
||||
public static final Key PERMISSION = Key.from("craftengine:permission");
|
||||
public static final Key EQUALS = Key.from("craftengine:equals");
|
||||
public static final Key EXPRESSION = Key.from("craftengine:expression");
|
||||
public static final Key IS_NULL = Key.from("craftengine:is_null");
|
||||
}
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
package net.momirealms.craftengine.core.plugin.context.condition;
|
||||
|
||||
import net.momirealms.craftengine.core.plugin.context.Condition;
|
||||
import net.momirealms.craftengine.core.plugin.context.Context;
|
||||
import net.momirealms.craftengine.core.plugin.context.ContextKey;
|
||||
import net.momirealms.craftengine.core.util.Key;
|
||||
import net.momirealms.craftengine.core.util.ResourceConfigUtils;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
|
||||
public class IsNullCondition<CTX extends Context> implements Condition<CTX> {
|
||||
private final ContextKey<?> key;
|
||||
|
||||
public IsNullCondition(ContextKey<?> key) {
|
||||
this.key = key;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Key type() {
|
||||
return CommonConditions.IS_NULL;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean test(CTX ctx) {
|
||||
Optional<?> optional = ctx.getOptionalParameter(this.key);
|
||||
return optional.isPresent();
|
||||
}
|
||||
|
||||
public static class FactoryImpl<CTX extends Context> implements ConditionFactory<CTX> {
|
||||
|
||||
@Override
|
||||
public Condition<CTX> create(Map<String, Object> arguments) {
|
||||
String argument = ResourceConfigUtils.requireNonEmptyStringOrThrow(arguments.get("argument"), "warning.config.condition.is_null.missing_argument");
|
||||
return new IsNullCondition<>(ContextKey.chain(argument));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -31,6 +31,7 @@ public class EventConditions {
|
||||
register(CommonConditions.PERMISSION, new PermissionCondition.FactoryImpl<>());
|
||||
register(CommonConditions.EQUALS, new EqualsCondition.FactoryImpl<>());
|
||||
register(CommonConditions.EXPRESSION, new ExpressionCondition.FactoryImpl<>());
|
||||
register(CommonConditions.IS_NULL, new IsNullCondition.FactoryImpl<>());
|
||||
}
|
||||
|
||||
public static void register(Key key, ConditionFactory<PlayerOptionalContext> factory) {
|
||||
|
||||
@@ -24,6 +24,8 @@ public class EventFunctions {
|
||||
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));
|
||||
register(CommonFunctions.PLACE_BLOCK, new PlaceBlockFunction.FactoryImpl<>(EventConditions::fromMap));
|
||||
register(CommonFunctions.BREAK_BLOCK, new BreakBlockFunction.FactoryImpl<>(EventConditions::fromMap));
|
||||
}
|
||||
|
||||
public static void register(Key key, FunctionFactory<PlayerOptionalContext> factory) {
|
||||
|
||||
@@ -10,8 +10,8 @@ import net.momirealms.craftengine.core.plugin.context.text.TextProviders;
|
||||
import net.momirealms.craftengine.core.util.AdventureHelper;
|
||||
import net.momirealms.craftengine.core.util.Key;
|
||||
import net.momirealms.craftengine.core.util.ResourceConfigUtils;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
|
||||
@@ -0,0 +1,53 @@
|
||||
package net.momirealms.craftengine.core.plugin.context.function;
|
||||
|
||||
import net.momirealms.craftengine.core.entity.player.Player;
|
||||
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.util.Key;
|
||||
import net.momirealms.craftengine.core.util.MCUtils;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
|
||||
public class BreakBlockFunction<CTX extends Context> extends AbstractConditionalFunction<CTX> {
|
||||
private final NumberProvider x;
|
||||
private final NumberProvider y;
|
||||
private final NumberProvider z;
|
||||
|
||||
public BreakBlockFunction(NumberProvider x, NumberProvider y, NumberProvider z, List<Condition<CTX>> predicates) {
|
||||
super(predicates);
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
this.z = z;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void runInternal(CTX ctx) {
|
||||
Optional<Player> optionalPlayer = ctx.getOptionalParameter(DirectContextParameters.PLAYER);
|
||||
optionalPlayer.ifPresent(player -> player.breakBlock(MCUtils.fastFloor(x.getDouble(ctx)), MCUtils.fastFloor(y.getDouble(ctx)), MCUtils.fastFloor(z.getDouble(ctx))));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Key type() {
|
||||
return CommonFunctions.BREAK_BLOCK;
|
||||
}
|
||||
|
||||
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) {
|
||||
NumberProvider x = NumberProviders.fromObject(arguments.getOrDefault("x", "<arg:block.block_x>"));
|
||||
NumberProvider y = NumberProviders.fromObject(arguments.getOrDefault("y", "<arg:block.block_y>"));
|
||||
NumberProvider z = NumberProviders.fromObject(arguments.getOrDefault("z", "<arg:block.block_z>"));
|
||||
return new BreakBlockFunction<>(x, y, z, getPredicates(arguments));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -12,8 +12,8 @@ import net.momirealms.craftengine.core.plugin.context.text.TextProviders;
|
||||
import net.momirealms.craftengine.core.util.Key;
|
||||
import net.momirealms.craftengine.core.util.MiscUtils;
|
||||
import net.momirealms.craftengine.core.util.ResourceConfigUtils;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
|
||||
@@ -16,9 +16,11 @@ public final class CommonFunctions {
|
||||
public static final Key POTION_EFFECT = Key.of("craftengine:potion_effect");
|
||||
public static final Key BREAK_BLOCK = Key.of("craftengine:break_block");
|
||||
public static final Key CANCEL_EVENT = Key.of("craftengine:cancel_event");
|
||||
public static final Key PLACE_BLOCK = Key.of("craftengine:place_block");
|
||||
public static final Key FOOD = Key.of("craftengine:food");
|
||||
public static final Key SATURATION = Key.of("craftengine:saturation");
|
||||
public static final Key MONEY = Key.of("craftengine:money");
|
||||
public static final Key OXYGEN = Key.of("craftengine:oxygen");
|
||||
public static final Key MINE_RADIUS = Key.of("craftengine:mine_radius");
|
||||
public static final Key DROP_LOOT = Key.of("craftengine:drop_loot");
|
||||
}
|
||||
|
||||
@@ -11,8 +11,8 @@ import net.momirealms.craftengine.core.util.AdventureHelper;
|
||||
import net.momirealms.craftengine.core.util.Key;
|
||||
import net.momirealms.craftengine.core.util.MiscUtils;
|
||||
import net.momirealms.craftengine.core.util.ResourceConfigUtils;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
|
||||
@@ -14,8 +14,8 @@ import net.momirealms.craftengine.core.util.AdventureHelper;
|
||||
import net.momirealms.craftengine.core.util.EnumUtils;
|
||||
import net.momirealms.craftengine.core.util.Key;
|
||||
import net.momirealms.craftengine.core.util.ResourceConfigUtils;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
|
||||
@@ -0,0 +1,87 @@
|
||||
package net.momirealms.craftengine.core.plugin.context.function;
|
||||
|
||||
import net.momirealms.craftengine.core.block.BlockStateWrapper;
|
||||
import net.momirealms.craftengine.core.block.UpdateOption;
|
||||
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.util.Key;
|
||||
import net.momirealms.craftengine.core.util.MCUtils;
|
||||
import net.momirealms.craftengine.core.util.ResourceConfigUtils;
|
||||
import net.momirealms.craftengine.core.world.World;
|
||||
import net.momirealms.craftengine.core.world.WorldPosition;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
|
||||
public class PlaceBlockFunction<CTX extends Context> extends AbstractConditionalFunction<CTX> {
|
||||
private final DelayedInitBlockState delayedInitBlockState;
|
||||
private final NumberProvider x;
|
||||
private final NumberProvider y;
|
||||
private final NumberProvider z;
|
||||
private final NumberProvider updateFlags;
|
||||
|
||||
public PlaceBlockFunction(DelayedInitBlockState delayedInitBlockState, NumberProvider x, NumberProvider y, NumberProvider z, NumberProvider updateFlags, List<Condition<CTX>> predicates) {
|
||||
super(predicates);
|
||||
this.delayedInitBlockState = delayedInitBlockState;
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
this.z = z;
|
||||
this.updateFlags = updateFlags;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void runInternal(CTX ctx) {
|
||||
Optional<WorldPosition> optionalWorldPosition = ctx.getOptionalParameter(DirectContextParameters.POSITION);
|
||||
if (optionalWorldPosition.isPresent()) {
|
||||
World world = optionalWorldPosition.get().world();
|
||||
world.setBlockAt(MCUtils.fastFloor(this.x.getDouble(ctx)), MCUtils.fastFloor(this.y.getDouble(ctx)), MCUtils.fastFloor(this.z.getDouble(ctx)), this.delayedInitBlockState.getState(), this.updateFlags.getInt(ctx));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Key type() {
|
||||
return CommonFunctions.PLACE_BLOCK;
|
||||
}
|
||||
|
||||
public static class DelayedInitBlockState {
|
||||
private final String state;
|
||||
private BlockStateWrapper packedBlockState;
|
||||
|
||||
public DelayedInitBlockState(String state) {
|
||||
this.state = state;
|
||||
}
|
||||
|
||||
public BlockStateWrapper getState() {
|
||||
if (this.packedBlockState == null) {
|
||||
this.packedBlockState = CraftEngine.instance().blockManager().createPackedBlockState(state);
|
||||
if (this.packedBlockState == null) {
|
||||
CraftEngine.instance().logger().warn("Could not create block state: " + this.state);
|
||||
}
|
||||
}
|
||||
return this.packedBlockState;
|
||||
}
|
||||
}
|
||||
|
||||
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) {
|
||||
String state = ResourceConfigUtils.requireNonEmptyStringOrThrow(arguments.get("block-state"), "warning.config.function.place_block.missing_block_state");
|
||||
DelayedInitBlockState delayedInitBlockState = new DelayedInitBlockState(state);
|
||||
NumberProvider x = NumberProviders.fromObject(arguments.getOrDefault("x", "<arg:block.block_x>"));
|
||||
NumberProvider y = NumberProviders.fromObject(arguments.getOrDefault("y", "<arg:block.block_y>"));
|
||||
NumberProvider z = NumberProviders.fromObject(arguments.getOrDefault("z", "<arg:block.block_z>"));
|
||||
NumberProvider flags = Optional.ofNullable(arguments.get("update-flags")).map(NumberProviders::fromObject).orElse(NumberProviders.direct(UpdateOption.UPDATE_ALL.flags()));
|
||||
return new PlaceBlockFunction<>(delayedInitBlockState, x, y, z, flags, getPredicates(arguments));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -11,8 +11,8 @@ import net.momirealms.craftengine.core.plugin.context.text.TextProvider;
|
||||
import net.momirealms.craftengine.core.plugin.context.text.TextProviders;
|
||||
import net.momirealms.craftengine.core.util.AdventureHelper;
|
||||
import net.momirealms.craftengine.core.util.Key;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
|
||||
@@ -31,6 +31,18 @@ public class ExpressionNumberProvider implements NumberProvider {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public double getDouble(Context context) {
|
||||
Component resultComponent = AdventureHelper.customMiniMessage().deserialize(this.expr, context.tagResolvers());
|
||||
String resultString = AdventureHelper.plainTextContent(resultComponent);
|
||||
Expression expression = new Expression(resultString);
|
||||
try {
|
||||
return expression.evaluate().getNumberValue().doubleValue();
|
||||
} catch (EvaluationException | ParseException e) {
|
||||
throw new RuntimeException("Invalid expression: " + this.expr + " -> " + resultString + " -> Cannot parse", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Key type() {
|
||||
return NumberProviders.EXPRESSION;
|
||||
|
||||
@@ -10,14 +10,19 @@ import java.util.Map;
|
||||
|
||||
public class FixedNumberProvider implements NumberProvider {
|
||||
public static final FactoryImpl FACTORY = new FactoryImpl();
|
||||
private final float value;
|
||||
private final double value;
|
||||
|
||||
public FixedNumberProvider(float value) {
|
||||
public FixedNumberProvider(double value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public float getFloat(Context context) {
|
||||
return (float) this.value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public double getDouble(Context context) {
|
||||
return this.value;
|
||||
}
|
||||
|
||||
@@ -32,12 +37,12 @@ public class FixedNumberProvider implements NumberProvider {
|
||||
public NumberProvider create(Map<String, Object> arguments) {
|
||||
String plainOrExpression = ResourceConfigUtils.requireNonEmptyStringOrThrow(arguments.get("value"), "warning.config.number.fixed.missing_value");
|
||||
try {
|
||||
float value = Float.parseFloat(plainOrExpression);
|
||||
double value = Double.parseDouble(plainOrExpression);
|
||||
return new FixedNumberProvider(value);
|
||||
} catch (NumberFormatException e) {
|
||||
Expression expression = new Expression(plainOrExpression);
|
||||
try {
|
||||
return new FixedNumberProvider(expression.evaluate().getNumberValue().floatValue());
|
||||
return new FixedNumberProvider(expression.evaluate().getNumberValue().doubleValue());
|
||||
} catch (Exception e1) {
|
||||
throw new LocalizedResourceConfigException("warning.config.number.fixed.invalid_value", e1, plainOrExpression);
|
||||
}
|
||||
|
||||
@@ -7,6 +7,8 @@ public interface NumberProvider {
|
||||
|
||||
float getFloat(Context context);
|
||||
|
||||
double getDouble(Context context);
|
||||
|
||||
default int getInt(Context context) {
|
||||
return Math.round(this.getFloat(context));
|
||||
}
|
||||
|
||||
@@ -41,6 +41,10 @@ public class NumberProviders {
|
||||
return functions;
|
||||
}
|
||||
|
||||
public static NumberProvider direct(double value) {
|
||||
return new FixedNumberProvider(value);
|
||||
}
|
||||
|
||||
public static NumberProvider fromMap(Map<String, Object> map) {
|
||||
String type = ResourceConfigUtils.requireNonEmptyStringOrThrow(map.get("type"), "warning.config.number.missing_type");
|
||||
Key key = Key.withDefaultNamespace(type, Key.DEFAULT_NAMESPACE);
|
||||
|
||||
@@ -30,6 +30,11 @@ public class UniformNumberProvider implements NumberProvider {
|
||||
return RandomUtils.generateRandomInt(this.min.getInt(context), this.max.getInt(context) + 1);
|
||||
}
|
||||
|
||||
@Override
|
||||
public double getDouble(Context context) {
|
||||
return RandomUtils.generateRandomDouble(this.min.getDouble(context), this.max.getDouble(context));
|
||||
}
|
||||
|
||||
@Override
|
||||
public float getFloat(Context context) {
|
||||
return RandomUtils.generateRandomFloat(this.min.getFloat(context), this.max.getFloat(context));
|
||||
|
||||
@@ -3,8 +3,8 @@ package net.momirealms.craftengine.core.registry;
|
||||
import it.unimi.dsi.fastutil.objects.ReferenceOpenHashSet;
|
||||
import net.momirealms.craftengine.core.util.Key;
|
||||
import net.momirealms.craftengine.core.util.ResourceKey;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.Optional;
|
||||
|
||||
@@ -3,8 +3,8 @@ package net.momirealms.craftengine.core.registry;
|
||||
import com.google.common.collect.Maps;
|
||||
import net.momirealms.craftengine.core.util.Key;
|
||||
import net.momirealms.craftengine.core.util.ResourceKey;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
import java.util.*;
|
||||
|
||||
public class MappedRegistry<T> implements WritableRegistry<T> {
|
||||
|
||||
@@ -2,8 +2,8 @@ package net.momirealms.craftengine.core.registry;
|
||||
|
||||
import net.momirealms.craftengine.core.util.Key;
|
||||
import net.momirealms.craftengine.core.util.ResourceKey;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
package net.momirealms.craftengine.core.util;
|
||||
|
||||
import com.google.common.collect.Iterators;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package net.momirealms.craftengine.core.world;
|
||||
|
||||
import net.momirealms.craftengine.core.block.BlockStateWrapper;
|
||||
import net.momirealms.craftengine.core.item.Item;
|
||||
import net.momirealms.craftengine.core.sound.SoundData;
|
||||
import net.momirealms.craftengine.core.util.Key;
|
||||
@@ -21,6 +22,8 @@ public interface World {
|
||||
return getBlockAt(pos.x(), pos.y(), pos.z());
|
||||
}
|
||||
|
||||
void setBlockAt(int x, int y, int z, BlockStateWrapper blockState, int flags);
|
||||
|
||||
String name();
|
||||
|
||||
Path directory();
|
||||
|
||||
@@ -5,8 +5,8 @@ import net.jpountz.lz4.LZ4BlockInputStream;
|
||||
import net.jpountz.lz4.LZ4BlockOutputStream;
|
||||
import net.momirealms.craftengine.core.plugin.CraftEngine;
|
||||
import net.momirealms.craftengine.core.plugin.dependency.Dependencies;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
import java.io.BufferedOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
@@ -5,8 +5,8 @@ import net.momirealms.craftengine.core.plugin.logger.PluginLogger;
|
||||
import net.momirealms.craftengine.core.world.ChunkPos;
|
||||
import net.momirealms.sparrow.nbt.CompoundTag;
|
||||
import net.momirealms.sparrow.nbt.NBT;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
import java.io.*;
|
||||
import java.nio.Buffer;
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
@@ -3,8 +3,8 @@ package net.momirealms.craftengine.core.world.collision;
|
||||
import net.momirealms.craftengine.core.util.Direction;
|
||||
import net.momirealms.craftengine.core.world.EntityHitResult;
|
||||
import net.momirealms.craftengine.core.world.Vec3d;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
import java.util.Optional;
|
||||
|
||||
public class AABB {
|
||||
|
||||
Reference in New Issue
Block a user