diff --git a/src/main/java/jds/bibliocraft/CommonProxy.java b/src/main/java/jds/bibliocraft/CommonProxy.java index 0461d37..d71bf01 100644 --- a/src/main/java/jds/bibliocraft/CommonProxy.java +++ b/src/main/java/jds/bibliocraft/CommonProxy.java @@ -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")); @@ -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")); diff --git a/src/main/java/jds/bibliocraft/blocks/BlockBell.java b/src/main/java/jds/bibliocraft/blocks/BlockBell.java index 7e2a95f..ca3006c 100644 --- a/src/main/java/jds/bibliocraft/blocks/BlockBell.java +++ b/src/main/java/jds/bibliocraft/blocks/BlockBell.java @@ -1,11 +1,15 @@ 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; @@ -13,11 +17,13 @@ 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() { @@ -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; } diff --git a/src/main/java/jds/bibliocraft/blocks/BlockCase.java b/src/main/java/jds/bibliocraft/blocks/BlockCase.java index 0673267..d85894b 100644 --- a/src/main/java/jds/bibliocraft/blocks/BlockCase.java +++ b/src/main/java/jds/bibliocraft/blocks/BlockCase.java @@ -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; @@ -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() { @@ -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()) diff --git a/src/main/java/jds/bibliocraft/network/BiblioNetworking.java b/src/main/java/jds/bibliocraft/network/BiblioNetworking.java index bacf7a9..a9c81e0 100644 --- a/src/main/java/jds/bibliocraft/network/BiblioNetworking.java +++ b/src/main/java/jds/bibliocraft/network/BiblioNetworking.java @@ -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; @@ -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); } } diff --git a/src/main/java/jds/bibliocraft/network/packet/client/BiblioSoundPlayer.java b/src/main/java/jds/bibliocraft/network/packet/client/BiblioSoundPlayer.java new file mode 100644 index 0000000..25d3646 --- /dev/null +++ b/src/main/java/jds/bibliocraft/network/packet/client/BiblioSoundPlayer.java @@ -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 + { + @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); + } + +}