Skip to content

Commit

Permalink
use jpl_call/4 to login
Browse files Browse the repository at this point in the history
  • Loading branch information
TeamSPoon committed Feb 6, 2025
1 parent 9ff5fe1 commit aed40d4
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 17 deletions.
11 changes: 8 additions & 3 deletions libraries/minecraft-npc-in-metta/prolog/minecraft_bot_driver.pl
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
:- use_module(library(jpl)).

:- dynamic command_queue/1.
:- dynamic bot_controller/1.
:- dynamic voxel_data/4.

%% enqueue_command(+Command) is det.
Expand All @@ -21,9 +22,11 @@
retract(command_queue(Command)),!.

%% login(+Username, +Password, +Server, +Port) is det.
% Issues a login command to log in the bot.
% Call login method on the registered bot_controller object
login(Username, Password, Server, Port) :-
enqueue_command(login(Username, Password, Server, Port)).
bot_controller(Bot), % Retrieve Java object reference
jpl_call(Bot, 'login', [Username, Password, Server, Port], _).


%% move(+X, +Y, +Z) is det.
% Queues a movement command for execution.
Expand Down Expand Up @@ -56,7 +59,7 @@
%% respawn_bot is det.
% Respawns the bot by issuing a login command.
respawn_bot :-
login("PrologBot", "", "localhost", 25565),
login('MeTTaPrologBot', '', 'localhost', 25565),
format("Respawn command issued~n").

%% on_chat_message(+Message) is det.
Expand Down Expand Up @@ -95,3 +98,5 @@
forall(voxel_data(X, Y, Z, BlockID),
format("Voxel: (~w, ~w, ~w) -> Block ~w~n", [X, Y, Z, BlockID])).



Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
:- use_module(library(jpl)).

on_main:- login("PrologBot", "", "localhost", 25565).
on_main:- writeq(login('MeTTaPrologBot', '', 'localhost', 25565)),nl.
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
import org.geysermc.mcprotocollib.protocol.packet.ingame.serverbound.player.ServerboundMovePlayerPosPacket;
import org.geysermc.mcprotocollib.protocol.packet.ingame.serverbound.player.ServerboundMovePlayerPosRotPacket;

import org.jpl7.Atom;
import org.jpl7.Compound;
import org.jpl7.Query;
import org.jpl7.Atom;
import org.jpl7.Compound;
import org.jpl7.Query;
import org.jpl7.Term;
import org.jpl7.JRef;

Expand Down Expand Up @@ -64,15 +64,16 @@ public BotController() {
// Load Prolog files dynamically
loadPrologFiles();


// Assert a Prolog predicate with the BotController object
invokeProlog("assert", makeTerm("bot_controller", this));

if (new File(prologPathRunner).exists()) {
new Query("consult('" + prologPathRunner.replace("\\", "\\\\") + "')").hasSolution();
} else {
log.error("Prolog file missing: {}", prologPathRunner);
}

// Step 2: Assert a Prolog predicate with the client object
Term clientObjectTerm = makeTerm("client_controller", this);
invokeProlog("assert", clientObjectTerm);
}

public BotController(String username, String password, String server, int port) {
Expand Down Expand Up @@ -130,14 +131,10 @@ public void login(String username, String password, String server, int port) {
this.password = password != null ? password : DEFAULT_PASSWORD;
this.serverAddress = new InetSocketAddress(server, port);

// Step 1: Set up client with login details
// Set up client with login details
setupClient();

// Step 2: Assert a Prolog predicate with the client object
Term clientObjectTerm = makeTerm("client_object", client);
invokeProlog("assert", clientObjectTerm);

// Step 3: Connect the client
// Connect the client
connectClient();
}

Expand Down Expand Up @@ -351,7 +348,7 @@ public void readInputAndQueryProlog() {
Scanner scanner = new Scanner(System.in);
log.info("Enter Prolog queries (type 'exit' to quit):");

jplQuery("listing(client_controller/1).");
jplQuery("listing(bot_controller/1).");
while (true) {
System.out.print("> "); // User prompt
String input = scanner.nextLine().trim();
Expand Down Expand Up @@ -386,3 +383,4 @@ private void jplQuery(String queryStr) {
}



0 comments on commit aed40d4

Please sign in to comment.