mirror of
https://github.com/darksaid98/EntityBingo.git
synced 2026-12-01 15:58:46 +00:00
warnings
This commit is contained in:
1 parent
837b842c0e
commit
6176a61e05
6 files changed
+149
-163
No files matched your search
@@ -1,8 +1,6 @@
|
||||
package org.aubrithehuman.entitybingo;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
public class BingoEvent {
|
||||
|
||||
@@ -10,11 +8,8 @@ public class BingoEvent {
|
||||
|
||||
private boolean done = false;
|
||||
|
||||
private final long age;
|
||||
|
||||
public BingoEvent() {
|
||||
this.entries = new HashMap<>();
|
||||
age = System.currentTimeMillis();
|
||||
}
|
||||
|
||||
public HashMap<String, Integer> getEntries() {
|
||||
@@ -25,12 +20,12 @@ public class BingoEvent {
|
||||
* return 0 if this is first guess, 1 if previous guess, 2 if guess already exists.
|
||||
*
|
||||
* @param player player
|
||||
* @param value value
|
||||
* @param value value
|
||||
* @return previous guess
|
||||
*/
|
||||
public int addEntry(String player, int value) {
|
||||
if(!entries.containsValue(value)) {
|
||||
if(!entries.containsKey(player)) {
|
||||
if (!entries.containsValue(value)) {
|
||||
if (!entries.containsKey(player)) {
|
||||
entries.put(player, value);
|
||||
return 0;
|
||||
}
|
||||
@@ -41,29 +36,6 @@ public class BingoEvent {
|
||||
|
||||
}
|
||||
|
||||
public int totalGuesses() {
|
||||
return this.entries.size();
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a player based on the entry, if none match then return null;
|
||||
*
|
||||
* @param i value
|
||||
* @return player
|
||||
*/
|
||||
public String getByEntry(int i) {
|
||||
for(String s : entries.keySet()) {
|
||||
if (entries.get(s) == i) {
|
||||
return s;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public long getAge() {
|
||||
return age;
|
||||
}
|
||||
|
||||
public boolean isDone() {
|
||||
return done;
|
||||
}
|
||||
|
||||
@@ -6,7 +6,6 @@ import org.yaml.snakeyaml.Yaml;
|
||||
|
||||
import java.io.*;
|
||||
import java.nio.file.Files;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
@@ -26,14 +25,15 @@ public class DataManager {
|
||||
genDefaultFiles();
|
||||
}
|
||||
|
||||
private static String dataFolder = "plugins" + File.separator + "EntityBingo" + File.separator + "data";
|
||||
private final static String dataFolder = "plugins" + File.separator + "EntityBingo" + File.separator + "data";
|
||||
|
||||
/**
|
||||
* Saves data into a .yml format
|
||||
*
|
||||
* @param filePath - File's path after <PLUGIN>/Data/
|
||||
* @param map - Map to be saved
|
||||
* @param map - Map to be saved
|
||||
*/
|
||||
public static void saveData(String filePath, HashMap<String,Object> map) {
|
||||
public static void saveData(String filePath, HashMap<String, Object> map) {
|
||||
File file = getFile(filePath);
|
||||
PrintWriter writer = null;
|
||||
try {
|
||||
@@ -42,14 +42,14 @@ public class DataManager {
|
||||
Bukkit.getLogger().info("Encountered error when creating PrintWriter for " + filePath);
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
if (options == null) {
|
||||
options = new DumperOptions();
|
||||
options.setIndent(2);
|
||||
options.setPrettyFlow(true);
|
||||
options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
|
||||
}
|
||||
|
||||
|
||||
Yaml yaml = new Yaml(options);
|
||||
yaml.dump(map, writer);
|
||||
writer.close();
|
||||
@@ -57,25 +57,27 @@ public class DataManager {
|
||||
|
||||
/**
|
||||
* Gets the data back as a HashMap
|
||||
*
|
||||
* @param filePath - FilePath of data
|
||||
* @return HashMap of data or null
|
||||
*/
|
||||
public static HashMap<String,Object> getData(String filePath) {
|
||||
public static HashMap<String, Object> getData(String filePath) {
|
||||
File file = getFile(filePath);
|
||||
return getData(file);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the data back as a HashMap
|
||||
*
|
||||
* @param file - Data file
|
||||
* @return HashMap of data or null
|
||||
*/
|
||||
public static HashMap<String,Object> getData(File file) {
|
||||
public static HashMap<String, Object> getData(File file) {
|
||||
if (!file.exists()) {
|
||||
Bukkit.getLogger().info("Attempted to retrieve non-existant file: " + file.getPath());
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
InputStream inputStream = null;
|
||||
try {
|
||||
inputStream = new FileInputStream(file);
|
||||
@@ -90,12 +92,13 @@ public class DataManager {
|
||||
} catch (IOException e) {
|
||||
Bukkit.getLogger().info(e.getMessage());
|
||||
}
|
||||
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes the specified file
|
||||
*
|
||||
* @param filePath - FilePath of deletion
|
||||
* @return Boolean of success
|
||||
*/
|
||||
@@ -106,7 +109,7 @@ public class DataManager {
|
||||
Bukkit.getLogger().info("Attempted to retrieve non-existant file: " + filePath + ".yml");
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
try {
|
||||
return Files.deleteIfExists(file.toPath());
|
||||
} catch (IOException e) {
|
||||
@@ -117,12 +120,13 @@ public class DataManager {
|
||||
|
||||
/**
|
||||
* Converts filePath into File
|
||||
*
|
||||
* @param path - File's path within data folder
|
||||
* @return File or null
|
||||
*/
|
||||
private static File getFile(String path) {
|
||||
path = dataFolder + File.separator + path;
|
||||
|
||||
|
||||
File file = new File(path);
|
||||
if (!file.exists()) {
|
||||
try {
|
||||
|
||||
@@ -8,13 +8,14 @@ import org.bukkit.plugin.java.JavaPlugin;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.logging.Level;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public final class EntityBingo extends JavaPlugin {
|
||||
|
||||
static EntityBingo instance;
|
||||
|
||||
static BingoEvent currrentEvent;
|
||||
static BingoEvent currentEvent;
|
||||
|
||||
@Override
|
||||
public void onEnable() {
|
||||
@@ -33,28 +34,32 @@ public final class EntityBingo extends JavaPlugin {
|
||||
|
||||
//Reload scoreboard
|
||||
HashMap<String, Object> raw = DataManager.getData("scoreboard.yml");
|
||||
HashMap<String, Object> data = (HashMap<String, Object>) raw.get("scores");
|
||||
//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()));
|
||||
try {
|
||||
HashMap<String, Object> data = (HashMap<String, Object>) raw.get("scores");
|
||||
//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
|
||||
ChatListener.scoreboard = Helper.sortData(filtered);
|
||||
//Save to static scoreboard reference
|
||||
ChatListener.scoreboard = Helper.sortData(filtered);
|
||||
} catch (ClassCastException ex) {
|
||||
this.getLogger().log(Level.WARNING, "Failed to load scoreboard! Is scoreboard.yml broken?");
|
||||
}
|
||||
|
||||
this.getLogger().info("EntityBingo loaded.");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDisable() {
|
||||
if(currrentEvent != null) {
|
||||
if(!currrentEvent.isDone()) {
|
||||
if(currentEvent != null) {
|
||||
if(!currentEvent.isDone()) {
|
||||
Bukkit.broadcastMessage(Helper.chatLabel() + "Server Shutting down, clearing Bingo Event without results!");
|
||||
currrentEvent.setDone();
|
||||
currrentEvent = null;
|
||||
currentEvent.setDone();
|
||||
currentEvent = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -63,11 +68,11 @@ public final class EntityBingo extends JavaPlugin {
|
||||
return instance;
|
||||
}
|
||||
|
||||
public static BingoEvent getCurrrentEvent() {
|
||||
return currrentEvent;
|
||||
public static BingoEvent getCurrentEvent() {
|
||||
return currentEvent;
|
||||
}
|
||||
|
||||
public static void setCurrrentEvent(BingoEvent currrentEvent) {
|
||||
EntityBingo.currrentEvent = currrentEvent;
|
||||
public static void setCurrentEvent(BingoEvent currrentEvent) {
|
||||
EntityBingo.currentEvent = currrentEvent;
|
||||
}
|
||||
}
|
||||
@@ -4,20 +4,24 @@ import org.aubrithehuman.entitybingo.DataManager;
|
||||
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;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.command.TabCompleter;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
import java.util.logging.Level;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class EntityBingoCommand implements CommandExecutor, TabCompleter {
|
||||
|
||||
private final List<String> adminOptions = List.of(new String[] {
|
||||
private final List<String> adminOptions = List.of(new String[]{
|
||||
"scoreboard",
|
||||
"guesses",
|
||||
"reloadscoreboard",
|
||||
@@ -25,29 +29,29 @@ public class EntityBingoCommand implements CommandExecutor, TabCompleter {
|
||||
"reset"
|
||||
});
|
||||
|
||||
private final List<String> options = List.of(new String[] {
|
||||
private final List<String> options = List.of(new String[]{
|
||||
"scoreboard",
|
||||
"guesses"
|
||||
});
|
||||
|
||||
|
||||
public EntityBingoCommand(EntityBingo entityBingo) {
|
||||
entityBingo.getCommand("eb").setExecutor((CommandExecutor) this);
|
||||
entityBingo.getCommand("entb").setExecutor((CommandExecutor) this);
|
||||
entityBingo.getCommand("entitybingo").setExecutor((CommandExecutor) this);
|
||||
entityBingo.getCommand("eb").setExecutor(this);
|
||||
entityBingo.getCommand("entb").setExecutor(this);
|
||||
entityBingo.getCommand("entitybingo").setExecutor(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
|
||||
public boolean onCommand(@NonNull CommandSender sender, @NonNull Command command, @NonNull String label, String[] args) {
|
||||
final Player p = (Player) sender;
|
||||
if(args.length >= 1) {
|
||||
if(args[0].equalsIgnoreCase("scoreboard")) {
|
||||
if (args.length >= 1) {
|
||||
if (args[0].equalsIgnoreCase("scoreboard")) {
|
||||
//check if we can do anything
|
||||
if(ChatListener.scoreboard == null) {
|
||||
if (ChatListener.scoreboard == null) {
|
||||
p.sendMessage(Helper.chatLabel() + Helper.color("&cError loading scoreboard."));
|
||||
return true;
|
||||
}
|
||||
if(ChatListener.scoreboard.isEmpty()) {
|
||||
if (ChatListener.scoreboard.isEmpty()) {
|
||||
p.sendMessage(Helper.chatLabel() + Helper.color("&cError loading scoreboard."));
|
||||
return true;
|
||||
}
|
||||
@@ -83,77 +87,81 @@ public class EntityBingoCommand implements CommandExecutor, TabCompleter {
|
||||
p.sendMessage(Helper.chatLabel() + Helper.color("&7" + (i + 1) + ". " + name + ", " + ChatListener.scoreboard.get(s)));
|
||||
i++;
|
||||
//page limit
|
||||
if(i > 7 + startIndex) break;
|
||||
if (i > 7 + startIndex) break;
|
||||
}
|
||||
|
||||
|
||||
return true;
|
||||
} else if(args[0].equalsIgnoreCase("guesses")) {
|
||||
} else if (args[0].equalsIgnoreCase("guesses")) {
|
||||
|
||||
if (EntityBingo.getCurrrentEvent() == null) {
|
||||
if (EntityBingo.getCurrentEvent() == null) {
|
||||
p.sendMessage(Helper.chatLabel() + Helper.color("No games have occurred recently."));
|
||||
}
|
||||
|
||||
//print off all guesses
|
||||
p.sendMessage(Helper.chatLabel() + Helper.color("List of all guesses in the " + (EntityBingo.getCurrrentEvent().isDone() ? "previous" : "current") + " event:"));
|
||||
//print off all guesses
|
||||
p.sendMessage(Helper.chatLabel() + Helper.color("List of all guesses in the " + (EntityBingo.getCurrentEvent().isDone() ? "previous" : "current") + " event:"));
|
||||
p.sendMessage(Helper.chatLabel() + Helper.color("----------------------------------"));
|
||||
|
||||
for (String s : EntityBingo.getCurrrentEvent().getEntries().keySet()) {
|
||||
for (String s : EntityBingo.getCurrentEvent().getEntries().keySet()) {
|
||||
String name = Bukkit.getOfflinePlayer(UUID.fromString(s)).getName();
|
||||
p.sendMessage(Helper.chatLabel() + Helper.color("&7 - " + name + ": " + EntityBingo.getCurrrentEvent().getEntries().get(s)));
|
||||
p.sendMessage(Helper.chatLabel() + Helper.color("&7 - " + name + ": " + EntityBingo.getCurrentEvent().getEntries().get(s)));
|
||||
}
|
||||
return true;
|
||||
} else if(args[0].equalsIgnoreCase("purge")) {
|
||||
} else if (args[0].equalsIgnoreCase("purge")) {
|
||||
if (!p.hasPermission("EntityBingo.admin")) {
|
||||
p.sendMessage(Helper.chatLabel() + Helper.color("&cYou do not have permissions to run this command."));
|
||||
return true;
|
||||
}
|
||||
|
||||
if(EntityBingo.getCurrrentEvent() == null) {
|
||||
p.sendMessage(Helper.chatLabel() + Helper.color("&cCleared all entries in the " + (EntityBingo.getCurrrentEvent().isDone() ? "previous" : "current") + " event. This won't effect the scoreboard."));
|
||||
if (EntityBingo.getCurrentEvent() == null) {
|
||||
p.sendMessage(Helper.chatLabel() + Helper.color("&cCleared all entries in the " + (EntityBingo.getCurrentEvent().isDone() ? "previous" : "current") + " event. This won't effect the scoreboard."));
|
||||
Bukkit.broadcastMessage(Helper.chatLabel() + Helper.color("&cEntries cleared by an admin!"));
|
||||
EntityBingo.getInstance().getLogger().info(Helper.color("&cEntries cleared by an admin!"));
|
||||
}
|
||||
//clear entry pool
|
||||
EntityBingo.getCurrrentEvent().getEntries().clear();
|
||||
p.sendMessage(Helper.chatLabel() + Helper.color("&cCleared all entries in the " + (EntityBingo.getCurrrentEvent().isDone() ? "previous" : "current") + " event. This won't effect the scoreboard."));
|
||||
EntityBingo.getCurrentEvent().getEntries().clear();
|
||||
p.sendMessage(Helper.chatLabel() + Helper.color("&cCleared all entries in the " + (EntityBingo.getCurrentEvent().isDone() ? "previous" : "current") + " event. This won't effect the scoreboard."));
|
||||
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")) {
|
||||
} else if (args[0].equalsIgnoreCase("reset")) {
|
||||
if (!p.hasPermission("EntityBingo.admin")) {
|
||||
p.sendMessage(Helper.chatLabel() + Helper.color("&cYou do not have permissions to run this command."));
|
||||
return true;
|
||||
}
|
||||
|
||||
//clear event
|
||||
EntityBingo.setCurrrentEvent(null);
|
||||
EntityBingo.setCurrentEvent(null);
|
||||
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;
|
||||
} else if(args[0].equalsIgnoreCase("reloadscoreboard")) {
|
||||
} else if (args[0].equalsIgnoreCase("reloadscoreboard")) {
|
||||
if (!p.hasPermission("EntityBingo.admin")) {
|
||||
p.sendMessage(Helper.chatLabel() + Helper.color("&cYou do not have permissions to run this command."));
|
||||
return true;
|
||||
}
|
||||
|
||||
HashMap<String, Object> raw = DataManager.getData("scoreboard.yml");
|
||||
HashMap<String, Object> data = (HashMap<String, Object>) raw.get("scores");
|
||||
//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()));
|
||||
try {
|
||||
HashMap<String, Object> data = (HashMap<String, Object>) raw.get("scores");
|
||||
//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
|
||||
ChatListener.scoreboard = Helper.sortData(filtered);
|
||||
//Save to static scoreboard reference
|
||||
ChatListener.scoreboard = Helper.sortData(filtered);
|
||||
|
||||
//clear event
|
||||
p.sendMessage(Helper.chatLabel() + Helper.color("&cReloaded scoreboard."));
|
||||
EntityBingo.getInstance().getLogger().info(Helper.color("&cAdmin reloaded scoreboard!"));
|
||||
//clear event
|
||||
p.sendMessage(Helper.chatLabel() + Helper.color("&cReloaded scoreboard."));
|
||||
EntityBingo.getInstance().getLogger().info(Helper.color("&cAdmin reloaded scoreboard!"));
|
||||
} catch (ClassCastException ex) {
|
||||
EntityBingo.getInstance().getLogger().log(Level.WARNING, "Failed to load scores, is scoreboard.yml broken?");
|
||||
}
|
||||
return true;
|
||||
} else {
|
||||
p.sendMessage(Helper.chatLabel() + Helper.color("&cError. Subcommand not found"));
|
||||
@@ -163,12 +171,11 @@ public class EntityBingoCommand implements CommandExecutor, TabCompleter {
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public List<String> onTabComplete(CommandSender sender, Command command, String label, String[] args) {
|
||||
public List<String> onTabComplete(@NonNull CommandSender sender, @NonNull Command command, @NonNull String label, String[] args) {
|
||||
final Player p = (Player) sender;
|
||||
if(args.length >= 1) {
|
||||
if(args.length >= 2) {
|
||||
if (args.length >= 1) {
|
||||
if (args.length >= 2) {
|
||||
return null;
|
||||
} else {
|
||||
if (p.hasPermission("EntityBingo.admin")) {
|
||||
|
||||
@@ -3,7 +3,6 @@ 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;
|
||||
@@ -26,16 +25,16 @@ public class ChatListener implements Listener {
|
||||
@EventHandler(priority = EventPriority.MONITOR)
|
||||
public void onBroadcast(BroadcastMessageEvent e) {
|
||||
boolean matchFound = e.getMessage().equals(EntityBingo.getInstance().getConfig().getString("startMessage"));
|
||||
boolean matchFound2 = false;
|
||||
boolean matchFound2;
|
||||
if(matchFound) {
|
||||
//start the event, only if previous one is marked as done
|
||||
if(EntityBingo.getCurrrentEvent() == null) {
|
||||
EntityBingo.setCurrrentEvent(new BingoEvent());
|
||||
if(EntityBingo.getCurrentEvent() == null) {
|
||||
EntityBingo.setCurrentEvent(new BingoEvent());
|
||||
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 {
|
||||
if(EntityBingo.getCurrrentEvent().isDone()) {
|
||||
EntityBingo.setCurrrentEvent(new BingoEvent());
|
||||
if(EntityBingo.getCurrentEvent().isDone()) {
|
||||
EntityBingo.setCurrentEvent(new BingoEvent());
|
||||
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 {
|
||||
@@ -43,7 +42,6 @@ public class ChatListener implements Listener {
|
||||
EntityBingo.getInstance().getLogger().info(Helper.chatLabel() + "Event Started too recently, ignoring Bingo attempt!");
|
||||
}
|
||||
}
|
||||
return;
|
||||
} else {
|
||||
//check for end broadcast
|
||||
String check = EntityBingo.getInstance().getConfig().getString("endMessage");
|
||||
@@ -51,8 +49,8 @@ public class ChatListener implements Listener {
|
||||
String p1 = e.getMessage().substring(0, 23);
|
||||
matchFound2 = p1.equals(check);
|
||||
if(matchFound2) {
|
||||
//check if we have an bingo event happening
|
||||
if(EntityBingo.getCurrrentEvent() != null) {
|
||||
//check if we have a bingo event happening
|
||||
if(EntityBingo.getCurrentEvent() != null) {
|
||||
Bukkit.broadcastMessage(Helper.chatLabel() + "Entity Bingo has ended!");
|
||||
|
||||
//extract the result number
|
||||
@@ -66,7 +64,7 @@ public class ChatListener implements Listener {
|
||||
i = Integer.parseInt(numberOnly);
|
||||
} catch (NumberFormatException exception) {
|
||||
EntityBingo.getInstance().getLogger().info(Helper.chatLabel() + Helper.color("&cFailed to find the result number, cancelling bingo."));
|
||||
EntityBingo.getCurrrentEvent().setDone();
|
||||
EntityBingo.getCurrentEvent().setDone();
|
||||
return;
|
||||
}
|
||||
}
|
||||
@@ -74,8 +72,8 @@ public class ChatListener implements Listener {
|
||||
//look for winners
|
||||
List<String> winners = new ArrayList<>();
|
||||
if(i >= 0) {
|
||||
for(String s : EntityBingo.getCurrrentEvent().getEntries().keySet()) {
|
||||
if(EntityBingo.getCurrrentEvent().getEntries().get(s) == i) {
|
||||
for(String s : EntityBingo.getCurrentEvent().getEntries().keySet()) {
|
||||
if(EntityBingo.getCurrentEvent().getEntries().get(s) == i) {
|
||||
winners.add(s);
|
||||
}
|
||||
}
|
||||
@@ -89,42 +87,46 @@ public class ChatListener implements Listener {
|
||||
} else {
|
||||
HashMap<String, Object> raw = DataManager.getData("scoreboard.yml");
|
||||
if(raw.containsKey("scores")) {
|
||||
HashMap<String, Object> data = (HashMap<String, Object>) raw.get("scores");
|
||||
for (String s : winners) {
|
||||
//broadcast winner
|
||||
OfflinePlayer player = Bukkit.getOfflinePlayer(UUID.fromString(s));
|
||||
Bukkit.broadcastMessage(Helper.chatLabel() + player.getName() + " guessed correctly!");
|
||||
EntityBingo.getInstance().getLogger().info(Helper.chatLabel() + player.getName() + " guessed correctly!");
|
||||
try {
|
||||
HashMap<String, Object> data = (HashMap<String, Object>) raw.get("scores");
|
||||
for (String s : winners) {
|
||||
//broadcast winner
|
||||
OfflinePlayer player = Bukkit.getOfflinePlayer(UUID.fromString(s));
|
||||
Bukkit.broadcastMessage(Helper.chatLabel() + player.getName() + " guessed correctly!");
|
||||
EntityBingo.getInstance().getLogger().info(Helper.chatLabel() + player.getName() + " guessed correctly!");
|
||||
|
||||
//get player uuid and save their wins
|
||||
UUID uuid = UUID.fromString(s);
|
||||
if(data != null) {
|
||||
if(data.containsKey(uuid.toString())) {
|
||||
data.put(uuid.toString(), (int) data.get(uuid.toString()) + 1);
|
||||
//get player uuid and save their wins
|
||||
UUID uuid = UUID.fromString(s);
|
||||
if (data != null) {
|
||||
if (data.containsKey(uuid.toString())) {
|
||||
data.put(uuid.toString(), (int) data.get(uuid.toString()) + 1);
|
||||
} else {
|
||||
data.put(uuid.toString(), 1);
|
||||
}
|
||||
} else {
|
||||
data.put(uuid.toString(), 1);
|
||||
EntityBingo.getInstance().getLogger().log(Level.WARNING, "Data failed to load, plugins/EntityBingo/data/scoreboard.yml may be broken.");
|
||||
}
|
||||
} else {
|
||||
EntityBingo.getInstance().getLogger().log(Level.WARNING, "Data failed to load, plugins/EntityBingo/data/scoreboard.yml may be broken.");
|
||||
}
|
||||
}
|
||||
|
||||
if(data != null) {
|
||||
//save scoreboard
|
||||
HashMap<String, Object> out = new HashMap<String, Object>();
|
||||
out.put("scores", data);
|
||||
DataManager.saveData("scoreboard.yml", out);
|
||||
if (data != null) {
|
||||
//save scoreboard
|
||||
HashMap<String, Object> out = new HashMap<>();
|
||||
out.put("scores", data);
|
||||
DataManager.saveData("scoreboard.yml", out);
|
||||
|
||||
//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()));
|
||||
//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);
|
||||
//Save to static scoreboard reference
|
||||
scoreboard = Helper.sortData(filtered);
|
||||
}
|
||||
} catch (ClassCastException ex) {
|
||||
EntityBingo.getInstance().getLogger().log(Level.WARNING, "Failed to load scores, is scoreboard.yml broken?");
|
||||
}
|
||||
} else {
|
||||
HashMap<String, Object> data = new HashMap<>();
|
||||
@@ -142,7 +144,7 @@ public class ChatListener implements Listener {
|
||||
}
|
||||
}
|
||||
//save scoreboard
|
||||
HashMap<String, Object> out = new HashMap<String, Object>();
|
||||
HashMap<String, Object> out = new HashMap<>();
|
||||
out.put("scores", data);
|
||||
DataManager.saveData("scoreboard.yml", out);
|
||||
}
|
||||
@@ -150,7 +152,7 @@ public class ChatListener implements Listener {
|
||||
|
||||
}
|
||||
|
||||
EntityBingo.getCurrrentEvent().setDone();
|
||||
EntityBingo.getCurrentEvent().setDone();
|
||||
|
||||
}
|
||||
} else {
|
||||
@@ -158,10 +160,6 @@ public class ChatListener implements Listener {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if(matchFound2) {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -170,8 +168,8 @@ public class ChatListener implements Listener {
|
||||
public void onChat(AsyncPlayerChatEvent e) {
|
||||
//Entry detection
|
||||
Bukkit.getLogger().info(Helper.chatLabel() + "comparing: " + "\"" + e.getMessage() + "\"");
|
||||
if (EntityBingo.getCurrrentEvent() != null) {
|
||||
if(EntityBingo.getCurrrentEvent().isDone()) {
|
||||
if (EntityBingo.getCurrentEvent() != null) {
|
||||
if(EntityBingo.getCurrentEvent().isDone()) {
|
||||
return;
|
||||
}
|
||||
String str = e.getMessage();
|
||||
@@ -193,7 +191,7 @@ public class ChatListener implements Listener {
|
||||
|
||||
if (guess > 0) {
|
||||
//do the entry
|
||||
int result = EntityBingo.getCurrrentEvent().addEntry(e.getPlayer().getUniqueId().toString(), guess);
|
||||
int result = EntityBingo.getCurrentEvent().addEntry(e.getPlayer().getUniqueId().toString(), guess);
|
||||
//feedback to entrant
|
||||
switch (result) {
|
||||
case 0 -> e.getPlayer().sendMessage(Helper.chatLabel() + Helper.color("&aYou have entered with the guess " + guess + "."));
|
||||
|
||||
@@ -3,31 +3,31 @@ package org.aubrithehuman.entitybingo.util;
|
||||
import org.bukkit.ChatColor;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class Helper {
|
||||
|
||||
/**
|
||||
* sorts a map from the highest value to the lowest value
|
||||
* @param data
|
||||
* @return
|
||||
*
|
||||
* @param data map of uuid and values
|
||||
* @return sorted map
|
||||
*/
|
||||
public static HashMap<String, Integer> sortData(Map<String, Integer> data) {
|
||||
|
||||
// Create a list from elements of HashMap
|
||||
List<Map.Entry<String, Integer> > list
|
||||
= new LinkedList<Map.Entry<String, Integer> >(
|
||||
List<Map.Entry<String, Integer>> list
|
||||
= new LinkedList<>(
|
||||
data.entrySet());
|
||||
|
||||
// Sort the list using lambda expression
|
||||
Collections.sort(
|
||||
list,
|
||||
(i1,
|
||||
i2) -> (- i1.getValue().compareTo(i2.getValue())));
|
||||
i2) -> (-i1.getValue().compareTo(i2.getValue())));
|
||||
|
||||
// put data from sorted list to hashmap
|
||||
HashMap<String, Integer> temp
|
||||
= new LinkedHashMap<String, Integer>();
|
||||
= new LinkedHashMap<>();
|
||||
for (Map.Entry<String, Integer> aa : list) {
|
||||
temp.put(aa.getKey(), aa.getValue());
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user