@SideOnly(Side.CLIENT) @SubscribeEvent public void onPlaySound(PlaySoundEvent event) { if (event.getResultSound() == null || event.getResultSound() instanceof ITickableSound || ModCyclic.proxy.getClientWorld() == null) { return; } //long term/repeating/music ISound sound = event.getResultSound(); List<BlockPos> blocks = UtilWorld.findBlocks(ModCyclic.proxy.getClientWorld(), new BlockPos(sound.getXPosF(), sound.getYPosF(), sound.getZPosF()), this, RADIUS); if (blocks == null || blocks.size() == 0) { return; } try {//WARNING": DO NOT USE getVolume anywhere here it just crashes //we do use it inside the sound class, but the engine callss tat later on, and our factor is tacked in SoundVolumeControlled newSound = new SoundVolumeControlled(sound); //the number of nearby blocks informs how much we muffle the sound by float pct = ((float) VOL_REDUCE_PER_BLOCK) / 100F; newSound.setVolume(pct / blocks.size()); event.setResultSound(newSound); } catch (Exception e) { ModCyclic.logger.error("Error trying to detect volume of sound from 3rd party "); ModCyclic.logger.error(e.getMessage()); e.printStackTrace();//getVolume() in naive Positioned sound event gives NPE } }
@SubscribeEvent public void soundMufflerPlay(final PlaySoundEvent17 event) { if (Minecraft.getMinecraft().theWorld != null && ExtraUtils.soundMuffler != null && event.result != null) { final World world = (World)Minecraft.getMinecraft().theWorld; if (event.result instanceof ITickableSound) { return; } final float x = event.result.getXPosF(); final float y = event.result.getYPosF(); final float z = event.result.getZPosF(); for (int dx = (int)Math.floor(x - 8.0f) >> 4; dx <= (int)Math.floor(x + 8.0f) >> 4; ++dx) { for (int dz = (int)Math.floor(z - 8.0f) >> 4; dz <= (int)Math.floor(z + 8.0f) >> 4; ++dz) { for (final TileEntity var19 : (Collection<TileEntity>) world.getChunkFromChunkCoords(dx, dz).chunkTileEntityMap.values()) { if (var19 instanceof TileEntitySoundMuffler) { if (var19.getBlockMetadata() == 1) { continue; } double d = (var19.xCoord + 0.5 - x) * (var19.xCoord + 0.5 - x) + (var19.yCoord + 0.5 - y) * (var19.yCoord + 0.5 - y) + (var19.zCoord + 0.5 - z) * (var19.zCoord + 0.5 - z); if (d > 64.0) { continue; } if (d <= 0.0) { continue; } event.result = (ISound)new SoundMuffled(event.result); d = Math.sqrt(d); if (d != 0.0) { d = 1.0 / d / 8.0; } world.spawnParticle("smoke", (double)x, (double)y, (double)z, (var19.xCoord + 0.5 - x) * d, (var19.yCoord + 0.5 - y) * d, (var19.zCoord + 0.5 - z) * d); } } } } } }
@Override public void updateAllSounds() { final SoundSystem sndSystem = getSoundSystem(); ++this.playTime; for (final ITickableSound itickablesound : this.tickableSounds) { itickablesound.update(); if (itickablesound.isDonePlaying()) { this.stopSound(itickablesound); } else { final String s = this.invPlayingSounds.get(itickablesound); synchronized (SoundSystemConfig.THREAD_SYNC) { sndSystem.setVolume(s, this.getClampedVolume(itickablesound)); sndSystem.setPitch(s, this.getClampedPitch(itickablesound)); sndSystem.setPosition(s, itickablesound.getXPosF(), itickablesound.getYPosF(), itickablesound.getZPosF()); } } } final Iterator<Entry<String, ISound>> iterator = this.playingSounds.entrySet().iterator(); while (iterator.hasNext()) { final Entry<String, ISound> entry = iterator.next(); final String s1 = entry.getKey(); if (!sndSystem.playing(s1)) { final ISound isound = entry.getValue(); final int j = isound.getRepeatDelay(); final int minThresholdDelay = isound instanceof BasicSound ? 0 : 1; // Repeatable sound could have a delay of 0, meaning // don't delay a requeue. if (isound.canRepeat() && j >= minThresholdDelay) { this.playDelayedSound(isound, j); } else { this.setState(isound, SoundState.DONE); } iterator.remove(); sndSystem.removeSource(s1); this.playingSoundsStopTime.remove(s1); try { this.categorySounds.remove(isound.getCategory(), s1); } catch (RuntimeException var8) { ; } if (isound instanceof ITickableSound) { this.tickableSounds.remove(isound); } } } final Iterator<Entry<ISound, Integer>> iterator1 = this.delayedSounds.entrySet().iterator(); while (iterator1.hasNext()) { final Entry<ISound, Integer> entry1 = iterator1.next(); if (this.playTime >= entry1.getValue().intValue()) { final ISound isound1 = entry1.getKey(); if (isound1 instanceof ITickableSound) { ((ITickableSound) isound1).update(); } this.playSound(isound1); iterator1.remove(); } } }
public static boolean canRestartSound(ITickableSound sound) { return sound.isDonePlaying() && !getSoundMap().containsKey(sound); }