Search in sources :

Example 16 with ResourceKey

use of org.spongepowered.api.ResourceKey in project SpongeCommon by SpongePowered.

the class SpongeResourceKeyBuilder method build.

@Override
public ResourceKey build() throws IllegalStateException {
    checkState(this.namespace != null, "Namespace cannot be empty");
    checkState(this.value != null, "Value cannot be empty");
    try {
        final ResourceLocation resourceLocation = new ResourceLocation(this.namespace, this.value);
        return (ResourceKey) (Object) resourceLocation;
    } catch (ResourceLocationException e) {
        throw new IllegalStateException(e);
    }
}
Also used : ResourceLocation(net.minecraft.resources.ResourceLocation) ResourceLocationException(net.minecraft.ResourceLocationException) ResourceKey(org.spongepowered.api.ResourceKey)

Example 17 with ResourceKey

use of org.spongepowered.api.ResourceKey in project SpongeCommon by SpongePowered.

the class BlockEntityMixin_API method toContainer.

@Override
public DataContainer toContainer() {
    final ResourceKey key = (ResourceKey) (Object) Registry.BLOCK_ENTITY_TYPE.getKey(this.type);
    final DataContainer container = DataContainer.createNew().set(Queries.CONTENT_VERSION, this.contentVersion()).set(Queries.WORLD_KEY, ((ServerWorld) this.level).key()).set(Queries.POSITION_X, this.shadow$getBlockPos().getX()).set(Queries.POSITION_Y, this.shadow$getBlockPos().getY()).set(Queries.POSITION_Z, this.shadow$getBlockPos().getZ()).set(Constants.TileEntity.TILE_TYPE, key);
    final CompoundTag compound = new CompoundTag();
    this.shadow$save(compound);
    // We must filter the custom data so it isn't stored twice
    Constants.NBT.filterSpongeCustomData(compound);
    container.set(Constants.Sponge.UNSAFE_NBT, NBTTranslator.INSTANCE.translateFrom(compound));
    return container;
}
Also used : DataContainer(org.spongepowered.api.data.persistence.DataContainer) CompoundTag(net.minecraft.nbt.CompoundTag) ResourceKey(org.spongepowered.api.ResourceKey)

Example 18 with ResourceKey

use of org.spongepowered.api.ResourceKey in project SpongeCommon by SpongePowered.

the class LevelMixin method bridge$createEntity.

@Override
@SuppressWarnings("unchecked")
public <E extends org.spongepowered.api.entity.Entity> E bridge$createEntity(final EntityType<E> type, final Vector3d position, final boolean naturally) throws IllegalArgumentException, IllegalStateException {
    if (type == net.minecraft.world.entity.EntityType.PLAYER) {
        // Unable to construct these
        throw new IllegalArgumentException("A Player cannot be created by the API!");
    }
    net.minecraft.world.entity.Entity entity = null;
    final double x = position.x();
    final double y = position.y();
    final double z = position.z();
    final net.minecraft.world.level.Level thisWorld = (net.minecraft.world.level.Level) (Object) this;
    // Not all entities have a single World parameter as their constructor
    if (type == net.minecraft.world.entity.EntityType.LIGHTNING_BOLT) {
        entity = net.minecraft.world.entity.EntityType.LIGHTNING_BOLT.create(thisWorld);
        entity.moveTo(x, y, z);
        ((LightningBolt) entity).setVisualOnly(false);
    }
    // TODO - archetypes should solve the problem of calling the correct constructor
    if (type == net.minecraft.world.entity.EntityType.ENDER_PEARL) {
        final ArmorStand tempEntity = new ArmorStand(thisWorld, x, y, z);
        tempEntity.setPos(tempEntity.getX(), tempEntity.getY() - tempEntity.getEyeHeight(), tempEntity.getZ());
        entity = new ThrownEnderpearl(thisWorld, tempEntity);
        ((EnderPearl) entity).offer(Keys.SHOOTER, UnknownProjectileSource.UNKNOWN);
    }
    // set them is to use the more specialised constructor).
    if (type == net.minecraft.world.entity.EntityType.FALLING_BLOCK) {
        entity = new FallingBlockEntity(thisWorld, x, y, z, Blocks.SAND.defaultBlockState());
    }
    if (type == net.minecraft.world.entity.EntityType.ITEM) {
        entity = new ItemEntity(thisWorld, x, y, z, new ItemStack(Blocks.STONE));
    }
    if (entity == null) {
        final ResourceKey key = (ResourceKey) (Object) Registry.ENTITY_TYPE.getKey((net.minecraft.world.entity.EntityType<?>) type);
        try {
            entity = ((net.minecraft.world.entity.EntityType) type).create(thisWorld);
            entity.moveTo(x, y, z);
        } catch (final Exception e) {
            throw new RuntimeException("There was an issue attempting to construct " + key, e);
        }
    }
    if (entity instanceof HangingEntity) {
        if (!((HangingEntity) entity).survives()) {
            throw new IllegalArgumentException("Hanging entity does not survive at the given position: " + position);
        }
    }
    if (naturally && entity instanceof Mob) {
        // Adding the default equipment
        final DifficultyInstance difficulty = this.shadow$getCurrentDifficultyAt(new BlockPos(x, y, z));
        ((MobAccessor) entity).invoker$populateDefaultEquipmentSlots(difficulty);
    }
    if (entity instanceof Painting) {
        // This is default when art is null when reading from NBT, could
        // choose a random art instead?
        ((Painting) entity).motive = Motive.KEBAB;
    }
    return (E) entity;
}
Also used : DifficultyInstance(net.minecraft.world.DifficultyInstance) ThrownEnderpearl(net.minecraft.world.entity.projectile.ThrownEnderpearl) LightningBolt(net.minecraft.world.entity.LightningBolt) ArmorStand(net.minecraft.world.entity.decoration.ArmorStand) BlockPos(net.minecraft.core.BlockPos) FallingBlockEntity(net.minecraft.world.entity.item.FallingBlockEntity) HangingEntity(net.minecraft.world.entity.decoration.HangingEntity) Mob(net.minecraft.world.entity.Mob) ItemEntity(net.minecraft.world.entity.item.ItemEntity) EnderPearl(org.spongepowered.api.entity.projectile.EnderPearl) ResourceKey(org.spongepowered.api.ResourceKey) Painting(net.minecraft.world.entity.decoration.Painting) MobAccessor(org.spongepowered.common.accessor.world.entity.MobAccessor) Level(net.minecraft.world.level.Level) Level(net.minecraft.world.level.Level) ItemStack(net.minecraft.world.item.ItemStack)

