Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Featured Replies

Posted

I want to add a sound to a block. How can I do it? I'd like to add a machine to minecraft and that should repeat the sound of itself.

I can't play the sound.

 

TestOre.java

package naburus.mod.bc_sounds;
import java.util.Random;
import net.minecraft.block.Block;
import net.minecraft.block.BlockOre;
import net.minecraft.block.StepSound;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.block.material.Material;
import net.minecraftforge.client.event.sound.PlaySoundEffectEvent;
public class TestOre extends Block{
public TestOre(int id, Material material) {
     super(id, material);        
         setHardness(2.0F);
         setStepSound(Block.soundLadderFootstep);
         setUnlocalizedName("testOre");
         setCreativeTab(CreativeTabs.tabBlock);        
         // How can I play my sound? <------------------------------------------------
}

}

 

Test.java

package naburus.mod.bc_sounds;
import net.minecraftforge.client.event.sound.SoundLoadEvent;
import net.minecraftforge.event.ForgeSubscribe;
public class Test {
@ForgeSubscribe
public void onSound(SoundLoadEvent event) {
     event.manager.addSound("bc_sounds:death.ogg");
}

}

 

Main.java

package naburus.mod.bc_sounds;
import net.minecraft.block.Block;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.block.material.Material;
import net.minecraftforge.common.ForgeHooks;
import net.minecraftforge.common.MinecraftForge;
import cpw.mods.fml.common.Mod;
import cpw.mods.fml.common.Mod.EventHandler;
import cpw.mods.fml.common.Mod.Init;
import cpw.mods.fml.common.Mod.Instance;
import cpw.mods.fml.common.Mod.PostInit;
import cpw.mods.fml.common.Mod.PreInit;
import cpw.mods.fml.common.SidedProxy;
import cpw.mods.fml.common.event.FMLInitializationEvent;
import cpw.mods.fml.common.event.FMLPostInitializationEvent;
import cpw.mods.fml.common.event.FMLPreInitializationEvent;
import cpw.mods.fml.common.network.NetworkMod;
import cpw.mods.fml.common.registry.GameRegistry;
import cpw.mods.fml.common.registry.LanguageRegistry;
import naburus.mod.bc_sounds.TestOre;
@Mod(modid = "bc_sounds", name = "Build Craft Sounds", version = "1.0")
@NetworkMod(clientSideRequired = true, serverSideRequired = false)
public class Main {

// --> DEL
public static Block TestOre_;
// <-- DEL

@Instance(value = "bc_sounds")
public static Main instance;

@SidedProxy(clientSide="naburus.mod.bc_sounds.client.ClientProxy", serverSide="naburus.mod.bc_sounds.CommonProxy")
public static CommonProxy proxy;

@EventHandler
public void preInit(FMLPreInitializationEvent event) {
     TestOre_ = new TestOre(501, Material.rock);
}

@EventHandler
public void load(FMLInitializationEvent event) {
     // --> DEL
     LanguageRegistry.addName(TestOre_, "Test Ore");
     MinecraftForge.setBlockHarvestLevel(TestOre_, "pickaxe", 3);
     GameRegistry.registerBlock(TestOre_, "testOre");
     // <-- DEL    
    
     proxy.registerRenderers();
}

@EventHandler
public void postInit(FMLPostInitializationEvent event) {
}
}

Do you want it to play the sound when it gets a redstone signal? When it is placed? All the time never stopping, or what? Specifics be needed.

  • Author

I'd like to do, if I put down a block, the sound does repeat itself constantly.

Hmm... Try using the "onBlockPlaced" method. Then in that do a for statement for as long as x > 0 play a sound, and then constantly add to x. Not the best method ever. But it is what I can think of off the top of my head without having tried to do this before. To actually play the sound, use this statement inside the for statement: par2World.playSound

 

 

In Block#updateTick(World,...)

use World#playSound(String,...)

then World#scheduleBlockUpdate(...)

try to set the delay to something close to the sound length in ticks.

  • Author

Could you explain to me why this code is not working properly?

 

import java.util.Random;

import net.minecraft.block.Block;
import net.minecraft.block.BlockOre;
import net.minecraft.block.StepSound;
import net.minecraft.client.audio.SoundManager;
import net.minecraft.client.audio.SoundPoolEntry;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.world.World;
import net.minecraft.block.material.Material;
import net.minecraftforge.client.event.sound.PlaySoundEffectEvent;

public class TestOre extends Block{

    public TestOre(int id, Material material) {
    	super(id, material);           
        setHardness(2.0F);
        setStepSound(Block.soundLadderFootstep);
        setUnlocalizedName("testOre");
        setCreativeTab(CreativeTabs.tabBlock);                 
   }  
    
    public void updateTick(World world, int i, int j, int k, Random random) {
    	world.playSound(i, j, k, "liquid.swim", 0.3f, 0.6f, false);
    	world.scheduleBlockUpdate(i, j, k, this.blockID, this.tickRate(world));
    }
}

  • Author

So, the main problem is the updateTick is not running down. setStepSound(Block.soundGlassFootstep); if I step above the block it should play the sound of the glass.

public void updateTick(World world, int x, int y, int z, Random random) {
setStepSound(Block.soundGlassFootstep);
world.playSound(x, y, z, "bc_sounds:death.ogg", 0.3f, 0.6f, false);
world.scheduleBlockUpdate(x, y, z, this.blockID, this.tickRate(world));
}

So, in my understanding the updateTick at every tick runs down what it contain? Or am I wrong?

You don't need a TileEntity. But the updateTick needs a first call so then the ticks will be scheduled.

For example with Block#onBlockPlacedBy(...)

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

Important Information

By using this site, you agree to our Terms of Use.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.