QAPlugin - API

Dec 24, 2016
QAPlugin - API
  • QAPlugin
    Developer API

    be.maximvdw.qaplugin.api.QAPluginAPI
    This class provides static method(s) to add your own question (instead of adding it in the config)

    be.maximvdw.qaplugin.AIComponent
    This is used to create your own AIComponents to use in combination with API.ai
    Once you create a new AIComponent it will be registered, so you do not need to handle this.

    Example: Simple AIComponent with no parameter usage
    Code (Text):
    package be.maximvdw.qaplugin.ai;

    import be.maximvdw.qaplugin.api.AIComponent;
    import be.maximvdw.qaplugin.api.AIQuestionEvent;
    import org.bukkit.inventory.ItemStack;

    import java.util.HashMap;
    import java.util.Map;

    /**
    * DurabilityComponent
    *
    * Asks the bot for the durability of the item you are holding.
    *
    * Created by maxim on 24-Dec-16.
    */
    public class DurabilityComponent extends AIComponent {

        public DurabilityComponent() {
            // This is the name you give to your action
            // it is the name that you will have to fill in under "action" in api.ai
            super("qaplugin_itemdurability");
            // Here you can define possible responses
            // ${....} are custom placeholders you can fill in yourself
            addResponse("The durability of your item is ${durability}");
            addResponse("The item's durability is ${durability}");
        }

        /**
         * Called every time a response with your action name is returned
         * NOTE: The request is async
         *
         * @param event AIQuestionEvent even containing all data to parse the response
         * @return Always return a response, even error responses
         */
        @Override
        public String getResponse(AIQuestionEvent event) {
            ItemStack item = event.getPlayer().getItemInHand();
            short durability = item.getDurability();
            Map<String,String> placeholders = new HashMap<>();
            placeholders.put("durability",String.valueOf(durability)); // Will replace ${durability}

            return getRandomResponse(placeholders,event.getPlayer());
        }
    }
     
    [​IMG]
    [​IMG]

    Example: Reading parameters + error handling
    Code (Text):

    package be.maximvdw.qaplugin.ai;

    import be.maximvdw.basecore.BasePlugin;
    import be.maximvdw.qaplugin.api.AIComponent;
    import be.maximvdw.qaplugin.api.AIQuestionEvent;
    import org.bukkit.Bukkit;
    import org.bukkit.FireworkEffect;
    import org.bukkit.entity.Firework;
    import org.bukkit.inventory.meta.FireworkMeta;

    import java.awt.*;
    import java.lang.reflect.Field;
    import java.util.HashMap;
    import java.util.Map;

    /**
    * FireworkComponent
    *
    * Asks the bot to fire a specific color firework
    *
    * Created by maxim on 24-Dec-16.
    */
    public class FireworkComponent extends AIComponent {

        public FireworkComponent() {
            // This is the name you give to your action
            // it is the name that you will have to fill in under "action" in api.ai
            super("qaplugin_firework");
            // Here you can define possible responses
            // ${....} are custom placeholders you can fill in yourself
            addResponse("I've fired ${color} fireworks!");
            addResponse("You should see some ${color} firework now!");
        }

        /**
         * Called every time a response with your action name is returned
         * NOTE: The request is async
         *
         * @param event AIQuestionEvent even containing all data to parse the response
         * @return Always return a response, even error responses
         */
        @Override
        public String getResponse(final AIQuestionEvent event) {
            // These are the parsed arguments of the question
            // In this case it should contain the color
            Map<String,String> params = event.getParameters();
            if (!params.containsKey("color")){
                return "Oeps, I don't know what color!";
            }

            Color color;
            try {
                Field field = Color.class.getField(params.get("color").toLowerCase());
                color = (Color)field.get(null);
            } catch (Exception e) {
                color = null; // Not defined
            }
            if (color == null){
                return "Euhm, I don't know that color :S";
            }

            // We need to make it sync since we are running this thread async
            final Color finalColor = color;
            Bukkit.getScheduler().runTask(BasePlugin.getInstance(),new Runnable(){

                @Override
                public void run() {
                    Firework f = event.getPlayer().getWorld().spawn(event.getPlayer().getLocation(), Firework.class);

                    FireworkMeta fm = f.getFireworkMeta();
                    fm.addEffect(FireworkEffect.builder()
                            .flicker(false)
                            .trail(true)
                            .with(FireworkEffect.Type.BALL)
                            .with(FireworkEffect.Type.BALL_LARGE)
                            .with(FireworkEffect.Type.STAR)
                            .withColor(org.bukkit.Color.fromRGB(finalColor.getRed(),finalColor.getGreen(),finalColor.getBlue()))
                            .build());
                    fm.setPower(2);
                    f.setFireworkMeta(fm);
                }
            });

            Map<String,String> placeholders = new HashMap<>();
            placeholders.put("color",String.valueOf(params.get("color").toLowerCase())); // Will replace ${color}

            return getRandomResponse(placeholders,event.getPlayer());
        }
    }
     
    [​IMG]
    [​IMG]

    Example: Entities + paramters
    First I created an entity "settingkey" that checks for the "type" of setting you want to change
    In this case "prefix" or "suffix"
    [​IMG]
    I then created an intent with some examples for the key and value
    [​IMG]
    Code (Text):

    package be.maximvdw.qaplugin.ai;

    import be.maximvdw.qaplugin.QuestionManager;
    import be.maximvdw.qaplugin.api.AIComponent;
    import be.maximvdw.qaplugin.api.AIQuestionEvent;

    import java.util.HashMap;
    import java.util.Map;

    /**
    * ChangeSettingsComponent
    *
    * Asks the bot to change its prefix/suffix
    *
    * Created by maxim on 24-Dec-16.
    */
    public class ChangeSettingsComponent extends AIComponent {

        public ChangeSettingsComponent() {
            // This is the name you give to your action
            // it is the name that you will have to fill in under "action" in api.ai
            super("qaplugin_changesettings");
            // Here you can define possible responses
            // ${....} are custom placeholders you can fill in yourself
            addResponse("The setting ${setting} has been changed to \"${value}\"");
            addResponse("The new value for ${setting} is \"${value}\"");
            addResponse("I know use \"${value}\" as the ${setting}!");
        }

        /**
         * Called every time a response with your action name is returned
         * NOTE: The request is async
         *
         * @param event AIQuestionEvent even containing all data to parse the response
         * @return Always return a response, even error responses
         */
        @Override
        public String getResponse(final AIQuestionEvent event) {
            // These are the parsed arguments of the question
            // In this case it should contain the color
            Map<String,String> params = event.getParameters();
            if (!params.containsKey("key")){
                return "Oeps, I don't know what setting to change";
            }
            if (!params.containsKey("value")){
                return "Oeps, I don't know what setting to change it to";
            }

            switch (params.get("key").toLowerCase()){
                case "prefix":
                    QuestionManager.getInstance().setAnswerPrefix(params.get("value"));
                    break;
                case "suffix":
                    QuestionManager.getInstance().setAnswerSuffix(params.get("value"));
                    break;
                default:
                    return "Unknown setting!";
            }

            Map<String,String> placeholders = new HashMap<>();
            placeholders.put("setting",String.valueOf(params.get("key"))); // Will replace ${setting}
            placeholders.put("value",String.valueOf(params.get("value"))); // Will replace ${value}

            return getRandomResponse(placeholders,event.getPlayer());
        }
    }
     
    [​IMG]

  • Loading...
  • Loading...