Remove games
Dieser Commit ist enthalten in:
Ursprung
271622e967
Commit
2f69c4fe18
@ -1,30 +0,0 @@
|
|||||||
package de.steamwar.lobby.games;
|
|
||||||
|
|
||||||
import de.steamwar.lobby.LobbySystem;
|
|
||||||
import lombok.Getter;
|
|
||||||
import org.bukkit.Bukkit;
|
|
||||||
import org.bukkit.entity.Player;
|
|
||||||
import org.bukkit.event.HandlerList;
|
|
||||||
import org.bukkit.event.Listener;
|
|
||||||
|
|
||||||
public abstract class Game implements Listener {
|
|
||||||
|
|
||||||
@Getter
|
|
||||||
private final Player player;
|
|
||||||
|
|
||||||
@Getter
|
|
||||||
private final Player opponent;
|
|
||||||
|
|
||||||
protected Game(Player player, Player opponent) {
|
|
||||||
this.player = player;
|
|
||||||
this.opponent = opponent;
|
|
||||||
|
|
||||||
Bukkit.getPluginManager().registerEvents(this, LobbySystem.getPlugin());
|
|
||||||
}
|
|
||||||
|
|
||||||
public abstract void start();
|
|
||||||
|
|
||||||
public void end() {
|
|
||||||
HandlerList.unregisterAll(this);
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,62 +0,0 @@
|
|||||||
package de.steamwar.lobby.games;
|
|
||||||
|
|
||||||
import de.steamwar.lobby.LobbySystem;
|
|
||||||
import lombok.experimental.UtilityClass;
|
|
||||||
import net.md_5.bungee.api.chat.ClickEvent;
|
|
||||||
import org.bukkit.Bukkit;
|
|
||||||
import org.bukkit.entity.Player;
|
|
||||||
import org.bukkit.event.EventHandler;
|
|
||||||
import org.bukkit.event.Listener;
|
|
||||||
import org.bukkit.event.player.PlayerQuitEvent;
|
|
||||||
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
@UtilityClass
|
|
||||||
public class GameManager {
|
|
||||||
|
|
||||||
private Map<Player, Game> games = new HashMap<>();
|
|
||||||
|
|
||||||
private GameListener gameListener = new GameListener();
|
|
||||||
|
|
||||||
static {
|
|
||||||
Bukkit.getPluginManager().registerEvents(gameListener, LobbySystem.getPlugin());
|
|
||||||
}
|
|
||||||
|
|
||||||
private static class GameListener implements Listener {
|
|
||||||
|
|
||||||
@EventHandler(ignoreCancelled = true)
|
|
||||||
public void onPlayerQuit(PlayerQuitEvent event) {
|
|
||||||
games.remove(event.getPlayer());
|
|
||||||
games.values().removeIf(game -> game.getOpponent() == event.getPlayer());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void registerGame(Games games, Player player, Player opponent) {
|
|
||||||
if (opponent == player) {
|
|
||||||
LobbySystem.getMessage().send("GAMES_NO_SELF", player);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (GameManager.games.containsKey(opponent)) {
|
|
||||||
Game game = GameManager.games.get(opponent);
|
|
||||||
if (game.getOpponent() == player && games.isGame(game)) {
|
|
||||||
game.start();
|
|
||||||
GameManager.games.remove(opponent);
|
|
||||||
GameManager.games.remove(player);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (GameManager.games.containsKey(player)) {
|
|
||||||
LobbySystem.getMessage().send("GAMES_ALREADY_PRESENT", player);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
GameManager.games.put(player, games.createGame(player, opponent));
|
|
||||||
LobbySystem.getMessage().send("GAMES_CHALLENGED_BY", opponent, LobbySystem.getMessage().parse("GAMES_CHALLENGED_BY_HOVER", opponent), new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/games " + games.name() + " " + player.getName()), player.getName(), LobbySystem.getMessage().parse("GAMES_TICTACTOE", opponent));
|
|
||||||
}
|
|
||||||
|
|
||||||
public void unregisterGame(Player player) {
|
|
||||||
games.remove(player);
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,49 +0,0 @@
|
|||||||
package de.steamwar.lobby.games;
|
|
||||||
|
|
||||||
import de.steamwar.lobby.games.tictactoe.TicTacToe;
|
|
||||||
import org.bukkit.entity.Player;
|
|
||||||
|
|
||||||
public enum Games {
|
|
||||||
|
|
||||||
TICTACTOE {
|
|
||||||
@Override
|
|
||||||
public String getName() {
|
|
||||||
return "GAMES_TICTACTOE";
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean isGame(Game game) {
|
|
||||||
return game instanceof TicTacToe;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Game createGame(Player player, Player opponent) {
|
|
||||||
return new TicTacToe(player, opponent);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
/*
|
|
||||||
CONNECT_4 {
|
|
||||||
@Override
|
|
||||||
public String getName() {
|
|
||||||
return "GAMES_CONNECT_4";
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean isGame(Game game) {
|
|
||||||
return game instanceof Connect4;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Game createGame(Player player, Player opponent) {
|
|
||||||
return new Connect4(player, opponent);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
*/
|
|
||||||
;
|
|
||||||
|
|
||||||
public abstract String getName();
|
|
||||||
|
|
||||||
public abstract boolean isGame(Game game);
|
|
||||||
|
|
||||||
public abstract Game createGame(Player player, Player opponent);
|
|
||||||
}
|
|
@ -1,21 +0,0 @@
|
|||||||
package de.steamwar.lobby.games;
|
|
||||||
|
|
||||||
import de.steamwar.command.SWCommand;
|
|
||||||
import org.bukkit.entity.Player;
|
|
||||||
|
|
||||||
public class GamesCommand extends SWCommand {
|
|
||||||
|
|
||||||
public GamesCommand() {
|
|
||||||
super("games");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Register
|
|
||||||
public void game(Player player, Games games, Player opponent) {
|
|
||||||
GameManager.registerGame(games, player, opponent);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Register("stop")
|
|
||||||
public void stop(Player player) {
|
|
||||||
GameManager.unregisterGame(player);
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,15 +0,0 @@
|
|||||||
package de.steamwar.lobby.games;
|
|
||||||
|
|
||||||
import lombok.AllArgsConstructor;
|
|
||||||
import lombok.EqualsAndHashCode;
|
|
||||||
import lombok.Getter;
|
|
||||||
import lombok.ToString;
|
|
||||||
|
|
||||||
@AllArgsConstructor
|
|
||||||
@Getter
|
|
||||||
@ToString
|
|
||||||
@EqualsAndHashCode
|
|
||||||
public class Pair<A, B> {
|
|
||||||
private A a;
|
|
||||||
private B b;
|
|
||||||
}
|
|
@ -1,32 +0,0 @@
|
|||||||
package de.steamwar.lobby.games.connect4;
|
|
||||||
|
|
||||||
import de.steamwar.lobby.games.Game;
|
|
||||||
import org.bukkit.entity.Player;
|
|
||||||
|
|
||||||
import java.util.Random;
|
|
||||||
|
|
||||||
public class Connect4 extends Game {
|
|
||||||
|
|
||||||
private static final Random RANDOM = new Random();
|
|
||||||
|
|
||||||
/**
|
|
||||||
* {@code false} means player, {@code true} means opponent
|
|
||||||
*/
|
|
||||||
private boolean turn = RANDOM.nextBoolean();
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 0 = empty, 1 = player, 2 = opponent
|
|
||||||
*/
|
|
||||||
private final int[] board = new int[42];
|
|
||||||
|
|
||||||
private int turns = 0;
|
|
||||||
|
|
||||||
public Connect4(Player player, Player opponent) {
|
|
||||||
super(player, opponent);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void start() {
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,122 +0,0 @@
|
|||||||
package de.steamwar.lobby.games.tictactoe;
|
|
||||||
|
|
||||||
import de.steamwar.inventory.SWInventory;
|
|
||||||
import de.steamwar.inventory.SWItem;
|
|
||||||
import de.steamwar.lobby.LobbySystem;
|
|
||||||
import de.steamwar.lobby.games.Game;
|
|
||||||
import de.steamwar.lobby.games.Pair;
|
|
||||||
import org.bukkit.Bukkit;
|
|
||||||
import org.bukkit.Material;
|
|
||||||
import org.bukkit.entity.Player;
|
|
||||||
import org.bukkit.event.inventory.InventoryType;
|
|
||||||
|
|
||||||
import java.util.Random;
|
|
||||||
|
|
||||||
public class TicTacToe extends Game {
|
|
||||||
|
|
||||||
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}};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* {@code false} means player, {@code true} means opponent
|
|
||||||
*/
|
|
||||||
private boolean turn = RANDOM.nextBoolean();
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 0 = empty, 1 = player, 2 = opponent
|
|
||||||
*/
|
|
||||||
private final int[] board = new int[9];
|
|
||||||
|
|
||||||
private int turns = 0;
|
|
||||||
|
|
||||||
public TicTacToe(Player player, Player opponent) {
|
|
||||||
super(player, opponent);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void showBoard(Player player) {
|
|
||||||
Pair<Player, int[]> winner = checkWin();
|
|
||||||
boolean draw = turns == 9;
|
|
||||||
|
|
||||||
String title;
|
|
||||||
if (winner != null) {
|
|
||||||
title = LobbySystem.getMessage().parse("GAMES_WON", player, winner.getA().getName());
|
|
||||||
} else if (draw) {
|
|
||||||
title = LobbySystem.getMessage().parse("GAMES_DRAW", player);
|
|
||||||
} else {
|
|
||||||
title = LobbySystem.getMessage().parse("GAMES_TURN", player, (turn ? getOpponent() : getPlayer()).getName());
|
|
||||||
}
|
|
||||||
|
|
||||||
SWInventory inventory = new SWInventory(player, () -> Bukkit.createInventory(null, InventoryType.DROPPER, title));
|
|
||||||
for (int i = 0; i < 9; i++) {
|
|
||||||
if (board[i] == 0) {
|
|
||||||
int finalI = i;
|
|
||||||
if (winner != null) {
|
|
||||||
inventory.setItem(i, new SWItem(Material.GRAY_STAINED_GLASS_PANE, "§" + i));
|
|
||||||
} else {
|
|
||||||
inventory.setItem(i, new SWItem(Material.LIGHT_GRAY_WOOL, "§" + i, clickType -> {
|
|
||||||
if (turn == (player == getPlayer())) return;
|
|
||||||
|
|
||||||
board[finalI] = turn ? 2 : 1;
|
|
||||||
turn = !turn;
|
|
||||||
turns++;
|
|
||||||
showBoard(getPlayer());
|
|
||||||
showBoard(getOpponent());
|
|
||||||
}));
|
|
||||||
}
|
|
||||||
} else if (board[i] == 1) {
|
|
||||||
inventory.setItem(i, new SWItem((winner == null || contains(winner.getB(), i)) ? Material.RED_WOOL : Material.RED_STAINED_GLASS_PANE, "§" + i + "§cX"));
|
|
||||||
} else {
|
|
||||||
inventory.setItem(i, new SWItem((winner == null || contains(winner.getB(), i)) ? Material.BLUE_WOOL : Material.BLUE_STAINED_GLASS_PANE, "§" + i + "§bO"));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Remake
|
|
||||||
if (winner != null || draw) {
|
|
||||||
for (int i = 0; i < board.length; i++) {
|
|
||||||
inventory.setCallback(i, clickType -> {
|
|
||||||
player.performCommand("games tictactoe " + getOpponent().getName());
|
|
||||||
player.getOpenInventory().close();
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
inventory.addCloseRunnable(() -> {
|
|
||||||
Bukkit.getScheduler().runTaskLater(LobbySystem.getPlugin(), () -> {
|
|
||||||
if (getOpponent().getOpenInventory().getTopInventory().getType() != InventoryType.DROPPER) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (player.getOpenInventory().getTopInventory().getType() != InventoryType.DROPPER) {
|
|
||||||
getPlayer().closeInventory();
|
|
||||||
getOpponent().closeInventory();
|
|
||||||
LobbySystem.getMessage().send("GAMES_LEFT", getPlayer(), player.getName());
|
|
||||||
LobbySystem.getMessage().send("GAMES_LEFT", getOpponent(), player.getName());
|
|
||||||
end();
|
|
||||||
}
|
|
||||||
}, 1);
|
|
||||||
});
|
|
||||||
inventory.open();
|
|
||||||
}
|
|
||||||
|
|
||||||
private boolean contains(int[] ints, int i) {
|
|
||||||
for (int anInt : ints) {
|
|
||||||
if (anInt == i) return true;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
private Pair<Player, int[]> checkWin() {
|
|
||||||
for (int[] winningCombination : WINNING_COMBINATIONS) {
|
|
||||||
if (board[winningCombination[0]] == board[winningCombination[1]] && board[winningCombination[1]] == board[winningCombination[2]] && board[winningCombination[0]] != 0) {
|
|
||||||
return new Pair<>(board[winningCombination[0]] == 1 ? getPlayer() : getOpponent(), winningCombination);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void start() {
|
|
||||||
showBoard(getPlayer());
|
|
||||||
showBoard(getOpponent());
|
|
||||||
}
|
|
||||||
}
|
|
In neuem Issue referenzieren
Einen Benutzer sperren