1.3.3 beta 2

This commit is contained in:
Tyler Murphy 2021-12-25 22:46:22 -06:00
parent 8cf4d881dc
commit 6e09da9150
17 changed files with 473 additions and 36 deletions

View file

@ -39,5 +39,10 @@
<artifactId>ProtocolLib</artifactId>
<version>4.7.0</version>
</dependency>
<dependency>
<groupId>org.xerial</groupId>
<artifactId>sqlite-jdbc</artifactId>
<version>3.36.0.3</version>
</dependency>
</dependencies>
</project>

View file

@ -7,7 +7,9 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import net.tylermurphy.hideAndSeek.database.Database;
import net.tylermurphy.hideAndSeek.game.Status;
import net.tylermurphy.hideAndSeek.util.UUIDFetcher;
import org.bukkit.Bukkit;
import org.bukkit.command.Command;
@ -36,6 +38,7 @@ public class Main extends JavaPlugin implements Listener {
public Board board;
public WorldLoader worldLoader;
public Status status = Status.STANDBY;
public Database database;
private BukkitTask onTickTask;
public void onEnable() {
@ -64,10 +67,19 @@ public class Main extends JavaPlugin implements Listener {
board = new Board();
board.reload();
//Database
database = new Database();
database.init();
//UUIDFetcher Cache
UUIDFetcher.init();
//Init game
game = new Game();
// Start Tick Timer
onTickTask = Bukkit.getServer().getScheduler().runTaskTimer(this, () -> {
try{
game = new Game();
game.onTick();
} catch (Exception e) {
e.printStackTrace();
@ -79,6 +91,7 @@ public class Main extends JavaPlugin implements Listener {
public void onDisable() {
if(onTickTask != null)
onTickTask.cancel();
UUIDFetcher.cleanup();
}
public boolean onCommand(CommandSender sender, Command cmd,String label, String[] args) {

View file

@ -3,6 +3,7 @@ package net.tylermurphy.hideAndSeek.command;
import static net.tylermurphy.hideAndSeek.configuration.Config.*;
import net.tylermurphy.hideAndSeek.game.Status;
import net.tylermurphy.hideAndSeek.game.WinType;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;
@ -20,7 +21,7 @@ public class Stop implements ICommand {
if(Main.plugin.status == Status.STARTING || Main.plugin.status == Status.PLAYING) {
if(announceMessagesToNonPlayers) Bukkit.broadcastMessage(abortPrefix + message("STOP"));
else Util.broadcastMessage(abortPrefix + message("STOP"));
Main.plugin.game.stop();
Main.plugin.game.stop(WinType.NONE);
} else {
sender.sendMessage(errorPrefix + message("GAME_NOT_INPROGRESS"));
}

View file

@ -0,0 +1,60 @@
package net.tylermurphy.hideAndSeek.command;
import net.tylermurphy.hideAndSeek.Main;
import net.tylermurphy.hideAndSeek.database.PlayerInfo;
import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender;
import java.util.List;
import static net.tylermurphy.hideAndSeek.configuration.Config.*;
import static net.tylermurphy.hideAndSeek.configuration.Localization.*;
public class Top implements ICommand {
public void execute(CommandSender sender, String[] args) {
int page;
if(args.length == 0) page = 1;
else try{
page = Integer.parseInt(args[0]);
} catch(Exception e){
sender.sendMessage(errorPrefix + message("WORLDBORDER_INVALID_INPUT").addAmount(args[0]));
return;
}
if(page < 1){
sender.sendMessage(errorPrefix + message("WORLDBORDER_INVALID_INPUT").addAmount(page));
return;
}
String message = String.format(
"%s------- %sLEADERBOARD %s(Page %s) %s-------\n",
ChatColor.WHITE, ChatColor.BOLD, ChatColor.GRAY, page, ChatColor.WHITE);
List<PlayerInfo> infos = Main.plugin.database.playerInfo.getInfoPage(page);
int i = 1 + (page-1)*10;
for(PlayerInfo info : infos){
String name = Main.plugin.getServer().getOfflinePlayer(info.uuid).getName();
ChatColor color;
switch (i){
case 1: color = ChatColor.YELLOW; break;
case 2: color = ChatColor.GRAY; break;
case 3: color = ChatColor.GOLD; break;
default: color = ChatColor.WHITE; break;
}
message = message + String.format("%s%s. %s%s %s%s\n",
color, i, ChatColor.RED, info.wins, ChatColor.WHITE, name);
i++;
}
sender.sendMessage(message);
}
public String getLabel() {
return "top";
}
public String getUsage() {
return "<page>";
}
public String getDescription() {
return "Gets the top players in the server.";
}
}

View file

@ -0,0 +1,66 @@
package net.tylermurphy.hideAndSeek.command;
import com.comphenix.protocol.PacketType;
import net.tylermurphy.hideAndSeek.Main;
import net.tylermurphy.hideAndSeek.database.PlayerInfo;
import net.tylermurphy.hideAndSeek.util.CommandHandler;
import net.tylermurphy.hideAndSeek.util.UUIDFetcher;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.OfflinePlayer;
import org.bukkit.command.CommandSender;
import java.util.UUID;
import static net.tylermurphy.hideAndSeek.configuration.Config.*;
import static net.tylermurphy.hideAndSeek.configuration.Localization.*;
public class Wins implements ICommand {
public void execute(CommandSender sender, String[] args) {
Main.plugin.getServer().getScheduler().runTaskAsynchronously(Main.plugin, () -> {
UUID uuid;
String name;
if(args.length == 0) {
uuid = Main.plugin.getServer().getPlayer(sender.getName()).getUniqueId();
name = sender.getName();
}
else {
try {
name = args[0];
uuid = UUIDFetcher.getUUID(args[0]);
} catch (Exception e){
sender.sendMessage(errorPrefix + message("START_INVALID_NAME").addPlayer(args[0]));
return;
}
}
PlayerInfo info = Main.plugin.database.playerInfo.getInfo(uuid);
if(info == null){
sender.sendMessage(errorPrefix + message("NO_GAME_INFO"));
return;
}
String message = ChatColor.WHITE + "" + ChatColor.BOLD + "==============================\n";
message = message + message("INFORMATION_FOR").addPlayer(name) + "\n";
message = message + "==============================\n";
message = message + String.format("%sTOTAL WINS: %s%s\n%sHIDER WINS: %s%s\n%sSEEKER WINS: %s%s\n%sGAMES PLAYED: %s",
ChatColor.YELLOW, ChatColor.WHITE, info.wins, ChatColor.GOLD, ChatColor.WHITE, info.hider_wins,
ChatColor.RED, ChatColor.WHITE, info.seeker_wins, ChatColor.WHITE, info.games_played);
message = message + ChatColor.WHITE + "" + ChatColor.BOLD + "\n==============================";
sender.sendMessage(message);
});
}
public String getLabel() {
return "wins";
}
public String getUsage() {
return "<player>";
}
public String getDescription() {
return "Get the win information for yourself or another player.";
}
}

View file

@ -173,7 +173,6 @@ public class ConfigManager {
if(entry.getValue() instanceof String){
replace = "\"" + replace + "\"";
}
System.out.println(entry.getKey() + " " + index + " " + start + " " + end);
StringBuilder builder = new StringBuilder(yamlString);
builder.replace(start+1, end, replace);
yamlString = builder.toString();

View file

@ -0,0 +1,30 @@
package net.tylermurphy.hideAndSeek.database;
import net.tylermurphy.hideAndSeek.Main;
import java.io.File;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class Database {
private final File databaseFile = new File(Main.data, "database.db");
public PlayerInfoTable playerInfo;
protected Connection connect() {
Connection conn = null;
try {
String url = "jdbc:sqlite:"+databaseFile;
conn = DriverManager.getConnection(url);
} catch (SQLException e) {
System.out.println(e.getMessage());
}
return conn;
}
public void init(){
playerInfo = new PlayerInfoTable();
}
}

View file

@ -0,0 +1,18 @@
package net.tylermurphy.hideAndSeek.database;
import java.util.UUID;
public class PlayerInfo {
public UUID uuid;
public int wins, hider_wins, seeker_wins, games_played;
public PlayerInfo(UUID uuid, int wins, int hider_wins, int seeker_wins, int games_played){
this.uuid = uuid;
this.wins = wins;
this.hider_wins = hider_wins;
this.seeker_wins = seeker_wins;
this.games_played = games_played;
}
}

View file

@ -0,0 +1,109 @@
package net.tylermurphy.hideAndSeek.database;
import net.tylermurphy.hideAndSeek.Main;
import net.tylermurphy.hideAndSeek.game.WinType;
import net.tylermurphy.hideAndSeek.util.Util;
import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
public class PlayerInfoTable {
protected PlayerInfoTable(){
String sql = "CREATE TABLE IF NOT EXISTS player_info (\n"
+ " uuid BINARY(16) PRIMARY KEY,\n"
+ " wins int NOT NULL,\n"
+ " seeker_wins int NOT NULL,\n"
+ " hider_wins int NOT NULL,\n"
+ " games_played int NOT NULL\n"
+ ");";
try(Connection connection = Main.plugin.database.connect(); Statement statement = connection.createStatement()){
statement.execute(sql);
} catch (SQLException e){
Main.plugin.getLogger().severe("SQL Error: " + e.getMessage());
}
}
public PlayerInfo getInfo(UUID uuid){
String sql = "SELECT * FROM player_info WHERE uuid = ?;";
try(Connection connection = Main.plugin.database.connect(); PreparedStatement statement = connection.prepareStatement(sql)){
InputStream is = Util.convertUniqueId(uuid);
byte[] bytes = new byte[is.available()];
is.read(bytes);
statement.setBytes(1, bytes);
ResultSet rs = statement.executeQuery();
if(rs.next()){
PlayerInfo info = new PlayerInfo(
uuid,
rs.getInt("wins"),
rs.getInt("seeker_wins"),
rs.getInt("hider_wins"),
rs.getInt("games_played")
);
return info;
}
} catch (SQLException e){
Main.plugin.getLogger().severe("SQL Error: " + e.getMessage());
} catch (IOException e) {
Main.plugin.getLogger().severe("IO Error: " + e.getMessage());
e.printStackTrace();
}
return new PlayerInfo(uuid, 0, 0, 0, 0);
}
public List<PlayerInfo> getInfoPage(int page){
String sql = "SELECT * FROM player_info ORDER BY wins DESC LIMIT 10 OFFSET ?;";
try(Connection connection = Main.plugin.database.connect(); PreparedStatement statement = connection.prepareStatement(sql)){
statement.setInt(1, (page-1)*10);
ResultSet rs = statement.executeQuery();
List<PlayerInfo> infoList = new ArrayList<>();
while(rs.next()){
PlayerInfo info = new PlayerInfo(
Util.convertBinaryStream(rs.getBinaryStream("uuid")),
rs.getInt("wins"),
rs.getInt("seeker_wins"),
rs.getInt("hider_wins"),
rs.getInt("games_played")
);
infoList.add(info);
}
return infoList;
} catch (SQLException e){
Main.plugin.getLogger().severe("SQL Error: " + e.getMessage());
}
return null;
}
public boolean addWins(List<UUID> uuids, List<UUID> winners, WinType type){
for(UUID uuid : uuids){
String sql = "INSERT OR REPLACE INTO player_info (uuid, wins, seeker_wins, hider_wins, games_played) VALUES (?,?,?,?,?)";
PlayerInfo info = getInfo(uuid);
try(Connection connection = Main.plugin.database.connect(); PreparedStatement statement = connection.prepareStatement(sql)){
InputStream is = Util.convertUniqueId(uuid);
byte[] bytes = new byte[is.available()];
is.read(bytes);
statement.setBytes(1, bytes);
statement.setInt(2, info.wins + (winners.contains(uuid) ? 1 : 0));
statement.setInt(3, info.seeker_wins + (winners.contains(uuid) && type == WinType.SEEKER_WIN ? 1 : 0));
statement.setInt(4, info.hider_wins + (winners.contains(uuid) && type == WinType.HIDER_WIN ? 1 : 0));
statement.setInt(5, info.games_played + 1);
statement.execute();
} catch (SQLException e){
Main.plugin.getLogger().severe("SQL Error: " + e.getMessage());
e.printStackTrace();
return false;
} catch (IOException e) {
Main.plugin.getLogger().severe("IO Error: " + e.getMessage());
e.printStackTrace();
}
}
return true;
}
}

View file

@ -10,6 +10,7 @@ import org.bukkit.Bukkit;
import org.bukkit.GameMode;
import org.bukkit.Location;
import org.bukkit.Sound;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import net.tylermurphy.hideAndSeek.Main;
@ -18,7 +19,11 @@ import net.tylermurphy.hideAndSeek.util.Util;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.UUID;
import java.util.stream.Collectors;
import static net.tylermurphy.hideAndSeek.configuration.Localization.*;
import static net.tylermurphy.hideAndSeek.configuration.Config.*;
@ -80,11 +85,13 @@ public class Game {
Util.sendDelayedMessage(messagePrefix + message("START_COUNTDOWN").addAmount(3), Main.plugin.game.gameId, 20 * 27);
Util.sendDelayedMessage(messagePrefix + message("START_COUNTDOWN").addAmount(2), Main.plugin.game.gameId, 20 * 28);
Util.sendDelayedMessage(messagePrefix + message("START_COUNTDOWN").addAmount(1), Main.plugin.game.gameId, 20 * 29);
Bukkit.getServer().getScheduler().runTaskLater(Main.plugin, new Runnable() {
public void run() {
if(gameLength > 0) {
timeLeft = gameLength;
}
Bukkit.getServer().getScheduler().runTaskLater(Main.plugin, () -> {
if(temp != Main.plugin.game.gameId) return;
Util.broadcastMessage(messagePrefix + message("START"));
Main.plugin.status = Status.PLAYING;
for(Player player : Main.plugin.board.getPlayers()) {
Util.resetPlayer(player);
}
@ -103,20 +110,26 @@ public class Game {
glow = new Glow(Main.plugin.game.gameId);
}
if(gameLength > 0) {
timeLeft = gameLength;
}
}
Main.plugin.status = Status.PLAYING;
}, 20 * 30);
}
public void stop(){
public void stop(WinType type){
if(Main.plugin.status == Status.STANDBY) return;
tick = 0;
countdownTime = -1;
Main.plugin.status = Status.STANDBY;
Main.plugin.game.gameId++;
timeLeft = 0;
List<UUID> players = Main.plugin.board.getPlayers().stream().map(Entity::getUniqueId).collect(Collectors.toList());
if(type == WinType.HIDER_WIN){
List<UUID> winners = Main.plugin.board.getHiders().stream().map(Entity::getUniqueId).collect(Collectors.toList());
Main.plugin.database.playerInfo.addWins(players, winners, type);
} else if(type == WinType.SEEKER_WIN){
List<UUID> winners = new ArrayList<>();
winners.add(Main.plugin.board.getFirstSeeker().getUniqueId());
Main.plugin.database.playerInfo.addWins(players, winners, type);
}
Worldborder.resetWorldborder("hideandseek_"+spawnWorld);
for(Player player : Main.plugin.board.getPlayers()) {
Main.plugin.board.createLobbyBoard(player);
@ -146,12 +159,12 @@ public class Game {
if(( Main.plugin.status == Status.STARTING || Main.plugin.status == Status.PLAYING ) && Main.plugin.board.sizeHider() < 1) {
if(announceMessagesToNonPlayers) Bukkit.broadcastMessage(gameoverPrefix + message("GAME_GAMEOVER_HIDERS_FOUND"));
else Util.broadcastMessage(gameoverPrefix + message("GAME_GAMEOVER_HIDERS_FOUND"));
stop();
stop(WinType.SEEKER_WIN);
}
if(( Main.plugin.status == Status.STARTING || Main.plugin.status == Status.PLAYING ) && Main.plugin.board.sizeSeeker() < 1) {
if(announceMessagesToNonPlayers) Bukkit.broadcastMessage(abortPrefix + message("GAME_GAMEOVER_SEEKERS_QUIT"));
else Util.broadcastMessage(abortPrefix + message("GAME_GAMEOVER_SEEKERS_QUIT"));
stop();
stop(WinType.NONE);
}
tick++;
@ -219,7 +232,7 @@ public class Game {
if(timeLeft < 1) {
if(announceMessagesToNonPlayers) Bukkit.broadcastMessage(gameoverPrefix + message("GAME_GAMEOVER_TIME"));
else Util.broadcastMessage(gameoverPrefix + message("GAME_GAMEOVER_TIME"));
stop();
stop(WinType.HIDER_WIN);
}
}
}

View file

@ -0,0 +1,7 @@
package net.tylermurphy.hideAndSeek.game;
public enum WinType {
HIDER_WIN,
SEEKER_WIN,
NONE
}

View file

@ -73,6 +73,10 @@ public class Board {
return Seeker.stream().map(playerName -> playerList.get(playerName)).collect(Collectors.toList());
}
public Player getFirstSeeker(){
return playerList.get(Seeker.get(0));
}
public List<Player> getSpectators(){
return Spectator.stream().map(playerName -> playerList.get(playerName)).collect(Collectors.toList());
}

View file

@ -3,6 +3,8 @@ package net.tylermurphy.hideAndSeek.util;
import static net.tylermurphy.hideAndSeek.configuration.Config.*;
import static net.tylermurphy.hideAndSeek.configuration.Localization.*;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.Map;
@ -38,6 +40,8 @@ public class CommandHandler {
registerCommand(new SetBounds());
registerCommand(new Join());
registerCommand(new Leave());
registerCommand(new Top());
registerCommand(new Wins());
}
public static boolean handleCommand(CommandSender sender, Command cmd, String label, String[] args) {

View file

@ -0,0 +1,84 @@
package net.tylermurphy.hideAndSeek.util;
import net.tylermurphy.hideAndSeek.Main;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
public final class UUIDFetcher {
private static final Map<String,UUID> CACHE = new HashMap<>();
private static final String UUID_URL = "https://api.mojang.com/users/profiles/minecraft/";
private static int cacheTask;
public static void init(){
cacheTask = Main.plugin.getServer().getScheduler().scheduleSyncRepeatingTask(Main.plugin, CACHE::clear,600*20, 600*20);
}
public static void cleanup(){
Main.plugin.getServer().getScheduler().cancelTask(cacheTask);
}
public static UUID getUUID(String playername) {
if(CACHE.containsKey(playername)) return CACHE.get(playername);
String output = callURL(UUID_URL + playername);
StringBuilder result = new StringBuilder();
readData(output, result);
String u = result.toString();
StringBuilder uuid = new StringBuilder();
for (int i = 0; i <= 31; i++) {
uuid.append(u.charAt(i));
if (i == 7 || i == 11 || i == 15 || i == 19) {
uuid.append('-');
}
}
return UUID.fromString(uuid.toString());
}
private static void readData(String toRead, StringBuilder result) {
for (int i = toRead.length() - 3; i >= 0; i--) {
if (toRead.charAt(i) != '"') {
result.insert(0, toRead.charAt(i));
} else {
break;
}
}
}
private static String callURL(String urlStr) {
StringBuilder sb = new StringBuilder();
URLConnection urlConn;
InputStreamReader in;
try {
URL url = new URL(urlStr);
urlConn = url.openConnection();
if (urlConn != null) {
urlConn.setReadTimeout(60 * 1000);
}
if (urlConn != null && urlConn.getInputStream() != null) {
in = new InputStreamReader(urlConn.getInputStream(),
Charset.defaultCharset());
BufferedReader bufferedReader = new BufferedReader(in);
int cp;
while ((cp = bufferedReader.read()) != -1) {
sb.append((char) cp);
}
bufferedReader.close();
in.close();
}
} catch (Exception e) {
e.printStackTrace();
}
return sb.toString();
}
}

View file

@ -3,10 +3,13 @@ package net.tylermurphy.hideAndSeek.util;
import static net.tylermurphy.hideAndSeek.configuration.Config.*;
import java.io.*;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;
import com.google.common.io.ByteStreams;
import net.md_5.bungee.api.ChatColor;
import net.tylermurphy.hideAndSeek.configuration.Items;
import net.tylermurphy.hideAndSeek.configuration.LocalizationString;
@ -92,4 +95,22 @@ public class Util {
for(ItemStack i : player.getInventory().getContents())
if(hi.isSimilar(i)) player.getInventory().remove(i);
}
public static InputStream convertUniqueId(UUID uuid) {
byte[] bytes = new byte[16];
ByteBuffer.wrap(bytes)
.putLong(uuid.getMostSignificantBits())
.putLong(uuid.getLeastSignificantBits());
return new ByteArrayInputStream(bytes);
}
public static UUID convertBinaryStream(InputStream stream) {
ByteBuffer buffer = ByteBuffer.allocate(16);
try {
buffer.put(ByteStreams.toByteArray(stream));
buffer.flip();
return new UUID(buffer.getLong(), buffer.getLong());
} catch (IOException e) {}
return null;
}
}

View file

@ -60,7 +60,8 @@ Localization:
BOUNDS_WRONG_WORLD: "Führe diesen Befehl bitte in der Spielwelt aus."
BOUNDS: "Grenzen erfolgreich an dieser Position gesetzt. ({AMOUNT}/2)"
NOT_AT_ZERO: "Bitte nicht an einer Position setzen, die eine Koordinate bei 0 enthält."
NO_GAME_INFO: "Der Spieler hat keine Gameplay-Informationen."
INFORMATION_FOR: "Gewinninformationen für {PLAYER}"
# DO NOT EDIT IT OR IT MAY BREAK OR RESET FILE
version: 2
type: "de-DE"

View file

@ -60,6 +60,8 @@ Localization:
BOUNDS_WRONG_WORLD: "Please run this command in the game world."
BOUNDS: "Successfully set bounds at this position ({AMOUNT}/2)."
NOT_AT_ZERO: "Please do not set at a location containing a coordinate at 0."
NO_GAME_INFO: "Player has no gameplay information."
INFORMATION_FOR: "Win information for {PLAYER}:"
# DO NOT EDIT IT OR IT MAY BREAK OR RESET FILE
version: 2