Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor commands #46

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
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
4 changes: 2 additions & 2 deletions Core/src/main/java/se/hyperver/hyperverse/Hyperverse.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
import org.bukkit.plugin.java.JavaPlugin;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import se.hyperver.hyperverse.commands.HyperCommandManager;
import se.hyperver.hyperverse.commands.HyperCommand;
import se.hyperver.hyperverse.configuration.FileHyperConfiguration;
import se.hyperver.hyperverse.configuration.HyperConfiguration;
import se.hyperver.hyperverse.configuration.Messages;
Expand Down Expand Up @@ -195,7 +195,7 @@ public static HyperverseAPI getApi() {

// Create the command manager instance
try {
injector.getInstance(HyperCommandManager.class);
injector.getInstance(HyperCommand.class);
} catch (final Exception e) {
e.printStackTrace();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.PluginDescriptionFile;
import org.jetbrains.annotations.NotNull;
import se.hyperver.hyperverse.Hyperverse;
import se.hyperver.hyperverse.configuration.FileHyperConfiguration;
import se.hyperver.hyperverse.configuration.Message;
Expand Down Expand Up @@ -61,22 +60,20 @@
import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@CommandAlias("hyperverse|hv|worlds|world")
@CommandPermission("hyperverse.worlds")
@SuppressWarnings("unused")
public class HyperCommandManager extends BaseCommand {
public class HyperCommand extends BaseCommand {

private final BukkitCommandManager bukkitCommandManager;
private final WorldManager worldManager;
private final FileHyperConfiguration fileHyperConfiguration;
private final HyperWorldFactory hyperWorldFactory;
private final GlobalWorldFlagContainer globalFlagContainer;
private final TaskChainFactory taskChainFactory;

@Inject public HyperCommandManager(final Hyperverse hyperverse, final WorldManager worldManager,
@Inject public HyperCommand(final Hyperverse hyperverse, final WorldManager worldManager,
final HyperWorldFactory hyperWorldFactory, final GlobalWorldFlagContainer globalFlagContainer,
final TaskChainFactory taskChainFactory, final FileHyperConfiguration hyperConfiguration) {
this.worldManager = Objects.requireNonNull(worldManager);
Expand All @@ -86,276 +83,9 @@ public class HyperCommandManager extends BaseCommand {
this.fileHyperConfiguration = Objects.requireNonNull(hyperConfiguration);

// Create the command manager
bukkitCommandManager = new PaperCommandManager(hyperverse);
bukkitCommandManager.usePerIssuerLocale(true, true);
bukkitCommandManager.getLocales().addMessages(Locale.ENGLISH, Messages.getMessages());
bukkitCommandManager.setDefaultFormatter(new BukkitMessageFormatter(ChatColor.GRAY) {
@Override public String format(@NotNull final String message) {
return ChatColor.translateAlternateColorCodes('&', Messages.messagePrefix.toString())
+ super.format(message);
}
});
bukkitCommandManager.setFormat(MessageType.ERROR, new BukkitMessageFormatter(ChatColor.RED, ChatColor.GOLD, ChatColor.WHITE) {
@Override public String format(final String message) {
return ChatColor.translateAlternateColorCodes('&', Messages.messagePrefix.toString())
+ ChatColor.RED + super.format(message);
}
});
bukkitCommandManager.setFormat(MessageType.SYNTAX, new BukkitMessageFormatter(ChatColor.GRAY, ChatColor.GOLD, ChatColor.WHITE) {
@Override public String format(final String message) {
return ChatColor.translateAlternateColorCodes('&', Messages.messagePrefix.toString())
+ ChatColor.GRAY + super.format(message);
}
});
bukkitCommandManager.setFormat(MessageType.HELP, new BukkitMessageFormatter(ChatColor.GRAY, ChatColor.GOLD, ChatColor.WHITE) {
@Override public String format(final String message) {
return ChatColor.translateAlternateColorCodes('&', Messages.messagePrefix.toString())
+ ChatColor.GRAY + super.format(message);
}
});
bukkitCommandManager.getCommandCompletions().registerAsyncCompletion("hyperworlds",
context -> worldManager.getWorlds().stream().filter(hyperWorld -> {
final String stateSel = context.getConfig("state", "").toLowerCase();
final String playerSel = context.getConfig("players", "").toLowerCase();
if (!hyperWorld.isLoaded()) {
return false;
}
assert hyperWorld.getBukkitWorld() != null;
boolean ret = true;
switch (stateSel) {
case "loaded":
ret = hyperWorld.isLoaded(); break;
case "not_loaded":
ret = !hyperWorld.isLoaded(); break;
default:
break;
}
switch (playerSel) {
case "no_players":
ret = ret && hyperWorld.getBukkitWorld().getPlayers().isEmpty(); break;
case "has_players":
ret = ret && !hyperWorld.getBukkitWorld().getPlayers().isEmpty(); break;
default:
break;
}
return ret;

}).map(HyperWorld::getConfiguration).map(WorldConfiguration::getName)
.filter(worldName -> {
final String selection = context.getConfig("player", "").toLowerCase();
final boolean inWorld = context.getIssuer().isPlayer() &&
worldName.equalsIgnoreCase(context.getPlayer().getWorld().getName());
switch (selection) {
case "not_in":
return !inWorld;
case "in":
return inWorld;
default:
return true;
}
}).collect(Collectors.toList()));
bukkitCommandManager.getCommandCompletions()
.registerAsyncCompletion("import-candidates", context -> {
final File baseDirectory = Bukkit.getWorldContainer();
try {
return Files.list(baseDirectory.toPath()).filter(path -> {
final File file = path.toFile();
return file.isDirectory() && new File(file, "level.dat").isFile()
&& this.worldManager.getWorld(file.getName()) == null;
}).map(path -> path.toFile().getName()).sorted(Comparator.naturalOrder())
.collect(Collectors.toList());
} catch (IOException ex) {
return Collections.emptyList();
}
});
bukkitCommandManager.getCommandCompletions().registerAsyncCompletion("worldtypes", context -> {
if (context.getInput().contains(" ")) {
return Collections.emptyList();
}
return Arrays.stream(WorldType.values()).map(WorldType::name).map(String::toLowerCase)
.collect(Collectors.toList());
});
bukkitCommandManager.getCommandCompletions().registerAsyncCompletion("worldfeatures", context -> {
if (context.getInput().contains(" ")) {
return Collections.emptyList();
}
return Arrays.stream(WorldFeatures.values()).map(WorldFeatures::name).map(String::toLowerCase)
.collect(Collectors.toList());
});
bukkitCommandManager.getCommandCompletions().registerCompletion("null", context ->
Collections.emptyList());
bukkitCommandManager.getCommandCompletions()
.registerAsyncCompletion("generators", context -> {
final String arg = context.getInput();
if (arg.contains(":")) {
return Collections.emptyList();
}
final List<String> generators = new ArrayList<>();
generators.add("vanilla");
for (final Plugin plugin : Bukkit.getPluginManager().getPlugins()) {
generators.add(plugin.getName().toLowerCase());
}
return generators;
});
bukkitCommandManager.getCommandCompletions().registerAsyncCompletion("flags", context ->
globalFlagContainer.getFlagMap().values().stream().map(WorldFlag::getName).collect(
Collectors.toList()));
bukkitCommandManager.getCommandCompletions().registerAsyncCompletion("gamerules", context ->
Arrays.stream(GameRule.values()).map(GameRule::getName).collect(Collectors.toList()));
bukkitCommandManager.getCommandCompletions().registerCompletion("flag", context -> {
final WorldFlag<?, ?> flag = context.getContextValue(WorldFlag.class);
if (flag != null) {
return flag.getTabCompletions();
}
return Collections.emptyList();
});
bukkitCommandManager.getCommandCompletions().registerAsyncCompletion("gamerule", context -> {
final GameRule<?> gameRule = context.getContextValue(GameRule.class);
if (gameRule != null) {
if (gameRule.getType() == Boolean.class) {
return Arrays.asList("true", "false");
}
}
return Collections.emptyList();
});
bukkitCommandManager.getCommandCompletions().registerCompletion("profile_groups", context -> {
Stream<String> groups = worldManager.getWorlds().stream().map(world -> world.getFlag(ProfileGroupFlag.class)).filter(s -> !s.isEmpty());
String requirePerms = context.getConfig("has_perms", "false");
if (requirePerms.equalsIgnoreCase("true")) {
groups = groups.filter(profile -> context.getSender().hasPermission("hyperverse.teleportgroup." + profile));
}
return groups.collect(Collectors.toList());
});
bukkitCommandManager.getCommandCompletions().registerAsyncCompletion("structures", context ->
Arrays.asList("yes", "true", "generate_structures", "structures", "no", "false", "no_structures"));
bukkitCommandManager.getCommandContexts().registerContext(WorldStructureSetting.class, context -> {
switch (context.popFirstArg().toLowerCase()) {
case "yes":
case "true":
case "generate_structures":
case "structures":
return WorldStructureSetting.GENERATE_STRUCTURES;
case "no":
case "false":
case "no_structures":
return WorldStructureSetting.NO_STRUCTURES;
default:
throw new InvalidCommandArgument(Messages.messageInvalidStructureSetting.withoutColorCodes());
}
});
bukkitCommandManager.getCommandCompletions()
.registerCompletion("vararg_players", context -> {
String[] input = context.getInput().split(" ");
final int toPop;
try {
toPop = Integer.parseInt(context.getConfig("pop"));
} catch (final NumberFormatException ex) {
ex.printStackTrace();
return Collections.emptyList();
}
if (toPop > input.length) {
throw new IllegalArgumentException(
"Config to pop is greater than input length!");
}
input = Arrays.copyOfRange(input, toPop, input.length - 1);
for (int index = 0; index < input.length; index++) {
input[index] = input[index].toLowerCase();
}
final List<String> players = new ArrayList<>(Arrays.asList(input));
if (context.getPlayer() != null && !context.getConfig("self", "false")
.equalsIgnoreCase("true")) {
players.remove(context.getPlayer().getName());
}
return Bukkit.getOnlinePlayers().stream().map(Player::getName)
.filter(player -> !players.contains(player.toLowerCase()))
.sorted(Comparator.naturalOrder()).collect(Collectors.toList());

});
bukkitCommandManager.getCommandCompletions().registerCompletion("vararg_player_world", context -> {
String[] input = context.getInput().split(" ");
final int toPop;
try {
toPop = Integer.parseInt(context.getConfig("pop"));
} catch (final NumberFormatException ex) {
ex.printStackTrace();
return Collections.emptyList();
}
if (toPop > input.length) {
throw new IllegalArgumentException(
"Config to pop is greater than input length!");
}
final String inWorld = context.getConfig("in_world", "false");
final boolean checkInWorld = !inWorld.equalsIgnoreCase("false");
input = Arrays.copyOfRange(input, toPop, input.length - 1);
for (int index = 0; index < input.length; index++) {
input[index] = input[index].toLowerCase();
}
final List<String> players = new ArrayList<>(Arrays.asList(input));
if (context.getPlayer() != null && !context.getConfig("self", "false")
.equalsIgnoreCase("true")) {
players.remove(context.getPlayer().getName());
}
Stream<? extends Player> stream = Bukkit.getOnlinePlayers().stream();
if (checkInWorld) {
final HyperWorld world = context.getContextValue(HyperWorld.class);
if (world == null) {
return Collections.emptyList();
}
stream = stream.filter(player -> player.getWorld() != world.getBukkitWorld());
}
return stream.map(Player::getName)
.filter(player -> !players.contains(player.toLowerCase()))
.sorted(Comparator.naturalOrder()).collect(Collectors.toList());
});
/*bukkitCommandManager.getCommandContexts().registerContext(Player[].class, context -> {
final List<String> args = context.getArgs();
final Player[] arr = new Player[args.size()];
for (int index = 0; index < args.size(); index++) {
final Player player = Bukkit.getPlayer(args.get(index));
if (player == null) {
throw new InvalidCommandArgument(MessageUtil.format(Messages.messageNoPlayerFound.toString(), "%name%", args.get(index)));
}
arr[index] = player;
}
args.clear();
return arr;
});*/
bukkitCommandManager.getCommandContexts().registerContext(WorldType.class, context -> {
final String arg = context.popFirstArg();
return WorldType.fromString(arg).orElseThrow(() ->
new InvalidCommandArgument(Messages.messageInvalidWorldType.withoutColorCodes()));
});
bukkitCommandManager.getCommandContexts().registerContext(WorldFeatures.class, context -> {
final String arg = context.popFirstArg();
return WorldFeatures.fromName(arg).orElseThrow(() ->
new InvalidCommandArgument(Messages.messageInvalidWorldFeatures.withoutColorCodes()));
});
bukkitCommandManager.getCommandContexts().registerIssuerAwareContext(HyperWorld.class, context -> {
HyperWorld hyperWorld = worldManager.getWorld(context.getFirstArg());
if (hyperWorld == null) {
if (context.getPlayer() != null) {
hyperWorld = worldManager.getWorld(context.getPlayer().getWorld());
}
if (hyperWorld == null) {
throw new InvalidCommandArgument(Messages.messageNoSuchWorld.withoutColorCodes());
}
} else {
context.popFirstArg(); // remove the world argument as it's a valid world
}
return hyperWorld;
});
bukkitCommandManager.getCommandContexts().registerContext(GameRule.class, context ->
java.util.Optional.ofNullable(GameRule.getByName(context.popFirstArg()))
.orElseThrow(() -> new InvalidCommandArgument(Messages.messageInvalidGameRule.withoutColorCodes())));
bukkitCommandManager.getCommandContexts().registerContext(WorldFlag.class, context -> {
final WorldFlag<?, ?> flag = this.globalFlagContainer.getFlagFromString(context.popFirstArg().toLowerCase());
if (flag == null) {
throw new InvalidCommandArgument(Messages.messageFlagUnknown.withoutColorCodes());
}
return flag;
});
//noinspection deprecation
bukkitCommandManager.enableUnstableAPI("help");
BukkitCommandManager bukkitCommandManager = new PaperCommandManager(hyperverse);
// Register our completions etc
HyperCommandUtil.setupCommandManager(bukkitCommandManager, worldManager, globalFlagContainer);
bukkitCommandManager.registerCommand(this);
}

Expand Down
Loading