Search in sources :

Example 51 with ResourceLocation

use of net.minecraft.resources.ResourceLocation in project SpongeCommon by SpongePowered.

the class BlockStatePropertiesGenerator method computeUsedProperties.

private Map<PropertyType, Map<Property<?>, Set<ResourceLocation>>> computeUsedProperties() {
    // get all block state properties
    final Map<PropertyType, Map<Property<?>, Set<ResourceLocation>>> propertyUsages = new HashMap<>();
    for (final Block block : Registry.BLOCK) {
        for (final Property<?> property : block.defaultBlockState().getProperties()) {
            final var type = PropertyType.ofProperty(property);
            if (type == null) {
                Logger.warn("Unknown property type for state property {} in block {}", property, Registry.BLOCK.getKey(block));
            }
            propertyUsages.computeIfAbsent(type, $ -> new IdentityHashMap<>()).computeIfAbsent(property, $ -> new HashSet<>()).add(Registry.BLOCK.getKey(block));
        }
    }
    return propertyUsages;
}
Also used : ResourceLocation(net.minecraft.resources.ResourceLocation) IdentityHashMap(java.util.IdentityHashMap) Modifier(javax.lang.model.element.Modifier) TypeDeclaration(com.github.javaparser.ast.body.TypeDeclaration) MethodSpec(com.squareup.javapoet.MethodSpec) FieldSpec(com.squareup.javapoet.FieldSpec) ClassName(com.squareup.javapoet.ClassName) BlockState(net.minecraft.world.level.block.state.BlockState) Set(java.util.Set) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) IOException(java.io.IOException) HashMap(java.util.HashMap) Property(net.minecraft.world.level.block.state.properties.Property) Logger(org.tinylog.Logger) Registry(net.minecraft.core.Registry) HashSet(java.util.HashSet) BooleanProperty(net.minecraft.world.level.block.state.properties.BooleanProperty) IntegerProperty(net.minecraft.world.level.block.state.properties.IntegerProperty) EnumProperty(net.minecraft.world.level.block.state.properties.EnumProperty) Locale(java.util.Locale) Map(java.util.Map) TypeName(com.squareup.javapoet.TypeName) Block(net.minecraft.world.level.block.Block) Comparator(java.util.Comparator) IdentityHashMap(java.util.IdentityHashMap) HashMap(java.util.HashMap) ResourceLocation(net.minecraft.resources.ResourceLocation) Block(net.minecraft.world.level.block.Block) IdentityHashMap(java.util.IdentityHashMap) HashMap(java.util.HashMap) Map(java.util.Map) HashSet(java.util.HashSet)

Example 52 with ResourceLocation

use of net.minecraft.resources.ResourceLocation in project SpongeCommon by SpongePowered.

the class SpongeEntityArchetype method apply.

@Override
public Optional<org.spongepowered.api.entity.Entity> apply(final ServerLocation location) {
    if (!PlatformHooks.INSTANCE.getGeneralHooks().onServerThread()) {
        return Optional.empty();
    }
    final org.spongepowered.api.world.server.ServerWorld spongeWorld = location.world();
    final ServerLevel level = (ServerLevel) spongeWorld;
    final ResourceLocation key = net.minecraft.world.entity.EntityType.getKey((net.minecraft.world.entity.EntityType<?>) this.type);
    if (key == null) {
        return Optional.empty();
    }
    final CompoundTag compound = this.compound.copy();
    compound.putString(Constants.Entity.ENTITY_TYPE_ID, key.toString());
    final ListTag pos = new ListTag();
    pos.add(DoubleTag.valueOf(location.x()));
    pos.add(DoubleTag.valueOf(location.y()));
    pos.add(DoubleTag.valueOf(location.z()));
    compound.put(Constants.Entity.ENTITY_POSITION, pos);
    compound.remove(Constants.Entity.ENTITY_UUID);
    final boolean requiresInitialSpawn;
    if (compound.contains(Constants.Sponge.EntityArchetype.REQUIRES_EXTRA_INITIAL_SPAWN)) {
        requiresInitialSpawn = compound.getBoolean(Constants.Sponge.EntityArchetype.REQUIRES_EXTRA_INITIAL_SPAWN);
        compound.remove(Constants.Sponge.EntityArchetype.REQUIRES_EXTRA_INITIAL_SPAWN);
    } else {
        requiresInitialSpawn = true;
    }
    @Nullable final Entity entity = net.minecraft.world.entity.EntityType.loadEntityRecursive(compound, level, e -> {
        e.moveTo(location.x(), location.y(), location.z());
        if (requiresInitialSpawn && e instanceof Mob) {
            ((Mob) e).finalizeSpawn(level, level.getCurrentDifficultyAt(e.blockPosition()), MobSpawnType.COMMAND, null, compound);
        }
        return e;
    });
    if (entity == null) {
        return Optional.empty();
    }
    if (level.tryAddFreshEntityWithPassengers(entity)) {
        return Optional.of((org.spongepowered.api.entity.Entity) entity);
    }
    return Optional.empty();
}
Also used : ServerLevel(net.minecraft.server.level.ServerLevel) Entity(net.minecraft.world.entity.Entity) Mob(net.minecraft.world.entity.Mob) ListTag(net.minecraft.nbt.ListTag) ResourceLocation(net.minecraft.resources.ResourceLocation) CompoundTag(net.minecraft.nbt.CompoundTag) Nullable(org.checkerframework.checker.nullness.qual.Nullable)

