#411 Forced commands: initial implementation

This commit is contained in:
ljacqu
2016-11-17 21:02:01 +01:00
parent 40cf79d2c1
commit 4214c6dc80
11 changed files with 351 additions and 30 deletions
@@ -0,0 +1,28 @@
package fr.xephi.authme.settings.commandconfig;
/**
* Command to be run.
*/
public class Command {
/** The command to execute. */
private String command;
/** The executor of the command. */
private Executor executor = Executor.PLAYER;
public String getCommand() {
return command;
}
public void setCommand(String command) {
this.command = command;
}
public Executor getExecutor() {
return executor;
}
public void setExecutor(Executor executor) {
this.executor = executor;
}
}
@@ -0,0 +1,40 @@
package fr.xephi.authme.settings.commandconfig;
import java.util.Collections;
import java.util.Map;
/**
* Command configuration.
*
* @see CommandManager
*/
public class CommandConfig {
private Map<String, Command> onJoin = Collections.emptyMap();
private Map<String, Command> onLogin = Collections.emptyMap();
private Map<String, Command> onRegister = Collections.emptyMap();
public Map<String, Command> getOnJoin() {
return onJoin;
}
public void setOnJoin(Map<String, Command> onJoin) {
this.onJoin = onJoin;
}
public Map<String, Command> getOnLogin() {
return onLogin;
}
public void setOnLogin(Map<String, Command> onLogin) {
this.onLogin = onLogin;
}
public Map<String, Command> getOnRegister() {
return onRegister;
}
public void setOnRegister(Map<String, Command> onRegister) {
this.onRegister = onRegister;
}
}
@@ -0,0 +1,65 @@
package fr.xephi.authme.settings.commandconfig;
import com.github.authme.configme.SettingsManager;
import com.github.authme.configme.resource.YamlFileResource;
import fr.xephi.authme.initialization.DataFolder;
import fr.xephi.authme.initialization.Reloadable;
import fr.xephi.authme.service.BukkitService;
import fr.xephi.authme.util.FileUtils;
import org.bukkit.entity.Player;
import javax.annotation.PostConstruct;
import javax.inject.Inject;
import java.io.File;
import java.util.Map;
/**
* Manages configurable commands to be run when various events occur.
*/
public class CommandManager implements Reloadable {
private CommandConfig commandConfig;
@Inject
@DataFolder
private File dataFolder;
@Inject
private BukkitService bukkitService;
CommandManager() {
}
public void runCommandsOnJoin(Player player) {
executeCommands(player, commandConfig.getOnJoin());
}
public void runCommandsOnRegister(Player player) {
executeCommands(player, commandConfig.getOnRegister());
}
private void executeCommands(Player player, Map<String, Command> commands) {
for (Command command : commands.values()) {
final String execution = command.getCommand().replace("%p", player.getName());
if (Executor.CONSOLE.equals(command.getExecutor())) {
bukkitService.dispatchConsoleCommand(execution);
} else {
bukkitService.dispatchCommand(player, execution);
}
}
}
@PostConstruct
@Override
public void reload() {
File file = new File(dataFolder, "commands.yml");
FileUtils.copyFileFromResource(file, "commands.yml");
SettingsManager settingsManager = new SettingsManager(
new YamlFileResource(file), null, CommandSettingsHolder.class);
commandConfig = settingsManager.getProperty(CommandSettingsHolder.COMMANDS);
}
}
@@ -0,0 +1,51 @@
package fr.xephi.authme.settings.commandconfig;
import com.github.authme.configme.SectionComments;
import com.github.authme.configme.SettingsHolder;
import com.github.authme.configme.beanmapper.BeanProperty;
import com.github.authme.configme.properties.Property;
import java.util.HashMap;
import java.util.Map;
/**
* Settings holder class for the commands.yml settings.
*/
public final class CommandSettingsHolder implements SettingsHolder {
public static final Property<CommandConfig> COMMANDS =
new BeanProperty<>(CommandConfig.class, "", new CommandConfig());
private CommandSettingsHolder() {
}
@SectionComments
public static Map<String, String[]> sectionComments() {
String[] comments = {
"This configuration file allows you to execute commands on various events.",
"%p in commands will be replaced with the player name.",
"For example, if you want to message a welcome message to a player who just registered:",
"onRegister:",
" welcome:",
" command: 'msg %p Welcome to the server!'",
" as: CONSOLE",
"",
"This will make the console execute the msg command to the player.",
"Each command under an event has a name you can choose freely (e.g. 'welcome' as above),",
"after which a mandatory 'command' field defines the command to run, ",
"and 'as' defines who will run the command (either PLAYER or CONSOLE). Longer example:",
"onLogin:",
" welcome:",
" command: 'msg %p Welcome back!'",
" # as: PLAYER # player is the default, you can leave this out if you want",
" broadcast:",
" command: 'broadcast %p has joined, welcome back!'",
" as: CONSOLE"
};
Map<String, String[]> commentMap = new HashMap<>();
commentMap.put("onLogin", comments);
return commentMap;
}
}
@@ -0,0 +1,14 @@
package fr.xephi.authme.settings.commandconfig;
/**
* The executor of the command.
*/
public enum Executor {
/** The player of the event. */
PLAYER,
/** The console user. */
CONSOLE
}