/
AirstrikeMessageToServer.java
156 lines (134 loc) · 5.23 KB
/
AirstrikeMessageToServer.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
package minecraftbyexample.mbe60_network_messages;
import net.minecraft.entity.EntityType;
import net.minecraft.network.PacketBuffer;
import net.minecraft.util.math.vector.Vector3d;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.util.Random;
/**
* This Network Message is sent from the client to the server, to tell it to spawn projectiles at a particular location.
* Typical usage:
* PREREQUISITES:
* have previously setup SimpleChannel, registered the message class and the handler
*
* 1) User creates an AirStrikeMessageToServer(projectile, targetCoordinates)
* 2) simpleChannel.sendToServer(airstrikeMessageToServer);
* 3) Forge network code calls airstrikeMessageToServer.encode() to copy the message member variables to a PacketBuffer, ready for sending
* ... bytes are sent over the network and arrive at the server....
* 4) Forge network code calls airstrikeMessageToServer.decode() to recreate the airstrickeMessageToServer instance by reading
* from the PacketBuffer into the member variables
* 6) the handler.onMessage(airStrikeMessageToServer) is called to process the message
*
* User: The Grey Ghost
* Date: 15/01/2015
*/
public class AirstrikeMessageToServer
{
public AirstrikeMessageToServer(Projectile i_projectile, Vector3d i_targetCoordinates)
{
projectile = i_projectile;
targetCoordinates = i_targetCoordinates;
messageIsValid = true;
}
public Vector3d getTargetCoordinates() {
return targetCoordinates;
}
public Projectile getProjectile() {
return projectile;
}
public boolean isMessageValid() {
return messageIsValid;
}
// not a valid way to construct the message
private AirstrikeMessageToServer()
{
messageIsValid = false;
}
/**
* Called by the network code once it has received the message bytes over the network.
* Used to read the PacketBuffer contents into your member variables
* @param buf
*/
public static AirstrikeMessageToServer decode(PacketBuffer buf)
{
AirstrikeMessageToServer retval = new AirstrikeMessageToServer();
try {
retval.projectile = Projectile.fromPacketBuffer(buf);
double x = buf.readDouble();
double y = buf.readDouble();
double z = buf.readDouble();
retval.targetCoordinates = new Vector3d(x, y, z);
// these methods may also be of use for your code:
// for Itemstacks - ByteBufUtils.readItemStack()
// for NBT tags ByteBufUtils.readTag();
// for Strings: ByteBufUtils.readUTF8String();
// NB that PacketBuffer is a derived class of ByteBuf
} catch (IllegalArgumentException | IndexOutOfBoundsException e) {
LOGGER.warn("Exception while reading AirStrikeMessageToServer: " + e);
return retval;
}
retval.messageIsValid = true;
return retval;
}
/**
* Called by the network code.
* Used to write the contents of your message member variables into the PacketBuffer, ready for transmission over the network.
* @param buf
*/
public void encode(PacketBuffer buf)
{
if (!messageIsValid) return;
projectile.toPacketBuffer(buf);
buf.writeDouble(targetCoordinates.x);
buf.writeDouble(targetCoordinates.y);
buf.writeDouble(targetCoordinates.z);
// these methods may also be of use for your code:
// for Itemstacks - ByteBufUtils.writeItemStack()
// for NBT tags ByteBufUtils.writeTag();
// for Strings: ByteBufUtils.writeUTF8String();
// NB that PacketBuffer is a derived class of ByteBuf
}
public enum Projectile {
PIG(1, "PIG", EntityType.PIG),
SNOWBALL(2, "SNOWBALL", EntityType.SNOWBALL),
TNT(3, "TNT", EntityType.TNT),
SNOWMAN(4, "SNOWMAN", EntityType.SNOW_GOLEM),
EGG(5, "EGG", EntityType.EGG),
FIREBALL(6, "FIREBALL", EntityType.FIREBALL);
public void toPacketBuffer(PacketBuffer buffer) {
buffer.writeByte(projectileID);
}
public EntityType getEntityType() {return entityType;}
public static Projectile fromPacketBuffer(PacketBuffer buffer) throws IllegalArgumentException {
byte ID = buffer.readByte();
for (Projectile projectile : Projectile.values()) {
if (ID == projectile.projectileID) return projectile;
}
throw new IllegalArgumentException("Unrecognised Projectile ID:" + ID);
}
public static Projectile getRandom() {
Random random = new Random();
AirstrikeMessageToServer.Projectile [] choices = AirstrikeMessageToServer.Projectile.values();
return choices[random.nextInt(choices.length)];
}
@Override
public String toString() {return name;}
private Projectile(int i_projectileID, String i_name, EntityType i_entityType) {
projectileID = (byte)i_projectileID;
name = i_name;
entityType = i_entityType;
}
private final byte projectileID;
private final String name;
private final EntityType entityType;
}
@Override
public String toString() {
return "AirstrikeMessageToServer[projectile=" + String.valueOf(projectile)
+ ", targetCoordinates=" + String.valueOf(targetCoordinates) + "]";
}
private Vector3d targetCoordinates;
private Projectile projectile;
private boolean messageIsValid;
private static final Logger LOGGER = LogManager.getLogger();
}