From de505889856123c1eeea5f94d726174912dd589d Mon Sep 17 00:00:00 2001 From: Chaoscaot Date: Fri, 7 Apr 2023 21:34:13 +0200 Subject: [PATCH 1/2] Add TicTacToe --- src/de/steamwar/lobby/LobbySystem.java | 2 + src/de/steamwar/lobby/LobbySystem.properties | 10 ++ .../steamwar/lobby/LobbySystem_de.properties | 10 ++ src/de/steamwar/lobby/games/GamesCommand.java | 37 +++++ src/de/steamwar/lobby/games/TicTacToe.java | 148 ++++++++++++++++++ 5 files changed, 207 insertions(+) create mode 100644 src/de/steamwar/lobby/games/GamesCommand.java create mode 100644 src/de/steamwar/lobby/games/TicTacToe.java diff --git a/src/de/steamwar/lobby/LobbySystem.java b/src/de/steamwar/lobby/LobbySystem.java index f87d9d4..e5a3c92 100644 --- a/src/de/steamwar/lobby/LobbySystem.java +++ b/src/de/steamwar/lobby/LobbySystem.java @@ -24,6 +24,7 @@ import de.steamwar.lobby.command.FlyCommand; import de.steamwar.lobby.command.HologramCommand; import de.steamwar.lobby.command.ModifyCommand; import de.steamwar.lobby.command.PortalCommand; +import de.steamwar.lobby.games.GamesCommand; import de.steamwar.lobby.jumpandrun.JumpAndRun; import de.steamwar.lobby.listener.*; import de.steamwar.lobby.map.CustomMapCommand; @@ -80,6 +81,7 @@ public class LobbySystem extends JavaPlugin { new AlphaWall(l -> l.getZ() > 892, AlphaWall.REFLECT_Z); new AlphaWall(l -> l.getZ() < 1794, AlphaWall.REFLECT_Z); new LobbyPacketHandler().register(); + new GamesCommand(); } @Override diff --git a/src/de/steamwar/lobby/LobbySystem.properties b/src/de/steamwar/lobby/LobbySystem.properties index 8cc5995..3467e47 100644 --- a/src/de/steamwar/lobby/LobbySystem.properties +++ b/src/de/steamwar/lobby/LobbySystem.properties @@ -111,6 +111,16 @@ JUMP_AND_RUN_CANCEL = {0} JUMP_AND_RUN_TIME = mm:ss SSS JUMP_AND_RUN_FINISHED = §aFinished in {0} with {1} fails +# Games +GAMES_TICTACTOE = TicTacToe +GAMES_NO_SELF = §cYou can\'t play against yourself +GAMES_CHALLENGED_BY = §e{0} §7challenged you to a game of §e{1} +GAMES_CHALLENGED_BY_HOVER = §7Click to accept +GAMES_WON = §6{0} has won! +GAMES_DRAW = §6Draw! +GAMES_TURN = {0}\'s turn +GAMES_LEFT = §7{0} left the game + # Easter Egg Hunt DIFFICULTY_EASY = §aEasy DIFFICULTY_MEDIUM = §eMedium diff --git a/src/de/steamwar/lobby/LobbySystem_de.properties b/src/de/steamwar/lobby/LobbySystem_de.properties index a836eeb..cdf431c 100644 --- a/src/de/steamwar/lobby/LobbySystem_de.properties +++ b/src/de/steamwar/lobby/LobbySystem_de.properties @@ -105,6 +105,16 @@ PARTICLE_EGG_HUNT_HALF = §fDie Jagd ist eröffnet JUMP_AND_RUN_FINISHED = §aBeendet in {0} mit {1} Fails +# Games +GAMES_TICTACTOE = TicTacToe +GAMES_NO_SELF = §cDu kannst dich nicht selbst herausfordern! +GAMES_CHALLENGED_BY = §e{0} §7hat dich zu einem Spiel §e{1}§7 herausgefordert! +GAMES_CHALLENGED_BY_HOVER = §7Klicke hier, um das Spiel zu bestätigen +GAMES_WON = §6{0} hat das Spiel gewonnen! +GAMES_DRAW = §6Unentschieden! +GAMES_TURN = {0} ist dran +GAMES_LEFT = §7{0} hat das Spiel verlassen + # Easter Egg Hunt DIFFICULTY_EASY = §aLeicht DIFFICULTY_MEDIUM = §eMedium diff --git a/src/de/steamwar/lobby/games/GamesCommand.java b/src/de/steamwar/lobby/games/GamesCommand.java new file mode 100644 index 0000000..1cdb267 --- /dev/null +++ b/src/de/steamwar/lobby/games/GamesCommand.java @@ -0,0 +1,37 @@ +package de.steamwar.lobby.games; + +import de.steamwar.command.SWCommand; +import de.steamwar.lobby.LobbySystem; +import net.md_5.bungee.api.chat.ClickEvent; +import net.md_5.bungee.api.chat.TextComponent; +import org.bukkit.entity.Player; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; + +public class GamesCommand extends SWCommand { + + private final HashMap> CHALLENGES = new HashMap<>(); + + public GamesCommand() { + super("games"); + } + + @Register("tictactoe") + public void tictactoe(Player player, Player target) { + if(player == target) { + LobbySystem.getMessage().send("GAMES_NO_SELF", player); + return; + } + + if(CHALLENGES.containsKey(player) && CHALLENGES.get(player).contains(target)) { + new TicTacToe(player, target).updateBoard(); + CHALLENGES.get(player).remove(target); + return; + } + + CHALLENGES.computeIfAbsent(target, k -> new ArrayList<>()).add(player); + LobbySystem.getMessage().send("GAMES_CHALLENGED_BY", target, LobbySystem.getMessage().parse("GAMES_CHALLENGED_BY_HOVER", target), new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/games tictactoe " + player.getName()), player.getName(), LobbySystem.getMessage().parse("GAMES_TICTACTOE", target)); + } +} diff --git a/src/de/steamwar/lobby/games/TicTacToe.java b/src/de/steamwar/lobby/games/TicTacToe.java new file mode 100644 index 0000000..64addc5 --- /dev/null +++ b/src/de/steamwar/lobby/games/TicTacToe.java @@ -0,0 +1,148 @@ +package de.steamwar.lobby.games; + +import de.steamwar.inventory.SWInventory; +import de.steamwar.inventory.SWItem; +import de.steamwar.lobby.LobbySystem; +import org.bukkit.Bukkit; +import org.bukkit.Material; +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.HandlerList; +import org.bukkit.event.Listener; +import org.bukkit.event.inventory.InventoryType; +import org.bukkit.event.player.PlayerQuitEvent; + +import java.util.Random; + +public class TicTacToe implements Listener { + + private static final Random random = new Random(); + + private static final int[][] WINNING_COMBINATIONS = { + {0, 1, 2}, + {3, 4, 5}, + {6, 7, 8}, + {0, 3, 6}, + {1, 4, 7}, + {2, 5, 8}, + {0, 4, 8}, + {2, 4, 6} + }; + + private final Player player1; + private final Player player2; + private Player turn; + + private final int[] board = new int[9]; + + private int turns = 0; + + public TicTacToe(Player player1, Player player2) { + this.player1 = player1; + this.player2 = player2; + this.turn = random.nextBoolean() ? player1 : player2; + Bukkit.getPluginManager().registerEvents(this, LobbySystem.getPlugin()); + } + + public void updateBoard() { + for (int[] winningCombination : WINNING_COMBINATIONS) { + if(board[winningCombination[0]] == board[winningCombination[1]] && + board[winningCombination[1]] == board[winningCombination[2]] && + board[winningCombination[0]] != 0) { + win(turn, winningCombination); + return; + } + } + + if(turns++ == 9) { + draw(); + return; + } + + turn = turn == player1 ? player2 : player1; + showBoard(player1); + showBoard(player2); + } + + private void win(Player player, int[] winningCombination) { + SWInventory inventory = createCurrentBoard(player1, LobbySystem.getMessage().parse("GAMES_WON", player1, player.getName())); + SWInventory inventory2 = createCurrentBoard(player2, LobbySystem.getMessage().parse("GAMES_WON", player2, player.getName())); + for (int j : winningCombination) { + inventory.setItem(j, new SWItem(Material.LIME_WOOL, "§" + j + "§a" + (board[j] == 1 ? "X" : "O"))); + inventory2.setItem(j, new SWItem(Material.LIME_WOOL, "§" + j + "§a" + (board[j] == 1 ? "X" : "O"))); + } + remake(inventory, inventory2); + inventory.open(); + inventory2.open(); + } + + private void draw() { + SWInventory inventory = createCurrentBoard(player1, LobbySystem.getMessage().parse("GAMES_DRAW", player1)); + SWInventory inventory2 = createCurrentBoard(player2, LobbySystem.getMessage().parse("GAMES_DRAW", player2)); + remake(inventory, inventory2); + inventory.open(); + inventory2.open(); + } + + private void remake(SWInventory inventory, SWInventory inventory2) { + for (int i = 0; i < board.length; i++) { + inventory.setCallback(i, clickType -> player1.performCommand("games tictactoe " + player2.getName())); + inventory2.setCallback(i, clickType -> player2.performCommand("games tictactoe " + player1.getName())); + } + } + + private void end() { + HandlerList.unregisterAll(this); + } + + private void showBoard(Player player) { + SWInventory inventory = createCurrentBoard(player, LobbySystem.getMessage().parse("GAMES_TURN", player, turn.getName())); + inventory.addCloseRunnable(() -> { + Bukkit.getScheduler().runTaskLater(LobbySystem.getPlugin(), () -> { + if(player.getOpenInventory().getTopInventory().getType() != InventoryType.DROPPER) { + player1.closeInventory(); + player2.closeInventory(); + LobbySystem.getMessage().send("GAMES_LEFT", player1, player.getName()); + LobbySystem.getMessage().send("GAMES_LEFT", player2, player.getName()); + end(); + } + }, 1); + }); + inventory.open(); + } + + private SWInventory createCurrentBoard(Player player, String title) { + SWInventory inventory = new SWInventory(player, () -> Bukkit.createInventory(null, InventoryType.DROPPER, title)); + for (int i = 0; i < board.length; i++) { + if(board[i] == 0) { + int finalI = i; + inventory.setItem(i, new SWItem(Material.LIGHT_GRAY_WOOL, "§" + i, clickType -> makeMove(player, finalI))); + } else { + inventory.setItem(i, new SWItem(board[i] == 1 ? Material.RED_WOOL : Material.BLUE_WOOL, "§" + i + "§" + (board[i] == 1? "c" : "3") + (board[i] == 1 ? "X" : "O"))); + } + } + return inventory; + } + + private void makeMove(Player player, int i) { + if(player != turn) { + return; + } + + if(board[i] != 0) { + return; + } + + board[i] = player == player1 ? 1 : 2; + updateBoard(); + } + + @EventHandler + public void onPlayerQuit(PlayerQuitEvent event) { + if(event.getPlayer() == player1 || event.getPlayer() == player2) { + player1.closeInventory(); + player2.closeInventory(); + end(); + } + } +} -- 2.39.5 From 2c441bbcfd1e9cf049e7f6b656b778d6bf3a9c90 Mon Sep 17 00:00:00 2001 From: Chaoscaot Date: Fri, 7 Apr 2023 22:19:10 +0200 Subject: [PATCH 2/2] Add TicTacToe --- src/de/steamwar/lobby/games/GamesCommand.java | 7 ++++--- src/de/steamwar/lobby/games/TicTacToe.java | 7 ++++++- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/src/de/steamwar/lobby/games/GamesCommand.java b/src/de/steamwar/lobby/games/GamesCommand.java index 1cdb267..c6e597d 100644 --- a/src/de/steamwar/lobby/games/GamesCommand.java +++ b/src/de/steamwar/lobby/games/GamesCommand.java @@ -3,16 +3,16 @@ package de.steamwar.lobby.games; import de.steamwar.command.SWCommand; import de.steamwar.lobby.LobbySystem; import net.md_5.bungee.api.chat.ClickEvent; -import net.md_5.bungee.api.chat.TextComponent; import org.bukkit.entity.Player; import java.util.ArrayList; import java.util.HashMap; import java.util.List; +import java.util.Map; public class GamesCommand extends SWCommand { - private final HashMap> CHALLENGES = new HashMap<>(); + private final Map> CHALLENGES = new HashMap<>(); public GamesCommand() { super("games"); @@ -27,7 +27,8 @@ public class GamesCommand extends SWCommand { if(CHALLENGES.containsKey(player) && CHALLENGES.get(player).contains(target)) { new TicTacToe(player, target).updateBoard(); - CHALLENGES.get(player).remove(target); + CHALLENGES.remove(player); + CHALLENGES.remove(target); return; } diff --git a/src/de/steamwar/lobby/games/TicTacToe.java b/src/de/steamwar/lobby/games/TicTacToe.java index 64addc5..ecf847a 100644 --- a/src/de/steamwar/lobby/games/TicTacToe.java +++ b/src/de/steamwar/lobby/games/TicTacToe.java @@ -37,6 +37,8 @@ public class TicTacToe implements Listener { private int turns = 0; + private boolean running = true; + public TicTacToe(Player player1, Player player2) { this.player1 = player1; this.player2 = player2; @@ -65,6 +67,7 @@ public class TicTacToe implements Listener { } private void win(Player player, int[] winningCombination) { + running = false; SWInventory inventory = createCurrentBoard(player1, LobbySystem.getMessage().parse("GAMES_WON", player1, player.getName())); SWInventory inventory2 = createCurrentBoard(player2, LobbySystem.getMessage().parse("GAMES_WON", player2, player.getName())); for (int j : winningCombination) { @@ -77,6 +80,7 @@ public class TicTacToe implements Listener { } private void draw() { + running = false; SWInventory inventory = createCurrentBoard(player1, LobbySystem.getMessage().parse("GAMES_DRAW", player1)); SWInventory inventory2 = createCurrentBoard(player2, LobbySystem.getMessage().parse("GAMES_DRAW", player2)); remake(inventory, inventory2); @@ -92,6 +96,7 @@ public class TicTacToe implements Listener { } private void end() { + running = false; HandlerList.unregisterAll(this); } @@ -99,7 +104,7 @@ public class TicTacToe implements Listener { SWInventory inventory = createCurrentBoard(player, LobbySystem.getMessage().parse("GAMES_TURN", player, turn.getName())); inventory.addCloseRunnable(() -> { Bukkit.getScheduler().runTaskLater(LobbySystem.getPlugin(), () -> { - if(player.getOpenInventory().getTopInventory().getType() != InventoryType.DROPPER) { + if(player.getOpenInventory().getTopInventory().getType() != InventoryType.DROPPER && running) { player1.closeInventory(); player2.closeInventory(); LobbySystem.getMessage().send("GAMES_LEFT", player1, player.getName()); -- 2.39.5