Example 19 with ResourceKey

use of org.spongepowered.api.ResourceKey in project SpongeCommon by SpongePowered.

the class PrimaryLevelDataMixin method bridge$populateFromDimension.

@Override
public void bridge$populateFromDimension(final LevelStem dimension) {
    final LevelStemBridge levelStemBridge = (LevelStemBridge) (Object) dimension;
    this.impl$key = ((ResourceKeyBridge) (Object) dimension).bridge$getKey();
    this.impl$dimensionType = dimension.type();
    this.impl$displayName = levelStemBridge.bridge$displayName().orElse(null);
    levelStemBridge.bridge$difficulty().ifPresent(v -> {
        ((LevelSettingsAccessor) (Object) this.settings).accessor$difficulty(RegistryTypes.DIFFICULTY.get().value((ResourceKey) (Object) v));
        this.impl$customDifficulty = true;
    });
    levelStemBridge.bridge$gameMode().ifPresent(v -> {
        ((LevelSettingsAccessor) (Object) this.settings).accessor$gameType(RegistryTypes.GAME_MODE.get().value((ResourceKey) (Object) v));
        this.impl$customGameType = true;
    });
    levelStemBridge.bridge$spawnPosition().ifPresent(v -> {
        this.setSpawn(VecHelper.toBlockPos(v), this.spawnAngle);
        this.impl$customSpawnPosition = true;
    });
    levelStemBridge.bridge$hardcore().ifPresent(v -> ((LevelSettingsAccessor) (Object) this.settings).accessor$hardcode(v));
    this.impl$serializationBehavior = levelStemBridge.bridge$serializationBehavior().orElse(null);
    this.impl$pvp = levelStemBridge.bridge$pvp().orElse(null);
    this.impl$loadOnStartup = levelStemBridge.bridge$loadOnStartup();
    this.impl$performsSpawnLogic = levelStemBridge.bridge$performsSpawnLogic();
    this.impl$viewDistance = levelStemBridge.bridge$viewDistance().orElse(null);
}
Also used : LevelStemBridge(org.spongepowered.common.bridge.world.level.dimension.LevelStemBridge) LevelSettingsAccessor(org.spongepowered.common.accessor.world.level.LevelSettingsAccessor) ResourceKey(org.spongepowered.api.ResourceKey)

Example 20 with ResourceKey

use of org.spongepowered.api.ResourceKey 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));
    }
}
Also used : Optional(java.util.Optional) ArgumentParseException(org.spongepowered.api.command.exception.ArgumentParseException) Registry(org.spongepowered.api.registry.Registry) ArgumentReader(org.spongepowered.api.command.parameter.ArgumentReader) ResourceKey(org.spongepowered.api.ResourceKey) NonNull(org.checkerframework.checker.nullness.qual.NonNull)

Aggregations

ResourceKey (org.spongepowered.api.ResourceKey)71 Sponge (org.spongepowered.api.Sponge)13 List (java.util.List)11 Map (java.util.Map)11 DataContainer (org.spongepowered.api.data.persistence.DataContainer)11 Optional (java.util.Optional)10 Collectors (java.util.stream.Collectors)10 ArrayList (java.util.ArrayList)8 Collection (java.util.Collection)8 CompoundTag (net.minecraft.nbt.CompoundTag)8 NonNull (org.checkerframework.checker.nullness.qual.NonNull)8 RegistryTypes (org.spongepowered.api.registry.RegistryTypes)8 IOException (java.io.IOException)7 Component (net.kyori.adventure.text.Component)7 Nullable (org.checkerframework.checker.nullness.qual.Nullable)7 Keys (org.spongepowered.api.data.Keys)7 Constants (org.spongepowered.common.util.Constants)7 ImmutableList (com.google.common.collect.ImmutableList)6 ServerLevel (net.minecraft.server.level.ServerLevel)6 DataQuery (org.spongepowered.api.data.persistence.DataQuery)6