mirror of
https://github.com/darksaid98/EntityBingo.git
synced 2026-12-01 15:58:46 +00:00
scoreboard saving
This commit is contained in:
1 parent
c1997e7cc3
commit
30d7cdb28b
4 files changed
+136
-60
No files matched your search
@@ -2,6 +2,7 @@ package org.aubrithehuman.entitybingo;
|
||||
|
||||
import org.aubrithehuman.entitybingo.command.EntityBingoCommand;
|
||||
import org.aubrithehuman.entitybingo.listeners.ChatListener;
|
||||
import org.aubrithehuman.entitybingo.util.Helper;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
|
||||
@@ -32,7 +33,7 @@ public final class EntityBingo extends JavaPlugin {
|
||||
@Override
|
||||
public void onDisable() {
|
||||
if(currrentEvent != null) {
|
||||
Bukkit.broadcastMessage(ChatListener.chatLabel() + "Server Shutting down, clearing Bingo Event without results!");
|
||||
Bukkit.broadcastMessage(Helper.chatLabel() + "Server Shutting down, clearing Bingo Event without results!");
|
||||
currrentEvent.setDone();
|
||||
currrentEvent = null;
|
||||
}
|
||||
|
||||
@@ -2,6 +2,8 @@ package org.aubrithehuman.entitybingo.command;
|
||||
|
||||
import org.aubrithehuman.entitybingo.EntityBingo;
|
||||
import org.aubrithehuman.entitybingo.listeners.ChatListener;
|
||||
import org.aubrithehuman.entitybingo.util.Helper;
|
||||
import org.aubrithehuman.entitybingo.util.Pair;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandExecutor;
|
||||
@@ -9,7 +11,7 @@ import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.command.TabCompleter;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.*;
|
||||
|
||||
public class EntityBingoCommand implements CommandExecutor, TabCompleter {
|
||||
|
||||
@@ -25,6 +27,7 @@ public class EntityBingoCommand implements CommandExecutor, TabCompleter {
|
||||
"guesses"
|
||||
});
|
||||
|
||||
|
||||
public EntityBingoCommand(EntityBingo entityBingo) {
|
||||
entityBingo.getCommand("eb").setExecutor((CommandExecutor) this);
|
||||
entityBingo.getCommand("entb").setExecutor((CommandExecutor) this);
|
||||
@@ -36,46 +39,86 @@ public class EntityBingoCommand implements CommandExecutor, TabCompleter {
|
||||
final Player p = (Player) sender;
|
||||
if(args.length >= 1) {
|
||||
if(args[0].equalsIgnoreCase("scoreboard")) {
|
||||
//check if we can do anything
|
||||
if(ChatListener.scoreboard == null) {
|
||||
p.sendMessage(Helper.chatLabel() + Helper.color("&cError loading scoreboard."));
|
||||
return true;
|
||||
}
|
||||
if(ChatListener.scoreboard.isEmpty()) {
|
||||
p.sendMessage(Helper.chatLabel() + Helper.color("&cError loading scoreboard."));
|
||||
return true;
|
||||
}
|
||||
|
||||
int max = ChatListener.scoreboard.size();
|
||||
double pages = Math.ceil((double) max / 8.0D);
|
||||
|
||||
//parse a page number
|
||||
int page = 1;
|
||||
try {
|
||||
page = args.length >= 2 ? Integer.parseInt(args[1]) : 1;
|
||||
} catch (NumberFormatException ex) {
|
||||
p.sendMessage(Helper.chatLabel() + Helper.color("&cPage number not a value."));
|
||||
}
|
||||
|
||||
int startIndex = (page - 1) * 10;
|
||||
|
||||
//print
|
||||
p.sendMessage(Helper.chatLabel() + Helper.color("Entity Bingo Scoreboard:"));
|
||||
p.sendMessage(Helper.chatLabel() + Helper.color("-------------[" + page + "/" + (int) pages + "]-------------"));
|
||||
|
||||
int i = 0;
|
||||
for (String s : ChatListener.scoreboard.keySet()) {
|
||||
if (i < startIndex) continue;
|
||||
String name = Bukkit.getOfflinePlayer(UUID.fromString(s)).getName();
|
||||
p.sendMessage(Helper.chatLabel() + Helper.color("&7 " + (i + 1) + ". " + name + ", " + ChatListener.scoreboard.get(s)));
|
||||
i++;
|
||||
//page limit
|
||||
if(i > 10 + startIndex) break;
|
||||
}
|
||||
|
||||
|
||||
return true;
|
||||
} else if(args[0].equalsIgnoreCase("guesses")) {
|
||||
//print off all guesses
|
||||
p.sendMessage(ChatListener.chatLabel() + ChatListener.color("List of all guesses in the " + (EntityBingo.getCurrrentEvent().isDone() ? "previous" : "current") + " event:"));
|
||||
p.sendMessage(ChatListener.chatLabel() + ChatListener.color("----------------------------------"));
|
||||
p.sendMessage(Helper.chatLabel() + Helper.color("List of all guesses in the " + (EntityBingo.getCurrrentEvent().isDone() ? "previous" : "current") + " event:"));
|
||||
p.sendMessage(Helper.chatLabel() + Helper.color("----------------------------------"));
|
||||
|
||||
for (String s : EntityBingo.getCurrrentEvent().getEntries().keySet()) {
|
||||
p.sendMessage(ChatListener.chatLabel() + ChatListener.color("&7 - " + s + ": " + EntityBingo.getCurrrentEvent().getEntries().get(s)));
|
||||
String name = Bukkit.getOfflinePlayer(UUID.fromString(s)).getName();
|
||||
p.sendMessage(Helper.chatLabel() + Helper.color("&7 - " + name + ": " + EntityBingo.getCurrrentEvent().getEntries().get(s)));
|
||||
}
|
||||
return true;
|
||||
} else if(args[0].equalsIgnoreCase("purge")) {
|
||||
if (!p.hasPermission("EntityBingo.admin")) {
|
||||
p.sendMessage(ChatListener.chatLabel() + ChatListener.color("&cYou do not have permissions to run this command."));
|
||||
p.sendMessage(Helper.chatLabel() + Helper.color("&cYou do not have permissions to run this command."));
|
||||
return true;
|
||||
}
|
||||
|
||||
//clear entry pool
|
||||
EntityBingo.getCurrrentEvent().getEntries().clear();
|
||||
p.sendMessage(ChatListener.chatLabel() + ChatListener.color("&cCleared all entries in the current event."));
|
||||
Bukkit.broadcastMessage(ChatListener.chatLabel() + ChatListener.color("&cEntries cleared by an admin!"));
|
||||
EntityBingo.getInstance().getLogger().info(ChatListener.color("&cEntries cleared by an admin!"));
|
||||
p.sendMessage(Helper.chatLabel() + Helper.color("&cCleared all entries in the current event."));
|
||||
Bukkit.broadcastMessage(Helper.chatLabel() + Helper.color("&cEntries cleared by an admin!"));
|
||||
EntityBingo.getInstance().getLogger().info(Helper.color("&cEntries cleared by an admin!"));
|
||||
return true;
|
||||
} else if(args[0].equalsIgnoreCase("reset")) {
|
||||
if (!p.hasPermission("EntityBingo.admin")) {
|
||||
p.sendMessage(ChatListener.chatLabel() + ChatListener.color("&cYou do not have permissions to run this command."));
|
||||
p.sendMessage(Helper.chatLabel() + Helper.color("&cYou do not have permissions to run this command."));
|
||||
return true;
|
||||
}
|
||||
|
||||
//clear event
|
||||
EntityBingo.setCurrrentEvent(null);
|
||||
p.sendMessage(ChatListener.chatLabel() + ChatListener.color("&cEnded current bingo event."));
|
||||
Bukkit.broadcastMessage(ChatListener.chatLabel() + ChatListener.color("&cEvent forcefully ended by and admin!"));
|
||||
EntityBingo.getInstance().getLogger().info(ChatListener.color("&cEvent forcefully ended by and admin!"));
|
||||
p.sendMessage(Helper.chatLabel() + Helper.color("&cEnded current bingo event."));
|
||||
Bukkit.broadcastMessage(Helper.chatLabel() + Helper.color("&cEvent forcefully ended by and admin!"));
|
||||
EntityBingo.getInstance().getLogger().info(Helper.color("&cEvent forcefully ended by and admin!"));
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public List<String> onTabComplete(CommandSender sender, Command command, String label, String[] args) {
|
||||
final Player p = (Player) sender;
|
||||
|
||||
@@ -3,6 +3,8 @@ package org.aubrithehuman.entitybingo.listeners;
|
||||
import org.aubrithehuman.entitybingo.BingoEvent;
|
||||
import org.aubrithehuman.entitybingo.DataManager;
|
||||
import org.aubrithehuman.entitybingo.EntityBingo;
|
||||
import org.aubrithehuman.entitybingo.command.EntityBingoCommand;
|
||||
import org.aubrithehuman.entitybingo.util.Helper;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.event.EventHandler;
|
||||
@@ -11,26 +13,29 @@ import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.player.AsyncPlayerChatEvent;
|
||||
import org.bukkit.event.server.BroadcastMessageEvent;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.*;
|
||||
import java.util.logging.Level;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class ChatListener implements Listener {
|
||||
|
||||
|
||||
public static HashMap<String, Integer> scoreboard;
|
||||
|
||||
@EventHandler(priority = EventPriority.MONITOR)
|
||||
public void onBroadcast(BroadcastMessageEvent e) {
|
||||
Bukkit.getLogger().info(chatLabel() + "comparing: " + e.getMessage());
|
||||
Bukkit.getLogger().info(Helper.chatLabel() + "comparing: " + e.getMessage());
|
||||
boolean matchFound = e.getMessage().equals("§4[ClearLag] §cWarning Ground items will be removed in §760 §cseconds!");
|
||||
boolean matchFound2 = false;
|
||||
if(matchFound) {
|
||||
//start the event, only if previous one is marked as done
|
||||
if(EntityBingo.getCurrrentEvent().isDone()) {
|
||||
EntityBingo.setCurrrentEvent(new BingoEvent());
|
||||
Bukkit.broadcastMessage(chatLabel() + "Entity Bingo has begun! Type a number to enter!");
|
||||
EntityBingo.getInstance().getLogger().info(chatLabel() + "Entity Bingo has begun! Type a number to enter!");
|
||||
Bukkit.broadcastMessage(Helper.chatLabel() + "Entity Bingo has begun! Type a number to enter!");
|
||||
EntityBingo.getInstance().getLogger().info(Helper.chatLabel() + "Entity Bingo has begun! Type a number to enter!");
|
||||
} else {
|
||||
Bukkit.getLogger().info(chatLabel() + "Event Started too recently, ignoring Bingo attempt!");
|
||||
EntityBingo.getInstance().getLogger().info(chatLabel() + "Event Started too recently, ignoring Bingo attempt!");
|
||||
Bukkit.getLogger().info(Helper.chatLabel() + "Event Started too recently, ignoring Bingo attempt!");
|
||||
EntityBingo.getInstance().getLogger().info(Helper.chatLabel() + "Event Started too recently, ignoring Bingo attempt!");
|
||||
}
|
||||
return;
|
||||
} else {
|
||||
@@ -45,7 +50,7 @@ public class ChatListener implements Listener {
|
||||
Bukkit.getLogger().info("Match found for endpoint");
|
||||
//check if we have an bingo event happening
|
||||
if(EntityBingo.getCurrrentEvent() != null) {
|
||||
Bukkit.broadcastMessage(chatLabel() + "Entity Bingo has ended!");
|
||||
Bukkit.broadcastMessage(Helper.chatLabel() + "Entity Bingo has ended!");
|
||||
|
||||
//extract the result number
|
||||
String str = e.getMessage();
|
||||
@@ -57,7 +62,7 @@ public class ChatListener implements Listener {
|
||||
try {
|
||||
i = Integer.parseInt(numberOnly);
|
||||
} catch (NumberFormatException exception) {
|
||||
EntityBingo.getInstance().getLogger().info(chatLabel() + color("&cFailed to find the result number, cancelling bingo."));
|
||||
EntityBingo.getInstance().getLogger().info(Helper.chatLabel() + Helper.color("&cFailed to find the result number, cancelling bingo."));
|
||||
EntityBingo.getCurrrentEvent().setDone();
|
||||
return;
|
||||
}
|
||||
@@ -75,23 +80,44 @@ public class ChatListener implements Listener {
|
||||
|
||||
//broadcast winners
|
||||
if(winners.isEmpty()) {
|
||||
Bukkit.broadcastMessage(chatLabel() + "No one guessed correctly.");
|
||||
EntityBingo.getInstance().getLogger().info(chatLabel() + "No one guessed correctly.");
|
||||
//no winners??????
|
||||
Bukkit.broadcastMessage(Helper.chatLabel() + "No one guessed correctly.");
|
||||
EntityBingo.getInstance().getLogger().info(Helper.chatLabel() + "No one guessed correctly.");
|
||||
} else {
|
||||
HashMap<String, Object> data = DataManager.getData("scoreboard.yml");
|
||||
for (String s : winners) {
|
||||
Bukkit.broadcastMessage(chatLabel() + s + " guessed correctly!");
|
||||
EntityBingo.getInstance().getLogger().info(chatLabel() + s + " guessed correctly!");
|
||||
//broadcast winner
|
||||
Bukkit.broadcastMessage(Helper.chatLabel() + s + " guessed correctly!");
|
||||
EntityBingo.getInstance().getLogger().info(Helper.chatLabel() + s + " guessed correctly!");
|
||||
|
||||
//get player uuid and save their wins
|
||||
UUID uuid = UUID.fromString(s);
|
||||
if(data != null) {
|
||||
if(data.containsKey(s)) {
|
||||
data.put(s, (int) data.get(s) + 1);
|
||||
if(data.containsKey(uuid.toString())) {
|
||||
data.put(uuid.toString(), (int) data.get(uuid.toString()) + 1);
|
||||
} else {
|
||||
data.put(s, 1);
|
||||
data.put(uuid.toString(), 1);
|
||||
}
|
||||
} else {
|
||||
EntityBingo.getInstance().getLogger().log(Level.WARNING, "Data failed to load, plugins/EntityBingo/data/scoreboard.yml may be broken.");
|
||||
}
|
||||
}
|
||||
DataManager.saveData("scoreboard.yml", data);
|
||||
|
||||
if(data != null) {
|
||||
//save scoreboard
|
||||
DataManager.saveData("scoreboard.yml", data);
|
||||
|
||||
//grab only entries with integer values, should be all but we need to check anyway
|
||||
Map<String, Integer> filtered = data.entrySet()
|
||||
.stream()
|
||||
.filter(v -> v.getValue() instanceof Integer)
|
||||
.collect(Collectors.toMap(
|
||||
Map.Entry::getKey,
|
||||
v -> (int) v.getValue()));
|
||||
|
||||
//Save to static scoreboard reference
|
||||
scoreboard = Helper.sortData(filtered);
|
||||
}
|
||||
}
|
||||
|
||||
EntityBingo.getCurrrentEvent().setDone();
|
||||
@@ -113,38 +139,38 @@ public class ChatListener implements Listener {
|
||||
@EventHandler(priority = EventPriority.MONITOR)
|
||||
public void onChat(AsyncPlayerChatEvent e) {
|
||||
//Entry detection
|
||||
Bukkit.getLogger().info(chatLabel() + "comparing: " + "\"" + e.getMessage() + "\"");
|
||||
Bukkit.getLogger().info(Helper.chatLabel() + "comparing: " + "\"" + e.getMessage() + "\"");
|
||||
if (EntityBingo.getCurrrentEvent() != null) {
|
||||
String str = e.getMessage();
|
||||
//remove all color codes from this message
|
||||
str = ChatColor.stripColor(str);
|
||||
Bukkit.getLogger().info(chatLabel() + "purges codes for: " + "\"" + str + "\"");
|
||||
Bukkit.getLogger().info(Helper.chatLabel() + "purges codes for: " + "\"" + str + "\"");
|
||||
//remove all text other than numbers
|
||||
str = str.replaceAll("[^0-9.]", "");
|
||||
Bukkit.getLogger().info(chatLabel() + "numbers only: " + "\"" + str + "\"");
|
||||
Bukkit.getLogger().info(Helper.chatLabel() + "numbers only: " + "\"" + str + "\"");
|
||||
if(!str.isEmpty()) {
|
||||
int guess;
|
||||
//we need to check if the guess exists as an int
|
||||
try {
|
||||
guess = Integer.parseInt(str);
|
||||
} catch (NumberFormatException exception) {
|
||||
e.getPlayer().sendMessage(chatLabel() + color("&cDetected a number that could not be formatted, make sure not to use decimals."));
|
||||
e.getPlayer().sendMessage(Helper.chatLabel() + Helper.color("&cDetected a number that could not be formatted, make sure not to use decimals."));
|
||||
return;
|
||||
}
|
||||
|
||||
if (guess > 0) {
|
||||
//do the entry
|
||||
int result = EntityBingo.getCurrrentEvent().addEntry(e.getPlayer().getName(), guess);
|
||||
int result = EntityBingo.getCurrrentEvent().addEntry(e.getPlayer().getUniqueId().toString(), guess);
|
||||
//feedback to entrant
|
||||
switch (result) {
|
||||
case 0 -> e.getPlayer().sendMessage(chatLabel() + color("&aYou have entered with the guess " + guess + "."));
|
||||
case 0 -> e.getPlayer().sendMessage(Helper.chatLabel() + Helper.color("&aYou have entered with the guess " + guess + "."));
|
||||
case 1 -> {
|
||||
e.getPlayer().sendMessage(chatLabel() + color("&eYou have previously entered entity bingo, your old guess has been overridden."));
|
||||
e.getPlayer().sendMessage(chatLabel() + color("&aYou have entered with the guess " + guess + "."));
|
||||
e.getPlayer().sendMessage(Helper.chatLabel() + Helper.color("&eYou have previously entered entity bingo, your old guess has been overridden."));
|
||||
e.getPlayer().sendMessage(Helper.chatLabel() + Helper.color("&aYou have entered with the guess " + guess + "."));
|
||||
}
|
||||
case 2 -> e.getPlayer().sendMessage(chatLabel() + color("&eSomeone has already made the guess " + guess + ". Enter a different number!"));
|
||||
case 2 -> e.getPlayer().sendMessage(Helper.chatLabel() + Helper.color("&eSomeone has already made the guess " + guess + ". Enter a different number!"));
|
||||
default -> {
|
||||
e.getPlayer().sendMessage(chatLabel() + color("&cSomething went wrong entering your guess. Try again!"));
|
||||
e.getPlayer().sendMessage(Helper.chatLabel() + Helper.color("&cSomething went wrong entering your guess. Try again!"));
|
||||
EntityBingo.getInstance().getLogger().info("Failed to enter guess \"" + guess + "\" by player " + e.getPlayer() + " from message \"" + e.getMessage() + "\"");
|
||||
}
|
||||
}
|
||||
@@ -153,11 +179,4 @@ public class ChatListener implements Listener {
|
||||
}
|
||||
}
|
||||
|
||||
public static String color(final String input) {
|
||||
return ChatColor.translateAlternateColorCodes('&', input);
|
||||
}
|
||||
|
||||
public static String chatLabel() {
|
||||
return color("&2[&aEntityBingo&2]&r ");
|
||||
}
|
||||
}
|
||||
@@ -2,23 +2,36 @@ package org.aubrithehuman.entitybingo.util;
|
||||
|
||||
import org.bukkit.ChatColor;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class Helper {
|
||||
|
||||
public static HashMap<String, Integer> sortData(HashMap<String, Integer> data) {
|
||||
/**
|
||||
* sorts a map from the highest value to the lowest value
|
||||
* @param data
|
||||
* @return
|
||||
*/
|
||||
public static HashMap<String, Integer> sortData(Map<String, Integer> data) {
|
||||
|
||||
return data.entrySet()
|
||||
.stream()
|
||||
.sorted((i1, i2)
|
||||
-> (- i1.getValue().compareTo(i2.getValue())))
|
||||
.collect(Collectors.toMap(
|
||||
Map.Entry::getKey,
|
||||
Map.Entry::getValue,
|
||||
(e1, e2) -> e1, LinkedHashMap::new));
|
||||
// Create a list from elements of HashMap
|
||||
List<Map.Entry<String, Integer> > list
|
||||
= new LinkedList<Map.Entry<String, Integer> >(
|
||||
data.entrySet());
|
||||
|
||||
// Sort the list using lambda expression
|
||||
Collections.sort(
|
||||
list,
|
||||
(i1,
|
||||
i2) -> (- i1.getValue().compareTo(i2.getValue())));
|
||||
|
||||
// put data from sorted list to hashmap
|
||||
HashMap<String, Integer> temp
|
||||
= new LinkedHashMap<String, Integer>();
|
||||
for (Map.Entry<String, Integer> aa : list) {
|
||||
temp.put(aa.getKey(), aa.getValue());
|
||||
}
|
||||
return temp;
|
||||
}
|
||||
|
||||
public static String color(final String input) {
|
||||
|
||||
Reference in new issue
Block a user