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:
parent
7ff4631a9f
commit
b3c49db761
@ -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[]{};
|
||||||
|
}
|
||||||
|
}
|
@ -1,4 +1,4 @@
|
|||||||
/**
|
/*
|
||||||
* This file is part of ComputerCraft - http://www.computercraft.info
|
* This file is part of ComputerCraft - http://www.computercraft.info
|
||||||
* Copyright Daniel Ratcliffe, 2011-2016. Do not distribute without permission.
|
* Copyright Daniel Ratcliffe, 2011-2016. Do not distribute without permission.
|
||||||
* Send enquiries to dratcliffe@gmail.com
|
* 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.IComputerAccess;
|
||||||
import dan200.computercraft.api.peripheral.IPeripheral;
|
import dan200.computercraft.api.peripheral.IPeripheral;
|
||||||
import net.minecraft.util.ResourceLocation;
|
import net.minecraft.util.ResourceLocation;
|
||||||
import net.minecraft.util.SoundCategory;
|
|
||||||
import net.minecraft.util.SoundEvent;
|
import net.minecraft.util.SoundEvent;
|
||||||
import net.minecraft.util.math.BlockPos;
|
import net.minecraft.util.math.BlockPos;
|
||||||
import net.minecraft.world.World;
|
import net.minecraft.world.World;
|
||||||
|
|
||||||
|
import javax.annotation.Nonnull;
|
||||||
|
|
||||||
public class SpeakerPeripheral implements IPeripheral {
|
public class SpeakerPeripheral implements IPeripheral {
|
||||||
|
|
||||||
private TileSpeaker m_speaker;
|
private TileSpeaker m_speaker;
|
||||||
@ -31,7 +32,7 @@ public class SpeakerPeripheral implements IPeripheral {
|
|||||||
m_notesThisTick = 0;
|
m_notesThisTick = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
public SpeakerPeripheral(TileSpeaker speaker)
|
SpeakerPeripheral(TileSpeaker speaker)
|
||||||
{
|
{
|
||||||
this();
|
this();
|
||||||
m_speaker = speaker;
|
m_speaker = speaker;
|
||||||
@ -72,21 +73,23 @@ public class SpeakerPeripheral implements IPeripheral {
|
|||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void attach(IComputerAccess computerAccess)
|
public void attach(@Nonnull IComputerAccess computerAccess)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void detach(IComputerAccess computerAccess)
|
public void detach(@Nonnull IComputerAccess computerAccess)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Nonnull
|
||||||
@Override
|
@Override
|
||||||
public String getType()
|
public String getType()
|
||||||
{
|
{
|
||||||
return "speaker";
|
return "speaker";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Nonnull
|
||||||
@Override
|
@Override
|
||||||
public String[] getMethodNames()
|
public String[] getMethodNames()
|
||||||
{
|
{
|
||||||
@ -97,20 +100,20 @@ public class SpeakerPeripheral implements IPeripheral {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@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)
|
switch (methodIndex)
|
||||||
{
|
{
|
||||||
// playsound
|
// playsound
|
||||||
case 0:
|
case 0:
|
||||||
{
|
{
|
||||||
return playSound(args, false);
|
return playSound(args, context, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
// playnote
|
// playnote
|
||||||
case 1:
|
case 1:
|
||||||
{
|
{
|
||||||
return playNote(args);
|
return playNote(args, context);
|
||||||
}
|
}
|
||||||
|
|
||||||
default:
|
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 volume = 1f;
|
||||||
double pitch = 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
|
// 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);
|
Object[] returnValue = playSound(new Object[] {"block.note." + arguments[0], volume, Math.pow(2d, (pitch - 12) / 12d)}, context, true);
|
||||||
m_notesThisTick++;
|
|
||||||
|
if (returnValue[0] instanceof Boolean && (Boolean) returnValue[0])
|
||||||
|
{
|
||||||
|
m_notesThisTick++;
|
||||||
|
}
|
||||||
|
|
||||||
return returnValue;
|
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;
|
float volume = 1f;
|
||||||
@ -217,12 +224,12 @@ public class SpeakerPeripheral implements IPeripheral {
|
|||||||
|
|
||||||
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))
|
||||||
{
|
{
|
||||||
getWorld().playSound(null, getPos(), new SoundEvent(resourceName), SoundCategory.RECORDS, volume, pitch);
|
context.issueMainThreadTask(new SoundTask(getWorld(), getPos(), resourceName, volume, pitch));
|
||||||
m_lastPlayTime = m_clock;
|
m_lastPlayTime = m_clock;
|
||||||
return new Object[]{true}; // Success, return true
|
return new Object[]{true}; // Success, return true
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
/**
|
/*
|
||||||
* This file is part of ComputerCraft - http://www.computercraft.info
|
* This file is part of ComputerCraft - http://www.computercraft.info
|
||||||
* Copyright Daniel Ratcliffe, 2011-2016. Do not distribute without permission.
|
* Copyright Daniel Ratcliffe, 2011-2016. Do not distribute without permission.
|
||||||
* Send enquiries to dratcliffe@gmail.com
|
* Send enquiries to dratcliffe@gmail.com
|
||||||
@ -16,7 +16,7 @@ public class TileSpeaker extends TilePeripheralBase
|
|||||||
static final int MIN_TICKS_BETWEEN_SOUNDS = 1;
|
static final int MIN_TICKS_BETWEEN_SOUNDS = 1;
|
||||||
|
|
||||||
// Members
|
// Members
|
||||||
private SpeakerPeripheral m_peripheral; // TODO what happens when multiple computers wrap one peripheral?
|
private SpeakerPeripheral m_peripheral;
|
||||||
|
|
||||||
public TileSpeaker()
|
public TileSpeaker()
|
||||||
{
|
{
|
||||||
|
@ -16,14 +16,14 @@ public class PocketSpeakerPeripheral extends SpeakerPeripheral
|
|||||||
private World m_world;
|
private World m_world;
|
||||||
private BlockPos m_position;
|
private BlockPos m_position;
|
||||||
|
|
||||||
public PocketSpeakerPeripheral()
|
PocketSpeakerPeripheral()
|
||||||
{
|
{
|
||||||
super();
|
super();
|
||||||
m_world = null;
|
m_world = null;
|
||||||
m_position = new BlockPos( 0.0, 0.0, 0.0 );
|
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 );
|
m_position = new BlockPos( x, y, z );
|
||||||
|
|
||||||
|
@ -166,9 +166,11 @@ public class TurtleSpeaker implements ITurtleUpgrade
|
|||||||
@Override
|
@Override
|
||||||
public void update(@Nonnull ITurtleAccess turtle, @Nonnull TurtleSide turtleSide)
|
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();
|
peripheral.update();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user