diff --git a/src/main/java/carpet/script/api/Auxiliary.java b/src/main/java/carpet/script/api/Auxiliary.java index f354ae2c..d05251f6 100644 --- a/src/main/java/carpet/script/api/Auxiliary.java +++ b/src/main/java/carpet/script/api/Auxiliary.java @@ -591,7 +591,7 @@ public class Auxiliary { expression.addLazyFunction("current_dimension", 0, (c, t, lv) -> { ServerCommandSource s = ((CarpetContext)c).s; - Value retval = ValueConversions.dimName(s.getWorld()); + Value retval = ValueConversions.of(s.getWorld()); return (cc, tt) -> retval; }); diff --git a/src/main/java/carpet/script/api/WorldAccess.java b/src/main/java/carpet/script/api/WorldAccess.java index 93d208f9..d01f0d28 100644 --- a/src/main/java/carpet/script/api/WorldAccess.java +++ b/src/main/java/carpet/script/api/WorldAccess.java @@ -1088,7 +1088,7 @@ public class WorldAccess { StructureStart start = FeatureGenerator.shouldStructureStartAt(world, pos, structure, needSize); if (start == null) return LazyValue.NULL; if (!needSize) return LazyValue.TRUE; - Value ret = ValueConversions.fromStructure(start); + Value ret = ValueConversions.of(start); return (_c, _t) -> ret; } Map ret = new HashMap<>(); @@ -1098,7 +1098,7 @@ public class WorldAccess { if (start == null) continue; Value key = new StringValue(NBTSerializableValue.nameFromRegistryId(Registry.STRUCTURE_FEATURE.getId(str))); - ret.put(key, (!needSize)?Value.NULL: ValueConversions.fromStructure(start)); + ret.put(key, (!needSize)?Value.NULL: ValueConversions.of(start)); } Value retMap = MapValue.wrap(ret); return (_c, _t) -> retMap; @@ -1130,7 +1130,7 @@ public class WorldAccess { } String structureName = lv.get(locator.offset).evalValue(c).getString().toLowerCase(Locale.ROOT); StructureStart start = structures.get(Registry.STRUCTURE_FEATURE.get(new Identifier(structureName))); - Value ret = ValueConversions.fromStructure(start); + Value ret = ValueConversions.of(start); return (_c, _t) -> ret; }); diff --git a/src/main/java/carpet/script/command/CommandArgument.java b/src/main/java/carpet/script/command/CommandArgument.java index 20fc95a0..2f388d48 100644 --- a/src/main/java/carpet/script/command/CommandArgument.java +++ b/src/main/java/carpet/script/command/CommandArgument.java @@ -4,6 +4,7 @@ import carpet.fakes.BlockStateArgumentInterface; import carpet.script.CarpetScriptHost; import carpet.script.exception.InternalExpressionException; import carpet.script.value.BlockValue; +import carpet.script.value.EntityValue; import carpet.script.value.ListValue; import carpet.script.value.NumericValue; import carpet.script.value.StringValue; @@ -27,10 +28,14 @@ import net.minecraft.command.argument.BlockPosArgumentType; import net.minecraft.command.argument.BlockStateArgument; import net.minecraft.command.argument.BlockStateArgumentType; import net.minecraft.command.argument.ColorArgumentType; +import net.minecraft.command.argument.ColumnPosArgumentType; +import net.minecraft.command.argument.DimensionArgumentType; +import net.minecraft.command.argument.EntityArgumentType; import net.minecraft.command.argument.RotationArgumentType; import net.minecraft.command.argument.SwizzleArgumentType; import net.minecraft.command.argument.TimeArgumentType; import net.minecraft.command.argument.Vec3ArgumentType; +import net.minecraft.entity.Entity; import net.minecraft.server.command.ServerCommandSource; import net.minecraft.text.LiteralText; import net.minecraft.util.Formatting; @@ -45,7 +50,6 @@ import java.util.Locale; import java.util.Map; import java.util.Set; import java.util.concurrent.CompletableFuture; -import java.util.function.BiFunction; import java.util.function.Supplier; import java.util.stream.Collectors; @@ -57,21 +61,17 @@ public abstract class CommandArgument private static final List baseTypes = Lists.newArrayList( // default new StringArgument(), - // other customizable string arguments - new WordArgument(), new GreedyStringArgument(), - new BlockPosArgument(), new FloatArgument(), new IntArgument(), - // etc... - // vanilla arguments + // vanilla arguments as per https://minecraft.gamepedia.com/Argument_types new VanillaUnconfigurableArgument( "bool", - BoolArgumentType::bool, - (c, p) -> new NumericValue(BoolArgumentType.getBool(c, p)), - false + BoolArgumentType::bool, (c, p) -> new NumericValue(BoolArgumentType.getBool(c, p)), false ), + new FloatArgument(), + new IntArgument(), + new WordArgument(), new GreedyStringArgument(), new VanillaUnconfigurableArgument( "yaw", // angle - AngleArgumentType::angle, - (c, p) -> new NumericValue(AngleArgumentType.getAngle(c, p)), - true + AngleArgumentType::angle, (c, p) -> new NumericValue(AngleArgumentType.getAngle(c, p)), true ), + new BlockPosArgument(), new VanillaUnconfigurableArgument( "block", BlockStateArgumentType::blockState, (c, p) -> { @@ -89,9 +89,14 @@ public abstract class CommandArgument }, false ), - // columnpos + new VanillaUnconfigurableArgument("columnpos", + ColumnPosArgumentType::columnPos, (c, p) -> ValueConversions.of(ColumnPosArgumentType.getColumnPos(c, p)), false + ), // component // raw json - // dimension + new VanillaUnconfigurableArgument("dimension", + DimensionArgumentType::dimension, (c, p) -> ValueConversions.of(DimensionArgumentType.getDimensionArgument(c, p)), false + ), + new EntityArgument(), // entity // anchor // entity_anchor // entitytype // entity_summon @@ -123,15 +128,11 @@ public abstract class CommandArgument // scoreholder // scoreboardslot new VanillaUnconfigurableArgument("swizzle", - SwizzleArgumentType::swizzle, - (c, p) -> StringValue.of(SwizzleArgumentType.getSwizzle(c, p).stream().map(Direction.Axis::asString).collect(Collectors.joining())), - true + SwizzleArgumentType::swizzle, (c, p) -> StringValue.of(SwizzleArgumentType.getSwizzle(c, p).stream().map(Direction.Axis::asString).collect(Collectors.joining())), true ), // team new VanillaUnconfigurableArgument("time", - TimeArgumentType::time, - (c, p) -> new NumericValue(IntegerArgumentType.getInteger(c, p)), - false + TimeArgumentType::time, (c, p) -> new NumericValue(IntegerArgumentType.getInteger(c, p)), false ), // uuid // columnlocation // vec2 @@ -342,7 +343,7 @@ public abstract class CommandArgument BlockPos pos = mustBeLoaded ? BlockPosArgumentType.getLoadedBlockPos(context, param) : BlockPosArgumentType.getBlockPos(context, param); - return ValueConversions.fromPos(pos); + return ValueConversions.of(pos); } @Override @@ -377,7 +378,7 @@ public abstract class CommandArgument @Override protected Value getValueFromContext(CommandContext context, String param) throws CommandSyntaxException { - return ValueConversions.fromVec(Vec3ArgumentType.getVec3(context, param)); + return ValueConversions.of(Vec3ArgumentType.getVec3(context, param)); } @Override @@ -394,6 +395,59 @@ public abstract class CommandArgument } } + private static class EntityArgument extends CommandArgument + { + boolean onlyFans; + boolean single; + + private EntityArgument() + { + super("entity", EntityArgumentType.entities().getExamples(), false); + onlyFans = false; + single = false; + } + @Override + protected ArgumentType getArgumentType() + { + if (onlyFans) + { + return single?EntityArgumentType.player():EntityArgumentType.players(); + } + else + { + return single?EntityArgumentType.entity():EntityArgumentType.entities(); + } + } + + @Override + protected Value getValueFromContext(CommandContext context, String param) throws CommandSyntaxException + { + List founds = (List)EntityArgumentType.getOptionalEntities(context, param); + if (single) + { + return founds.isEmpty()?Value.NULL:new EntityValue(founds.get(0)); + } + else + { + return ListValue.wrap(founds.stream().map(EntityValue::new).collect(Collectors.toList())); + } + } + + @Override + protected void configure(Map config) + { + super.configure(config); + onlyFans = config.getOrDefault("players", Value.FALSE).getBoolean(); + single = config.getOrDefault("single", Value.FALSE).getBoolean(); + } + + @Override + protected Supplier builder() + { + return EntityArgument::new; + } + } + private static class FloatArgument extends CommandArgument { private Double min = null; @@ -496,15 +550,21 @@ public abstract class CommandArgument } } + @FunctionalInterface + private interface ValueExtractor + { + Value apply(CommandContext ctx, String param) throws CommandSyntaxException; + } + public static class VanillaUnconfigurableArgument extends CommandArgument { private Supplier> argumentTypeSupplier; - private BiFunction, String, Value> valueExtractor; + private ValueExtractor valueExtractor; private boolean providesExamples; public VanillaUnconfigurableArgument( String suffix, Supplier> argumentTypeSupplier, - BiFunction, String, Value> valueExtractor, + ValueExtractor valueExtractor, boolean providesExamples ) { diff --git a/src/main/java/carpet/script/value/EntityValue.java b/src/main/java/carpet/script/value/EntityValue.java index b09cb5b5..49978474 100644 --- a/src/main/java/carpet/script/value/EntityValue.java +++ b/src/main/java/carpet/script/value/EntityValue.java @@ -14,7 +14,6 @@ import carpet.script.CarpetContext; import carpet.script.EntityEventsGroup; import carpet.script.argument.Vector3Argument; import carpet.script.exception.InternalExpressionException; -import com.google.common.collect.ImmutableMap; import com.google.common.collect.Sets; import com.mojang.brigadier.StringReader; import com.mojang.brigadier.exceptions.CommandSyntaxException; @@ -66,13 +65,11 @@ import net.minecraft.util.registry.Registry; import net.minecraft.util.registry.RegistryKey; import net.minecraft.world.GameMode; import net.minecraft.world.World; -import org.apache.commons.lang3.tuple.Pair; import java.util.ArrayList; import java.util.Collection; import java.util.EnumSet; import java.util.HashMap; -import java.util.HashSet; import java.util.List; import java.util.Locale; import java.util.Map; @@ -444,8 +441,8 @@ public class EntityValue extends Value ServerPlayerEntity spe = (ServerPlayerEntity)e; if (spe.getSpawnPointPosition() == null) return Value.FALSE; return ListValue.of( - ValueConversions.fromPos(spe.getSpawnPointPosition()), - ValueConversions.dimName(spe.getSpawnPointDimension()), + ValueConversions.of(spe.getSpawnPointPosition()), + ValueConversions.of(spe.getSpawnPointDimension()), new NumericValue(spe.getSpawnAngle()), new NumericValue(spe.isSpawnPointSet()) // true if forced spawn point ); @@ -715,7 +712,7 @@ public class EntityValue extends Value hitres = Tracer.rayTraceBlocks(e, 1, reach, liquids); if (hitres == null) return Value.NULL; - if (exact && hitres.getType() != HitResult.Type.MISS) return ValueConversions.fromVec(hitres.getPos()); + if (exact && hitres.getType() != HitResult.Type.MISS) return ValueConversions.of(hitres.getPos()); switch (hitres.getType()) { case MISS: return Value.NULL; diff --git a/src/main/java/carpet/script/value/ValueConversions.java b/src/main/java/carpet/script/value/ValueConversions.java index 8cfbe3e1..a8b7c6a5 100644 --- a/src/main/java/carpet/script/value/ValueConversions.java +++ b/src/main/java/carpet/script/value/ValueConversions.java @@ -17,6 +17,7 @@ import net.minecraft.util.Identifier; import net.minecraft.util.dynamic.GlobalPos; import net.minecraft.util.math.BlockBox; import net.minecraft.util.math.BlockPos; +import net.minecraft.util.math.ColumnPos; import net.minecraft.util.math.Vec3d; import net.minecraft.util.registry.Registry; import net.minecraft.util.registry.RegistryKey; @@ -33,25 +34,27 @@ import java.util.stream.Collectors; public class ValueConversions { - public static Value fromPos(BlockPos pos) + public static Value of(BlockPos pos) { return ListValue.of(new NumericValue(pos.getX()), new NumericValue(pos.getY()), new NumericValue(pos.getZ())); } - public static Value fromPosOptional(BlockPos pos) + public static Value ofOptional(BlockPos pos) { if (pos == null) return Value.NULL; return ListValue.of(new NumericValue(pos.getX()), new NumericValue(pos.getY()), new NumericValue(pos.getZ())); } - public static Value fromVec(Vec3d vec) + public static Value of(Vec3d vec) { return ListValue.of(new NumericValue(vec.x), new NumericValue(vec.y), new NumericValue(vec.z)); } - public static Value dimName(ServerWorld world) + public static Value of(ColumnPos cpos) { return ListValue.of(new NumericValue(cpos.x), new NumericValue(cpos.z));} + + public static Value of(ServerWorld world) { - return ofId(world.getRegistryKey().getValue()); + return of(world.getRegistryKey().getValue()); } public static World dimFromValue(Value dimensionValue, MinecraftServer server) @@ -105,12 +108,12 @@ public class ValueConversions } } - public static Value dimName(RegistryKey dim) + public static Value of(RegistryKey dim) { - return ofId(dim.getValue()); + return of(dim.getValue()); } - public static Value ofId(Identifier id) + public static Value of(Identifier id) { if (id == null) // should be Value.NULL return Value.NULL; @@ -128,11 +131,11 @@ public class ValueConversions return id.toString(); } - public static Value ofGlobalPos(GlobalPos pos) + public static Value of(GlobalPos pos) { return ListValue.of( - ValueConversions.dimName(pos.getDimension()), - ValueConversions.fromPos(pos.getPos()) + ValueConversions.of(pos.getDimension()), + ValueConversions.of(pos.getPos()) ); } @@ -165,7 +168,7 @@ public class ValueConversions if (v instanceof GlobalPos) { GlobalPos pos = (GlobalPos)v; - return ofGlobalPos(pos); + return of(pos); } if (v instanceof Entity) { @@ -226,7 +229,7 @@ public class ValueConversions } if (el instanceof GlobalPos) { - return ListValue.wrap((List) l.stream().map(o -> ofGlobalPos((GlobalPos) o)).collect(Collectors.toList())); + return ListValue.wrap((List) l.stream().map(o -> of((GlobalPos) o)).collect(Collectors.toList())); } } return Value.NULL; @@ -241,7 +244,7 @@ public class ValueConversions ); } - public static Value fromStructure(StructureStart structure) + public static Value of(StructureStart structure) { if (structure == null || structure == StructureStart.DEFAULT) return Value.NULL; List pieces = new ArrayList<>();