in.voidma.lemming.Lemming.java Source code

Java tutorial

Introduction

Here is the source code for in.voidma.lemming.Lemming.java

Source

/**
 * Lemming - A Spigot plugin for network stress testing.
 * Copyright (C) 2017 Miles W. Silberling-Cook
 * <p>
 * This program is free software; you can redistribute it and/or modify it under the terms of the
 * GNU General Public License as published by the Free Software Foundation; either version 2 of
 * the License, or (at your option) any later version.
 * <p>
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 * <p>
 * You should have received a copy of the GNU General Public License along with this program;
 * if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
 * 02111-1307 USA
 */
package in.voidma.lemming;

import com.comphenix.protocol.ProtocolLibrary;
import com.comphenix.protocol.ProtocolManager;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.local.LocalAddress;
import io.netty.channel.local.LocalServerChannel;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.handler.timeout.ReadTimeoutHandler;
import net.minecraft.server.v1_10_R1.*;
import org.bukkit.plugin.java.JavaPlugin;

import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * The main entrypoint for Lemming.
 *
 * @author NthTensor
 * @version 1.0
 * @since 2017-04-23
 */
public class Lemming extends JavaPlugin {

    // Strongly typed configuration
    private static LemmingConfig config;
    // Logger
    private static Logger logger;
    ProtocolManager manager;

    @Override
    public void onLoad() {

        getLogger().log(Level.ALL, "LoadingLemming");

        // Logging
        logger = getLogger();
        LemmingLogger.init(this);

        // Configuration
        saveDefaultConfig();
        reloadConfig();

        try {
            config = new LemmingConfig(this);
        } catch (Exception e) {
            // TODO: Add warnings
        }

        // Print the state of the debug mode
        if (config.isServerDebug()) {
            logger.warning("Server side debug mode is enabled!");
        }

        // And the state of the error reporter
        if (config.isServerDetailedErrorReporting()) {
            // TODO: Enable detailed reporting
            logger.warning("Server side detailed error reporting enabled!");
        }

        // ProtocolLib setup
        manager = ProtocolLibrary.getProtocolManager();

        InternalManager manager = new InternalManager();

        LemmingLibrary.init(this, config, manager);

        DedicatedServer server = (DedicatedServer) getServer();
        final ServerConnection serverConnection = server.getServerConnection();
        try {
            final List<ChannelFuture> endpoints = (List<ChannelFuture>) serverConnection.getClass()
                    .getDeclaredField("g").get(serverConnection);
            final List<NetworkManager> networkManagers = (List<NetworkManager>) serverConnection.getClass()
                    .getDeclaredField("h").get(serverConnection);

            ChannelFuture channelfuture;

            synchronized (endpoints) {
                channelfuture = new ServerBootstrap().channel(LocalServerChannel.class)
                        .childHandler(new ChannelInitializer<Channel>() {
                            protected void initChannel(Channel channel) throws Exception {

                                NetworkManager networkmanager = new NetworkManager(
                                        EnumProtocolDirection.SERVERBOUND);
                                networkmanager.setPacketListener(
                                        new HandshakeListener(serverConnection.d(), networkmanager));
                                networkManagers.add(networkmanager);

                                channel.pipeline().addLast("timeout", new ReadTimeoutHandler(30));
                                channel.pipeline().addLast("legacy_query", new LegacyPingHandler(serverConnection));
                                channel.pipeline().addLast("splitter", new PacketSplitter());
                                channel.pipeline().addLast("decoder",
                                        new PacketDecoder(EnumProtocolDirection.SERVERBOUND));
                                channel.pipeline().addLast("prepender", new PacketPrepender());
                                channel.pipeline().addLast("encoder",
                                        new PacketEncoder(EnumProtocolDirection.CLIENTBOUND));
                                channel.pipeline().addLast("packet_handler", networkmanager);
                            }
                        }).group(new NioEventLoopGroup(4)).localAddress(LocalAddress.ANY).bind()
                        .syncUninterruptibly();

                endpoints.add(channelfuture);
            }

            LemmingClient client = manager.newLemming().name("TotallyNotABot").address("192.168.0.10", 2344)
                    .latency(20).packetLoss(3).throttling(400).create().connect();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}