Add a SoundType subclass that uses suppliers (#7538)

This commit is contained in:
sciwhiz12 2020-12-29 05:09:03 +08:00 committed by GitHub
parent 74ca88b7be
commit 8ec4253068
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 205 additions and 2 deletions

View File

@ -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_;

View File

@ -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<SoundEvent>}s.
* <p>
* This class allows mod developers to safely create custom {@code SoundType}s for use in their e.g. {@link Block}.
* <p>
* 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.
* <p>
* 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.
* <p>
* 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<SoundEvent> breakSound;
private final Supplier<SoundEvent> stepSound;
private final Supplier<SoundEvent> placeSound;
private final Supplier<SoundEvent> hitSound;
private final Supplier<SoundEvent> fallSound;
public ForgeSoundType(float volumeIn, float pitchIn, Supplier<SoundEvent> breakSoundIn, Supplier<SoundEvent> stepSoundIn, Supplier<SoundEvent> placeSoundIn, Supplier<SoundEvent> hitSoundIn, Supplier<SoundEvent> 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();
}
}

View File

@ -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<Block> BLOCKS = DeferredRegister.create(ForgeRegistries.BLOCKS, MODID);
private static final DeferredRegister<Item> ITEMS = DeferredRegister.create(ForgeRegistries.ITEMS, MODID);
private static final DeferredRegister<SoundEvent> SOUND_EVENTS = DeferredRegister.create(ForgeRegistries.SOUND_EVENTS, MODID);
private static final RegistryObject<SoundEvent> 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<Block> TEST_STEP_BLOCK = BLOCKS.register("test_block",
() -> new Block(AbstractBlock.Properties.create(Material.WOOD).sound(TEST_SOUND_TYPE)));
private static final RegistryObject<Item> 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);
}
}

View File

@ -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"

View File

@ -0,0 +1,7 @@
{
"variants": {
"": {
"model": "custom_sound_type_test:block/test_block"
}
}
}

View File

@ -0,0 +1,6 @@
{
"parent": "minecraft:block/cube_all",
"textures": {
"all": "minecraft:block/cartography_table_side3"
}
}

View File

@ -0,0 +1,3 @@
{
"parent": "custom_sound_type_test:block/test_block"
}

View File

@ -0,0 +1,8 @@
{
"block.sound_type_test.step": {
"subtitle": "Test step",
"sounds": [
"minecraft:mob/horse/zombie/hit1"
]
}
}