1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2024-12-12 11:10:29 +00:00

Some more minor cleanups

- Move SpeakerPeripheral's TileSpeaker functionality to a sub-class.
 - Use Vec3d instead of BlockPos for speaker's positions.
 - Use WorldUtil.dropItemStack to spawn in items.
 - Remove redundant lock on ModemPeripheral.
This commit is contained in:
SquidDev 2019-01-12 15:42:50 +00:00
parent ed69495b03
commit 77666d7399
8 changed files with 76 additions and 104 deletions

View File

@ -181,21 +181,19 @@ public abstract class ModemPeripheral implements IPeripheral, IPacketSender, IPa
int channel = parseChannel( arguments, 0 ); int channel = parseChannel( arguments, 0 );
int replyChannel = parseChannel( arguments, 1 ); int replyChannel = parseChannel( arguments, 1 );
Object payload = arguments.length > 2 ? arguments[2] : null; Object payload = arguments.length > 2 ? arguments[2] : null;
synchronized( this ) World world = getWorld();
Vec3d position = getPosition();
IPacketNetwork network = m_network;
if( world != null && position != null && network != null )
{ {
World world = getWorld(); Packet packet = new Packet( channel, replyChannel, payload, this );
Vec3d position = getPosition(); if( isInterdimensional() )
if( world != null && position != null && m_network != null )
{ {
Packet packet = new Packet( channel, replyChannel, payload, this ); network.transmitInterdimensional( packet );
if( isInterdimensional() ) }
{ else
m_network.transmitInterdimensional( packet ); {
} network.transmitSameDimension( packet, getRange() );
else
{
m_network.transmitSameDimension( packet, getRange() );
}
} }
} }
return null; return null;

View File

@ -14,8 +14,8 @@ import dan200.computercraft.shared.peripheral.PeripheralType;
import dan200.computercraft.shared.peripheral.common.TilePeripheralBase; import dan200.computercraft.shared.peripheral.common.TilePeripheralBase;
import dan200.computercraft.shared.util.DefaultSidedInventory; import dan200.computercraft.shared.util.DefaultSidedInventory;
import dan200.computercraft.shared.util.InventoryUtil; import dan200.computercraft.shared.util.InventoryUtil;
import dan200.computercraft.shared.util.WorldUtil;
import net.minecraft.block.state.IBlockState; import net.minecraft.block.state.IBlockState;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Items; import net.minecraft.init.Items;
import net.minecraft.item.Item; import net.minecraft.item.Item;
@ -521,11 +521,7 @@ public class TilePrinter extends TilePeripheralBase implements DefaultSidedInven
double x = pos.getX() + 0.5; double x = pos.getX() + 0.5;
double y = pos.getY() + 0.75; double y = pos.getY() + 0.75;
double z = pos.getZ() + 0.5; double z = pos.getZ() + 0.5;
EntityItem entityitem = new EntityItem( getWorld(), x, y, z, stack ); WorldUtil.dropItemStack( stack, getWorld(), x, y, z );
entityitem.motionX = getWorld().rand.nextFloat() * 0.2 - 0.1;
entityitem.motionY = getWorld().rand.nextFloat() * 0.2 - 0.1;
entityitem.motionZ = getWorld().rand.nextFloat() * 0.2 - 0.1;
getWorld().spawnEntity( entityitem );
} }
} }
} }

View File

