001package net.minecraft.entity.item; 002 003import cpw.mods.fml.relauncher.Side; 004import cpw.mods.fml.relauncher.SideOnly; 005import net.minecraft.client.audio.SoundManager; 006import net.minecraft.client.entity.EntityPlayerSP; 007import net.minecraft.server.gui.IUpdatePlayerListBox; 008import net.minecraft.util.MathHelper; 009 010@SideOnly(Side.CLIENT) 011public class SoundUpdaterMinecart implements IUpdatePlayerListBox 012{ 013 private final SoundManager theSoundManager; 014 015 /** Minecart which sound is being updated. */ 016 private final EntityMinecart theMinecart; 017 018 /** The player that is getting the minecart sound updates. */ 019 private final EntityPlayerSP thePlayer; 020 private boolean playerSPRidingMinecart = false; 021 private boolean minecartIsDead = false; 022 private boolean minecartIsMoving = false; 023 private boolean silent = false; 024 private float minecartSoundPitch = 0.0F; 025 private float minecartMoveSoundVolume = 0.0F; 026 private float minecartRideSoundVolume = 0.0F; 027 private double minecartSpeed = 0.0D; 028 029 public SoundUpdaterMinecart(SoundManager par1SoundManager, EntityMinecart par2EntityMinecart, EntityPlayerSP par3EntityPlayerSP) 030 { 031 this.theSoundManager = par1SoundManager; 032 this.theMinecart = par2EntityMinecart; 033 this.thePlayer = par3EntityPlayerSP; 034 } 035 036 /** 037 * Updates the JList with a new model. 038 */ 039 public void update() 040 { 041 boolean flag = false; 042 boolean flag1 = this.playerSPRidingMinecart; 043 boolean flag2 = this.minecartIsDead; 044 boolean flag3 = this.minecartIsMoving; 045 float f = this.minecartMoveSoundVolume; 046 float f1 = this.minecartSoundPitch; 047 float f2 = this.minecartRideSoundVolume; 048 double d0 = this.minecartSpeed; 049 this.playerSPRidingMinecart = this.thePlayer != null && this.theMinecart.riddenByEntity == this.thePlayer; 050 this.minecartIsDead = this.theMinecart.isDead; 051 this.minecartSpeed = (double)MathHelper.sqrt_double(this.theMinecart.motionX * this.theMinecart.motionX + this.theMinecart.motionZ * this.theMinecart.motionZ); 052 this.minecartIsMoving = this.minecartSpeed >= 0.01D; 053 054 if (flag1 && !this.playerSPRidingMinecart) 055 { 056 this.theSoundManager.stopEntitySound(this.thePlayer); 057 } 058 059 if (this.minecartIsDead || !this.silent && this.minecartMoveSoundVolume == 0.0F && this.minecartRideSoundVolume == 0.0F) 060 { 061 if (!flag2) 062 { 063 this.theSoundManager.stopEntitySound(this.theMinecart); 064 065 if (flag1 || this.playerSPRidingMinecart) 066 { 067 this.theSoundManager.stopEntitySound(this.thePlayer); 068 } 069 } 070 071 this.silent = true; 072 073 if (this.minecartIsDead) 074 { 075 return; 076 } 077 } 078 079 if (!this.theSoundManager.isEntitySoundPlaying(this.theMinecart) && this.minecartMoveSoundVolume > 0.0F) 080 { 081 this.theSoundManager.playEntitySound("minecart.base", this.theMinecart, this.minecartMoveSoundVolume, this.minecartSoundPitch, false); 082 this.silent = false; 083 flag = true; 084 } 085 086 if (this.playerSPRidingMinecart && !this.theSoundManager.isEntitySoundPlaying(this.thePlayer) && this.minecartRideSoundVolume > 0.0F) 087 { 088 this.theSoundManager.playEntitySound("minecart.inside", this.thePlayer, this.minecartRideSoundVolume, 1.0F, true); 089 this.silent = false; 090 flag = true; 091 } 092 093 if (this.minecartIsMoving) 094 { 095 if (this.minecartSoundPitch < 1.0F) 096 { 097 this.minecartSoundPitch += 0.0025F; 098 } 099 100 if (this.minecartSoundPitch > 1.0F) 101 { 102 this.minecartSoundPitch = 1.0F; 103 } 104 105 float f3 = MathHelper.clamp_float((float)this.minecartSpeed, 0.0F, 4.0F) / 4.0F; 106 this.minecartRideSoundVolume = 0.0F + f3 * 0.75F; 107 f3 = MathHelper.clamp_float(f3 * 2.0F, 0.0F, 1.0F); 108 this.minecartMoveSoundVolume = 0.0F + f3 * 0.7F; 109 } 110 else if (flag3) 111 { 112 this.minecartMoveSoundVolume = 0.0F; 113 this.minecartSoundPitch = 0.0F; 114 this.minecartRideSoundVolume = 0.0F; 115 } 116 117 if (!this.silent) 118 { 119 if (this.minecartSoundPitch != f1) 120 { 121 this.theSoundManager.setEntitySoundPitch(this.theMinecart, this.minecartSoundPitch); 122 } 123 124 if (this.minecartMoveSoundVolume != f) 125 { 126 this.theSoundManager.setEntitySoundVolume(this.theMinecart, this.minecartMoveSoundVolume); 127 } 128 129 if (this.minecartRideSoundVolume != f2) 130 { 131 this.theSoundManager.setEntitySoundVolume(this.thePlayer, this.minecartRideSoundVolume); 132 } 133 } 134 135 if (!flag && (this.minecartMoveSoundVolume > 0.0F || this.minecartRideSoundVolume > 0.0F)) 136 { 137 this.theSoundManager.updateSoundLocation(this.theMinecart); 138 139 if (this.playerSPRidingMinecart) 140 { 141 this.theSoundManager.updateSoundLocation(this.thePlayer, this.theMinecart); 142 } 143 } 144 else 145 { 146 if (this.theSoundManager.isEntitySoundPlaying(this.theMinecart)) 147 { 148 this.theSoundManager.stopEntitySound(this.theMinecart); 149 } 150 151 if (this.playerSPRidingMinecart && this.theSoundManager.isEntitySoundPlaying(this.thePlayer)) 152 { 153 this.theSoundManager.stopEntitySound(this.thePlayer); 154 } 155 } 156 } 157}