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);
}
}
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;
}
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;
}
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);
}
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));
}
}
Aggregations