-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 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
Showing
9 changed files
with
645 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
187 changes: 187 additions & 0 deletions
187
src/main/java/net/simplyvanilla/simplynicks/commands/TeamCommandExecutor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
src/main/java/net/simplyvanilla/simplynicks/database/TeamCache.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
Oops, something went wrong.