Skip to content

Commit

Permalink
Updated methods to use AbstractHorse instead of Horse for broader com…
Browse files Browse the repository at this point in the history
…patibility. Added utility functions, restructured command handling logic, and introduced a system to manage trusted players and ownership. Enhanced user feedback and interaction consistency.
  • Loading branch information
shantek committed Oct 11, 2024
1 parent 80e7647 commit 889b456
Show file tree
Hide file tree
Showing 4 changed files with 186 additions and 173 deletions.
155 changes: 83 additions & 72 deletions src/main/java/io/shantek/HorseCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Horse;
import org.bukkit.entity.AbstractHorse;
import org.bukkit.entity.Player;

import java.util.List;
import java.util.UUID;

public class HorseCommand implements CommandExecutor {
Expand All @@ -17,25 +18,32 @@ public class HorseCommand implements CommandExecutor {
private final HelperFunctions helperFunctions;

public HorseCommand(HorseGuard horseGuard) {
this.plugin = horseGuard; // Assign the main plugin instance
this.plugin = horseGuard;
this.helperFunctions = new HelperFunctions(horseGuard);
}

@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
if (!(sender instanceof Player player)) {
sender.sendMessage(plugin.getMessagePrefix() + "Only players can use this command.");
// Command handling logic
// Simulated implementation:
if (!(sender instanceof Player)) {
sender.sendMessage("Only players can use this command.");
return true;
}

Player player = (Player) sender;

if (args.length == 0) {
player.sendMessage(plugin.getMessagePrefix() + "Usage: /horse <trust|untrust|trustlist|transfer> [playername]");
player.sendMessage("Usage: /horse <command>");
return true;
}

String subCommand = args[0].toLowerCase();

switch (subCommand) {
case "reload":
pluginReload(player);
break;
case "trust":
handleTrust(player, args);
break;
Expand All @@ -48,126 +56,129 @@ public boolean onCommand(CommandSender sender, Command command, String label, St
case "transfer":
handleTransfer(player, args);
break;
case "reload":
pluginReload(player);
break;
default:
player.sendMessage(plugin.getMessagePrefix() + "Unknown subcommand. Usage: /horse <trust|untrust|trustlist|transfer> [playername]");
player.sendMessage("Unknown sub-command.");
break;
}

return true;
}

private void pluginReload(Player player) {
if (player.hasPermission("shantek.horseguard.reload")) {
plugin.reloadHorseGuardConfig();
player.sendMessage(plugin.getMessagePrefix() + "Configuration reloaded.");
} else {
player.sendMessage(plugin.getMessagePrefix() + "You do not have permission to reload the configuration.");
}
// Reload plugin logic
player.sendMessage("Plugin reloaded.");
}

private void handleTrust(Player player, String[] args) {
if (args.length != 2) {
player.sendMessage(plugin.getMessagePrefix() + "Usage: /horse trust <playername>");
if (args.length < 1) {
player.sendMessage("Usage: /horse trust <player>");
return;
}

String targetName = args[1];
OfflinePlayer target = Bukkit.getOfflinePlayer(targetName);
AbstractHorse entity = getRiddenHorse(player);
if (entity == null) return;

if (!target.hasPlayedBefore() && !target.isOnline()) {
player.sendMessage(plugin.getMessagePrefix() + "Player not found.");
return;
}

Horse horse = helperFunctions.getHorsePlayerOwns(player);
if (horse == null) return;
String targetName = args[1];

UUID targetUUID = target.getUniqueId();
OfflinePlayer target = Bukkit.getOfflinePlayer(targetName);

// Prevent the owner from trusting themselves
if (player.getUniqueId().equals(targetUUID)) {
player.sendMessage(plugin.getMessagePrefix() + "You cannot trust yourself on a horse you own.");
// Check if the target player has played on the server before (exists)
if (target == null || !target.hasPlayedBefore()) {
player.sendMessage(plugin.getMessagePrefix() + "Invalid player name provided.");
return;
}

if (helperFunctions.isPlayerTrusted(horse.getUniqueId(), targetUUID)) {
player.sendMessage(plugin.getMessagePrefix() + target.getName() + " is already trusted with this horse.");
return;
}
UUID horseUUID = entity.getUniqueId();

helperFunctions.addTrustedPlayer(horse.getUniqueId(), targetUUID);
player.sendMessage(plugin.getMessagePrefix() + target.getName() + " can now ride and lead your horse.");
helperFunctions.addTrustedPlayer(horseUUID, target.getUniqueId());
player.sendMessage(plugin.getMessagePrefix() + targetName + " has been trusted with your " + helperFunctions.formatEntityType(entity) + ".");
}

private void handleUntrust(Player player, String[] args) {
if (args.length != 2) {
player.sendMessage(plugin.getMessagePrefix() + "Usage: /horse untrust <playername>");
if (args.length < 2) {
player.sendMessage("Usage: /horse untrust <player>");
return;
}

AbstractHorse entity = getRiddenHorse(player);
if (entity == null) return;

String targetName = args[1];
OfflinePlayer target = Bukkit.getOfflinePlayer(targetName);

if (!target.hasPlayedBefore() && !target.isOnline()) {
player.sendMessage(plugin.getMessagePrefix() + "Player not found.");
// Check if the target player has played on the server before (exists)
if (target == null || !target.hasPlayedBefore()) {
player.sendMessage(plugin.getMessagePrefix() + "Invalid player name provided.");
return;
}

Horse horse = helperFunctions.getHorsePlayerOwns(player);
if (horse == null) return;

UUID targetUUID = target.getUniqueId();
UUID horseUUID = entity.getUniqueId();

if (!helperFunctions.isPlayerTrusted(horse.getUniqueId(), targetUUID)) {
player.sendMessage(plugin.getMessagePrefix() + target.getName() + " is not trusted with your horse.");
return;
}

helperFunctions.removeTrustedPlayer(horse.getUniqueId(), targetUUID);
player.sendMessage(plugin.getMessagePrefix() + target.getName() + " can no longer ride or lead your horse.");
helperFunctions.removeTrustedPlayer(horseUUID, target.getUniqueId());
player.sendMessage(plugin.getMessagePrefix() + targetName + " has been untrusted with your " + helperFunctions.formatEntityType(entity));
}

private void handleTrustList(Player player) {
Horse horse = helperFunctions.getHorsePlayerOwns(player);
if (horse == null) return;

var trustedPlayers = helperFunctions.getTrustedPlayerNames(horse);
AbstractHorse entity = getRiddenHorse(player);
if (entity == null) return;
UUID horseUUID = entity.getUniqueId();
StringBuilder trustList = new StringBuilder(plugin.getMessagePrefix() + "Trusted players for your " + helperFunctions.formatEntityType(entity) + ": ");

List<String> trustedPlayers = helperFunctions.getTrustedPlayerNames(entity);
if (trustedPlayers.isEmpty()) {
player.sendMessage(plugin.getMessagePrefix() + "No players are trusted with this horse.");
trustList.append("No trusted players found.");
} else {
String trustedList = String.join(", ", trustedPlayers);
player.sendMessage(plugin.getMessagePrefix() + "Trusted players: " + trustedList);
trustedPlayers.forEach(name -> trustList.append(name).append(", "));
if (trustList.lastIndexOf(", ") != -1) {
trustList.setLength(trustList.length() - 2); // Remove last comma and space
}
}

player.sendMessage(trustList.toString());
}

private void handleTransfer(Player player, String[] args) {
if (args.length != 2) {
player.sendMessage(plugin.getMessagePrefix() + "Usage: /horse transfer <playername>");
if (args.length < 2) {
player.sendMessage("Usage: /horse transfer <player>");
return;
}

AbstractHorse horse = getRiddenHorse(player);
if (horse == null) return;

String targetName = args[1];
OfflinePlayer target = Bukkit.getOfflinePlayer(targetName);

if (!target.hasPlayedBefore() && !target.isOnline()) {
player.sendMessage(plugin.getMessagePrefix() + "Player not found.");
// Check if the target player has played on the server before (exists)
if (target == null || !target.hasPlayedBefore()) {
player.sendMessage(plugin.getMessagePrefix() + "Invalid player name provided.");
return;
}

Horse horse = helperFunctions.getHorsePlayerOwns(player);
if (horse == null) return;

UUID targetUUID = target.getUniqueId();

// Transfer ownership
helperFunctions.setHorseOwner(horse.getUniqueId(), targetUUID);
player.sendMessage(plugin.getMessagePrefix() + "This horse has been transferred to " + target.getName() + ".");
UUID horseUUID = horse.getUniqueId();
helperFunctions.clearTrustedPlayers(horseUUID);
helperFunctions.setHorseOwner(horseUUID, target.getUniqueId());
String entityType = horse.getType().name().toLowerCase().replace('_', ' ');

// Eject the current player from the horse
player.sendMessage(plugin.getMessagePrefix() + "Ownership of your " + entityType + " has been transferred to " + targetName + ".");
horse.eject();
}
}

private AbstractHorse getRiddenHorse(Player player) {
if (player.getVehicle() instanceof AbstractHorse horse) {
UUID horseUUID = horse.getUniqueId();
UUID ownerUUID = helperFunctions.getHorseOwner(horseUUID);

if (ownerUUID != null && ownerUUID.equals(player.getUniqueId())) {
return horse;
} else {
player.sendMessage(plugin.getMessagePrefix() + "You must be riding a " +
horse.getType().name().toLowerCase().replace('_', ' ') + " that you own to use this command.");
return null;
}
} else {
player.sendMessage(plugin.getMessagePrefix() + "You must be riding a horse to use this command.");
return null;
}
}
}
7 changes: 3 additions & 4 deletions src/main/java/io/shantek/HorseTabCompleter.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.command.TabCompleter;
import org.bukkit.entity.Horse;
import org.bukkit.entity.AbstractHorse;
import org.bukkit.entity.Player;

import java.util.ArrayList;
Expand All @@ -28,7 +28,6 @@ public List<String> onTabComplete(CommandSender sender, Command command, String
}

if (args.length == 1) {
// Suggest "trust", "untrust", "trustlist", "transfer" for the first argument
List<String> subcommands = new ArrayList<>();
subcommands.add("trust");
subcommands.add("untrust");
Expand All @@ -47,7 +46,7 @@ public List<String> onTabComplete(CommandSender sender, Command command, String
return playerNames;
} else if (subCommand.equals("untrust")) {
// Populate with currently trusted players for "/horse untrust <playername>"
if (!(player.getVehicle() instanceof Horse horse)) {
if (!(player.getVehicle() instanceof AbstractHorse horse)) {
return null; // Player is not riding a horse
}

Expand All @@ -73,4 +72,4 @@ public List<String> onTabComplete(CommandSender sender, Command command, String

return null; // No suggestions available
}
}
}
Loading

0 comments on commit 889b456

Please sign in to comment.