-
Notifications
You must be signed in to change notification settings - Fork 137
/
WDLPluginChannels.java
894 lines (780 loc) · 25.5 KB
/
WDLPluginChannels.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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
package wdl;
import io.netty.buffer.Unpooled;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import net.minecraft.client.Minecraft;
import net.minecraft.network.PacketBuffer;
import net.minecraft.network.play.client.C17PacketCustomPayload;
import net.minecraft.world.chunk.Chunk;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import com.google.common.collect.HashMultimap;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableMultimap;
import com.google.common.collect.Multimap;
import com.google.common.io.ByteArrayDataInput;
import com.google.common.io.ByteArrayDataOutput;
import com.google.common.io.ByteStreams;
/**
* World Downloader permission system implemented with Plugin Channels.
*
* This system is used to configure the mod, and disable certain features,
* at a server's decision. I've made this system because there already were
* other (more esoteric) methods of finding the mod, based off of forge and
* lightloader handshakes. I think that a system like this, where there are
* <em>degrees of control</em>, is better than one where the player is
* indiscriminately kicked. For instance, this system allows for permission
* requests, which would be hard to do with another mechanism.
*
* This system makes use of plugin channels (hence the class name). If you
* haven't read <a href="http://wiki.vg/Plugin_channels">their info</a>,
* they're a vanilla minecraft packet intended for mods. But they <em>do</em>
* need each channel to be REGISTERed before use, so the server does know
* when the mod is installed. As such, I actually did a second step and
* instead send a second packet when the data is ready to be received by the
* client, since mid-download permission changes can be problematic.
*
* Theoretically, these could be implemented with chat-based codes or even
* MOTD-based codes. However, I <em>really</em> do not like that system, as
* it is really rigid. So I chose this one, which is also highly expandable.
*
* This system is also used to fetch a few things from willing servers, mainly
* entity track distances so that things can be saved correctly.
*
* And yes, this is the fabled "backdoor" / "back door"; I don't like that term
* but people will call it that. I think it's the best possible system out
* of the available options (and doing nothing wouldn't work - as I said,
* there <em>are</em> weird ways of finding mods).
*
* <a href="http://wiki.vg/User:Pokechu22/World_downloader">Packet
* documentation is on wiki.vg</a>, if you're interested.
*/
public class WDLPluginChannels {
private static Logger logger = LogManager.getLogger();
/**
* Packets that have been received.
*/
private static HashSet<Integer> receivedPackets = new HashSet<Integer>();
/**
* Whether functions that the server is not aware of can be used.
* (Packet #0)
*/
private static boolean canUseFunctionsUnknownToServer = true;
/**
* Whether all players are allowed to download the world in general.
* If false, they aren't allowed, regardless of the other values below.
*/
private static boolean canDownloadInGeneral = true;
/**
* The distance from a player that WDL can save chunks.
*
* This is only used when {@link #canCacheChunks} is false.
*/
private static int saveRadius = -1;
/**
* Whether a player can cache chunks as they move. In essence, this means
* that if the value is true, the player can download the entire map while
* moving about, but if false, the player will only save the nearby chunks
* when they stop download.
*/
private static boolean canCacheChunks = true;
/**
* Whether or not a player can save entities in the map.
*/
private static boolean canSaveEntities = true;
/**
* Whether or not a player can save TileEntities in general.
* <br/>
* Chests and other containers also require {@link #canSaveContainers}.
*/
private static boolean canSaveTileEntities = true;
/**
* Whether a player can save containers that require opening to save their
* contents, such as chests. For this value to have meaning, the value of
* {@link #canSaveTileEntities} must also be true.
*/
private static boolean canSaveContainers = true;
/**
* Map of entity ranges.
*
* Key is the entity string, int is the range.
*/
private static Map<String, Integer> entityRanges =
new HashMap<String, Integer>();
/**
* Whether players can request permissions.
*
* With the default implementation, this is always <i>sent</i> as
* <code>true</code>. However, this needs to be sent for it to be useful -
* if the plugin does NOT send it, it does not support permission requests.
*/
private static boolean canRequestPermissions = false;
/**
* Message to display when requesting. If empty, nothing
* is displayed.
*/
private static String requestMessage = "";
/**
* Chunk overrides. Any chunk within a range is allowed to be downloaded in.
*/
private static Map<String, Multimap<String, ChunkRange>> chunkOverrides = new HashMap<String, Multimap<String, ChunkRange>>();
/**
* Active permission requests.
*/
private static Map<String, String> requests = new HashMap<String, String>();
/**
* Permission request fields that take boolean parameters.
*/
public static final List<String> BOOLEAN_REQUEST_FIELDS = Arrays.asList(
"downloadInGeneral", "cacheChunks", "saveEntities",
"saveTileEntities", "saveContainers", "getEntityRanges");
/**
* Permission request fields that take integer parameters.
*/
public static final List<String> INTEGER_REQUEST_FIELDS = Arrays.asList(
"saveRadius");
/**
* List of new chunk override requests.
*/
private static List<ChunkRange> chunkOverrideRequests = new ArrayList<ChunkRange>();
/**
* Checks whether players can use functions unknown to the server.
*/
public static boolean canUseFunctionsUnknownToServer() {
if (receivedPackets.contains(0)) {
return canUseFunctionsUnknownToServer;
} else {
return true;
}
}
/**
* Checks whether the player should be able to start download at all: Either
* {@link #canDownloadInGeneral()} is true, or the player has some chunks
* overridden.
*/
public static boolean canDownloadAtAll() {
if (hasChunkOverrides()) {
return true;
} else {
return canDownloadInGeneral();
}
}
/**
* Checks whether players are allowed to download in general (outside of
* overridden chunks).
*/
public static boolean canDownloadInGeneral() {
if (receivedPackets.contains(1)) {
return canDownloadInGeneral;
} else {
return canUseFunctionsUnknownToServer();
}
}
/**
* Checks if a chunk is within the saveRadius
* (and chunk caching is disabled).
*/
public static boolean canSaveChunk(Chunk chunk) {
if (isChunkOverridden(chunk)) {
return true;
}
if (!canDownloadInGeneral()) {
return false;
}
if (receivedPackets.contains(1)) {
if (!canCacheChunks && saveRadius >= 0) {
int distanceX = chunk.xPosition - WDL.thePlayer.chunkCoordX;
int distanceZ = chunk.zPosition - WDL.thePlayer.chunkCoordZ;
if (Math.abs(distanceX) > saveRadius ||
Math.abs(distanceZ) > saveRadius) {
return false;
}
}
return true;
} else {
return canUseFunctionsUnknownToServer();
}
}
/**
* Checks whether entities are allowed to be saved.
*/
public static boolean canSaveEntities() {
if (!canDownloadInGeneral()) {
return false;
}
if (receivedPackets.contains(1)) {
return canSaveEntities;
} else {
return canUseFunctionsUnknownToServer();
}
}
/**
* Checks whether entities are allowed to be saved in the given chunk.
*/
public static boolean canSaveEntities(Chunk chunk) {
if (isChunkOverridden(chunk)) {
return true;
}
return canSaveEntities();
}
/**
* Checks whether entities are allowed to be saved in the given chunk.
*/
public static boolean canSaveEntities(int chunkX, int chunkZ) {
if (isChunkOverridden(chunkX, chunkZ)) {
return true;
}
return canSaveEntities();
}
/**
* Checks whether a player can save tile entities.
*/
public static boolean canSaveTileEntities() {
if (!canDownloadInGeneral()) {
return false;
}
if (receivedPackets.contains(1)) {
return canSaveTileEntities;
} else {
return canUseFunctionsUnknownToServer();
}
}
/**
* Checks whether a player can save tile entities in the given chunk.
*/
public static boolean canSaveTileEntities(Chunk chunk) {
if (isChunkOverridden(chunk)) {
return true;
}
return canSaveTileEntities();
}
/**
* Checks whether a player can save tile entities in the given chunk.
*/
public static boolean canSaveTileEntities(int chunkX, int chunkZ) {
if (isChunkOverridden(chunkX, chunkZ)) {
return true;
}
return canSaveTileEntities();
}
/**
* Checks whether containers (such as chests) can be saved.
*/
public static boolean canSaveContainers() {
if (!canDownloadInGeneral()) {
return false;
}
if (!canSaveTileEntities()) {
return false;
}
if (receivedPackets.contains(1)) {
return canSaveContainers;
} else {
return canUseFunctionsUnknownToServer();
}
}
/**
* Checks whether containers (such as chests) can be saved.
*/
public static boolean canSaveContainers(Chunk chunk) {
if (isChunkOverridden(chunk)) {
return true;
}
return canSaveContainers();
}
/**
* Checks whether containers (such as chests) can be saved.
*/
public static boolean canSaveContainers(int chunkX, int chunkZ) {
if (isChunkOverridden(chunkX, chunkZ)) {
return true;
}
return canSaveContainers();
}
/**
* Checks whether maps (the map item, not the world itself) can be saved.
*/
public static boolean canSaveMaps() {
if (!canDownloadInGeneral()) {
return false;
}
//TODO: Better value than 'canSaveTileEntities'.
if (receivedPackets.contains(1)) {
return canSaveTileEntities;
} else {
return canUseFunctionsUnknownToServer();
}
}
/**
* Gets the server-set range for the given entity.
*
* @param entity The entity's name (via {@link EntityUtils#getEntityType}).
* @return The entity's range, or -1 if no data was recieved.
*/
public static int getEntityRange(String entity) {
if (!canSaveEntities(null)) {
return -1;
}
if (receivedPackets.contains(2)) {
if (entityRanges.containsKey(entity)) {
return entityRanges.get(entity);
} else {
return -1;
}
} else {
return -1;
}
}
/**
* Gets the save radius.
*
* Note that using {@link #canSaveChunk(Chunk)} is generally better
* as it handles most of the radius logic.
*
* @return {@link #saveRadius}.
*/
public static int getSaveRadius() {
return saveRadius;
}
/**
* Gets whether chunks can be cached.
*
* Note that using {@link #canSaveChunk(Chunk)} is generally better
* as it handles most of the radius logic.
*
* @return {@link #canCacheChunks}.
*/
public static boolean canCacheChunks() {
return canCacheChunks;
}
/**
* Checks if the server-set entity range is configured.
*/
public static boolean hasServerEntityRange() {
return receivedPackets.contains(2) && entityRanges.size() > 0;
}
public static Map<String, Integer> getEntityRanges() {
return new HashMap<String, Integer>(entityRanges);
}
/**
* Gets whether permissions are available.
*/
public static boolean hasPermissions() {
return receivedPackets != null && !receivedPackets.isEmpty();
}
/**
* Gets whether permissions are available.
*/
public static boolean canRequestPermissions() {
return receivedPackets.contains(3) && canRequestPermissions;
}
/**
* Gets the request message.
* @return The {@link #requestMessage}.
*/
public static String getRequestMessage() {
if (receivedPackets.contains(3)) {
return requestMessage;
} else {
return null;
}
}
/**
* Is the given chunk part of a chunk override?
*/
public static boolean isChunkOverridden(Chunk chunk) {
if (chunk == null) {
return false;
}
return isChunkOverridden(chunk.xPosition, chunk.zPosition);
}
/**
* Is the given chunk location part of a chunk override?
*/
public static boolean isChunkOverridden(int x, int z) {
for (Multimap<String, ChunkRange> map : chunkOverrides.values()) {
for (ChunkRange range : map.values()) {
if (x >= range.x1 &&
x <= range.x2 &&
z >= range.z1 &&
z <= range.z2) {
return true;
}
}
}
return false;
}
/**
* Are there any chunk overrides present?
*/
public static boolean hasChunkOverrides() {
if (!receivedPackets.contains(4)) {
// XXX It's possible that some implementations may not send
// packet 4, but still send ranges. If so, that may lead to issues.
// But right now, I'm not checking that.
return false;
}
if (chunkOverrides == null || chunkOverrides.isEmpty()) {
return false;
}
for (Multimap<String, ChunkRange> m : chunkOverrides.values()) {
if (!m.isEmpty()) {
return true;
}
}
return false;
}
/**
* Gets an immutable copy of the {@link #chunkOverrides} map.
*/
public static Map<String, Multimap<String, ChunkRange>> getChunkOverrides() {
Map<String, Multimap<String, ChunkRange>> returned = new
HashMap<String, Multimap<String,ChunkRange>>();
for (Map.Entry<String, Multimap<String, ChunkRange>> e : chunkOverrides
.entrySet()) {
// Create a copy of the given map.
Multimap<String, ChunkRange> map = ImmutableMultimap.copyOf(e.getValue());
returned.put(e.getKey(), map);
}
return ImmutableMap.copyOf(returned);
}
/**
* Create a new permission request.
* @param key The key for the request.
* @param value The wanted value.
*/
public static void addRequest(String key, String value) {
if (!isValidRequest(key, value)) {
return;
}
requests.put(key, value);
}
/**
* Gets an immutable copy of the current requests.
*/
public static Map<String, String> getRequests() {
return ImmutableMap.copyOf(requests);
}
/**
* Is the given set of values valid for the given request?
*
* Handles checking if the key exists and if the value is valid.
*
* @param key The key for the request.
* @param value The wanted value.
*/
public static boolean isValidRequest(String key, String value) {
if (key == null || value == null) {
return false;
}
if (BOOLEAN_REQUEST_FIELDS.contains(key)) {
return value.equals("true") || value.equals("false");
} else if (INTEGER_REQUEST_FIELDS.contains(key)) {
try {
Integer.parseInt(value);
return true;
} catch (NumberFormatException e) {
return false;
}
}
return false;
}
/**
* Gets the current list of chunk override requests.
*/
public static List<ChunkRange> getChunkOverrideRequests() {
return ImmutableList.copyOf(chunkOverrideRequests);
}
/**
* Adds a new chunk override request for the given range.
*/
public static void addChunkOverrideRequest(ChunkRange range) {
chunkOverrideRequests.add(range);
}
/**
* Sends the current requests to the server.
*/
public static void sendRequests() {
if (requests.isEmpty() && chunkOverrideRequests.isEmpty()) {
return;
}
ByteArrayDataOutput output = ByteStreams.newDataOutput();
output.writeUTF("REQUEST REASON WILL GO HERE"); //TODO
output.writeInt(requests.size());
for (Map.Entry<String, String> request : requests.entrySet()) {
output.writeUTF(request.getKey());
output.writeUTF(request.getValue());
}
output.writeInt(chunkOverrideRequests.size());
for (ChunkRange range : chunkOverrideRequests) {
range.writeToOutput(output);
}
PacketBuffer requestBuffer = new PacketBuffer(Unpooled.buffer());
requestBuffer.writeBytes(output.toByteArray());
C17PacketCustomPayload requestPacket = new C17PacketCustomPayload(
"WDL|REQUEST", requestBuffer);
Minecraft.getMinecraft().getNetHandler().addToSendQueue(requestPacket);
}
/**
* Event that is called when the world is loaded.
* Sets the default values, and then asks the server to give the
* correct ones.
*/
static void onWorldLoad() {
Minecraft minecraft = Minecraft.getMinecraft();
receivedPackets = new HashSet<Integer>();
requests = new HashMap<String, String>();
chunkOverrideRequests = new ArrayList<ChunkRange>();
canUseFunctionsUnknownToServer = true;
WDLMessages.chatMessageTranslated(
WDLMessageTypes.PLUGIN_CHANNEL_MESSAGE,
"wdl.messages.permissions.init");
// Register the WDL messages.
PacketBuffer registerPacketBuffer = new PacketBuffer(Unpooled.buffer());
// Done like this because "buffer.writeString" doesn't do the propper
// null-terminated strings.
registerPacketBuffer.writeBytes(new byte[] {
'W', 'D', 'L', '|', 'I', 'N', 'I', 'T', '\0',
'W', 'D', 'L', '|', 'C', 'O', 'N', 'T', 'R', 'O', 'L', '\0',
'W', 'D', 'L', '|', 'R', 'E', 'Q', 'U', 'E', 'S', 'T', '\0' });
C17PacketCustomPayload registerPacket = new C17PacketCustomPayload(
"REGISTER", registerPacketBuffer);
minecraft.getNetHandler().addToSendQueue(registerPacket);
// Send the init message.
C17PacketCustomPayload initPacket;
try {
initPacket = new C17PacketCustomPayload("WDL|INIT",
new PacketBuffer(Unpooled.copiedBuffer(WDL.VERSION
.getBytes("UTF-8"))));
} catch (UnsupportedEncodingException e) {
WDLMessages.chatMessageTranslated(WDLMessageTypes.ERROR,
"wdl.messages.generalError.noUTF8", e);
initPacket = new C17PacketCustomPayload("WDL|INIT",
new PacketBuffer(Unpooled.buffer()));
}
minecraft.getNetHandler().addToSendQueue(initPacket);
}
static void onPluginChannelPacket(String channel, byte[] bytes) {
if ("WDL|CONTROL".equals(channel)) {
ByteArrayDataInput input = ByteStreams.newDataInput(bytes);
int section = input.readInt();
receivedPackets.add(section);
switch (section) {
case 0:
canUseFunctionsUnknownToServer = input.readBoolean();
WDLMessages.chatMessageTranslated(
WDLMessageTypes.PLUGIN_CHANNEL_MESSAGE,
"wdl.messages.permissions.packet0",
canUseFunctionsUnknownToServer);
break;
case 1:
canDownloadInGeneral = input.readBoolean();
saveRadius = input.readInt();
canCacheChunks = input.readBoolean();
canSaveEntities = input.readBoolean();
canSaveTileEntities = input.readBoolean();
canSaveContainers = input.readBoolean();
WDLMessages.chatMessageTranslated(
WDLMessageTypes.PLUGIN_CHANNEL_MESSAGE,
"wdl.messages.permissions.packet1",
canDownloadInGeneral, saveRadius,
canCacheChunks, canSaveEntities,
canSaveTileEntities, canSaveContainers);
//Cancel a download if it is occurring.
if (!canDownloadInGeneral) {
if (WDL.downloading) {
WDLMessages.chatMessageTranslated(
WDLMessageTypes.ERROR,
"wdl.messages.generalError.forbidden");
WDL.cancelDownload();
}
}
break;
case 2:
entityRanges.clear();
int count = input.readInt();
for (int i = 0; i < count; i++) {
String name = input.readUTF();
int range = input.readInt();
entityRanges.put(name, range);
}
WDLMessages.chatMessageTranslated(
WDLMessageTypes.PLUGIN_CHANNEL_MESSAGE,
"wdl.messages.permissions.packet2",
entityRanges.size());
break;
case 3:
canRequestPermissions = input.readBoolean();
requestMessage = input.readUTF();
WDLMessages.chatMessageTranslated(
WDLMessageTypes.PLUGIN_CHANNEL_MESSAGE,
"wdl.messages.permissions.packet3",
canRequestPermissions, requestMessage.length(),
Integer.toHexString(requestMessage.hashCode()));
// Don't include the exact message because it's too long and would be spammy.
break;
case 4:
chunkOverrides.clear();
int numRangeGroups = input.readInt();
int totalRanges = 0;
for (int i = 0; i < numRangeGroups; i++) {
String groupName = input.readUTF();
int groupSize = input.readInt();
Multimap<String, ChunkRange> ranges = HashMultimap
.<String, ChunkRange> create();
for (int j = 0; j < groupSize; j++) {
ChunkRange range = ChunkRange.readFromInput(input);
ranges.put(range.tag, range);
}
chunkOverrides.put(groupName, ranges);
totalRanges += groupSize;
}
WDLMessages.chatMessageTranslated(
WDLMessageTypes.PLUGIN_CHANNEL_MESSAGE,
"wdl.messages.permissions.packet4",
numRangeGroups, totalRanges);
break;
case 5:
String groupToEdit = input.readUTF();
boolean replaceGroups = input.readBoolean();
int numNewGroups = input.readInt();
Multimap<String, ChunkRange> newRanges = HashMultimap
.<String, ChunkRange> create();
if (!replaceGroups) {
newRanges.putAll(chunkOverrides.get(groupToEdit));
}
for (int i = 0; i < numNewGroups; i++) {
ChunkRange range = ChunkRange.readFromInput(input);
newRanges.put(range.tag, range);
}
chunkOverrides.put(groupToEdit, newRanges);
if (replaceGroups) {
WDLMessages.chatMessageTranslated(
WDLMessageTypes.PLUGIN_CHANNEL_MESSAGE,
"wdl.messages.permissions.packet5.set",
numNewGroups, groupToEdit);
} else {
WDLMessages.chatMessageTranslated(
WDLMessageTypes.PLUGIN_CHANNEL_MESSAGE,
"wdl.messages.permissions.packet5.added",
numNewGroups, groupToEdit);
}
break;
case 6:
String groupToChangeTagsFor = input.readUTF();
int numTags = input.readInt();
String[] tags = new String[numTags];
for (int i = 0; i < numTags; i++) {
tags[i] = input.readUTF();
}
int oldCount = 0;
for (String tag : tags) {
oldCount += chunkOverrides.get(groupToChangeTagsFor)
.get(tag).size();
chunkOverrides.get(groupToChangeTagsFor).removeAll(tag);
}
WDLMessages.chatMessageTranslated(
WDLMessageTypes.PLUGIN_CHANNEL_MESSAGE,
"wdl.messages.permissions.packet6",
oldCount, groupToChangeTagsFor, Arrays.toString(tags));
break;
case 7:
String groupToSetTagFor = input.readUTF();
String tag = input.readUTF();
int numNewRanges = input.readInt();
Collection<ChunkRange> oldRanges = chunkOverrides.get(
groupToSetTagFor).removeAll(tag);
int numRangesRemoved = oldRanges.size();
for (int i = 0; i < numNewRanges; i++) {
//TODO: Ensure that the range has the right tag.
chunkOverrides.get(groupToSetTagFor).put(tag,
ChunkRange.readFromInput(input));
}
WDLMessages.chatMessageTranslated(
WDLMessageTypes.PLUGIN_CHANNEL_MESSAGE,
"wdl.messages.permissions.packet7",
numRangesRemoved, groupToSetTagFor, tag, numNewRanges);
break;
default:
WDLMessages.chatMessageTranslated(
WDLMessageTypes.PLUGIN_CHANNEL_MESSAGE,
"wdl.messages.permissions.unknownPacket", section);
StringBuilder messageBuilder = new StringBuilder();
for (byte b : bytes) {
messageBuilder.append(b).append(' ');
}
logger.info(messageBuilder.toString());
}
}
}
/**
* A range of chunks.
*/
public static class ChunkRange {
public ChunkRange(String tag, int x1, int z1, int x2, int z2) {
this.tag = tag;
// Ensure that the order is correct
if (x1 > x2) {
this.x1 = x2;
this.x2 = x1;
} else {
this.x1 = x1;
this.x2 = x2;
}
if (z1 > z2) {
this.z1 = z2;
this.z2 = z1;
} else {
this.z1 = z1;
this.z2 = z2;
}
}
/**
* The tag of this chunk range.
*/
public final String tag;
/**
* Range of coordinates. x1 will never be higher than x2, as will z1
* with z2.
*/
public final int x1, z1, x2, z2;
/**
* Reads and creates a new ChunkRange from the given
* {@link ByteArrayDataInput}.
*/
public static ChunkRange readFromInput(ByteArrayDataInput input) {
String tag = input.readUTF();
int x1 = input.readInt();
int z1 = input.readInt();
int x2 = input.readInt();
int z2 = input.readInt();
return new ChunkRange(tag, x1, z1, x2, z2);
}
/**
* Writes this ChunkRange to the given {@link ByteArrayDataOutput}.
*
* Note that I expect most serverside implementations will ignore the
* tag, but it still is included for clarity. The value in it can be
* anything so long as it is not null - an empty string will do.
*/
public void writeToOutput(ByteArrayDataOutput output) {
output.writeUTF(this.tag);
output.writeInt(this.x1);
output.writeInt(this.z1);
output.writeInt(this.x2);
output.writeInt(this.z2);
}
@Override
public String toString() {
return "ChunkRange [tag=" + tag + ", x1=" + x1 + ", z1=" + z1
+ ", x2=" + x2 + ", z2=" + z2 + "]";
}
}
}