From 6c2ddd68dbbebb7c045d7b9adc51167c428bef5c Mon Sep 17 00:00:00 2001 From: gnembon Date: Tue, 29 Oct 2019 00:46:44 -0300 Subject: [PATCH] fixed named results, and added loaded_status --- .../java/carpet/script/CarpetExpression.java | 16 +++++++++++++--- .../java/carpet/script/value/BlockValue.java | 4 +++- .../java/carpet/script/value/EntityValue.java | 6 ++++-- src/main/java/carpet/script/value/ListValue.java | 9 +++++++-- .../script/value/NBTSerializableValue.java | 8 ++++++++ 5 files changed, 35 insertions(+), 8 deletions(-) diff --git a/src/main/java/carpet/script/CarpetExpression.java b/src/main/java/carpet/script/CarpetExpression.java index ceff1040..4f2075df 100644 --- a/src/main/java/carpet/script/CarpetExpression.java +++ b/src/main/java/carpet/script/CarpetExpression.java @@ -77,6 +77,7 @@ import net.minecraft.world.LightType; import net.minecraft.world.World; import net.minecraft.world.biome.Biome; import net.minecraft.world.chunk.Chunk; +import net.minecraft.world.chunk.WorldChunk; import net.minecraft.world.dimension.DimensionType; import net.minecraft.world.loot.context.LootContext; import net.minecraft.world.loot.context.LootContextParameters; @@ -95,6 +96,7 @@ import java.util.function.BiFunction; import java.util.function.Predicate; import java.util.stream.Collectors; +import static carpet.script.value.NBTSerializableValue.nameFromRegistryId; import static java.lang.Math.abs; import static java.lang.Math.max; import static java.lang.Math.min; @@ -789,8 +791,16 @@ public class CarpetExpression { BlockPos pos = BlockValue.fromParams((CarpetContext)c, lv, 0).block.getPos(); Value retval = ((CarpetContext)c).s.getWorld().getChunkManager().shouldTickChunk(new ChunkPos(pos))?Value.TRUE : Value.FALSE; - //Value retval = ((CarpetContext)c).s.getWorld().isAreaLoaded(pos.getX() - 32, 0, pos.getZ() - 32, - // pos.getX() + 32, 0, pos.getZ() + 32) ? Value.TRUE : Value.FALSE; + return (c_, t_) -> retval; + }); + + this.expr.addLazyFunction("loaded_status", -1, (c, t, lv) -> + { + BlockPos pos = BlockValue.fromParams((CarpetContext)c, lv, 0).block.getPos(); + WorldChunk chunk = ((CarpetContext)c).s.getWorld().getChunkManager().getWorldChunk(pos.getX()>>4, pos.getZ()>>4, false); + if (chunk == null) + return LazyValue.ZERO; + Value retval = new NumericValue(chunk.getLevelType().ordinal()); return (c_, t_) -> retval; }); @@ -2869,7 +2879,7 @@ public class CarpetExpression this.expr.addLazyFunction("current_dimension", 0, (c, t, lv) -> { ServerCommandSource s = ((CarpetContext)c).s; - Value retval = new StringValue(s.getWorld().dimension.getType().toString().replaceFirst("minecraft:","")); + Value retval = new StringValue(nameFromRegistryId(Registry.DIMENSION.getId(s.getWorld().dimension.getType()))); return (cc, tt) -> retval; }); diff --git a/src/main/java/carpet/script/value/BlockValue.java b/src/main/java/carpet/script/value/BlockValue.java index 1f084efd..1c8abf49 100644 --- a/src/main/java/carpet/script/value/BlockValue.java +++ b/src/main/java/carpet/script/value/BlockValue.java @@ -28,6 +28,8 @@ import java.util.List; import java.util.Map; import java.util.stream.Collectors; +import static carpet.script.value.NBTSerializableValue.nameFromRegistryId; + public class BlockValue extends Value { public static final BlockValue AIR = new BlockValue(Blocks.AIR.getDefaultState(), null, BlockPos.ORIGIN); @@ -234,7 +236,7 @@ public class BlockValue extends Value @Override public String getString() { - return Registry.BLOCK.getId(getBlockState().getBlock()).getPath(); + return nameFromRegistryId(Registry.BLOCK.getId(getBlockState().getBlock())); } @Override diff --git a/src/main/java/carpet/script/value/EntityValue.java b/src/main/java/carpet/script/value/EntityValue.java index c8d9b150..21c6b6d9 100644 --- a/src/main/java/carpet/script/value/EntityValue.java +++ b/src/main/java/carpet/script/value/EntityValue.java @@ -56,6 +56,8 @@ import java.util.function.BiFunction; import java.util.function.Predicate; import java.util.stream.Collectors; +import static carpet.script.value.NBTSerializableValue.nameFromRegistryId; + public class EntityValue extends Value { private Entity entity; @@ -197,7 +199,7 @@ public class EntityValue extends Value put("motion_z", (e, a) -> new NumericValue(e.getVelocity().z)); put("name", (e, a) -> new StringValue(e.getDisplayName().getString())); put("custom_name", (e, a) -> e.hasCustomName()?new StringValue(e.getCustomName().getString()):Value.NULL); - put("type", (e, a) -> new StringValue(Registry.ENTITY_TYPE.getId(e.getType()).toString().replaceFirst("minecraft:",""))); + put("type", (e, a) -> new StringValue(nameFromRegistryId(Registry.ENTITY_TYPE.getId(e.getType())))); put("is_riding", (e, a) -> new NumericValue(e.hasVehicle())); put("is_ridden", (e, a) -> new NumericValue(e.hasPassengers())); put("passengers", (e, a) -> ListValue.wrap(e.getPassengerList().stream().map(EntityValue::new).collect(Collectors.toList()))); @@ -217,7 +219,7 @@ public class EntityValue extends Value put("immune_to_fire", (e, a) -> new NumericValue(e.isFireImmune())); put("invulnerable", (e, a) -> new NumericValue(e.isInvulnerable())); - put("dimension", (e, a) -> new StringValue(e.dimension.toString().replaceFirst("minecraft:",""))); + put("dimension", (e, a) -> new StringValue(nameFromRegistryId(Registry.DIMENSION.getId(e.dimension)))); put("height", (e, a) -> new NumericValue(e.getDimensions(EntityPose.STANDING).height)); put("width", (e, a) -> new NumericValue(e.getDimensions(EntityPose.STANDING).width)); put("eye_height", (e, a) -> new NumericValue(e.getEyeHeight(EntityPose.STANDING))); diff --git a/src/main/java/carpet/script/value/ListValue.java b/src/main/java/carpet/script/value/ListValue.java index c364c33e..b16a54b1 100644 --- a/src/main/java/carpet/script/value/ListValue.java +++ b/src/main/java/carpet/script/value/ListValue.java @@ -4,10 +4,15 @@ import carpet.script.exception.InternalExpressionException; import net.minecraft.item.ItemStack; import net.minecraft.util.registry.Registry; -import java.util.*; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.Iterator; +import java.util.List; import java.util.stream.Collectors; import java.util.stream.StreamSupport; +import static carpet.script.value.NBTSerializableValue.nameFromRegistryId; import static java.lang.Math.abs; public class ListValue extends AbstractListValue implements ContainerValueInterface @@ -49,7 +54,7 @@ public class ListValue extends AbstractListValue implements ContainerValueInterf if (itemstack == null || itemstack.isEmpty()) return Value.NULL; return ListValue.of( - new StringValue(Registry.ITEM.getId(itemstack.getItem()).getPath()), + new StringValue(nameFromRegistryId(Registry.ITEM.getId(itemstack.getItem()))), new NumericValue(itemstack.getCount()), NBTSerializableValue.fromStack(itemstack) ); diff --git a/src/main/java/carpet/script/value/NBTSerializableValue.java b/src/main/java/carpet/script/value/NBTSerializableValue.java index 392e106b..fe99bef5 100644 --- a/src/main/java/carpet/script/value/NBTSerializableValue.java +++ b/src/main/java/carpet/script/value/NBTSerializableValue.java @@ -21,6 +21,7 @@ import net.minecraft.nbt.ListTag; import net.minecraft.nbt.StringNbtReader; import net.minecraft.nbt.StringTag; import net.minecraft.nbt.Tag; +import net.minecraft.util.Identifier; import net.minecraft.util.math.BlockPos; import java.util.Collection; @@ -71,6 +72,13 @@ public class NBTSerializableValue extends Value implements ContainerValueInterfa return Value.NULL; } + public static String nameFromRegistryId(Identifier id) + { + if (id.getNamespace().equals("minecraft")) + return id.getPath(); + return id.toString(); + } + public static NBTSerializableValue parseString(String nbtString) { Tag tag;