-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathMinecraftServer.java
More file actions
189 lines (163 loc) · 5.45 KB
/
MinecraftServer.java
File metadata and controls
189 lines (163 loc) · 5.45 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
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
package roj.plugins.minecraft.server;
import roj.collect.ArrayList;
import roj.config.JsonParser;
import roj.config.JsonSerializer;
import roj.config.TextEmitter;
import roj.config.node.MapValue;
import roj.event.EventBus;
import roj.io.IOUtil;
import roj.net.ChannelCtx;
import roj.net.MyChannel;
import roj.net.ServerLaunch;
import roj.net.handler.Timeout;
import roj.net.handler.VarintSplitter;
import roj.plugin.Plugin;
import roj.plugins.minecraft.server.data.Block;
import roj.plugins.minecraft.server.data.Item;
import roj.plugins.minecraft.server.data.PlayerEntity;
import roj.plugins.minecraft.server.event.PlayerLoginEvent;
import roj.plugins.minecraft.server.network.FlowControl;
import roj.plugins.minecraft.server.network.PacketDecoder;
import roj.plugins.minecraft.server.network.PlayerConnection;
import roj.plugins.minecraft.server.network.PlayerInit;
import roj.text.TextWriter;
import roj.text.logging.Logger;
import roj.ui.Text;
import roj.util.ByteList;
import roj.util.DynByteBuf;
import roj.util.JVM;
import roj.util.TypedKey;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.SecureRandom;
import java.util.Collection;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
/**
* @author Roj234
* @since 2024/3/19 15:04
*/
public class MinecraftServer extends Plugin {
public static final TypedKey<PlayerConnection> PLAYER = new TypedKey<>("player");
public static Logger LOGGER;
public static MinecraftServer INSTANCE;
public KeyPair rsa;
public byte[] rsaPublicKeyBytes;
public SecureRandom random;
private final EventBus eventBus = new EventBus();
private ServerLaunch listener;
public EventBus getEventBus() { return eventBus; }
@Override
protected void onEnable() throws Exception {
JVM.useAccurateTiming();
LOGGER = getLogger();
INSTANCE = this;
setMeta(JsonParser.parses("""
{
"version": {
"protocol": 760,
"name": "1.19.2"
},
"players": {
"online": 114,
"max": 514
},
"previewsChat": false,
"enforcesSecureChat": false,
"preventsChatReports": true,
"description": {
"text": "\\u00a7c史\\u00a76上\\u00a7e最\\u00a7a牛\\u00a7b验\\u00a79证\\u00a7d码\\n\\u00a7b\\u00a7ldoge"
}
}""").asMap());
getLogger().info("代理版本: 1.19.2 注册的方块: "+ Block.STATE_ID.nextId()+" 注册的物品: "+ Item.byId.length);
getLogger().info("正在生成密钥对");
KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
kpg.initialize(512);
this.rsa = kpg.genKeyPair();
this.rsaPublicKeyBytes = rsa.getPublic().getEncoded();
this.random = SecureRandom.getInstanceStrong();
int port = 25565;
getLogger().info("正在监听0.0.0.0:"+port);
listener = ServerLaunch.tcp().bind(port).initializator(ch -> {
LOGGER.info("来自{}的连接", ch.remoteAddress());
ch.addLast("flow_control", new FlowControl())
.addLast("splitter", VarintSplitter.twoMbVI())
.addLast("timeout", new Timeout(30000,500))
.addLast("packet", new PacketDecoder());
connection.put(ch, true);
}).launch();
}
@Override
protected void onDisable() {
getLogger().info("正在停止监听");
try {
listener.close();
} catch (IOException e) {
e.printStackTrace();
}
getLogger().info("正在停止服务");
for (PlayerConnection value : players.values()) {
value.disconnect("服务器已关闭");
}
for (MyChannel ch : connection.keySet()) {
try {
ch.closeGracefully();
} catch (IOException e) {
try {
ch.close();
} catch (IOException ex) {
LOGGER.warn("error", ex);
}
}
}
}
public final ConcurrentHashMap<MyChannel, Boolean> connection = new ConcurrentHashMap<>();
public final ConcurrentHashMap<UUID, PlayerConnection> players = new ConcurrentHashMap<>();
public PlayerConnection getPlayer(UUID uuid) { return players.get(uuid); }
public Collection<PlayerConnection> getPlayers() { return new ArrayList<>(players.values()); }
private final MapValue meta = new MapValue();
private volatile DynByteBuf metaBytes;
public void setMeta(MapValue meta) {
metaBytes = null;
this.meta.merge(meta, false, true);
}
public DynByteBuf getMetaBytes() {
block:
if (metaBytes == null) {
synchronized (this) {
if (metaBytes != null) break block;
metaBytes = new ByteList();
ByteList buf = IOUtil.getSharedByteBuf();
try (TextWriter tw = new TextWriter(buf, StandardCharsets.UTF_8)) {
TextEmitter ser = new JsonSerializer().to(tw);
meta.accept(ser);
ser.close();
} catch (Exception e) {
e.printStackTrace();
}
metaBytes.putVarInt(buf.readableBytes()).put(buf);
}
}
return metaBytes.slice();
}
public Text preLogin(ChannelCtx ctx, PlayerConnection connection) {
ctx.channel().addLast("create_world", new PlayerInit(connection));
PlayerLoginEvent.Pre event = new PlayerLoginEvent.Pre(connection, ctx.channel());
if (eventBus.post(event)) {
Text message = event.getMessage();
return message == null ? new Text("你被拒绝加入服务器") : message;
}
return null;
}
public PlayerEntity postLogin(PlayerConnection connection) {
PlayerEntity entity = new PlayerEntity();
entity.uuid = connection.getUUID();
PlayerLoginEvent.Post event = new PlayerLoginEvent.Post(connection, entity);
eventBus.post(event);
return event.getPlayerEntity();
}
public int getCompressionThreshold() {return 256;}
public int getViewDistance() {return 12;}
}