Introduced a new `flagTurnInLocation` property to the `Team` class, used to determine where players return flags. Updated relevant logic in `Flag` and `CreateTeam` to utilize this new property, including reducing the required distance for flag turn-in.
98 lines
3.4 KiB
Java
98 lines
3.4 KiB
Java
package com.alttd.ctf.commands.subcommands;
|
|
|
|
import com.alttd.ctf.Main;
|
|
import com.alttd.ctf.commands.SubCommand;
|
|
import com.alttd.ctf.config.Messages;
|
|
import com.alttd.ctf.game.GameManager;
|
|
import com.alttd.ctf.json_config.JacksonConfig;
|
|
import com.alttd.ctf.json_config.JsonConfigManager;
|
|
import com.alttd.ctf.team.Team;
|
|
import com.alttd.ctf.team.TeamColor;
|
|
import lombok.AllArgsConstructor;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import net.kyori.adventure.text.minimessage.MiniMessage;
|
|
import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder;
|
|
import org.bukkit.command.CommandSender;
|
|
import org.bukkit.entity.Player;
|
|
|
|
import java.awt.*;
|
|
import java.io.File;
|
|
import java.io.IOException;
|
|
import java.util.List;
|
|
|
|
@Slf4j
|
|
@AllArgsConstructor
|
|
public class CreateTeam extends SubCommand {
|
|
|
|
private final Main main;
|
|
private final GameManager gameManager;
|
|
private final JsonConfigManager<Team> jsonConfigManager = new JsonConfigManager<>(JacksonConfig.configureMapper());
|
|
|
|
@FunctionalInterface
|
|
private interface CreateTeamConsumer {
|
|
int apply(Team team);
|
|
}
|
|
|
|
@Override
|
|
public int onCommand(CommandSender commandSender, String[] args) {
|
|
return handle(commandSender, args, team -> {
|
|
File teamsDirectory = new File(main.getDataFolder(), "teams");
|
|
try {
|
|
jsonConfigManager.saveConfig(team, teamsDirectory, String.valueOf(team.getId()));
|
|
} catch (IOException e) {
|
|
log.error("Unable to save config teams config with id {}.", team.getId(), e);
|
|
commandSender.sendRichMessage("<red>Unable to save team</red>");
|
|
return -1;
|
|
}
|
|
gameManager.registerTeam(team);
|
|
commandSender.sendRichMessage("<green>Created team <team> and registered them.</green>",
|
|
Placeholder.component("team", team.getName()));
|
|
return 0;
|
|
});
|
|
}
|
|
|
|
private int handle(CommandSender commandSender, String[] args, CreateTeamConsumer consumer) {
|
|
if (args.length != 3) {
|
|
return -1;
|
|
}
|
|
if (!(commandSender instanceof Player player)) {
|
|
commandSender.sendRichMessage(Messages.GENERIC.PLAYER_ONLY);
|
|
return -1;
|
|
}
|
|
String name = args[1];
|
|
if (name.length() > 16) {
|
|
commandSender.sendRichMessage("<red>name needs to be between 3 and 16 characters</red>");
|
|
return 1;
|
|
}
|
|
|
|
String color = args[2];
|
|
if (!color.matches("^#[0-9a-fA-F]{6}$")) {
|
|
commandSender.sendRichMessage("<red>Invalid hex color, style it like #FF00FF</red>");
|
|
return 2;
|
|
}
|
|
Color decodedColor = Color.decode(color);
|
|
TeamColor teamColor = new TeamColor(decodedColor.getRed(), decodedColor.getGreen(), decodedColor.getBlue(), color);
|
|
|
|
int highestId = gameManager.getMaxTeamId();
|
|
Team team = new Team(MiniMessage.miniMessage().deserialize(String.format("<color:%s>%s</color>", color, name)),
|
|
highestId + 1, player.getLocation(), player.getLocation(), player.getLocation(), teamColor);
|
|
|
|
return consumer.apply(team);
|
|
}
|
|
|
|
@Override
|
|
public String getName() {
|
|
return "createteam";
|
|
}
|
|
|
|
@Override
|
|
public List<String> getTabComplete(CommandSender commandSender, String[] args) {
|
|
return List.of();
|
|
}
|
|
|
|
@Override
|
|
public String getHelpMessage() {
|
|
return Messages.HELP.CREATE_TEAM;
|
|
}
|
|
}
|