use of org.spongepowered.api.registry.Registry in project SpongeCommon by SpongePowered.
the class SpongeCatalogedElementValueParameter method parseValue.
@Override
@NonNull
public Optional<? extends T> parseValue(final Parameter.@NonNull Key<? super T> parameterKey, final ArgumentReader.@NonNull Mutable reader, final CommandContext.@NonNull Builder context) throws ArgumentParseException {
final List<Registry<? extends T>> registry = this.registryHolderFunctions.stream().map(x -> this.retrieveRegistry(x, context)).filter(Objects::nonNull).collect(Collectors.toList());
if (registry.isEmpty()) {
throw reader.createException(Component.text("No registries associated with this parameter are active."));
}
final ArgumentReader.Immutable snapshot = reader.immutable();
try {
final ResourceKey resourceKey = reader.parseResourceKey();
final Optional<? extends T> result = this.selectValue(registry, resourceKey);
if (!result.isPresent()) {
throw reader.createException(Component.text("None of the selected registries contain the ID " + resourceKey.asString()));
}
return result;
} catch (final ArgumentParseException ex) {
if (this.prefixes.isEmpty()) {
throw ex;
}
reader.setState(snapshot);
final String check = reader.parseUnquotedString();
for (final String prefix : this.prefixes) {
final Optional<? extends T> result = this.selectValue(registry, ResourceKey.of(prefix, check));
if (result.isPresent()) {
return result;
}
}
final String ids = this.prefixes.stream().map(x -> x + ":" + check).collect(Collectors.joining(", "));
throw reader.createException(Component.text("None of the selected registries contain any of the following IDs: " + ids));
}
}
use of org.spongepowered.api.registry.Registry in project SpongeCommon by SpongePowered.
the class SchematicTranslator method addTo.
@Override
public DataView addTo(final Schematic schematic, final DataView data) {
final int xMin = schematic.min().x();
final int yMin = schematic.min().y();
final int zMin = schematic.min().z();
final int width = schematic.size().x();
final int height = schematic.size().y();
final int length = schematic.size().z();
if (width > Constants.Sponge.Schematic.MAX_SIZE || height > Constants.Sponge.Schematic.MAX_SIZE || length > Constants.Sponge.Schematic.MAX_SIZE) {
throw new IllegalArgumentException(String.format("Schematic is larger than maximum allowable size (found: (%d, %d, %d) max: (%d, %<d, %<d)", width, height, length, Constants.Sponge.Schematic.MAX_SIZE));
}
data.set(Constants.Sponge.Schematic.WIDTH, (short) width);
data.set(Constants.Sponge.Schematic.HEIGHT, (short) height);
data.set(Constants.Sponge.Schematic.LENGTH, (short) length);
data.set(Constants.Sponge.Schematic.VERSION, Constants.Sponge.Schematic.CURRENT_VERSION);
data.set(Constants.Sponge.Schematic.DATA_VERSION, SharedConstants.getCurrentVersion().getWorldVersion());
for (final DataQuery metaKey : schematic.metadata().keys(false)) {
data.set(Constants.Sponge.Schematic.METADATA.then(metaKey), schematic.metadata().get(metaKey).get());
}
final Set<String> requiredMods = new HashSet<>();
final int[] offset = new int[] { xMin, yMin, zMin };
data.set(Constants.Sponge.Schematic.OFFSET, offset);
// Check if we have blocks to store
if (schematic.blockPalette().highestId() != 0) {
final DataView blockData = data.createView(Constants.Sponge.Schematic.BLOCK_CONTAINER);
final Palette.Mutable<BlockState, BlockType> palette = schematic.blockPalette().asMutable(Sponge.server());
try (final ByteArrayOutputStream buffer = new ByteArrayOutputStream(width * height * length)) {
for (int y = 0; y < height; y++) {
final int y0 = yMin + y;
for (int z = 0; z < length; z++) {
final int z0 = zMin + z;
for (int x = 0; x < width; x++) {
final int x0 = xMin + x;
final BlockState state = schematic.block(x0, y0, z0);
SchematicTranslator.writeIdToBuffer(buffer, palette.orAssign(state));
}
}
}
blockData.set(Constants.Sponge.Schematic.BLOCK_DATA, buffer.toByteArray());
} catch (final IOException e) {
// should never reach here
}
final Registry<BlockType> blockRegistry = VolumeStreamUtils.nativeToSpongeRegistry(net.minecraft.core.Registry.BLOCK);
SchematicTranslator.writePaletteToView(blockData, palette, blockRegistry, Constants.Sponge.Schematic.BLOCK_PALETTE, BlockState::type, requiredMods);
final List<DataView> blockEntities = schematic.blockEntityArchetypes().entrySet().stream().map(entry -> {
final DataContainer container = DataContainer.createNew(DataView.SafetyMode.NO_DATA_CLONED);
final Vector3i pos = entry.getKey();
final BlockEntityArchetype archetype = entry.getValue();
final DataContainer entityData = archetype.blockEntityData();
final int[] apos = new int[] { pos.x() - xMin, pos.y() - yMin, pos.z() - zMin };
container.set(Constants.Sponge.Schematic.BLOCKENTITY_POS, apos);
container.set(Constants.Sponge.Schematic.BLOCKENTITY_DATA, entityData);
final ResourceKey key = archetype.blockEntityType().key(RegistryTypes.BLOCK_ENTITY_TYPE);
container.set(Constants.Sponge.Schematic.ENTITIES_ID, key.asString());
final String namespace = key.namespace();
if (!ResourceKey.MINECRAFT_NAMESPACE.equals(namespace)) {
requiredMods.add(namespace);
}
return container;
}).collect(Collectors.toList());
blockData.set(Constants.Sponge.Schematic.BLOCKENTITY_CONTAINER, blockEntities);
}
if (schematic.biomePalette().highestId() != 0) {
final DataView biomeContainer = data.createView(Constants.Sponge.Schematic.BIOME_CONTAINER);
final Palette.Mutable<Biome, Biome> biomePalette = schematic.biomePalette().asMutable(Sponge.game());
try (final ByteArrayOutputStream buffer = new ByteArrayOutputStream(width * height * length)) {
for (int y = 0; y < height; y++) {
final int y0 = yMin + y;
for (int z = 0; z < length; z++) {
final int z0 = zMin + z;
for (int x = 0; x < width; x++) {
final int x0 = xMin + x;
final Biome state = schematic.biome(x0, y0, z0);
SchematicTranslator.writeIdToBuffer(buffer, biomePalette.orAssign(state));
}
}
}
biomeContainer.set(Constants.Sponge.Schematic.BIOME_DATA, buffer.toByteArray());
} catch (final IOException e) {
// Should never reach here.
}
final Registry<Biome> biomeRegistry = VolumeStreamUtils.nativeToSpongeRegistry(BuiltinRegistries.BIOME);
SchematicTranslator.writePaletteToView(biomeContainer, biomePalette, biomeRegistry, Constants.Sponge.Schematic.BIOME_PALETTE, Function.identity(), requiredMods);
}
final List<DataView> entities = schematic.entityArchetypesByPosition().stream().map(entry -> {
final DataContainer container = DataContainer.createNew(DataView.SafetyMode.NO_DATA_CLONED);
final List<Double> entityPosition = new ArrayList<>();
entityPosition.add(entry.position().x());
entityPosition.add(entry.position().y());
entityPosition.add(entry.position().z());
container.set(Constants.Sponge.Schematic.ENTITIES_POS, entityPosition);
final ResourceKey key = entry.archetype().type().key(RegistryTypes.ENTITY_TYPE);
if (!ResourceKey.MINECRAFT_NAMESPACE.equals(key.namespace())) {
requiredMods.add(key.namespace());
}
container.set(Constants.Sponge.Schematic.ENTITIES_ID, key.toString());
final DataContainer entityData = entry.archetype().entityData();
container.set(Constants.Sponge.Schematic.BLOCKENTITY_DATA, entityData);
return container;
}).collect(Collectors.toList());
data.set(Constants.Sponge.Schematic.ENTITIES, entities);
if (!requiredMods.isEmpty()) {
data.set(Constants.Sponge.Schematic.METADATA.then(Constants.Sponge.Schematic.REQUIRED_MODS), requiredMods);
}
return data;
}
Aggregations