-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMessage.java
More file actions
162 lines (149 loc) · 5.61 KB
/
Copy pathMessage.java
File metadata and controls
162 lines (149 loc) · 5.61 KB
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
package de.minebench.syncinv.messenger;
/*
* SyncInv
* Copyright (c) 2021 Max Lee aka Phoenix616 (max@themoep.de)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import org.bukkit.configuration.InvalidConfigurationException;
import org.bukkit.util.io.BukkitObjectInputStream;
import org.bukkit.util.io.BukkitObjectOutputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import java.util.ArrayDeque;
import java.util.Collections;
import java.util.Queue;
public class Message {
public static final int VERSION = 4;
private final String sender;
private final long id;
private final MessageType type;
private final Queue<Object> data = new ArrayDeque<>();
/**
* A Message of a certain type. Optionally with some data
*
* @param sender The sender which sent the message
* @param id The transaction ID this Message is associated with
* @param type The type of the message
* @param objects The data, in the order that it should be sent
* @throws IllegalArgumentException when the amount of Objects given didn't match the MessageType requirements
*/
public Message(String sender, long id, MessageType type, Object... objects) {
if (objects.length < type.getArgCount()) {
throw new IllegalArgumentException(type + " requires at least " + type.getArgCount() + " arguments. Only " + objects.length + " were given!");
}
this.sender = sender;
this.id = id;
this.type = type;
if (objects.length > 0) {
Collections.addAll(data, objects);
}
}
/**
* Get the sender which sent the message
* @return The name of the sender
*/
public String getSender() {
return this.sender;
}
/**
* Get transaction ID this Message is associated with
* @return The transaction ID
*/
public long getId() {
return this.id;
}
/**
* Get the type of the message
* @return The type of the message
*/
public MessageType getType() {
return this.type;
}
/**
* The data of the message
* @return The data
*/
public Queue<Object> getData() {
return this.data;
}
/**
* Read the first object in this message
* @return The first object
*/
public Object read() {
return data.poll();
}
/**
* Generate a byte array out of the data of this message
* @return The generated byte array (starts with the sender
* then the type ordinal, then the amount
* of data being sent and each data object);
* an empty one if an error occurred
*/
public byte[] toByteArray() {
try (ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutput out = new BukkitObjectOutputStream(bos)) {
out.writeInt(VERSION);
out.writeUTF(getSender());
out.writeLong(id);
out.writeUTF(getType().toString());
out.writeInt(data.size());
for (Object o : data) {
out.writeObject(o);
}
out.flush();
return bos.toByteArray();
} catch (IOException e) {
e.printStackTrace();
}
return new byte[0];
}
/**
* Get the message from a bytearray generated as described in the toByteArray method
* @param bytes The bytes
* @return The Message object
* @throws IOException
* @throws IllegalArgumentException When the message type is not supported
* @throws ClassNotFoundException
* @throws InvalidConfigurationException If the data is invalid
* @throws VersionMismatchException If the received message is of a different version than it can accept
*/
public static Message fromByteArray(byte[] bytes) throws IOException, IllegalArgumentException, ClassNotFoundException, InvalidConfigurationException, VersionMismatchException {
try (ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
ObjectInput in = new BukkitObjectInputStream(bis)) {
int version = in.readInt();
if (version != VERSION) {
throw new VersionMismatchException(version, VERSION, "The received message is of version " + version + " while this plugin expects version " + VERSION);
}
String sender = in.readUTF();
long id = in.readLong();
MessageType type = MessageType.valueOf(in.readUTF());
Object[] data = new Object[in.readInt()];
for (int i = 0; i < data.length; i++) {
data[i] = in.readObject();
}
return new Message(sender, id, type, data);
}
}
/**
* @return a Sting representation of this message
*/
public String toString() {
return "Message(sender=" + this.getSender() + ", id=" + this.getId() + ", type=" + this.getType() + ", data=" + this.getData() + ")";
}
}