forked from FabricMC/fabric-docs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added more page content and back end code
- Loading branch information
Showing
20 changed files
with
473 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file added
BIN
+412 KB
public/assets/develop/sounds/dynamic-sounds/custom-dynamic-sound-handling.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
29 changes: 29 additions & 0 deletions
29
reference/latest/src/client/java/com/example/docs/FabricDocsDynamicSound.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package com.example.docs; | ||
|
||
import net.minecraft.client.MinecraftClient; | ||
import net.minecraft.client.sound.PositionedSoundInstance; | ||
import net.minecraft.sound.SoundCategory; | ||
import net.minecraft.sound.SoundEvents; | ||
|
||
import net.fabricmc.api.ClientModInitializer; | ||
import com.example.docs.sound.CustomSounds; | ||
import com.example.docs.sound.DynamicSoundManager; | ||
import com.example.docs.sound.instance.CustomSoundInstance; | ||
|
||
public class FabricDocsDynamicSound implements ClientModInitializer { | ||
|
||
public static final DynamicSoundManager SOUND_MANAGER = DynamicSoundManager.getInstance(); | ||
|
||
@Override | ||
public void onInitializeClient() { | ||
// :::1 | ||
MinecraftClient client = MinecraftClient.getInstance(); | ||
client.getSoundManager().play(PositionedSoundInstance.master(SoundEvents.UI_BUTTON_CLICK, 1.0F)); | ||
// :::1 | ||
// :::2 | ||
client.getSoundManager().play( | ||
new CustomSoundInstance(client.player, CustomSounds.ENGINE_LOOP, SoundCategory.NEUTRAL) | ||
); | ||
// :::2 | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
reference/latest/src/client/java/com/example/docs/sound/AbstractDynamicSoundInstance.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package com.example.docs.sound; | ||
|
||
import net.minecraft.client.sound.MovingSoundInstance; | ||
import net.minecraft.client.sound.SoundInstance; | ||
import net.minecraft.sound.SoundCategory; | ||
import net.minecraft.sound.SoundEvent; | ||
|
||
// :::1 | ||
public abstract class AbstractDynamicSoundInstance extends MovingSoundInstance { | ||
private final DynamicSoundSource soundSource; | ||
|
||
protected AbstractDynamicSoundInstance(DynamicSoundSource soundSource, SoundEvent soundEvent, SoundCategory soundCategory) { | ||
super(soundEvent, soundCategory, SoundInstance.createRandom()); | ||
this.soundSource = soundSource; | ||
|
||
|
||
} | ||
// ... | ||
// :::1 | ||
|
||
@Override | ||
public void tick() { | ||
|
||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
reference/latest/src/client/java/com/example/docs/sound/DynamicSoundManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package com.example.docs.sound; | ||
|
||
import net.minecraft.client.MinecraftClient; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class DynamicSoundManager { | ||
|
||
private static DynamicSoundManager instance; | ||
private static final MinecraftClient client = MinecraftClient.getInstance(); | ||
|
||
private final List<? extends AbstractDynamicSoundInstance> activeSounds = new ArrayList<>(); | ||
|
||
private DynamicSoundManager() { | ||
// private constructor to make sure that the normal | ||
// instantiation of that object is not used externally | ||
} | ||
|
||
/** | ||
* This "Singleton Design Pattern" makes sure that, at runtime, | ||
* only one instance of this class can exist. | ||
* <p> | ||
* If this class has been used once already, it keeps its instance stored | ||
* in the static instance variable and return it.<br> | ||
* Otherwise, the instance variable is not initialized yet (null). | ||
* It will create a new instance, use it | ||
* and store it in the static variable for next uses. | ||
*/ | ||
public static DynamicSoundManager getInstance() { | ||
if (instance == null) return new DynamicSoundManager(); | ||
return instance; | ||
} | ||
|
||
public void play(AbstractDynamicSoundInstance sound) { | ||
client.getSoundManager().play(sound); | ||
} | ||
|
||
public void stop(AbstractDynamicSoundInstance sound) { | ||
|
||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
reference/latest/src/client/java/com/example/docs/sound/TransitionState.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package com.example.docs.sound; | ||
|
||
public enum TransitionState { | ||
STARTING, RUNNING, ENDING | ||
} |
46 changes: 46 additions & 0 deletions
46
reference/latest/src/client/java/com/example/docs/sound/instance/CustomSoundInstance.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package com.example.docs.sound.instance; | ||
|
||
import net.minecraft.client.sound.MovingSoundInstance; | ||
import net.minecraft.client.sound.SoundInstance; | ||
import net.minecraft.entity.LivingEntity; | ||
import net.minecraft.sound.SoundCategory; | ||
import net.minecraft.sound.SoundEvent; | ||
import net.minecraft.util.math.random.Random; | ||
|
||
import com.example.docs.sound.AbstractDynamicSoundInstance; | ||
// :::1 | ||
public class CustomSoundInstance extends MovingSoundInstance { | ||
Check failure on line 12 in reference/latest/src/client/java/com/example/docs/sound/instance/CustomSoundInstance.java GitHub Actions / mod
|
||
|
||
private final LivingEntity entity; | ||
|
||
// Here we pass over the sound source of the SoundInstance and store it in the instance. | ||
public CustomSoundInstance(LivingEntity entity, SoundEvent soundEvent, SoundCategory soundCategory) { | ||
super(soundEvent, soundCategory, SoundInstance.createRandom()); | ||
|
||
// here we can set up values when the sound is about to start. | ||
this.repeat = true; | ||
this.entity = entity; | ||
setPositionToEntity(); | ||
} | ||
|
||
@Override | ||
public void tick() { | ||
// stop sound instantly if sound source does not exist anymore | ||
if (this.entity == null || this.entity.isRemoved() || this.entity.isDead()) { | ||
this.setDone(); | ||
return; | ||
} | ||
|
||
// move sound position over to the new position for every tick | ||
setPositionToEntity(); | ||
} | ||
|
||
// small utility method to move the sound instance position | ||
// to the sound source's position | ||
private void setPositionToEntity() { | ||
this.x = entity.getX(); | ||
this.y = entity.getY(); | ||
this.z = entity.getZ(); | ||
} | ||
} | ||
// :::1 |
17 changes: 17 additions & 0 deletions
17
reference/latest/src/client/java/com/example/docs/sound/instance/EngineSoundInstance.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package com.example.docs.sound.instance; | ||
|
||
import com.example.docs.sound.AbstractDynamicSoundInstance; | ||
|
||
import com.example.docs.sound.DynamicSoundSource; | ||
|
||
import net.minecraft.sound.SoundCategory; | ||
import net.minecraft.sound.SoundEvent; | ||
import net.minecraft.util.math.random.Random; | ||
|
||
public class EngineSoundInstance extends AbstractDynamicSoundInstance { | ||
protected EngineSoundInstance(DynamicSoundSource source, SoundEvent soundEvent, SoundCategory soundCategory, Random random) { | ||
super(source, soundEvent, soundCategory); | ||
} | ||
Check failure on line 14 in reference/latest/src/client/java/com/example/docs/sound/instance/EngineSoundInstance.java GitHub Actions / mod
|
||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
reference/latest/src/main/java/com/example/docs/block/custom/EngineBlock.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package com.example.docs.block.custom; | ||
|
||
import com.mojang.serialization.MapCodec; | ||
import net.minecraft.block.BlockState; | ||
import net.minecraft.block.BlockWithEntity; | ||
import net.minecraft.block.entity.BlockEntity; | ||
import net.minecraft.block.entity.BlockEntityTicker; | ||
import net.minecraft.block.entity.BlockEntityType; | ||
import net.minecraft.entity.player.PlayerEntity; | ||
import net.minecraft.util.ActionResult; | ||
import net.minecraft.util.hit.BlockHitResult; | ||
import net.minecraft.util.math.BlockPos; | ||
import net.minecraft.world.World; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
import com.example.docs.block.entity.ModBlockEntities; | ||
import com.example.docs.block.entity.custom.EngineBlockEntity; | ||
|
||
public class EngineBlock extends BlockWithEntity { | ||
public static final MapCodec<EngineBlock> CODEC = createCodec(EngineBlock::new); | ||
|
||
public EngineBlock(Settings settings) { | ||
super(settings); | ||
} | ||
|
||
@Override | ||
protected MapCodec<? extends BlockWithEntity> getCodec() { | ||
return CODEC; | ||
} | ||
|
||
@Nullable | ||
@Override | ||
public BlockEntity createBlockEntity(BlockPos pos, BlockState state) { | ||
return new EngineBlockEntity(pos, state); | ||
} | ||
|
||
@Nullable | ||
@Override | ||
public <T extends BlockEntity> BlockEntityTicker<T> getTicker(World world, BlockState state, BlockEntityType<T> type) { | ||
return validateTicker(type, ModBlockEntities.ENGINE_BLOCK_ENTITY, EngineBlockEntity::tick); | ||
} | ||
|
||
@Override | ||
protected ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEntity player, BlockHitResult hit) { | ||
if (!(world.getBlockEntity(pos) instanceof EngineBlockEntity blockEntity)) | ||
return super.onUse(state, world, pos, player, hit); | ||
|
||
if (blockEntity.isTicking()) return super.onUse(state, world, pos, player, hit); | ||
blockEntity.setTick(0); // starts ticking | ||
return ActionResult.SUCCESS; | ||
} | ||
} |
Oops, something went wrong.