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

(Hopefully) addressed @dan200's concerns

Push your fix to thread safety if this doesn't cut it
This commit is contained in:
Restioson 2017-05-16 19:48:38 +02:00
parent b3c49db761
commit b28c565665
6 changed files with 52 additions and 82 deletions

View File

@ -294,7 +294,7 @@ public class ComputerCraft
Config.turtlesCanPush.setComment( "If set to true, Turtles will push entities out of the way instead of stopping if there is space to do so" );
Config.maxNotesPerTick = Config.config.get( Configuration.CATEGORY_GENERAL, "maxNotesPerTick", maxNotesPerTick );
Config.maxNotesPerTick.setComment( "Maximum amount of sounds a speaker can play at once" );
Config.maxNotesPerTick.setComment( "Maximum amount of notes a speaker can play at once" );
for (Property property : Config.config.getCategory( Configuration.CATEGORY_GENERAL ).getOrderedValues())
{
@ -335,7 +335,7 @@ public class ComputerCraft
turtlesObeyBlockProtection = Config.turtlesObeyBlockProtection.getBoolean();
turtlesCanPush = Config.turtlesCanPush.getBoolean();
maximumFilesOpen = Math.max(1, Config.maximumFilesOpen.getInt());
maxNotesPerTick = Math.max(1, Config.maxNotesPerTick.getInt());
Config.config.save();
}

View File

