Initial commit for Gitea

main
Etzelia 2018-09-12 10:56:08 -05:00 committed by John Olheiser
commit d8a895f7f3
11 changed files with 561 additions and 0 deletions

4
.gitignore vendored 100644
View File

@ -0,0 +1,4 @@
.idea/
*.iml
target/
dependency-reduced-pom.xml

76
pom.xml 100644
View File

@ -0,0 +1,76 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>xyz.etztech</groupId>
<artifactId>mixtape</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
<name>Mixtape</name>
<description>A Command wRapper plugin.</description>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<url>www.etztech.xyz</url>
<build>
<defaultGoal>clean package</defaultGoal>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.1.0</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<minimizeJar>true</minimizeJar>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
<repositories>
<repository>
<id>spigotmc-repo</id>
<url>https://hub.spigotmc.org/nexus/content/groups/public/</url>
</repository>
<repository>
<id>sonatype</id>
<url>https://oss.sonatype.org/content/groups/public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot-api</artifactId>
<version>1.13.1-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,28 @@
package xyz.etztech.mixtape;
public class Alias {
private String alias;
private String command;
public String getAlias() {
return alias;
}
public void setAlias(String alias) {
this.alias = alias;
}
public String getCommand() {
return command;
}
public void setCommand(String command) {
this.command = command;
}
@Override
public String toString() {
return alias + " -> " + command;
}
}

View File

@ -0,0 +1,127 @@
package xyz.etztech.mixtape;
import org.bukkit.Bukkit;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.plugin.java.JavaPlugin;
import xyz.etztech.mixtape.commands.CommandAlias;
import xyz.etztech.mixtape.commands.CommandSlashAlias;
import xyz.etztech.mixtape.commands.CommandMixtape;
import xyz.etztech.mixtape.listeners.CommandPreprocessListener;
import java.util.*;
import java.util.logging.Logger;
public final class Mixtape extends JavaPlugin {
private Logger log = Logger.getLogger( "Minecraft" );
private static Mixtape instance;
public static FileConfiguration config;
private static Map<String, Map<String, String>> chatAliases = new HashMap<>();
private static Map<String, Map<String, String>> commandAliases = new HashMap<>();
private static boolean global;
@Override
public void onEnable() {
// Plugin startup logic
instance = this;
saveDefaultConfig();
reloadConfig();
this.getCommand("mixtape").setExecutor(new CommandMixtape(this));
this.getCommand("alias").setExecutor(new CommandAlias(this));
this.getCommand("/alias").setExecutor(new CommandSlashAlias(this));
getServer().getPluginManager().registerEvents(new CommandPreprocessListener(this), this);
}
@Override
public void onDisable() {
// Plugin shutdown logic
}
@Override
public void reloadConfig() {
super.reloadConfig();
config = Bukkit.getPluginManager().getPlugin("Mixtape").getConfig();
validate();
}
public void log(String message) {
log.info( "[Mixtape]: " + message );
}
public static Mixtape getInstance() {
return instance;
}
public static void setCommandAlias(String uuid, String alias, String command) {
Map<String, String> map = new HashMap<>();
if (commandAliases.containsKey(uuid)) {
map = commandAliases.get(uuid);
}
map.put(alias, command);
commandAliases.put(uuid, map);
}
public static Map<String, String> getCommandAliases(String uuid) {
return commandAliases.containsKey(uuid) ? commandAliases.get(uuid) : new HashMap<>();
}
public static void setChatAlias(String uuid, String alias, String command) {
Map<String, String> map = new HashMap<>();
if (chatAliases.containsKey(uuid)) {
map = chatAliases.get(uuid);
}
map.put(alias, command);
chatAliases.put(uuid, map);
}
public static Map<String, String> getChatAliases(String uuid) {
return chatAliases.containsKey(uuid) ? chatAliases.get(uuid) : new HashMap<>();
}
public static boolean getGlobal() {
return global;
}
public static void setGlobal(boolean bool) {
global = bool;
}
private void validate() {
List<String> errors = new ArrayList<>();
try {
List<?> aliases = getConfig().getList("aliases");
for (int i = 0; i < aliases.size(); i++) {
try {
LinkedHashMap<String, String> alias = (LinkedHashMap<String, String>) aliases.get(i);
if (!alias.containsKey("alias")) {
errors.add("Could not load alias #" + (i+1) + "'s alias");
}
if (!alias.containsKey("command")) {
errors.add("Could not load alias #" + (i+1) + "'s command");
}
} catch (Exception ex) {
errors.add("Could not load alias #" + (i+1));
}
}
} catch (Exception ex) {
errors.add("Could not load config. Is it up-to-date?");
}
for (String message : errors) {
log(message);
}
if (errors.size() > 0) {
log("Disabling global aliases...");
}
Mixtape.setGlobal(errors.size() == 0);
}
}

