Skip to content

Commit

Permalink
add .allowConsole()
Browse files Browse the repository at this point in the history
  • Loading branch information
KaiAF committed Jan 9, 2024
1 parent 27ec777 commit 2b120ff
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 13 deletions.
2 changes: 1 addition & 1 deletion Dish-API
2 changes: 1 addition & 1 deletion dish/libraries/23w51b.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
},
{
"downloads": {
"url": "https://maven.ouja.net/net/ouja/api/23w51b-R0.2-SNAPSHOT/api-23w51b-R0.2-20240109.112828-1.jar",
"url": "https://maven.ouja.net/net/ouja/api/23w51b-R0.2-SNAPSHOT/api-23w51b-R0.2-20240109.113258-2.jar",
"path": "net/ouja/api/23w51b/23w51b-R0.2-SNAPSHOT.jar"
},
"name": "net.ouja:api:23w51b-R0.2-SNAPSHOT"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package net.ouja.dish.commands;

import com.mojang.brigadier.builder.ArgumentBuilder;
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import net.minecraft.ChatFormatting;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.commands.Commands;
import net.minecraft.network.chat.Component;
import net.ouja.api.commands.CommandListener;
Expand All @@ -26,24 +29,32 @@ public RegisteredCommands(CommandListener commandListener, Method method, Class<
names.put(name, method);
classes.put(name, commandClass);

LiteralArgumentBuilder<CommandSourceStack> command = Commands.literal(name);
String finalName = name;
Commands.getDispatcher().register(Commands.literal(name).executes((cmd) -> {
DishPlayer player = new DishPlayer(cmd.getSource().getPlayer());
try {
return runCommand(finalName, player) ? 1 : 0;
} catch (Exception e) {
Component component = Component.literal("An error occurred when running this command").withStyle(ChatFormatting.RED);
player.sendMessage(DishComponent.fromComponent(component).setColor(String.valueOf(ChatFormatting.RED.getColor().intValue())));
return 0;
}
}));
Commands.getDispatcher().register(
command.executes((cmd) -> {
DishPlayer player = new DishPlayer(cmd.getSource().getPlayer());
try {
return runCommand(finalName, player, commandListener) ? 1 : 0;
} catch (Exception e) {
Component component = Component.literal("An error occurred when running this command").withStyle(ChatFormatting.RED);
player.sendMessage(DishComponent.fromComponent(component).setColor(String.valueOf(ChatFormatting.RED.getColor().intValue())));
return 0;
}
})
);
}

public static boolean runCommand(String commandName, DishPlayer player) throws InstantiationException, IllegalAccessException, InvocationTargetException {
public static boolean runCommand(String commandName, DishPlayer player, CommandListener commandListener) throws InstantiationException, IllegalAccessException, InvocationTargetException {
Method method = names.get(commandName);
Class<?> clazz = classes.get(commandName);
if (method == null || clazz == null) return false;

if (player.isConsole() && !commandListener.allowConsole()) {
System.out.println("Command can't be ran from the console.");
return false;
}

method.setAccessible(true);
return (boolean)method.invoke(clazz.newInstance(), player);
}
Expand Down

0 comments on commit 2b120ff

Please sign in to comment.