tk.playerforcehd.networklib.bukkit.connection.NetworkManager.java Source code

Java tutorial

Introduction

Here is the source code for tk.playerforcehd.networklib.bukkit.connection.NetworkManager.java

Source

/*
 *     NetworkLib - A Spigot/BungeeCord plugin messaging Library
 *     Copyright (C) 2015 Pascal Zarrad
 *
 *     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.
 *
 *     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.
 *
 *     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.,
 *     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

package tk.playerforcehd.networklib.bukkit.connection;

import com.google.common.io.ByteArrayDataOutput;
import com.google.common.io.ByteStreams;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.plugin.messaging.PluginMessageListener;
import tk.playerforcehd.networklib.bukkit.Main;
import tk.playerforcehd.networklib.bukkit.api.NetworkLib;
import tk.playerforcehd.networklib.bukkit.event.PluginMessageIncomingEvent;
import tk.playerforcehd.networklib.bukkit.event.PluginMessageOutgoingEvent;

import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.IOException;

public class NetworkManager implements PluginMessageListener {

    /**
     * The main instance of the NetworkLib
     */
    private Main thePlugin;

    /**
     * The name of this manager
     */
    @SuppressWarnings("unused")
    private String name;

    /**
     * The channel where this manager is listen
     */
    private String channel;

    /**
     * The API Instance
     */
    @SuppressWarnings("unused")
    private NetworkLib theAPI;

    /**
     * Creates a new Network Manager,  needed for communication
     *
     * @param thePlugin     the main instance of the NetworkLib
     * @param name          the name of this Manager
     * @param channel       the channel where this class is listen
     * @param theNetworkLib the API
     */
    public NetworkManager(Main thePlugin, String name, String channel, NetworkLib theNetworkLib) {
        this.thePlugin = thePlugin;
        this.name = name;
        this.channel = channel;
        this.theAPI = theNetworkLib;
        Bukkit.getMessenger().registerOutgoingPluginChannel(this.thePlugin, this.channel);
        Bukkit.getMessenger().registerIncomingPluginChannel(this.thePlugin, this.channel, this);
    }

    /**
     * Send a plugin mesage to the proxy
     *
     * @param subchannel the subchannel where the message get send
     * @param messages   The messages to write
     */
    public void sendMessageToProxy(String subchannel, String... messages) {
        ByteArrayDataOutput stream = ByteStreams.newDataOutput();
        stream.writeUTF(subchannel);
        for (String write : messages) {
            stream.writeUTF(write);
        }
        PluginMessageOutgoingEvent pluginMessageOutgoingEvent = new PluginMessageOutgoingEvent(this.channel,
                stream);
        Bukkit.getPluginManager().callEvent(pluginMessageOutgoingEvent);
        if (!pluginMessageOutgoingEvent.isCancelled()) {
            for (Player p : Bukkit.getOnlinePlayers()) {
                p.sendPluginMessage(thePlugin, pluginMessageOutgoingEvent.getChannel(),
                        pluginMessageOutgoingEvent.getByteArrayDataOutput().toByteArray());
                break;
            }
        }
    }

    /**
     * Send a plugin mesage to the proxy
     *
     * @param byteArrayDataOutput The output of the message
     */
    public void sendMessageToProxy(ByteArrayDataOutput byteArrayDataOutput) {
        PluginMessageOutgoingEvent pluginMessageOutgoingEvent = new PluginMessageOutgoingEvent(this.channel,
                byteArrayDataOutput);
        Bukkit.getPluginManager().callEvent(pluginMessageOutgoingEvent);
        if (!pluginMessageOutgoingEvent.isCancelled()) {
            for (Player p : Bukkit.getOnlinePlayers()) {
                p.sendPluginMessage(thePlugin, pluginMessageOutgoingEvent.getChannel(),
                        pluginMessageOutgoingEvent.getByteArrayDataOutput().toByteArray());
                break;
            }
        }
    }

    /**
     * Send a plugin mesage to a player
     *
     * @param subchannel the subchannel where the message get send
     * @param messages   The messages to send
     */
    public void sendMessageToPlayer(Player player, String subchannel, String... messages) {
        ByteArrayDataOutput stream = ByteStreams.newDataOutput();
        stream.writeUTF(subchannel);
        for (String write : messages) {
            stream.writeUTF(write);
        }
        PluginMessageOutgoingEvent pluginMessageOutgoingEvent = new PluginMessageOutgoingEvent(this.channel,
                stream);
        Bukkit.getPluginManager().callEvent(pluginMessageOutgoingEvent);
        if (!pluginMessageOutgoingEvent.isCancelled()) {
            player.sendPluginMessage(thePlugin, pluginMessageOutgoingEvent.getChannel(),
                    pluginMessageOutgoingEvent.getByteArrayDataOutput().toByteArray());
        }
    }

    /**
     * Send a plugin mesage to a player
     *
     * @param byteArrayDataOutput The Output of the Message
     */
    public void sendMessageToPlayer(Player player, ByteArrayDataOutput byteArrayDataOutput) {
        PluginMessageOutgoingEvent pluginMessageOutgoingEvent = new PluginMessageOutgoingEvent(this.channel,
                byteArrayDataOutput);
        Bukkit.getPluginManager().callEvent(pluginMessageOutgoingEvent);
        if (!pluginMessageOutgoingEvent.isCancelled()) {
            player.sendPluginMessage(thePlugin, pluginMessageOutgoingEvent.getChannel(),
                    pluginMessageOutgoingEvent.getByteArrayDataOutput().toByteArray());
        }
    }

    /**
     * This is listen to the incomming pluginmessages
     */
    @Override
    public void onPluginMessageReceived(String arg0, Player arg1, byte[] arg2) {
        DataInputStream stream = new DataInputStream(new ByteArrayInputStream(arg2));
        String subchannel = "";
        try {
            subchannel = stream.readUTF();
        } catch (IOException e) {
        }
        DataInputStream eventStream = new DataInputStream(new ByteArrayInputStream(arg2));
        PluginMessageIncomingEvent pluginMessageIncomingEvent = new PluginMessageIncomingEvent(this.channel,
                subchannel, eventStream);
        Bukkit.getPluginManager().callEvent(pluginMessageIncomingEvent);
        if (!pluginMessageIncomingEvent.isCancelled()) {
            String suCh = pluginMessageIncomingEvent.getSubchannel();
            DataInputStream execStr = pluginMessageIncomingEvent.getDataInputStream();
            onRecievePluginMessage(suCh, execStr);
            onRecievePluginMessage(arg1, suCh, execStr);
            this.thePlugin.getTheNetworkCommandManager().exec(suCh, execStr);
        }
    }

    /**
     * Get fired when a plugin message cames in, only needed when a instance extends this class and overiddes the method
     *
     * @param subchannel      the subchannnel where the message cames in
     * @param dataInputStream the DataInputStream, can read with stream.readUTF();
     */
    public void onRecievePluginMessage(String subchannel, DataInputStream dataInputStream) {
    }

    /**
     * Get fired when a plugin message cames in, only needed when a instance extends this class and overiddes the method
     *
     * @param player          the player who get the message
     * @param subchannel      the subchannel where the message cames in
     * @param dataInputStream the DataInputStream, can read with stream.readUTF();
     */
    public void onRecievePluginMessage(Player player, String subchannel, DataInputStream dataInputStream) {
    }

    /**
     * Creates a new DataInputStream to read the data from a byte[]
     *
     * @param data the data which get converted to a input stream
     * @return a new input stream, based on the byte[] data
     */
    public DataInputStream createDataInputStream(byte[] data) {
        return new DataInputStream(new ByteArrayInputStream(data));
    }

    /**
     * Read the next String from the byte message
     *
     * @param dataInputStream the stream who get readed
     * @return the String from the last byte
     * @throws IOException if anything went wrong
     */
    public String readNextString(DataInputStream dataInputStream) throws IOException {
        return dataInputStream.readUTF();
    }

    /**
     * Read the next int from the byte message
     *
     * @param dataInputStream the stream who get readed
     * @return the int from the last byte
     * @throws IOException if anything went wrong
     */
    public int readNextInt(DataInputStream dataInputStream) throws IOException {
        return dataInputStream.readInt();
    }

    /**
     * Read the next boolean from the byte message
     *
     * @param dataInputStream the stream who get readed
     * @return the boolean from the last byte
     * @throws IOException if anything went wrong
     */
    public boolean readNextBoolean(DataInputStream dataInputStream) throws IOException {
        return dataInputStream.readBoolean();
    }

    /**
     * Executer a global broadcast with a permission who recieve it over the network.
     * this can be used to broadcast global things with a plugin
     *
     * @param whoExecuteIt the player who send the plugin message
     * @param permission   the needed permission to recieve the message
     * @param message      the message which get send
     */
    public void executeBroadcast(Player whoExecuteIt, String permission, String message) {
        ByteArrayDataOutput stream = ByteStreams.newDataOutput();
        stream.writeUTF("broadcast");
        stream.writeUTF("Permission:" + permission);
        stream.writeUTF(message);
        whoExecuteIt.sendPluginMessage(this.thePlugin, "BungeeCord", stream.toByteArray());
    }

    /**
     * Executer a global broadcast with a permission who recieve it over the network.
     * this can be used to broadcast global things with a plugin
     *
     * @param whoExecuteIt the player who send the plugin message
     * @param message      the message which get send
     */
    public void executeBroadcast(Player whoExecuteIt, String message) {
        ByteArrayDataOutput stream = ByteStreams.newDataOutput();
        stream.writeUTF("broadcast");
        stream.writeUTF(message);
        whoExecuteIt.sendPluginMessage(this.thePlugin, "BungeeCord", stream.toByteArray());
    }

    /**
     * Execute a command on the proxy
     *
     * @param command the command which get runned
     */
    public void executeCommand_Proxy(String command) {
        ByteArrayDataOutput stream = ByteStreams.newDataOutput();
        stream.writeUTF("executeCommand");
        stream.writeUTF("bungee");
        stream.writeUTF(command);
        Bukkit.getOnlinePlayers().iterator().next().sendPluginMessage(this.thePlugin, "BungeeCord",
                stream.toByteArray());
    }

    /**
     * Execute a command in the console of a speciefied server
     *
     * @param command    the command which get executed
     * @param serverName the name of the server the the command get executed
     */
    public void executeCommand_Server(String command, String serverName) {
        ByteArrayDataOutput stream = ByteStreams.newDataOutput();
        stream.writeUTF("executeCommand");
        stream.writeUTF("server_" + serverName);
        stream.writeUTF(command);
        Bukkit.getOnlinePlayers().iterator().next().sendPluginMessage(this.thePlugin, "BungeeCord",
                stream.toByteArray());
    }

    /**
     * Let's a player execute a command on his current server
     *
     * @param command the command which get executes
     * @param player  the player who executes it
     */
    public void executeCommand_Player(String command, Player player) {
        ByteArrayDataOutput stream = ByteStreams.newDataOutput();
        stream.writeUTF("executeCommand");
        stream.writeUTF(player.getName());
        stream.writeUTF(command);
        Bukkit.getOnlinePlayers().iterator().next().sendPluginMessage(this.thePlugin, "BungeeCord",
                stream.toByteArray());
    }
}