1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2025-12-04 05:18:05 +00:00
This commit is contained in:
Devan-Kerman
2020-08-29 18:01:01 -05:00
parent 605e1f6b9b
commit 621bc526be
757 changed files with 23755 additions and 74210 deletions

View File

@@ -1,43 +0,0 @@
/*
* This file is part of ComputerCraft - http://www.computercraft.info
* Copyright Daniel Ratcliffe, 2011-2019. Do not distribute without permission.
* Send enquiries to dratcliffe@gmail.com
*/
package dan200.computercraft.shared.peripheral.speaker;
import dan200.computercraft.shared.common.BlockGeneric;
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.item.ItemPlacementContext;
import net.minecraft.state.StateManager;
import net.minecraft.state.property.DirectionProperty;
import net.minecraft.state.property.Properties;
import net.minecraft.util.math.Direction;
import javax.annotation.Nullable;
public class BlockSpeaker extends BlockGeneric
{
private static final DirectionProperty FACING = Properties.HORIZONTAL_FACING;
public BlockSpeaker( Settings settings )
{
super( settings, TileSpeaker.FACTORY );
setDefaultState( getStateManager().getDefaultState()
.with( FACING, Direction.NORTH ) );
}
@Override
protected void appendProperties( StateManager.Builder<Block, BlockState> properties )
{
properties.add( FACING );
}
@Nullable
@Override
public BlockState getPlacementState( ItemPlacementContext placement )
{
return getDefaultState().with( FACING, placement.getPlayerFacing().getOpposite() );
}
}

View File

@@ -1,158 +0,0 @@
/*
* This file is part of ComputerCraft - http://www.computercraft.info
* Copyright Daniel Ratcliffe, 2011-2019. Do not distribute without permission.
* Send enquiries to dratcliffe@gmail.com
*/
package dan200.computercraft.shared.peripheral.speaker;
import dan200.computercraft.ComputerCraft;
import dan200.computercraft.api.lua.ILuaContext;
import dan200.computercraft.api.lua.LuaException;
import dan200.computercraft.api.peripheral.IComputerAccess;
import dan200.computercraft.api.peripheral.IPeripheral;
import net.minecraft.block.enums.Instrument;
import net.minecraft.network.packet.s2c.play.PlaySoundIdS2CPacket;
import net.minecraft.server.MinecraftServer;
import net.minecraft.sound.SoundCategory;
import net.minecraft.util.Identifier;
import net.minecraft.util.InvalidIdentifierException;
import net.minecraft.util.math.Vec3d;
import net.minecraft.world.World;
import javax.annotation.Nonnull;
import java.util.concurrent.atomic.AtomicInteger;
import static dan200.computercraft.core.apis.ArgumentHelper.getString;
import static dan200.computercraft.core.apis.ArgumentHelper.optReal;
public abstract class SpeakerPeripheral implements IPeripheral
{
private long m_clock = 0;
private long m_lastPlayTime = 0;
private final AtomicInteger m_notesThisTick = new AtomicInteger();
public void update()
{
m_clock++;
m_notesThisTick.set( 0 );
}
public abstract World getWorld();
public abstract Vec3d getPosition();
public boolean madeSound( long ticks )
{
return m_clock - m_lastPlayTime <= ticks;
}
@Nonnull
@Override
public String getType()
{
return "speaker";
}
@Nonnull
@Override
public String[] getMethodNames()
{
return new String[] {
"playSound", // Plays sound at resourceLocator
"playNote" // Plays note
};
}
@Override
public Object[] callMethod( @Nonnull IComputerAccess computerAccess, @Nonnull ILuaContext context, int methodIndex, @Nonnull Object[] args ) throws LuaException
{
switch( methodIndex )
{
case 0: // playSound
{
String name = getString( args, 0 );
float volume = (float) optReal( args, 1, 1.0 );
float pitch = (float) optReal( args, 2, 1.0 );
Identifier identifier;
try
{
identifier = new Identifier( name );
}
catch( InvalidIdentifierException e )
{
throw new LuaException( "Malformed sound name '" + name + "' " );
}
return new Object[] { playSound( context, identifier, volume, pitch, false ) };
}
case 1: // playNote
return playNote( args, context );
default:
throw new IllegalStateException( "Method index out of range!" );
}
}
@Nonnull
private synchronized Object[] playNote( Object[] arguments, ILuaContext context ) throws LuaException
{
String name = getString( arguments, 0 );
float volume = (float) optReal( arguments, 1, 1.0 );
float pitch = (float) optReal( arguments, 2, 1.0 );
Instrument instrument = null;
for( Instrument testInstrument : Instrument.values() )
{
if( testInstrument.asString().equalsIgnoreCase( name ) )
{
instrument = testInstrument;
break;
}
}
// Check if the note exists
if( instrument == null )
{
throw new LuaException( "Invalid instrument, \"" + name + "\"!" );
}
// If the resource location for note block notes changes, this method call will need to be updated
boolean success = playSound( context, instrument.getSound().getId(), volume, (float) Math.pow( 2.0, (pitch - 12.0) / 12.0 ), true );
if( success ) m_notesThisTick.incrementAndGet();
return new Object[] { success };
}
private synchronized boolean playSound( ILuaContext context, Identifier name, float volume, float pitch, boolean isNote ) throws LuaException
{
if( m_clock - m_lastPlayTime < TileSpeaker.MIN_TICKS_BETWEEN_SOUNDS &&
(!isNote || m_clock - m_lastPlayTime != 0 || m_notesThisTick.get() >= ComputerCraft.maxNotesPerTick) )
{
// Rate limiting occurs when we've already played a sound within the last tick, or we've
// played more notes than allowable within the current tick.
return false;
}
World world = getWorld();
Vec3d pos = getPosition();
context.issueMainThreadTask( () -> {
MinecraftServer server = world.getServer();
if( server == null ) return null;
float adjVolume = Math.min( volume, 3.0f );
server.getPlayerManager().sendToAround(
null, pos.x, pos.y, pos.z, adjVolume > 1.0f ? 16 * adjVolume : 16.0, world.getDimension().getType(),
new PlaySoundIdS2CPacket( name, SoundCategory.RECORDS, pos, adjVolume, pitch )
);
return null;
} );
m_lastPlayTime = m_clock;
return true;
}
}