@ -16,7 +16,7 @@ import net.minecraft.server.MinecraftServer;
import net.minecraft.util.ResourceLocation; import net.minecraft.util.ResourceLocation;
import net.minecraft.util.SoundCategory; 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.Vec3d;
import net.minecraft.world.World; import net.minecraft.world.World;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
@ -25,25 +25,11 @@ import java.util.concurrent.atomic.AtomicInteger;
import static dan200.computercraft.core.apis.ArgumentHelper.getString; import static dan200.computercraft.core.apis.ArgumentHelper.getString;
import static dan200.computercraft.core.apis.ArgumentHelper.optReal; import static dan200.computercraft.core.apis.ArgumentHelper.optReal;
public class SpeakerPeripheral implements IPeripheral public abstract class SpeakerPeripheral implements IPeripheral
{ {
private final TileSpeaker m_speaker; private long m_clock = 0;
private long m_clock; private long m_lastPlayTime = 0;
private long m_lastPlayTime; private final AtomicInteger m_notesThisTick = new AtomicInteger();
private final AtomicInteger m_notesThisTick;
public SpeakerPeripheral()
{
this( null );
}
SpeakerPeripheral( TileSpeaker speaker )
{
m_clock = 0;
m_lastPlayTime = 0;
m_notesThisTick = new AtomicInteger();
m_speaker = speaker;
}
public void update() public void update()
{ {
@ -51,31 +37,15 @@ public class SpeakerPeripheral implements IPeripheral
m_notesThisTick.set( 0 ); m_notesThisTick.set( 0 );
} }
public World getWorld() public abstract World getWorld();
{
return m_speaker.getWorld();
}
public BlockPos getPos() public abstract Vec3d getPos();
{
return m_speaker.getPos();
}
public boolean madeSound( long ticks ) public boolean madeSound( long ticks )
{ {
return m_clock - m_lastPlayTime <= ticks; return m_clock - m_lastPlayTime <= ticks;
} }
/* IPeripheral implementation */
@Override
public boolean equals( IPeripheral other )
{
if( other == this ) return true;
if( !(other instanceof SpeakerPeripheral) ) return false;
return m_speaker == ((SpeakerPeripheral) other).m_speaker;
}
@Nonnull @Nonnull
@Override @Override
public String getType() public String getType()
@ -155,17 +125,16 @@ public class SpeakerPeripheral implements IPeripheral
} }
World world = getWorld(); World world = getWorld();
BlockPos pos = getPos(); Vec3d pos = getPos();
context.issueMainThreadTask( () -> { context.issueMainThreadTask( () -> {
MinecraftServer server = world.getMinecraftServer(); MinecraftServer server = world.getMinecraftServer();
if( server == null ) return null; if( server == null ) return null;
double x = pos.getX() + 0.5, y = pos.getY() + 0.5, z = pos.getZ() + 0.5;
float adjVolume = Math.min( volume, 3.0f ); float adjVolume = Math.min( volume, 3.0f );
server.getPlayerList().sendToAllNearExcept( server.getPlayerList().sendToAllNearExcept(
null, x, y, z, adjVolume > 1.0f ? 16 * adjVolume : 16.0, world.provider.getDimension(), null, pos.x, pos.y, pos.z, adjVolume > 1.0f ? 16 * adjVolume : 16.0, world.provider.getDimension(),
new SPacketCustomSound( name, SoundCategory.RECORDS, x, y, z, adjVolume, pitch ) new SPacketCustomSound( name, SoundCategory.RECORDS, pos.x, pos.y, pos.z, adjVolume, pitch )
); );
return null; return null;
} ); } );

View File

@ -9,6 +9,11 @@ package dan200.computercraft.shared.peripheral.speaker;
import dan200.computercraft.api.peripheral.IPeripheral; import dan200.computercraft.api.peripheral.IPeripheral;
import dan200.computercraft.shared.peripheral.common.TilePeripheralBase; import dan200.computercraft.shared.peripheral.common.TilePeripheralBase;
import net.minecraft.util.EnumFacing; import net.minecraft.util.EnumFacing;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Vec3d;
import net.minecraft.world.World;
import javax.annotation.Nullable;
public class TileSpeaker extends TilePeripheralBase public class TileSpeaker extends TilePeripheralBase
{ {
@ -21,7 +26,7 @@ public class TileSpeaker extends TilePeripheralBase
public TileSpeaker() public TileSpeaker()
{ {
super(); super();
m_peripheral = new SpeakerPeripheral( this ); m_peripheral = new Peripheral( this );
} }
@Override @Override
@ -37,4 +42,33 @@ public class TileSpeaker extends TilePeripheralBase
{ {
return m_peripheral; return m_peripheral;
} }
private static 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 getPos()
{
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);
}
}
} }

View File

@ -12,7 +12,6 @@ import dan200.computercraft.shared.peripheral.PeripheralType;
import dan200.computercraft.shared.peripheral.common.PeripheralItemFactory; import dan200.computercraft.shared.peripheral.common.PeripheralItemFactory;
import dan200.computercraft.shared.peripheral.modem.ModemState; import dan200.computercraft.shared.peripheral.modem.ModemState;
import net.minecraft.entity.Entity; import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.util.ResourceLocation; import net.minecraft.util.ResourceLocation;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
@ -54,15 +53,8 @@ public class PocketModem extends AbstractPocketUpgrade
Entity entity = access.getEntity(); Entity entity = access.getEntity();
PocketModemPeripheral modem = (PocketModemPeripheral) peripheral; PocketModemPeripheral modem = (PocketModemPeripheral) peripheral;
if( entity instanceof EntityLivingBase )
{ if( entity != null ) modem.setLocation( entity.getEntityWorld(), entity.getPositionEyes( 1 ) );
EntityLivingBase player = (EntityLivingBase) entity;
modem.setLocation( entity.getEntityWorld(), player.posX, player.posY + player.getEyeHeight(), player.posZ );
}
else if( entity != null )
{
modem.setLocation( entity.getEntityWorld(), entity.posX, entity.posY, entity.posZ );
}
ModemState state = modem.getModemState(); ModemState state = modem.getModemState();
if( state.pollChanged() ) access.setLight( state.isOpen() ? 0xBA0000 : -1 ); if( state.pollChanged() ) access.setLight( state.isOpen() ? 0xBA0000 : -1 );

