Search in sources :

Example 1 with Player

use of com.velocitypowered.api.proxy.Player in project VotingPlugin by Ben12345rocks.

the class VotingPluginVelocity method checkCachedVotes.

public synchronized void checkCachedVotes(RegisteredServer serverToCheck) {
    if (!serverToCheck.getPlayersConnected().isEmpty()) {
        if (cachedVotes.containsKey(serverToCheck) && !config.getBlockedServers().contains(serverToCheck.getServerInfo().getName())) {
            ArrayList<OfflineBungeeVote> c = cachedVotes.get(serverToCheck);
            ArrayList<OfflineBungeeVote> newSet = new ArrayList<OfflineBungeeVote>();
            if (!c.isEmpty()) {
                for (OfflineBungeeVote cache : c) {
                    boolean toSend = true;
                    if (getConfig().getWaitForUserOnline()) {
                        Player p = null;
                        if (server.getPlayer(UUID.fromString(cache.getUuid())).isPresent()) {
                            p = server.getPlayer(UUID.fromString(cache.getUuid())).get();
                        }
                        if (p == null || !p.isActive()) {
                            toSend = false;
                        } else if (p != null && p.isActive() && !p.getCurrentServer().get().getServerInfo().getName().equals(serverToCheck.getServerInfo().getName())) {
                            toSend = false;
                        }
                    }
                    if (toSend) {
                        sendPluginMessageServer(serverToCheck, "Vote", cache.getPlayerName(), cache.getUuid(), cache.getService(), "" + cache.getTime(), Boolean.FALSE.toString(), "" + cache.isRealVote(), cache.getText(), "" + getConfig().getBungeeManageTotals(), "" + BungeeVersion.getPluginMessageVersion(), "" + config.getBroadcast());
                    } else {
                        debug("Not sending vote because user isn't on server " + serverToCheck + ": " + cache.toString());
                        newSet.add(cache);
                    }
                }
                cachedVotes.put(serverToCheck, newSet);
            }
        }
    }
}
Also used : Player(com.velocitypowered.api.proxy.Player) OfflineBungeeVote(com.bencodez.votingplugin.bungee.OfflineBungeeVote) ArrayList(java.util.ArrayList)

Example 2 with Player

use of com.velocitypowered.api.proxy.Player 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 Player

use of com.velocitypowered.api.proxy.Player in project VotingPlugin by Ben12345rocks.

the class VotingPluginVelocity method sendSocketVote.

public void sendSocketVote(String name, String service, BungeeMessageData text) {
    String uuid = getUUID(name);
    if (config.getSendVotesToAllServers()) {
        sendServerMessage("bungeevote", uuid, name, service, text.toString(), "" + getConfig().getBungeeManageTotals());
        if (config.getBroadcast()) {
            sendServerMessage("BungeeBroadcast", service, uuid, name);
        }
    } else {
        // online server only
        Player p = server.getPlayer(name).get();
        String server = "";
        if (p != null && p.isActive()) {
            server = p.getCurrentServer().get().getServerInfo().getName();
        } else {
            server = config.getFallBack();
        }
        if (config.getBlockedServers().contains(server)) {
            server = config.getFallBack();
        }
        sendServerMessageServer(server, "bungeevoteonline", uuid, name, service, text.toString(), "" + getConfig().getBungeeManageTotals());
        if (config.getBroadcast()) {
            sendServerMessage("BungeeBroadcast", service, uuid, name);
        }
        sendServerMessage("BungeeUpdate");
    }
}
Also used : Player(com.velocitypowered.api.proxy.Player) DataValueString(com.bencodez.advancedcore.api.user.usercache.value.DataValueString)

Example 4 with Player

use of com.velocitypowered.api.proxy.Player in project VotingPlugin by Ben12345rocks.

the class VotingPluginVelocity method vote.

