mirror of
https://github.com/SquidDev-CC/CC-Tweaked
synced 2024-11-19 00:04:53 +00:00
Change network packet to a record
Look at all that code we can delete!
This commit is contained in:
parent
0b5fe990e5
commit
36e0dcbad0
@ -5,113 +5,26 @@
|
||||
*/
|
||||
package dan200.computercraft.api.network;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import javax.annotation.Nullable;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* Represents a packet which may be sent across a {@link IPacketNetwork}.
|
||||
*
|
||||
* @see IPacketSender
|
||||
* @see IPacketNetwork#transmitSameDimension(Packet, double)
|
||||
* @see IPacketNetwork#transmitInterdimensional(Packet)
|
||||
* @see IPacketReceiver#receiveDifferentDimension(Packet)
|
||||
* @see IPacketReceiver#receiveSameDimension(Packet, double)
|
||||
*/
|
||||
public class Packet
|
||||
{
|
||||
private final int channel;
|
||||
private final int replyChannel;
|
||||
private final Object payload;
|
||||
|
||||
private final IPacketSender sender;
|
||||
|
||||
/**
|
||||
* Create a new packet, ready for transmitting across the network.
|
||||
*
|
||||
* @param channel The channel to send the packet along. Receiving devices should only process packets from on
|
||||
* channels they are listening to.
|
||||
* @param replyChannel The channel to reply on.
|
||||
* @param payload The contents of this packet. This should be a "valid" Lua object, safe for queuing as an
|
||||
* event or returning from a peripheral call.
|
||||
* @param sender The object which sent this packet.
|
||||
* @see IPacketSender
|
||||
* @see IPacketNetwork#transmitSameDimension(Packet, double)
|
||||
* @see IPacketNetwork#transmitInterdimensional(Packet)
|
||||
* @see IPacketReceiver#receiveDifferentDimension(Packet)
|
||||
* @see IPacketReceiver#receiveSameDimension(Packet, double)
|
||||
*/
|
||||
public Packet( int channel, int replyChannel, @Nullable Object payload, @Nonnull IPacketSender sender )
|
||||
{
|
||||
Objects.requireNonNull( sender, "sender cannot be null" );
|
||||
|
||||
this.channel = channel;
|
||||
this.replyChannel = replyChannel;
|
||||
this.payload = payload;
|
||||
this.sender = sender;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the channel this packet is sent along. Receivers should generally only process packets from on channels they
|
||||
* are listening to.
|
||||
*
|
||||
* @return This packet's channel.
|
||||
*/
|
||||
public int getChannel()
|
||||
{
|
||||
return channel;
|
||||
}
|
||||
|
||||
/**
|
||||
* The channel to reply on. Objects which will reply should send it along this channel.
|
||||
*
|
||||
* @return This channel to reply on.
|
||||
*/
|
||||
public int getReplyChannel()
|
||||
{
|
||||
return replyChannel;
|
||||
}
|
||||
|
||||
/**
|
||||
* The actual data of this packet. This should be a "valid" Lua object, safe for queuing as an
|
||||
* event or returning from a peripheral call.
|
||||
*
|
||||
* @return The packet's payload
|
||||
*/
|
||||
@Nullable
|
||||
public Object getPayload()
|
||||
{
|
||||
return payload;
|
||||
}
|
||||
|
||||
/**
|
||||
* The object which sent this message.
|
||||
*
|
||||
* @return The sending object.
|
||||
*/
|
||||
@Nonnull
|
||||
public IPacketSender getSender()
|
||||
{
|
||||
return sender;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals( Object o )
|
||||
{
|
||||
if( this == o ) return true;
|
||||
if( o == null || getClass() != o.getClass() ) return false;
|
||||
|
||||
Packet packet = (Packet) o;
|
||||
|
||||
if( channel != packet.channel ) return false;
|
||||
if( replyChannel != packet.replyChannel ) return false;
|
||||
if( !Objects.equals( payload, packet.payload ) ) return false;
|
||||
return sender.equals( packet.sender );
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode()
|
||||
{
|
||||
int result;
|
||||
result = channel;
|
||||
result = 31 * result + replyChannel;
|
||||
result = 31 * result + (payload != null ? payload.hashCode() : 0);
|
||||
result = 31 * result + sender.hashCode();
|
||||
return result;
|
||||
}
|
||||
public record Packet(
|
||||
int channel,
|
||||
int replyChannel,
|
||||
Object payload,
|
||||
IPacketSender sender
|
||||
)
|
||||
{
|
||||
}
|
||||
|
@ -63,14 +63,14 @@ public abstract class ModemPeripheral implements IPeripheral, IPacketSender, IPa
|
||||
@Override
|
||||
public void receiveSameDimension( @Nonnull Packet packet, double distance )
|
||||
{
|
||||
if( packet.getSender() == this || !state.isOpen( packet.getChannel() ) ) return;
|
||||
if( packet.sender() == this || !state.isOpen( packet.channel() ) ) return;
|
||||
|
||||
synchronized( computers )
|
||||
{
|
||||
for( IComputerAccess computer : computers )
|
||||
{
|
||||
computer.queueEvent( "modem_message",
|
||||
computer.getAttachmentName(), packet.getChannel(), packet.getReplyChannel(), packet.getPayload(), distance );
|
||||
computer.getAttachmentName(), packet.channel(), packet.replyChannel(), packet.payload(), distance );
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -78,14 +78,14 @@ public abstract class ModemPeripheral implements IPeripheral, IPacketSender, IPa
|
||||
@Override
|
||||
public void receiveDifferentDimension( @Nonnull Packet packet )
|
||||
{
|
||||
if( packet.getSender() == this || !state.isOpen( packet.getChannel() ) ) return;
|
||||
if( packet.sender() == this || !state.isOpen( packet.channel() ) ) return;
|
||||
|
||||
synchronized( computers )
|
||||
{
|
||||
for( IComputerAccess computer : computers )
|
||||
{
|
||||
computer.queueEvent( "modem_message",
|
||||
computer.getAttachmentName(), packet.getChannel(), packet.getReplyChannel(), packet.getPayload() );
|
||||
computer.getAttachmentName(), packet.channel(), packet.replyChannel(), packet.payload() );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -63,7 +63,7 @@ public class WirelessNetwork implements IPacketNetwork
|
||||
|
||||
private static void tryTransmit( IPacketReceiver receiver, Packet packet, double range, boolean interdimensional )
|
||||
{
|
||||
IPacketSender sender = packet.getSender();
|
||||
IPacketSender sender = packet.sender();
|
||||
if( receiver.getLevel() == sender.getLevel() )
|
||||
{
|
||||
double receiveRange = Math.max( range, receiver.getRange() ); // Ensure range is symmetrical
|
||||
|
@ -325,9 +325,9 @@ public final class WiredNetwork implements IWiredNetwork
|
||||
TreeSet<TransmitPoint> transmitTo = new TreeSet<>();
|
||||
|
||||
{
|
||||
TransmitPoint startEntry = start.element.getLevel() != packet.getSender().getLevel()
|
||||
TransmitPoint startEntry = start.element.getLevel() != packet.sender().getLevel()
|
||||
? new TransmitPoint( start, Double.POSITIVE_INFINITY, true )
|
||||
: new TransmitPoint( start, start.element.getPosition().distanceTo( packet.getSender().getPosition() ), false );
|
||||
: new TransmitPoint( start, start.element.getPosition().distanceTo( packet.sender().getPosition() ), false );
|
||||
points.put( start, startEntry );
|
||||
transmitTo.add( startEntry );
|
||||
}
|
||||
|
@ -80,7 +80,7 @@ public final class WiredNode implements IWiredNode
|
||||
public void transmitSameDimension( @Nonnull Packet packet, double range )
|
||||
{
|
||||
Objects.requireNonNull( packet, "packet cannot be null" );
|
||||
if( !(packet.getSender() instanceof IWiredSender) || ((IWiredSender) packet.getSender()).getNode() != this )
|
||||
if( !(packet.sender() instanceof IWiredSender) || ((IWiredSender) packet.sender()).getNode() != this )
|
||||
{
|
||||
throw new IllegalArgumentException( "Sender is not in the network" );
|
||||
}
|
||||
@ -100,7 +100,7 @@ public final class WiredNode implements IWiredNode
|
||||
public void transmitInterdimensional( @Nonnull Packet packet )
|
||||
{
|
||||
Objects.requireNonNull( packet, "packet cannot be null" );
|
||||
if( !(packet.getSender() instanceof IWiredSender) || ((IWiredSender) packet.getSender()).getNode() != this )
|
||||
if( !(packet.sender() instanceof IWiredSender) || ((IWiredSender) packet.sender()).getNode() != this )
|
||||
{
|
||||
throw new IllegalArgumentException( "Sender is not in the network" );
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user