View File

@ -15,24 +15,18 @@ import javax.annotation.Nonnull;
public class PocketModemPeripheral extends WirelessModemPeripheral public class PocketModemPeripheral extends WirelessModemPeripheral
{ {
private World world; private World world = null;
private Vec3d position; private Vec3d position = Vec3d.ZERO;
public PocketModemPeripheral( boolean advanced ) public PocketModemPeripheral( boolean advanced )
{ {
super( new ModemState(), advanced ); super( new ModemState(), advanced );
world = null;
position = new Vec3d( 0.0, 0.0, 0.0 );
} }
public void setLocation( World world, double x, double y, double z ) void setLocation( World world, Vec3d position )
{ {
position = new Vec3d( x, y, z ); this.position = position;
if( this.world != world ) this.world = world;
{
this.world = world;
switchNetwork();
}
} }
@Nonnull @Nonnull
@ -46,7 +40,7 @@ public class PocketModemPeripheral extends WirelessModemPeripheral
@Override @Override
public Vec3d getPosition() public Vec3d getPosition()
{ {
return world != null ? position : null; return position;
} }
@Override @Override

View File

@ -8,24 +8,17 @@ package dan200.computercraft.shared.pocket.peripherals;
import dan200.computercraft.api.peripheral.IPeripheral; import dan200.computercraft.api.peripheral.IPeripheral;
import dan200.computercraft.shared.peripheral.speaker.SpeakerPeripheral; import dan200.computercraft.shared.peripheral.speaker.SpeakerPeripheral;
import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.Vec3d;
import net.minecraft.world.World; import net.minecraft.world.World;
public class PocketSpeakerPeripheral extends SpeakerPeripheral public class PocketSpeakerPeripheral extends SpeakerPeripheral
{ {
private World world; private World world = null;
private BlockPos position; private Vec3d position = Vec3d.ZERO;
PocketSpeakerPeripheral()
{
super();
world = null;
position = BlockPos.ORIGIN;
}
void setLocation( World world, double x, double y, double z ) void setLocation( World world, double x, double y, double z )
{ {
position = new BlockPos( x, y, z ); position = new Vec3d( x, y, z );
this.world = world; this.world = world;
} }
@ -36,7 +29,7 @@ public class PocketSpeakerPeripheral extends SpeakerPeripheral
} }
@Override @Override
public BlockPos getPos() public Vec3d getPos()
{ {
return world != null ? position : null; return world != null ? position : null;
} }

View File

@ -20,6 +20,7 @@ import net.minecraft.client.renderer.block.model.ModelManager;
import net.minecraft.client.renderer.block.model.ModelResourceLocation; import net.minecraft.client.renderer.block.model.ModelResourceLocation;
import net.minecraft.util.ResourceLocation; import net.minecraft.util.ResourceLocation;
import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Vec3d;
import net.minecraft.world.World; import net.minecraft.world.World;
import net.minecraftforge.fml.relauncher.Side; import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly; import net.minecraftforge.fml.relauncher.SideOnly;
@ -47,21 +48,16 @@ public class TurtleSpeaker extends AbstractTurtleUpgrade
} }
@Override @Override
public BlockPos getPos() public Vec3d getPos()
{ {
return turtle.getPosition(); BlockPos pos = turtle.getPosition();
return new Vec3d( pos.getX() + 0.5, pos.getY() + 0.5, pos.getZ() + 0.5 );
} }
@Override @Override
public boolean equals( IPeripheral other ) public boolean equals( IPeripheral other )
{ {
if( other instanceof Peripheral ) return this == other || (other instanceof Peripheral && turtle == ((Peripheral) other).turtle);
{
Peripheral otherPeripheral = (Peripheral) other;
return otherPeripheral.turtle == turtle;
}
return false;
} }
} }