Search in sources :

Example 6 with Subscribe

use of com.velocitypowered.api.event.Subscribe 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 7 with Subscribe

use of com.velocitypowered.api.event.Subscribe in project VotingPlugin by Ben12345rocks.

the class VotingPluginVelocity method onVotifierEvent.

@Subscribe
public void onVotifierEvent(VotifierEvent event) {
    final String serviceSiteVote = event.getVote().getServiceName();
    final String name = event.getVote().getUsername();
    timer.schedule(new TimerTask() {

        @Override
        public void run() {
            String serviceSite = serviceSiteVote;
            logger.info("Vote received " + name + " from service site " + serviceSite);
            if (serviceSite.isEmpty()) {
                serviceSite = "Empty";
            }
            vote(name, serviceSite, true);
        }
    }, 0);
}
Also used : TimerTask(java.util.TimerTask) DataValueString(com.bencodez.advancedcore.api.user.usercache.value.DataValueString) Subscribe(com.velocitypowered.api.event.Subscribe)

Example 8 with Subscribe

use of com.velocitypowered.api.event.Subscribe in project commands by aikar.

the class ACFVelocityListener method onDisconnect.

@Subscribe
public void onDisconnect(DisconnectEvent disconnectEvent) {
    // cleanup
    Player player = disconnectEvent.getPlayer();
    manager.issuersLocale.remove(player.getUniqueId());
}
Also used : Player(com.velocitypowered.api.proxy.Player) Subscribe(com.velocitypowered.api.event.Subscribe)

Example 9 with Subscribe

use of com.velocitypowered.api.event.Subscribe in project LuckPerms by lucko.

the class PluginMessageMessenger method onPluginMessage.

@Subscribe
public void onPluginMessage(PluginMessageEvent e) {
    // are able to support both. :)
    if (!e.getIdentifier().getId().equals(CHANNEL.getId())) {
        return;
    }
    e.setResult(ForwardResult.handled());
    if (e.getSource() instanceof Player) {
        return;
    }
    ByteArrayDataInput in = e.dataAsDataStream();
    String msg = in.readUTF();
    if (this.consumer.consumeIncomingMessageAsString(msg)) {
        // Forward to other servers
        this.plugin.getBootstrap().getScheduler().executeAsync(() -> dispatchMessage(e.getData()));
    }
}
Also used : Player(com.velocitypowered.api.proxy.Player) ByteArrayDataInput(com.google.common.io.ByteArrayDataInput) Subscribe(com.velocitypowered.api.event.Subscribe)

Aggregations

Subscribe (com.velocitypowered.api.event.Subscribe)9 Player (com.velocitypowered.api.proxy.Player)6 DataValueString (com.bencodez.advancedcore.api.user.usercache.value.DataValueString)4 RegisteredServer (com.velocitypowered.api.proxy.server.RegisteredServer)3 OfflineBungeeVote (com.bencodez.votingplugin.bungee.OfflineBungeeVote)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 DataInputStream (java.io.DataInputStream)2 IOException (java.io.IOException)2 SQLException (java.sql.SQLException)2 ArrayList (java.util.ArrayList)2 TimerTask (java.util.TimerTask)2 User (me.lucko.luckperms.common.model.User)2 EncryptionHandler (com.bencodez.advancedcore.api.misc.encryption.EncryptionHandler)1 ClientHandler (com.bencodez.advancedcore.bungeeapi.sockets.ClientHandler)1 SocketHandler (com.bencodez.advancedcore.bungeeapi.sockets.SocketHandler)1 SocketReceiver (com.bencodez.advancedcore.bungeeapi.sockets.SocketReceiver)1 TimeHandle (com.bencodez.votingplugin.bungee.TimeHandle)1 ByteArrayDataInput (com.google.common.io.ByteArrayDataInput)1 CommandMeta (com.velocitypowered.api.command.CommandMeta)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1