From 30eab4ee6af063e46dea488c193a660478ec92d3 Mon Sep 17 00:00:00 2001 From: Sollace Date: Mon, 30 Sep 2024 19:28:45 +0100 Subject: [PATCH] Move poisoned joke sound to a tag --- .../com/minelittlepony/unicopia/UTags.java | 9 +++ .../com/minelittlepony/unicopia/Unicopia.java | 2 - .../providers/tag/USoundEventTagProvider.java | 53 +++++++++++++++++ .../enchantment/PoisonedJokeEnchantment.java | 58 +++++-------------- .../unicopia/util/RegistryUtils.java | 10 ++-- .../data/unicopia/poisoned_joke_sounds.json | 29 ---------- 6 files changed, 81 insertions(+), 80 deletions(-) create mode 100644 src/main/java/com/minelittlepony/unicopia/datagen/providers/tag/USoundEventTagProvider.java delete mode 100644 src/main/resources/data/unicopia/poisoned_joke_sounds.json diff --git a/src/main/java/com/minelittlepony/unicopia/UTags.java b/src/main/java/com/minelittlepony/unicopia/UTags.java index cf06e053..f2c0080b 100644 --- a/src/main/java/com/minelittlepony/unicopia/UTags.java +++ b/src/main/java/com/minelittlepony/unicopia/UTags.java @@ -7,6 +7,7 @@ import net.minecraft.entity.effect.StatusEffect; import net.minecraft.item.Item; import net.minecraft.registry.*; import net.minecraft.registry.tag.TagKey; +import net.minecraft.sound.SoundEvent; import net.minecraft.util.Identifier; import net.minecraft.world.dimension.DimensionType; @@ -148,4 +149,12 @@ public interface UTags { return TagKey.of(RegistryKeys.STATUS_EFFECT, Unicopia.id(name)); } } + + interface Sounds { + TagKey POISON_JOKE_EVENTS = sound("poison_joke_events"); + + private static TagKey sound(String name) { + return TagKey.of(RegistryKeys.SOUND_EVENT, Unicopia.id(name)); + } + } } diff --git a/src/main/java/com/minelittlepony/unicopia/Unicopia.java b/src/main/java/com/minelittlepony/unicopia/Unicopia.java index e26aa90c..7b4179b4 100644 --- a/src/main/java/com/minelittlepony/unicopia/Unicopia.java +++ b/src/main/java/com/minelittlepony/unicopia/Unicopia.java @@ -27,7 +27,6 @@ import com.minelittlepony.unicopia.entity.effect.SeaponyGraceStatusEffect; import com.minelittlepony.unicopia.entity.effect.UPotions; import com.minelittlepony.unicopia.entity.mob.UEntities; import com.minelittlepony.unicopia.item.UItems; -import com.minelittlepony.unicopia.item.enchantment.UEnchantments; import com.minelittlepony.unicopia.network.Channel; import com.minelittlepony.unicopia.particle.UParticles; import com.minelittlepony.unicopia.server.world.BlockDestructionManager; @@ -104,7 +103,6 @@ public class Unicopia implements ModInitializer { private void registerServerDataReloaders(ResourceManagerHelper registry) { registry.registerReloadListener(TreeTypeLoader.INSTANCE); - registry.registerReloadListener(UEnchantments.POISONED_JOKE); registry.registerReloadListener(new TraitLoader()); registry.registerReloadListener(StateMapLoader.INSTANCE); registry.registerReloadListener(SpellbookChapterLoader.INSTANCE); diff --git a/src/main/java/com/minelittlepony/unicopia/datagen/providers/tag/USoundEventTagProvider.java b/src/main/java/com/minelittlepony/unicopia/datagen/providers/tag/USoundEventTagProvider.java new file mode 100644 index 00000000..03e35e21 --- /dev/null +++ b/src/main/java/com/minelittlepony/unicopia/datagen/providers/tag/USoundEventTagProvider.java @@ -0,0 +1,53 @@ +package com.minelittlepony.unicopia.datagen.providers.tag; + +import java.util.concurrent.CompletableFuture; + +import com.minelittlepony.unicopia.UTags; +import net.fabricmc.fabric.api.datagen.v1.FabricDataOutput; +import net.fabricmc.fabric.api.datagen.v1.provider.FabricTagProvider; +import net.minecraft.registry.RegistryKeys; +import net.minecraft.registry.RegistryWrapper.WrapperLookup; +import net.minecraft.sound.SoundEvent; +import net.minecraft.sound.SoundEvents; + +public class USoundEventTagProvider extends FabricTagProvider { + public USoundEventTagProvider(FabricDataOutput output, CompletableFuture registriesFuture) { + super(output, RegistryKeys.SOUND_EVENT, registriesFuture); + } + + @Override + protected void configure(WrapperLookup lookup) { + getOrCreateTagBuilder(UTags.Sounds.POISON_JOKE_EVENTS).add( + SoundEvents.AMBIENT_CAVE.registryKey(), + SoundEvents.MUSIC_MENU.registryKey(), + SoundEvents.ENTITY_GENERIC_EXPLODE.registryKey(), + SoundEvents.AMBIENT_NETHER_WASTES_LOOP.registryKey(), + SoundEvents.AMBIENT_NETHER_WASTES_MOOD.registryKey(), + SoundEvents.AMBIENT_CRIMSON_FOREST_MOOD.registryKey(), + SoundEvents.AMBIENT_BASALT_DELTAS_MOOD.registryKey(), + SoundEvents.AMBIENT_BASALT_DELTAS_LOOP.registryKey() + ).add( + SoundEvents.ENTITY_CREEPER_PRIMED, + SoundEvents.BLOCK_STONE_BUTTON_CLICK_ON, + SoundEvents.BLOCK_STONE_BUTTON_CLICK_OFF, + SoundEvents.BLOCK_WOODEN_PRESSURE_PLATE_CLICK_ON, + SoundEvents.BLOCK_WOODEN_PRESSURE_PLATE_CLICK_OFF, + SoundEvents.BLOCK_WOODEN_DOOR_CLOSE, + SoundEvents.BLOCK_STONE_BREAK, + SoundEvents.ITEM_SHIELD_BREAK, + SoundEvents.ENTITY_BLAZE_AMBIENT, + SoundEvents.ENTITY_ZOMBIE_AMBIENT, + SoundEvents.ENTITY_DROWNED_AMBIENT, + SoundEvents.ENTITY_ENDERMITE_AMBIENT, + SoundEvents.ENTITY_SKELETON_AMBIENT, + SoundEvents.ENTITY_SKELETON_HORSE_AMBIENT, + SoundEvents.ENTITY_ZOGLIN_AMBIENT, + SoundEvents.ENTITY_ZOMBIFIED_PIGLIN_AMBIENT, + SoundEvents.ENTITY_ZOMBIE_BREAK_WOODEN_DOOR, + SoundEvents.ENTITY_TNT_PRIMED, + SoundEvents.AMBIENT_UNDERWATER_LOOP_ADDITIONS_ULTRA_RARE, + SoundEvents.AMBIENT_UNDERWATER_LOOP_ADDITIONS_RARE + ); + + } +} diff --git a/src/main/java/com/minelittlepony/unicopia/item/enchantment/PoisonedJokeEnchantment.java b/src/main/java/com/minelittlepony/unicopia/item/enchantment/PoisonedJokeEnchantment.java index 93677d83..710c2bbd 100644 --- a/src/main/java/com/minelittlepony/unicopia/item/enchantment/PoisonedJokeEnchantment.java +++ b/src/main/java/com/minelittlepony/unicopia/item/enchantment/PoisonedJokeEnchantment.java @@ -10,8 +10,10 @@ import java.util.stream.Stream; import org.apache.commons.lang3.reflect.TypeUtils; +import com.minelittlepony.unicopia.UTags; import com.minelittlepony.unicopia.Unicopia; import com.minelittlepony.unicopia.entity.Living; +import com.minelittlepony.unicopia.util.RegistryUtils; import com.minelittlepony.unicopia.util.Resources; import net.fabricmc.fabric.api.resource.IdentifiableResourceReloadListener; @@ -23,11 +25,7 @@ import net.minecraft.util.math.random.Random; import net.minecraft.util.profiler.Profiler; import net.minecraft.registry.Registries; -public class PoisonedJokeEnchantment extends SimpleEnchantment implements IdentifiableResourceReloadListener { - private static final Identifier ID = Unicopia.id("data/poisoned_joke_sounds"); - private static final Identifier FILE = Unicopia.id("poisoned_joke_sounds.json"); - private static final Type TYPE = TypeUtils.parameterize(List.class, Identifier.class); - private List sounds = new ArrayList<>(); +public class PoisonedJokeEnchantment extends SimpleEnchantment { protected PoisonedJokeEnchantment(Options options) { super(options); @@ -35,7 +33,7 @@ public class PoisonedJokeEnchantment extends SimpleEnchantment implements Identi @Override public void onUserTick(Living user, int level) { - if (sounds.isEmpty() || user.asWorld().isClient) { + if (user.asWorld().isClient) { return; } @@ -47,45 +45,15 @@ public class PoisonedJokeEnchantment extends SimpleEnchantment implements Identi if (rng.nextInt(Math.max(1, (light * 9) + (int)data.level)) == 0) { data.level = rng.nextInt(5000); - user.asWorld().playSoundFromEntity( - null, - user.asEntity(), - sounds.get(rng.nextInt(sounds.size())), SoundCategory.HOSTILE, - 0.5F + rng.nextFloat() * 0.5F, - 0.5F + rng.nextFloat() * 0.5F - ); + RegistryUtils.pickRandom(user.asWorld(), UTags.Sounds.POISON_JOKE_EVENTS).ifPresent(event -> { + user.asWorld().playSoundFromEntity( + null, + user.asEntity(), + event, SoundCategory.HOSTILE, + 0.5F + rng.nextFloat() * 0.5F, + 0.5F + rng.nextFloat() * 0.5F + ); + }); } } - - @Override - public Identifier getFabricId() { - return ID; - } - - @Override - public CompletableFuture reload(Synchronizer sync, ResourceManager manager, - Profiler serverProfiler, Profiler clientProfiler, - Executor serverExecutor, Executor clientExecutor) { - - return sync.whenPrepared(null).thenRunAsync(() -> { - clientProfiler.startTick(); - clientProfiler.push("Loading poisoned joke sound options"); - - sounds = Resources.getResources(manager, FILE) - .flatMap(r -> Resources.loadFile(r, TYPE, "Failed to load sounds file at ")) - .distinct() - .flatMap(this::findSound) - .collect(Collectors.toList()); - - clientProfiler.pop(); - clientProfiler.endTick(); - }, clientExecutor); - } - - private Stream findSound(Identifier id) { - return Registries.SOUND_EVENT.getOrEmpty(id).map(Stream::of).orElseGet(() -> { - Unicopia.LOGGER.warn("Could not find sound with id {}", id); - return Stream.empty(); - }); - } } diff --git a/src/main/java/com/minelittlepony/unicopia/util/RegistryUtils.java b/src/main/java/com/minelittlepony/unicopia/util/RegistryUtils.java index 4a5b06a2..4c7b003b 100644 --- a/src/main/java/com/minelittlepony/unicopia/util/RegistryUtils.java +++ b/src/main/java/com/minelittlepony/unicopia/util/RegistryUtils.java @@ -4,6 +4,7 @@ import java.util.Optional; import java.util.function.Predicate; import java.util.stream.Stream; +import com.google.common.base.Predicates; import com.mojang.serialization.Lifecycle; import net.fabricmc.fabric.api.event.registry.FabricRegistryBuilder; @@ -34,10 +35,11 @@ public interface RegistryUtils { } static Optional pickRandom(World world, TagKey key) { - return world.getRegistryManager().getOptional(key.registry()) - .flatMap(registry -> registry.getEntryList(key)) - .flatMap(entries -> entries.getRandom(world.random)) - .map(RegistryEntry::value); + return pickRandomEntry(world, key).map(RegistryEntry::value); + } + + static Optional> pickRandomEntry(World world, TagKey key) { + return pickRandomEntry(world, key, Predicates.alwaysTrue()); } static Optional pickRandom(World world, TagKey key, Predicate filter) { diff --git a/src/main/resources/data/unicopia/poisoned_joke_sounds.json b/src/main/resources/data/unicopia/poisoned_joke_sounds.json deleted file mode 100644 index 139f0fa0..00000000 --- a/src/main/resources/data/unicopia/poisoned_joke_sounds.json +++ /dev/null @@ -1,29 +0,0 @@ -[ - "minecraft:ambient.cave", - "minecraft:entity.creeper.primed", - "minecraft:block.stone_button.click_on", - "minecraft:block.stone_button.click_off", - "minecraft:block.wooden_pressure_plate.click_on", - "minecraft:block.wooden_door.close", - "minecraft:block.stone.break", - "minecraft:item.shield.break", - "minecraft:entity.blaze.ambient", - "minecraft:entity.zombie.ambient", - "minecraft:entity.drowned.ambient", - "minecraft:entity.endermite.ambient", - "minecraft:entity.skeleton.ambient", - "minecraft:entity.skeleton_horse.ambient", - "minecraft:entity.zoglin.ambient", - "minecraft:entity.zombified_piglin.ambient", - "minecraft:entity.zombie.break_wooden_door", - "minecraft:music.menu", - "minecraft:entity.tnt.primed", - "minecraft:entity.generic.explode", - "minecraft:ambient.nether_wastes.loop", - "minecraft:ambient.nether_wastes.mood", - "minecraft:ambient.crimson_forest.mood", - "minecraft:ambient.basalt_deltas.mood", - "minecraft:ambient.basalt_deltas.loop", - "minecraft:ambient.underwater.loop.additions.ultra_rare", - "minecraft:ambient.underwater.loop.additions.rare" -] \ No newline at end of file