Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private static final boolean VERIFY_USERS = false;
- private static final String HOST = "127.0.0.1";
- private static final int PORT = 2222;
- private static final Proxy PROXY = Proxy.NO_PROXY;
- private static final Proxy AUTH_PROXY = Proxy.NO_PROXY;
- private static final String USERNAME = "MProxy_STARTBOT";
- private static final String PASSWORD = "-";
- public static void loadServer() {
- Server server = new Server(HOST, PORT, MinecraftProtocol.class, new TcpSessionFactory(PROXY));
- server.setGlobalFlag(MinecraftConstants.AUTH_PROXY_KEY, AUTH_PROXY);
- server.setGlobalFlag(MinecraftConstants.VERIFY_USERS_KEY, VERIFY_USERS);
- server.setGlobalFlag(MinecraftConstants.SERVER_INFO_BUILDER_KEY, new ServerInfoBuilder() {
- @Override
- public ServerStatusInfo buildInfo(Session session) {
- return new ServerStatusInfo(new VersionInfo(MinecraftConstants.GAME_VERSION, MinecraftConstants.PROTOCOL_VERSION), new PlayerInfo(10, 0, new GameProfile[0]), new TextMessage("Testowe proxy"), null);
- }
- });
- server.setGlobalFlag(MinecraftConstants.SERVER_LOGIN_HANDLER_KEY, new ServerLoginHandler() {
- @Override
- public void loggedIn(Session session) {
- session.send(new ServerJoinGamePacket(0, true, GameMode.SURVIVAL, 0, Difficulty.PEACEFUL, 10, WorldType.LARGE_BIOMES, false));
- session.send(new ServerPlayerPositionRotationPacket(1337, 69, 1337, 0, 0));
- session.send(new ServerJoinGamePacket(0, false, GameMode.SURVIVAL, 0, Difficulty.PEACEFUL, 10, WorldType.DEFAULT, false));
- }
- });
- server.setGlobalFlag(MinecraftConstants.SERVER_COMPRESSION_THRESHOLD, 100);
- server.addListener(new ServerAdapter() {
- @Override
- public void sessionAdded(SessionAddedEvent event) {
- event.getSession().addListener(new SessionAdapter() {
- @Override
- public void packetReceived(PacketReceivedEvent event) {
- if(event.getPacket() instanceof ClientChatPacket) {
- ClientChatPacket packet = event.getPacket();
- GameProfile profile = event.getSession().getFlag(MinecraftConstants.PROFILE_KEY);
- System.out.println(profile.getName() + ": " + packet.getMessage());
- Message msg = new TextMessage("Hello, ").setStyle(new MessageStyle().setColor(ChatColor.GREEN));
- Message name = new TextMessage(profile.getName()).setStyle(new MessageStyle().setColor(ChatColor.AQUA).addFormat(ChatFormat.UNDERLINED));
- Message end = new TextMessage("!");
- msg.addExtra(name);
- msg.addExtra(end);
- event.getSession().send(new ServerChatPacket(msg));
- }
- }
- });
- }
- @Override
- public void sessionRemoved(SessionRemovedEvent event) {
- MinecraftProtocol protocol = (MinecraftProtocol) event.getSession().getPacketProtocol();
- if(protocol.getSubProtocol() == SubProtocol.GAME) {
- System.out.println("Closing server.");
- event.getServer().close();
- }
- }
- });
- server.bind();
- status();
- login();
- }
- private static void status() {
- MinecraftProtocol protocol = new MinecraftProtocol(SubProtocol.STATUS);
- Client client = new Client(HOST, PORT, protocol, new TcpSessionFactory(PROXY));
- client.getSession().setFlag(MinecraftConstants.AUTH_PROXY_KEY, AUTH_PROXY);
- client.getSession().setFlag(MinecraftConstants.SERVER_INFO_HANDLER_KEY, new ServerInfoHandler() {
- @Override
- public void handle(Session session, ServerStatusInfo info) {
- System.out.println("Version: " + info.getVersionInfo().getVersionName() + ", " + info.getVersionInfo().getProtocolVersion());
- System.out.println("Player Count: " + info.getPlayerInfo().getOnlinePlayers() + " / " + info.getPlayerInfo().getMaxPlayers());
- System.out.println("Players: " + Arrays.toString(info.getPlayerInfo().getPlayers()));
- System.out.println("Description: " + info.getDescription().getFullText());
- System.out.println("Icon: " + info.getIcon());
- }
- });
- client.getSession().setFlag(MinecraftConstants.SERVER_PING_TIME_HANDLER_KEY, new ServerPingTimeHandler() {
- @Override
- public void handle(Session session, long pingTime) {
- System.out.println("Server ping took " + pingTime + "ms");
- }
- });
- client.getSession().connect();
- while(client.getSession().isConnected()) {
- try {
- Thread.sleep(5);
- } catch(InterruptedException e) {
- e.printStackTrace();
- }
- }
- }
- private static void login() {
- MinecraftProtocol protocol = null;
- if(VERIFY_USERS) {
- try {
- protocol = new MinecraftProtocol(USERNAME, PASSWORD, false);
- System.out.println("Successfully authenticated user.");
- } catch(RequestException e) {
- e.printStackTrace();
- return;
- }
- } else {
- protocol = new MinecraftProtocol(USERNAME);
- }
- Client client = new Client(HOST, PORT, protocol, new TcpSessionFactory(PROXY));
- client.getSession().setFlag(MinecraftConstants.AUTH_PROXY_KEY, AUTH_PROXY);
- client.getSession().addListener(new SessionAdapter() {
- @Override
- public void packetReceived(PacketReceivedEvent event) {
- if(event.getPacket() instanceof ServerChatPacket) {
- Message message = event.<ServerChatPacket>getPacket().getMessage();
- System.out.println("Odebrana Wiadomosc: " + message.getFullText());
- }
- }
- @Override
- public void disconnected(DisconnectedEvent event) {
- System.out.println("Rozlaczono: " + Message.fromString(event.getReason()).getFullText());
- if(event.getCause() != null) {
- event.getCause().printStackTrace();
- }
- }
- });
- client.getSession().connect();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement