|
@@ -0,0 +1,365 @@
|
|
|
+package org.noip.olyol95.yarp;
|
|
|
+
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.Hashtable;
|
|
|
+import java.util.UUID;
|
|
|
+import java.util.logging.Level;
|
|
|
+
|
|
|
+import com.evilmidget38.UUIDFetcher;
|
|
|
+import com.vexsoftware.votifier.model.Vote;
|
|
|
+import com.vexsoftware.votifier.model.VotifierEvent;
|
|
|
+import org.bukkit.GameMode;
|
|
|
+import org.bukkit.Material;
|
|
|
+import org.bukkit.command.Command;
|
|
|
+import org.bukkit.command.CommandSender;
|
|
|
+import org.bukkit.entity.Player;
|
|
|
+import org.bukkit.inventory.ItemStack;
|
|
|
+import org.bukkit.inventory.PlayerInventory;
|
|
|
+import org.bukkit.plugin.java.JavaPlugin;
|
|
|
+import org.noip.olyol95.yarp.entities.Milestone;
|
|
|
+import org.noip.olyol95.yarp.entities.Reward;
|
|
|
+import org.noip.olyol95.yarp.entities.VoteRecord;
|
|
|
+import org.noip.olyol95.yarp.listeners.VotingListener;
|
|
|
+import org.noip.olyol95.yarp.util.ChatUtils;
|
|
|
+import org.noip.olyol95.yarp.util.ConfigurationParser;
|
|
|
+
|
|
|
+public class Yarp extends JavaPlugin {
|
|
|
+
|
|
|
+ private static Yarp instance;
|
|
|
+
|
|
|
+ public static Boolean BROADCAST_VOTES, BROADCAST_REWARDS;
|
|
|
+
|
|
|
+ private Hashtable<UUID,VoteRecord> userVotes;
|
|
|
+ private ArrayList<Milestone> milestones;
|
|
|
+ private ArrayList<Reward> rewards;
|
|
|
+ private ArrayList<String> links;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onEnable() {
|
|
|
+
|
|
|
+ instance = this;
|
|
|
+ milestones = new ArrayList<>();
|
|
|
+ rewards = new ArrayList<>();
|
|
|
+ links = new ArrayList<>();
|
|
|
+ userVotes = new Hashtable<>();
|
|
|
+
|
|
|
+ saveDefaultConfig();
|
|
|
+
|
|
|
+ if (!ConfigurationParser.parse()) {
|
|
|
+
|
|
|
+ getLogger().log(Level.INFO,"Disabling Yarp.");
|
|
|
+ setEnabled(false);
|
|
|
+ return;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ getServer().getPluginManager().registerEvents(new VotingListener(), this);
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onDisable() {
|
|
|
+
|
|
|
+ milestones = null;
|
|
|
+ rewards = null;
|
|
|
+ links = null;
|
|
|
+ userVotes = null;
|
|
|
+ instance = null;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ public Hashtable<UUID,VoteRecord> getUserVotes() {
|
|
|
+
|
|
|
+ return userVotes;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ public void addVoteRecord(VoteRecord record) {
|
|
|
+
|
|
|
+ userVotes.put(record.getPlayerID(),record);
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ public VoteRecord getVoteRecord(UUID playerID) {
|
|
|
+
|
|
|
+ return userVotes.get(playerID);
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ public VoteRecord getVoteRecord(String username) throws Exception {
|
|
|
+
|
|
|
+ if (isOnline(username)) {
|
|
|
+
|
|
|
+ return userVotes.get(getServer().getPlayer(username).getUniqueId());
|
|
|
+
|
|
|
+ } else {
|
|
|
+
|
|
|
+ return userVotes.get(getServer().getOfflinePlayer(UUIDFetcher.getUUIDOf(username)).getUniqueId());
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ public int getTotalVotes() {
|
|
|
+
|
|
|
+ int totalVotes = 0;
|
|
|
+
|
|
|
+ for (VoteRecord voteRecord: userVotes.values()) {
|
|
|
+
|
|
|
+ totalVotes += voteRecord.getTotalVotes();
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ return totalVotes;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ public boolean isOnline(String username) {
|
|
|
+
|
|
|
+ boolean isOnline = false;
|
|
|
+
|
|
|
+ for (Player p: getServer().getOnlinePlayers()) {
|
|
|
+
|
|
|
+ if (p.getName().equals(username)) {
|
|
|
+
|
|
|
+ isOnline = true;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ return isOnline;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ public void broadcastVoteMessage(String username, String address) {
|
|
|
+
|
|
|
+ if (BROADCAST_VOTES) {
|
|
|
+
|
|
|
+ getServer().broadcastMessage(ChatUtils.formatMessage(username + " has voted for the server at: " + address + "!"));
|
|
|
+
|
|
|
+ } else {
|
|
|
+
|
|
|
+ if (isOnline(username)) {
|
|
|
+
|
|
|
+ getServer().getPlayer(username).sendMessage(ChatUtils.formatMessage("You have voted for the server at: " + address + "!"));
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ public void broadcastRewardMessage(String username, ArrayList<String> rewards) {
|
|
|
+
|
|
|
+ String rewardText = "";
|
|
|
+
|
|
|
+ for (String reward: rewards) {
|
|
|
+
|
|
|
+ rewardText += "\n\t"+reward;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ if (BROADCAST_REWARDS) {
|
|
|
+
|
|
|
+ getServer().broadcastMessage(ChatUtils.formatMessage(username + " has earned themselves the following by voting:" + rewardText));
|
|
|
+
|
|
|
+ } else {
|
|
|
+
|
|
|
+ if (isOnline(username)) {
|
|
|
+
|
|
|
+ getServer().getPlayer(username).sendMessage(ChatUtils.formatMessage("You have earned the following rewards:"+rewardText));
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ public static Yarp getInstance() {
|
|
|
+
|
|
|
+ return instance;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ public void checkForRewards(VoteRecord voteRecord) {
|
|
|
+
|
|
|
+ for (Reward reward: getRewards()) {
|
|
|
+
|
|
|
+ if (reward.isEarned(voteRecord)) {
|
|
|
+
|
|
|
+ Player player = getServer().getPlayer(voteRecord.getPlayerID());
|
|
|
+
|
|
|
+ player.sendMessage(ChatUtils.formatMessage("You have a reward waiting for you, use /vote reward to claim it!"));
|
|
|
+ break;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ public ArrayList<Milestone> getMilestones() {
|
|
|
+
|
|
|
+ return milestones;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ public void addMilestone(Milestone milestone) {
|
|
|
+
|
|
|
+ if (!milestones.contains(milestone)) {
|
|
|
+
|
|
|
+ milestones.add(milestone);
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ public ArrayList<Reward> getRewards() {
|
|
|
+
|
|
|
+ return rewards;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ public void addReward(Reward reward) {
|
|
|
+
|
|
|
+ if (!rewards.contains(reward)) {
|
|
|
+
|
|
|
+ rewards.add(reward);
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ public ArrayList<String> getLinks() {
|
|
|
+
|
|
|
+ return links;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ public void addLink(String link) {
|
|
|
+
|
|
|
+ if (!links.contains(link)) {
|
|
|
+
|
|
|
+ links.add(link);
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
|
|
|
+
|
|
|
+ if (command.getName().equalsIgnoreCase("vote")) {
|
|
|
+
|
|
|
+ if (args.length == 0) {
|
|
|
+
|
|
|
+ if (sender.hasPermission("yarp.user.vote")) {
|
|
|
+
|
|
|
+ sender.sendMessage(ChatUtils.formatMessage("Vote for us at the following links to earn rewards:"));
|
|
|
+
|
|
|
+ Vote vote = new Vote();
|
|
|
+ vote.setServiceName("Test.com");
|
|
|
+ vote.setUsername("Olyol95");
|
|
|
+
|
|
|
+ getServer().getPluginManager().callEvent(new VotifierEvent(vote));
|
|
|
+
|
|
|
+ } else {
|
|
|
+
|
|
|
+ sender.sendMessage(ChatUtils.formatMessage("You do not have permission to do that!"));
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ return true;
|
|
|
+
|
|
|
+ } else {
|
|
|
+
|
|
|
+ if (args[0].equalsIgnoreCase("reward")) {
|
|
|
+
|
|
|
+ if (sender.equals(getServer().getConsoleSender())) {
|
|
|
+
|
|
|
+ sender.sendMessage(ChatUtils.formatMessage("Only players can use this command!"));
|
|
|
+
|
|
|
+ } else if (sender.hasPermission("yarp.user.reward")) {
|
|
|
+
|
|
|
+ Player player = (Player) sender;
|
|
|
+
|
|
|
+ VoteRecord voteRecord = getVoteRecord(player.getUniqueId());
|
|
|
+
|
|
|
+ if (voteRecord == null) {
|
|
|
+
|
|
|
+ sender.sendMessage(ChatUtils.formatMessage("You have no rewards waiting for you at this time. Vote for the server to earn some!"));
|
|
|
+ return true;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ ArrayList<String> rewardMessages = new ArrayList<>();
|
|
|
+
|
|
|
+ PlayerInventory playerInventory = player.getInventory();
|
|
|
+
|
|
|
+ boolean isFull = true;
|
|
|
+
|
|
|
+ for (ItemStack itemStack : playerInventory.getContents()) {
|
|
|
+
|
|
|
+ if (itemStack == null || itemStack.getType() == Material.AIR) {
|
|
|
+
|
|
|
+ isFull = false;
|
|
|
+ break;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ for (Reward reward : getRewards()) {
|
|
|
+
|
|
|
+ if (reward.isEarned(voteRecord)) {
|
|
|
+
|
|
|
+ if (player.getGameMode() == GameMode.CREATIVE) {
|
|
|
+
|
|
|
+ player.sendMessage(ChatUtils.formatMessage("You have a reward waiting for you, however you are in creative mode!\nPlease change your game mode and try again."));
|
|
|
+ return true;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ if (isFull) {
|
|
|
+
|
|
|
+ player.sendMessage(ChatUtils.formatMessage("You have a reward waiting for you, however your inventory is full!\nPlease free up a slot and try again."));
|
|
|
+ return true;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ rewardMessages.add(reward.execute(voteRecord));
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ if (rewardMessages.isEmpty()) {
|
|
|
+
|
|
|
+ sender.sendMessage(ChatUtils.formatMessage("You have no rewards waiting for you at this time. Vote for the server to earn some!"));
|
|
|
+
|
|
|
+ } else {
|
|
|
+
|
|
|
+ broadcastRewardMessage(getServer().getPlayer(voteRecord.getPlayerID()).getName(), rewardMessages);
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ } else {
|
|
|
+
|
|
|
+ sender.sendMessage(ChatUtils.formatMessage("You do not have permission to do that!"));
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ return true;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ return false;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+}
|