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
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.modnmetl</groupId>
<artifactId>virtualrealty</artifactId>
<version>2.2.4</version>
<version>2.3.0</version>
<packaging>jar</packaging>

<description>A plot creation and management plugin for Minecraft</description>
Expand Down
26 changes: 22 additions & 4 deletions src/main/java/com/modnmetl/virtualrealty/VirtualRealty.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.modnmetl.virtualrealty;

import com.modnmetl.virtualrealty.commands.CommandManager;
import com.modnmetl.virtualrealty.commands.CommandRegistry;
import com.modnmetl.virtualrealty.commands.SubCommand;
import com.modnmetl.virtualrealty.commands.plot.PlotCommand;
import com.modnmetl.virtualrealty.commands.vrplot.VirtualRealtyCommand;
Expand Down Expand Up @@ -61,6 +62,7 @@ public final class VirtualRealty extends JavaPlugin {
public SizesConfiguration sizesConfiguration;
public MessagesConfiguration messagesConfiguration;
public PermissionsConfiguration permissionsConfiguration;
public CommandsConfiguration commandsConfiguration;
private static ClassLoader classLoader;
public static final String PREFIX = "§a§lVR §8§l» §7";
public static List<BukkitTask> tasks = new ArrayList<>();
Expand All @@ -83,6 +85,7 @@ public final class VirtualRealty extends JavaPlugin {
private final File pluginConfigurationFile = new File(this.getDataFolder(), "config.yml");
private final File sizesConfigurationFile = new File(this.getDataFolder(), "sizes.yml");
private final File permissionsConfigurationFile = new File(this.getDataFolder(), "permissions.yml");
private final File commandsConfigurationFile = new File(this.getDataFolder(), "commands.yml");
private final File languagesDirectory = new File(this.getDataFolder(), "messages");
private final File databaseFolder = new File(this.getDataFolder().getAbsolutePath(), File.separator + "data" + File.separator);
private final File databaseFile = new File(databaseFolder, "data.db");
Expand Down Expand Up @@ -159,6 +162,7 @@ public void onEnable() {
dynmapManager.registerDynmap();
}
registerCommands();
configureCommands();
registerListeners();
if (Bukkit.getPluginManager().isPluginEnabled("PlaceholderAPI")) {
new VirtualPlaceholders(this).register();
Expand All @@ -173,8 +177,7 @@ public void onDisable() {
Method method = Class.forName("com.modnmetl.virtualrealty.premiumloader.PremiumLoader", true, getLoader()).getMethod("onDisable");
method.setAccessible(true);
method.invoke(premium);
} catch (Exception ignored) {
}
} catch (Exception ignored) {}
DraftListener.DRAFT_MAP.forEach((player, gridStructureEntryEntry) -> {
player.getInventory().remove(gridStructureEntryEntry.getValue().getValue().getItemStack());
player.getInventory().addItem(gridStructureEntryEntry.getValue().getKey().getItemStack());
Expand Down Expand Up @@ -208,6 +211,14 @@ public void configureMessages() {
configFactory.loadMessagesConfiguration(messagesConfigurationFile);
}

public void configureCommands() {
ConfigurationFactory configFactory = new ConfigurationFactory();
commandsConfiguration = configFactory.loadCommandsConfiguration(commandsConfigurationFile);
commandsConfiguration.refreshHelpMessages();
commandsConfiguration.assignAliases();
CommandRegistry.setupPlaceholders();
}

public void reloadConfigs() {
try {
ConfigurationFactory configFactory = new ConfigurationFactory();
Expand Down Expand Up @@ -236,15 +247,18 @@ private void registerCommands() {
}

public void registerSubCommands(Class<?> mainCommandClass, String... names) {
SubCommand.registerSubCommands(names, mainCommandClass);
if (names.length > 0)
SubCommand.registerSubCommands(names, mainCommandClass);
for (JarFile jarFile : jarFiles) {
for (Enumeration<JarEntry> entry = jarFile.entries(); entry.hasMoreElements();) {
JarEntry jarEntry = entry.nextElement();
String name = jarEntry.getName().replace("/", ".");
if (name.endsWith(".class") && name.startsWith(mainCommandClass.getPackage().getName() + ".subcommand.")) {
try {
Class<?> clazz = Class.forName(name.replaceAll("[.]class", ""), true, getClassLoader());
SubCommand.registerSubCommands(new String[]{ clazz.getSimpleName().toLowerCase().replaceAll("subcommand", "") }, mainCommandClass);
String subcommand = clazz.getSimpleName().toLowerCase().replaceAll("subcommand", "");
if (subcommand.isEmpty()) continue;
SubCommand.registerSubCommands(new String[]{subcommand}, mainCommandClass);
} catch (ClassNotFoundException ignored) {}
}
}
Expand Down Expand Up @@ -352,6 +366,10 @@ public static PermissionsConfiguration getPermissions() {
return getInstance().permissionsConfiguration;
}

public static CommandsConfiguration getCommands() {
return getInstance().commandsConfiguration;
}

public static DynmapManager getDynmapManager() {
return getInstance().dynmapManager;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.modnmetl.virtualrealty.commands;

import com.modnmetl.virtualrealty.VirtualRealty;
import com.modnmetl.virtualrealty.commands.plot.PlotCommand;
import com.modnmetl.virtualrealty.commands.vrplot.VirtualRealtyCommand;
import com.modnmetl.virtualrealty.enums.PlotSize;
import com.modnmetl.virtualrealty.enums.commands.CommandType;
import com.modnmetl.virtualrealty.managers.PlotManager;
import com.modnmetl.virtualrealty.objects.Plot;
import com.modnmetl.virtualrealty.utils.EnumUtils;
Expand All @@ -21,11 +23,23 @@ public class CommandManager implements TabCompleter {

public static final HashMap<Class<?>, SortedSet<String>> SUBCOMMANDS = new HashMap<>();


public static void addSubCommand(String subCommand, Class<?> mainCommandClass) {
if (subCommand == null || subCommand.isEmpty()) return;
if (!SUBCOMMANDS.containsKey(mainCommandClass)) {
SUBCOMMANDS.put(mainCommandClass, new TreeSet<>());
}
SUBCOMMANDS.get(mainCommandClass).add(subCommand);
if (subCommand.equalsIgnoreCase("Panel") && VirtualRealty.getPremium() == null) return;
String className = mainCommandClass.getPackage().getName() + ".subcommand." + subCommand.replaceFirst(Character.toString(subCommand.toCharArray()[0]), Character.toString(Character.toUpperCase(subCommand.toCharArray()[0]))) + "SubCommand";
Class<?> subcommandClass;
try {
subcommandClass = VirtualRealty.getLoader().loadClass(className);
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
String name = mainCommandClass.getPackage().getName().replaceAll("com.modnmetl.virtualrealty.commands.", "").toUpperCase();
CommandRegistry.addSubCommandToRegistry(subcommandClass, CommandType.valueOf(name));
}

@SneakyThrows
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package com.modnmetl.virtualrealty.commands;

import com.modnmetl.virtualrealty.VirtualRealty;
import com.modnmetl.virtualrealty.enums.commands.CommandType;
import lombok.SneakyThrows;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Optional;

public class CommandRegistry {

public static final HashMap<String, String> PLOT_PLACEHOLDERS = new HashMap<>();
public static final HashMap<String, String> VRPLOT_PLACEHOLDERS = new HashMap<>();
public static final List<SubCommand> PLOT_SUB_COMMAND_LIST = new ArrayList<>();
public static final List<SubCommand> VRPLOT_SUB_COMMAND_LIST = new ArrayList<>();

public static void setupPlaceholders() {
for (SubCommand subCommand : PLOT_SUB_COMMAND_LIST) {
PLOT_PLACEHOLDERS.put("%" + subCommand.getSubCommandClassName() + "_command%", subCommand.getSubCommandName());
}
for (SubCommand subCommand : VRPLOT_SUB_COMMAND_LIST) {
VRPLOT_PLACEHOLDERS.put("%" + subCommand.getSubCommandClassName() + "_command%", subCommand.getSubCommandName());
}
if (VirtualRealty.getPremium() == null) {
PLOT_PLACEHOLDERS.put("%panel_command%", "panel");
} else {
getSubCommand("panel", CommandType.PLOT).ifPresent(subCommand -> {
PLOT_PLACEHOLDERS.put("%panel_command%", subCommand.getSubCommandName());
});
}
}

@SneakyThrows
public static void addSubCommandToRegistry(Class<?> clazz, CommandType commandType) {
if (clazz.getSimpleName().equalsIgnoreCase("SubCommand")) return;
Object newInstance = clazz.newInstance();
switch (commandType) {
case PLOT: {
PLOT_SUB_COMMAND_LIST.add((SubCommand) newInstance);
break;
}
case VRPLOT: {
VRPLOT_SUB_COMMAND_LIST.add((SubCommand) newInstance);
break;
}
}
}

public static Optional<SubCommand> getSubCommand(String name, CommandType commandType) {
switch (commandType) {
case PLOT: {
return PLOT_SUB_COMMAND_LIST.stream().filter(subCommand ->
subCommand.getSubCommandClassName().equalsIgnoreCase(name)
||
(subCommand.getAlias() != null && subCommand.getAlias().equalsIgnoreCase(name))
).findAny();
}
case VRPLOT: {
return VRPLOT_SUB_COMMAND_LIST.stream().filter(subCommand ->
subCommand.getSubCommandClassName().equalsIgnoreCase(name)
||
(subCommand.getAlias() != null && subCommand.getAlias().equalsIgnoreCase(name))
).findAny();
}
}
return Optional.empty();
}

public static List<SubCommand> getSubCommandList(CommandType commandType) {
switch (commandType) {
case VRPLOT:
return VRPLOT_SUB_COMMAND_LIST;
case PLOT:
return PLOT_SUB_COMMAND_LIST;
}
return new ArrayList<>();
}

@SneakyThrows
public static String getSubCommandName(String placeholder, CommandType commandType) {
Optional<SubCommand> command = getSubCommand(placeholder.replaceAll("%", "").replaceAll("_command", ""), commandType);
return command.map(SubCommand::getSubCommandName).orElse(null);
}

@SneakyThrows
public static List<String> getSubCommandNames(SubCommand subCommand, CommandType commandType) {
List<String> strings = new ArrayList<>();
getSubCommand(subCommand.getSubCommandClassName(), commandType).ifPresent(subCommand1 -> {
strings.add(subCommand1.getSubCommandClassName());
strings.add(subCommand1.getSubCommandName());
});
return strings;
}

}
55 changes: 48 additions & 7 deletions src/main/java/com/modnmetl/virtualrealty/commands/SubCommand.java
Original file line number Diff line number Diff line change
@@ -1,40 +1,59 @@
package com.modnmetl.virtualrealty.commands;

import com.modnmetl.virtualrealty.VirtualRealty;
import com.modnmetl.virtualrealty.enums.commands.CommandType;
import com.modnmetl.virtualrealty.exceptions.FailedCommandException;
import com.modnmetl.virtualrealty.exceptions.InsufficientPermissionsException;
import lombok.Getter;
import lombok.Setter;
import lombok.SneakyThrows;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

import java.lang.reflect.Method;
import java.util.LinkedList;
import java.util.Optional;

import static com.modnmetl.virtualrealty.commands.vrplot.VirtualRealtyCommand.COMMAND_PERMISSION;

public abstract class SubCommand {

private final String[] args;
private final CommandSender commandSender;
private final LinkedList<String> helpList;
@Getter
public LinkedList<String> HELP_LIST;
private final boolean bypass;
@Getter
@Setter
private String alias;
private String commandName;

public SubCommand() {
this.args = null;
this.commandSender = null;
this.HELP_LIST = null;
this.bypass = false;
this.alias = null;
}

@SneakyThrows
public SubCommand(CommandSender sender, Command command, String label, String[] args, boolean bypass, LinkedList<String> helpList) throws FailedCommandException {
public SubCommand(CommandSender sender, Command command, String label, String[] args, boolean bypass, LinkedList<String> HELP_LIST) throws FailedCommandException {
this.args = args;
this.helpList = helpList;
this.HELP_LIST = HELP_LIST;
this.commandSender = sender;
this.bypass = bypass;
this.alias = null;
exec(sender, command, label, args);
}

@SneakyThrows
public SubCommand(CommandSender sender, Command command, String label, String[] args, LinkedList<String> helpList) throws FailedCommandException {
public SubCommand(CommandSender sender, Command command, String label, String[] args, LinkedList<String> HELP_LIST) throws FailedCommandException {
this.args = args;
this.helpList = helpList;
this.HELP_LIST = HELP_LIST;
this.commandSender = sender;
this.bypass = false;
this.alias = null;
exec(sender, command, label, args);
}

Expand All @@ -47,6 +66,28 @@ public void assertPlayer() throws FailedCommandException {
}
}

public String getSubCommandClassName() {
return this.getClass().getSimpleName().replaceAll("SubCommand", "").toLowerCase();
}

public String getSubCommandName() {
if (this.commandName != null) return this.commandName;
Optional<SubCommand> subCommand = CommandRegistry.getSubCommand(this.getSubCommandClassName(), getCommandType());
if (subCommand.isPresent()) {
if (subCommand.get().getAlias() != null) {
this.commandName = subCommand.get().getAlias();
return this.commandName;
}
}
this.commandName = this.getSubCommandClassName();
return this.commandName;
}

public CommandType getCommandType() {
String name = this.getClass().getPackage().getName().replaceAll("com.modnmetl.virtualrealty.commands.", "").replaceAll(".subcommand", "").toUpperCase();
return CommandType.valueOf(name);
}

public String getDefaultPermission() {
return COMMAND_PERMISSION.getName() + "." + args[0].toLowerCase();
}
Expand Down Expand Up @@ -78,8 +119,8 @@ public boolean isBypass() {
}

public void printHelp() throws FailedCommandException {
for (String s : helpList) {
commandSender.sendMessage(s);
for (String s : HELP_LIST) {
commandSender.sendMessage(s.replaceAll("%command%", getSubCommandName()));
}
throw new FailedCommandException();
}
Expand Down
Loading