1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2025-03-04 10:38:12 +00:00

Made Speaker threadsafe & fix warnings

This commit is contained in:
Restioson 2017-05-15 16:47:13 +02:00
parent 7ff4631a9f
commit b3c49db761
5 changed files with 74 additions and 20 deletions

View File

@ -0,0 +1,45 @@
/*
* This file is part of ComputerCraft - http://www.computercraft.info
* Copyright Daniel Ratcliffe, 2011-2016. Do not distribute without permission.
* Send enquiries to dratcliffe@gmail.com
*/
package dan200.computercraft.shared.peripheral.speaker;
import dan200.computercraft.api.lua.ILuaTask;
import dan200.computercraft.api.lua.LuaException;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.SoundCategory;
import net.minecraft.util.SoundEvent;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import javax.annotation.Nullable;
public class SoundTask implements ILuaTask
{
SoundTask(World world, BlockPos pos, ResourceLocation resourceName, float volume, float pitch)
{
m_world = world;
m_pos = pos;
m_resourceName = resourceName;
m_volume = volume;
m_pitch = pitch;
}
// Fields
private World m_world;
private BlockPos m_pos;
private ResourceLocation m_resourceName;
private float m_volume;
private float m_pitch;
@Nullable
@Override
public Object[] execute() throws LuaException {
m_world.playSound(null, m_pos, new SoundEvent(m_resourceName), SoundCategory.RECORDS, m_volume, m_pitch);
return new Object[]{};
}
}

View File

@ -1,4 +1,4 @@
/**
/*
* This file is part of ComputerCraft - http://www.computercraft.info
* Copyright Daniel Ratcliffe, 2011-2016. Do not distribute without permission.
* Send enquiries to dratcliffe@gmail.com
@ -12,11 +12,12 @@ import dan200.computercraft.api.lua.LuaException;
import dan200.computercraft.api.peripheral.IComputerAccess;
import dan200.computercraft.api.peripheral.IPeripheral;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.SoundCategory;
import net.minecraft.util.SoundEvent;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import javax.annotation.Nonnull;
public class SpeakerPeripheral implements IPeripheral {
private TileSpeaker m_speaker;
@ -31,7 +32,7 @@ public class SpeakerPeripheral implements IPeripheral {
m_notesThisTick = 0;
}
public SpeakerPeripheral(TileSpeaker speaker)
SpeakerPeripheral(TileSpeaker speaker)
{
this();
m_speaker = speaker;
@ -72,21 +73,23 @@ public class SpeakerPeripheral implements IPeripheral {
@Override
public void attach(IComputerAccess computerAccess)
public void attach(@Nonnull IComputerAccess computerAccess)
{
}
@Override
public void detach(IComputerAccess computerAccess)
public void detach(@Nonnull IComputerAccess computerAccess)
{
}
@Nonnull
@Override
public String getType()
{
return "speaker";
}
@Nonnull
@Override
public String[] getMethodNames()
{
@ -97,20 +100,20 @@ public class SpeakerPeripheral implements IPeripheral {
}
@Override
public Object[] callMethod(IComputerAccess computerAccess, ILuaContext context, int methodIndex, Object[] args) throws LuaException
public Object[] callMethod(@Nonnull IComputerAccess computerAccess, @Nonnull ILuaContext context, int methodIndex, @Nonnull Object[] args) throws LuaException
{
switch (methodIndex)
{
// playsound
case 0:
{
return playSound(args, false);
return playSound(args, context, false);
}
// playnote
case 1:
{
return playNote(args);
return playNote(args, context);
}
default:
@ -121,7 +124,7 @@ public class SpeakerPeripheral implements IPeripheral {
}
}
private Object[] playNote(Object[] arguments) throws LuaException
private Object[] playNote(Object[] arguments, ILuaContext context) throws LuaException
{
double volume = 1f;
double pitch = 1f;
@ -167,14 +170,18 @@ public class SpeakerPeripheral implements IPeripheral {
}
// If the resource location for noteblock notes changes, this method call will need to be updated
Object[] returnValue = playSound(new Object[] {"block.note." + arguments[0], volume, Math.pow(2d, (pitch - 12) / 12d)}, true);
m_notesThisTick++;
Object[] returnValue = playSound(new Object[] {"block.note." + arguments[0], volume, Math.pow(2d, (pitch - 12) / 12d)}, context, true);
if (returnValue[0] instanceof Boolean && (Boolean) returnValue[0])
{
m_notesThisTick++;
}
return returnValue;
}
private Object[] playSound(Object[] arguments, boolean isNote) throws LuaException
private Object[] playSound(Object[] arguments, ILuaContext context, boolean isNote) throws LuaException
{
float volume = 1f;
@ -217,12 +224,12 @@ public class SpeakerPeripheral implements IPeripheral {
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))
{
getWorld().playSound(null, getPos(), new SoundEvent(resourceName), SoundCategory.RECORDS, volume, pitch);
context.issueMainThreadTask(new SoundTask(getWorld(), getPos(), resourceName, volume, pitch));
m_lastPlayTime = m_clock;
return new Object[]{true}; // Success, return true
}

View File

@ -1,4 +1,4 @@
/**
/*
* This file is part of ComputerCraft - http://www.computercraft.info
* Copyright Daniel Ratcliffe, 2011-2016. Do not distribute without permission.
* Send enquiries to dratcliffe@gmail.com
@ -16,7 +16,7 @@ public class TileSpeaker extends TilePeripheralBase
static final int MIN_TICKS_BETWEEN_SOUNDS = 1;
// Members
private SpeakerPeripheral m_peripheral; // TODO what happens when multiple computers wrap one peripheral?
private SpeakerPeripheral m_peripheral;
public TileSpeaker()
{

View File

@ -16,14 +16,14 @@ public class PocketSpeakerPeripheral extends SpeakerPeripheral
private World m_world;
private BlockPos m_position;
public PocketSpeakerPeripheral()
PocketSpeakerPeripheral()
{
super();
m_world = null;
m_position = new BlockPos( 0.0, 0.0, 0.0 );
}
public void setLocation( World world, double x, double y, double z )
void setLocation(World world, double x, double y, double z)
{
m_position = new BlockPos( x, y, z );

View File

@ -166,9 +166,11 @@ public class TurtleSpeaker implements ITurtleUpgrade
@Override
public void update(@Nonnull ITurtleAccess turtle, @Nonnull TurtleSide turtleSide)
{
if (turtle.getPeripheral(turtleSide) instanceof Peripheral)
IPeripheral turtlePeripheral = turtle.getPeripheral(turtleSide);
if (turtlePeripheral instanceof Peripheral)
{
Peripheral peripheral = (Peripheral) turtle.getPeripheral(turtleSide);
Peripheral peripheral = (Peripheral) turtlePeripheral;
peripheral.update();
}
}