From cf6c2987764a61113e9dfb100acda27b40c8e493 Mon Sep 17 00:00:00 2001 From: Lukas Date: Sun, 4 Jan 2015 15:14:35 +0100 Subject: [PATCH] *Added Vote (not ready) --- src/de/anura/core/AnuraCore.java | 1 + src/de/anura/core/Setup.java | 9 +- .../anura/core/events/VotifierListener.java | 111 ++++++++++++++++++ src/plugin.yml | 2 +- 4 files changed, 120 insertions(+), 3 deletions(-) create mode 100644 src/de/anura/core/events/VotifierListener.java diff --git a/src/de/anura/core/AnuraCore.java b/src/de/anura/core/AnuraCore.java index 1e823c1..c75a7d1 100644 --- a/src/de/anura/core/AnuraCore.java +++ b/src/de/anura/core/AnuraCore.java @@ -72,6 +72,7 @@ public class AnuraCore extends JavaPlugin { Setup.setupCommands(); if (this.getConfig().getBoolean("is-main-lobby")) { Setup.setupXPBattle(); + Setup.setupVotifier(); } this.getServer().getMessenger().registerIncomingPluginChannel(this, "BungeeCord", signs); this.getServer().getMessenger().registerIncomingPluginChannel(this, "BungeeCord", new Teleports()); diff --git a/src/de/anura/core/Setup.java b/src/de/anura/core/Setup.java index be27860..6296a7c 100644 --- a/src/de/anura/core/Setup.java +++ b/src/de/anura/core/Setup.java @@ -144,7 +144,7 @@ public class Setup { Core.getMainClass().sittableBlocks.add(l.getBlock()); } } - + Bukkit.getScheduler().scheduleSyncRepeatingTask(Core.getMainClass(), new Runnable() { @Override @@ -301,7 +301,7 @@ public class Setup { Errors.reportException(e); } } - + public static void setupXPBattle() { Scoreboard s = Bukkit.getScoreboardManager().getMainScoreboard(); if (s.getObjective("xpBattleStart") == null) { @@ -311,4 +311,9 @@ public class Setup { s.registerNewObjective("xpBattleIngame", "dummy"); } } + + public static void setupVotifier() { + AnuraCore.getInstance().getServer().getPluginManager().registerEvents(new de.anura.core.events.VotifierListener(), AnuraCore.getInstance()); + AnuraCore.getInstance().getServer().getMessenger().registerIncomingPluginChannel(AnuraCore.getInstance(), "BungeeCord", new VotifierListener()); + } } diff --git a/src/de/anura/core/events/VotifierListener.java b/src/de/anura/core/events/VotifierListener.java new file mode 100644 index 0000000..026c164 --- /dev/null +++ b/src/de/anura/core/events/VotifierListener.java @@ -0,0 +1,111 @@ +package de.anura.core.events; + +import com.google.common.collect.Iterables; +import com.vexsoftware.votifier.model.Vote; +import com.vexsoftware.votifier.model.VotifierEvent; +import de.anura.core.API.Core; +import de.anura.core.API.Errors; +import de.anura.core.API.Money; +import de.anura.core.AnuraCore; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.IOException; +import java.sql.ResultSet; +import java.sql.SQLException; +import org.bukkit.Bukkit; +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.plugin.messaging.PluginMessageListener; + +public class VotifierListener implements Listener, PluginMessageListener { + + private int timestampInt; + private int votesInRow; + private int playerId; + + @EventHandler + public void onVotifierEvent(VotifierEvent event) { + Vote vote = event.getVote(); + String timestamp = vote.getTimeStamp(); + + try { + timestampInt = Integer.parseInt(timestamp); + } catch (NumberFormatException ex) { + timestampInt = (int) (System.currentTimeMillis() / 1000); + } + addPlayerMoney(25, vote); + try { + ResultSet rs1 = Core.getMySql().querySelect("SELECT * FROM `players` WHERE name = '" + vote.getUsername() + "'"); + if (!rs1.next()) { + return; + } + playerId = rs1.getInt("id"); + ResultSet rs2 = Core.getMySql().querySelect("SELECT * FROM `voteUsers` WHERE playerId = '" + playerId + "'"); + if (rs2.first()) { + int lastVoteDate = rs2.getInt("dateOfLastVote"); + votesInRow = rs2.getInt("votesInARow"); + if (lastVoteDate > (System.currentTimeMillis() - 86400)) { + votesInRow++; + if (votesInRow == 10) { + addPlayerMoney(50, vote); + votesInRow = 0; + } + } else { + votesInRow = 1; + } + Core.getMySql().queryUpdate("UPDATE `voteUsers` SET `dateOfLastVote`=" + timestampInt + ",`votesInARow`=" + votesInRow + " WHERE playerId=" + playerId); + } else { + Core.getMySql().queryUpdate("INSERT INTO `voteUsers`(`playerId`, `dateOfLastVote`, `votesInARow`) VALUES (" + playerId + "," + timestampInt + ",1)"); + } + } catch (SQLException ex) { + Errors.reportException(ex); + } + } + + public void addPlayerMoney(int money, Vote vote) { + /*if (Bukkit.getPlayer(vote.getUsername()) != null) { + Money.payMoney(Bukkit.getPlayer(vote.getUsername()), money); + } else {*/ + try { + ByteArrayOutputStream stream = new ByteArrayOutputStream(); + DataOutputStream out = new DataOutputStream(stream); + out.writeUTF("money"); + out.writeUTF(vote.getUsername()); + out.writeInt(money); + + Iterables.get(AnuraCore.getInstance().getServer().getOnlinePlayers(), 0).sendPluginMessage(AnuraCore.getInstance(), "BungeeCord", stream.toByteArray()); + } catch (Exception ex) { + Errors.reportException(ex); + //} + } + } + + @Override + public void onPluginMessageReceived(String channel, Player player, byte[] bytes) { + if (!channel.equals("BungeeCord")) { + return; + } + try { + ByteArrayInputStream stream = new ByteArrayInputStream(bytes); + DataInputStream in = new DataInputStream(stream); + + if (in.readUTF().equalsIgnoreCase("money")) { + String playername = in.readUTF(); + Player p = Bukkit.getPlayer(playername); + int money = Integer.parseInt(in.readUTF()); + Money.payMoney(p, money); + if (money == 25) { + Core.getl("Vote_Normal", p); + } else if (money == 50) { + Core.getl("Vote_10_Times", p); + } + } + } catch (IOException ex) { + Errors.reportException(ex); + } + } + +} \ No newline at end of file diff --git a/src/plugin.yml b/src/plugin.yml index 03fa44b..ce39a94 100644 --- a/src/plugin.yml +++ b/src/plugin.yml @@ -2,7 +2,7 @@ name: Core main: de.anura.core.AnuraCore version: 0.9 author: kaenganxt -softdepend: [Multiverse-Core] +softdepend: [Multiverse-Core, Votifier] commands: setjumper: description: Admin Command