From 36e0dcbad01914b676fe9ab68c62b61d342164ad Mon Sep 17 00:00:00 2001 From: Jonathan Coates Date: Wed, 6 Oct 2021 18:38:51 +0100 Subject: [PATCH] Change network packet to a record Look at all that code we can delete! --- .../computercraft/api/network/Packet.java | 111 ++---------------- .../peripheral/modem/ModemPeripheral.java | 8 +- .../modem/wireless/WirelessNetwork.java | 2 +- .../shared/wired/WiredNetwork.java | 4 +- .../computercraft/shared/wired/WiredNode.java | 4 +- 5 files changed, 21 insertions(+), 108 deletions(-) diff --git a/src/main/java/dan200/computercraft/api/network/Packet.java b/src/main/java/dan200/computercraft/api/network/Packet.java index 7bd159f8b..5d1854120 100644 --- a/src/main/java/dan200/computercraft/api/network/Packet.java +++ b/src/main/java/dan200/computercraft/api/network/Packet.java @@ -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}. * + * @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 class Packet +public record Packet( + int channel, + int replyChannel, + Object payload, + IPacketSender sender +) { - 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. - */ - 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; - } } diff --git a/src/main/java/dan200/computercraft/shared/peripheral/modem/ModemPeripheral.java b/src/main/java/dan200/computercraft/shared/peripheral/modem/ModemPeripheral.java index 038a06669..e312c43ff 100644 --- a/src/main/java/dan200/computercraft/shared/peripheral/modem/ModemPeripheral.java +++ b/src/main/java/dan200/computercraft/shared/peripheral/modem/ModemPeripheral.java @@ -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() ); } } } diff --git a/src/main/java/dan200/computercraft/shared/peripheral/modem/wireless/WirelessNetwork.java b/src/main/java/dan200/computercraft/shared/peripheral/modem/wireless/WirelessNetwork.java index ad164e871..c1f06c922 100644 --- a/src/main/java/dan200/computercraft/shared/peripheral/modem/wireless/WirelessNetwork.java +++ b/src/main/java/dan200/computercraft/shared/peripheral/modem/wireless/WirelessNetwork.java @@ -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 diff --git a/src/main/java/dan200/computercraft/shared/wired/WiredNetwork.java b/src/main/java/dan200/computercraft/shared/wired/WiredNetwork.java index d38011703..34db6a87f 100644 --- a/src/main/java/dan200/computercraft/shared/wired/WiredNetwork.java +++ b/src/main/java/dan200/computercraft/shared/wired/WiredNetwork.java @@ -325,9 +325,9 @@ public final class WiredNetwork implements IWiredNetwork TreeSet 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 ); } diff --git a/src/main/java/dan200/computercraft/shared/wired/WiredNode.java b/src/main/java/dan200/computercraft/shared/wired/WiredNode.java index 3900de297..c6426ca18 100644 --- a/src/main/java/dan200/computercraft/shared/wired/WiredNode.java +++ b/src/main/java/dan200/computercraft/shared/wired/WiredNode.java @@ -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" ); }