Search in sources :

Example 1 with SocketHandler

use of com.bencodez.advancedcore.bungeeapi.sockets.SocketHandler in project VotingPlugin by Ben12345rocks.

the class VotingPluginBungee method onEnable.

@Override
public void onEnable() {
    try {
        Class.forName("com.vexsoftware.votifier.bungee.events.VotifierEvent");
    } catch (ClassNotFoundException e) {
        votifierEnabled = false;
        getLogger().warning("Votifier event not found, not loading votifier event");
    }
    if (votifierEnabled) {
        try {
            getProxy().getPluginManager().registerListener(this, new VoteEventBungee(this));
            getProxy().getPluginManager().registerListener(this, this);
        } catch (Exception e) {
        }
    }
    config = new Config(this);
    config.load();
    try {
        redis = new RedisBungee(this);
    } catch (Exception e) {
        e.printStackTrace();
    }
    getProxy().getPluginManager().registerCommand(this, new VotingPluginBungeeCommand(this));
    boolean mysqlLoaded = true;
    try {
        if (!config.getData().getString("Host", "").isEmpty()) {
            loadMysql();
        } else {
            mysqlLoaded = false;
            getLogger().severe("MySQL settings not set in bungeeconfig.yml");
        }
    } catch (Exception e) {
        mysqlLoaded = false;
        e.printStackTrace();
    }
    method = BungeeMethod.getByName(config.getBungeeMethod());
    if (method == null) {
        method = BungeeMethod.PLUGINMESSAGING;
    }
    this.getProxy().registerChannel("vp:vp");
    if (mysqlLoaded) {
        uuidPlayerNameCache = mysql.getRowsUUIDNameQuery();
        voteCacheFile = new VoteCache(this);
        voteCacheFile.load();
        nonVotedPlayersCache = new NonVotedPlayersCache(this);
        nonVotedPlayersCache.load();
        timeHandle = new TimeHandle(voteCacheFile.getData().getString("Time.Month", ""), voteCacheFile.getData().getInt("Time.Day"), voteCacheFile.getData().getInt("Time.Week")) {

            @Override
            public void save() {
                voteCacheFile.getData().set("Time.Month", timeHandle.getMonth());
                voteCacheFile.getData().set("Time.Day", timeHandle.getDay());
                voteCacheFile.getData().set("Time.Week", timeHandle.getWeek());
            }
        };
        if (method.equals(BungeeMethod.PLUGINMESSAGING)) {
            try {
                for (String server : voteCacheFile.getServers()) {
                    ArrayList<OfflineBungeeVote> vote = new ArrayList<OfflineBungeeVote>();
                    for (String num : voteCacheFile.getServerVotes(server)) {
                        Configuration data = voteCacheFile.getServerVotes(server, num);
                        vote.add(new OfflineBungeeVote(data.getString("Name"), data.getString("UUID"), data.getString("Service"), data.getLong("Time"), data.getBoolean("Real"), data.getString("Text")));
                    }
                    cachedVotes.put(server, vote);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            try {
                for (String player : voteCacheFile.getPlayers()) {
                    ArrayList<OfflineBungeeVote> vote = new ArrayList<OfflineBungeeVote>();
                    for (String num : voteCacheFile.getOnlineVotes(player)) {
                        Configuration data = voteCacheFile.getOnlineVotes(player, num);
                        vote.add(new OfflineBungeeVote(data.getString("Name"), data.getString("UUID"), data.getString("Service"), data.getLong("Time"), data.getBoolean("Real"), data.getString("Text")));
                    }
                    cachedOnlineVotes.put(player, vote);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            voteCacheFile.clearData();
            getProxy().getScheduler().schedule(this, new Runnable() {

                @Override
                public void run() {
                    for (String server : cachedVotes.keySet()) {
                        checkCachedVotes(server);
                    }
                    for (String player : cachedOnlineVotes.keySet()) {
                        checkOnlineVotes(getProxy().getPlayer(UUID.fromString(player)), player, null);
                    }
                }
            }, 15l, 30l, TimeUnit.SECONDS);
            getProxy().getScheduler().schedule(this, new Runnable() {

                @Override
                public void run() {
                    if (nonVotedPlayersCache != null) {
                        debug("Checking nonvotedplayers.yml...");
                        nonVotedPlayersCache.check();
                    }
                }
            }, 1l, 60l, TimeUnit.MINUTES);
        } else if (method.equals(BungeeMethod.SOCKETS)) {
            encryptionHandler = new EncryptionHandler(new File(getDataFolder(), "secretkey.key"));
            socketHandler = new SocketHandler(getDescription().getVersion(), config.getBungeeHost(), config.getBungeePort(), encryptionHandler, config.getDebug());
            socketHandler.add(new SocketReceiver() {

                @Override
                public void onReceive(String[] data) {
                    if (data.length > 1) {
                        if (data.length > 2) {
                            if (data[0].equalsIgnoreCase("Broadcast")) {
                                sendServerMessage(data);
                            }
                        }
                    }
                }
            });
            socketHandler.add(new SocketReceiver() {

                @Override
                public void onReceive(String[] data) {
                    if (data.length > 1) {
                        if (data[0].equalsIgnoreCase("StatusOkay")) {
                            String server = data[1];
                            getLogger().info("Voting communicaton okay with " + server);
                        }
                    }
                }
            });
            clientHandles = new HashMap<String, ClientHandler>();
            List<String> l = config.getBlockedServers();
            for (String s : config.getSpigotServers()) {
                if (!l.contains(s)) {
                    Configuration d = config.getSpigotServerConfiguration(s);
                    clientHandles.put(s, new ClientHandler(d.getString("Host", ""), d.getInt("Port", 1298), encryptionHandler, config.getDebug()));
                }
            }
        }
    }
    currentVotePartyVotesRequired = getConfig().getVotePartyVotesRequired() + voteCacheFile.getVotePartyInreaseVotesRequired();
    votePartyVotes = voteCacheFile.getVotePartyCurrentVotes();
    BStatsMetricsBungee metrics = new BStatsMetricsBungee(this, 9453);
    metrics.addCustomChart(new BStatsMetricsBungee.SimplePie("bungee_method", () -> getConfig().getBungeeMethod().toString()));
    metrics.addCustomChart(new BStatsMetricsBungee.SimplePie("sendtoallservers", () -> "" + getConfig().getSendVotesToAllServers()));
    metrics.addCustomChart(new BStatsMetricsBungee.SimplePie("allowunjoined", () -> "" + getConfig().getAllowUnJoined()));
    metrics.addCustomChart(new BStatsMetricsBungee.SimplePie("pointsonvote", () -> "" + getConfig().getPointsOnVote()));
    metrics.addCustomChart(new BStatsMetricsBungee.SimplePie("bungeemanagetotals", () -> "" + getConfig().getBungeeManageTotals()));
    metrics.addCustomChart(new BStatsMetricsBungee.SimplePie("waitforuseronline", () -> "" + getConfig().getWaitForUserOnline()));
    if (!buildNumber.equals("NOTSET")) {
        metrics.addCustomChart(new BStatsMetricsBungee.SimplePie("dev_build_number", () -> "" + buildNumber));
    }
    loadVersionFile();
    getLogger().info("VotingPlugin loaded, using method: " + method.toString() + ", PluginMessagingVersion: " + BungeeVersion.getPluginMessageVersion());
    if (!buildNumber.equals("NOTSET")) {
        getLogger().info("Detected using dev build number: " + buildNumber);
    }
}
Also used : Configuration(net.md_5.bungee.config.Configuration) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) SocketHandler(com.bencodez.advancedcore.bungeeapi.sockets.SocketHandler) SocketReceiver(com.bencodez.advancedcore.bungeeapi.sockets.SocketReceiver) ArrayList(java.util.ArrayList) DataValueString(com.bencodez.advancedcore.api.user.usercache.value.DataValueString) EncryptionHandler(com.bencodez.advancedcore.api.misc.encryption.EncryptionHandler) List(java.util.List) ArrayList(java.util.ArrayList) ClientHandler(com.bencodez.advancedcore.bungeeapi.sockets.ClientHandler) SQLException(java.sql.SQLException) File(java.io.File)

Example 2 with SocketHandler

use of com.bencodez.advancedcore.bungeeapi.sockets.SocketHandler in project VotingPlugin by Ben12345rocks.

the class VotingPluginVelocity method onProxyInitialization.

@Subscribe
public void onProxyInitialization(ProxyInitializeEvent event) {
    File configFile = new File(dataDirectory.toFile(), "bungeeconfig.yml");
    configFile.getParentFile().mkdirs();
    if (!configFile.exists()) {
        try {
            configFile.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
        }
        InputStream toCopyStream = VotingPluginVelocity.class.getClassLoader().getResourceAsStream("bungeeconfig.yml");
        try (FileOutputStream fos = new FileOutputStream(configFile)) {
            byte[] buf = new byte[2048];
            int r;
            while (-1 != (r = toCopyStream.read(buf))) {
                fos.write(buf, 0, r);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    config = new Config(configFile);
    server.getChannelRegistrar().register(CHANNEL);
    method = BungeeMethod.getByName(config.getBungeeMethod());
    boolean mysqlLoaded = true;
    try {
        if (!config.getString(config.getNode("Host"), "").isEmpty()) {
            loadMysql();
        } else {
            mysqlLoaded = false;
            logger.error("MySQL settings not set in bungeeconfig.yml");
        }
    } catch (Exception e) {
        mysqlLoaded = false;
        e.printStackTrace();
    }
    CommandMeta meta = server.getCommandManager().metaBuilder("votingpluginbungee").aliases("vpb").build();
    server.getCommandManager().register(meta, new VotingPluginVelocityCommand(this));
    if (mysqlLoaded) {
        uuidPlayerNameCache = mysql.getRowsUUIDNameQuery();
        voteCacheFile = new VoteCache(new File(dataDirectory.toFile(), "votecache.yml"));
        nonVotedPlayersCache = new NonVotedPlayersCache(new File(dataDirectory.toFile(), "nonvotedplayerscache.yml"), this);
        timeHandle = new TimeHandle(voteCacheFile.getNode("Time", "Month").getString(""), voteCacheFile.getNode("Time", "Day").getInt(), voteCacheFile.getNode("Time", "Week").getInt()) {

            @Override
            public void save() {
                voteCacheFile.getNode("Time", "Month").setValue(timeHandle.getMonth());
                voteCacheFile.getNode("Time", "Day").setValue(timeHandle.getDay());
                voteCacheFile.getNode("Time", "Week").setValue(timeHandle.getWeek());
            }
        };
        if (method.equals(BungeeMethod.PLUGINMESSAGING)) {
            try {
                for (String serverToCheck : voteCacheFile.getServers()) {
                    ArrayList<OfflineBungeeVote> vote = new ArrayList<OfflineBungeeVote>();
                    for (String num : voteCacheFile.getServerVotes(serverToCheck)) {
                        ConfigurationNode data = voteCacheFile.getServerVotes(serverToCheck, num);
                        vote.add(new OfflineBungeeVote(data.getNode("Name").getString(), data.getNode("UUID").getString(), data.getNode("Service").getString(), data.getNode("Time").getLong(), data.getNode("Real").getBoolean(), data.getNode("TEXT").getString()));
                    }
                    cachedVotes.put(server.getServer(serverToCheck).get(), vote);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            try {
                for (String player : voteCacheFile.getPlayers()) {
                    ArrayList<OfflineBungeeVote> vote = new ArrayList<OfflineBungeeVote>();
                    for (String num : voteCacheFile.getOnlineVotes(player)) {
                        ConfigurationNode data = voteCacheFile.getOnlineVotes(player, num);
                        vote.add(new OfflineBungeeVote(data.getNode("Name").getString(), data.getNode("UUID").getString(), data.getNode("Service").getString(), data.getNode("Time").getLong(), data.getNode("Real").getBoolean(), data.getNode("TEXT").getString()));
                    }
                    cachedOnlineVotes.put(player, vote);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            voteCacheFile.clearData();
            server.getScheduler().buildTask(this, () -> {
                for (RegisteredServer server : cachedVotes.keySet()) {
                    checkCachedVotes(server);
                }
                for (String player : cachedOnlineVotes.keySet()) {
                    if (server.getPlayer(UUID.fromString(player)).isPresent()) {
                        checkOnlineVotes(server.getPlayer(UUID.fromString(player)).get(), player, null);
                    }
                }
            }).delay(15L, TimeUnit.SECONDS).repeat(30l, TimeUnit.SECONDS).schedule();
            server.getScheduler().buildTask(this, () -> {
                if (nonVotedPlayersCache != null) {
                    debug("Checking nonvotedplayerscache.yml...");
                    nonVotedPlayersCache.check();
                }
            }).delay(1L, TimeUnit.MINUTES).repeat(60l, TimeUnit.MINUTES).schedule();
        } else if (method.equals(BungeeMethod.SOCKETS)) {
            encryptionHandler = new EncryptionHandler(new File(dataDirectory.toFile(), "secretkey.key"));
            socketHandler = new SocketHandler(server.getPluginManager().getPlugin("votingplugin").get().getDescription().getVersion().get(), config.getBungeeHost(), config.getBungeePort(), encryptionHandler, config.getDebug());
            socketHandler.add(new SocketReceiver() {

                @Override
                public void onReceive(String[] data) {
                    if (data.length > 1) {
                        if (data.length > 2) {
                            if (data[0].equalsIgnoreCase("Broadcast")) {
                                sendServerMessage(data);
                            }
                        }
                    }
                }
            });
            socketHandler.add(new SocketReceiver() {

                @Override
                public void onReceive(String[] data) {
                    if (data.length > 1) {
                        if (data[0].equalsIgnoreCase("StatusOkay")) {
                            String server = data[1];
                            logger.info("Voting communicaton okay with " + server);
                        }
                    }
                }
            });
            clientHandles = new HashMap<String, ClientHandler>();
            List<String> l = config.getBlockedServers();
            for (ConfigurationNode d : config.getSpigotServers()) {
                String s = d.getKey().toString();
                if (!l.contains(s)) {
                    clientHandles.put(s, new ClientHandler(d.getNode("Host").getString("0.0.0.0"), d.getNode("Port").getInt(1298), encryptionHandler, config.getDebug()));
                }
            }
        }
    }
    currentVotePartyVotesRequired = getConfig().getVotePartyVotesRequired() + voteCacheFile.getVotePartyInreaseVotesRequired();
    votePartyVotes = voteCacheFile.getVotePartyCurrentVotes();
    try {
        getVersionFile();
        if (versionFile != null) {
            versionFile.delete();
            versionFile.getParentFile().delete();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    Metrics metrics = metricsFactory.make(this, 11547);
    metrics.addCustomChart(new SimplePie("bungee_method", () -> getConfig().getBungeeMethod().toString()));
    metrics.addCustomChart(new SimplePie("sendtoallservers", () -> "" + getConfig().getSendVotesToAllServers()));
    metrics.addCustomChart(new SimplePie("allowunjoined", () -> "" + getConfig().getAllowUnJoined()));
    metrics.addCustomChart(new SimplePie("pointsonvote", () -> "" + getConfig().getPointsOnVote()));
    metrics.addCustomChart(new SimplePie("bungeemanagetotals", () -> "" + getConfig().getBungeeManageTotals()));
    metrics.addCustomChart(new SimplePie("waitforuseronline", () -> "" + getConfig().getWaitForUserOnline()));
    metrics.addCustomChart(new SimplePie("plugin_version", () -> "" + version));
    if (!buildNumber.equals("NOTSET")) {
        metrics.addCustomChart(new SimplePie("dev_build_number", () -> "" + buildNumber));
    }
    logger.info("VotingPlugin velocity loaded, method: " + method.toString() + ", PluginMessagingVersion: " + BungeeVersion.getPluginMessageVersion() + ", Internal Jar Version: " + version);
    if (!buildNumber.equals("NOTSET")) {
        logger.info("Detected using dev build number: " + buildNumber);
    }
}
Also used : ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) SocketHandler(com.bencodez.advancedcore.bungeeapi.sockets.SocketHandler) SocketReceiver(com.bencodez.advancedcore.bungeeapi.sockets.SocketReceiver) ArrayList(java.util.ArrayList) SimplePie(org.bstats.charts.SimplePie) DataValueString(com.bencodez.advancedcore.api.user.usercache.value.DataValueString) EncryptionHandler(com.bencodez.advancedcore.api.misc.encryption.EncryptionHandler) Metrics(org.bstats.velocity.Metrics) RegisteredServer(com.velocitypowered.api.proxy.server.RegisteredServer) List(java.util.List) ArrayList(java.util.ArrayList) CommandMeta(com.velocitypowered.api.command.CommandMeta) ByteArrayInputStream(java.io.ByteArrayInputStream) DataInputStream(java.io.DataInputStream) ZipInputStream(java.util.zip.ZipInputStream) InputStream(java.io.InputStream) OfflineBungeeVote(com.bencodez.votingplugin.bungee.OfflineBungeeVote) ClientHandler(com.bencodez.advancedcore.bungeeapi.sockets.ClientHandler) IOException(java.io.IOException) SQLException(java.sql.SQLException) IOException(java.io.IOException) TimeHandle(com.bencodez.votingplugin.bungee.TimeHandle) ConfigurationNode(ninja.leaping.configurate.ConfigurationNode) FileOutputStream(java.io.FileOutputStream) File(java.io.File) Subscribe(com.velocitypowered.api.event.Subscribe)

Example 3 with SocketHandler

use of com.bencodez.advancedcore.bungeeapi.sockets.SocketHandler in project VotingPlugin by Ben12345rocks.

the class BungeeHandler method load.

public void load() {
    plugin.debug("Loading bungee handler");
    method = BungeeMethod.getByName(plugin.getBungeeSettings().getBungeeMethod());
    plugin.getLogger().info("Using BungeeMethod: " + method.toString());
    if (method.equals(BungeeMethod.MYSQL)) {
        plugin.registerBungeeChannels("vp:vp");
    } else if (method.equals(BungeeMethod.PLUGINMESSAGING)) {
        plugin.registerBungeeChannels("vp:vp");
        bungeeVotePartyCurrent = plugin.getServerData().getBungeeVotePartyCurrent();
        bungeeVotePartyRequired = plugin.getServerData().getBungeeVotePartyRequired();
        plugin.getPluginMessaging().setDebug(plugin.getBungeeSettings().isBungeeDebug());
        plugin.getPluginMessaging().add(new PluginMessageHandler("Vote") {

            @Override
            public void onRecieve(String subChannel, ArrayList<String> args) {
                if (args.size() > 8) {
                    int bungeeVersion = Integer.parseInt(args.get(8));
                    if (bungeeVersion != BungeeVersion.getPluginMessageVersion()) {
                        plugin.getLogger().warning("Incompatible version with bungee, please update all servers");
                        return;
                    }
                    String player = args.get(0);
                    String uuid = args.get(1);
                    String service = args.get(2);
                    long time = Long.parseLong(args.get(3));
                    plugin.debug("pluginmessaging vote received from " + player + "/" + uuid + " on " + service);
                    VotingPluginUser user = UserManager.getInstance().getVotingPluginUser(UUID.fromString(uuid), player);
                    boolean wasOnline = Boolean.valueOf(args.get(4));
                    BungeeMessageData text = new BungeeMessageData(args.get(6));
                    bungeeVotePartyCurrent = text.getVotePartyCurrent();
                    bungeeVotePartyRequired = text.getVotePartyRequired();
                    plugin.getPlaceholders().onBungeeVotePartyUpdate();
                    boolean setTotals = Boolean.valueOf(args.get(7));
                    user.clearCache();
                    boolean broadcast = true;
                    boolean bungeeBroadcast = false;
                    if (args.size() > 9) {
                        bungeeBroadcast = Boolean.valueOf(args.get(9));
                    }
                    if (!bungeeBroadcast) {
                        if (!plugin.getBungeeSettings().isBungeeBroadcast() && !plugin.getBungeeSettings().isDisableBroadcast()) {
                            if (wasOnline || plugin.getBungeeSettings().isBungeeBroadcastAlways()) {
                                VoteSite site = plugin.getVoteSite(service, true);
                                if (site != null) {
                                    site.broadcastVote(user, false);
                                    broadcast = false;
                                } else {
                                    plugin.getLogger().warning("No votesite for " + service);
                                }
                            }
                        }
                    } else {
                        broadcast = false;
                    }
                    user.bungeeVotePluginMessaging(service, time, text, !setTotals, wasOnline, broadcast);
                    if (plugin.getBungeeSettings().isPerServerPoints()) {
                        user.addPoints(plugin.getConfigFile().getPointsOnVote());
                    }
                    if (Boolean.valueOf(args.get(5))) {
                        plugin.getServerData().addServiceSite(service);
                    }
                } else {
                    plugin.getLogger().warning("Incompatible version with bungee, please update all servers");
                }
            }
        });
        plugin.getPluginMessaging().add(new PluginMessageHandler("VoteOnline") {

            @Override
            public void onRecieve(String subChannel, ArrayList<String> args) {
                if (args.size() > 8) {
                    int bungeeVersion = Integer.parseInt(args.get(8));
                    if (bungeeVersion != BungeeVersion.getPluginMessageVersion()) {
                        plugin.getLogger().warning("Incompatible version with bungee, please update all servers");
                        return;
                    }
                    String player = args.get(0);
                    String uuid = args.get(1);
                    String service = args.get(2);
                    long time = Long.parseLong(args.get(3));
                    BungeeMessageData text = new BungeeMessageData(args.get(6));
                    bungeeVotePartyCurrent = text.getVotePartyCurrent();
                    bungeeVotePartyRequired = text.getVotePartyRequired();
                    plugin.getPlaceholders().onBungeeVotePartyUpdate();
                    plugin.debug("pluginmessaging voteonline received from " + player + "/" + uuid + " on " + service);
                    VotingPluginUser user = UserManager.getInstance().getVotingPluginUser(UUID.fromString(uuid), player);
                    user.clearCache();
                    if (plugin.getBungeeSettings().isPerServerPoints()) {
                        user.addPoints(plugin.getConfigFile().getPointsOnVote());
                    }
                    boolean setTotals = Boolean.valueOf(args.get(7));
                    boolean wasOnline = Boolean.valueOf(args.get(4));
                    boolean broadcast = true;
                    boolean bungeeBroadcast = false;
                    if (args.size() > 9) {
                        bungeeBroadcast = Boolean.valueOf(args.get(9));
                    }
                    if (!bungeeBroadcast) {
                        if (!plugin.getBungeeSettings().isBungeeBroadcast() && !plugin.getBungeeSettings().isDisableBroadcast()) {
                            if (Boolean.valueOf(args.get(4)) || plugin.getBungeeSettings().isBungeeBroadcastAlways()) {
                                VoteSite site = plugin.getVoteSite(service, true);
                                if (site != null) {
                                    site.broadcastVote(user, false);
                                    broadcast = false;
                                } else {
                                    plugin.getLogger().warning("No votesite for " + service);
                                }
                            }
                        }
                    } else {
                        broadcast = false;
                    }
                    user.bungeeVotePluginMessaging(service, time, text, !setTotals, wasOnline, broadcast);
                    if (plugin.getBungeeSettings().isPerServerPoints()) {
                        user.addPoints(plugin.getConfigFile().getPointsOnVote());
                    }
                    if (Boolean.valueOf(args.get(5))) {
                        plugin.getServerData().addServiceSite(service);
                    }
                } else {
                    plugin.getLogger().warning("Incompatible version with bungee, please update all servers");
                }
            }
        });
        plugin.getPluginMessaging().add(new PluginMessageHandler("VoteUpdate") {

            @Override
            public void onRecieve(String subChannel, ArrayList<String> args) {
                String player = args.get(0);
                plugin.debug("pluginmessaging voteupdate received for " + player);
                VotingPluginUser user = UserManager.getInstance().getVotingPluginUser(UUID.fromString(player));
                user.clearCache();
                user.offVote();
                if (args.size() > 2) {
                    bungeeVotePartyCurrent = Integer.parseInt(args.get(1));
                    bungeeVotePartyRequired = Integer.parseInt(args.get(2));
                }
                plugin.setUpdate(true);
            }
        });
        plugin.getPluginMessaging().add(new PluginMessageHandler("VoteBroadcast") {

            @Override
            public void onRecieve(String subChannel, ArrayList<String> args) {
                if (args.size() > 2) {
                    String uuid = args.get(0);
                    String service = args.get(2);
                    VotingPluginUser user = UserManager.getInstance().getVotingPluginUser(UUID.fromString(uuid), args.get(1));
                    VoteSite site = plugin.getVoteSite(service, true);
                    if (site != null) {
                        site.broadcastVote(user, false);
                    } else {
                        plugin.getLogger().warning("No votesite for " + service);
                    }
                }
            }
        });
        plugin.getPluginMessaging().add(new PluginMessageHandler("Status") {

            @Override
            public void onRecieve(String subChannel, ArrayList<String> args) {
                String server = args.get(0);
                plugin.getPluginMessaging().sendPluginMessage("statusokay", server);
            }
        });
        plugin.getPluginMessaging().add(new PluginMessageHandler("VotePartyBungee") {

            @Override
            public void onRecieve(String subChannel, ArrayList<String> args) {
                for (Player p : Bukkit.getOnlinePlayers()) {
                    new RewardBuilder(plugin.getBungeeSettings().getData(), "BungeeVotePartyRewards").send(p);
                }
            }
        });
        plugin.getPluginMessaging().add(new PluginMessageHandler("VotePartyBroadcast") {

            @Override
            public void onRecieve(String subChannel, ArrayList<String> args) {
                String broadcast = args.get(0);
                MiscUtils.getInstance().broadcast(broadcast);
            }
        });
    } else if (method.equals(BungeeMethod.SOCKETS)) {
        encryptionHandler = new EncryptionHandler(new File(plugin.getDataFolder(), "secretkey.key"));
        clientHandler = new ClientHandler(plugin.getBungeeSettings().getBungeeServerHost(), plugin.getBungeeSettings().getBungeeServerPort(), encryptionHandler, plugin.getBungeeSettings().isBungeeDebug());
        socketHandler = new SocketHandler(plugin.getVersion(), plugin.getBungeeSettings().getSpigotServerHost(), plugin.getBungeeSettings().getSpigotServerPort(), encryptionHandler, plugin.getBungeeSettings().isBungeeDebug());
        socketHandler.add(new SocketReceiver("bungeevote") {

            @Override
            public void onReceive(String[] data) {
                if (data.length > 5) {
                    plugin.extraDebug("BungeeVote from " + data[2] + ", processing");
                    String uuid = data[1];
                    VotingPluginUser user = null;
                    if (!uuid.isEmpty()) {
                        user = UserManager.getInstance().getVotingPluginUser(UUID.fromString(uuid));
                    } else {
                        user = UserManager.getInstance().getVotingPluginUser(data[2]);
                    }
                    user.clearCache();
                    if (plugin.getBungeeSettings().isPerServerPoints()) {
                        user.addPoints(plugin.getConfigFile().getPointsOnVote());
                    }
                    user.bungeeVote(data[3], new BungeeMessageData(data[4]), !Boolean.valueOf(data[5]));
                }
            }
        });
        socketHandler.add(new SocketReceiver("bungeevoteonline") {

            @Override
            public void onReceive(String[] data) {
                if (data.length > 5) {
                    plugin.extraDebug("BungeeVoteOnline from " + data[2] + ", processing");
                    String uuid = data[1];
                    VotingPluginUser user = null;
                    if (!uuid.isEmpty()) {
                        user = UserManager.getInstance().getVotingPluginUser(UUID.fromString(uuid));
                    } else {
                        user = UserManager.getInstance().getVotingPluginUser(data[2]);
                    }
                    user.clearCache();
                    if (plugin.getBungeeSettings().isPerServerPoints()) {
                        user.addPoints(plugin.getConfigFile().getPointsOnVote());
                    }
                    user.bungeeVoteOnline(data[3], new BungeeMessageData(data[4]), !Boolean.valueOf(data[5]));
                }
            }
        });
        socketHandler.add(new SocketReceiver("BungeeBroadcast") {

            @Override
            public void onReceive(String[] data) {
                if (data.length > 2) {
                    VoteSite site = plugin.getVoteSite(data[1], true);
                    String p = data[3];
                    VotingPluginUser user = UserManager.getInstance().getVotingPluginUser(p);
                    if (site != null) {
                        site.broadcastVote(user, false);
                    } else {
                        plugin.getLogger().warning("No votesite for " + data[1]);
                    }
                }
            }
        });
        socketHandler.add(new SocketReceiver("Status") {

            @Override
            public void onReceive(String[] data) {
                if (data.length > 0) {
                    plugin.getLogger().info("Received status command, sending status back");
                    sendData("StatusOkay", plugin.getOptions().getServer());
                }
            }
        });
        socketHandler.add(new SocketReceiver("BungeeUpdate") {

            @Override
            public void onReceive(String[] data) {
                plugin.setUpdate(true);
            }
        });
        socketHandler.add(new SocketReceiver("VotePartyBungee") {

            @Override
            public void onReceive(String[] data) {
                for (Player p : Bukkit.getOnlinePlayers()) {
                    new RewardBuilder(plugin.getBungeeSettings().getData(), "BungeeVotePartyRewards").send(p);
                }
            }
        });
        socketHandler.add(new SocketReceiver("VotePartyBroadcast") {

            @Override
            public void onReceive(String[] data) {
                String broadcast = data[0];
                MiscUtils.getInstance().broadcast(broadcast);
            }
        });
        if (plugin.getOptions().getServer().equalsIgnoreCase("pleaseset")) {
            plugin.getLogger().warning("Server name for bungee voting is not set, please set it");
        }
    }
}
Also used : Player(org.bukkit.entity.Player) SocketHandler(com.bencodez.advancedcore.bungeeapi.sockets.SocketHandler) SocketReceiver(com.bencodez.advancedcore.bungeeapi.sockets.SocketReceiver) ArrayList(java.util.ArrayList) ClientHandler(com.bencodez.advancedcore.bungeeapi.sockets.ClientHandler) PluginMessageHandler(com.bencodez.advancedcore.bungeeapi.pluginmessage.PluginMessageHandler) EncryptionHandler(com.bencodez.advancedcore.api.misc.encryption.EncryptionHandler) VoteSite(com.bencodez.votingplugin.objects.VoteSite) RewardBuilder(com.bencodez.advancedcore.api.rewards.RewardBuilder) BungeeMessageData(com.bencodez.votingplugin.bungee.BungeeMessageData) File(java.io.File) VotingPluginUser(com.bencodez.votingplugin.user.VotingPluginUser)

Aggregations

EncryptionHandler (com.bencodez.advancedcore.api.misc.encryption.EncryptionHandler)3 ClientHandler (com.bencodez.advancedcore.bungeeapi.sockets.ClientHandler)3 SocketHandler (com.bencodez.advancedcore.bungeeapi.sockets.SocketHandler)3 SocketReceiver (com.bencodez.advancedcore.bungeeapi.sockets.SocketReceiver)3 File (java.io.File)3 ArrayList (java.util.ArrayList)3 DataValueString (com.bencodez.advancedcore.api.user.usercache.value.DataValueString)2 SQLException (java.sql.SQLException)2 HashMap (java.util.HashMap)2 List (java.util.List)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 RewardBuilder (com.bencodez.advancedcore.api.rewards.RewardBuilder)1 PluginMessageHandler (com.bencodez.advancedcore.bungeeapi.pluginmessage.PluginMessageHandler)1 BungeeMessageData (com.bencodez.votingplugin.bungee.BungeeMessageData)1 OfflineBungeeVote (com.bencodez.votingplugin.bungee.OfflineBungeeVote)1 TimeHandle (com.bencodez.votingplugin.bungee.TimeHandle)1 VoteSite (com.bencodez.votingplugin.objects.VoteSite)1 VotingPluginUser (com.bencodez.votingplugin.user.VotingPluginUser)1 CommandMeta (com.velocitypowered.api.command.CommandMeta)1 Subscribe (com.velocitypowered.api.event.Subscribe)1