use of org.spongepowered.api.ResourceKey in project SpongeCommon by SpongePowered.
the class SpongeLocatableBlockBuilder method buildContent.
@Override
protected Optional<LocatableBlock> buildContent(final DataView container) throws InvalidDataException {
final ResourceKey worldKey = container.getResourceKey(Queries.WORLD_KEY).orElseThrow(() -> new InvalidDataException("Could not locate a world key"));
final int x = container.getInt(Queries.POSITION_X).orElseThrow(() -> new InvalidDataException("Could not locate an \"x\" coordinate in the container!"));
final int y = container.getInt(Queries.POSITION_Y).orElseThrow(() -> new InvalidDataException("Could not locate an \"y\" coordinate in the container!"));
final int z = container.getInt(Queries.POSITION_Z).orElseThrow(() -> new InvalidDataException("Could not locate an \"z\" coordinate in the container!"));
final BlockState blockState = container.getSerializable(Constants.Block.BLOCK_STATE, BlockState.class).orElseThrow(() -> new InvalidDataException("Could not locate a BlockState"));
return Sponge.server().worldManager().world(worldKey).map(world -> new SpongeLocatableBlockBuilder().position(x, y, z).world(world).state(blockState).build());
}
use of org.spongepowered.api.ResourceKey in project SpongeCommon by SpongePowered.
the class SchematicTranslator method deserializeBiomeContainer.
private static void deserializeBiomeContainer(final DataView view, final SpongeArchetypeVolume archetypeVolume, final int width, final int length, final Vector3i offset) {
final MutableBimapPalette<Biome, Biome> biomePalette;
final DataView biomeMap = view.getView(Constants.Sponge.Schematic.BIOME_PALETTE).orElseThrow(() -> new InvalidDataException("Missing BiomePalette as required by the schematic spec"));
final Set<DataQuery> biomeKeys = biomeMap.keys(false);
final Registry<Biome> biomeRegistry = VolumeStreamUtils.nativeToSpongeRegistry(BuiltinRegistries.BIOME);
biomePalette = new MutableBimapPalette<>(PaletteTypes.BIOME_PALETTE.get(), biomeRegistry, RegistryTypes.BIOME, biomeKeys.size());
for (final DataQuery biomeKey : biomeKeys) {
final ResourceKey key = ResourceKey.resolve(biomeKey.parts().get(0));
final Biome biome = biomeRegistry.findValue(key).get();
biomePalette.assign(biome, biomeMap.getInt(biomeKey).get());
}
final byte[] biomeData = (byte[]) view.get(Constants.Sponge.Schematic.BIOME_DATA).orElseThrow(() -> new InvalidDataException("Missing BlockData for Schematic"));
SchematicTranslator.readByteArrayData(width, (width * length), offset, biomePalette, biomeData, archetypeVolume, BiomeVolume.Modifiable::setBiome);
}
use of org.spongepowered.api.ResourceKey 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;
}
use of org.spongepowered.api.ResourceKey in project SpongeCommon by SpongePowered.
the class VanishableData method register.
// @formatter:off
@SuppressWarnings("unchecked")
public static void register(final DataProviderRegistrator registrator) {
registrator.asMutable(VanishableBridge.class).create(Keys.IS_INVISIBLE).get(VanishableBridge::bridge$isInvisible).set(VanishableBridge::bridge$setInvisible).create(Keys.VANISH).get(bridge -> bridge.bridge$vanishState().invisible()).setAnd((h, v) -> {
if (h instanceof Entity && ((Entity) h).level.isClientSide) {
return false;
}
h.bridge$vanishState(v ? VanishState.vanished() : VanishState.unvanished());
return true;
}).create(Keys.VANISH_IGNORES_COLLISION).get(b -> b.bridge$vanishState().ignoresCollisions()).setAnd((h, v) -> {
if (h instanceof Entity && ((Entity) h).level.isClientSide) {
return false;
}
if (!h.bridge$vanishState().invisible()) {
return false;
}
h.bridge$vanishState(h.bridge$vanishState().ignoreCollisions(v));
return true;
}).create(Keys.VANISH_PREVENTS_TARGETING).get(b -> b.bridge$vanishState().untargetable()).setAnd((h, v) -> {
if (h instanceof Entity && ((Entity) h).level.isClientSide) {
return false;
}
if (!h.bridge$vanishState().invisible()) {
return false;
}
h.bridge$vanishState(h.bridge$vanishState().untargetable(v));
return true;
}).create(Keys.VANISH_STATE).get(VanishableBridge::bridge$vanishState).setAnd((h, v) -> {
if (h instanceof Entity && ((Entity) h).level.isClientSide) {
return false;
}
h.bridge$vanishState(v);
return true;
});
final ResourceKey dataStoreKey = ResourceKey.sponge("invisibility");
registrator.spongeDataStore(dataStoreKey, VanishableBridge.class, Keys.IS_INVISIBLE, Keys.VANISH_STATE);
SpongeDataManager.INSTANCE.registerLegacySpongeData(Constants.Sponge.Entity.IS_INVISIBLE, dataStoreKey, Keys.IS_INVISIBLE);
SpongeDataManager.INSTANCE.registerLegacySpongeData(Constants.Sponge.Entity.IS_VANISHED, dataStoreKey, Keys.VANISH_STATE);
SpongeDataManager.INSTANCE.registerLegacySpongeData(Constants.Sponge.Entity.VANISH_UNCOLLIDEABLE, dataStoreKey, Keys.VANISH_IGNORES_COLLISION);
SpongeDataManager.INSTANCE.registerLegacySpongeData(Constants.Sponge.Entity.VANISH_UNTARGETABLE, dataStoreKey, Keys.VANISH_PREVENTS_TARGETING);
}
use of org.spongepowered.api.ResourceKey in project SpongeCommon by SpongePowered.
the class SpongeRegistryLoaders method valueParameter.
@SuppressWarnings("ConstantConditions")
public static RegistryLoader<ValueParameter<?>> valueParameter() {
return RegistryLoader.of(l -> {
l.add(ResourceKeyedValueParameters.BIG_DECIMAL, SpongeBigDecimalValueParameter::new);
l.add(ResourceKeyedValueParameters.BIG_INTEGER, SpongeBigIntegerValueParameter::new);
l.add(ResourceKeyedValueParameters.BLOCK_STATE, k -> ClientNativeArgumentParser.createConverter(k, BlockStateArgument.block(), (reader, cause, state) -> (BlockState) state.getState()));
l.add(ResourceKeyedValueParameters.BOOLEAN, k -> ClientNativeArgumentParser.createIdentity(k, BoolArgumentType.bool()));
l.add(ResourceKeyedValueParameters.COLOR, SpongeColorValueParameter::new);
l.add(ResourceKeyedValueParameters.DATA_CONTAINER, SpongeDataContainerValueParameter::new);
l.add(ResourceKeyedValueParameters.DATE_TIME, SpongeDateTimeValueParameter::new);
l.add(ResourceKeyedValueParameters.DOUBLE, k -> ClientNativeArgumentParser.createIdentity(k, DoubleArgumentType.doubleArg()));
l.add(ResourceKeyedValueParameters.DURATION, SpongeDurationValueParameter::new);
l.add(ResourceKeyedValueParameters.ENTITY, k -> ClientNativeArgumentParser.createConverter(k, EntityArgument.entity(), (reader, cause, selector) -> (Entity) selector.findSingleEntity((CommandSourceStack) cause)));
l.add(ResourceKeyedValueParameters.GAME_PROFILE, SpongeGameProfileValueParameter::new);
l.add(ResourceKeyedValueParameters.INTEGER, k -> ClientNativeArgumentParser.createIdentity(k, IntegerArgumentType.integer()));
l.add(ResourceKeyedValueParameters.IP, SpongeIPAddressValueParameter::new);
l.add(ResourceKeyedValueParameters.ITEM_STACK_SNAPSHOT, k -> ClientNativeArgumentParser.createConverter(k, ItemArgument.item(), (reader, cause, converter) -> new SpongeItemStackSnapshot((ItemStack) (Object) converter.createItemStack(1, true))));
l.add(ResourceKeyedValueParameters.LOCATION, SpongeServerLocationValueParameter::new);
l.add(ResourceKeyedValueParameters.LONG, k -> ClientNativeArgumentParser.createIdentity(k, LongArgumentType.longArg()));
l.add(ResourceKeyedValueParameters.MANY_ENTITIES, k -> ClientNativeArgumentParser.createConverter(k, EntityArgument.entities(), (reader, cause, selector) -> selector.findEntities((CommandSourceStack) cause).stream().map(x -> (Entity) x).collect(Collectors.toList())));
l.add(ResourceKeyedValueParameters.MANY_GAME_PROFILES, k -> ClientNativeArgumentParser.createConverter(k, GameProfileArgument.gameProfile(), (reader, cause, converter) -> converter.getNames((CommandSourceStack) cause)));
l.add(ResourceKeyedValueParameters.MANY_PLAYERS, k -> ClientNativeArgumentParser.createConverter(k, EntityArgument.players(), (reader, cause, selector) -> selector.findPlayers((CommandSourceStack) cause)));
l.add(ResourceKeyedValueParameters.NONE, SpongeNoneValueParameter::new);
l.add(ResourceKeyedValueParameters.OPERATOR, SpongeOperatorValueParameter::new);
l.add(ResourceKeyedValueParameters.PLAYER, k -> ClientNativeArgumentParser.createConverter(k, EntityArgument.player(), (reader, cause, selector) -> (Player) selector.findSinglePlayer((CommandSourceStack) cause)));
l.add(ResourceKeyedValueParameters.PLUGIN, SpongePluginContainerValueParameter::new);
l.add(ResourceKeyedValueParameters.REMAINING_JOINED_STRINGS, k -> ClientNativeArgumentParser.createIdentity(k, StringArgumentType.greedyString()));
l.add(ResourceKeyedValueParameters.RESOURCE_KEY, k -> ClientNativeArgumentParser.createConverter(k, ResourceLocationArgument.id(), (reader, cause, resourceLocation) -> (ResourceKey) (Object) resourceLocation));
l.add(ResourceKeyedValueParameters.ROTATION, k -> ClientNativeArgumentParser.createConverter(k, RotationArgument.rotation(), (reader, cause, coords) -> {
final Vec2 rotation = coords.getRotation((CommandSourceStack) cause);
return new Vector3d(rotation.x, rotation.y, 0);
}));
l.add(ResourceKeyedValueParameters.STRING, k -> ClientNativeArgumentParser.createIdentity(k, StringArgumentType.string()));
l.add(ResourceKeyedValueParameters.TARGET_BLOCK, SpongeTargetBlockValueParameter::new);
l.add(ResourceKeyedValueParameters.TARGET_ENTITY, k -> new SpongeTargetEntityValueParameter(k, false));
l.add(ResourceKeyedValueParameters.TARGET_PLAYER, k -> new SpongeTargetEntityValueParameter(k, true));
l.add(ResourceKeyedValueParameters.TEXT_FORMATTING_CODE, k -> ClientNativeArgumentParser.createConverter(k, StringArgumentType.string(), (reader, cause, result) -> LegacyComponentSerializer.legacyAmpersand().deserialize(result)));
l.add(ResourceKeyedValueParameters.TEXT_FORMATTING_CODE_ALL, k -> ClientNativeArgumentParser.createConverter(k, StringArgumentType.greedyString(), (reader, cause, result) -> LegacyComponentSerializer.legacyAmpersand().deserialize(result)));
l.add(ResourceKeyedValueParameters.TEXT_JSON, k -> ClientNativeArgumentParser.createConverter(k, ComponentArgument.textComponent(), (reader, cause, result) -> SpongeAdventure.asAdventure(result)));
l.add(ResourceKeyedValueParameters.TEXT_JSON_ALL, k -> ClientNativeArgumentParser.createConverter(k, StringArgumentType.greedyString(), (reader, cause, result) -> GsonComponentSerializer.gson().deserialize(result)));
l.add(ResourceKeyedValueParameters.URL, k -> ClientNativeArgumentParser.createConverter(k, StringArgumentType.string(), (reader, cause, input) -> {
try {
return new URL(input);
} catch (final MalformedURLException ex) {
throw new SimpleCommandExceptionType(new TextComponent("Could not parse " + input + " as a URL")).createWithContext(reader);
}
}));
l.add(ResourceKeyedValueParameters.USER, SpongeUserValueParameter::new);
l.add(ResourceKeyedValueParameters.UUID, k -> ClientNativeArgumentParser.createIdentity(k, UuidArgument.uuid()));
l.add(ResourceKeyedValueParameters.VECTOR2D, k -> ClientNativeArgumentParser.createConverter(k, Vec2Argument.vec2(), (reader, cause, result) -> {
final net.minecraft.world.phys.Vec3 r = result.getPosition((CommandSourceStack) cause);
return new Vector2d(r.x, r.z);
}));
l.add(ResourceKeyedValueParameters.VECTOR3D, k -> ClientNativeArgumentParser.createConverter(k, Vec3Argument.vec3(false), (reader, cause, result) -> VecHelper.toVector3d(result.getPosition((CommandSourceStack) cause))));
l.add(ResourceKeyedValueParameters.WORLD, k -> ClientNativeArgumentParser.createConverter(k, DimensionArgument.dimension(), (reader, cause, result) -> Sponge.server().worldManager().world((ResourceKey) (Object) result).orElseThrow(() -> DimensionArgumentAccessor.accessor$ERROR_INVALID_VALUE().createWithContext(reader, result))));
});
}
Aggregations