public synchronized void vote(String player, String service, boolean realVote) {
    try {
        if (player == null || player.isEmpty()) {
            logger.info("No name from vote on " + service);
            return;
        }
        String uuid = getUUID(player);
        if (uuid.isEmpty()) {
            if (config.getAllowUnJoined()) {
                debug("Fetching UUID online, since allowunjoined is enabled");
                UUID u = null;
                try {
                    u = fetchUUID(player);
                } catch (Exception e) {
                    if (getConfig().getDebug()) {
                        e.printStackTrace();
                    }
                }
                if (u == null) {
                    debug("Failed to get uuid for " + player);
                    return;
                }
                uuid = u.toString();
            } else {
                logger.info("Ignoring vote from " + player + " since player hasn't joined before");
                return;
            }
        }
        player = getProperName(uuid, player);
        BungeeMessageData text = null;
        addVoteParty();
        if (getConfig().getBungeeManageTotals()) {
            if (mysql == null) {
                logger.error("Mysql is not loaded correctly, stopping vote processing");
                return;
            }
            if (!mysql.getUuids().contains(uuid)) {
                mysql.update(uuid, "PlayerName", new DataValueString(player));
            }
            ArrayList<Column> data = mysql.getExactQuery(new Column("uuid", new DataValueString(uuid)));
            int allTimeTotal = getValue(data, "AllTimeTotal", 1);
            int monthTotal = getValue(data, "MonthTotal", 1);
            int weeklyTotal = getValue(data, "WeeklyTotal", 1);
            int dailyTotal = getValue(data, "DailyTotal", 1);
            int points = getValue(data, "Points", getConfig().getPointsOnVote());
            int milestoneCount = getValue(data, "MilestoneCount", 1);
            text = new BungeeMessageData(allTimeTotal, monthTotal, weeklyTotal, dailyTotal, points, milestoneCount, votePartyVotes, currentVotePartyVotesRequired);
            ArrayList<Column> update = new ArrayList<Column>();
            update.add(new Column("AllTimeTotal", new DataValueInt(allTimeTotal)));
            update.add(new Column("MonthTotal", new DataValueInt(monthTotal)));
            update.add(new Column("WeeklyTotal", new DataValueInt(weeklyTotal)));
            update.add(new Column("DailyTotal", new DataValueInt(dailyTotal)));
            update.add(new Column("Points", new DataValueInt(points)));
            update.add(new Column("MilestoneCount", new DataValueInt(milestoneCount)));
            debug("Setting totals " + text.toString());
            mysql.update(uuid, update);
        } else {
            text = new BungeeMessageData(0, 0, 0, 0, 0, 0, votePartyVotes, currentVotePartyVotesRequired);
        }
        /*
			 * String text = mysqlUpdate(data, uuid, "AllTimeTotal", 1) + "//" +
			 * mysqlUpdate(data, uuid, "MonthTotal", 1) + "//" + mysqlUpdate(data, uuid,
			 * "WeeklyTotal", 1) + "//" + mysqlUpdate(data, uuid, "DailyTotal", 1) + "//" +
			 * mysqlUpdate(data, uuid, "Points", 1) + "//" + mysqlUpdate(data, uuid,
			 * "MilestoneCount", 1);
			 */
        long time = LocalDateTime.now().atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
        if (method.equals(BungeeMethod.PLUGINMESSAGING)) {
            if (config.getSendVotesToAllServers()) {
                for (RegisteredServer s : server.getAllServers()) {
                    if (!config.getBlockedServers().contains(s.getServerInfo().getName())) {
                        boolean forceCache = false;
                        Player p = null;
                        if (server.getPlayer(UUID.fromString(uuid)).isPresent()) {
                            p = server.getPlayer(UUID.fromString(uuid)).get();
                        }
                        if ((p == null || !p.isActive()) && getConfig().getWaitForUserOnline()) {
                            forceCache = true;
                            debug("Forcing vote to cache");
                        }
                        if (s.getPlayersConnected().isEmpty() || forceCache) {
                            // cache
                            if (!cachedVotes.containsKey(s)) {
                                cachedVotes.put(s, new ArrayList<OfflineBungeeVote>());
                            }
                            ArrayList<OfflineBungeeVote> list = cachedVotes.get(s);
                            list.add(new OfflineBungeeVote(player, uuid, service, time, realVote, text.toString()));
                            cachedVotes.put(s, list);
                            debug("Caching vote for " + player + " on " + service + " for " + s);
                        } else {
                            // send
                            sendPluginMessageServer(s, "Vote", player, uuid, service, "" + time, Boolean.TRUE.toString(), "" + realVote, text.toString(), "" + getConfig().getBungeeManageTotals(), "" + BungeeVersion.getPluginMessageVersion(), "" + config.getBroadcast());
                        }
                        if (config.getBroadcast()) {
                            sendPluginMessageServer(s, "VoteBroadcast", uuid, player, service);
                        }
                    }
                }
            } else {
                Player p = null;
                if (server.getPlayer(UUID.fromString(uuid)).isPresent()) {
                    p = server.getPlayer(UUID.fromString(uuid)).get();
                }
                if (p != null && p.isActive() && !config.getBlockedServers().contains(p.getCurrentServer().get().getServerInfo().getName())) {
                    sendPluginMessageServer(p.getCurrentServer().get().getServer(), "VoteOnline", player, uuid, service, "" + time, Boolean.TRUE.toString(), "" + realVote, text.toString(), "" + getConfig().getBungeeManageTotals(), "" + BungeeVersion.getPluginMessageVersion(), "" + config.getBroadcast());
                } else {
                    if (!cachedOnlineVotes.containsKey(uuid)) {
                        cachedOnlineVotes.put(uuid, new ArrayList<OfflineBungeeVote>());
                    }
                    ArrayList<OfflineBungeeVote> list = cachedOnlineVotes.get(uuid);
                    if (list == null) {
                        list = new ArrayList<OfflineBungeeVote>();
                    }
                    list.add(new OfflineBungeeVote(player, uuid, service, time, realVote, text.toString()));
                    cachedOnlineVotes.put(uuid, list);
                    debug("Caching online vote for " + player + " on " + service);
                }
                for (RegisteredServer s : server.getAllServers()) {
                    sendPluginMessageServer(s, "VoteUpdate", uuid, "" + votePartyVotes, "" + currentVotePartyVotesRequired);
                    if (config.getBroadcast()) {
                        sendPluginMessageServer(s, "VoteBroadcast", uuid, player, service);
                    }
                }
            }
        } else if (method.equals(BungeeMethod.SOCKETS)) {
            sendSocketVote(player, service, text);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : Player(com.velocitypowered.api.proxy.Player) OfflineBungeeVote(com.bencodez.votingplugin.bungee.OfflineBungeeVote) ArrayList(java.util.ArrayList) DataValueString(com.bencodez.advancedcore.api.user.usercache.value.DataValueString) SQLException(java.sql.SQLException) IOException(java.io.IOException) DataValueInt(com.bencodez.advancedcore.api.user.usercache.value.DataValueInt) Column(com.bencodez.advancedcore.api.user.userstorage.Column) RegisteredServer(com.velocitypowered.api.proxy.server.RegisteredServer) DataValueString(com.bencodez.advancedcore.api.user.usercache.value.DataValueString) UUID(java.util.UUID) BungeeMessageData(com.bencodez.votingplugin.bungee.BungeeMessageData)

Aggregations

Player (com.velocitypowered.api.proxy.Player)4 DataValueString (com.bencodez.advancedcore.api.user.usercache.value.DataValueString)3 OfflineBungeeVote (com.bencodez.votingplugin.bungee.OfflineBungeeVote)2 RegisteredServer (com.velocitypowered.api.proxy.server.RegisteredServer)2 IOException (java.io.IOException)2 SQLException (java.sql.SQLException)2 ArrayList (java.util.ArrayList)2 DataValueInt (com.bencodez.advancedcore.api.user.usercache.value.DataValueInt)1 Column (com.bencodez.advancedcore.api.user.userstorage.Column)1 BungeeMessageData (com.bencodez.votingplugin.bungee.BungeeMessageData)1 Subscribe (com.velocitypowered.api.event.Subscribe)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 DataInputStream (java.io.DataInputStream)1 DataOutputStream (java.io.DataOutputStream)1 TimerTask (java.util.TimerTask)1 UUID (java.util.UUID)1