Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
987 changes: 500 additions & 487 deletions src/main/java/com/modnmetl/virtualrealty/commands/CommandManager.java

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -19,93 +19,132 @@
import com.modnmetl.virtualrealty.model.permission.ManagementPermission;
import com.modnmetl.virtualrealty.model.plot.Plot;
import com.modnmetl.virtualrealty.model.plot.PlotMember;
import com.modnmetl.virtualrealty.util.PlayerLookupUtil;

public class AddSubCommand extends SubCommand {

public static LinkedList<String> HELP = new LinkedList<>();

static {
HELP.add(" ");
HELP.add(" §8§l«§8§m §8[§aVirtualRealty§8]§m §8§l»");
HELP.add(" §a/plot %command% §8<§7plot§8> §8<§7player§8>");
}

public AddSubCommand() {
}

public AddSubCommand(CommandSender sender, Command command, String label, String[] args)
throws FailedCommandException {
super(sender, command, label, args, false, HELP);
}

@Override
public void exec(CommandSender sender, Command command, String label, String[] args) throws FailedCommandException {
assertPlayer();
Player player = ((Player) sender);
if (args.length < 3) {
printHelp(CommandType.PLOT);
return;
}
int plotID;
try {
plotID = Integer.parseInt(args[1]);
} catch (IllegalArgumentException e) {
ChatMessage.of(VirtualRealty.getMessages().useNaturalNumbersOnly).sendWithPrefix(player);
return;
}

Player playerToAdd = Bukkit.getPlayer(args[2]);
OfflinePlayer offlinePlayer = Bukkit.getOfflinePlayer(args[2]);

if (playerToAdd != null && offlinePlayer.getFirstPlayed() == 0) {
ChatMessage.of(VirtualRealty.getMessages().playerNotFoundWithUsername).sendWithPrefix(player);
return;
}

UUID toAdd = offlinePlayer.getUniqueId();
String name = offlinePlayer.getName();
if (playerToAdd != null) {
toAdd = playerToAdd.getUniqueId();
name = playerToAdd.getName();
}
Plot plot = PlotManager.getInstance().getPlot(plotID);
if (plot == null) {
ChatMessage.of(VirtualRealty.getMessages().noPlotFound).sendWithPrefix(sender);
return;
}
if (!plot.hasMembershipAccess(player.getUniqueId())) {
ChatMessage.of(VirtualRealty.getMessages().notYourPlot).sendWithPrefix(sender);
return;
}
PlotMember plotMember = plot.getMember(player.getUniqueId());
if (plotMember != null) {
if (!plotMember.hasManagementPermission(ManagementPermission.ADD_MEMBER)) {
ChatMessage.of(VirtualRealty.getMessages().noAccess).sendWithPrefix(sender);
return;
}
} else {
if (!plot.getOwnedBy().equals(player.getUniqueId())) {
ChatMessage.of(VirtualRealty.getMessages().noAccess).sendWithPrefix(sender);
return;
}
}
if (plot.getOwnedUntilDate().isBefore(LocalDateTime.now())) {
ChatMessage.of(VirtualRealty.getMessages().ownershipExpired).sendWithPrefix(sender);
return;
}
if (plot.getOwnedBy().equals(toAdd)) {
boolean equals = plot.getOwnedBy().equals(player.getUniqueId());
ChatMessage.of(
equals ? VirtualRealty.getMessages().cantAddYourself : VirtualRealty.getMessages().alreadyInMembers)
.sendWithPrefix(sender);
return;
}
if (plot.getMember(toAdd) != null) {
ChatMessage.of(VirtualRealty.getMessages().alreadyInMembers).sendWithPrefix(sender);
return;
}
plot.addMember(toAdd);
ChatMessage.of(VirtualRealty.getMessages().playerAdd.replaceAll("%player%", name)).sendWithPrefix(sender);
}
public static LinkedList<String> HELP = new LinkedList<>();

static {
HELP.add(" ");
HELP.add(" §8§l«§8§m §8[§aVirtualRealty§8]§m §8§l»");
HELP.add(" §a/plot %command% §8<§7plot§8> §8<§7player§8>");
}

public AddSubCommand() {}

public AddSubCommand(CommandSender sender, Command command, String label, String[] args)
throws FailedCommandException {
super(sender, command, label, args, false, HELP);
}

@Override
public void exec(CommandSender sender, Command command, String label, String[] args) throws FailedCommandException {
assertPlayer();
Player player = (Player) sender;

if (args.length < 3) {
printHelp(CommandType.PLOT);
return;
}

// Parse plot ID safely
int plotID;
try {
plotID = Integer.parseInt(args[1]);
} catch (Exception ex) {
ChatMessage.of(VirtualRealty.getMessages().useNaturalNumbersOnly).sendWithPrefix(player);
return;
}

String targetArg = args[2];

// Try online first
Player online = Bukkit.getPlayer(targetArg);
UUID targetUUID;
String displayName;

if (online != null) {
// Cleanest case — real online player
targetUUID = online.getUniqueId();
displayName = online.getName();
} else {
// Offline or Floodgate, or unknown/uncached
OfflinePlayer offline = Bukkit.getOfflinePlayer(targetArg);
targetUUID = offline.getUniqueId();

if (targetUUID == null) {
ChatMessage.of(VirtualRealty.getMessages().playerNotFoundWithUsername).sendWithPrefix(player);
return;
}

// Resolve a SAFE display name
displayName = PlayerLookupUtil.getBestName(offline);

if (displayName == null || displayName.isEmpty()) {
// Fallback: use what the player typed, or short UUID
if (!targetArg.isEmpty()) {
displayName = targetArg;
} else {
displayName = targetUUID.toString().substring(0, 8);
}
}
}

// Fetch plot
Plot plot = PlotManager.getInstance().getPlot(plotID);
if (plot == null) {
ChatMessage.of(VirtualRealty.getMessages().noPlotFound).sendWithPrefix(player);
return;
}

// Access check
if (!plot.hasMembershipAccess(player.getUniqueId())) {
ChatMessage.of(VirtualRealty.getMessages().notYourPlot).sendWithPrefix(player);
return;
}

PlotMember memberSender = plot.getMember(player.getUniqueId());
if (memberSender != null) {
if (!memberSender.hasManagementPermission(ManagementPermission.ADD_MEMBER)) {
ChatMessage.of(VirtualRealty.getMessages().noAccess).sendWithPrefix(player);
return;
}
} else {
if (!plot.getOwnedBy().equals(player.getUniqueId())) {
ChatMessage.of(VirtualRealty.getMessages().noAccess).sendWithPrefix(player);
return;
}
}

// Ownership time check
if (plot.getOwnedUntilDate().isBefore(LocalDateTime.now())) {
ChatMessage.of(VirtualRealty.getMessages().ownershipExpired).sendWithPrefix(player);
return;
}

// Cannot add yourself if you're owner
if (plot.getOwnedBy().equals(targetUUID)) {
boolean equals = plot.getOwnedBy().equals(player.getUniqueId());
ChatMessage msg = ChatMessage.of(
equals ? VirtualRealty.getMessages().cantAddYourself : VirtualRealty.getMessages().alreadyInMembers
);
msg.sendWithPrefix(player);
return;
}

// Already a member?
if (plot.getMember(targetUUID) != null) {
ChatMessage.of(VirtualRealty.getMessages().alreadyInMembers).sendWithPrefix(player);
return;
}

// Add the member
plot.addMember(targetUUID);

// Send safe success message
ChatMessage.of(
VirtualRealty.getMessages().playerAdd.replace("%player%", displayName)
).sendWithPrefix(player);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.modnmetl.virtualrealty.manager.PlotManager;
import com.modnmetl.virtualrealty.model.plot.Plot;
import com.modnmetl.virtualrealty.model.other.ChatMessage;
import com.modnmetl.virtualrealty.util.PlayerLookupUtil;
import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
import org.bukkit.command.Command;
Expand All @@ -27,33 +28,52 @@ public InfoSubCommand(CommandSender sender, Command command, String label, Strin
@Override
public void exec(CommandSender sender, Command command, String label, String[] args) throws FailedCommandException {
assertPlayer();
Player player = ((Player) sender);
Player player = (Player) sender;

Plot plot = PlotManager.getInstance().getPlot(player.getLocation());
if (plot == null) {
sender.sendMessage(VirtualRealty.PREFIX + VirtualRealty.getMessages().notStandingOnPlot);
return;
}

printInfo(sender, plot);
}

private void printInfo(CommandSender sender, Plot plot) {
LocalDateTime localDateTime = plot.getOwnedUntilDate();
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("dd-MM-yyyy");

ChatMessage.of(" ").send(sender);
ChatMessage.of(" §8§l«§8§m §8[§aVirtualRealty§8]§m §8§l»").send(sender);
ChatMessage.of(" §7Plot ID §8§l‣ §f" + plot.getID()).send(sender);
ChatMessage.of(" §7Owned By §8§l‣ §a" + (plot.getOwnedBy() != null ? (Bukkit.getOfflinePlayer(plot.getOwnedBy()).isOnline() ? "§a" : "§c") + Bukkit.getOfflinePlayer(plot.getOwnedBy()).getName() : "§cAvailable")).send(sender);
if (plot.getMembers().size() != 0) {

// OWNER display (Floodgate/Offline-safe)
String ownerDisplay;
if (plot.getOwnedBy() == null) {
ownerDisplay = "§cAvailable";
} else {
OfflinePlayer owner = Bukkit.getOfflinePlayer(plot.getOwnedBy());
boolean online = owner.isOnline();
String name = PlayerLookupUtil.getBestName(owner);
ownerDisplay = (online ? "§a" : "§c") + name;
}
ChatMessage.of(" §7Owned By §8§l‣ " + ownerDisplay).send(sender);

// MEMBERS section
if (!plot.getMembers().isEmpty()) {
ChatMessage.of(" §7Members §8§l↴").send(sender);
for (OfflinePlayer offlinePlayer : plot.getPlayerMembers()) {
ChatMessage.of(" §8§l⁍ §" + (offlinePlayer.isOnline() ? "a" : "c") + offlinePlayer.getName()).send(sender);

for (OfflinePlayer member : plot.getPlayerMembers()) {
boolean online = member.isOnline();
String name = PlayerLookupUtil.getBestName(member);
ChatMessage.of(" §8§l⁍ §" + (online ? "a" : "c") + name).send(sender);
}
}

ChatMessage.of(" §7Owned Until §8§l‣ §f" + dateTimeFormatter.format(localDateTime)).send(sender);
ChatMessage.of(" §7Size §8§l‣ §f" + plot.getPlotSize()).send(sender);
ChatMessage.of(" §7Length §8§l‣ §f" + plot.getLength()).send(sender);
ChatMessage.of(" §7Height §8§l‣ §f" + plot.getHeight()).send(sender);
ChatMessage.of(" §7Width §8§l‣ §f" + plot.getWidth()).send(sender);
}

}
Loading