Skip to content

Commit

Permalink
Add GameConfig validation
Browse files Browse the repository at this point in the history
  • Loading branch information
lscgh committed Jan 25, 2024
1 parent 86e9552 commit 77d5ebd
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ public boolean onCommand(CommandSender sender, Command command, String label, St
if(args[0].equals(noAvailablePlayersPlaceholder[0]) && args[1].equals(noAvailablePlayersPlaceholder[1]) && args[2].equals(noAvailablePlayersPlaceholder[2])) return true;
}

// Create the game's config from the command's args
GameConfig config;

try {
Expand All @@ -66,6 +67,18 @@ public boolean onCommand(CommandSender sender, Command command, String label, St
return true;
}

// Check for errors in the game's config
List<String> configErrors = config.validate();
if(!configErrors.isEmpty()) {
for(String error: configErrors) {
sender.sendMessage(ChatColor.RED + error + ChatColor.RESET);
}

// Don't continue on error
return true;
}

// Show the config to the player
sender.sendMessage("You just executed /tictactoe correctly");
sender.sendMessage("Opponent player name: '" + config.opponentPlayer.getName() + "'");
sender.sendMessage("Game size: " + config.size);
Expand Down
14 changes: 14 additions & 0 deletions mavenmcserver/src/main/java/mavenmcserver/game/GameConfig.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package mavenmcserver.game;

import java.util.ArrayList;
import java.util.List;

import org.bukkit.entity.Player;
import org.joml.Vector3i;

Expand All @@ -15,4 +18,15 @@ public GameConfig(Player mainPlayer, Player opponentPlayer, Vector3i size, int w
this.size = size;
this.winRequiredAmount = winRequiredAmount;
}

/// Returns an empty list if everything is OK.
public List<String> validate() {
ArrayList<String> errors = new ArrayList<String>();

if(this.winRequiredAmount < this.size.maxComponent()) {
errors.add("The required win amount must not be larger than the size's largest dimension");
}

return errors;
}
}

0 comments on commit 77d5ebd

Please sign in to comment.