Search in sources :

Example 1 with Subscribe

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

the class ACFVelocityListener method onPlayerJoin.

@Subscribe
public void onPlayerJoin(PostLoginEvent loginEvent) {
    Player player = loginEvent.getPlayer();
    // the client settings are sent after a successful login
    Runnable task = () -> manager.readLocale(player);
    proxy.getScheduler().buildTask(plugin, task).delay(1, TimeUnit.SECONDS).schedule();
}
Also used : Player(com.velocitypowered.api.proxy.Player) Subscribe(com.velocitypowered.api.event.Subscribe)

Example 2 with Subscribe

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

the class VotingPluginVelocity method onPluginMessagingReceived.

@Subscribe
public void onPluginMessagingReceived(PluginMessageEvent event) {
    if (event.getIdentifier().getId().equals(CHANNEL.getId())) {
        ByteArrayInputStream instream = new ByteArrayInputStream(event.getData());
        DataInputStream in = new DataInputStream(instream);
        try {
            ByteArrayOutputStream outstream = new ByteArrayOutputStream();
            DataOutputStream out = new DataOutputStream(outstream);
            String subchannel = in.readUTF();
            int size = in.readInt();
            // check for status message returns
            if (subchannel.equalsIgnoreCase("statusokay")) {
                String server = in.readUTF();
                logger.info("Status okay for " + server);
                return;
            } else if (subchannel.equalsIgnoreCase("login")) {
                String player = in.readUTF();
                debug("Login: " + player);
                if (server.getPlayer(player).isPresent()) {
                    Player p = server.getPlayer(player).get();
                    if (p.getCurrentServer().isPresent()) {
                        final RegisteredServer server = p.getCurrentServer().get().getServer();
                        final Player p1 = p;
                        timer.schedule(new TimerTask() {

                            @Override
                            public void run() {
                                checkCachedVotes(server);
                                checkOnlineVotes(p1, p.getUniqueId().toString(), server);
                            }
                        }, 0);
                    }
                }
                return;
            } else if (subchannel.equalsIgnoreCase("timeupdate")) {
                String str = in.readUTF();
                String[] data = str.split(Pattern.quote("//"));
                if (data.length == 3) {
                    timeHandle.setMonth(data[0]);
                    timeHandle.setDay(Integer.parseInt(data[1]));
                    timeHandle.setWeek(Integer.parseInt(data[2]));
                }
            } else {
                // reforward message
                out.writeUTF(subchannel);
                out.writeInt(size);
                for (int i = 0; i < size; i++) {
                    out.writeUTF(in.readUTF());
                }
                for (RegisteredServer send : server.getAllServers()) {
                    if (send.getPlayersConnected().size() > 0) {
                        send.sendPluginMessage(CHANNEL, outstream.toByteArray());
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
Also used : Player(com.velocitypowered.api.proxy.Player) TimerTask(java.util.TimerTask) ByteArrayInputStream(java.io.ByteArrayInputStream) DataOutputStream(java.io.DataOutputStream) RegisteredServer(com.velocitypowered.api.proxy.server.RegisteredServer) ByteArrayOutputStream(java.io.ByteArrayOutputStream) DataValueString(com.bencodez.advancedcore.api.user.usercache.value.DataValueString) DataInputStream(java.io.DataInputStream) SQLException(java.sql.SQLException) IOException(java.io.IOException) Subscribe(com.velocitypowered.api.event.Subscribe)

Example 3 with Subscribe

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

the class VotingPluginVelocity method onProxyDisable.

@Subscribe
public void onProxyDisable(ProxyShutdownEvent event) {
    if (method.equals(BungeeMethod.PLUGINMESSAGING)) {
        logger.info("VotingPlugin saving vote cache: " + cachedVotes.size() + "/" + cachedOnlineVotes.size());
        for (Entry<RegisteredServer, ArrayList<OfflineBungeeVote>> entry : cachedVotes.entrySet()) {
            RegisteredServer server = entry.getKey();
            int num = 0;
            for (OfflineBungeeVote voteData : entry.getValue()) {
                voteCacheFile.addVote(server.getServerInfo().getName(), num, voteData);
                num++;
            }
        }
        for (Entry<String, ArrayList<OfflineBungeeVote>> entry : cachedOnlineVotes.entrySet()) {
            String name = entry.getKey();
            int num = 0;
            for (OfflineBungeeVote voteData : entry.getValue()) {
                voteCacheFile.addVoteOnline(name, num, voteData);
                num++;
            }
        }
    }
    if (timeHandle != null) {
        timeHandle.save();
    }
    voteCacheFile.save();
    nonVotedPlayersCache.save();
    if (mysql != null) {
        mysql.shutDown();
    }
    logger.info("VotingPlugin disabled");
}
Also used : OfflineBungeeVote(com.bencodez.votingplugin.bungee.OfflineBungeeVote) RegisteredServer(com.velocitypowered.api.proxy.server.RegisteredServer) ArrayList(java.util.ArrayList) DataValueString(com.bencodez.advancedcore.api.user.usercache.value.DataValueString) Subscribe(com.velocitypowered.api.event.Subscribe)

Example 4 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 5 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)

Aggregations

Subscribe (com.velocitypowered.api.event.Subscribe)6 DataValueString (com.bencodez.advancedcore.api.user.usercache.value.DataValueString)4 Player (com.velocitypowered.api.proxy.Player)3 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 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 CommandMeta (com.velocitypowered.api.command.CommandMeta)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 DataOutputStream (java.io.DataOutputStream)1 File (java.io.File)1