Skip to content

Commit

Permalink
Fixed the bell and display case sounds (#7)
Browse files Browse the repository at this point in the history
I added a new packet type for sending sounds and used it for both the Display case and the Bell.
  • Loading branch information
Nuchaz committed Feb 12, 2022
1 parent f15b8f0 commit 8029ad1
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 9 deletions.
9 changes: 6 additions & 3 deletions src/main/java/jds/bibliocraft/CommonProxy.java
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,8 @@ public class CommonProxy
public static final ResourceLocation PAINTINGSHEET = new ResourceLocation("textures/painting/paintings_kristoffer_zetterstrand.png");


public static final SoundEvent SOUND_DING = new SoundEvent(new ResourceLocation("bibliocraft:ding"));
public static final String SOUND_BELL_DING_TEXT = "bibliocraft:ding";
public static final SoundEvent SOUND_DING = new SoundEvent(new ResourceLocation(SOUND_BELL_DING_TEXT));

public static final SoundEvent SOUND_TYPEWRITER_ADDPAPER = new SoundEvent(new ResourceLocation("bibliocraft:addpaper"));
public static final SoundEvent SOUND_TYPEWRITER_TYPEING = new SoundEvent(new ResourceLocation("bibliocraft:typing"));
Expand All @@ -193,8 +194,10 @@ public class CommonProxy
public static final SoundEvent SOUND_ITEM_HANDDRILL = new SoundEvent(new ResourceLocation("bibliocraft:drill"));
public static final SoundEvent SOUND_ITEM_SCREWGUN = new SoundEvent(new ResourceLocation("bibliocraft:screw"));

public static final SoundEvent SOUND_CASE_OPEN = new SoundEvent(new ResourceLocation("bibliocraft:copen"));
public static final SoundEvent SOUND_CASE_CLOSE = new SoundEvent(new ResourceLocation("bibliocraft:cclose"));
public static final String SOUND_CASE_OPEN_TEXT = "bibliocraft:copen";
public static final String SOUND_CASE_CLOSE_TEXT = "bibliocraft:cclose";
public static final SoundEvent SOUND_CASE_OPEN = new SoundEvent(new ResourceLocation(SOUND_CASE_OPEN_TEXT));
public static final SoundEvent SOUND_CASE_CLOSE = new SoundEvent(new ResourceLocation(SOUND_CASE_CLOSE_TEXT));

public static final SoundEvent SOUND_TAPE_OPEN = new SoundEvent(new ResourceLocation("bibliocraft:tapeopen"));
public static final SoundEvent SOUND_TAPE_CLOSE = new SoundEvent(new ResourceLocation("bibliocraft:tapeclose"));
Expand Down
12 changes: 9 additions & 3 deletions src/main/java/jds/bibliocraft/blocks/BlockBell.java
Original file line number Diff line number Diff line change
@@ -1,23 +1,29 @@
package jds.bibliocraft.blocks;

import jds.bibliocraft.CommonProxy;
import jds.bibliocraft.network.BiblioNetworking;
import jds.bibliocraft.network.packet.client.BiblioSoundPlayer;
import jds.bibliocraft.network.packet.client.BiblioStockLog;
import jds.bibliocraft.tileentities.TileEntityBell;
import net.minecraft.block.SoundType;
import net.minecraft.block.material.Material;
import net.minecraft.block.state.IBlockState;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.SoundCategory;
import net.minecraft.util.math.AxisAlignedBB;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.IBlockAccess;
import net.minecraft.world.World;
import net.minecraftforge.fml.common.network.NetworkRegistry.TargetPoint;

public class BlockBell extends BiblioSimpleBlock
{
public static final BlockBell instance = new BlockBell();
public static final String name = "Bell";
public static final float range = 32.0F;

public BlockBell()
{
Expand All @@ -33,11 +39,11 @@ public AxisAlignedBB getBoundingBox(IBlockState state, IBlockAccess blockAccess,
@Override
public boolean onBlockActivatedCustomCommands(World world, BlockPos pos, IBlockState state, EntityPlayer player, EnumFacing side, float hitX, float hitY, float hitZ)
{
System.out.println("remote? " + world.isRemote);
// this doesn't run client side at all anymore. hmmm.
if (!world.isRemote)
{
world.playSound(null, pos.getX(), pos.getY(), pos.getZ(), CommonProxy.SOUND_DING, SoundCategory.BLOCKS, 1.0F, 1.0F);
//world.playSound(null, pos.getX(), pos.getY(), pos.getZ(), CommonProxy.SOUND_DING, SoundCategory.BLOCKS, 1.0F, 1.0F);
TargetPoint target = new TargetPoint(world.provider.getDimension(), pos.getX(), pos.getY(), pos.getZ(), range);
BiblioNetworking.INSTANCE.sendToAllAround(new BiblioSoundPlayer(CommonProxy.SOUND_BELL_DING_TEXT, pos, 1.0F, 1.0F), target);
}
return true;
}
Expand Down
12 changes: 9 additions & 3 deletions src/main/java/jds/bibliocraft/blocks/BlockCase.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import jds.bibliocraft.CommonProxy;
import jds.bibliocraft.helpers.EnumColor;
import jds.bibliocraft.helpers.EnumVertPosition;
import jds.bibliocraft.network.BiblioNetworking;
import jds.bibliocraft.network.packet.client.BiblioSoundPlayer;
import jds.bibliocraft.states.TextureState;
import jds.bibliocraft.tileentities.BiblioTileEntity;
import jds.bibliocraft.tileentities.TileEntityCase;
Expand All @@ -33,11 +35,13 @@
import net.minecraft.world.IBlockAccess;
import net.minecraft.world.World;
import net.minecraftforge.common.model.TRSRTransformation;
import net.minecraftforge.fml.common.network.NetworkRegistry.TargetPoint;

public class BlockCase extends BiblioWoodBlock
{
public static final String name = "Case";
public static final BlockCase instance = new BlockCase();
public static final float range = 32.0F;

public BlockCase()
{
Expand All @@ -56,14 +60,16 @@ public boolean onBlockActivatedCustomCommands(World world, BlockPos pos, IBlockS
if (player.isSneaking())
{
tile.setOpenLid(!tile.getOpenLid());
TargetPoint target = new TargetPoint(world.provider.getDimension(), pos.getX(), pos.getY(), pos.getZ(), range);
if (tile.getOpenLid())
{
// this works in singleplayer, but not on a server. I think we will have to send packets.
world.playSound(null, pos, CommonProxy.SOUND_CASE_OPEN, SoundCategory.BLOCKS, 1.0F, 1.0F);
//world.playSound(null, pos, CommonProxy.SOUND_CASE_OPEN, SoundCategory.BLOCKS, 1.0F, 1.0F);
BiblioNetworking.INSTANCE.sendToAllAround(new BiblioSoundPlayer(CommonProxy.SOUND_CASE_OPEN_TEXT, pos, 1.0F, 1.0F), target);
}
else
{
world.playSound(null, pos, CommonProxy.SOUND_CASE_CLOSE, SoundCategory.BLOCKS, 1.0F, 1.0F);
//world.playSound(null, pos, CommonProxy.SOUND_CASE_CLOSE, SoundCategory.BLOCKS, 1.0F, 1.0F);
BiblioNetworking.INSTANCE.sendToAllAround(new BiblioSoundPlayer(CommonProxy.SOUND_CASE_CLOSE_TEXT, pos, 1.0F, 1.0F), target);
}
}
else if (tile.getOpenLid())
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/jds/bibliocraft/network/BiblioNetworking.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import jds.bibliocraft.network.packet.client.BiblioOpenBook;
import jds.bibliocraft.network.packet.client.BiblioPanelerClient;
import jds.bibliocraft.network.packet.client.BiblioRecipeText;
import jds.bibliocraft.network.packet.client.BiblioSoundPlayer;
import jds.bibliocraft.network.packet.client.BiblioStockLog;
import jds.bibliocraft.network.packet.server.BiblioAtlas;
import jds.bibliocraft.network.packet.server.BiblioAtlasWPT;
Expand Down Expand Up @@ -81,6 +82,8 @@ public static void setup()
INSTANCE.registerMessage(BiblioDeskOpenGui.Handler.class, BiblioDeskOpenGui.class, packetId++, Side.CLIENT);
INSTANCE.registerMessage(BiblioAtlasSWPClient.Handler.class, BiblioAtlasSWPClient.class, packetId++, Side.CLIENT);
INSTANCE.registerMessage(BiblioAtlasTGUI.Handler.class, BiblioAtlasTGUI.class, packetId++, Side.CLIENT);

INSTANCE.registerMessage(BiblioSoundPlayer.Handler.class, BiblioSoundPlayer.class, packetId++, Side.CLIENT);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package jds.bibliocraft.network.packet.client;

import io.netty.buffer.ByteBuf;
import net.minecraft.client.Minecraft;
import net.minecraft.server.MinecraftServer;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.SoundCategory;
import net.minecraft.util.SoundEvent;
import net.minecraft.util.math.BlockPos;
import net.minecraftforge.fml.common.network.ByteBufUtils;
import net.minecraftforge.fml.common.network.simpleimpl.IMessage;
import net.minecraftforge.fml.common.network.simpleimpl.IMessageHandler;
import net.minecraftforge.fml.common.network.simpleimpl.MessageContext;
import net.minecraftforge.fml.server.FMLServerHandler;

public class BiblioSoundPlayer implements IMessage
{
BlockPos position;
float volume;
float pitch;
String theSound; // it might be better to just pass along an id to grab a reference to a preloaded sound, but well see if this works first, it's simpler.

public BiblioSoundPlayer()
{

}

public BiblioSoundPlayer(String sound, BlockPos pos, float vol, float pit)
{
this.theSound = sound;
this.position = pos;
this.volume = vol;
this.pitch = pit;
}

@Override
public void fromBytes(ByteBuf buf)
{
this.theSound = ByteBufUtils.readUTF8String(buf);
this.position = new BlockPos(buf.readFloat(), buf.readFloat(), buf.readFloat());
this.volume = buf.readFloat();
this.pitch = buf.readFloat();
}

@Override
public void toBytes(ByteBuf buf)
{
ByteBufUtils.writeUTF8String(buf, theSound);
buf.writeFloat(position.getX());
buf.writeFloat(position.getY());
buf.writeFloat(position.getZ());
buf.writeFloat(this.volume);
buf.writeFloat(this.pitch);
}

public static class Handler implements IMessageHandler<BiblioSoundPlayer, IMessage>
{
@Override
public IMessage onMessage(BiblioSoundPlayer message, MessageContext ctx)
{

Minecraft.getMinecraft().addScheduledTask(() ->
{
PlaySound(message.theSound, message.position, message.volume, message.pitch);
});
return null;
}
}

public static void PlaySound(String soundString, BlockPos thePosition, float vol, float pit)
{
SoundEvent sound = new SoundEvent(new ResourceLocation(soundString));
Minecraft.getMinecraft().world.playSound(thePosition, sound, SoundCategory.BLOCKS, vol, pit, false);
}

}

0 comments on commit 8029ad1

Please sign in to comment.