Skip to content

Commit

Permalink
Event Debugger
Browse files Browse the repository at this point in the history
  • Loading branch information
Owen1212055 committed Dec 3, 2022
1 parent 6b6faec commit d7e14b7
Show file tree
Hide file tree
Showing 11 changed files with 270 additions and 55 deletions.
2 changes: 1 addition & 1 deletion debuggery-bukkit/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
plugins {
id("com.github.johnrengelman.shadow")
id("xyz.jpenilla.run-paper") version "1.0.6"
id("xyz.jpenilla.run-paper") version "2.0.0"
}

tasks {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import io.zachbr.debuggery.commands.*;
import io.zachbr.debuggery.commands.base.CommandBase;
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;
Expand All @@ -29,6 +30,7 @@

public class DebuggeryBukkit extends DebuggeryBase {

private final EventDebugger eventDebugger;
@Nullable
private UUID targetedEntity;
private final DebuggeryJavaPlugin javaPlugin;
Expand All @@ -37,6 +39,7 @@ public class DebuggeryBukkit extends DebuggeryBase {
DebuggeryBukkit(DebuggeryJavaPlugin plugin, Logger logger) {
super(logger);
this.javaPlugin = plugin;
this.eventDebugger = new EventDebugger(this);
}

void onEnable() {
Expand All @@ -61,6 +64,8 @@ private void registerCommands() {
this.registerCommand(new ServerCommand(this));
this.registerCommand(new WorldCommand(this));
this.registerCommand(new SelectEntityCommand(this));
this.registerCommand(new EventCommand(this));
this.registerCommand(new EventRemoveCommand(this));

for (CommandBase c : commands.values()) {
PluginCommand bukkitCmd = this.getJavaPlugin().getCommand(c.getName());
Expand Down Expand Up @@ -93,6 +98,10 @@ public void setTargetedEntity(@Nullable UUID targetedEntity) {
this.targetedEntity = targetedEntity;
}

public EventDebugger getEventDebugger() {
return eventDebugger;
}

@Override
String getPluginVersion() {
return javaPlugin.getDescription().getVersion();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.Nullable;

import java.util.List;

public class EntityCommand extends CommandReflection {
public EntityCommand(DebuggeryBukkit debuggery) {
Expand All @@ -36,6 +39,30 @@ public EntityCommand(DebuggeryBukkit debuggery) {
@Override
protected boolean commandLogic(CommandSender sender, Command command, String label, String[] args) {
Player player = (Player) sender;
Entity target = this.getTarget(player);
if (target == null) {
sender.sendMessage(Component.text("Couldn't detect the entity you were looking at!", NamedTextColor.RED));
return true;
}

return doReflectionLookups(sender, args, target);
}

@Override
public List<String> tabCompleteLogic(CommandSender sender, Command command, String alias, String[] args) {
Entity target = this.getTarget((Player) sender);
if (target == null) {
clearReflectionClass();
return List.of("NOT FOUND");
} else {
updateReflectionClass(target.getClass());
}

return super.tabCompleteLogic(sender, command, alias, args);
}

@Nullable
private Entity getTarget(Player player) {
Entity entity = null;
if (this.debuggery.getTargetedEntity() != null) {
entity = Bukkit.getEntity(this.debuggery.getTargetedEntity());
Expand All @@ -47,13 +74,6 @@ protected boolean commandLogic(CommandSender sender, Command command, String lab
if (entity == null) {
entity = PlatformUtil.getEntityPlayerLookingAt(player, 25, 1.5D);
}

if (entity == null) {
sender.sendMessage(Component.text("Couldn't detect the entity you were looking at!", NamedTextColor.RED));
return true;
}

updateReflectionClass(entity.getClass());
return doReflectionLookups(sender, args, entity);
return entity;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*
* This file is part of Debuggery.
*
* Debuggery is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Debuggery is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Debuggery. If not, see <http://www.gnu.org/licenses/>.
*/

package io.zachbr.debuggery.commands;

import io.zachbr.debuggery.DebuggeryBukkit;
import io.zachbr.debuggery.commands.base.CommandReflection;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Entity;
import org.bukkit.event.Event;

import java.util.Arrays;
import java.util.List;

public class EventCommand extends CommandReflection {


public EventCommand(DebuggeryBukkit debuggery) {
super("devent", "debuggery.devent", true, Entity.class, debuggery);
}

@Override
protected boolean commandLogic(CommandSender sender, Command command, String label, String[] args) {
if (args.length == 0) {
return true;
}

String clazz = args[0];

try {
Class<?> event = Class.forName(clazz, true, this.getClass().getClassLoader());
updateReflectionClass(event);
if (!Event.class.isAssignableFrom(event)) {
sender.sendMessage(Component.text("Provided class is not an event.", NamedTextColor.RED));
return true;
}

String[] offsetArgs = Arrays.copyOfRange(args, 1, args.length);
sender.sendMessage(Component.text("Added event debugger!", NamedTextColor.GREEN));
this.debuggery.getEventDebugger().addDebugger(event, (eventInstance) -> {
this.doReflectionLookups(sender, offsetArgs, eventInstance);
});
} catch (Exception e) {
sender.sendMessage(Component.text("Unknown class name %s!".formatted(clazz), NamedTextColor.RED));
return true;
}

return true;
}

@Override
public List<String> tabCompleteLogic(CommandSender sender, Command command, String alias, String[] args) {
if (args.length == 0) {
return List.of();
}
try {
Class<?> event = Class.forName(args[0], true, this.getClass().getClassLoader());
updateReflectionClass(event);
} catch (ClassNotFoundException e) {
return List.of();
}

String[] trimmed = Arrays.copyOfRange(args, 1, args.length);
if (trimmed.length == 0) {
return List.of();
}

return super.tabCompleteLogic(sender, command, alias, trimmed);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
* This file is part of Debuggery.
*
* Debuggery is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Debuggery is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Debuggery. If not, see <http://www.gnu.org/licenses/>.
*/

package io.zachbr.debuggery.commands;

import io.zachbr.debuggery.DebuggeryBukkit;
import io.zachbr.debuggery.commands.base.CommandBase;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;

import java.util.ArrayList;
import java.util.List;

public class EventRemoveCommand extends CommandBase {

private final DebuggeryBukkit debuggery;

public EventRemoveCommand(DebuggeryBukkit debuggery) {
super("deventremove", "debuggery.devent.remove", true);
this.debuggery = debuggery;
}

@Override
protected boolean commandLogic(CommandSender sender, Command command, String label, String[] args) {
if (args.length > 0 && args[0].equals("*")) {
this.debuggery.getEventDebugger().clearAll();
sender.sendMessage(Component.text("Cleared all event debuggers!", NamedTextColor.GREEN));
return true;
}

try {
Class<?> event = Class.forName(args[0], false, this.getClass().getClassLoader());
if (this.debuggery.getEventDebugger().clear(event)) {
sender.sendMessage(Component.text("Cleared event debugger for " + event, NamedTextColor.GREEN));
} else {
sender.sendMessage(Component.text("No debugger for that event found!", NamedTextColor.RED));
}
} catch (Exception e) {
sender.sendMessage(Component.text("Class not found!", NamedTextColor.RED));
}

return true;
}

@Override
protected boolean helpLogic(CommandSender sender, String[] args) {
sender.sendMessage(Component.text("Clears the event debugger for the provided event."));
return true;
}

@Override
protected List<String> tabCompleteLogic(CommandSender sender, Command command, String alias, String[] args) {
List<String> names = new ArrayList<>();
this.debuggery.getEventDebugger().getAll().forEach((clazz) -> names.add(clazz.getName()));
names.add("*");

return names;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,10 @@ protected void updateReflectionClass(Class<?> typeIn) {
}
}

protected void clearReflectionClass() {
this.availableMethods = MethodMap.EMPTY;
}

/**
* Convenience method to run objects past the TypeHandler
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ public BukkitBootstrap(TypeHandler typeHandler, Logger logger) {
bukkitHandlers.add(new InventoryInputHandler());
bukkitHandlers.add(new ItemStackInputHandler());
bukkitHandlers.add(new LocationInputHandler());
bukkitHandlers.add(new MaterialDataInputHandler());
bukkitHandlers.add(new MaterialInputHandler());
bukkitHandlers.add(new NamespacedKeyInputHandler());
bukkitHandlers.add(new PermissionInputHandler());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

package io.zachbr.debuggery.reflection.types.handlers.bukkit.input;

import io.zachbr.debuggery.reflection.types.handlers.base.InputHandler;
import io.zachbr.debuggery.reflection.types.handlers.base.InputPolymorphicHandler;
import io.zachbr.debuggery.reflection.types.handlers.base.platform.PlatformSender;
import io.zachbr.debuggery.reflection.types.handlers.input.UUIDInputHandler;
import io.zachbr.debuggery.util.PlatformUtil;
Expand All @@ -30,7 +30,7 @@

import java.util.UUID;

public class EntityInputHandler implements InputHandler<Entity> {
public class EntityInputHandler implements InputPolymorphicHandler<Entity> {

private static final UUIDInputHandler UUID_INPUT_HANDLER = new UUIDInputHandler();

Expand Down

This file was deleted.

Loading

0 comments on commit d7e14b7

Please sign in to comment.