Skip to content

Commit

Permalink
Merge pull request #668 from Multiverse/ben/mv5/commandsss
Browse files Browse the repository at this point in the history
Implement config, debug, info, list and select commands
  • Loading branch information
benwoo1110 authored Nov 24, 2024
2 parents e52f3fd + 7db6523 commit 67c5668
Show file tree
Hide file tree
Showing 7 changed files with 427 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package org.mvplugins.multiverse.portals.commands;

import org.bukkit.ChatColor;
import org.mvplugins.multiverse.core.commandtools.MVCommandIssuer;
import org.mvplugins.multiverse.core.commandtools.MVCommandManager;
import org.mvplugins.multiverse.external.acf.commands.annotation.CommandAlias;
import org.mvplugins.multiverse.external.acf.commands.annotation.CommandCompletion;
import org.mvplugins.multiverse.external.acf.commands.annotation.CommandPermission;
import org.mvplugins.multiverse.external.acf.commands.annotation.Description;
import org.mvplugins.multiverse.external.acf.commands.annotation.Optional;
import org.mvplugins.multiverse.external.acf.commands.annotation.Single;
import org.mvplugins.multiverse.external.acf.commands.annotation.Subcommand;
import org.mvplugins.multiverse.external.acf.commands.annotation.Syntax;
import org.mvplugins.multiverse.external.jakarta.inject.Inject;
import org.mvplugins.multiverse.external.jetbrains.annotations.NotNull;
import org.mvplugins.multiverse.external.jvnet.hk2.annotations.Service;
import org.mvplugins.multiverse.portals.MultiversePortals;
import org.mvplugins.multiverse.portals.enums.PortalConfigProperty;

