Skip to content

Commit

Permalink
Merge pull request #38 from Andre601/feature/use-generic-commandhandler
Browse files Browse the repository at this point in the history
v3.3.0: Use a generic Command handler
  • Loading branch information
Andre601 authored Mar 12, 2021
2 parents 6650d9a + 7d5fa78 commit d81e830
Show file tree
Hide file tree
Showing 14 changed files with 308 additions and 177 deletions.
9 changes: 9 additions & 0 deletions bungeecord/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@
<artifactId>adventure-platform-bungeecord</artifactId>
<version>4.0.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>net.kyori</groupId>
<artifactId>adventure-api</artifactId>
<version>4.7.0</version>
</dependency>
<dependency>
<groupId>org.spongepowered</groupId>
<artifactId>configurate-yaml</artifactId>
Expand Down Expand Up @@ -107,6 +112,10 @@
<pattern>org.bstats</pattern>
<shadedPattern>com.andre601.oneversionremake.bungeecord.dependencies.bstats</shadedPattern>
</relocation>
<relocation>
<pattern>net.kyori</pattern>
<shadedPattern>com.andre601.oneversionremake.bungeecord.dependencies.adventure</shadedPattern>
</relocation>
</relocations>
<filters>
<filter>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.andre601.oneversionremake.bungeecord.listener.BungeePingListener;
import com.andre601.oneversionremake.bungeecord.logging.BungeeLogger;
import com.andre601.oneversionremake.core.OneVersionRemake;
import com.andre601.oneversionremake.core.commands.CommandHandler;
import com.andre601.oneversionremake.core.enums.ProtocolVersion;
import com.andre601.oneversionremake.core.enums.ProxyPlatform;
import com.andre601.oneversionremake.core.files.ConfigHandler;
Expand Down Expand Up @@ -99,11 +100,6 @@ public void loadMetrics(){
}));
}

@Override
public boolean reloadConfig(){
return core.reloadConfig();
}

