Skip to content

Commit

Permalink
Team commands (#26)
Browse files Browse the repository at this point in the history
* prepare for team commands

* register command

* implement

* implement team placeholders

* more checks

* support for both uuid & name in commands

* just to be safe

* remove owner feature

* single value in modify

* use uuid directly
  • Loading branch information
HSGamer authored Mar 10, 2024
1 parent b132bdc commit 5eaee22
Show file tree
Hide file tree
Showing 9 changed files with 645 additions and 1 deletion.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ repositories {

dependencies {
compileOnly 'io.papermc.paper:paper-api:1.20.4-R0.1-SNAPSHOT'
compileOnly 'io.github.miniplaceholders:miniplaceholders-api:2.2.3'
}

processResources {
Expand Down
67 changes: 67 additions & 0 deletions src/main/java/net/simplyvanilla/simplynicks/SimplyNicks.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,24 @@
package net.simplyvanilla.simplynicks;

import io.github.miniplaceholders.api.Expansion;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import net.kyori.adventure.text.minimessage.MiniMessage;
import net.kyori.adventure.text.minimessage.tag.Tag;
import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder;
import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver;
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
import net.simplyvanilla.simplynicks.commands.NickCommandExecutor;
import net.simplyvanilla.simplynicks.commands.RealnameCommandExecutor;
import net.simplyvanilla.simplynicks.commands.TeamCommandExecutor;
import net.simplyvanilla.simplynicks.database.Cache;
import net.simplyvanilla.simplynicks.database.MySQL;
import net.simplyvanilla.simplynicks.database.TeamCache;
import net.simplyvanilla.simplynicks.database.TeamMySQL;
import net.simplyvanilla.simplynicks.event.PlayerEvents;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;

import java.util.HashMap;
Expand All @@ -21,6 +30,8 @@ public class SimplyNicks extends JavaPlugin {
private MySQL database;
private Cache cache;
private List<String> colors;
private TeamMySQL teamDatabase;
private TeamCache teamCache;

@Override
public void onEnable() {
Expand All @@ -45,15 +56,63 @@ public void onEnable() {
getServer().getPluginManager().disablePlugin(this);
return;
}

this.teamDatabase = new TeamMySQL(this);
this.teamCache = new TeamCache(this);

try {
this.teamDatabase.connect();
} catch (Exception e) {
getLogger().warning("Could not connect to database! Please check your config.yml and try again.");
getServer().getPluginManager().disablePlugin(this);
return;
}
try {
this.teamCache.initCache();
} catch (Exception e) {
getLogger().warning("Could not load cache! Please check your config.yml and try again.");
getServer().getPluginManager().disablePlugin(this);
return;
}

this.getServer().getPluginManager().registerEvents(new PlayerEvents(this), this);
this.getCommand("nick").setExecutor(new NickCommandExecutor(this));
this.getCommand("realname").setExecutor(new RealnameCommandExecutor(this));
this.getCommand("team").setExecutor(new TeamCommandExecutor(this));
colors = this.getConfig().getStringList("colors");

if (Bukkit.getPluginManager().isPluginEnabled("MiniPlaceholders")) {
Expansion.builder("simplynicks")
.filter(Player.class)
.audiencePlaceholder("team_name", (audience, ctx, queue) -> {
Player player = (Player) audience;
TeamMySQL.PlayerTeam playerTeam = teamCache.getTeam(player.getUniqueId());
Component teamName = playerTeam == null ? Component.text("") : Component.text(playerTeam.getName());
return Tag.selfClosingInserting(teamName);
})
.audiencePlaceholder("team_color", (audience, ctx, queue) -> {
Player player = (Player) audience;
TeamMySQL.PlayerTeam playerTeam = teamCache.getTeam(player.getUniqueId());
Component teamColor = playerTeam == null ? Component.text("") : Component.text(playerTeam.getColor());
return Tag.selfClosingInserting(teamColor);
})
.audiencePlaceholder("team_prefix", (audience, ctx, queue) -> {
Player player = (Player) audience;
TeamMySQL.PlayerTeam playerTeam = teamCache.getTeam(player.getUniqueId());
Component teamPrefix = playerTeam == null
? Component.text("")
: Component.text(playerTeam.getName()).color(NamedTextColor.NAMES.valueOr(playerTeam.getName(), NamedTextColor.WHITE));
return Tag.selfClosingInserting(teamPrefix);
})
.build()
.register();
}
}

@Override
public void onDisable() {
database.close();
teamDatabase.close();
}

public MySQL getDatabase() {
Expand All @@ -68,6 +127,14 @@ public List<String> getColors() {
return colors;
}

public TeamMySQL getTeamDatabase() {
return teamDatabase;
}

public TeamCache getTeamCache() {
return teamCache;
}

public String getMessage(String path) {
return Optional.ofNullable(this.getConfig().getString(path)).orElse(path);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
package net.simplyvanilla.simplynicks.commands;

import net.simplyvanilla.simplynicks.SimplyNicks;
import net.simplyvanilla.simplynicks.database.TeamMySQL;
import org.bukkit.OfflinePlayer;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.jetbrains.annotations.NotNull;

import java.util.Locale;
import java.util.Map;
import java.util.UUID;

public class TeamCommandExecutor implements CommandExecutor {
private final SimplyNicks plugin;

public TeamCommandExecutor(SimplyNicks plugin) {
this.plugin = plugin;
}

@Override
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) {
if (!sender.hasPermission("simplynicks.team")) {
return false;
}

if (args.length == 0) {
return false;
}

String subCommand = args[0];
switch (subCommand.toLowerCase(Locale.ROOT)) {
case "create": {
if (args.length < 2) {
return false;
}
String name = args[1];
return handleCreate(sender, name);
}
case "modify": {
if (args.length < 3) {
return false;
}
String name = args[1];
String modifyType = args[2];
String value = args.length > 3 ? args[3] : "";
return handleModify(sender, name, modifyType, value);
}
case "join": {
if (args.length < 3) {
return false;
}
String member = args[1];
String name = args[2];
return handleJoin(sender, member, name);
}
case "leave": {
if (args.length < 3) {
return false;
}
String leaver = args[1];
String name = args[2];
return handleLeave(sender, leaver, name);
}
case "delete":
if (args.length < 2) {
return false;
}
String name = args[1];
return handleDelete(sender, name);
}

return false;
}

private UUID getPlayerId(String input) {
try {
return UUID.fromString(input);
} catch (IllegalArgumentException e) {
OfflinePlayer player = this.plugin.getServer().getOfflinePlayer(input);
if (!player.hasPlayedBefore() || player.getName() == null) {
return null;
}
return player.getUniqueId();
}
}

// /team delete <name>
private boolean handleDelete(CommandSender sender, String name) {
if (!this.plugin.getTeamCache().isTeamExists(name)) {
this.plugin.sendConfigMessage(sender, "messages.error.teamNotFound");
return false;
}

if (!this.plugin.getTeamDatabase().deleteTeam(name)) {
return false;
}

this.plugin.sendConfigMessage(sender, "messages.teamDeletedMessage");
return true;
}

// /team leave <leaver> <owner>
private boolean handleLeave(CommandSender sender, String leaver, String name) {
UUID leaverId = getPlayerId(leaver);
if (leaverId == null) {
this.plugin.sendConfigMessage(sender, "messages.error.playerCannotFoundErrorMessage");
return false;
}

TeamMySQL.PlayerTeam playerTeam = this.plugin.getTeamCache().getTeam(leaverId);
if (playerTeam == null) {
this.plugin.sendConfigMessage(sender, "messages.error.teamNotInTeam");
return false;
}

if (!playerTeam.getName().equals(name)) {
this.plugin.sendConfigMessage(sender, "messages.error.teamNotInTeam");
return false;
}

if (!this.plugin.getTeamDatabase().leaveTeam(leaverId, name)) {
return false;
}

this.plugin.sendConfigMessage(sender, "messages.teamLeftMessage");
return true;
}

// /team join <member> <name>
private boolean handleJoin(CommandSender sender, String member, String name) {
UUID memberId = getPlayerId(member);
if (memberId == null) {
this.plugin.sendConfigMessage(sender, "messages.error.playerCannotFoundErrorMessage");
return false;
}

if (this.plugin.getTeamCache().getTeam(memberId) != null) {
this.plugin.sendConfigMessage(sender, "messages.error.teamAlreadyInTeam");
return false;
}

if (!this.plugin.getTeamCache().isTeamExists(name)) {
this.plugin.sendConfigMessage(sender, "messages.error.teamNotFound");
return false;
}

if (!this.plugin.getTeamDatabase().joinTeam(memberId, name)) {
return false;
}

this.plugin.sendConfigMessage(sender, "messages.teamJoinedMessage");
return true;
}

// /team modify <name> <modifyType> [value]
// modifyType: name, color
private boolean handleModify(CommandSender sender, String name, String modifyType, String value) {
if (!this.plugin.getTeamCache().isTeamExists(name)) {
this.plugin.sendConfigMessage(sender, "messages.error.teamNotFound");
return false;
}

if (!this.plugin.getTeamDatabase().modifyTeam(name, modifyType, value)) {
return false;
}

this.plugin.sendConfigMessage(sender, "messages.teamModifiedMessage");
return true;
}

// /team create <name>
private boolean handleCreate(CommandSender sender, String teamName) {
if (this.plugin.getTeamCache().isTeamExists(teamName)) {
this.plugin.sendConfigMessage(sender, "messages.error.teamAlreadyExists");
return false;
}

if (!this.plugin.getTeamDatabase().createTeam(teamName)) {
return false;
}

this.plugin.sendConfigMessage(sender, "messages.teamCreatedMessage", Map.of("team", teamName));
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,5 @@ public void close() {
this.plugin.getLogger().log(Level.INFO, "MySQL database is closing...");
ex.printStackTrace();
}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package net.simplyvanilla.simplynicks.database;

import net.simplyvanilla.simplynicks.SimplyNicks;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.function.Consumer;

public class TeamCache {
private final SimplyNicks plugin;
private final Map<UUID, TeamMySQL.PlayerTeam> teams = new HashMap<>();

public TeamCache(SimplyNicks plugin) {
this.plugin = plugin;
}

public void initCache() {
for (var entry : this.plugin.getTeamDatabase().getAllTeams().entrySet()) {
addTeam(entry.getKey(), entry.getValue());
}
}

public boolean isTeamExists(String name) {
return this.teams.values().stream().anyMatch(team -> team.getName().equals(name));
}

public void addTeam(UUID key, TeamMySQL.PlayerTeam value) {
this.teams.put(key, value);
}

public TeamMySQL.PlayerTeam getTeam(UUID uuid) {
return this.teams.get(uuid);
}

public void removeTeam(UUID uuid) {
this.teams.remove(uuid);
}

public void removeTeamByName(String name) {
this.teams.values().removeIf(team -> team.getName().equals(name));
}

public void updateTeam(String name, Consumer<TeamMySQL.PlayerTeam> consumer) {
this.teams.values().stream().filter(team -> team.getName().equals(name)).forEach(consumer);
}
}
Loading

0 comments on commit 5eaee22

Please sign in to comment.