Search in sources :

Example 6 with MissionInit

use of com.microsoft.Malmo.Schemas.MissionInit in project malmo by Microsoft.

the class MissionQuitCommandsImplementation method install.

// ------------- ICommandHandler methods -----------
@Override
public void install(MissionInit missionInit) {
    // In order to trigger the end of the mission, we need to hook into the quit handlers.
    MissionBehaviour mb = parentBehaviour();
    mb.addQuitProducer(new IWantToQuit() {

        @Override
        public void prepare(MissionInit missionInit) {
        }

        @Override
        public String getOutcome() {
            return MissionQuitCommandsImplementation.this.quitcomParams.getQuitDescription();
        }

        @Override
        public boolean doIWantToQuit(MissionInit missionInit) {
            return MissionQuitCommandsImplementation.this.iWantToQuit;
        }

        @Override
        public void cleanup() {
        }
    });
}
Also used : IWantToQuit(com.microsoft.Malmo.MissionHandlerInterfaces.IWantToQuit) MissionInit(com.microsoft.Malmo.Schemas.MissionInit)

Example 7 with MissionInit

use of com.microsoft.Malmo.Schemas.MissionInit in project malmo by Microsoft.

the class ObservationFromRecentCommandsImplementation method writeObservationsToJSON.

@Override
public void writeObservationsToJSON(JsonObject json, MissionInit missionInit) {
    if (!hookedIntoCommandChain) {
        // We need to see the commands as they come in, so we can determine which ones to echo back in the observation message.
        // To do this we create our own command handler and insert it at the root of the command chain.
        // It's slightly dirty behaviour, but it saves
        // a) adding special code into ProjectMalmo.java just to allow for this ObservationProducer to work, and
        // b) requiring the user to add a special command handler themselves at the right point in the XML.
        MissionBehaviour mb = parentBehaviour();
        ICommandHandler oldch = mb.commandHandler;
        CommandGroup newch = new CommandGroup() {

            protected boolean onExecute(String verb, String parameter, MissionInit missionInit) {
                // See if this command gets handled by the legitimate handlers:
                boolean handled = super.onExecute(verb, parameter, missionInit);
                if (// Yes, so record it:
                handled)
                    ObservationFromRecentCommandsImplementation.this.addHandledCommand(verb, parameter);
                return handled;
            }
        };
        newch.setOverriding((oldch != null) ? oldch.isOverriding() : true);
        if (oldch != null)
            newch.addCommandHandler(oldch);
        mb.commandHandler = newch;
        this.hookedIntoCommandChain = true;
    }
    synchronized (this.recentCommandList) {
        // Have any commands been processed since we last sent a burst of observations?
        if (this.recentCommandList.size() != 0) {
            // Yes, so build up a JSON array:
            JsonArray commands = new JsonArray();
            for (String s : this.recentCommandList) {
                commands.add(new JsonPrimitive(s));
            }
            json.add("CommandsSinceLastObservation", commands);
        }
        this.recentCommandList.clear();
    }
}
Also used : JsonArray(com.google.gson.JsonArray) JsonPrimitive(com.google.gson.JsonPrimitive) MissionInit(com.microsoft.Malmo.Schemas.MissionInit) ICommandHandler(com.microsoft.Malmo.MissionHandlerInterfaces.ICommandHandler)

Example 8 with MissionInit

use of com.microsoft.Malmo.Schemas.MissionInit in project malmo by Microsoft.

the class RewardForSendingMatchingChatMessageImplementation method prepare.

/**
 * Called once before the mission starts - use for any necessary
 * initialisation.
 *
 * @param missionInit
 */
@Override
public void prepare(MissionInit missionInit) {
    super.prepare(missionInit);
    // We need to see chat commands as they come in.
    // Following the example of RewardForSendingCommandImplementation.
    MissionBehaviour mb = parentBehaviour();
    ICommandHandler oldch = mb.commandHandler;
    CommandGroup newch = new CommandGroup() {

        protected boolean onExecute(String verb, String parameter, MissionInit missionInit) {
            // See if this command gets handled by the legitimate handlers:
            boolean handled = super.onExecute(verb, parameter, missionInit);
            if (// Yes, so check if we need to produce a reward
            handled && verb.equalsIgnoreCase(ChatCommand.CHAT.value())) {
                Iterator<Map.Entry<Pattern, Float>> patternIt = patternMap.entrySet().iterator();
                while (patternIt.hasNext()) {
                    Map.Entry<Pattern, Float> entry = patternIt.next();
                    Matcher m = entry.getKey().matcher(parameter);
                    if (m.matches()) {
                        String distribution = distributionMap.get(entry.getKey());
                        addAndShareCachedReward(RewardForSendingMatchingChatMessageImplementation.this.params.getDimension(), entry.getValue(), distribution);
                    }
                }
            }
            return handled;
        }
    };
    newch.setOverriding((oldch != null) ? oldch.isOverriding() : true);
    if (oldch != null)
        newch.addCommandHandler(oldch);
    mb.commandHandler = newch;
}
Also used : MissionInit(com.microsoft.Malmo.Schemas.MissionInit) ICommandHandler(com.microsoft.Malmo.MissionHandlerInterfaces.ICommandHandler)

Aggregations

MissionInit (com.microsoft.Malmo.Schemas.MissionInit)8 ICommandHandler (com.microsoft.Malmo.MissionHandlerInterfaces.ICommandHandler)4 IWantToQuit (com.microsoft.Malmo.MissionHandlerInterfaces.IWantToQuit)2 JsonArray (com.google.gson.JsonArray)1 JsonPrimitive (com.google.gson.JsonPrimitive)1 IState (com.microsoft.Malmo.IState)1 MinecraftServerConnection (com.microsoft.Malmo.Schemas.MinecraftServerConnection)1 TCPInputPoller (com.microsoft.Malmo.Utils.TCPInputPoller)1 DataOutputStream (java.io.DataOutputStream)1 IOException (java.io.IOException)1 TextComponentString (net.minecraft.util.text.TextComponentString)1 Configuration (net.minecraftforge.common.config.Configuration)1