@Override
public Path getPath(){
return getDataFolder().toPath();
Expand All @@ -129,6 +125,11 @@ public ConfigHandler getConfigHandler(){
return core.getConfigHandler();
}

@Override
public CommandHandler getCommandHandler(){
return core.getCommandHandler();
}

@Override
public String getVersion(){
return core.getVersion();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright 2020 - 2021 Andre601
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
* documentation files (the "Software"), to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
* and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial
* portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
* OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

package com.andre601.oneversionremake.bungeecord.commands;

import com.andre601.oneversionremake.core.CommandPermissions;
import com.andre601.oneversionremake.core.interfaces.CmdSender;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import net.kyori.adventure.text.serializer.bungeecord.BungeeComponentSerializer;
import net.md_5.bungee.api.CommandSender;

public class BungeeSender implements CmdSender{

private final CommandSender sender;

public BungeeSender(CommandSender sender){
this.sender = sender;
}

@Override
public boolean hasPermission(String permission){
return sender.hasPermission(permission) || sender.hasPermission(CommandPermissions.ADMIN);
}

@Override
public void sendMsg(){
sendMsg("");
}

@Override
public void sendMsg(String msg, Object... args){
sendMsg(NamedTextColor.WHITE, msg, args);
}

@Override
public void sendMsg(NamedTextColor color, String msg, Object... args){
sender.sendMessage(BungeeComponentSerializer.get().serialize(
Component.text(String.format(msg, args)).color(color)
));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,9 @@

import com.andre601.oneversionremake.bungeecord.BungeeCore;
import com.andre601.oneversionremake.core.CommandPermissions;
import com.andre601.oneversionremake.core.enums.ProtocolVersion;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import net.kyori.adventure.text.serializer.bungeecord.BungeeComponentSerializer;
import net.md_5.bungee.api.CommandSender;
import net.md_5.bungee.api.plugin.Command;

import java.util.List;

public class CmdOneVersionRemake extends Command{

private final BungeeCore plugin;
Expand All @@ -40,59 +34,8 @@ public CmdOneVersionRemake(BungeeCore plugin){

@Override
public void execute(CommandSender commandSender, String[] args){
if(args.length == 0 || args[0].equalsIgnoreCase("help")){
if(!commandSender.hasPermission(CommandPermissions.COMMAND_HELP)){
sendMsg(commandSender, NamedTextColor.RED, "Insufficient permissions!");
return;
}

sendMsg(commandSender, "OneVersionRemake v%s", plugin.getVersion());
sendMsg(commandSender);
sendMsg(commandSender, NamedTextColor.AQUA, "/ovr help");
sendMsg(commandSender, NamedTextColor.GRAY, "Shows this command list.");
sendMsg(commandSender);
sendMsg(commandSender, NamedTextColor.AQUA, "/ovr reload");
sendMsg(commandSender, NamedTextColor.GRAY, "Reloads the configuration.");
}else
if(args[0].equalsIgnoreCase("reload")){
if(!commandSender.hasPermission(CommandPermissions.COMMAND_RELOAD)){
sendMsg(commandSender, NamedTextColor.RED, "Insufficient permissions!");
return;
}

if(plugin.reloadConfig()){
List<Integer> serverProtocols = plugin.getConfigHandler().getIntList("Protocol", "Versions");

sendMsg(commandSender, NamedTextColor.AQUA, "Loaded Minecraft Version(s):");
if(serverProtocols.isEmpty()){
sendMsg(commandSender, NamedTextColor.RED, "None");
}else{
sendMsg(commandSender, NamedTextColor.GRAY, ProtocolVersion.getFriendlyNames(serverProtocols, false));
}

sendMsg(commandSender);
sendMsg(commandSender, NamedTextColor.GREEN, "Reload successful!");
}else{
sendMsg(commandSender, NamedTextColor.RED, "There was an error while reloading the config.yml");
sendMsg(commandSender, NamedTextColor.RED, "Please check the console for any errors and warnings.");
}
}else{
sendMsg(commandSender, NamedTextColor.RED, "Unknown argument \"%s\".", args[0]);
sendMsg(commandSender, NamedTextColor.RED, "Run \"/ovr help\" for help");
}
}

private void sendMsg(CommandSender sender){
sendMsg(sender, "");
}

private void sendMsg(CommandSender sender, String msg, Object... args){
sendMsg(sender, NamedTextColor.WHITE, msg, args);
}

private void sendMsg(CommandSender sender, NamedTextColor color, String msg, Object... args){
sender.sendMessage(BungeeComponentSerializer.get()
.serialize(Component.text(String.format(msg, args)).color(color))
);
BungeeSender sender = new BungeeSender(commandSender);

plugin.getCommandHandler().handle(sender, args);
}
}
4 changes: 4 additions & 0 deletions core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,10 @@
<pattern>org.spongepowered.configurate</pattern>
<shadedPattern>com.andre601.oneversionremake.core.dependencies.configurate</shadedPattern>
</relocation>
<relocation>
<pattern>net.kyori</pattern>
<shadedPattern>com.andre601.oneversionremake.core.dependencies.adventure</shadedPattern>
</relocation>
</relocations>
<filters>
<filter>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,11 @@ public class CommandPermissions{
private final static String BASE = "oneversionremake.";
private final static String COMMAND_BASE = BASE + "command.";

// oneversionremake.admin
public final static String ADMIN = BASE + "admin";

// oneversionremake.command.help
public static final String COMMAND_HELP = COMMAND_BASE + "help";
// oneversionremake.command.reload
public static final String COMMAND_RELOAD = COMMAND_BASE + "reload";
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

package com.andre601.oneversionremake.core;

import com.andre601.oneversionremake.core.commands.CommandHandler;
import com.andre601.oneversionremake.core.enums.ProtocolVersion;
import com.andre601.oneversionremake.core.files.ConfigHandler;
import com.andre601.oneversionremake.core.interfaces.PluginCore;
Expand All @@ -32,12 +33,14 @@ public class OneVersionRemake{

private final PluginCore pluginCore;
private final ConfigHandler configHandler;
private final CommandHandler commandHandler;

private String version;

public OneVersionRemake(PluginCore pluginCore){
this.pluginCore = pluginCore;
this.configHandler = new ConfigHandler(this, pluginCore.getPath());
this.commandHandler = new CommandHandler(this);

start();
}
Expand All @@ -58,77 +61,78 @@ public boolean reloadConfig(){
return configHandler.reload();
}

public CommandHandler getCommandHandler(){
return commandHandler;
}

private void start(){
loadVersion();

ProxyLogger logger = pluginCore.getProxyLogger();

printBanner(logger);
printBanner();

if(configHandler.loadConfig()){
logger.info("Loaded config.yml!");
getProxyLogger().info("Loaded config.yml!");
}else{
logger.warn("Couldn't load config.yml! Check above lines for errors and warnings.");
getProxyLogger().warn("Couldn't load config.yml! Check above lines for errors and warnings.");
return;
}

List<Integer> protocols = configHandler.getIntList("Protocol", "Versions");
boolean versionsSet;
if(protocols.isEmpty()){
printWarning(logger);
printWarning();

versionsSet = false;
}else{
logger.info("Loaded the following Protocol Version(s):");
logger.info(ProtocolVersion.getFriendlyNames(protocols, false));
getProxyLogger().info("Loaded the following Protocol Version(s):");
getProxyLogger().info(ProtocolVersion.getFriendlyNames(protocols, false));

versionsSet = true;
}
logger.info("Loading command /ovr...");

getProxyLogger().info("Loading command /ovr...");
pluginCore.loadCommands();
logger.info("Command loaded!");
logger.info("Loading Event Listeners...");
getProxyLogger().info("Command loaded!");

getProxyLogger().info("Loading Event Listeners...");
pluginCore.loadEventListeners();
logger.info("Event Listeners loaded!");
logger.info("Loading Metrics...");
getProxyLogger().info("Event Listeners loaded!");

getProxyLogger().info("Loading Metrics...");
if(versionsSet){
pluginCore.loadMetrics();
logger.info("Metrics loaded!");
getProxyLogger().info("Metrics loaded!");
}else{
logger.info("No Protocol Versions set. Skipping Metrics initialization...");
getProxyLogger().info("No Protocol Versions set. Skipping Metrics initialization...");
}
logger.info("OneVersionRemake is ready!");

getProxyLogger().info("OneVersionRemake is ready!");
}

private void printBanner(ProxyLogger logger){
logger.info("");
logger.info(" ____ _ ______");
logger.info(" / __ \\ | / / __ \\");
logger.info(" / / / / | / / /_/ /");
logger.info("/ /_/ /| |/ / _, _/");
logger.info("\\____/ |___/_/ |_|");
logger.info("");
logger.info("OneVersionRemake v" + getVersion());
logger.info("Platform: " + pluginCore.getProxyPlatform().getName());
logger.info("");
private void printBanner(){
getProxyLogger().info("");
getProxyLogger().info(" ____ _ ______");
getProxyLogger().info(" / __ \\ | / / __ \\");
getProxyLogger().info(" / / / / | / / /_/ /");
getProxyLogger().info("/ /_/ /| |/ / _, _/");
getProxyLogger().info("\\____/ |___/_/ |_|");
getProxyLogger().info("");
getProxyLogger().info("OneVersionRemake v" + getVersion());
getProxyLogger().info("Platform: " + pluginCore.getProxyPlatform().getName());
getProxyLogger().info("");
}

private void printWarning(ProxyLogger logger){
logger.warn("================================================================================");
logger.warn("WARNING!");
logger.warn("The config option 'Versions' doesn't contain any protocol numbers!");
logger.warn("Please edit the config to include valid protocol numbers or OneVersionRemake");
logger.warn("won't work as expected.");
logger.warn("");
logger.warn("You may find a list of supported protocol versions here:");
logger.warn("https://github.com/Andre601/OneVersionRemake/wiki/Supported-Protocols");
logger.warn("");
logger.warn("OneVersionRemake won't handle joining Players to prevent any possible issues.");
logger.warn("================================================================================");
private void printWarning(){
getProxyLogger().warn("================================================================================");
getProxyLogger().warn("WARNING!");
getProxyLogger().warn("The config option 'Versions' doesn't contain any protocol numbers!");
getProxyLogger().warn("Please edit the config to include valid protocol numbers or OneVersionRemake");
getProxyLogger().warn("won't work as expected.");
getProxyLogger().warn("");
getProxyLogger().warn("You may find a list of supported protocol versions here:");
getProxyLogger().warn("https://github.com/Andre601/OneVersionRemake/wiki/Supported-Protocols");
getProxyLogger().warn("");
getProxyLogger().warn("OneVersionRemake won't handle joining Players to prevent any possible issues.");
getProxyLogger().warn("================================================================================");
}

private void loadVersion(){
Expand Down
Loading

0 comments on commit d81e830

Please sign in to comment.