From 8ec4253068cd6d4310aea70d5953bbeacc3f5520 Mon Sep 17 00:00:00 2001 From: sciwhiz12 Date: Tue, 29 Dec 2020 05:09:03 +0800 Subject: [PATCH] Add a SoundType subclass that uses suppliers (#7538) --- .../net/minecraft/block/SoundType.java.patch | 10 ++ .../common/util/ForgeSoundType.java | 102 ++++++++++++++++++ .../debug/CustomSoundTypeTest.java | 65 +++++++++++ src/test/resources/META-INF/mods.toml | 6 +- .../blockstates/test_block.json | 7 ++ .../models/block/test_block.json | 6 ++ .../models/item/test_block.json | 3 + .../assets/custom_sound_type_test/sounds.json | 8 ++ 8 files changed, 205 insertions(+), 2 deletions(-) create mode 100644 patches/minecraft/net/minecraft/block/SoundType.java.patch create mode 100644 src/main/java/net/minecraftforge/common/util/ForgeSoundType.java create mode 100644 src/test/java/net/minecraftforge/debug/CustomSoundTypeTest.java create mode 100644 src/test/resources/assets/custom_sound_type_test/blockstates/test_block.json create mode 100644 src/test/resources/assets/custom_sound_type_test/models/block/test_block.json create mode 100644 src/test/resources/assets/custom_sound_type_test/models/item/test_block.json create mode 100644 src/test/resources/assets/custom_sound_type_test/sounds.json diff --git a/patches/minecraft/net/minecraft/block/SoundType.java.patch b/patches/minecraft/net/minecraft/block/SoundType.java.patch new file mode 100644 index 0000000000..67656aa09f --- /dev/null +++ b/patches/minecraft/net/minecraft/block/SoundType.java.patch @@ -0,0 +1,10 @@ +--- a/net/minecraft/block/SoundType.java ++++ b/net/minecraft/block/SoundType.java +@@ -59,6 +59,7 @@ + private final SoundEvent field_185865_r; + private final SoundEvent field_185866_s; + ++ @Deprecated // Forge: Use {@link net.minecraftforge.common.util.ForgeSoundType} instead for suppliers + public SoundType(float p_i46679_1_, float p_i46679_2_, SoundEvent p_i46679_3_, SoundEvent p_i46679_4_, SoundEvent p_i46679_5_, SoundEvent p_i46679_6_, SoundEvent p_i46679_7_) { + this.field_185860_m = p_i46679_1_; + this.field_185861_n = p_i46679_2_; diff --git a/src/main/java/net/minecraftforge/common/util/ForgeSoundType.java b/src/main/java/net/minecraftforge/common/util/ForgeSoundType.java new file mode 100644 index 0000000000..523ac36406 --- /dev/null +++ b/src/main/java/net/minecraftforge/common/util/ForgeSoundType.java @@ -0,0 +1,102 @@ +/* + * Minecraft Forge + * Copyright (c) 2016-2020. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation version 2.1 + * of the License. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +package net.minecraftforge.common.util; + +import net.minecraft.block.Block; +import net.minecraft.block.SoundType; +import net.minecraft.util.SoundEvent; +import net.minecraftforge.fml.RegistryObject; +import net.minecraftforge.registries.DeferredRegister; + +import javax.annotation.Nonnull; +import java.util.function.Supplier; + +/** + * A subclass of {@link SoundType} that uses {@link Supplier}s. + *

+ * This class allows mod developers to safely create custom {@code SoundType}s for use in their e.g. {@link Block}. + *

+ * The problem with using {@code SoundType} directly is it requires {@link SoundEvent} instances directly, because + * {@code SoundType}s are required to be present during {@link Block} creation and registration. However, + * {@code SoundEvent} must also be registered. + *

+ * A possible solution of initializing {@code SoundEvent}s first would require static initialization of the + * {@code SoundEvent} instances and later registration, which goes against the contract of the registry system and + * prevents the use of {@link DeferredRegister} and {@link RegistryObject}s. + *

+ * This class offers an alternative and preferable solution, by allowing mods to create {@link SoundType}s using + * {@link Supplier}s of {@link SoundEvent}s instead, which do not require static initialization of {@code SoundEvent}s + * and allow the direct use of {@code RegistryObject}s. + * + * @see SoundType + */ +public class ForgeSoundType extends SoundType +{ + private final Supplier breakSound; + private final Supplier stepSound; + private final Supplier placeSound; + private final Supplier hitSound; + private final Supplier fallSound; + + public ForgeSoundType(float volumeIn, float pitchIn, Supplier breakSoundIn, Supplier stepSoundIn, Supplier placeSoundIn, Supplier hitSoundIn, Supplier fallSoundIn) + { + super(volumeIn, pitchIn, (SoundEvent) null, (SoundEvent) null, (SoundEvent) null, (SoundEvent) null, (SoundEvent) null); + this.breakSound = breakSoundIn; + this.stepSound = stepSoundIn; + this.placeSound = placeSoundIn; + this.hitSound = hitSoundIn; + this.fallSound = fallSoundIn; + } + + @Nonnull + @Override + public SoundEvent getBreakSound() + { + return breakSound.get(); + } + + @Nonnull + @Override + public SoundEvent getStepSound() + { + return stepSound.get(); + } + + @Nonnull + @Override + public SoundEvent getPlaceSound() + { + return placeSound.get(); + } + + @Nonnull + @Override + public SoundEvent getHitSound() + { + return hitSound.get(); + } + + @Nonnull + @Override + public SoundEvent getFallSound() + { + return fallSound.get(); + } +} diff --git a/src/test/java/net/minecraftforge/debug/CustomSoundTypeTest.java b/src/test/java/net/minecraftforge/debug/CustomSoundTypeTest.java new file mode 100644 index 0000000000..ef8349b77a --- /dev/null +++ b/src/test/java/net/minecraftforge/debug/CustomSoundTypeTest.java @@ -0,0 +1,65 @@ +/* + * Minecraft Forge + * Copyright (c) 2016-2020. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation version 2.1 + * of the License. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +package net.minecraftforge.debug; + +import net.minecraft.block.AbstractBlock; +import net.minecraft.block.Block; +import net.minecraft.block.SoundType; +import net.minecraft.block.material.Material; +import net.minecraft.item.BlockItem; +import net.minecraft.item.Item; +import net.minecraft.item.ItemGroup; +import net.minecraft.util.ResourceLocation; +import net.minecraft.util.SoundEvent; +import net.minecraftforge.common.util.ForgeSoundType; +import net.minecraftforge.eventbus.api.IEventBus; +import net.minecraftforge.fml.RegistryObject; +import net.minecraftforge.fml.common.Mod; +import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext; +import net.minecraftforge.registries.DeferredRegister; +import net.minecraftforge.registries.ForgeRegistries; + +@Mod(CustomSoundTypeTest.MODID) +public class CustomSoundTypeTest +{ + static final String MODID = "custom_sound_type_test"; + + private static final DeferredRegister BLOCKS = DeferredRegister.create(ForgeRegistries.BLOCKS, MODID); + private static final DeferredRegister ITEMS = DeferredRegister.create(ForgeRegistries.ITEMS, MODID); + private static final DeferredRegister SOUND_EVENTS = DeferredRegister.create(ForgeRegistries.SOUND_EVENTS, MODID); + + private static final RegistryObject TEST_STEP_EVENT = SOUND_EVENTS.register("test_step", + () -> new SoundEvent(new ResourceLocation(MODID, "block.sound_type_test.step"))); + private static final SoundType TEST_SOUND_TYPE = new ForgeSoundType(1.0F, 1.0F, TEST_STEP_EVENT, TEST_STEP_EVENT, TEST_STEP_EVENT, TEST_STEP_EVENT, TEST_STEP_EVENT); + + private static final RegistryObject TEST_STEP_BLOCK = BLOCKS.register("test_block", + () -> new Block(AbstractBlock.Properties.create(Material.WOOD).sound(TEST_SOUND_TYPE))); + + private static final RegistryObject TEST_STEP_BLOCK_ITEM = ITEMS.register("test_block", + () -> new BlockItem(TEST_STEP_BLOCK.get(), new Item.Properties().group(ItemGroup.MISC))); + + public CustomSoundTypeTest() + { + final IEventBus modEventBus = FMLJavaModLoadingContext.get().getModEventBus(); + BLOCKS.register(modEventBus); + ITEMS.register(modEventBus); + SOUND_EVENTS.register(modEventBus); + } +} diff --git a/src/test/resources/META-INF/mods.toml b/src/test/resources/META-INF/mods.toml index 5c23c0cfba..a7de59836a 100644 --- a/src/test/resources/META-INF/mods.toml +++ b/src/test/resources/META-INF/mods.toml @@ -98,7 +98,9 @@ license="LGPL v2.1" modId="forge_codecs_test" [[mods]] modId="render_local_player_test" -[[mods]] - modId="item_modifier_test" [[mods]] modId="forge_world_type_test" +[[mods]] + modId="custom_sound_type_test" +[[mods]] + modId="item_modifier_test" diff --git a/src/test/resources/assets/custom_sound_type_test/blockstates/test_block.json b/src/test/resources/assets/custom_sound_type_test/blockstates/test_block.json new file mode 100644 index 0000000000..1dc31f421b --- /dev/null +++ b/src/test/resources/assets/custom_sound_type_test/blockstates/test_block.json @@ -0,0 +1,7 @@ +{ + "variants": { + "": { + "model": "custom_sound_type_test:block/test_block" + } + } +} \ No newline at end of file diff --git a/src/test/resources/assets/custom_sound_type_test/models/block/test_block.json b/src/test/resources/assets/custom_sound_type_test/models/block/test_block.json new file mode 100644 index 0000000000..2437ff61de --- /dev/null +++ b/src/test/resources/assets/custom_sound_type_test/models/block/test_block.json @@ -0,0 +1,6 @@ +{ + "parent": "minecraft:block/cube_all", + "textures": { + "all": "minecraft:block/cartography_table_side3" + } +} \ No newline at end of file diff --git a/src/test/resources/assets/custom_sound_type_test/models/item/test_block.json b/src/test/resources/assets/custom_sound_type_test/models/item/test_block.json new file mode 100644 index 0000000000..686698a9b8 --- /dev/null +++ b/src/test/resources/assets/custom_sound_type_test/models/item/test_block.json @@ -0,0 +1,3 @@ +{ + "parent": "custom_sound_type_test:block/test_block" +} \ No newline at end of file diff --git a/src/test/resources/assets/custom_sound_type_test/sounds.json b/src/test/resources/assets/custom_sound_type_test/sounds.json new file mode 100644 index 0000000000..678bf621fd --- /dev/null +++ b/src/test/resources/assets/custom_sound_type_test/sounds.json @@ -0,0 +1,8 @@ +{ + "block.sound_type_test.step": { + "subtitle": "Test step", + "sounds": [ + "minecraft:mob/horse/zombie/hit1" + ] + } +} \ No newline at end of file