@ -1,45 +0,0 @@
/*
* 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

@ -8,15 +8,17 @@ package dan200.computercraft.shared.peripheral.speaker;
import dan200.computercraft.ComputerCraft;
import dan200.computercraft.api.lua.ILuaContext;
import dan200.computercraft.api.lua.ILuaTask;
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;
import javax.annotation.Nullable;
public class SpeakerPeripheral implements IPeripheral {
@ -38,7 +40,7 @@ public class SpeakerPeripheral implements IPeripheral {
m_speaker = speaker;
}
public void update() {
public synchronized void update() {
m_clock++;
m_notesThisTick = 0;
}
@ -124,7 +126,8 @@ public class SpeakerPeripheral implements IPeripheral {
}
}
private Object[] playNote(Object[] arguments, ILuaContext context) throws LuaException
@Nonnull
private synchronized Object[] playNote(Object[] arguments, ILuaContext context) throws LuaException
{
double volume = 1f;
double pitch = 1f;
@ -147,29 +150,24 @@ public class SpeakerPeripheral implements IPeripheral {
if (arguments.length > 1)
{
if (!(arguments[1] instanceof Double)) // Arg wrong type
if (!(arguments[1] instanceof Double) && !(arguments[1] == null)) // Arg wrong type
{
throw new LuaException("Expected string, number (optional), number (optional)");
}
volume = ((Double) arguments[1]).floatValue();
volume = arguments[1] != null ? ((Double) arguments[1]).floatValue() : 1f;
}
if (arguments.length > 2)
{
if (!(arguments[1] instanceof Double)) // Arg wrong type
if (!(arguments[1] instanceof Double) && !(arguments[2] == null)) // Arg wrong type
{
throw new LuaException("Expected string, number (optional), number (optional)");
}
pitch = ((Double) arguments[2]).floatValue();
pitch = arguments[2] != null ? ((Double) arguments[2]).floatValue() : 1f;
}
if (arguments.length > 3)
{
throw new LuaException("Expected string, number (optional), number (optional)");
}
// If the resource location for noteblock 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], volume, Math.pow(2d, (pitch - 12) / 12d)}, context, true);
if (returnValue[0] instanceof Boolean && (Boolean) returnValue[0])
@ -181,7 +179,8 @@ public class SpeakerPeripheral implements IPeripheral {
}
private Object[] playSound(Object[] arguments, ILuaContext context, boolean isNote) throws LuaException
@Nonnull
private synchronized Object[] playSound(Object[] arguments, ILuaContext context, boolean isNote) throws LuaException
{
float volume = 1f;
@ -196,31 +195,29 @@ 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)) // Arg wrong type
if (!(arguments[1] instanceof Double) && !(arguments[1] == null)) // Arg wrong type
{
throw new LuaException("Expected string, number (optional), number (optional)");
}
volume = ((Double) arguments[1]).floatValue();
volume = arguments[1] != null ? ((Double) arguments[1]).floatValue() : 1f;
}
if (arguments.length > 2)
{
if (!(arguments[2] instanceof Double)) // Arg wrong type
if (!(arguments[2] instanceof Double) && !(arguments[2] == null)) // Arg wrong type
{
throw new LuaException("Expected string, number (optional), number (optional)");
}
pitch = ((Double) arguments[2]).floatValue();
pitch = arguments[2] != null ? ((Double) arguments[2]).floatValue() : 1f;
}
if (arguments.length > 3)
{
throw new LuaException("Expected string, number (optional), number (optional)");
}
ResourceLocation resourceName = new ResourceLocation((String) arguments[0]);
@ -229,7 +226,24 @@ public class SpeakerPeripheral implements IPeripheral {
if (SoundEvent.REGISTRY.containsKey(resourceName))
{
context.issueMainThreadTask(new SoundTask(getWorld(), getPos(), resourceName, volume, pitch));
final World world = getWorld();
final BlockPos pos = getPos();
final ResourceLocation resource = resourceName;
final float vol = volume;
final float soundPitch = pitch;
context.issueMainThreadTask(new ILuaTask() {
@Nullable
@Override
public Object[] execute() throws LuaException {
world.playSound(null, pos, new SoundEvent(resource), SoundCategory.RECORDS, vol, soundPitch);
return null;
}
});
m_lastPlayTime = m_clock;
return new Object[]{true}; // Success, return true
}

View File

@ -6,9 +6,9 @@
package dan200.computercraft.shared.peripheral.speaker;
import dan200.computercraft.api.peripheral.IPeripheral;
import dan200.computercraft.shared.peripheral.common.TilePeripheralBase;
import net.minecraft.util.EnumFacing;
import dan200.computercraft.api.peripheral.IPeripheral;
import dan200.computercraft.shared.peripheral.common.TilePeripheralBase;
import net.minecraft.util.EnumFacing;
public class TileSpeaker extends TilePeripheralBase
{

View File

@ -555,15 +555,15 @@ public abstract class ComputerCraftProxyCommon implements IComputerCraftProxy
private void registerTileEntities()
{
// Tile Entities
GameRegistry.registerTileEntity( TileComputer.class, "computer" );
GameRegistry.registerTileEntity( TileDiskDrive.class, "diskdrive" );
GameRegistry.registerTileEntity( TileWirelessModem.class, "wirelessmodem" );
GameRegistry.registerTileEntity( TileMonitor.class, "monitor" );
GameRegistry.registerTileEntity( TilePrinter.class, "ccprinter" );
GameRegistry.registerTileEntity( TileCable.class, "wiredmodem" );
GameRegistry.registerTileEntity( TileCommandComputer.class, "command_computer" );
GameRegistry.registerTileEntity( TileAdvancedModem.class, "advanced_modem" );
GameRegistry.registerTileEntity( TileSpeaker.class, "speaker" );
registerTileEntity( TileComputer.class, "computer" );
registerTileEntity( TileDiskDrive.class, "diskdrive" );
registerTileEntity( TileWirelessModem.class, "wirelessmodem" );
registerTileEntity( TileMonitor.class, "monitor" );
registerTileEntity( TilePrinter.class, "ccprinter" );
registerTileEntity( TileCable.class, "wiredmodem" );
registerTileEntity( TileCommandComputer.class, "command_computer" );
registerTileEntity( TileAdvancedModem.class, "advanced_modem" );
registerTileEntity( TileSpeaker.class, "speaker" );
// Register peripheral providers
ComputerCraftAPI.registerPeripheralProvider( new DefaultPeripheralProvider() );

View File

@ -59,3 +59,4 @@ gui.computercraft:config.advanced_turtle_fuel_limit=Advanced Turtle fuel limit
gui.computercraft:config.turtles_obey_block_protection=Turtles obey block protection
gui.computercraft:config.turtles_can_push=Turtles can push entities
gui.computercraft:config.maximum_files_open=Maximum files open per computer
gui.computercraft:config.max_notes_per_tick=Maximum notes that a computer can play at once