@Service
@CommandAlias("mvp")
public class ConfigCommand extends PortalsCommand {

private final MultiversePortals plugin;

@Inject
ConfigCommand(@NotNull MVCommandManager commandManager, @NotNull MultiversePortals plugin) {
super(commandManager);
this.plugin = plugin;
}

@CommandAlias("mvpconfig|mvpconf")
@Subcommand("config|conf")
@CommandPermission("multiverse.portal.config")
@CommandCompletion("@portalconfigproperty @empty")
@Syntax("<property> <value>")
@Description("Allows you to set Global MV Portals Variables.")
void onConfigCommand(
@NotNull MVCommandIssuer issuer,

@Optional
@Syntax("<property>")
@Description("The property to set.")
PortalConfigProperty property,

@Optional
@Single
@Syntax("<value>")
@Description("The value to set.")
String value
) {
if (property == null) {
String[] allProps = PortalConfigProperty.getAllValues().split(" ");
StringBuilder currentvals = new StringBuilder();
for (String prop : allProps) {
currentvals.append(ChatColor.GREEN);
currentvals.append(prop);
currentvals.append(ChatColor.WHITE);
currentvals.append(" = ");
currentvals.append(ChatColor.GOLD);
currentvals.append(this.plugin.getMainConfig().get(prop, "NOT SET"));
currentvals.append(ChatColor.WHITE);
currentvals.append(", ");
}
issuer.sendMessage(currentvals.substring(0,currentvals.length() - 2));
return;
}

if (value == null) {
issuer.sendMessage(ChatColor.AQUA + property.name() + ChatColor.WHITE + " has value "
+ ChatColor.GREEN + this.plugin.getMainConfig().get(property.name().toLowerCase()));
return;
}

if (property.equals(PortalConfigProperty.wand) || property.equals(PortalConfigProperty.portalcooldown)) {
try {
this.plugin.getMainConfig().set(property.name(), Integer.parseInt(value));
} catch (NumberFormatException e) {
issuer.sendMessage(ChatColor.RED + "Sorry, " + ChatColor.AQUA + property.name() + ChatColor.WHITE + " must be an integer!");
return;
}
} else {
try {
this.plugin.getMainConfig().set(property.name().toLowerCase(), Boolean.parseBoolean(value));
} catch (Exception e) {
issuer.sendMessage(ChatColor.RED + "Sorry, " + ChatColor.AQUA + property.name() + ChatColor.WHITE + " must be true or false!");
return;
}
}

if (this.plugin.saveMainConfig()) {
issuer.sendMessage(ChatColor.GREEN + "SUCCESS!" + ChatColor.WHITE + " Values were updated successfully!");
this.plugin.reloadConfigs(false);
} else {
issuer.sendMessage(ChatColor.RED + "FAIL!" + ChatColor.WHITE + " Check your console for details!");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package org.mvplugins.multiverse.portals.commands;

import org.bukkit.entity.Player;
import org.mvplugins.multiverse.core.commandtools.MVCommandManager;
import org.mvplugins.multiverse.external.acf.commands.annotation.CommandAlias;
import org.mvplugins.multiverse.external.acf.commands.annotation.CommandCompletion;
import org.mvplugins.multiverse.external.acf.commands.annotation.CommandPermission;
import org.mvplugins.multiverse.external.acf.commands.annotation.Description;
import org.mvplugins.multiverse.external.acf.commands.annotation.Flags;
import org.mvplugins.multiverse.external.acf.commands.annotation.Optional;
import org.mvplugins.multiverse.external.acf.commands.annotation.Single;
import org.mvplugins.multiverse.external.acf.commands.annotation.Subcommand;
import org.mvplugins.multiverse.external.acf.commands.annotation.Syntax;
import org.mvplugins.multiverse.external.jakarta.inject.Inject;
import org.mvplugins.multiverse.external.jetbrains.annotations.NotNull;
import org.mvplugins.multiverse.external.jvnet.hk2.annotations.Service;
import org.mvplugins.multiverse.portals.MultiversePortals;
import org.mvplugins.multiverse.portals.PortalPlayerSession;

@Service
@CommandAlias("mvp")
public class DebugCommand extends PortalsCommand {

private final MultiversePortals plugin;

@Inject
DebugCommand(@NotNull MVCommandManager commandManager, @NotNull MultiversePortals plugin) {
super(commandManager);
this.plugin = plugin;
}

@CommandAlias("mvpdebug|mvpd")
@Subcommand("debug")
@CommandPermission("multiverse.portal.debug")
@CommandCompletion("on|off")
@Syntax("[on|off]")
@Description("Instead of teleporting you to a place when you walk into a portal you will see the details about it. This command toggles.")
void onDebugCommand(
@Flags("resolve=issuerOnly")
Player player,

@Optional
@Single
@Syntax("[on|off]")
String toggle
) {
PortalPlayerSession ps = this.plugin.getPortalSession(player);
if (toggle != null) {
ps.setDebugMode(toggle.equalsIgnoreCase("on"));
return;
}
ps.setDebugMode(!ps.isDebugModeOn());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package org.mvplugins.multiverse.portals.commands;

import org.bukkit.entity.Player;
import org.mvplugins.multiverse.core.commandtools.MVCommandIssuer;
import org.mvplugins.multiverse.core.commandtools.MVCommandManager;
import org.mvplugins.multiverse.external.acf.commands.annotation.CommandAlias;
import org.mvplugins.multiverse.external.acf.commands.annotation.CommandCompletion;
import org.mvplugins.multiverse.external.acf.commands.annotation.CommandPermission;
import org.mvplugins.multiverse.external.acf.commands.annotation.Description;
import org.mvplugins.multiverse.external.acf.commands.annotation.Flags;
import org.mvplugins.multiverse.external.acf.commands.annotation.Subcommand;
import org.mvplugins.multiverse.external.acf.commands.annotation.Syntax;
import org.mvplugins.multiverse.external.jakarta.inject.Inject;
import org.mvplugins.multiverse.external.jetbrains.annotations.NotNull;
import org.mvplugins.multiverse.external.jvnet.hk2.annotations.Service;
import org.mvplugins.multiverse.portals.MVPortal;
import org.mvplugins.multiverse.portals.MultiversePortals;
import org.mvplugins.multiverse.portals.utils.DisplayUtils;

@Service
@CommandAlias("mvp")
public class InfoCommand extends PortalsCommand {

private final MultiversePortals plugin;
private final DisplayUtils displayUtils;

@Inject
InfoCommand(
@NotNull MVCommandManager commandManager,
@NotNull MultiversePortals plugin,
@NotNull DisplayUtils displayUtils
) {
super(commandManager);
this.plugin = plugin;
this.displayUtils = displayUtils;
}

@CommandAlias("mvpinfo|mvpi")
@Subcommand("info")
@CommandPermission("multiverse.portal.info")
@CommandCompletion("@mvportals")
@Syntax("[portal]")
@Description("Displays information about a portal.")
void onInfoCommand(
@NotNull MVCommandIssuer issuer,

@Flags("resolve=issuerAware")
@Syntax("[portal]")
@Description("The portal to show info")
MVPortal portal
) {
if(issuer.isPlayer()) {
Player p = issuer.getPlayer();
this.plugin.getPortalSession(p).showDebugInfo(portal);
} else {
displayUtils.showStaticInfo(issuer.getIssuer(), portal, "Portal Info: ");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
package org.mvplugins.multiverse.portals.commands;

import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender;
import org.mvplugins.multiverse.core.commandtools.MVCommandManager;
import org.mvplugins.multiverse.core.world.MultiverseWorld;
import org.mvplugins.multiverse.core.world.WorldManager;
import org.mvplugins.multiverse.external.acf.commands.annotation.CommandAlias;
import org.mvplugins.multiverse.external.acf.commands.annotation.CommandCompletion;
import org.mvplugins.multiverse.external.acf.commands.annotation.CommandPermission;
import org.mvplugins.multiverse.external.acf.commands.annotation.Default;
import org.mvplugins.multiverse.external.acf.commands.annotation.Description;
import org.mvplugins.multiverse.external.acf.commands.annotation.Optional;
import org.mvplugins.multiverse.external.acf.commands.annotation.Subcommand;
import org.mvplugins.multiverse.external.acf.commands.annotation.Syntax;
import org.mvplugins.multiverse.external.jakarta.inject.Inject;
import org.mvplugins.multiverse.external.jetbrains.annotations.NotNull;
import org.mvplugins.multiverse.external.jvnet.hk2.annotations.Service;
import org.mvplugins.multiverse.portals.MVPortal;
import org.mvplugins.multiverse.portals.utils.PortalManager;

import java.util.ArrayList;
import java.util.List;

@Service
@CommandAlias("mvp")
public class ListCommand extends PortalsCommand {

private static final int ITEMS_PER_PAGE = 9;

private final PortalManager portalManager;
private final WorldManager worldManager;

@Inject
ListCommand(MVCommandManager commandManager, PortalManager portalManager, WorldManager worldManager) {
super(commandManager);
this.portalManager = portalManager;
this.worldManager = worldManager;
}

@CommandAlias("mvplist|mvpl")
@Subcommand("list")
@CommandPermission("multiverse.portal.list")
@CommandCompletion("@empty @empty")
@Syntax("[filter/world] [page]")
@Description("Displays a listing of all portals that you can enter.")
void onListCommand(
@NotNull CommandSender sender,

@Optional
@Syntax("[filter/world]")
@Description("Filter by name or world")
String filterOrWorld,

@Default("1")
@Syntax("[page]")
@Description("Page to display")
int page
) {
String filter = filterOrWorld;

MultiverseWorld world = this.worldManager.getLoadedWorld(filter).getOrNull();
if (world != null) {
filter = null;
}

List<String> portals = new ArrayList<>(getPortals(sender, world, filter, page));

if(portals.isEmpty() && filter == null) {
page = (int) Math.ceil(1F*getPortals(sender, world, filter).size()/ITEMS_PER_PAGE);
portals.addAll(getPortals(sender, world, filter, page));
}

String titleString = ChatColor.AQUA + String.valueOf(getPortals(sender, world, filter).size()) + " Portals";
if (world != null) {
titleString += " in " + ChatColor.YELLOW + world.getAlias();
}
if (filter != null) {
titleString += ChatColor.GOLD + " [" + filter + "]";
}

titleString += ChatColor.GOLD + " - Page " + page + "/" + (int) Math.ceil(1F*getPortals(sender, world, filter).size()/ITEMS_PER_PAGE);
sender.sendMessage(ChatColor.AQUA + "--- " + titleString + ChatColor.AQUA + " ---");

for(String portal : portals) {
sender.sendMessage(portal);
}
}

private List<String> getPortals(CommandSender sender, MultiverseWorld world, String filter) {
List<String> portals = new ArrayList<>();
if (filter == null) {
filter = "";
}
for (MVPortal portal : (world == null) ? this.portalManager.getPortals(sender) : this.portalManager.getPortals(sender, world)) {
String destination = "";
if(portal.getDestination() != null) {
destination = portal.getDestination().toString();
String destType = portal.getDestination().getIdentifier();
if(destType.equals("w")) {
MultiverseWorld destWorld = this.worldManager.getLoadedWorld(destination).getOrNull();
if (destWorld != null) {
destination = "(World) " + ChatColor.DARK_AQUA + destination;
}
}
if (destType.equals("p")) {
// todo: I think should use instance check instead of destType prefix
// String targetWorldName = this.portalManager.getPortal(portal.getDestination().getName()).getWorld().getName();
// destination = "(Portal) " + ChatColor.DARK_AQUA + portal.getDestination().getName() + ChatColor.GRAY + " (" + targetWorldName + ")";
}
if (destType.equals("e")) {
String destinationWorld = portal.getDestination().toString().split(":")[1];
String destPart = portal.getDestination().toString().split(":")[2];
String[] locParts = destPart.split(",");
int x, y, z;
try {
x = (int) Double.parseDouble(locParts[0]);
y = (int) Double.parseDouble(locParts[1]);
z = (int) Double.parseDouble(locParts[2]);
} catch(NumberFormatException e) {
e.printStackTrace();
continue;
}
if(destType.equals("i")) {
destination = ChatColor.RED + "Invalid destination";
}
destination = "(Location) " + ChatColor.DARK_AQUA + destinationWorld + ", " + x + ", " + y + ", " + z;
}
}

if (portal.getName().toLowerCase().contains(filter.toLowerCase()) || ( portal.getDestination() != null && destination.toLowerCase().contains(filter.toLowerCase()))) {
portals.add(ChatColor.YELLOW + portal.getName() + ((portal.getDestination() != null) ? (ChatColor.AQUA + " -> " + ChatColor.GOLD + destination) : ""));
}
}
java.util.Collections.sort(portals);
return portals;
}

private List<String> getPortals(CommandSender sender, MultiverseWorld world, String filter, int page) {
List<String> portals = new ArrayList<>();
for(int i = 0; i < getPortals(sender, world, filter).size(); i++) {
if((i >= (page* ITEMS_PER_PAGE)- ITEMS_PER_PAGE && i <= (page* ITEMS_PER_PAGE)-1)) {
portals.add(getPortals(sender, world, filter).get(i));
}
}
return portals;
}
}
Loading

0 comments on commit 67c5668

Please sign in to comment.