View File

@@ -1,82 +0,0 @@
/*
* This file is part of ComputerCraft - http://www.computercraft.info
* Copyright Daniel Ratcliffe, 2011-2019. Do not distribute without permission.
* Send enquiries to dratcliffe@gmail.com
*/
package dan200.computercraft.shared.peripheral.speaker;
import dan200.computercraft.ComputerCraft;
import dan200.computercraft.api.peripheral.IPeripheral;
import dan200.computercraft.api.peripheral.IPeripheralTile;
import dan200.computercraft.shared.common.TileGeneric;
import dan200.computercraft.shared.util.NamedBlockEntityType;
import net.minecraft.util.Identifier;
import net.minecraft.util.Tickable;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Direction;
import net.minecraft.util.math.Vec3d;
import net.minecraft.world.World;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
public class TileSpeaker extends TileGeneric implements Tickable, IPeripheralTile
{
public static final int MIN_TICKS_BETWEEN_SOUNDS = 1;
public static final NamedBlockEntityType<TileSpeaker> FACTORY = NamedBlockEntityType.create(
new Identifier( ComputerCraft.MOD_ID, "speaker" ),
TileSpeaker::new
);
private final SpeakerPeripheral m_peripheral;
public TileSpeaker()
{
super( FACTORY );
m_peripheral = new Peripheral( this );
}
@Override
public void tick()
{
m_peripheral.update();
}
@Override
public IPeripheral getPeripheral( @Nonnull Direction side )
{
return m_peripheral;
}
private static final class Peripheral extends SpeakerPeripheral
{
private final TileSpeaker speaker;
private Peripheral( TileSpeaker speaker )
{
this.speaker = speaker;
}
@Override
public World getWorld()
{
return speaker.getWorld();
}
@Override
public Vec3d getPosition()
{
BlockPos pos = speaker.getPos();
return new Vec3d( pos.getX(), pos.getY(), pos.getZ() );
}
@Override
public boolean equals( @Nullable IPeripheral other )
{
return this == other || (other instanceof Peripheral && speaker == ((Peripheral) other).speaker);
}
}
}