View File

@ -0,0 +1,12 @@
package xyz.etztech.mixtape;
public class MixtapeUtil {
public static String stripSlash(String base) {
if (base.startsWith("/")) {
return base.substring(1);
}
return base;
}
}

View File

@ -0,0 +1,48 @@
package xyz.etztech.mixtape.commands;
import org.apache.commons.lang.StringUtils;
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import xyz.etztech.mixtape.Mixtape;
import xyz.etztech.mixtape.MixtapeUtil;
import java.util.Arrays;
public class CommandAlias implements CommandExecutor {
Mixtape plugin;
public CommandAlias(Mixtape plugin) {
this.plugin = plugin;
}
@Override
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
if (!(sender instanceof Player)) {
sender.sendMessage(ChatColor.RED + "This command is not supported in Console.");
return true;
}
Player player = (Player) sender;
if (!player.hasPermission("mixtape.alias.chat")) {
player.sendMessage(ChatColor.RED + "You do not have permission to create chat aliases.");
return true;
}
if (args.length < 2) {
player.sendMessage(ChatColor.RED + "/alias <alias> <chat>");
return true;
}
String alias = MixtapeUtil.stripSlash(args[0]);
String command = StringUtils.join(Arrays.copyOfRange(args, 1, args.length), " ");
Mixtape.setChatAlias(player.getUniqueId().toString(), alias, command);
player.sendMessage(ChatColor.GREEN + "Chat alias created.");
return true;
}
}

View File

@ -0,0 +1,37 @@
package xyz.etztech.mixtape.commands;
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.configuration.file.FileConfiguration;
import xyz.etztech.mixtape.Mixtape;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
public class CommandMixtape implements CommandExecutor {
Mixtape plugin;
public CommandMixtape(Mixtape plugin) {
this.plugin = plugin;
}
@Override
public boolean onCommand(CommandSender sender, Command command, String base, String[] args) {
if (args.length == 0) {
sender.sendMessage(ChatColor.GOLD + "===== Mixtape =====");
sender.sendMessage(ChatColor.YELLOW + "/mixtape reload");
} else if (sender.hasPermission("alias.admin")) {
if ("reload".equalsIgnoreCase(args[0])) {
plugin.reloadConfig();
sender.sendMessage(ChatColor.GREEN + "Mixtape Reloaded.");
}
}
return true;
}
}

View File

@ -0,0 +1,48 @@
package xyz.etztech.mixtape.commands;
import org.apache.commons.lang.StringUtils;
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import xyz.etztech.mixtape.Mixtape;
import xyz.etztech.mixtape.MixtapeUtil;
import java.util.Arrays;
public class CommandSlashAlias implements CommandExecutor {
Mixtape plugin;
public CommandSlashAlias(Mixtape plugin) {
this.plugin = plugin;
}
@Override
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
if (!(sender instanceof Player)) {
sender.sendMessage(ChatColor.RED + "This command is not supported in Console.");
return true;
}
Player player = (Player) sender;
if (!player.hasPermission("mixtape.alias.command")) {
player.sendMessage(ChatColor.RED + "You do not have permission to create command aliases.");
return true;
}
if (args.length < 2) {
player.sendMessage(ChatColor.RED + "//alias <alias> <command>");
return true;
}
String alias = MixtapeUtil.stripSlash(args[0]);
String command = StringUtils.join(Arrays.copyOfRange(args, 1, args.length), " ");
Mixtape.setCommandAlias(player.getUniqueId().toString(), alias, command);
player.sendMessage(ChatColor.GREEN + "Command alias created.");
return true;
}
}

View File

