/
PanicMessage.java
65 lines (51 loc) · 2.03 KB
/
PanicMessage.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
package mod.dragonita.fantasymod.customthings;
import java.util.function.Supplier;
import org.apache.commons.lang3.tuple.Pair;
import io.netty.buffer.Unpooled;
import net.minecraft.entity.player.ServerPlayerEntity;
import net.minecraft.network.IPacket;
import net.minecraft.network.PacketBuffer;
import net.minecraftforge.fml.network.NetworkDirection;
import net.minecraftforge.fml.network.NetworkEvent;
import net.minecraftforge.fml.network.NetworkInstance;
import net.minecraftforge.fml.network.PacketDistributor;
import net.minecraftforge.fml.network.simple.IndexedMessageCodec;
public class PanicMessage{
private final int data;
private final NetworkInstance instance;
private final IndexedMessageCodec indexedCodec;
public PanicMessage(PacketBuffer buf, NetworkInstance instance){
this.data = buf.readInt();
this.instance = instance;
this.indexedCodec = new IndexedMessageCodec(instance);
}
public PanicMessage(int data, NetworkInstance instance) {
this.data = data;
this.instance = instance;
this.indexedCodec = new IndexedMessageCodec(instance);
}
public <MSG> void send(PacketDistributor.PacketTarget target, MSG message) {
target.send(toVanillaPacket(message, target.getDirection()));
}
private <MSG> IPacket<?> toVanillaPacket(MSG message, NetworkDirection direction) {
return direction.buildPacket(toBuffer(message), instance.getChannelName()).getThis();
}
private <MSG> Pair<PacketBuffer,Integer> toBuffer(MSG msg) {
final PacketBuffer bufIn = new PacketBuffer(Unpooled.buffer());
int index = this.encodeMessage(msg ,bufIn);
return Pair.of(bufIn, index);
}
public void handle(PanicMessage message ,Supplier<NetworkEvent.Context> context) {
context.get().enqueueWork(() -> {
@SuppressWarnings("unused")
ServerPlayerEntity sender = context.get().getSender();
});
context.get().setPacketHandled(true);
}
public void encode(PacketBuffer buf) {
buf.writeInt(data);
}
public <MSG> int encodeMessage(MSG message, final PacketBuffer target) {
return this.indexedCodec.build(message, target);
}
}