Package org.bat.client.plugins

Source Code of org.bat.client.plugins.TriggerPluginHandler

package org.bat.client.plugins;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.TreeMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.bat.client.persistence.HibernateUtil;
import org.bat.client.plugins.triggers.Chests;
import org.bat.client.plugins.triggers.MerchantServices;
import org.bat.client.plugins.triggers.PartyKills;
import org.bat.client.plugins.triggers.PartyReports;
import org.bat.client.plugins.triggers.ProtMessages;
import org.bat.client.plugins.triggers.ShowEffects;

import com.mythicscape.batclient.interfaces.BatClientPlugin;
import com.mythicscape.batclient.interfaces.BatClientPluginTrigger;
import com.mythicscape.batclient.interfaces.BatClientPluginUtil;
import com.mythicscape.batclient.interfaces.ParsedResult;

public class TriggerPluginHandler extends BatClientPlugin implements
    BatClientPluginTrigger, BatClientPluginUtil {

  private static TreeMap<Integer, HashSet<TriggerSet>> loadedTriggerSets;

  @Override
  public String getName() {
    return "TriggerPluginHandler";
  }

  @Override
  public void loadPlugin() {
    loadedTriggerSets = new TreeMap<Integer, HashSet<TriggerSet>>();
    addTriggerSet(new Integer(10), new ShowEffects(this));
    addTriggerSet(new Integer(10), new PartyKills(this));
    addTriggerSet(new Integer(20), new ProtMessages(this));
    addTriggerSet(new Integer(30), new PartyReports(this));
    addTriggerSet(new Integer(10), new MerchantServices(this));
    addTriggerSet(new Integer(90), new Chests(this));
    GagHandler.initGags();
    testFiles();
  }

  private void testFiles() {
    /* debugInfo("Trying to create some files");
    File test1 = new File(getBaseDirectory()+"/test1.conf");
    File test2 = new File("/test2.conf");
    try {
      if (!test1.exists()) {
        test1.createNewFile();
      }
      FileWriter test1Writer = new FileWriter(test1);
      test1Writer.write("Test 1\n");
      test1Writer.flush();
      test1Writer.close();

      if (!test2.exists()) {
        test2.createNewFile();
      }
      FileWriter test2Writer = new FileWriter(test2);
      test2Writer.write("Test 2\n");
      test2Writer.flush();
      test2Writer.close();

    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
      //try {
        debugInfo("Trying to connect to the database..");
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction tx = session.beginTransaction();
       
        MonsterHibernateDAO monsterDAO = new MonsterHibernateDAO();
        monsterDAO.setSession(session);
        List<Monster> allMonster = monsterDAO.findAll();
        for (Monster aMob : allMonster) {
          debugInfo("Monster "+aMob.getId()+": "+aMob.getShortDescription());
        }
        tx.commit();
      debugInfo("Connected.");
    //}  */
  }

  @Override
  public ParsedResult trigger(ParsedResult inputText) {
    ParsedResult result = null;
    try {
      String matchTrigger = inputText.getStrippedText().trim();
      for (Integer priority : loadedTriggerSets.keySet()) {
        HashSet<TriggerSet> triggersForPriority = loadedTriggerSets
            .get(priority);
        for (TriggerSet aTrigger : triggersForPriority) {
          if (aTrigger.isActive()) {
            // First we check the endPattern
            if (aTrigger.getEndPattern() != null) {
              Matcher endMatcher = aTrigger.getEndPattern()
                  .matcher(matchTrigger);
              if (endMatcher.matches()) {
                result = aTrigger.handleEndTrigger(endMatcher,
                    inputText);
                return result;
              }
            }
            // Loop over trigger patterns
            for (String triggerName : aTrigger.getTriggerPatterns()
                .keySet()) {
              Pattern trigPattern = aTrigger.getTriggerPatterns()
                  .get(triggerName);
              Matcher triggerMatcher = trigPattern
                  .matcher(matchTrigger);
              if (triggerMatcher.matches()) {
                result = aTrigger.handleTrigger(triggerName,
                    triggerMatcher, inputText);
              }

            }
          }
        }
      }
      if (GagHandler.gagLine(inputText)) {
        debugInfo("Gagging: " + inputText.getStrippedText().trim());
        result = inputText;
        result.setStrippedText("");
      }
    } catch (Throwable t) {
      debugInfo("EXCEPTION: " + t.getMessage());
      t.printStackTrace();
    }
    return result;
  }

  private void addTriggerSet(Integer priority, TriggerSet aTriggerSet) {
    if (!loadedTriggerSets.containsKey(priority)) {
      HashSet<TriggerSet> newSet = new HashSet<TriggerSet>();
      loadedTriggerSets.put(priority, newSet);
    }
    loadedTriggerSets.get(priority).add(aTriggerSet);
  }

  public void debugInfo(String debugText) {
    if (Settings.isTestMode()) {
      System.out.println("DEBUG: " + debugText);
    } else {
      System.out.println(debugText);
    }
  }

  public void doCommand(String commandText) {
    if (Settings.isTestMode()) {
      System.out.println("COMMAND: " + commandText);
    } else {
      this.getClientGUI().doCommand(commandText);
    }
  }

  public void doCommand(String commandText, ArrayList<String> args) {
    if (Settings.isTestMode()) {
      System.out.println("COMMAND: " + commandText);
      for (String arg : args) {
        System.out.println("ARGUMENT: " + arg);
      }
    } else {
      this.getClientGUI().doCommand(commandText, args);
    }
  }

  @Override
  public void clientExit() {
    HibernateUtil.getSessionFactory().close();
   
  }
}
TOP

Related Classes of org.bat.client.plugins.TriggerPluginHandler

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.