Skip to content

Commit

Permalink
Readability changes
Browse files Browse the repository at this point in the history
  • Loading branch information
lscgh committed Feb 7, 2024
1 parent 82afad2 commit 17a64a6
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 21 deletions.
35 changes: 26 additions & 9 deletions mavenmcserver/src/main/java/mavenmcserver/game/Game.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,13 @@

public class Game {

// Constants for the block materials the game uses
public static Material BASE_PLATE_MATERIAL = Material.BLACK_CONCRETE;
public static Material NEUTRAL_MATERIAL = Material.WHITE_CONCRETE;
public static Material MAIN_PLAYER_MATERIAL = Material.RED_CONCRETE;
public static Material OPPONENT_PLAYER_MATERIAL = Material.LIGHT_BLUE_CONCRETE;

// Constants for the sounds the game uses
public static Sound MARK_FIELD_SOUND = Sound.BLOCK_NOTE_BLOCK_BELL;
public static float MARK_FIELD_SOUND_PITCH = 0.5f;
public static Sound WIN_BEEP_SOUND = Sound.BLOCK_NOTE_BLOCK_BIT; // no pitch because it varies
Expand All @@ -50,39 +52,44 @@ public class Game {

/// Contains a list of game setups (value) that were lost by player (key) (ties count as well).
public static HashMap<Player, GameConfig> lostGames = new HashMap<Player, GameConfig>();


public UUID uuid = UUID.randomUUID();

public GameConfig config;
public GameListener listener;
public Location location;

public GameState state;
/// The FieldPoint of the last marked field, starting as null
public FieldPoint lastPlacePosition = null;
boolean didCompletePlace = true;
private boolean didCompletePlace = true;
public boolean opponentPlayersTurn = true;

public Location location;
public CubicBlockArea gameArea; // the area to protect

// The Plugin instance managing this game. Used for registering the listener, running task timers ...
public Plugin plugin;

// Repeatedly makes the blocks fall
// Repeatedly makes the blocks fall and checks for win
public BukkitRunnable gravityRunnable;

// Stores all the blocks as they were in the world before this game destroyed them to restore them to their previous state after game end.
private HashMap<Location, BlockData> beforeGameBlocks = new HashMap<Location, BlockData>();


public Game(GameConfig config, Plugin plugin, boolean isReturnMatch) {
Game.queuedGames.put(this.uuid, this);
this.registerQueued();

this.plugin = plugin;

this.config = config;
this.location = this.generateGameLocation();

this.plugin = plugin;

this.listener = new GameListener(this);
this.state = new GameState(this.config.size);

Location startBlock = new Location(this.location.getWorld(), this.location.getBlockX() - 2, this.location.getBlockY() - 1, this.location.getBlockZ() - 2);
Location endBlock = new Location(this.location.getWorld(), this.location.getBlockX() + this.config.size.x * 2, this.location.getBlockY() + this.config.size.y * 2, this.location.getBlockZ() + this.config.size.z * 2);
this.gameArea = new CubicBlockArea(startBlock, endBlock);
this.gameArea = this.generateGameArea();

this.gravityRunnable = new BukkitRunnable() {

Expand All @@ -107,6 +114,10 @@ public void run() {
this.inviteOpponent(isReturnMatch);
}

private void registerQueued() {
Game.queuedGames.put(this.uuid, this);
}

private Location generateGameLocation() {
// double type to get rid of casting in the switch statement!
double gameWidthInBlocks = (double)this.config.size.x * 2 - 1;
Expand Down Expand Up @@ -140,6 +151,12 @@ private Location generateGameLocation() {
return new Location(playerLocation.getWorld(), playerLocation.getBlockX() + offsetX, playerLocation.getBlockY(), playerLocation.getBlockZ() + offsetZ);
}

private CubicBlockArea generateGameArea() {
Location startBlock = new Location(this.location.getWorld(), this.location.getBlockX() - 2, this.location.getBlockY() - 1, this.location.getBlockZ() - 2);
Location endBlock = new Location(this.location.getWorld(), this.location.getBlockX() + this.config.size.x * 2, this.location.getBlockY() + this.config.size.y * 2, this.location.getBlockZ() + this.config.size.z * 2);
return new CubicBlockArea(startBlock, endBlock);
}

private void inviteOpponent(boolean isReturnMatch) {
if(isReturnMatch) {
this.config.opponentPlayer.sendMessage("Hello " + ChatColor.AQUA + ChatColor.BOLD + this.config.opponentPlayer.getName() + ChatColor.RESET + "! " + ChatColor.AQUA + ChatColor.BOLD + this.config.mainPlayer.getName() + ChatColor.RESET + " would like to play a return match with you!");
Expand Down
21 changes: 13 additions & 8 deletions mavenmcserver/src/main/java/mavenmcserver/game/GameConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,30 @@ public class GameConfig {
*/
public static int MIN_HEIGHT = 1;

static String ERROR_MAIN_PLAYER_NULL = "Couldn't add you to the game. Please retry!";
static String ERROR_OPPONENT_PLAYER_NULL = "Couldn't add the opponent player to the game.";
static String ERROR_PLAYER_ALREADY_IN_GAME = "You are currently playing a game of tic-tac-toe and, thus, cannot start another one.";
static String ERROR_WIN_REQUIRED_AMOUNT_TOO_LARGE = "The required win amount must not be larger than the size's largest dimension.";

/**
* The player who started the game
*/
public Player mainPlayer;
public final Player mainPlayer;

/**
* The player who was invited to the game
*/
public Player opponentPlayer;
public final Player opponentPlayer;

/**
* The X, Y, Z size of the game
*/
public Vector3i size;
public final Vector3i size;

/**
* The number of same-player-marked fields required for that player to win
*/
public int winRequiredAmount;
public final int winRequiredAmount;


public GameConfig(Player mainPlayer, Player opponentPlayer, Vector3i size, int winRequiredAmount) {
Expand All @@ -54,17 +59,17 @@ public List<String> validate() {
ArrayList<String> errors = new ArrayList<String>();

if(this.mainPlayer == null) {
errors.add("Couldn't add you to the game. Please retry!");
errors.add(GameConfig.ERROR_MAIN_PLAYER_NULL);
return errors;
}

if(this.opponentPlayer == null) {
errors.add("Couldn't add the opponent player to the game.");
errors.add(GameConfig.ERROR_OPPONENT_PLAYER_NULL);
return errors;
}

if(Game.runningGames.containsKey(this.mainPlayer)) {
errors.add("You are currently playing a game of tic-tac-toe and, thus, cannot start another one.");
errors.add(GameConfig.ERROR_PLAYER_ALREADY_IN_GAME);
return errors;
}

Expand Down Expand Up @@ -94,7 +99,7 @@ List<String> validateNumbers() {
}

if(this.winRequiredAmount > Math.max(this.size.x, Math.max(this.size.y, this.size.z))) {
errors.add("The required win amount must not be larger than the size's largest dimension.");
errors.add(GameConfig.ERROR_WIN_REQUIRED_AMOUNT_TOO_LARGE);
}

return errors;
Expand Down
7 changes: 5 additions & 2 deletions mavenmcserver/src/main/resources/classes.uml
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ class FieldPoint {
+UUID uuid
+GameConfig config
+GameListener listener
+Location location /' Start block location; stores world '/
+GameState state /' Stores the marked fields in a separate positioning system '/
+FieldPoint lastPlacePosition
boolean didCompletePlace
+boolean opponentPlayersTurn /' Whose turn it is! '/
+Location location /' Start block location; stores world '/
+CubicBlockArea gameArea /' Set in contructor '/
+Plugin plugin /' Used for listener and for placing blocks '/

Expand All @@ -55,7 +55,9 @@ class FieldPoint {

.. Methods ..
+Game(GameConfig config, Plugin plugin, boolean isReturnMatch)
-void registerQueued()
-Location generateGameLocation() /' Based on the main player's facing '/
-CubicBlockArea generateGameArea() /' Based on the game's size and the main player's location '/
-void inviteOpponent(boolean isReturnMatch) /' Calls start() when accepted '/
+void start() /' Starts the game and places the blocks; activates the listener '/
-void registerStarted() /' Configures the static HashMaps! '/
Expand All @@ -72,9 +74,10 @@ class FieldPoint {
.. Constants ..
+{static}int MIN_X_Z_SIZE
+{static}int MIN_HEIGHT
{static}String ERROR_MAIN_PLAYER_NULL

.. Fields ..
+Player mainPlayer
+{final}Player mainPlayer
+Player opponentPlayer
+Vector3i size
+int winRequiredAmount
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class GameConfigTest {
public void testValidate() {
GameConfig config = new GameConfig(null, null, new Vector3i(2, 1, 2), 2);
List<String> errors = config.validate();
assertTrue(errors.contains("Couldn't add you to the game. Please retry!"));
assertTrue(errors.contains(GameConfig.ERROR_MAIN_PLAYER_NULL));
}


Expand All @@ -32,7 +32,7 @@ public void testValidateNumbers() {

config = new GameConfig(null, null, new Vector3i(3, 1, 2), 4);
errors = config.validateNumbers();
assertTrue(errors.contains("The required win amount must not be larger than the size's largest dimension."));
assertTrue(errors.contains(GameConfig.ERROR_WIN_REQUIRED_AMOUNT_TOO_LARGE));

config = new GameConfig(null, null, new Vector3i(1, 1, 2), 3);
errors = config.validateNumbers();
Expand Down

0 comments on commit 17a64a6

Please sign in to comment.