/
ModemPeripheral.java
246 lines (215 loc) · 7.05 KB
/
ModemPeripheral.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
/*
* This file is part of ComputerCraft - http://www.computercraft.info
* Copyright Daniel Ratcliffe, 2011-2021. Do not distribute without permission.
* Send enquiries to dratcliffe@gmail.com
*/
package dan200.computercraft.shared.peripheral.modem;
import dan200.computercraft.api.lua.LuaException;
import dan200.computercraft.api.lua.LuaFunction;
import dan200.computercraft.api.network.IPacketNetwork;
import dan200.computercraft.api.network.IPacketReceiver;
import dan200.computercraft.api.network.IPacketSender;
import dan200.computercraft.api.network.Packet;
import dan200.computercraft.api.peripheral.IComputerAccess;
import dan200.computercraft.api.peripheral.IPeripheral;
import net.minecraft.util.math.vector.Vector3d;
import net.minecraft.world.World;
import javax.annotation.Nonnull;
import java.util.HashSet;
import java.util.Set;
/**
* The modem peripheral allows you to send messages between computers.
*
* @cc.module modem
*/
public abstract class ModemPeripheral implements IPeripheral, IPacketSender, IPacketReceiver
{
private IPacketNetwork network;
private final Set<IComputerAccess> computers = new HashSet<>( 1 );
private final ModemState state;
protected ModemPeripheral( ModemState state )
{
this.state = state;
}
public ModemState getModemState()
{
return state;
}
private synchronized void setNetwork( IPacketNetwork network )
{
if( this.network == network ) return;
// Leave old network
if( this.network != null ) this.network.removeReceiver( this );
// Set new network
this.network = network;
// Join new network
if( this.network != null ) this.network.addReceiver( this );
}
public void destroy()
{
setNetwork( null );
}
@Override
public void receiveSameDimension( @Nonnull Packet packet, double distance )
{
if( packet.getSender() == this || !state.isOpen( packet.getChannel() ) ) return;
synchronized( computers )
{
for( IComputerAccess computer : computers )
{
computer.queueEvent( "modem_message",
computer.getAttachmentName(), packet.getChannel(), packet.getReplyChannel(), packet.getPayload(), distance );
}
}
}
@Override
public void receiveDifferentDimension( @Nonnull Packet packet )
{
if( packet.getSender() == this || !state.isOpen( packet.getChannel() ) ) return;
synchronized( computers )
{
for( IComputerAccess computer : computers )
{
computer.queueEvent( "modem_message",
computer.getAttachmentName(), packet.getChannel(), packet.getReplyChannel(), packet.getPayload() );
}
}
}
protected abstract IPacketNetwork getNetwork();
@Nonnull
@Override
public String getType()
{
return "modem";
}
private static int parseChannel( int channel ) throws LuaException
{
if( channel < 0 || channel > 65535 ) throw new LuaException( "Expected number in range 0-65535" );
return channel;
}
/**
* Open a channel on a modem. A channel must be open in order to receive messages. Modems can have up to 128
* channels open at one time.
*
* @param channel The channel to open. This must be a number between 0 and 65535.
* @throws LuaException If the channel is out of range.
* @throws LuaException If there are too many open channels.
*/
@LuaFunction
public final void open( int channel ) throws LuaException
{
state.open( parseChannel( channel ) );
}
/**
* Check if a channel is open.
*
* @param channel The channel to check.
* @return Whether the channel is open.
* @throws LuaException If the channel is out of range.
*/
@LuaFunction
public final boolean isOpen( int channel ) throws LuaException
{
return state.isOpen( parseChannel( channel ) );
}
/**
* Close an open channel, meaning it will no longer receive messages.
*
* @param channel The channel to close.
* @throws LuaException If the channel is out of range.
*/
@LuaFunction
public final void close( int channel ) throws LuaException
{
state.close( parseChannel( channel ) );
}
/**
* Close all open channels.
*/
@LuaFunction
public final void closeAll()
{
state.closeAll();
}
/**
* Sends a modem message on a certain channel. Modems listening on the channel will queue a {@code modem_message}
* event on adjacent computers.
*
* <blockquote><strong>Note:</strong> The channel does not need be open to send a message.</blockquote>
*
* @param channel The channel to send messages on.
* @param replyChannel The channel that responses to this message should be sent on.
* @param payload The object to send. This can be a boolean, string, number, or table.
* @throws LuaException If the channel is out of range.
*/
@LuaFunction
public final void transmit( int channel, int replyChannel, Object payload ) throws LuaException
{
parseChannel( channel );
parseChannel( replyChannel );
World world = getWorld();
Vector3d position = getPosition();
IPacketNetwork network = this.network;
if( world == null || position == null || network == null ) return;
Packet packet = new Packet( channel, replyChannel, payload, this );
if( isInterdimensional() )
{
network.transmitInterdimensional( packet );
}
else
{
network.transmitSameDimension( packet, getRange() );
}
}
/**
* Determine if this is a wired or wireless modem.
*
* Some methods (namely those dealing with wired networks and remote peripherals) are only available on wired
* modems.
*
* @return {@code true} if this is a wireless modem.
*/
@LuaFunction
public final boolean isWireless()
{
IPacketNetwork network = this.network;
return network != null && network.isWireless();
}
@Override
public synchronized void attach( @Nonnull IComputerAccess computer )
{
synchronized( computers )
{
computers.add( computer );
}
setNetwork( getNetwork() );
}
@Override
public synchronized void detach( @Nonnull IComputerAccess computer )
{
boolean empty;
synchronized( computers )
{
computers.remove( computer );
empty = computers.isEmpty();
}
if( empty ) setNetwork( null );
}
@Nonnull
@Override
public String getSenderID()
{
synchronized( computers )
{
if( computers.size() != 1 )
{
return "unknown";
}
else
{
IComputerAccess computer = computers.iterator().next();
return computer.getID() + "_" + computer.getAttachmentName();
}
}
}
}