Search in sources :

Example 86 with ResourceLocation

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

the class SpongeTagTemplate method toJson.

public JsonObject toJson() {
    final Tag.Builder builder = new Tag.Builder();
    this.elements.forEach((k, v) -> {
        final ResourceLocation location = (ResourceLocation) (Object) k;
        // "N/A" is supposed to be the source, but we don't know it, and we're serializing it so it isn't used anyway. (Gone when we serializeToJson)
        builder.add(v ? new Tag.ElementEntry(location) : new Tag.OptionalElementEntry(location), "N/A");
    });
    this.subTags.forEach((k, v) -> {
        final ResourceLocation location = (ResourceLocation) (Object) k;
        builder.add(v ? new Tag.TagEntry(location) : new Tag.OptionalTagEntry(location), "N/A");
    });
    return builder.serializeToJson();
}
Also used : ResourceLocation(net.minecraft.resources.ResourceLocation) Tag(net.minecraft.tags.Tag)

Example 87 with ResourceLocation

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

the class DamageEventUtil method createAttackEnchantmentFunction.

public static List<DamageFunction> createAttackEnchantmentFunction(final net.minecraft.world.item.ItemStack heldItem, final MobType creatureAttribute, final float attackStrength) {
    if (heldItem.isEmpty()) {
        return Collections.emptyList();
    }
    final ListTag enchantmentCompounds = heldItem.getEnchantmentTags();
    if (enchantmentCompounds.isEmpty()) {
        return Collections.emptyList();
    }
    final Map<Enchantment, Collection<Integer>> enchantments = new HashMap<>();
    for (int i = 0; i < enchantmentCompounds.size(); ++i) {
        final String id = enchantmentCompounds.getCompound(i).getString("id");
        final int enchantmentLevel = enchantmentCompounds.getCompound(i).getInt("lvl");
        final Enchantment enchantment = Registry.ENCHANTMENT.get(new ResourceLocation(id));
        if (enchantment != null) {
            enchantments.computeIfAbsent(enchantment, k -> new ArrayList<>()).add(enchantmentLevel);
        }
    }
    if (enchantments.isEmpty()) {
        return Collections.emptyList();
    }
    final List<DamageFunction> damageModifierFunctions = new ArrayList<>();
    final ItemStackSnapshot snapshot = ItemStackUtil.snapshotOf(heldItem);
    for (final Map.Entry<Enchantment, Collection<Integer>> enchantment : enchantments.entrySet()) {
        final DamageModifier enchantmentModifier = DamageModifier.builder().type(DamageModifierTypes.WEAPON_ENCHANTMENT).cause(Cause.of(EventContext.empty(), snapshot, enchantment)).build();
        final DoubleUnaryOperator enchantmentFunction = (damage) -> {
            double totalDamage = 0;
            for (final int level : enchantment.getValue()) {
                totalDamage += (double) enchantment.getKey().getDamageBonus(level, creatureAttribute) * attackStrength;
            }
            return totalDamage;
        };
        damageModifierFunctions.add(new DamageFunction(enchantmentModifier, enchantmentFunction));
    }
    return damageModifierFunctions;
}
Also used : ResourceLocation(net.minecraft.resources.ResourceLocation) LivingEntity(net.minecraft.world.entity.LivingEntity) AABB(net.minecraft.world.phys.AABB) LivingEntityAccessor(org.spongepowered.common.accessor.world.entity.LivingEntityAccessor) ItemStackSnapshot(org.spongepowered.api.item.inventory.ItemStackSnapshot) MobType(net.minecraft.world.entity.MobType) EventContext(org.spongepowered.api.event.EventContext) Registry(net.minecraft.core.Registry) ChunkSource(net.minecraft.world.level.chunk.ChunkSource) CreatorTrackedBridge(org.spongepowered.common.bridge.CreatorTrackedBridge) ItemStack(org.spongepowered.api.item.inventory.ItemStack) Map(java.util.Map) PotionEffect(org.spongepowered.api.effect.potion.PotionEffect) Predicate(java.util.function.Predicate) Collection(java.util.Collection) Player(net.minecraft.world.entity.player.Player) List(java.util.List) BlockPos(net.minecraft.core.BlockPos) BlockDamageSource(org.spongepowered.api.event.cause.entity.damage.source.BlockDamageSource) Optional(java.util.Optional) ServerLocation(org.spongepowered.api.world.server.ServerLocation) Enchantment(net.minecraft.world.item.enchantment.Enchantment) LevelChunk(net.minecraft.world.level.chunk.LevelChunk) LevelChunkBridge(org.spongepowered.common.bridge.world.level.chunk.LevelChunkBridge) EventContextKeys(org.spongepowered.api.event.EventContextKeys) ServerWorld(org.spongepowered.api.world.server.ServerWorld) BlockState(net.minecraft.world.level.block.state.BlockState) HashMap(java.util.HashMap) DamageModifierTypes(org.spongepowered.api.event.cause.entity.damage.DamageModifierTypes) MobEffects(net.minecraft.world.effect.MobEffects) DoubleUnaryOperator(java.util.function.DoubleUnaryOperator) ArrayList(java.util.ArrayList) EntityDamageSource(net.minecraft.world.damagesource.EntityDamageSource) ItemStackUtil(org.spongepowered.common.item.util.ItemStackUtil) DamageSource(net.minecraft.world.damagesource.DamageSource) EnchantmentHelper(net.minecraft.world.item.enchantment.EnchantmentHelper) CombatRules(net.minecraft.world.damagesource.CombatRules) Server(org.spongepowered.api.Server) CauseStackManager(org.spongepowered.api.event.CauseStackManager) FallingBlockDamageSource(org.spongepowered.api.event.cause.entity.damage.source.FallingBlockDamageSource) Cause(org.spongepowered.api.event.Cause) Entity(net.minecraft.world.entity.Entity) DamageFunction(org.spongepowered.api.event.cause.entity.damage.DamageFunction) DamageModifier(org.spongepowered.api.event.cause.entity.damage.DamageModifier) EquipmentSlot(net.minecraft.world.entity.EquipmentSlot) Attributes(net.minecraft.world.entity.ai.attributes.Attributes) Mth(net.minecraft.util.Mth) Collections(java.util.Collections) ListTag(net.minecraft.nbt.ListTag) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ListTag(net.minecraft.nbt.ListTag) DamageModifier(org.spongepowered.api.event.cause.entity.damage.DamageModifier) ResourceLocation(net.minecraft.resources.ResourceLocation) DamageFunction(org.spongepowered.api.event.cause.entity.damage.DamageFunction) Collection(java.util.Collection) ItemStackSnapshot(org.spongepowered.api.item.inventory.ItemStackSnapshot) Enchantment(net.minecraft.world.item.enchantment.Enchantment) Map(java.util.Map) HashMap(java.util.HashMap) DoubleUnaryOperator(java.util.function.DoubleUnaryOperator)

