mirror of
https://github.com/SquidDev-CC/CC-Tweaked
synced 2025-09-01 10:07:56 +00:00
Merge pull request #263 from Lignum/fix-speakers
Speaker-related cleanup
This commit is contained in:
@@ -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,32 +55,30 @@ 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 )
|
||||||
{
|
{
|
||||||
if (other != null && other instanceof SpeakerPeripheral)
|
if( other != null && other instanceof SpeakerPeripheral )
|
||||||
{
|
{
|
||||||
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void attach(@Nonnull IComputerAccess computerAccess)
|
public void attach( @Nonnull IComputerAccess computerAccess )
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void detach(@Nonnull IComputerAccess computerAccess)
|
public void detach( @Nonnull IComputerAccess computerAccess )
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -102,17 +100,17 @@ public class SpeakerPeripheral implements IPeripheral {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Object[] callMethod(@Nonnull IComputerAccess computerAccess, @Nonnull ILuaContext context, int methodIndex, @Nonnull Object[] args) throws LuaException
|
public Object[] callMethod( @Nonnull IComputerAccess computerAccess, @Nonnull ILuaContext context, int methodIndex, @Nonnull Object[] args ) throws LuaException
|
||||||
{
|
{
|
||||||
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);
|
||||||
@@ -127,40 +125,39 @@ 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
|
||||||
|
{
|
||||||
|
throw new LuaException( "Expected string, number (optional), number (optional)" );
|
||||||
|
}
|
||||||
|
|
||||||
|
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[0] instanceof String)) // Arg wrong type
|
if ( !SoundEvent.REGISTRY.containsKey( new ResourceLocation( "block.note." + arguments[0] ) ) )
|
||||||
{
|
|
||||||
throw new LuaException("Expected string, number (optional), number (optional)");
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!SoundEvent.REGISTRY.containsKey(new ResourceLocation("block.note." + arguments[0])))
|
|
||||||
{
|
{
|
||||||
throw new LuaException("Invalid instrument, \"" + arguments[0] + "\"!");
|
throw new LuaException("Invalid instrument, \"" + arguments[0] + "\"!");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (arguments.length > 1)
|
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)");
|
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 )
|
||||||
{
|
{
|
||||||
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)");
|
throw new LuaException("Expected string, number (optional), number (optional)");
|
||||||
}
|
}
|
||||||
@@ -168,93 +165,91 @@ 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] )
|
||||||
{
|
{
|
||||||
m_notesThisTick++;
|
m_notesThisTick++;
|
||||||
}
|
}
|
||||||
|
|
||||||
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
|
||||||
{
|
{
|
||||||
throw new LuaException("Expected string, number (optional), number (optional)");
|
throw new LuaException( "Expected string, number (optional), number (optional)" );
|
||||||
}
|
}
|
||||||
|
|
||||||
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 )
|
||||||
{
|
{
|
||||||
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)");
|
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 )
|
||||||
{
|
{
|
||||||
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)");
|
throw new LuaException( "Expected string, number (optional), number (optional)" );
|
||||||
}
|
}
|
||||||
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
|
||||||
|
@@ -13,10 +13,10 @@ import net.minecraft.util.EnumFacing;
|
|||||||
public class TileSpeaker extends TilePeripheralBase
|
public class TileSpeaker extends TilePeripheralBase
|
||||||
{
|
{
|
||||||
// Statics
|
// Statics
|
||||||
static final int MIN_TICKS_BETWEEN_SOUNDS = 1;
|
public static final int MIN_TICKS_BETWEEN_SOUNDS = 1;
|
||||||
|
|
||||||
// Members
|
// Members
|
||||||
private SpeakerPeripheral m_peripheral;
|
private final SpeakerPeripheral m_peripheral;
|
||||||
|
|
||||||
public TileSpeaker()
|
public TileSpeaker()
|
||||||
{
|
{
|
||||||
@@ -25,15 +25,15 @@ public class TileSpeaker extends TilePeripheralBase
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public synchronized void update() {
|
public synchronized void update()
|
||||||
|
{
|
||||||
m_peripheral.update();
|
m_peripheral.update();
|
||||||
}
|
}
|
||||||
|
|
||||||
// IPeripheralTile implementation
|
// IPeripheralTile implementation
|
||||||
|
|
||||||
public IPeripheral getPeripheral(EnumFacing side)
|
public IPeripheral getPeripheral( EnumFacing side )
|
||||||
{
|
{
|
||||||
return m_peripheral;
|
return m_peripheral;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@@ -81,7 +81,7 @@ public class TurtleSpeaker implements ITurtleUpgrade
|
|||||||
@SideOnly( Side.CLIENT )
|
@SideOnly( Side.CLIENT )
|
||||||
private ModelResourceLocation m_rightModel;
|
private ModelResourceLocation m_rightModel;
|
||||||
|
|
||||||
public TurtleSpeaker(ResourceLocation id, int legacyId)
|
public TurtleSpeaker( ResourceLocation id, int legacyId )
|
||||||
{
|
{
|
||||||
m_id = id;
|
m_id = id;
|
||||||
m_legacyID = legacyId;
|
m_legacyID = legacyId;
|
||||||
@@ -128,7 +128,7 @@ public class TurtleSpeaker implements ITurtleUpgrade
|
|||||||
|
|
||||||
@Nonnull
|
@Nonnull
|
||||||
@Override
|
@Override
|
||||||
public TurtleCommandResult useTool(@Nonnull ITurtleAccess turtleAccess, @Nonnull TurtleSide turtleSide, @Nonnull TurtleVerb verb, @Nonnull EnumFacing direction)
|
public TurtleCommandResult useTool( @Nonnull ITurtleAccess turtleAccess, @Nonnull TurtleSide turtleSide, @Nonnull TurtleVerb verb, @Nonnull EnumFacing direction )
|
||||||
{
|
{
|
||||||
return TurtleCommandResult.failure();
|
return TurtleCommandResult.failure();
|
||||||
}
|
}
|
||||||
@@ -146,29 +146,26 @@ public class TurtleSpeaker implements ITurtleUpgrade
|
|||||||
@Nonnull
|
@Nonnull
|
||||||
@Override
|
@Override
|
||||||
@SideOnly( Side.CLIENT )
|
@SideOnly( Side.CLIENT )
|
||||||
public Pair<IBakedModel, Matrix4f> getModel(ITurtleAccess turtle, @Nonnull TurtleSide side ) {
|
public Pair<IBakedModel, Matrix4f> getModel( ITurtleAccess turtle, @Nonnull TurtleSide side )
|
||||||
|
{
|
||||||
loadModelLocations();
|
loadModelLocations();
|
||||||
ModelManager modelManager = Minecraft.getMinecraft().getRenderItem().getItemModelMesher().getModelManager();
|
ModelManager modelManager = Minecraft.getMinecraft().getRenderItem().getItemModelMesher().getModelManager();
|
||||||
|
|
||||||
if (side == TurtleSide.Left)
|
if( side == TurtleSide.Left )
|
||||||
{
|
{
|
||||||
return Pair.of(modelManager.getModel( m_leftModel ), null);
|
return Pair.of( modelManager.getModel( m_leftModel ), null );
|
||||||
}
|
}
|
||||||
|
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
return Pair.of(modelManager.getModel( m_rightModel ), null);
|
return Pair.of( modelManager.getModel( m_rightModel ), null );
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void update(@Nonnull ITurtleAccess turtle, @Nonnull TurtleSide turtleSide)
|
public void update( @Nonnull ITurtleAccess turtle, @Nonnull TurtleSide turtleSide )
|
||||||
{
|
{
|
||||||
IPeripheral turtlePeripheral = turtle.getPeripheral(turtleSide);
|
IPeripheral turtlePeripheral = turtle.getPeripheral( turtleSide );
|
||||||
|
if ( turtlePeripheral instanceof Peripheral )
|
||||||
if (turtlePeripheral instanceof Peripheral)
|
|
||||||
{
|
{
|
||||||
Peripheral peripheral = (Peripheral) turtlePeripheral;
|
Peripheral peripheral = (Peripheral) turtlePeripheral;
|
||||||
peripheral.update();
|
peripheral.update();
|
||||||
|
@@ -2,7 +2,7 @@ The Speaker is a peripheral device available for CraftOS. Type "help peripheral"
|
|||||||
|
|
||||||
Methods exposed by the Speaker:
|
Methods exposed by the Speaker:
|
||||||
playSound( sResourceName, nVolume, nPitch )
|
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".
|
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.
|
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.
|
||||||
|
Reference in New Issue
Block a user