Skip to content

Commit

Permalink
Merge pull request #2 from A11v1r15/master
Browse files Browse the repository at this point in the history
Autocomplete on commands
  • Loading branch information
shasankp000 authored Aug 16, 2024
2 parents cf0c96d + 9ae2383 commit 38833b7
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 44 deletions.
87 changes: 45 additions & 42 deletions src/main/java/net/shasankp000/Commands/spawnFakePlayer.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
import com.mojang.brigadier.arguments.IntegerArgumentType;
import com.mojang.brigadier.arguments.StringArgumentType;
import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback;
import net.minecraft.command.argument.BlockPosArgumentType;
import net.minecraft.command.argument.EntityArgumentType;
import net.minecraft.registry.RegistryKey;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.command.CommandManager;
Expand Down Expand Up @@ -73,54 +76,50 @@ public static void register() {
CommandRegistrationCallback.EVENT.register((dispatcher, registryAccess, environment) -> dispatcher.register(
CommandManager.literal("bot")
.then(literal("spawn")
.then(CommandManager.argument("botName", StringArgumentType.string())
.then(CommandManager.argument("bot_name", StringArgumentType.string())
.executes(context -> { spawnBot(context); return 1; })
)
)
.then(literal("walk")
.then(CommandManager.argument("botName", StringArgumentType.string())
.then(CommandManager.argument("bot", EntityArgumentType.player())
.then(CommandManager.argument("till", IntegerArgumentType.integer())
.executes(context -> { botWalk(context); return 1; })
)
)
)
.then(literal("jump")
.then(CommandManager.argument("botName", StringArgumentType.string())
.then(CommandManager.argument("bot", EntityArgumentType.player())
.executes(context -> { botJump(context); return 1; })
)
)
.then(literal("teleportForward")
.then(CommandManager.argument("botName", StringArgumentType.string())
.then(literal("teleport_forward")
.then(CommandManager.argument("bot", EntityArgumentType.player())
.executes(context -> { teleportForward(context); return 1; })
)
)
.then(literal("testChatMessage")
.then(CommandManager.argument("botName", StringArgumentType.string())
.then(literal("test_chat_message")
.then(CommandManager.argument("bot", EntityArgumentType.player())
.executes(context -> { testChatMessage(context); return 1; })
)
)
.then(literal("goTo")
.then(CommandManager.argument("botName", StringArgumentType.string())
.then(CommandManager.argument("x-axis", IntegerArgumentType.integer())
.then(CommandManager.argument("y-axis", IntegerArgumentType.integer())
.then(CommandManager.argument("z-axis", IntegerArgumentType.integer())
.executes(context -> { botGo(context); return 1; })
)
)
.then(literal("go_to")
.then(CommandManager.argument("bot", EntityArgumentType.player())
.then(CommandManager.argument("pos", BlockPosArgumentType.blockPos())
.executes(context -> { botGo(context); return 1; })
)
)
)
.then(literal("sendAMessage")
.then(CommandManager.argument("botName", StringArgumentType.string())
.then(literal("send_message_to")
.then(CommandManager.argument("bot", EntityArgumentType.player())
.then(CommandManager.argument("message", StringArgumentType.greedyString())
.executes(context -> { ollamaClient.execute(context); return 1; })
)
)
)
.then(literal("detectEntities")
.then(CommandManager.argument("botName", StringArgumentType.string())
.then(literal("detect_entities")
.then(CommandManager.argument("bot", EntityArgumentType.player())
.executes(context -> {
String botName = StringArgumentType.getString(context,"botName");
String botName = StringArgumentType.getString(context,"bot_name");
ServerPlayerEntity bot = context.getSource().getServer().getPlayerManager().getPlayer(botName);
if (bot != null) {
RayCasting.detect(bot);
Expand All @@ -146,7 +145,7 @@ private static void spawnBot(CommandContext<ServerCommandSource> context) {

GameMode mode = GameMode.SURVIVAL;

String botName = StringArgumentType.getString(context, "botName");
String botName = StringArgumentType.getString(context, "bot_name");


createFakePlayer.createFake(
Expand Down Expand Up @@ -204,7 +203,7 @@ private static void notImplementedMessage(CommandContext<ServerCommandSource> co

MinecraftServer server = context.getSource().getServer();

String botName = StringArgumentType.getString(context, "botName");
String botName = StringArgumentType.getString(context, "bot_name");

ServerPlayerEntity bot = server.getPlayerManager().getPlayer(botName);

Expand All @@ -231,10 +230,8 @@ private static void notImplementedMessage(CommandContext<ServerCommandSource> co
private static void teleportForward(CommandContext<ServerCommandSource> context) {
MinecraftServer server = context.getSource().getServer();

String botName = StringArgumentType.getString(context, "botName");

ServerPlayerEntity bot = server.getPlayerManager().getPlayer(botName);

ServerPlayerEntity bot = null;
try {bot = EntityArgumentType.getPlayer(context, "bot");} catch (CommandSyntaxException ignored) {}

if (bot == null) {

Expand All @@ -245,6 +242,7 @@ private static void teleportForward(CommandContext<ServerCommandSource> context)
}

else {
String botName = bot.getName().getLiteralString();

BlockPos currentPosition = bot.getBlockPos();
BlockPos newPosition = currentPosition.add(1, 0, 0); // Move one block forward
Expand All @@ -260,12 +258,11 @@ private static void botWalk(CommandContext<ServerCommandSource> context) {

MinecraftServer server = context.getSource().getServer();

String botName = StringArgumentType.getString(context, "botName");
ServerPlayerEntity bot = null;
try {bot = EntityArgumentType.getPlayer(context, "bot");} catch (CommandSyntaxException ignored) {}

int travelTime = IntegerArgumentType.getInteger(context, "till");

ServerPlayerEntity bot = server.getPlayerManager().getPlayer(botName);


if (bot == null) {

Expand All @@ -277,6 +274,8 @@ private static void botWalk(CommandContext<ServerCommandSource> context) {

else {

String botName = bot.getName().getLiteralString();

ServerCommandSource botSource = bot.getCommandSource().withLevel(2).withSilent().withMaxLevel(4);
moveForward(server, botSource, botName);

Expand All @@ -292,9 +291,8 @@ private static void botJump(CommandContext<ServerCommandSource> context) {

MinecraftServer server = context.getSource().getServer();

String bot_name = StringArgumentType.getString(context, "botName");

ServerPlayerEntity bot = server.getPlayerManager().getPlayer(bot_name);
ServerPlayerEntity bot = null;
try {bot = EntityArgumentType.getPlayer(context, "bot");} catch (CommandSyntaxException ignored) {}


if (bot == null) {
Expand All @@ -307,10 +305,12 @@ private static void botJump(CommandContext<ServerCommandSource> context) {

else {

String botName = bot.getName().getLiteralString();

bot.jump();


LOGGER.info("{} jumped!", bot_name);
LOGGER.info("{} jumped!", botName);


}
Expand All @@ -321,11 +321,10 @@ private static void testChatMessage(CommandContext<ServerCommandSource> context)

String response = "I am doing great! It feels good to be able to chat with you again after a long time. So, how have you been doing? Are you enjoying the game world and having fun playing Minecraft with me? Let's continue chatting about whatever topic comes to mind! I love hearing from you guys and seeing your creations in the game. Don't hesitate to share anything with me, whether it's an idea, a problem, or simply something that makes you laugh. Cheers!";

String botName = StringArgumentType.getString(context,"botName");

MinecraftServer server = context.getSource().getServer();

ServerPlayerEntity bot = server.getPlayerManager().getPlayer(botName);
ServerPlayerEntity bot = null;
try {bot = EntityArgumentType.getPlayer(context, "bot");} catch (CommandSyntaxException ignored) {}

if (bot != null) {

Expand All @@ -348,15 +347,16 @@ private static void botGo(CommandContext<ServerCommandSource> context) {

MinecraftServer server = context.getSource().getServer();

String botName = StringArgumentType.getString(context, "botName");
BlockPos position = BlockPosArgumentType.getBlockPos(context, "pos");

int x_distance = IntegerArgumentType.getInteger(context, "x-axis");
int x_distance = position.getX();

int y_distance = IntegerArgumentType.getInteger(context, "y-axis");
int y_distance = position.getY();

int z_distance = IntegerArgumentType.getInteger(context, "z-axis");
int z_distance = position.getZ();

ServerPlayerEntity bot = server.getPlayerManager().getPlayer(botName);
ServerPlayerEntity bot = null;
try {bot = EntityArgumentType.getPlayer(context, "bot");} catch (CommandSyntaxException ignored) {}

if (bot == null) {

Expand All @@ -367,13 +367,16 @@ private static void botGo(CommandContext<ServerCommandSource> context) {
}

else {
String botName = bot.getName().getLiteralString();

ServerCommandSource botSource = bot.getCommandSource().withLevel(2).withSilent().withMaxLevel(4);

server.sendMessage(Text.literal("Finding the shortest path to the target, please wait patiently if the game seems hung"));
// Calculate path
ServerPlayerEntity finalBot = bot;
new Thread(() -> {

List<BlockPos> path = calculatePath(bot.getBlockPos(), new BlockPos(x_distance, y_distance , z_distance));
List<BlockPos> path = calculatePath(finalBot.getBlockPos(), new BlockPos(x_distance, y_distance , z_distance));

path = simplifyPath(path);

Expand Down
6 changes: 4 additions & 2 deletions src/main/java/net/shasankp000/OllamaClient/ollamaClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@

import com.mojang.brigadier.arguments.StringArgumentType;
import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import io.github.amithkoujalgi.ollama4j.core.OllamaAPI;
import io.github.amithkoujalgi.ollama4j.core.exceptions.OllamaBaseException;
import io.github.amithkoujalgi.ollama4j.core.models.chat.*;

import io.github.amithkoujalgi.ollama4j.core.types.OllamaModelType;
import net.minecraft.client.MinecraftClient;
import net.minecraft.command.argument.EntityArgumentType;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.server.network.ServerPlayerEntity;
Expand Down Expand Up @@ -53,8 +55,8 @@ public class ollamaClient {
private static final String DB_URL = "jdbc:sqlite:" + gameDir + "/sqlite_databases/memory_agent.db";


public static void execute(CommandContext<ServerCommandSource> context) {
botName = StringArgumentType.getString(context, "botName");
public static void execute(CommandContext<ServerCommandSource> context) throws CommandSyntaxException {
botName = EntityArgumentType.getPlayer(context, "bot").getName().getLiteralString();

System.out.println("Bot name set to: " + botName);

Expand Down

0 comments on commit 38833b7

Please sign in to comment.