This repository has been archived by the owner on Jan 9, 2023. It is now read-only.
/
JoinGame.java
148 lines (127 loc) · 4.83 KB
/
JoinGame.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
package dev.floffah.gamermode.server.packet.play;
import com.google.common.hash.Hashing;
import com.google.common.io.ByteArrayDataOutput;
import com.google.common.io.ByteStreams;
import dev.floffah.gamermode.datatype.Identifier;
import dev.floffah.gamermode.datatype.VarInt;
import dev.floffah.gamermode.datatype.util.StringUtil;
import dev.floffah.gamermode.events.network.PacketSentEvent;
import dev.floffah.gamermode.server.packet.BasePacket;
import dev.floffah.gamermode.server.packet.Packet;
import dev.floffah.gamermode.server.packet.PacketType;
import dev.floffah.gamermode.server.packet.play.info.ServerDifficulty;
import dev.floffah.gamermode.server.packet.play.state.PlayerAbilitiesClientBound;
import dev.floffah.gamermode.server.socket.ConnectionState;
import dev.floffah.gamermode.world.World;
import java.io.IOException;
import java.nio.ByteBuffer;
import net.querz.nbt.io.NBTSerializer;
import net.querz.nbt.io.NamedTag;
@Packet(
name = "JoinGame",
id = 0x26,
type = PacketType.CLIENTBOUND,
state = ConnectionState.PLAY
)
public class JoinGame extends BasePacket {
@Override
public ByteArrayDataOutput buildOutput() throws IOException {
ByteArrayDataOutput output = ByteStreams.newDataOutput();
output.writeInt(this.conn.getPlayer().getEntityId()); // Entity ID
output.writeBoolean(
this.conn.getSocketManager()
.getServer()
.getConfig()
.worlds.isHardcore
); // Is hardcore
output.writeByte(this.conn.getPlayer().getGameMode()); // Gamemode
output.writeByte(this.conn.getPlayer().getPreviousGameMode()); // Previous Gamemode
new VarInt(
this.conn.getSocketManager()
.getServer()
.getWorldManager()
.getWorlds()
.size()
)
.writeTo(output); // World Count
for (World world : this.conn.getSocketManager()
.getServer()
.getWorldManager()
.getWorlds()
.values()) {
StringUtil.writeUTF(world.getName(), output);
} // World Names
NBTSerializer serializer = new NBTSerializer(false);
output.write(
serializer.toBytes(
new NamedTag(
null,
this.conn.getPlayer()
.getWorld()
.getWorldManager()
.buildDimensionCodec()
)
)
);
output.write(
serializer.toBytes(
new NamedTag(
null,
this.conn.getPlayer().getWorld().buildDimType()
)
)
);
StringUtil.writeUTF(
this.conn.getPlayer().getWorld().getType().getName(),
output
); // World Name
output.writeLong(
ByteBuffer
.wrap(
Hashing
.sha256()
.hashLong(this.conn.getPlayer().getWorld().getSeed())
.asBytes()
)
.getLong()
); // Hashed seed
new VarInt(
this.conn.getSocketManager().getServer().getConfig().players.max
)
.writeTo(output); // Max Players
new VarInt(
this.conn.getSocketManager()
.getServer()
.getConfig()
.worlds.renderDistance
)
.writeTo(output); // View Distance
new VarInt(
this.conn.getSocketManager()
.getServer()
.getConfig()
.worlds.simulationDistance
)
.writeTo(output); // Simulation Distance
output.writeBoolean(
this.conn.getSocketManager().getServer().isDebugMode()
); // Reduced Debug Info
// TODO: implement gamerules
// even just reading and writing for now
output.writeBoolean(true); // Enable respawn screen
output.writeBoolean(false); // Is Debug - false because not necessary
// TODO: implement world types
// so the server can differentiate between superflat and normal worlds
output.writeBoolean(false); // Is Flat
return output;
}
@Override
public void postSend(PacketSentEvent e) throws IOException {
ByteArrayDataOutput brandOut = ByteStreams.newDataOutput();
StringUtil.writeUTF("GamerMode", brandOut);
this.conn.getPlayer()
.sendPluginMessage(Identifier.from("minecraft", "brand"), brandOut);
this.conn.send(new ServerDifficulty());
this.conn.send(new PlayerAbilitiesClientBound());
}
}