View difference between Paste ID: hgPvRLXn and LvVx3X8X
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, false, GameMode.SURVIVAL, 0, Difficulty.PEACEFUL, 10, WorldType.DEFAULT, false));
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
61
        server.bind();
62
        
63
        status();
64
        login();
65
    }
66
    
67
    private static void status() {
68
        MinecraftProtocol protocol = new MinecraftProtocol(SubProtocol.STATUS);
69
        Client client = new Client(HOST, PORT, protocol, new TcpSessionFactory(PROXY));
70
        client.getSession().setFlag(MinecraftConstants.AUTH_PROXY_KEY, AUTH_PROXY);
71
        client.getSession().setFlag(MinecraftConstants.SERVER_INFO_HANDLER_KEY, new ServerInfoHandler() {
72
            @Override
73
            public void handle(Session session, ServerStatusInfo info) {
74
                System.out.println("Version: " + info.getVersionInfo().getVersionName() + ", " + info.getVersionInfo().getProtocolVersion());
75
                System.out.println("Player Count: " + info.getPlayerInfo().getOnlinePlayers() + " / " + info.getPlayerInfo().getMaxPlayers());
76
                System.out.println("Players: " + Arrays.toString(info.getPlayerInfo().getPlayers()));
77
                System.out.println("Description: " + info.getDescription().getFullText());
78
                System.out.println("Icon: " + info.getIcon());
79
            }
80
        });
81
82
        client.getSession().setFlag(MinecraftConstants.SERVER_PING_TIME_HANDLER_KEY, new ServerPingTimeHandler() {
83
            @Override
84
            public void handle(Session session, long pingTime) {
85
                System.out.println("Server ping took " + pingTime + "ms");
86
            }
87
        });
88
89
        client.getSession().connect();
90
        while(client.getSession().isConnected()) {
91
            try {
92
                Thread.sleep(5);
93
            } catch(InterruptedException e) {
94
                e.printStackTrace();
95
            }
96
        }
97
    }
98
99
    private static void login() {
100
        MinecraftProtocol protocol = null;
101
        if(VERIFY_USERS) {
102
            try {
103
                protocol = new MinecraftProtocol(USERNAME, PASSWORD, false);
104
                System.out.println("Successfully authenticated user.");
105
            } catch(RequestException e) {
106
                e.printStackTrace();
107
                return;
108
            }
109
        } else {
110
            protocol = new MinecraftProtocol(USERNAME);
111
        }
112
113
        Client client = new Client(HOST, PORT, protocol, new TcpSessionFactory(PROXY));
114
        client.getSession().setFlag(MinecraftConstants.AUTH_PROXY_KEY, AUTH_PROXY);
115
        client.getSession().addListener(new SessionAdapter() {
116
            @Override
117
            public void packetReceived(PacketReceivedEvent event) {
118
                if(event.getPacket() instanceof ServerChatPacket) {
119
                    Message message = event.<ServerChatPacket>getPacket().getMessage();
120
                    System.out.println("Odebrana Wiadomosc: " + message.getFullText());
121
                }
122
            }
123
124
            @Override
125
            public void disconnected(DisconnectedEvent event) {
126
                System.out.println("Rozlaczono: " + Message.fromString(event.getReason()).getFullText());
127
                if(event.getCause() != null) {
128
                    event.getCause().printStackTrace();
129
                }
130
            }
131
        });
132
133
        client.getSession().connect();
134
    }