@ -0,0 +1,140 @@
package xyz.etztech.mixtape.listeners;
import org.apache.commons.lang.StringUtils;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerCommandPreprocessEvent;
import org.bukkit.event.server.ServerCommandEvent;
import xyz.etztech.mixtape.Mixtape;
import xyz.etztech.mixtape.MixtapeUtil;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class CommandPreprocessListener implements Listener {
private final String ERROR = "Mixtape/Error";
private final Pattern PATTERN = Pattern.compile("<([^>]+)>");
Mixtape plugin;
public CommandPreprocessListener(Mixtape plugin) {
this.plugin = plugin;
}
@EventHandler(priority= EventPriority.HIGH, ignoreCancelled=true)
public void onServerCommandPreprocess(ServerCommandEvent event) {
String command = event.getCommand();
if (command.startsWith("/")) {
command = command.substring(1);
}
String base = command.split(" ")[0];
String[] args = command.trim().length() == base.trim().length() ? new String[0] : command.substring(base.length()+1).split(" ");
event.setCancelled(handleEvent(Bukkit.getConsoleSender(), base, args));
}
@EventHandler(priority= EventPriority.HIGH, ignoreCancelled=true)
public void onPlayerCommandPreprocess(PlayerCommandPreprocessEvent event) {
String command = event.getMessage().substring(1); // Strip the slash
String base = command.split(" ")[0];
String[] args = command.trim().length() == base.trim().length() ? new String[0] : command.substring(base.length()+1).split(" ");
event.setCancelled(handleEvent(event.getPlayer(), base, args));
}
private boolean handleEvent(CommandSender sender, String base, String[] args) {
// Global aliases
if (Mixtape.getGlobal()) {
List<?> configs = plugin.getConfig().getList("aliases");
for (int i = 0; i < configs.size(); i++) {
LinkedHashMap<String, String> config = (LinkedHashMap<String, String>) configs.get(i);
String configBase = MixtapeUtil.stripSlash(config.get("alias"));
if (configBase.equalsIgnoreCase(base)) {
String configCommand = MixtapeUtil.stripSlash(config.get("command"));
String resolved = resolve(configCommand, args);
if (!ERROR.equalsIgnoreCase(resolved)) {
Bukkit.dispatchCommand(sender, resolved);
} else {
sender.sendMessage(ChatColor.RED + usage(base, configCommand));
}
return true;
}
}
}
// Personal aliases
if (sender instanceof Player) {
Player player = (Player) sender;
// Command aliases
Map<String, String> commands = Mixtape.getCommandAliases(player.getUniqueId().toString());
for (String alias : commands.keySet()) {
if (alias.equalsIgnoreCase(base)) {
String resolved = resolve(commands.get(alias), args);
if (!ERROR.equalsIgnoreCase(resolved)) {
Bukkit.dispatchCommand(player, resolved);
} else {
player.sendMessage(ChatColor.RED + usage(base, commands.get(alias)));
}
return true;
}
}
// Chat aliases
Map<String, String> chats = Mixtape.getChatAliases(player.getUniqueId().toString());
for (String alias : chats.keySet()) {
if (alias.equalsIgnoreCase(base)) {
String resolved = resolve(chats.get(alias), args);
if (!ERROR.equalsIgnoreCase(resolved)) {
resolved = resolved.startsWith("/") ? "." + resolved : resolved;
player.chat(resolved);
} else {
player.sendMessage(ChatColor.RED + usage(base, chats.get(alias)));
}
return true;
}
}
}
return false;
}
private String usage(String base, String template) {
StringBuilder usage = new StringBuilder("/" + base);
Matcher matcher = PATTERN.matcher(template);
while (matcher.find()) {
usage.append(" ").append(matcher.group(0));
}
return usage.toString();
}
private String resolve(String template, String... parts) {
Matcher matcher = PATTERN.matcher(template);
int idx = 0;
try {
while (matcher.find()) {
template = template.replaceFirst(matcher.group(0), parts[idx]);
idx++;
}
} catch (Exception ex) {
return ERROR;
}
return template.trim() + " " + StringUtils.join(Arrays.copyOfRange(parts, idx, parts.length), " ");
}
}

View File

@ -0,0 +1,6 @@
# This plugin is for making aliases easier.
# Rather than run /lp user Etzelia parent addtemp donator 1m accumulate
# You could run /donator Etzelia 1m
aliases:
- alias: /donator
command: /lp user <username> parent addtemp donator <time> accumulate

View File

@ -0,0 +1,35 @@
name: Mixtape
version: ${project.version}
main: xyz.etztech.mixtape.Mixtape
authors: [Etzelia]
description: A Command wRapper plugin.
website: www.etztech.xyz
commands:
mixtape:
description: Base Mixtape command
/alias:
description: Command Alias command
alias:
description: Chat Alias command
permissions:
mixtape.admin:
description: Ability to use Mixtape command to reload
default: op
mixtape.alias.command:
description: Ability to create personal alias commands
default: op
mixtape.alias.chat:
description: Ability to create personal alias chat
default: op
mixtape.alias.*:
description: Wildcard permission for Mixtape aliases
default: op
children:
mixtape.alias.command: true
mixtape.alias.chat: true
mixtape.*:
description: Wildcard permission for Mixtape
default: op
children:
mixtape.admin: true
mixtape.alias.*: true