Example 53 with ResourceLocation

use of net.minecraft.resources.ResourceLocation 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 54 with ResourceLocation

use of net.minecraft.resources.ResourceLocation in project SpongeCommon by SpongePowered.

the class SpongeFluidStateBuilder method readFluid.

private Fluid readFluid(final StringReader reader) throws CommandSyntaxException {
    final int cursor = reader.getCursor();
    final ResourceLocation fluidKey = ResourceLocation.read(reader);
    return Registry.FLUID.getOptional(fluidKey).orElseThrow(() -> {
        reader.setCursor(cursor);
        return BlockStateParser.ERROR_UNKNOWN_BLOCK.createWithContext(reader, fluidKey.toString());
    });
}
Also used : ResourceLocation(net.minecraft.resources.ResourceLocation)

Example 55 with ResourceLocation

use of net.minecraft.resources.ResourceLocation in project SpongeCommon by SpongePowered.

the class SpongeStonecuttingRecipeSerializer method fromJson.

@SuppressWarnings("unchecked")
@Override
public R fromJson(ResourceLocation recipeId, JsonObject json) {
    final String group = GsonHelper.getAsString(json, Constants.Recipe.GROUP, "");
    final Ingredient ingredient = IngredientUtil.spongeDeserialize(json.get(Constants.Recipe.STONECUTTING_INGREDIENT));
    final Function<Container, ItemStack> resultFunction = IngredientResultUtil.deserializeResultFunction(json);
    final ItemStack spongeStack = IngredientResultUtil.deserializeItemStack(json.getAsJsonObject(Constants.Recipe.SPONGE_RESULT));
    if (spongeStack != null) {
        return (R) new SpongeStonecuttingRecipe(recipeId, group, ingredient, spongeStack, resultFunction);
    }
    final String type = GsonHelper.getAsString(json, Constants.Recipe.RESULT);
    final int count = GsonHelper.getAsInt(json, Constants.Recipe.COUNT);
    final ItemStack itemstack = new ItemStack(Registry.ITEM.get(new ResourceLocation(type)), count);
    return (R) new SpongeStonecuttingRecipe(recipeId, group, ingredient, itemstack, resultFunction);
}
Also used : Container(net.minecraft.world.Container) Ingredient(net.minecraft.world.item.crafting.Ingredient) ResourceLocation(net.minecraft.resources.ResourceLocation) ItemStack(net.minecraft.world.item.ItemStack)

Aggregations

ResourceLocation (net.minecraft.resources.ResourceLocation)130 Map (java.util.Map)12 IOException (java.io.IOException)11 ArrayList (java.util.ArrayList)11 CompoundTag (net.minecraft.nbt.CompoundTag)11 List (java.util.List)10 BlockPos (net.minecraft.core.BlockPos)9 Collectors (java.util.stream.Collectors)7 Block (net.minecraft.world.level.block.Block)7 LogManager (org.apache.logging.log4j.LogManager)7 Logger (org.apache.logging.log4j.Logger)7 JsonObject (com.google.gson.JsonObject)6 HashMap (java.util.HashMap)6 FriendlyByteBuf (net.minecraft.network.FriendlyByteBuf)6 LivingEntity (net.minecraft.world.entity.LivingEntity)6 JsonElement (com.google.gson.JsonElement)5 CommandSyntaxException (com.mojang.brigadier.exceptions.CommandSyntaxException)5 Registry (net.minecraft.core.Registry)5 ServerLevel (net.minecraft.server.level.ServerLevel)5 InputStream (java.io.InputStream)4