Skip to content

Commit

Permalink
Improved Annotation Commands
Browse files Browse the repository at this point in the history
  • Loading branch information
funkemunky committed Feb 24, 2019
1 parent 3dfd7ff commit e272db9
Show file tree
Hide file tree
Showing 23 changed files with 319 additions and 504 deletions.
653 changes: 166 additions & 487 deletions API/.idea/workspace.xml

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@ public class CommandAdapter {
private Command command;
private Player player;
private String label;
private cc.funkemunky.api.commands.ancmd.Command annotation;
private String[] args;
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package cc.funkemunky.api.commands.ancmd;

import cc.funkemunky.api.commands.FunkeArgument;
import cc.funkemunky.api.utils.Color;
import cc.funkemunky.api.utils.JsonMessage;
import cc.funkemunky.api.utils.MathUtils;
import cc.funkemunky.api.utils.MiscUtils;
import com.google.common.collect.Lists;
import lombok.Getter;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandMap;
Expand Down Expand Up @@ -83,7 +81,7 @@ public boolean onCommand(CommandSender sender, org.bukkit.command.Command cmd, S

String labelFinal = IntStream.range(0, subCommand).mapToObj(x -> " " + args[x]).collect(Collectors.joining("", label, ""));
if(command.permission().length == 0 || Arrays.stream(command.permission()).anyMatch(sender::hasPermission)) {
CommandAdapter adapter = new CommandAdapter(sender, cmd, sender instanceof Player ? (Player) sender : null, labelFinal, modArgs);
CommandAdapter adapter = new CommandAdapter(sender, cmd, sender instanceof Player ? (Player) sender : null, labelFinal, command, modArgs);
try {
entry.getKey().invoke(entry.getValue(), adapter);
} catch (Exception e) {
Expand All @@ -103,9 +101,7 @@ public ColorScheme getDefaultScheme() {
return new ColorScheme(Color.Gold + Color.Bold, Color.Gray, Color.Yellow, Color.White, Color.Dark_Gray, Color.Red, Color.White);
}

public void runHelpMessage(CommandAdapter command, CommandSender sender, @Nullable ColorScheme scheme) {
ColorScheme colorScheme = scheme != null ? scheme : getDefaultScheme();

public void runHelpMessage(CommandAdapter command, CommandSender sender, ColorScheme scheme) {
try {
int page = command.getArgs().length > 0 ? Integer.parseInt(command.getArgs()[0]) : 1;
Set<Command> argumentSet = new HashSet<>();
Expand All @@ -120,7 +116,28 @@ public void runHelpMessage(CommandAdapter command, CommandSender sender, @Nullab

List<Command> arguments = new ArrayList<>(argumentSet);

sender.sendMessage(MiscUtils.line(Color.Dark_Gray));
sender.sendMessage(scheme.getTitle() + command.getAnnotation().display() + scheme.getBody() + " Help " + scheme.getValue() + "Page (" + page + " / " + (int) MathUtils.round(arguments.size() / 6D) + ")");
sender.sendMessage("");
sender.sendMessage(Color.translate(scheme.getBody2nd()) + "<> " + scheme.getBody() + "= required. " + scheme.getBody2nd() + " [] " + scheme.getBody() + "= optional.");
sender.sendMessage("");
if (sender instanceof Player) {
StringBuilder cmdaliasesFormatted = new StringBuilder();
List<String> cmdaliases = Arrays.asList(command.getAnnotation().aliases());
if (cmdaliases.size() > 0) {
for (String aliase : cmdaliases) {
cmdaliasesFormatted.append(scheme.getValue()).append(aliase).append(scheme.getBody()).append(", ");
}
int length = cmdaliasesFormatted.length();
cmdaliasesFormatted = new StringBuilder(cmdaliasesFormatted.substring(0, length - 2));
} else {
cmdaliasesFormatted = new StringBuilder(scheme.getError() + "None");
}
JsonMessage cmdMessage = new JsonMessage();
String commandText = Color.translate((command.getAnnotation().permission().length > 0 ? scheme.getTitle() + "Permissions: " + scheme.getValue() + " " + Arrays.toString(command.getAnnotation().permission()) : scheme.getTitle() + "Permission: " + scheme.getValue() + "none")
+ "\n" + scheme.getTitle() + "Aliases: " + scheme.getValue() + cmdaliasesFormatted);
cmdMessage.addText(scheme.getBody()+ "/" + scheme.getValue() + command.getLabel().toLowerCase() + scheme.getBody() + " to " + command.getAnnotation().description()).addHoverText(commandText);
cmdMessage.sendToPlayer((Player) sender);
for (int i = (page - 1) * 6; i < Math.min(page * 6, arguments.size()); i++) {
JsonMessage message = new JsonMessage();

Expand All @@ -129,28 +146,30 @@ public void runHelpMessage(CommandAdapter command, CommandSender sender, @Nullab
List<String> aliases = Arrays.asList(argument.aliases());
if (aliases.size() > 0) {
for (String aliase : aliases) {
aliasesFormatted.append(colorScheme.getValue()).append(aliase).append(colorScheme.getBody()).append(", ");
aliasesFormatted.append(scheme.getValue()).append(aliase).append(scheme.getBody()).append(", ");
}
int length = aliasesFormatted.length();
aliasesFormatted = new StringBuilder(aliasesFormatted.substring(0, length - 2));
} else {
aliasesFormatted = new StringBuilder(colorScheme.getError() + "None");
aliasesFormatted = new StringBuilder(scheme.getError() + "None");
}


String hoverText = Color.translate((argument.permission().length > 0 ? colorScheme.getTitle() + "Permissions: " + colorScheme.getValue() + " " + Arrays.toString(argument.permission()) : colorScheme.getTitle() + "Permission: " + colorScheme.getValue() + "none")
+ "\n" + colorScheme.getTitle() + "Aliases: " + colorScheme.getValue() + aliasesFormatted);
message.addText(colorScheme.getBody()+ "/" + command.getLabel().toLowerCase() + colorScheme.getValue() + " " + argument.display() + colorScheme.getBody() + " to " + argument.description()).addHoverText(hoverText);
String hoverText = Color.translate((argument.permission().length > 0 ? scheme.getTitle() + "Permissions: " + scheme.getValue() + " " + Arrays.toString(argument.permission()) : scheme.getTitle() + "Permission: " + scheme.getValue() + "none")
+ "\n" + scheme.getTitle() + "Aliases: " + scheme.getValue() + aliasesFormatted.toString());
message.addText(scheme.getBody()+ "/" + command.getLabel().toLowerCase() + scheme.getValue() + " " + argument.display() + scheme.getBody() + " to " + argument.description()).addHoverText(hoverText);
message.sendToPlayer((Player) sender);
}
} else {
sender.sendMessage(scheme.getBody()+ "/" + scheme.getValue() + command.getLabel().toLowerCase() + scheme.getBody() + " to " + command.getAnnotation().description());
for (int i = (page - 1) * 6; i < Math.min(arguments.size(), page * 6); i++) {
Command argument = arguments.get(i);
sender.sendMessage(colorScheme.getBody()+ "/" + command.getLabel().toLowerCase() + colorScheme.getValue() + " " + argument.display() + colorScheme.getBody() + " to " + argument.description());
sender.sendMessage(scheme.getBody()+ "/" + command.getLabel().toLowerCase() + scheme.getValue() + " " + argument.display() + scheme.getBody() + " to " + argument.description());
}
}
sender.sendMessage(MiscUtils.line(Color.Dark_Gray));
} catch(NumberFormatException e) {
sender.sendMessage(colorScheme.getError() + "The page input must be a number only!");
sender.sendMessage(scheme.getError() + "The page input must be a number only!");
}
/*try {
int page = args.length > 0 ? Integer.parseInt(args[0]) : 1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public List<String> onTabComplete(CommandSender sender, Command command, String
Entry<Method, Object> entry = completers.get(cmdLabel);
try {
return (List<String>) entry.getKey().invoke(entry.getValue(),
new CommandAdapter(sender, command, sender instanceof Player ? (Player) sender : null, label, args));
new CommandAdapter(sender, command, sender instanceof Player ? (Player) sender : null, label, null, args));
} catch (IllegalArgumentException | IllegalAccessException | InvocationTargetException e) {
e.printStackTrace();
}
Expand Down
2 changes: 1 addition & 1 deletion API/Atlas/src/java/cc/funkemunky/api/updater/Updater.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

@Getter
public class Updater {
private int update = - 1, currentUpdate = 17;
private int update = - 1, currentUpdate = 18;
private String version, downloadLink;
private File pluginLocation;
private boolean importantUpdate = true;
Expand Down
2 changes: 1 addition & 1 deletion API/Atlas/src/resources/plugin.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: Atlas
main: cc.funkemunky.api.Atlas
version: 1.1.3.1
version: 1.1.3.2
author: funkemunky
commands:
atlas:
Binary file modified API/out/artifacts/Atlas_jar/Atlas.jar
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file modified API/out/production/API/cc/funkemunky/api/updater/Updater.class
Binary file not shown.
2 changes: 1 addition & 1 deletion API/out/production/API/plugin.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: Atlas
main: cc.funkemunky.api.Atlas
version: 1.1.3.1
version: 1.1.3.2
author: funkemunky
commands:
atlas:
15 changes: 15 additions & 0 deletions Example/Example.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/resources" type="java-resource" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="lombok-1.18.0" level="project" />
<orderEntry type="library" name="spigot-1.8.8-R0.1-SNAPSHOT" level="project" />
<orderEntry type="library" name="Atlas" level="project" />
</component>
</module>
Binary file added Example/out/artifacts/Example_jar/Example.jar
Binary file not shown.
Binary file not shown.
Binary file not shown.
5 changes: 5 additions & 0 deletions Example/out/production/Example/plugin.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
name: ExamplePlugin
main: cc.funkemunky.example.Example
description: An Atlas example plugin.
version: 1.0
author: funkemunky
5 changes: 5 additions & 0 deletions Example/resources/plugin.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
name: ExamplePlugin
main: cc.funkemunky.example.Example
description: An Atlas example plugin.
version: 1.0
author: funkemunky
11 changes: 11 additions & 0 deletions Example/src/cc/funkemunky/example/Example.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package cc.funkemunky.example;

import cc.funkemunky.api.Atlas;
import org.bukkit.plugin.java.JavaPlugin;

public class Example extends JavaPlugin {

public void onEnable() {
Atlas.getInstance().initializeScanner(getClass(), this, Atlas.getInstance().getCommandManager());
}
}
22 changes: 22 additions & 0 deletions Example/src/cc/funkemunky/example/commands/ExampleCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package cc.funkemunky.example.commands;

import cc.funkemunky.api.Atlas;
import cc.funkemunky.api.commands.ancmd.Command;
import cc.funkemunky.api.commands.ancmd.CommandAdapter;
import cc.funkemunky.api.utils.Init;

@Init(commands = true) //This is needed to signify to the class scanner we implemented in the main class that there are commands to register here.
public class ExampleCommand {

//Runs the help message for the main commmand "/command".
@Command(name = "command", display = "Example Command", description = "show the help page.", permission = "cmd.perm", aliases = "aCommand")
public void onCommand(CommandAdapter command) {
Atlas.getInstance().getCommandManager().runHelpMessage(command, command.getSender(), Atlas.getInstance().getCommandManager().getDefaultScheme());
}

//One of the arguments for "/command". Run as "/command arg1".
@Command(name = "command.arg1", display = "arg1", permission = "cmd.perm.arg1", description = "view a test argument in action.")
public void onCommandArg(CommandAdapter command) {
command.getSender().sendMessage("You can an argument of the example command.");
}
}
22 changes: 22 additions & 0 deletions Example/src/cc/funkemunky/example/event/CustomMoveEvent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package cc.funkemunky.example.event;

import cc.funkemunky.api.event.system.Cancellable;
import cc.funkemunky.api.event.system.Event;
import lombok.Getter;
import lombok.Setter;
import org.bukkit.Location;
import org.bukkit.entity.Player;

@Getter
@Setter
public class CustomMoveEvent extends Event implements Cancellable {
private Player player;
private Location to, from;
private boolean cancelled;

public CustomMoveEvent(Player player, Location to, Location from) {
this.player = player;
this.to = to;
this.from = from;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package cc.funkemunky.example.listeners.atlas;

import cc.funkemunky.api.event.system.EventMethod;
import cc.funkemunky.api.event.system.Listener;
import cc.funkemunky.api.utils.Init;
import cc.funkemunky.example.event.CustomMoveEvent;

@Init
public class CustomMoveListeners implements Listener {

@EventMethod
public void onEvent(CustomMoveEvent event) {
event.getPlayer().sendMessage("Custom move event.");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package cc.funkemunky.example.listeners.bukkit;

import cc.funkemunky.api.event.system.EventManager;
import cc.funkemunky.api.utils.Init;
import cc.funkemunky.example.event.CustomMoveEvent;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerMoveEvent;

@Init
public class MoveListeners implements Listener {


@EventHandler
public void onEvent(PlayerMoveEvent event) {
CustomMoveEvent move = new CustomMoveEvent(event.getPlayer(), event.getTo(), event.getFrom());
EventManager.callEvent(move);

event.setCancelled(move.isCancelled());
}
}

0 comments on commit e272db9

Please sign in to comment.