This commit is contained in:
gnembon committed 2025-04-30 14:11:25 +02:00
1 parent d45f5ba05a
commit ef6ebac7fb
11 files changed
+88 -43

No files matched your search

@@ -53,6 +53,7 @@ import net.minecraft.server.level.TicketType;
import net.minecraft.sounds.SoundEvents;
import net.minecraft.tags.ItemTags;
import net.minecraft.tags.TagKey;
import net.minecraft.util.ProblemReporter;
import net.minecraft.world.level.ServerExplosion;
import net.minecraft.world.level.chunk.ChunkGenerator;
import net.minecraft.world.level.chunk.PalettedContainer;
@@ -64,6 +65,7 @@ import net.minecraft.world.level.levelgen.NoiseRouter;
import net.minecraft.world.level.levelgen.RandomState;
import net.minecraft.world.level.levelgen.structure.Structure;
import net.minecraft.world.level.levelgen.structure.StructureType;
import net.minecraft.world.level.storage.TagValueInput;
import org.apache.commons.lang3.mutable.MutableBoolean;
import org.apache.commons.lang3.tuple.Pair;
@@ -829,7 +831,9 @@ public class WorldAccess
destTag.putInt("x", targetPos.getX());
destTag.putInt("y", targetPos.getY());
destTag.putInt("z", targetPos.getZ());
be.loadWithComponents(destTag, world.registryAccess());
try (final ProblemReporter.ScopedCollector reporter = new ProblemReporter.ScopedCollector(be.problemPath(), CarpetScriptServer.LOG)) {
be.loadWithComponents(TagValueInput.create(reporter, world.registryAccess(), destTag));
}
be.setChanged();
success = true;
}
@@ -1718,13 +1718,13 @@ public class ShapeDispatcher
{
value = new FormattedTextValue(Component.literal(value.getString()));
}
return StringTag.valueOf(((FormattedTextValue) value).serialize(regs));
return ((FormattedTextValue) value).serialize(regs);
}
@Override
public Value decode(Tag tag, Level level)
{
return FormattedTextValue.deserialize(tag.asString().get(), level.registryAccess());
return FormattedTextValue.deserialize(tag, level.registryAccess());
}
}
@@ -38,6 +38,7 @@ import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.ListTag;
import net.minecraft.resources.ResourceKey;
import net.minecraft.util.Mth;
import net.minecraft.util.ProblemReporter;
import net.minecraft.world.item.ItemDisplayContext;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.LightLayer;
@@ -46,6 +47,7 @@ import net.minecraft.world.level.block.LeavesBlock;
import net.minecraft.world.level.block.RenderShape;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.storage.TagValueInput;
import net.minecraft.world.phys.Vec3;
import org.joml.Matrix4f;
import org.joml.Matrix4fStack;
@@ -423,7 +425,9 @@ public class ShapesRenderer
BlockEntity.setLevel(client.level);
if (shape.blockEntity != null)
{
BlockEntity.loadWithComponents(shape.blockEntity, client.level.registryAccess());
try (final ProblemReporter.ScopedCollector reporter = new ProblemReporter.ScopedCollector(BlockEntity.problemPath(), CarpetScriptServer.LOG)) {
BlockEntity.loadWithComponents(TagValueInput.create(reporter, client.level.registryAccess(), shape.blockEntity));
}
}
}
}
@@ -40,6 +40,7 @@ import net.minecraft.server.level.ServerPlayer;
import net.minecraft.tags.EntityTypeTags;
import net.minecraft.tags.TagKey;
import net.minecraft.util.Mth;
import net.minecraft.util.ProblemReporter;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.effect.MobEffect;
import net.minecraft.world.effect.MobEffectInstance;
@@ -73,6 +74,8 @@ import net.minecraft.world.level.GameType;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.entity.EntityTypeTest;
import net.minecraft.world.level.pathfinder.Path;
import net.minecraft.world.level.storage.TagValueInput;
import net.minecraft.world.level.storage.TagValueOutput;
import net.minecraft.world.phys.BlockHitResult;
import net.minecraft.world.phys.EntityHitResult;
import net.minecraft.world.phys.HitResult;
@@ -526,7 +529,7 @@ public class EntityValue extends Value
}
return Value.NULL;
});
put("home", (e, a) -> e instanceof Mob mob ? (mob.getRestrictRadius() > 0) ? new BlockValue(null, (ServerLevel) e.level(), mob.getRestrictCenter()) : Value.FALSE : Value.NULL);
put("home", (e, a) -> e instanceof Mob mob ? (mob.getHomeRadius() > 0) ? new BlockValue(null, (ServerLevel) e.level(), mob.getHomePosition()) : Value.FALSE : Value.NULL);
put("spawn_point", (e, a) -> {
if (e instanceof ServerPlayer spe)
{
@@ -856,12 +859,16 @@ public class EntityValue extends Value
});
put("nbt", (e, a) -> {
CompoundTag nbttagcompound = e.saveWithoutId((new CompoundTag()));
if (a == null)
{
return new NBTSerializableValue(nbttagcompound);
try (final ProblemReporter.ScopedCollector reporter = new ProblemReporter.ScopedCollector(e.problemPath(), CarpetScriptServer.LOG)) {
final TagValueOutput output = TagValueOutput.createWithContext(reporter, e.registryAccess());
e.saveWithoutId(output);
CompoundTag nbttagcompound = output.buildResult();
if (a == null)
{
return new NBTSerializableValue(nbttagcompound);
}
return new NBTSerializableValue(nbttagcompound).get(a);
}
return new NBTSerializableValue(nbttagcompound).get(a);
});
put("category", (e, a) -> {
@@ -1312,7 +1319,7 @@ public class EntityValue extends Value
}
if (v.isNull())
{
ec.restrictTo(BlockPos.ZERO, -1);
ec.setHomeTo(BlockPos.ZERO, -1);
Map<String, Goal> tasks = Vanilla.Mob_getTemporaryTasks(ec);
Vanilla.Mob_getAI(ec, false).removeGoal(tasks.get("home"));
tasks.remove("home");
@@ -1345,7 +1352,7 @@ public class EntityValue extends Value
throw new InternalExpressionException("'home' requires at least one position argument, and optional distance");
}
ec.restrictTo(pos, distance);
ec.setHomeTo(pos, distance);
Map<String, Goal> tasks = Vanilla.Mob_getTemporaryTasks(ec);
if (!tasks.containsKey("home"))
{
@@ -1675,7 +1682,9 @@ public class EntityValue extends Value
Value tagValue = NBTSerializableValue.fromValue(v);
if (tagValue instanceof NBTSerializableValue nbtsv)
{
e.load(nbtsv.getCompoundTag());
try (final ProblemReporter.ScopedCollector reporter = new ProblemReporter.ScopedCollector(e.problemPath(), CarpetScriptServer.LOG)) {
e.load(TagValueInput.create(reporter, e.registryAccess(), nbtsv.getCompoundTag()));
}
e.setUUID(uUID);
}
}
@@ -1687,9 +1696,16 @@ public class EntityValue extends Value
Value tagValue = NBTSerializableValue.fromValue(v);
if (tagValue instanceof NBTSerializableValue nbtsv)
{
CompoundTag nbttagcompound = e.saveWithoutId((new CompoundTag()));
CompoundTag nbttagcompound;
try (final ProblemReporter.ScopedCollector reporter = new ProblemReporter.ScopedCollector(e.problemPath(), CarpetScriptServer.LOG)) {
final TagValueOutput output = TagValueOutput.createWithContext(reporter, e.registryAccess());
e.saveWithoutId(output);
nbttagcompound = output.buildResult();
}
nbttagcompound.merge(nbtsv.getCompoundTag());
e.load(nbttagcompound);
try (final ProblemReporter.ScopedCollector reporter = new ProblemReporter.ScopedCollector(e.problemPath(), CarpetScriptServer.LOG)) {
e.load(TagValueInput.create(reporter, e.registryAccess(), nbttagcompound));
}
e.setUUID(uUID);
}
}
@@ -1742,10 +1758,18 @@ public class EntityValue extends Value
{
throw new NBTSerializableValue.IncompatibleTypeException(this);
}
CompoundTag tag = new CompoundTag();
tag.put("Data", getEntity().saveWithoutId(new CompoundTag()));
Registry<EntityType<?>> reg = getEntity().level().registryAccess().lookupOrThrow(Registries.ENTITY_TYPE);
tag.put("Name", StringTag.valueOf(reg.getKey(getEntity().getType()).toString()));
CompoundTag tag;
Entity entity = getEntity();
try (final ProblemReporter.ScopedCollector reporter = new ProblemReporter.ScopedCollector(entity.problemPath(), CarpetScriptServer.LOG)) {
final TagValueOutput output = TagValueOutput.createWithContext(reporter, entity.registryAccess());
entity.save(output);
tag = output.buildResult();
}
tag.put("Data", tag);
Registry<EntityType<?>> reg = entity.level().registryAccess().lookupOrThrow(Registries.ENTITY_TYPE);
tag.put("Name", StringTag.valueOf(reg.getKey(entity.getType()).toString()));
return tag;
}
}
@@ -1,9 +1,13 @@
package carpet.script.value;
import carpet.script.exception.InternalExpressionException;
import com.mojang.serialization.DynamicOps;
import net.minecraft.core.RegistryAccess;
import net.minecraft.nbt.NbtOps;
import net.minecraft.nbt.StringTag;
import net.minecraft.nbt.Tag;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.ComponentSerialization;
import net.minecraft.network.chat.MutableComponent;
public class FormattedTextValue extends StringValue
@@ -86,7 +90,7 @@ public class FormattedTextValue extends StringValue
{
throw new NBTSerializableValue.IncompatibleTypeException(this);
}
return StringTag.valueOf(Component.Serializer.toJson(text, regs));
return serialize(regs);
}
@Override
@@ -95,14 +99,14 @@ public class FormattedTextValue extends StringValue
return combine(this, o);
}
public String serialize(RegistryAccess regs)
public Tag serialize(RegistryAccess regs)
{
return Component.Serializer.toJson(text, regs);
return ComponentSerialization.CODEC.encodeStart(regs.createSerializationContext(NbtOps.INSTANCE), text).getOrThrow(InternalExpressionException::new);// text.getContents() Component.Serializer.toJson(text, regs);
}
public static FormattedTextValue deserialize(String serialized, RegistryAccess regs)
public static FormattedTextValue deserialize(Tag tag, RegistryAccess regs)
{
return new FormattedTextValue(Component.Serializer.fromJson(serialized, regs));
return new FormattedTextValue(ComponentSerialization.CODEC.decode(regs.createSerializationContext(NbtOps.INSTANCE), tag).getOrThrow(InternalExpressionException::new).getFirst());
}
public static Component getTextByValue(Value value)
@@ -20,6 +20,7 @@ import net.minecraft.commands.CommandSourceStack;
import net.minecraft.core.RegistryAccess;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.ListTag;
import net.minecraft.nbt.NbtOps;
import net.minecraft.nbt.Tag;
import net.minecraft.network.chat.Component;
import net.minecraft.server.level.ServerPlayer;
@@ -391,7 +392,7 @@ public class ScreenValue extends Value
}
else
{
nbtList.add(itemStack.save(regs));
nbtList.add(ItemStack.CODEC.encodeStart(regs.createSerializationContext(NbtOps.INSTANCE), itemStack).getOrThrow(InternalExpressionException::new));
}
}
return nbtList;