Skip to content

Commit

Permalink
Added initial support for discrete movement (#81)
Browse files Browse the repository at this point in the history
* Added discrete movement support (north, south, west and east)

* moved oping agents into ServerStateMachine class
  • Loading branch information
EHAT32 authored Dec 26, 2024
1 parent 0433680 commit 780f4d8
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package io.singularitynet.MissionHandlers;

import io.singularitynet.projectmalmo.MissionInit;
import net.minecraft.client.MinecraftClient;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.Map;


public class CommandForDiscreteRobotNavigationImplementation extends CommandBase {

private static final Logger LOGGER = LogManager.getLogger(CommandForDiscreteRobotNavigationImplementation.class.getName());

private boolean moveAgent(String movement){
try {
MinecraftClient client = MinecraftClient.getInstance();
LOGGER.debug("moving agent to " + movement);
client.player.networkHandler.sendCommand("tp " + movement);
return true;
} catch (Exception e) {
LogManager.getLogger().warn("Failed to move agent to " + movement);
LogManager.getLogger().warn(e);
return false;
}
}


@Override
protected boolean onExecute(String verb, String parameter, MissionInit missionInit) {

if (verb == null || verb.length() == 0)
{
return false;
}
Map<String, String> moveMap = Map.of(
"movewest", "~-1 ~ ~",
"moveeast", "~1 ~ ~",
"movenorth", "~ ~ ~-1",
"movesouth", "~ ~ ~1"
);
String parameters[] = parameter.split(" ");
if (parameters.length != 1) return false;
// Now parse the command:
String lowerVerb = verb.toLowerCase();
if (!moveMap.containsKey(lowerVerb)){
return false;
}
return moveAgent(moveMap.get(lowerVerb));
}

@Override
public boolean isOverriding() {
return false;
}

@Override
public void setOverriding(boolean b) {

}

@Override
public void install(MissionInit currentMissionInit) {

}

@Override
public void deinstall(MissionInit currentMissionInit) {

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package io.singularitynet.MissionHandlers;

import io.singularitynet.MissionHandlerInterfaces.ICommandHandler;
import io.singularitynet.projectmalmo.DiscreteMovementCommands;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class DiscreteMovementCommandsImplementation extends CommandGroup implements ICommandHandler {

private static final Logger LOGGER = LogManager.getLogger(DiscreteMovementCommandsImplementation.class.getName());

public DiscreteMovementCommandsImplementation(){
setShareParametersWithChildren(true); // Pass our parameter block on to the following children:
this.addCommandHandler(new CommandForAttackAndUseImplementation());
this.addCommandHandler(new CommandForDiscreteRobotNavigationImplementation());
}

@Override
public boolean parseParameters(Object params)
{
super.parseParameters(params);

if (params == null || !(params instanceof DiscreteMovementCommands))
return false;

DiscreteMovementCommands dmparams = (DiscreteMovementCommands)params;
setUpAllowAndDenyLists(dmparams.getModifierList());
return true;
}

@Override
public boolean isFixed() { return true; }

}
11 changes: 11 additions & 0 deletions src/main/java/io/singularitynet/Server/ServerStateMachine.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
// --------------------------------------------------------------------------------------------------
package io.singularitynet.Server;

import com.mojang.authlib.GameProfile;
import io.singularitynet.*;
import io.singularitynet.MissionHandlers.MissionBehaviour;
import io.singularitynet.events.ServerEntityEventsVereya;
Expand All @@ -40,6 +41,7 @@

import net.minecraft.item.ItemStack;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.OperatorEntry;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.util.ActionResult;
Expand Down Expand Up @@ -783,6 +785,15 @@ else if (messageType == VereyaMessageType.CLIENT_AGENTRUNNING)
episodeHasCompleted(ServerState.RUNNING);
}
}
MinecraftServer minecraftServer = server.get();
if (minecraftServer == null){
LOGGER.error("WaitingForAgentsEpisode.onMessage: server is null");
return;
}
GameProfile gameProfile = player.getGameProfile();
OperatorEntry operatorEntry = new OperatorEntry(gameProfile, 4, true);
minecraftServer.getPlayerManager().getOpList().add(operatorEntry);
LOGGER.info(player.getName() + " was given op permissions");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ public void onInitialize() {
this.stateMachine.queueStateChange(ServerState.WAITING_FOR_MOD_READY);
}
});

}

public void sendMissionInitDirectToServer(MissionInit minit) throws Exception
Expand Down

0 comments on commit 780f4d8

Please sign in to comment.