refactor limit access to built-in registries

This commit is contained in:
gnembon committed 2023-01-18 04:07:55 +01:00
1 parent 8dd08d5cb1
commit bd5dfdbf9f
25 files changed
+243 -176

No files matched your search

@@ -18,7 +18,7 @@ import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.core.Direction.Axis;
import net.minecraft.core.GlobalPos;
import net.minecraft.core.RegistryAccess;
import net.minecraft.core.Registry;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.core.registries.Registries;
import net.minecraft.nbt.CompoundTag;
@@ -29,23 +29,22 @@ import net.minecraft.world.InteractionHand;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.context.BlockPlaceContext;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.properties.Property;
import net.minecraft.world.level.chunk.LevelChunk;
import net.minecraft.world.phys.BlockHitResult;
import net.minecraft.world.phys.Vec3;
import org.jetbrains.annotations.NotNull;
import static carpet.script.value.NBTSerializableValue.nameFromRegistryId;
public class BlockValue extends Value
{
public static final BlockValue AIR = new BlockValue(Blocks.AIR.defaultBlockState(), null, BlockPos.ZERO);
public static final BlockValue NULL = new BlockValue(null, null, null);
private BlockState blockState;
private final BlockPos pos;
private final ServerLevel world;
@NotNull private final Level world;
private CompoundTag data;
public static BlockValue fromCoords(CarpetContext c, int x, int y, int z)
@@ -55,19 +54,19 @@ public class BlockValue extends Value
}
private static final Map<String, BlockValue> bvCache= new HashMap<>();
public static BlockValue fromString(String str, RegistryAccess regs)
public static BlockValue fromString(String str, Level level)
{
try
{
BlockValue bv = bvCache.get(str); // [SCARY SHIT] persistent caches over server reloads
if (bv != null) return bv;
BlockStateParser.BlockResult foo = BlockStateParser.parseForBlock(regs.lookupOrThrow(Registries.BLOCK), new StringReader(str), true );
BlockStateParser.BlockResult foo = BlockStateParser.parseForBlock(level.registryAccess().lookupOrThrow(Registries.BLOCK), new StringReader(str), true );
if (foo.blockState() != null)
{
CompoundTag bd = foo.nbt();
if (bd == null)
bd = new CompoundTag();
bv = new BlockValue(foo.blockState(), null, null, bd );
bv = new BlockValue(foo.blockState(), level, null, bd );
if (bvCache.size()>10000)
bvCache.clear();
bvCache.put(str, bv);
@@ -92,7 +91,7 @@ public class BlockValue extends Value
{
return blockState;
}
if (world != null && pos != null)
if (pos != null)
{
blockState = world.getBlockState(pos);
return blockState;
@@ -100,12 +99,15 @@ public class BlockValue extends Value
throw new InternalExpressionException("Attempted to fetch block state without world or stored block state");
}
public static BlockEntity getBlockEntity(ServerLevel world, BlockPos pos)
public static BlockEntity getBlockEntity(Level level, BlockPos pos)
{
if (world.getServer().isSameThread())
return world.getBlockEntity(pos);
else
return world.getChunkAt(pos).getBlockEntity(pos, LevelChunk.EntityCreationType.IMMEDIATE);
if (level instanceof ServerLevel serverLevel) {
if (serverLevel.getServer().isSameThread())
return serverLevel.getBlockEntity(pos);
else
return serverLevel.getChunkAt(pos).getBlockEntity(pos, LevelChunk.EntityCreationType.IMMEDIATE);
}
return null;
}
@@ -117,7 +119,7 @@ public class BlockValue extends Value
return null;
return data;
}
if (world != null && pos != null)
if (pos != null)
{
BlockEntity be = getBlockEntity(world, pos);
if (be == null)
@@ -132,7 +134,7 @@ public class BlockValue extends Value
}
public BlockValue(BlockState state, ServerLevel world, BlockPos position)
public BlockValue(BlockState state, @NotNull Level world, BlockPos position)
{
this.world = world;
blockState = state;
@@ -140,7 +142,7 @@ public class BlockValue extends Value
data = null;
}
public BlockValue(BlockState state, ServerLevel world, BlockPos position, CompoundTag nbt)
public BlockValue(BlockState state, @NotNull Level world, BlockPos position, CompoundTag nbt)
{
this.world = world;
blockState = state;
@@ -152,13 +154,14 @@ public class BlockValue extends Value
@Override
public String getString()
{
return nameFromRegistryId(BuiltInRegistries.BLOCK.getKey(getBlockState().getBlock()));
Registry<Block> blockRegistry = world.registryAccess().registryOrThrow(Registries.BLOCK);
return nameFromRegistryId(blockRegistry.getKey(getBlockState().getBlock()));
}
@Override
public boolean getBoolean()
{
return this != NULL && !getBlockState().isAir();
return !getBlockState().isAir();
}
@Override
@@ -176,7 +179,7 @@ public class BlockValue extends Value
@Override
public int hashCode()
{
if (world != null && pos != null )
if (pos != null )
return GlobalPos.of(world.dimension() , pos).hashCode(); //getDimension().getType()
return ("b"+getString()).hashCode();
}
@@ -186,7 +189,7 @@ public class BlockValue extends Value
return pos;
}
public ServerLevel getWorld() { return world;}
public Level getWorld() { return world;}
@Override
public Tag toTag(boolean force)
@@ -196,7 +199,7 @@ public class BlockValue extends Value
CompoundTag tag = new CompoundTag();
CompoundTag state = new CompoundTag();
BlockState s = getBlockState();
state.put("Name", StringTag.valueOf(BuiltInRegistries.BLOCK.getKey(s.getBlock()).toString()));
state.put("Name", StringTag.valueOf(world.registryAccess().registryOrThrow(Registries.BLOCK).getKey(s.getBlock()).toString()));
Collection<Property<?>> properties = s.getProperties();
if (!properties.isEmpty())
{
@@ -24,6 +24,8 @@ import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.core.Holder;
import net.minecraft.core.HolderSet;
import net.minecraft.core.Registry;
import net.minecraft.core.RegistryAccess;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.core.registries.Registries;
import net.minecraft.nbt.CompoundTag;
@@ -274,7 +276,7 @@ public class EntityValue extends Value
}
else
{
return new EntityClassDescriptor(ANY, e -> !eTag.contains(e.getType()) && e.isAlive(), BuiltInRegistries.ENTITY_TYPE.stream().filter(et -> !eTag.contains(et)));
return new EntityClassDescriptor(ANY, e -> !eTag.contains(e.getType()) && e.isAlive(), server.registryAccess().registryOrThrow(Registries.ENTITY_TYPE).stream().filter(et -> !eTag.contains(et)));
}
}
return eDesc;
@@ -300,8 +302,9 @@ public class EntityValue extends Value
this(type, predicate, types.toList());
}
public Value listValue() {
return ListValue.wrap(types.stream().map(et -> StringValue.of(nameFromRegistryId(BuiltInRegistries.ENTITY_TYPE.getKey(et)))));
public Value listValue(RegistryAccess regs) {
final Registry<EntityType> entityRegs = regs.registryOrThrow(Registries.ENTITY_TYPE);
return ListValue.wrap(types.stream().map(et -> StringValue.of(nameFromRegistryId(entityRegs.getKey(et)))));
}
public final static Map<String, EntityClassDescriptor> byName = new HashMap<>() {{
@@ -449,7 +452,7 @@ public class EntityValue extends Value
put("display_name", (e, a) -> new FormattedTextValue(e.getDisplayName()));
put("command_name", (e, a) -> new StringValue(e.getScoreboardName()));
put("custom_name", (e, a) -> e.hasCustomName()?new StringValue(e.getCustomName().getString()):Value.NULL);
put("type", (e, a) -> new StringValue(nameFromRegistryId(BuiltInRegistries.ENTITY_TYPE.getKey(e.getType()))));
put("type", (e, a) -> new StringValue(nameFromRegistryId(e.getLevel().registryAccess().registryOrThrow(Registries.ENTITY_TYPE).getKey(e.getType()))));
put("is_riding", (e, a) -> BooleanValue.of(e.isPassenger()));
put("is_ridden", (e, a) -> BooleanValue.of(e.isVehicle()));
put("passengers", (e, a) -> ListValue.wrap(e.getPassengers().stream().map(EntityValue::new).collect(Collectors.toList())));
@@ -529,9 +532,9 @@ public class EntityValue extends Value
});
put("item", (e, a) -> {
if(e instanceof ItemEntity)
return ValueConversions.of(((ItemEntity) e).getItem());
return ValueConversions.of(((ItemEntity) e).getItem(), e.getServer().registryAccess());
if(e instanceof ItemFrame)
return ValueConversions.of(((ItemFrame) e).getItem());
return ValueConversions.of(((ItemFrame) e).getItem(), e.getServer().registryAccess());
return Value.NULL;
});
put("count", (e, a) -> (e instanceof ItemEntity)?new NumericValue(((ItemEntity) e).getItem().getCount()):Value.NULL);
@@ -657,7 +660,7 @@ public class EntityValue extends Value
put("brain", (e, a) -> {
String module = a.getString();
MemoryModuleType<?> moduleType = BuiltInRegistries.MEMORY_MODULE_TYPE.get(InputValidator.identifierOf(module));
MemoryModuleType<?> moduleType = e.getLevel().registryAccess().registryOrThrow(Registries.MEMORY_MODULE_TYPE).get(InputValidator.identifierOf(module));
if (moduleType == MemoryModuleType.DUMMY) return Value.NULL;
if (e instanceof LivingEntity livingEntity)
{
@@ -820,7 +823,7 @@ public class EntityValue extends Value
if (where == null)
throw new InternalExpressionException("Unknown inventory slot: "+a.getString());
if (e instanceof LivingEntity)
return ValueConversions.of(((LivingEntity)e).getItemBySlot(where));
return ValueConversions.of(((LivingEntity)e).getItemBySlot(where), e.getServer().registryAccess());
return Value.NULL;
});
@@ -931,13 +934,14 @@ public class EntityValue extends Value
put("attribute", (e, a) ->{
if (!(e instanceof LivingEntity)) return Value.NULL;
LivingEntity el = (LivingEntity)e;
final Registry<Attribute> attributes = e.getLevel().registryAccess().registryOrThrow(Registries.ATTRIBUTE);
if (a == null)
{
AttributeMap container = el.getAttributes();
return MapValue.wrap(BuiltInRegistries.ATTRIBUTE.stream().filter(container::hasAttribute).collect(Collectors.toMap(aa -> ValueConversions.of(BuiltInRegistries.ATTRIBUTE.getKey(aa)), aa -> NumericValue.of(container.getValue(aa)))));
return MapValue.wrap(attributes.stream().filter(container::hasAttribute).collect(Collectors.toMap(aa -> ValueConversions.of(attributes.getKey(aa)), aa -> NumericValue.of(container.getValue(aa)))));
}
ResourceLocation id = InputValidator.identifierOf(a.getString());
Attribute attrib = BuiltInRegistries.ATTRIBUTE.getOptional(id).orElseThrow(
Attribute attrib = attributes.getOptional(id).orElseThrow(
() -> new InternalExpressionException("Unknown attribute: "+a.getString())
);
if (!el.getAttributes().hasAttribute(attrib)) return Value.NULL;
@@ -1717,7 +1721,8 @@ public class EntityValue extends Value
if (!force) throw new NBTSerializableValue.IncompatibleTypeException(this);
CompoundTag tag = new CompoundTag();
tag.put("Data", getEntity().saveWithoutId( new CompoundTag()));
tag.put("Name", StringTag.valueOf(BuiltInRegistries.ENTITY_TYPE.getKey(getEntity().getType()).toString()));
final Registry<EntityType<?>> reg = getEntity().level.registryAccess().registryOrThrow(Registries.ENTITY_TYPE);
tag.put("Name", StringTag.valueOf(reg.getKey(getEntity().getType()).toString()));
return tag;
}
}
@@ -228,7 +228,7 @@ public class ScreenValue extends Value {
public void slotChanged(AbstractContainerMenu handler, int slotId, ItemStack stack) {
Map<Value,Value> data = new HashMap<>();
data.put(StringValue.of("slot"),NumericValue.of(slotId));
data.put(StringValue.of("stack"),ValueConversions.of(stack));
data.put(StringValue.of("stack"),ValueConversions.of(stack, player.level.registryAccess()));
ScreenValue.this.callListener(ScreenValue.this.player,"slot_update",data);
}
@Override
@@ -11,6 +11,7 @@ import net.minecraft.advancements.critereon.MinMaxBounds;
import net.minecraft.commands.arguments.item.ItemInput;
import net.minecraft.core.BlockPos;
import net.minecraft.core.GlobalPos;
import net.minecraft.core.Registry;
import net.minecraft.core.RegistryAccess;
import net.minecraft.core.Vec3i;
import net.minecraft.core.particles.ParticleOptions;
@@ -31,6 +32,7 @@ import net.minecraft.world.entity.ai.memory.NearestVisibleLivingEntities;
import net.minecraft.world.entity.ai.memory.WalkTarget;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.pattern.BlockInWorld;
import net.minecraft.world.level.block.state.properties.Property;
@@ -80,6 +82,7 @@ public class ValueConversions
public static <T extends Number> Value of(MinMaxBounds<T> range) { return ListValue.of(NumericValue.of(range.getMin()), NumericValue.of(range.getMax()));}
@Deprecated
public static Value of(ItemStack stack)
{
if (stack == null || stack.isEmpty())
@@ -90,6 +93,15 @@ public class ValueConversions
NBTSerializableValue.fromStack(stack)
);
}
public static Value of(ItemStack stack, RegistryAccess regs) {
if (stack == null || stack.isEmpty())
return Value.NULL;
return ListValue.of(
of(regs.registryOrThrow(Registries.ITEM).getKey(stack.getItem())),
new NumericValue(stack.getCount()),
NBTSerializableValue.fromStack(stack)
);
}
public static Value of(Objective objective)
{
@@ -318,7 +330,7 @@ public class ValueConversions
);
}
public static Value of(StructureStart structure)
public static Value of(StructureStart structure, final RegistryAccess regs)
{
if (structure == null || structure == StructureStart.INVALID_START) return Value.NULL;
BoundingBox boundingBox = structure.getBoundingBox();
@@ -332,7 +344,7 @@ public class ValueConversions
if (box.maxX() >= box.minX() && box.maxY() >= box.minY() && box.maxZ() >= box.minZ())
{
pieces.add(ListValue.of(
new StringValue(NBTSerializableValue.nameFromRegistryId(BuiltInRegistries.STRUCTURE_PIECE.getKey(piece.getType()))),
new StringValue(NBTSerializableValue.nameFromRegistryId(regs.registryOrThrow(Registries.STRUCTURE_PIECE).getKey(piece.getType()))),
(piece.getOrientation() == null) ? Value.NULL : new StringValue(piece.getOrientation().getName()),
ListValue.fromTriple(box.minX(), box.minY(), box.minZ()),
ListValue.fromTriple(box.maxX(), box.maxY(), box.maxZ())
@@ -406,9 +418,10 @@ public class ValueConversions
public static Value ofBlockPredicate(RegistryAccess registryAccess, Predicate<BlockInWorld> blockPredicate)
{
BlockPredicateInterface predicateData = (BlockPredicateInterface) blockPredicate;
final Registry<Block> blocks = registryAccess.registryOrThrow(Registries.BLOCK);
return ListValue.of(
predicateData.getCMBlockState()==null?Value.NULL:of(BuiltInRegistries.BLOCK.getKey(predicateData.getCMBlockState().getBlock())),
predicateData.getCMBlockTagKey()==null?Value.NULL:of(registryAccess.registryOrThrow(Registries.BLOCK).getTag(predicateData.getCMBlockTagKey()).get().key()),
predicateData.getCMBlockState()==null?Value.NULL:of(blocks.getKey(predicateData.getCMBlockState().getBlock())),
predicateData.getCMBlockTagKey()==null?Value.NULL:of(blocks.getTag(predicateData.getCMBlockTagKey()).get().key()),
MapValue.wrap(predicateData.getCMProperties()),
predicateData.getCMDataTag() == null?Value.NULL:new NBTSerializableValue(predicateData.getCMDataTag())
);
@@ -481,7 +494,7 @@ public class ValueConversions
if (o instanceof BoundingBox)
return of((BoundingBox) o);
if (o instanceof ItemStack)
return of((ItemStack)o);
return of((ItemStack)o, serverWorld.registryAccess());
if (o instanceof Boolean)
return BooleanValue.of((Boolean) o);
if (o instanceof Number)