1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2025-05-29 20:54:10 +00:00

Merge pull request #263 from Lignum/fix-speakers

Speaker-related cleanup
This commit is contained in:
Daniel Ratcliffe 2017-05-20 12:27:12 +01:00 committed by GitHub
commit 853e626756
4 changed files with 71 additions and 79 deletions

View File

@ -21,7 +21,6 @@ import javax.annotation.Nonnull;
import javax.annotation.Nullable;
public class SpeakerPeripheral implements IPeripheral {
private TileSpeaker m_speaker;
private long m_clock;
private long m_lastPlayTime;
@ -40,7 +39,8 @@ public class SpeakerPeripheral implements IPeripheral {
m_speaker = speaker;
}
public synchronized void update() {
public synchronized void update()
{
m_clock++;
m_notesThisTick = 0;
}
@ -55,7 +55,7 @@ public class SpeakerPeripheral implements IPeripheral {
return m_speaker.getPos();
}
/* IPeripheral implementations */
/* IPeripheral implementation */
@Override
public boolean equals( IPeripheral other )
@ -65,12 +65,10 @@ public class SpeakerPeripheral implements IPeripheral {
SpeakerPeripheral otherSpeaker = (SpeakerPeripheral) other;
return otherSpeaker.m_speaker == m_speaker;
}
else
{
return false;
}
}
@ -106,13 +104,13 @@ public class SpeakerPeripheral implements IPeripheral {
{
switch( methodIndex )
{
// playsound
// playSound
case 0:
{
return playSound(args, context, false);
}
// playnote
// playNote
case 1:
{
return playNote(args, context);
@ -129,8 +127,8 @@ public class SpeakerPeripheral implements IPeripheral {
@Nonnull
private synchronized Object[] playNote( Object[] arguments, ILuaContext context ) throws LuaException
{
double volume = 1f;
double pitch = 1f;
float volume = 1.0f;
float pitch = 1.0f;
// Check if arguments are correct
if( arguments.length == 0 ) // Too few args
@ -150,17 +148,16 @@ public class SpeakerPeripheral implements IPeripheral {
if ( arguments.length > 1 )
{
if (!(arguments[1] instanceof Double) && arguments[1] != null) // Arg wrong type
if ( arguments[1] != null && !(arguments[1] instanceof Double) ) // Arg wrong type
{
throw new LuaException( "Expected string, number (optional), number (optional)" );
}
volume = arguments[1] != null ? ((Double) arguments[1]).floatValue() : 1f;
}
if( arguments.length > 2 )
{
if (!(arguments[1] instanceof Double) && arguments[2] != null) // Arg wrong type
if( arguments[2] != null && !(arguments[2] instanceof Double) ) // Arg wrong type
{
throw new LuaException("Expected string, number (optional), number (optional)");
}
@ -168,7 +165,13 @@ public class SpeakerPeripheral implements IPeripheral {
}
// If the resource location for note block notes changes, this method call will need to be updated
Object[] returnValue = playSound(new Object[] {"block.note." + arguments[0], Math.min(volume,3f) , Math.pow(2d, (pitch - 12) / 12d)}, context, true);
Object[] returnValue = playSound(
new Object[] {
"block.note." + arguments[0],
(double)Math.min( volume, 3f ),
Math.pow( 2.0f, ( pitch - 12.0f ) / 12.0f)
}, context, true
);
if( returnValue[0] instanceof Boolean && (Boolean) returnValue[0] )
{
@ -176,15 +179,14 @@ public class SpeakerPeripheral implements IPeripheral {
}
return returnValue;
}
@Nonnull
private synchronized Object[] playSound( Object[] arguments, ILuaContext context, boolean isNote ) throws LuaException
{
float volume = 1f;
float pitch = 1f;
float volume = 1.0f;
float pitch = 1.0f;
// Check if arguments are correct
if( arguments.length == 0 ) // Too few args
@ -195,12 +197,11 @@ public class SpeakerPeripheral implements IPeripheral {
if( !(arguments[0] instanceof String) ) // Arg wrong type
{
throw new LuaException( "Expected string, number (optional), number (optional)" );
}
if( arguments.length > 1 )
{
if (!(arguments[1] instanceof Double) && arguments[1] != null) // Arg wrong type
if( arguments[1] != null && !(arguments[1] instanceof Double) ) // Arg wrong type
{
throw new LuaException( "Expected string, number (optional), number (optional)" );
}
@ -211,50 +212,44 @@ public class SpeakerPeripheral implements IPeripheral {
if( arguments.length > 2 )
{
if (!(arguments[2] instanceof Double) && arguments[2] != null) // Arg wrong type
if( arguments[2] != null && !(arguments[2] instanceof Double) ) // Arg wrong type
{
throw new LuaException( "Expected string, number (optional), number (optional)" );
}
pitch = arguments[2] != null ? ((Double) arguments[2]).floatValue() : 1f;
}
ResourceLocation resourceName = new ResourceLocation( (String) arguments[0] );
if( m_clock - m_lastPlayTime >= TileSpeaker.MIN_TICKS_BETWEEN_SOUNDS || ( ( m_clock - m_lastPlayTime == 0 ) && ( m_notesThisTick < ComputerCraft.maxNotesPerTick ) && isNote ) )
{
if( SoundEvent.REGISTRY.containsKey(resourceName) )
{
final World world = getWorld();
final BlockPos pos = getPos();
final ResourceLocation resource = resourceName;
final float vol = volume;
final float soundPitch = pitch;
context.issueMainThreadTask(new ILuaTask() {
context.issueMainThreadTask(new ILuaTask()
{
@Nullable
@Override
public Object[] execute() throws LuaException {
public Object[] execute() throws LuaException
{
world.playSound( null, pos, SoundEvent.REGISTRY.getObject( resource ), SoundCategory.RECORDS, Math.min( vol, 3f ), soundPitch );
return null;
}
});
m_lastPlayTime = m_clock;
return new Object[]{true}; // Success, return true
}
else
{
return new Object[]{false}; // Failed - sound not existent, return false
}
}
else
{
return new Object[]{false}; // Failed - rate limited, return false

View File

@ -13,10 +13,10 @@ import net.minecraft.util.EnumFacing;
public class TileSpeaker extends TilePeripheralBase
{
// Statics
static final int MIN_TICKS_BETWEEN_SOUNDS = 1;
public static final int MIN_TICKS_BETWEEN_SOUNDS = 1;
// Members
private SpeakerPeripheral m_peripheral;
private final SpeakerPeripheral m_peripheral;
public TileSpeaker()
{
@ -25,7 +25,8 @@ public class TileSpeaker extends TilePeripheralBase
}
@Override
public synchronized void update() {
public synchronized void update()
{
m_peripheral.update();
}
@ -35,5 +36,4 @@ public class TileSpeaker extends TilePeripheralBase
{
return m_peripheral;
}
}

View File

@ -146,8 +146,8 @@ public class TurtleSpeaker implements ITurtleUpgrade
@Nonnull
@Override
@SideOnly( Side.CLIENT )
public Pair<IBakedModel, Matrix4f> getModel(ITurtleAccess turtle, @Nonnull TurtleSide side ) {
public Pair<IBakedModel, Matrix4f> getModel( ITurtleAccess turtle, @Nonnull TurtleSide side )
{
loadModelLocations();
ModelManager modelManager = Minecraft.getMinecraft().getRenderItem().getItemModelMesher().getModelManager();
@ -155,19 +155,16 @@ public class TurtleSpeaker implements ITurtleUpgrade
{
return Pair.of( modelManager.getModel( m_leftModel ), null );
}
else
{
return Pair.of( modelManager.getModel( m_rightModel ), null );
}
}
@Override
public void update( @Nonnull ITurtleAccess turtle, @Nonnull TurtleSide turtleSide )
{
IPeripheral turtlePeripheral = turtle.getPeripheral( turtleSide );
if ( turtlePeripheral instanceof Peripheral )
{
Peripheral peripheral = (Peripheral) turtlePeripheral;

View File

@ -2,7 +2,7 @@ The Speaker is a peripheral device available for CraftOS. Type "help peripheral"
Methods exposed by the Speaker:
playSound( sResourceName, nVolume, nPitch )
playNote( sInstrumentName, nVolume, nTicks )
playNote( sInstrumentName, nVolume, nPitch )
Resource name is the same as used by the /playsound command, such as "minecraft:entity.cow.ambient".
Instruments are as follows: "harp", "bass", "snare", "hat", and "basedrum" with the addition of "flute", "bell", "chime", and "guitar" in Minecraft versions 1.12 and above.