refactor limit access to built-in registries
This commit is contained in:
1 parent
8dd08d5cb1
commit
bd5dfdbf9f
25 files changed
+243
-176
No files matched your search
@@ -139,12 +139,12 @@ public class Auxiliary {
|
||||
CarpetContext cc = (CarpetContext)c;
|
||||
if (lv.size() == 0)
|
||||
{
|
||||
return ListValue.wrap(BuiltInRegistries.SOUND_EVENT.keySet().stream().map(ValueConversions::of));
|
||||
return ListValue.wrap(cc.registry(Registries.SOUND_EVENT).keySet().stream().map(ValueConversions::of));
|
||||
}
|
||||
String rawString = lv.get(0).getString();
|
||||
ResourceLocation soundName = InputValidator.identifierOf(rawString);
|
||||
Vector3Argument locator = Vector3Argument.findIn(lv, 1);
|
||||
if (BuiltInRegistries.SOUND_EVENT.get(soundName) == null)
|
||||
if (cc.registry(Registries.SOUND_EVENT).get(soundName) == null)
|
||||
throw new ThrowStatement(rawString, Throwables.UNKNOWN_SOUND);
|
||||
final Holder<SoundEvent> soundHolder = Holder.direct(SoundEvent.createVariableRangeEvent(soundName));
|
||||
float volume = 1.0F;
|
||||
@@ -180,7 +180,7 @@ public class Auxiliary {
|
||||
expression.addContextFunction("particle", -1, (c, t, lv) ->
|
||||
{
|
||||
CarpetContext cc = (CarpetContext)c;
|
||||
if (lv.size() == 0) return ListValue.wrap(BuiltInRegistries.PARTICLE_TYPE.keySet().stream().map(ValueConversions::of));
|
||||
if (lv.size() == 0) return ListValue.wrap(cc.registry(Registries.PARTICLE_TYPE).keySet().stream().map(ValueConversions::of));
|
||||
MinecraftServer ms = cc.server();
|
||||
ServerLevel world = cc.level();
|
||||
Vector3Argument locator = Vector3Argument.findIn(lv, 1);
|
||||
@@ -756,21 +756,20 @@ public class Auxiliary {
|
||||
{
|
||||
Map<Value, Value> plopData = new HashMap<>();
|
||||
CarpetContext cc = (CarpetContext)c;
|
||||
RegistryAccess registryManager = cc.registryAccess();
|
||||
plopData.put(StringValue.of("scarpet_custom"),
|
||||
ListValue.wrap(FeatureGenerator.featureMap.keySet().stream().sorted().map(StringValue::of).collect(Collectors.toList()))
|
||||
);
|
||||
plopData.put(StringValue.of("features"),
|
||||
ListValue.wrap(BuiltInRegistries.FEATURE.keySet().stream().sorted().map(ValueConversions::of).collect(Collectors.toList()))
|
||||
ListValue.wrap(cc.registry(Registries.FEATURE).keySet().stream().sorted().map(ValueConversions::of).collect(Collectors.toList()))
|
||||
);
|
||||
plopData.put(StringValue.of("configured_features"),
|
||||
ListValue.wrap(registryManager.registryOrThrow(Registries.CONFIGURED_FEATURE).keySet().stream().sorted().map(ValueConversions::of).collect(Collectors.toList()))
|
||||
ListValue.wrap(cc.registry(Registries.CONFIGURED_FEATURE).keySet().stream().sorted().map(ValueConversions::of).collect(Collectors.toList()))
|
||||
);
|
||||
plopData.put(StringValue.of("structure_types"),
|
||||
ListValue.wrap(BuiltInRegistries.STRUCTURE_TYPE.keySet().stream().sorted().map(ValueConversions::of).collect(Collectors.toList()))
|
||||
ListValue.wrap(cc.registry(Registries.STRUCTURE_TYPE).keySet().stream().sorted().map(ValueConversions::of).collect(Collectors.toList()))
|
||||
);
|
||||
plopData.put(StringValue.of("structures"),
|
||||
ListValue.wrap(registryManager.registryOrThrow(Registries.STRUCTURE).keySet().stream().sorted().map(ValueConversions::of).collect(Collectors.toList()))
|
||||
ListValue.wrap(cc.registry(Registries.STRUCTURE).keySet().stream().sorted().map(ValueConversions::of).collect(Collectors.toList()))
|
||||
);
|
||||
return MapValue.wrap(plopData);
|
||||
}
|
||||
@@ -966,7 +965,7 @@ public class Auxiliary {
|
||||
ResourceLocation statName;
|
||||
category = InputValidator.identifierOf(lv.get(1).getString());
|
||||
statName = InputValidator.identifierOf(lv.get(2).getString());
|
||||
StatType<?> type = BuiltInRegistries.STAT_TYPE.get(category);
|
||||
StatType<?> type = cc.registry(Registries.STAT_TYPE).get(category);
|
||||
if (type == null) return Value.NULL;
|
||||
Stat<?> stat = getStat(type, statName);
|
||||
if (stat == null) return Value.NULL;
|
||||
|
||||
@@ -26,6 +26,7 @@ import java.util.function.Predicate;
|
||||
import java.util.stream.Collectors;
|
||||
import net.minecraft.commands.CommandSourceStack;
|
||||
import net.minecraft.core.registries.BuiltInRegistries;
|
||||
import net.minecraft.core.registries.Registries;
|
||||
import net.minecraft.nbt.CompoundTag;
|
||||
import net.minecraft.resources.ResourceLocation;
|
||||
import net.minecraft.server.level.ServerLevel;
|
||||
@@ -101,7 +102,7 @@ public class Entities {
|
||||
try
|
||||
{
|
||||
entityId = ResourceLocation.read(new StringReader(entityString));
|
||||
EntityType<? extends Entity> type = BuiltInRegistries.ENTITY_TYPE.getOptional(entityId).orElse(null);
|
||||
EntityType<? extends Entity> type = cc.registry(Registries.ENTITY_TYPE).getOptional(entityId).orElse(null);
|
||||
if (type == null || !type.canSummon())
|
||||
return Value.NULL;
|
||||
}
|
||||
@@ -244,7 +245,7 @@ public class Entities {
|
||||
{
|
||||
if (lv.size() > 1) throw new InternalExpressionException("'entity_types' requires one or no arguments");
|
||||
String desc = (lv.size() == 1)?lv.get(0).getString():"*";
|
||||
return EntityValue.getEntityDescriptor(desc, ((CarpetContext) c).server()).listValue();
|
||||
return EntityValue.getEntityDescriptor(desc, ((CarpetContext) c).server()).listValue(((CarpetContext)c).registryAccess());
|
||||
});
|
||||
|
||||
expression.addContextFunction("entity_load_handler", -1, (c, t, lv) ->
|
||||
|
||||
@@ -30,6 +30,7 @@ import java.util.stream.Collectors;
|
||||
import net.minecraft.commands.arguments.item.ItemInput;
|
||||
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;
|
||||
@@ -66,10 +67,10 @@ public class Inventories {
|
||||
|
||||
expression.addContextFunction("item_list", -1, (c, t, lv) ->
|
||||
{
|
||||
if (lv.size() == 0)
|
||||
return ListValue.wrap(BuiltInRegistries.ITEM.keySet().stream().map(ValueConversions::of).collect(Collectors.toList()));
|
||||
CarpetContext cc = (CarpetContext)c;
|
||||
Registry<Item> items = cc.registry(Registries.ITEM);
|
||||
if (lv.size() == 0)
|
||||
return ListValue.wrap(items.keySet().stream().map(ValueConversions::of).collect(Collectors.toList()));
|
||||
String tag = lv.get(0).getString();
|
||||
Optional<HolderSet.Named<Item>> itemTag = items.getTag(TagKey.create(Registries.ITEM, InputValidator.identifierOf(tag)));
|
||||
if (itemTag.isEmpty()) return Value.NULL;
|
||||
@@ -125,13 +126,14 @@ public class Inventories {
|
||||
if (lv.size() > 1)
|
||||
{
|
||||
String recipeType = lv.get(1).getString();
|
||||
type = BuiltInRegistries.RECIPE_TYPE.get(InputValidator.identifierOf(recipeType));
|
||||
type = cc.registry(Registries.RECIPE_TYPE).get(InputValidator.identifierOf(recipeType));
|
||||
}
|
||||
List<Recipe<?>> recipes;
|
||||
recipes = ((RecipeManagerInterface) cc.server().getRecipeManager()).getAllMatching(type, InputValidator.identifierOf(recipeName));
|
||||
recipes = ((RecipeManagerInterface) cc.server().getRecipeManager()).getAllMatching(type, InputValidator.identifierOf(recipeName), cc.registryAccess());
|
||||
if (recipes.isEmpty())
|
||||
return Value.NULL;
|
||||
List<Value> recipesOutput = new ArrayList<>();
|
||||
RegistryAccess regs = cc.registryAccess();
|
||||
for (Recipe<?> recipe: recipes)
|
||||
{
|
||||
ItemStack result = recipe.getResultItem();
|
||||
@@ -150,7 +152,7 @@ public class Inventories {
|
||||
else
|
||||
{
|
||||
List<Value> alternatives = new ArrayList<>();
|
||||
stacks.forEach(col -> col.stream().map(ValueConversions::of).forEach(alternatives::add));
|
||||
stacks.forEach(col -> col.stream().map(is -> ValueConversions.of(is, regs)).forEach(alternatives::add));
|
||||
ingredientValue.add(ListValue.wrap(alternatives));
|
||||
}
|
||||
}
|
||||
@@ -189,19 +191,20 @@ public class Inventories {
|
||||
recipeSpec = ListValue.of(new StringValue("custom"));
|
||||
}
|
||||
|
||||
recipesOutput.add(ListValue.of(ValueConversions.of(result), ListValue.wrap(ingredientValue), recipeSpec));
|
||||
recipesOutput.add(ListValue.of(ValueConversions.of(result, regs), ListValue.wrap(ingredientValue), recipeSpec));
|
||||
}
|
||||
return ListValue.wrap(recipesOutput);
|
||||
});
|
||||
|
||||
expression.addUnaryFunction("crafting_remaining_item", v ->
|
||||
expression.addContextFunction("crafting_remaining_item", 1, (c, t, v) ->
|
||||
{
|
||||
String itemStr = v.getString();
|
||||
String itemStr = v.get(0).getString();
|
||||
Item item;
|
||||
ResourceLocation id = InputValidator.identifierOf(itemStr);
|
||||
item = BuiltInRegistries.ITEM.getOptional(id).orElseThrow(() -> new ThrowStatement(itemStr, Throwables.UNKNOWN_ITEM));
|
||||
Registry<Item> registry = ((CarpetContext)c).registry(Registries.ITEM);
|
||||
item = registry.getOptional(id).orElseThrow(() -> new ThrowStatement(itemStr, Throwables.UNKNOWN_ITEM));
|
||||
if (!item.hasCraftingRemainingItem()) return Value.NULL;
|
||||
return new StringValue(NBTSerializableValue.nameFromRegistryId(BuiltInRegistries.ITEM.getKey(item.getCraftingRemainingItem())));
|
||||
return new StringValue(NBTSerializableValue.nameFromRegistryId(registry.getKey(item.getCraftingRemainingItem())));
|
||||
});
|
||||
|
||||
expression.addContextFunction("inventory_size", -1, (c, t, lv) ->
|
||||
@@ -226,17 +229,18 @@ public class Inventories {
|
||||
CarpetContext cc = (CarpetContext) c;
|
||||
NBTSerializableValue.InventoryLocator inventoryLocator = NBTSerializableValue.locateInventory(cc, lv, 0);
|
||||
if (inventoryLocator == null) return Value.NULL;
|
||||
RegistryAccess regs = cc.registryAccess();
|
||||
if (lv.size() == inventoryLocator.offset())
|
||||
{
|
||||
List<Value> fullInventory = new ArrayList<>();
|
||||
for (int i = 0, maxi = inventoryLocator.inventory().getContainerSize(); i < maxi; i++)
|
||||
fullInventory.add(ValueConversions.of(inventoryLocator.inventory().getItem(i)));
|
||||
fullInventory.add(ValueConversions.of(inventoryLocator.inventory().getItem(i), regs));
|
||||
return ListValue.wrap(fullInventory);
|
||||
}
|
||||
int slot = (int)NumericValue.asNumber(lv.get(inventoryLocator.offset())).getLong();
|
||||
slot = NBTSerializableValue.validateSlot(slot, inventoryLocator.inventory());
|
||||
if (slot == inventoryLocator.inventory().getContainerSize()) return Value.NULL;
|
||||
return ValueConversions.of(inventoryLocator.inventory().getItem(slot));
|
||||
return ValueConversions.of(inventoryLocator.inventory().getItem(slot), regs);
|
||||
});
|
||||
|
||||
//inventory_set(<b,e>, <n>, <count>, <item>, <nbt>)
|
||||
@@ -251,13 +255,14 @@ public class Inventories {
|
||||
slot = NBTSerializableValue.validateSlot(slot, inventoryLocator.inventory());
|
||||
if (slot == inventoryLocator.inventory().getContainerSize()) return Value.NULL;
|
||||
int count = (int) NumericValue.asNumber(lv.get(inventoryLocator.offset()+1)).getLong();
|
||||
RegistryAccess regs = cc.registryAccess();
|
||||
if (count == 0)
|
||||
{
|
||||
// clear slot
|
||||
ItemStack removedStack = inventoryLocator.inventory().removeItemNoUpdate(slot);
|
||||
syncPlayerInventory(inventoryLocator, slot);
|
||||
//Value res = ListValue.fromItemStack(removedStack); // that tuple will be read only but cheaper if noone cares
|
||||
return ValueConversions.of(removedStack);
|
||||
return ValueConversions.of(removedStack, regs);
|
||||
}
|
||||
if (lv.size() < inventoryLocator.offset()+3)
|
||||
{
|
||||
@@ -266,7 +271,7 @@ public class Inventories {
|
||||
newStack.setCount(count);
|
||||
inventoryLocator.inventory().setItem(slot, newStack);
|
||||
syncPlayerInventory(inventoryLocator, slot);
|
||||
return ValueConversions.of(previousStack);
|
||||
return ValueConversions.of(previousStack, regs);
|
||||
}
|
||||
CompoundTag nbt = null; // skipping one argument
|
||||
if (lv.size() > inventoryLocator.offset()+3)
|
||||
@@ -290,7 +295,7 @@ public class Inventories {
|
||||
{
|
||||
throw new InternalExpressionException(e.getMessage());
|
||||
}
|
||||
return ValueConversions.of(previousStack);
|
||||
return ValueConversions.of(previousStack, regs);
|
||||
});
|
||||
|
||||
//inventory_find(<b, e>, <item> or null (first empty slot), <start_from=0> ) -> <N> or null
|
||||
|
||||
@@ -279,6 +279,7 @@ public class WorldAccess {
|
||||
BlockArgument locator = BlockArgument.findIn(cc, lv, 0, false);
|
||||
BlockPos pos = locator.block.getPos();
|
||||
PoiManager store = cc.level().getPoiManager();
|
||||
Registry<PoiType> poiReg = cc.registry(Registries.POINT_OF_INTEREST_TYPE);
|
||||
if (lv.size() == locator.offset)
|
||||
{
|
||||
Optional<Holder<PoiType>> foo = store.getType(pos);
|
||||
@@ -297,7 +298,7 @@ public class WorldAccess {
|
||||
if (poi == null)
|
||||
return Value.NULL;
|
||||
return ListValue.of(
|
||||
ValueConversions.of(BuiltInRegistries.POINT_OF_INTEREST_TYPE.getKey(poi.getPoiType().value())),
|
||||
ValueConversions.of(poiReg.getKey(poi.getPoiType().value())),
|
||||
new NumericValue(poiType.maxTickets() - ((PoiRecord_scarpetMixin)poi).getFreeTickets())
|
||||
);
|
||||
}
|
||||
@@ -311,7 +312,7 @@ public class WorldAccess {
|
||||
String poiType = lv.get(locator.offset+1).getString().toLowerCase(Locale.ROOT);
|
||||
if (!"any".equals(poiType))
|
||||
{
|
||||
PoiType type = BuiltInRegistries.POINT_OF_INTEREST_TYPE.getOptional(InputValidator.identifierOf(poiType))
|
||||
PoiType type = poiReg.getOptional(InputValidator.identifierOf(poiType))
|
||||
.orElseThrow(() -> new ThrowStatement(poiType, Throwables.UNKNOWN_POI));
|
||||
condition = (tt) -> tt.value() == type;
|
||||
}
|
||||
@@ -337,7 +338,7 @@ public class WorldAccess {
|
||||
store.getInRange(condition, pos, radius, status);
|
||||
return ListValue.wrap(pois.sorted(Comparator.comparingDouble(p -> p.getPos().distSqr(pos))).map(p ->
|
||||
ListValue.of(
|
||||
ValueConversions.of(BuiltInRegistries.POINT_OF_INTEREST_TYPE.getKey(p.getPoiType().value())),
|
||||
ValueConversions.of(poiReg.getKey(p.getPoiType().value())),
|
||||
new NumericValue(p.getPoiType().value().maxTickets() - ((PoiRecord_scarpetMixin)p).getFreeTickets()),
|
||||
ValueConversions.of(p.getPos())
|
||||
)
|
||||
@@ -362,9 +363,10 @@ public class WorldAccess {
|
||||
}
|
||||
String poiTypeString = poi.getString().toLowerCase(Locale.ROOT);
|
||||
ResourceLocation resource = InputValidator.identifierOf(poiTypeString);
|
||||
PoiType type = BuiltInRegistries.POINT_OF_INTEREST_TYPE.getOptional(resource)
|
||||
Registry<PoiType> poiReg = cc.registry(Registries.POINT_OF_INTEREST_TYPE);
|
||||
PoiType type = poiReg.getOptional(resource)
|
||||
.orElseThrow(() -> new ThrowStatement(poiTypeString, Throwables.UNKNOWN_POI));
|
||||
Holder<PoiType> holder = BuiltInRegistries.POINT_OF_INTEREST_TYPE.getHolderOrThrow(ResourceKey.create(Registries.POINT_OF_INTEREST_TYPE, resource));
|
||||
Holder<PoiType> holder = poiReg.getHolderOrThrow(ResourceKey.create(Registries.POINT_OF_INTEREST_TYPE, resource));
|
||||
|
||||
int occupancy = 0;
|
||||
if (locator.offset + 1 < lv.size())
|
||||
@@ -822,7 +824,7 @@ public class WorldAccess {
|
||||
{
|
||||
playerBreak = true;
|
||||
String itemString = val.getString();
|
||||
item = BuiltInRegistries.ITEM.getOptional(InputValidator.identifierOf(itemString))
|
||||
item = cc.registry(Registries.ITEM).getOptional(InputValidator.identifierOf(itemString))
|
||||
.orElseThrow(() -> new ThrowStatement(itemString, Throwables.UNKNOWN_ITEM));
|
||||
}
|
||||
}
|
||||
@@ -971,7 +973,7 @@ public class WorldAccess {
|
||||
attacker = (LivingEntity) attackingEntity;
|
||||
}
|
||||
else throw new InternalExpressionException("Attacking entity needs to be a living thing, "+
|
||||
ValueConversions.of(BuiltInRegistries.ENTITY_TYPE.getKey(attackingEntity.getType())).getString() +" ain't it.");
|
||||
ValueConversions.of(cc.registry(Registries.ENTITY_TYPE).getKey(attackingEntity.getType())).getString() +" ain't it.");
|
||||
|
||||
}
|
||||
else
|
||||
@@ -1131,12 +1133,12 @@ public class WorldAccess {
|
||||
|
||||
expression.addContextFunction("block_list", -1, (c, t, lv) ->
|
||||
{
|
||||
if (lv.size() == 0)
|
||||
return ListValue.wrap(BuiltInRegistries.BLOCK.keySet().stream().map(ValueConversions::of).collect(Collectors.toList()));
|
||||
CarpetContext cc = (CarpetContext)c;
|
||||
Registry<Block> blocks = cc.registry(Registries.BLOCK);
|
||||
if (lv.size() == 0)
|
||||
return ListValue.wrap(blocks.keySet().stream().map(ValueConversions::of).collect(Collectors.toList()));
|
||||
ResourceLocation tag = InputValidator.identifierOf(lv.get(0).getString());
|
||||
|
||||
Registry<Block> blocks = cc.registry(Registries.BLOCK);
|
||||
Optional<HolderSet.Named<Block>> tagset = blocks.getTag(TagKey.create(Registries.BLOCK, tag));
|
||||
if (tagset.isEmpty()) return Value.NULL;
|
||||
return ListValue.wrap(tagset.get().stream().map(b -> ValueConversions.of(blocks.getKey(b.value()))).collect(Collectors.toList()));
|
||||
@@ -1344,7 +1346,7 @@ public class WorldAccess {
|
||||
}
|
||||
else
|
||||
{
|
||||
StructureType<?> sss = BuiltInRegistries.STRUCTURE_TYPE.get(id);
|
||||
StructureType<?> sss = cc.registry(Registries.STRUCTURE_TYPE).get(id);
|
||||
reg.entrySet().stream().filter(e -> e.getValue().type() ==sss).forEach(e -> structure.add(e.getValue()));
|
||||
}
|
||||
if (structure.isEmpty())
|
||||
@@ -1371,7 +1373,7 @@ public class WorldAccess {
|
||||
StructureStart start = FeatureGenerator.shouldStructureStartAt(world, pos, structure.get(0), needSize);
|
||||
if (start == null) return Value.NULL;
|
||||
if (!needSize) return Value.TRUE;
|
||||
return ValueConversions.of(start);
|
||||
return ValueConversions.of(start, cc.registryAccess());
|
||||
}
|
||||
Map<Value, Value> ret = new HashMap<>();
|
||||
for(Structure str: structure)
|
||||
@@ -1390,7 +1392,7 @@ public class WorldAccess {
|
||||
if (start == null) continue;
|
||||
|
||||
Value key = new StringValue(NBTSerializableValue.nameFromRegistryId(reg.getKey(str)));
|
||||
ret.put(key, (!needSize)?Value.NULL: ValueConversions.of(start));
|
||||
ret.put(key, (!needSize)?Value.NULL: ValueConversions.of(start, cc.registryAccess()));
|
||||
}
|
||||
return MapValue.wrap(ret);
|
||||
});
|
||||
@@ -1420,7 +1422,7 @@ public class WorldAccess {
|
||||
return MapValue.wrap(structureList);
|
||||
}
|
||||
String structureName = lv.get(locator.offset).getString().toLowerCase(Locale.ROOT);
|
||||
return ValueConversions.of(structures.get(reg.get(InputValidator.identifierOf(structureName))));
|
||||
return ValueConversions.of(structures.get(reg.get(InputValidator.identifierOf(structureName))), cc.registryAccess());
|
||||
});
|
||||
|
||||
expression.addContextFunction("set_structure", -1, (c, t, lv) ->
|
||||
|
||||
Reference in new issue
Block a user