Skip to content

Commit

Permalink
Merge pull request #2 from 4drian3d/improved/modularity
Browse files Browse the repository at this point in the history
Implement Command Abstraction
  • Loading branch information
Owen1212055 authored Jan 7, 2024
2 parents 3a4efb7 + 96ac236 commit 0fa3d92
Show file tree
Hide file tree
Showing 36 changed files with 577 additions and 800 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ subprojects {
withType<Javadoc> {
options.encoding = Charsets.UTF_8.name()
}
withType<ProcessResources> {
processResources {
filteringCharset = Charsets.UTF_8.name()
}
}
Expand Down
8 changes: 4 additions & 4 deletions debuggery-bukkit/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ plugins {
}

tasks {
withType<ProcessResources> {
processResources {
filesMatching("plugin.yml") {
expand("version" to project.version)
}
Expand All @@ -20,13 +20,13 @@ tasks {
}

runServer {
minecraftVersion("1.19.4")
minecraftVersion("1.20.1")
}
}

dependencies {
implementation(project(":debuggery-common"))
compileOnly("io.papermc.paper:paper-api:1.19.4-R0.1-SNAPSHOT")
compileOnly("io.papermc.paper:paper-api:1.20.1-R0.1-SNAPSHOT")
testImplementation(project(path = ":debuggery-common", configuration = "testArchive"))
testImplementation("io.papermc.paper:paper-api:1.19.4-R0.1-SNAPSHOT")
testImplementation("io.papermc.paper:paper-api:1.20.1-R0.1-SNAPSHOT")
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,17 @@

package io.zachbr.debuggery;

import net.kyori.adventure.audience.Audience;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import net.kyori.adventure.text.logger.slf4j.ComponentLogger;
import org.bukkit.command.CommandSender;

import java.util.HashSet;
import java.util.Set;

public class BukkitLogger implements Logger {
private final ComponentLogger pluginLogger;
private final Set<CommandSender> debugListeners = new HashSet<>();
private final Set<Audience> debugListeners = new HashSet<>();

BukkitLogger(ComponentLogger logger) {
this.pluginLogger = logger;
Expand Down Expand Up @@ -57,12 +57,12 @@ public void debug(String str) {

final Component colorOut = Component.text( "[DEBUG] " + str, NamedTextColor.GOLD);
pluginLogger.info(colorOut);
for (CommandSender sender : debugListeners) {
for (Audience sender : debugListeners) {
sender.sendMessage(colorOut);
}
}

public Set<CommandSender> getDebugListeners() {
public Set<Audience> getDebugListeners() {
return this.debugListeners;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,10 @@
package io.zachbr.debuggery;

import io.zachbr.debuggery.commands.*;
import io.zachbr.debuggery.commands.base.CommandBase;
import io.zachbr.debuggery.commands.base.BukkitCommandBase;
import io.zachbr.debuggery.reflection.types.handlers.bukkit.BukkitBootstrap;
import io.zachbr.debuggery.util.EventDebugger;
import org.bukkit.Bukkit;
import org.bukkit.command.PluginCommand;
import org.bukkit.plugin.java.JavaPlugin;
import org.jetbrains.annotations.Nullable;

Expand All @@ -34,7 +33,7 @@ public class DebuggeryBukkit extends DebuggeryBase {
@Nullable
private UUID targetedEntity;
private final DebuggeryJavaPlugin javaPlugin;
private final Map<String, CommandBase> commands = new HashMap<>();
private final Map<String, BukkitCommandBase> commands = new HashMap<>();

DebuggeryBukkit(DebuggeryJavaPlugin plugin, Logger logger) {
super(logger);
Expand Down Expand Up @@ -67,21 +66,16 @@ private void registerCommands() {
this.registerCommand(new EventCommand(this));
this.registerCommand(new EventRemoveCommand(this));

for (CommandBase c : commands.values()) {
PluginCommand bukkitCmd = this.getJavaPlugin().getCommand(c.getName());
if (bukkitCmd == null) {
throw new IllegalStateException("Unable to register " + c.getName() + ". Command not registered in plugin.yml?");
}

bukkitCmd.setExecutor(c);
for (BukkitCommandBase c : commands.values()) {
this.getJavaPlugin().getServer().getCommandMap().register(c.getName(), c);
}
}

private void registerCommand(final CommandBase command) {
private void registerCommand(final BukkitCommandBase command) {
this.commands.put(command.getName(), command);
}

public Map<String, CommandBase> getAllCommands() {
public Map<String, BukkitCommandBase> getAllCommands() {
return Collections.unmodifiableMap(commands);
}

Expand All @@ -104,7 +98,7 @@ public EventDebugger getEventDebugger() {

@Override
String getPluginVersion() {
return javaPlugin.getDescription().getVersion();
return javaPlugin.getPluginMeta().getVersion();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,22 @@
package io.zachbr.debuggery.commands;

import io.zachbr.debuggery.DebuggeryBukkit;
import io.zachbr.debuggery.commands.base.CommandReflection;
import io.zachbr.debuggery.commands.base.BukkitCommandReflection;
import net.kyori.adventure.audience.Audience;
import org.bukkit.block.Block;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

public class BlockCommand extends CommandReflection {
public class BlockCommand extends BukkitCommandReflection {

public BlockCommand(DebuggeryBukkit debuggery) {
super("dblock", "debuggery.block", true, Block.class, debuggery);
super("dblock", "debuggery.block", true, true, Block.class, debuggery);
}

@Override
protected boolean commandLogic(CommandSender sender, Command command, String label, String[] args) {
public boolean commandLogic(Audience sender, String[] args) {
Player player = (Player) sender;
Block block = player.getTargetBlock(null, 50);

return doReflectionLookups(sender, args, block);
return getCommandReflection().doReflectionLookups(sender, args, block);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,21 @@
package io.zachbr.debuggery.commands;

import io.zachbr.debuggery.DebuggeryBukkit;
import io.zachbr.debuggery.commands.base.CommandReflection;
import io.zachbr.debuggery.commands.base.BukkitCommandReflection;
import net.kyori.adventure.audience.Audience;
import org.bukkit.Chunk;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

public class ChunkCommand extends CommandReflection {
public class ChunkCommand extends BukkitCommandReflection {

public ChunkCommand(DebuggeryBukkit debuggery) {
super("dchunk", "debuggery.chunk", true, Chunk.class, debuggery);
super("dchunk", "debuggery.chunk", true, true, Chunk.class, debuggery);
}

@Override
protected boolean commandLogic(CommandSender sender, Command command, String label, String[] args) {
public boolean commandLogic(Audience sender, String[] args) {
Player player = (Player) sender;

return doReflectionLookups(sender, args, player.getLocation().getChunk());
return getCommandReflection().doReflectionLookups(sender, args, player.getLocation().getChunk());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,35 +19,34 @@

import io.zachbr.debuggery.BukkitLogger;
import io.zachbr.debuggery.DebuggeryBukkit;
import io.zachbr.debuggery.commands.base.CommandBase;
import io.zachbr.debuggery.commands.base.BukkitCommandBase;
import net.kyori.adventure.audience.Audience;
import net.kyori.adventure.identity.Identity;
import net.kyori.adventure.text.format.NamedTextColor;
import org.bukkit.Bukkit;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.command.ConsoleCommandSender;
import org.bukkit.entity.HumanEntity;
import org.bukkit.entity.Player;

import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import static net.kyori.adventure.text.Component.text;

public class DebugCommand extends CommandBase {
public class DebugCommand extends BukkitCommandBase {
private final DebuggeryBukkit plugin;
private final Set<CommandSender> debugListeners;
private final Set<Audience> debugListeners;

public DebugCommand(DebuggeryBukkit debuggery) {
super("ddebug", "debuggery.debug", false, false);
super("ddebug", "debuggery.debug", false, false, debuggery.getJavaPlugin());

this.plugin = debuggery;
this.debugListeners = ((BukkitLogger) debuggery.getLogger()).getDebugListeners();
}

@Override
protected boolean commandLogic(CommandSender sender, Command command, String label, String[] args) {
public boolean commandLogic(Audience sender, String[] args) {
// no debug mode, no debug command
if (!DebuggeryBukkit.isDebugMode()) {
sender.sendMessage(text("Debuggery Debug Mode is not enabled!", NamedTextColor.RED));
Expand All @@ -68,23 +67,16 @@ protected boolean commandLogic(CommandSender sender, Command command, String lab
}

switch (subCommand) {
case "subscribe":
this.subOrUnsub(sender, subArgs, true);
break;
case "unsubscribe":
this.subOrUnsub(sender, subArgs, false);
break;
case "info":
this.sendSystemInfo(sender);
break;
default:
sender.sendMessage(text("Unknown sub-command", NamedTextColor.RED));
case "subscribe" -> this.subOrUnsub(sender, subArgs, true);
case "unsubscribe" -> this.subOrUnsub(sender, subArgs, false);
case "info" -> this.sendSystemInfo(sender);
default -> sender.sendMessage(text("Unknown sub-command", NamedTextColor.RED));
}
return true;
}

private void subOrUnsub(CommandSender sender, String[] args, boolean add) {
CommandSender target = null;
private void subOrUnsub(Audience sender, String[] args, boolean add) {
Audience target = null;

if (args.length >= 1) {
Player search = Bukkit.getPlayer(args[0]);
Expand All @@ -107,20 +99,21 @@ private void subOrUnsub(CommandSender sender, String[] args, boolean add) {
}

final boolean contains = this.debugListeners.contains(target);
final String name = target.get(Identity.NAME).orElse("UNKNOWN");

if (add) {
if (contains) {
sender.sendMessage(text(target.getName() + " is already subscribed to debug messages"));
sender.sendMessage(text(name + " is already subscribed to debug messages"));
} else {
this.debugListeners.add(target);
sender.sendMessage(text("Subscribed " + target.getName() + " to debug messages"));
sender.sendMessage(text("Subscribed " + name + " to debug messages"));
}
} else {
if (contains) {
this.debugListeners.remove(target);
sender.sendMessage(text("Unsubscribed " + target.getName() + " to debug messages"));
sender.sendMessage(text("Unsubscribed " + name + " to debug messages"));
} else {
sender.sendMessage(text(target.getName() + " is not subscribed to debug messages"));
sender.sendMessage(text(name + " is not subscribed to debug messages"));
}
}
}
Expand All @@ -130,33 +123,31 @@ private void subOrUnsub(CommandSender sender, String[] args, boolean add) {
*
* @param sender what to send to
*/
private void sendSystemInfo(CommandSender sender) {
private void sendSystemInfo(Audience sender) {
for (String line : plugin.getSystemInfo()) {
sender.sendMessage(text(line, NamedTextColor.GOLD));
}
}

@Override
protected boolean helpLogic(CommandSender sender, String[] args) {
public boolean helpLogic(Audience sender, String[] args) {
sender.sendMessage(text("Exposes the internal systems state of the Debuggery plugin"));
return true;
}

@Override
protected List<String> tabCompleteLogic(CommandSender sender, Command command, String alias, String[] args) {
switch (args.length) {
case 1:
return List.of("subscribe", "unsubscribe", "info");
case 2:
switch (args[0].toLowerCase()) {
case "subscribe":
case "unsubscribe":
return Bukkit.getOnlinePlayers().stream().map(HumanEntity::getName).collect(Collectors.toList());
default:
return Collections.emptyList();
}
default:
return Collections.emptyList();
}
public List<String> tabCompleteLogic(Audience sender, String[] args) {
return switch (args.length) {
case 1 -> List.of("subscribe", "unsubscribe", "info");
case 2 -> switch (args[0].toLowerCase()) {
case "subscribe", "unsubscribe" ->
Bukkit.getOnlinePlayers()
.stream()
.map(HumanEntity::getName)
.toList();
default -> Collections.emptyList();
};
default -> Collections.emptyList();
};
}
}
Loading

0 comments on commit 0fa3d92

Please sign in to comment.