SHOW:
|
|
- or go back to the newest paste.
1 | private static final boolean VERIFY_USERS = false; | |
2 | private static final String HOST = "127.0.0.1"; | |
3 | private static final int PORT = 2222; | |
4 | private static final Proxy PROXY = Proxy.NO_PROXY; | |
5 | private static final Proxy AUTH_PROXY = Proxy.NO_PROXY; | |
6 | private static final String USERNAME = "MProxy_STARTBOT"; | |
7 | private static final String PASSWORD = "-"; | |
8 | ||
9 | public static void loadServer() { | |
10 | Server server = new Server(HOST, PORT, MinecraftProtocol.class, new TcpSessionFactory(PROXY)); | |
11 | server.setGlobalFlag(MinecraftConstants.AUTH_PROXY_KEY, AUTH_PROXY); | |
12 | server.setGlobalFlag(MinecraftConstants.VERIFY_USERS_KEY, VERIFY_USERS); | |
13 | server.setGlobalFlag(MinecraftConstants.SERVER_INFO_BUILDER_KEY, new ServerInfoBuilder() { | |
14 | @Override | |
15 | public ServerStatusInfo buildInfo(Session session) { | |
16 | return new ServerStatusInfo(new VersionInfo(MinecraftConstants.GAME_VERSION, MinecraftConstants.PROTOCOL_VERSION), new PlayerInfo(10, 0, new GameProfile[0]), new TextMessage("Testowe proxy"), null); | |
17 | } | |
18 | }); | |
19 | ||
20 | server.setGlobalFlag(MinecraftConstants.SERVER_LOGIN_HANDLER_KEY, new ServerLoginHandler() { | |
21 | @Override | |
22 | public void loggedIn(Session session) { | |
23 | session.send(new ServerJoinGamePacket(0, true, GameMode.SURVIVAL, 0, Difficulty.PEACEFUL, 10, WorldType.LARGE_BIOMES, false)); | |
24 | session.send(new ServerPlayerPositionRotationPacket(1337, 69, 1337, 0, 0)); | |
25 | session.send(new ServerJoinGamePacket(0, false, GameMode.SURVIVAL, 0, Difficulty.PEACEFUL, 10, WorldType.DEFAULT, false)); | |
26 | } | |
27 | }); | |
28 | ||
29 | server.setGlobalFlag(MinecraftConstants.SERVER_COMPRESSION_THRESHOLD, 100); | |
30 | server.addListener(new ServerAdapter() { | |
31 | @Override | |
32 | public void sessionAdded(SessionAddedEvent event) { | |
33 | event.getSession().addListener(new SessionAdapter() { | |
34 | @Override | |
35 | public void packetReceived(PacketReceivedEvent event) { | |
36 | if(event.getPacket() instanceof ClientChatPacket) { | |
37 | ClientChatPacket packet = event.getPacket(); | |
38 | GameProfile profile = event.getSession().getFlag(MinecraftConstants.PROFILE_KEY); | |
39 | System.out.println(profile.getName() + ": " + packet.getMessage()); | |
40 | Message msg = new TextMessage("Hello, ").setStyle(new MessageStyle().setColor(ChatColor.GREEN)); | |
41 | Message name = new TextMessage(profile.getName()).setStyle(new MessageStyle().setColor(ChatColor.AQUA).addFormat(ChatFormat.UNDERLINED)); | |
42 | Message end = new TextMessage("!"); | |
43 | msg.addExtra(name); | |
44 | msg.addExtra(end); | |
45 | event.getSession().send(new ServerChatPacket(msg)); | |
46 | } | |
47 | } | |
48 | }); | |
49 | } | |
50 | ||
51 | @Override | |
52 | public void sessionRemoved(SessionRemovedEvent event) { | |
53 | MinecraftProtocol protocol = (MinecraftProtocol) event.getSession().getPacketProtocol(); | |
54 | if(protocol.getSubProtocol() == SubProtocol.GAME) { | |
55 | System.out.println("Closing server."); | |
56 | event.getServer().close(); | |
57 | } | |
58 | } | |
59 | }); | |
60 | server.setGlobalFlag(SERVER_LOGIN_HANDLER_KEY, new ServerLoginHandler() { | |
61 | @Override | |
62 | public void loggedIn(Session session) { | |
63 | System.out.println("## Odebrano pakiet ServerJoinGamePacket... ##"); | |
64 | session.send(new ServerJoinGamePacket(0, true, GameMode.SURVIVAL, 0, Difficulty.PEACEFUL, 10, WorldType.LARGE_BIOMES, false)); | |
65 | session.send(new ServerPlayerPositionRotationPacket(1337, 69, 1337, 0, 0)); | |
66 | } | |
67 | }); | |
68 | server.bind(); | |
69 | ||
70 | status(); | |
71 | login(); | |
72 | } | |
73 | ||
74 | private static void status() { | |
75 | MinecraftProtocol protocol = new MinecraftProtocol(SubProtocol.STATUS); | |
76 | Client client = new Client(HOST, PORT, protocol, new TcpSessionFactory(PROXY)); | |
77 | client.getSession().setFlag(MinecraftConstants.AUTH_PROXY_KEY, AUTH_PROXY); | |
78 | client.getSession().setFlag(MinecraftConstants.SERVER_INFO_HANDLER_KEY, new ServerInfoHandler() { | |
79 | @Override | |
80 | public void handle(Session session, ServerStatusInfo info) { | |
81 | System.out.println("Version: " + info.getVersionInfo().getVersionName() + ", " + info.getVersionInfo().getProtocolVersion()); | |
82 | System.out.println("Player Count: " + info.getPlayerInfo().getOnlinePlayers() + " / " + info.getPlayerInfo().getMaxPlayers()); | |
83 | System.out.println("Players: " + Arrays.toString(info.getPlayerInfo().getPlayers())); | |
84 | System.out.println("Description: " + info.getDescription().getFullText()); | |
85 | System.out.println("Icon: " + info.getIcon()); | |
86 | } | |
87 | }); | |
88 | ||
89 | client.getSession().setFlag(MinecraftConstants.SERVER_PING_TIME_HANDLER_KEY, new ServerPingTimeHandler() { | |
90 | @Override | |
91 | public void handle(Session session, long pingTime) { | |
92 | System.out.println("Server ping took " + pingTime + "ms"); | |
93 | } | |
94 | }); | |
95 | ||
96 | client.getSession().connect(); | |
97 | while(client.getSession().isConnected()) { | |
98 | try { | |
99 | Thread.sleep(5); | |
100 | } catch(InterruptedException e) { | |
101 | e.printStackTrace(); | |
102 | } | |
103 | } | |
104 | } | |
105 | ||
106 | private static void login() { | |
107 | MinecraftProtocol protocol = null; | |
108 | if(VERIFY_USERS) { | |
109 | try { | |
110 | protocol = new MinecraftProtocol(USERNAME, PASSWORD, false); | |
111 | System.out.println("Successfully authenticated user."); | |
112 | } catch(RequestException e) { | |
113 | e.printStackTrace(); | |
114 | return; | |
115 | } | |
116 | } else { | |
117 | protocol = new MinecraftProtocol(USERNAME); | |
118 | } | |
119 | ||
120 | Client client = new Client(HOST, PORT, protocol, new TcpSessionFactory(PROXY)); | |
121 | client.getSession().setFlag(MinecraftConstants.AUTH_PROXY_KEY, AUTH_PROXY); | |
122 | client.getSession().addListener(new SessionAdapter() { | |
123 | @Override | |
124 | public void packetReceived(PacketReceivedEvent event) { | |
125 | if(event.getPacket() instanceof ServerChatPacket) { | |
126 | Message message = event.<ServerChatPacket>getPacket().getMessage(); | |
127 | System.out.println("Odebrana Wiadomosc: " + message.getFullText()); | |
128 | } | |
129 | } | |
130 | ||
131 | @Override | |
132 | public void disconnected(DisconnectedEvent event) { | |
133 | System.out.println("Rozlaczono: " + Message.fromString(event.getReason()).getFullText()); | |
134 | if(event.getCause() != null) { | |
135 | event.getCause().printStackTrace(); | |
136 | } | |
137 | } | |
138 | }); | |
139 | ||
140 | client.getSession().connect(); | |
141 | } |