Example 88 with ResourceLocation

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

the class RegistryMixin_API method valueKey.

@Override
public ResourceKey valueKey(final T value) {
    Objects.requireNonNull(value, "value");
    final ResourceLocation key = this.shadow$getKey(value);
    if (key == null) {
        throw new IllegalStateException(String.format("No key was found for '%s'!", value));
    }
    return (ResourceKey) (Object) key;
}
Also used : ResourceLocation(net.minecraft.resources.ResourceLocation) ResourceKey(org.spongepowered.api.ResourceKey)

Example 89 with ResourceLocation

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

the class LevelMixin_API method playSound.

// ArchetypeVolumeCreator
// Audience
@Override
public void playSound(final Sound sound, final double x, final double y, final double z) {
    // Check if the event is registered (ie has an integer ID)
    final ResourceLocation soundKey = SpongeAdventure.asVanilla(sound.name());
    final Optional<SoundEvent> event = Registry.SOUND_EVENT.getOptional(soundKey);
    final SoundSource soundCategory = SpongeAdventure.asVanilla(sound.source());
    if (event.isPresent()) {
        this.shadow$playSound(null, x, y, z, event.get(), soundCategory, sound.volume(), sound.pitch());
    } else {
        // Otherwise send it as a custom sound
        final float volume = sound.volume();
        final double radius = volume > 1.0f ? (16.0f * volume) : 16.0d;
        final ClientboundCustomSoundPacket packet = new ClientboundCustomSoundPacket(soundKey, soundCategory, new net.minecraft.world.phys.Vec3(x, y, z), volume, sound.pitch());
        this.shadow$getServer().getPlayerList().broadcast(null, x, y, z, radius, this.shadow$dimension(), packet);
    }
}
Also used : SoundSource(net.minecraft.sounds.SoundSource) SoundEvent(net.minecraft.sounds.SoundEvent) ResourceLocation(net.minecraft.resources.ResourceLocation) ClientboundCustomSoundPacket(net.minecraft.network.protocol.game.ClientboundCustomSoundPacket)

Example 90 with ResourceLocation

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

the class SpongeAdvancementBuilder method build0.

@Override
public Advancement build0() {
    final Tuple<Map<String, Criterion>, String[][]> result = SpongeCriterionUtil.toVanillaCriteriaData(this.criterion);
    final AdvancementRewards rewards = AdvancementRewards.EMPTY;
    final ResourceLocation resourceLocation = (ResourceLocation) (Object) key;
    final net.minecraft.advancements.DisplayInfo displayInfo = this.displayInfo == null ? null : new net.minecraft.advancements.DisplayInfo(ItemStackUtil.fromSnapshotToNative(this.displayInfo.icon()), SpongeAdventure.asVanilla(this.displayInfo.title()), SpongeAdventure.asVanilla(this.displayInfo.description()), this.backgroundPath, (FrameType) (Object) this.displayInfo.type(), this.displayInfo.doesShowToast(), this.displayInfo.doesAnnounceToChat(), this.displayInfo.isHidden());
    final net.minecraft.advancements.Advancement parent = (net.minecraft.advancements.Advancement) this.parent;
    final Advancement advancement = (Advancement) new net.minecraft.advancements.Advancement(resourceLocation, parent, displayInfo, rewards, result.first(), result.second());
    ((AdvancementBridge) advancement).bridge$setCriterion(this.criterion);
    return advancement;
}
Also used : AdvancementRewards(net.minecraft.advancements.AdvancementRewards) ResourceLocation(net.minecraft.resources.ResourceLocation) FrameType(net.minecraft.advancements.FrameType) AdvancementBridge(org.spongepowered.common.bridge.advancements.AdvancementBridge) Map(java.util.Map) Advancement(org.spongepowered.api.advancement.Advancement)

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