1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2025-06-30 09:02:59 +00:00

Format SpeakerPeripheral correctly

This commit is contained in:
Lignum 2017-05-19 20:20:51 +02:00
parent d822147704
commit a402fc9093
No known key found for this signature in database
GPG Key ID: E4DE8F54CA0912BA

View File

@ -21,7 +21,6 @@ import javax.annotation.Nonnull;
import javax.annotation.Nullable; import javax.annotation.Nullable;
public class SpeakerPeripheral implements IPeripheral { public class SpeakerPeripheral implements IPeripheral {
private TileSpeaker m_speaker; private TileSpeaker m_speaker;
private long m_clock; private long m_clock;
private long m_lastPlayTime; private long m_lastPlayTime;
@ -40,7 +39,8 @@ public class SpeakerPeripheral implements IPeripheral {
m_speaker = speaker; m_speaker = speaker;
} }
public synchronized void update() { public synchronized void update()
{
m_clock++; m_clock++;
m_notesThisTick = 0; m_notesThisTick = 0;
} }
@ -55,7 +55,7 @@ public class SpeakerPeripheral implements IPeripheral {
return m_speaker.getPos(); return m_speaker.getPos();
} }
/* IPeripheral implementations */ /* IPeripheral implementation */
@Override @Override
public boolean equals( IPeripheral other ) public boolean equals( IPeripheral other )
@ -65,12 +65,10 @@ public class SpeakerPeripheral implements IPeripheral {
SpeakerPeripheral otherSpeaker = (SpeakerPeripheral) other; SpeakerPeripheral otherSpeaker = (SpeakerPeripheral) other;
return otherSpeaker.m_speaker == m_speaker; return otherSpeaker.m_speaker == m_speaker;
} }
else else
{ {
return false; return false;
} }
} }
@ -106,13 +104,13 @@ public class SpeakerPeripheral implements IPeripheral {
{ {
switch( methodIndex ) switch( methodIndex )
{ {
// playsound // playSound
case 0: case 0:
{ {
return playSound(args, context, false); return playSound(args, context, false);
} }
// playnote // playNote
case 1: case 1:
{ {
return playNote(args, context); return playNote(args, context);
@ -129,8 +127,8 @@ public class SpeakerPeripheral implements IPeripheral {
@Nonnull @Nonnull
private synchronized Object[] playNote( Object[] arguments, ILuaContext context ) throws LuaException private synchronized Object[] playNote( Object[] arguments, ILuaContext context ) throws LuaException
{ {
double volume = 1f; float volume = 1.0f;
double pitch = 1f; float pitch = 1.0f;
// Check if arguments are correct // Check if arguments are correct
if( arguments.length == 0 ) // Too few args if( arguments.length == 0 ) // Too few args
@ -155,7 +153,6 @@ public class SpeakerPeripheral implements IPeripheral {
throw new LuaException( "Expected string, number (optional), number (optional)" ); throw new LuaException( "Expected string, number (optional), number (optional)" );
} }
volume = arguments[1] != null ? ((Double) arguments[1]).floatValue() : 1f; volume = arguments[1] != null ? ((Double) arguments[1]).floatValue() : 1f;
} }
if( arguments.length > 2 ) if( arguments.length > 2 )
@ -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 // 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] ) if( returnValue[0] instanceof Boolean && (Boolean) returnValue[0] )
{ {
@ -176,15 +179,14 @@ public class SpeakerPeripheral implements IPeripheral {
} }
return returnValue; return returnValue;
} }
@Nonnull @Nonnull
private synchronized Object[] playSound( Object[] arguments, ILuaContext context, boolean isNote ) throws LuaException private synchronized Object[] playSound( Object[] arguments, ILuaContext context, boolean isNote ) throws LuaException
{ {
float volume = 1f; float volume = 1.0f;
float pitch = 1f; float pitch = 1.0f;
// Check if arguments are correct // Check if arguments are correct
if( arguments.length == 0 ) // Too few args if( arguments.length == 0 ) // Too few args
@ -195,7 +197,6 @@ public class SpeakerPeripheral implements IPeripheral {
if( !(arguments[0] instanceof String) ) // Arg wrong type if( !(arguments[0] instanceof String) ) // Arg wrong type
{ {
throw new LuaException( "Expected string, number (optional), number (optional)" ); throw new LuaException( "Expected string, number (optional), number (optional)" );
} }
if( arguments.length > 1 ) if( arguments.length > 1 )
@ -218,43 +219,37 @@ public class SpeakerPeripheral implements IPeripheral {
pitch = arguments[2] != null ? ((Double) arguments[2]).floatValue() : 1f; pitch = arguments[2] != null ? ((Double) arguments[2]).floatValue() : 1f;
} }
ResourceLocation resourceName = new ResourceLocation( (String) arguments[0] ); 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( m_clock - m_lastPlayTime >= TileSpeaker.MIN_TICKS_BETWEEN_SOUNDS || ( ( m_clock - m_lastPlayTime == 0 ) && ( m_notesThisTick < ComputerCraft.maxNotesPerTick ) && isNote ) )
{ {
if( SoundEvent.REGISTRY.containsKey(resourceName) ) if( SoundEvent.REGISTRY.containsKey(resourceName) )
{ {
final World world = getWorld(); final World world = getWorld();
final BlockPos pos = getPos(); final BlockPos pos = getPos();
final ResourceLocation resource = resourceName; final ResourceLocation resource = resourceName;
final float vol = volume; final float vol = volume;
final float soundPitch = pitch; final float soundPitch = pitch;
context.issueMainThreadTask(new ILuaTask() { context.issueMainThreadTask(new ILuaTask()
{
@Nullable @Nullable
@Override @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 ); world.playSound( null, pos, SoundEvent.REGISTRY.getObject( resource ), SoundCategory.RECORDS, Math.min( vol, 3f ), soundPitch );
return null; return null;
} }
}); });
m_lastPlayTime = m_clock; m_lastPlayTime = m_clock;
return new Object[]{true}; // Success, return true return new Object[]{true}; // Success, return true
} }
else else
{ {
return new Object[]{false}; // Failed - sound not existent, return false return new Object[]{false}; // Failed - sound not existent, return false
} }
} }
else else
{ {
return new Object[]{false}; // Failed - rate limited, return false return new Object[]{false}; // Failed - rate limited, return false