Package games.stendhal.server.entity.npc.condition

Source Code of games.stendhal.server.entity.npc.condition.TriggerExactlyInListCondition

/* $Id: TriggerExactlyInListCondition.java,v 1.8 2011/05/01 19:50:05 martinfuchs Exp $ */
/***************************************************************************
*                   (C) Copyright 2003-2010 - Stendhal                    *
***************************************************************************
***************************************************************************
*                                                                         *
*   This program is free software; you can redistribute it and/or modify  *
*   it under the terms of the GNU General Public License as published by  *
*   the Free Software Foundation; either version 2 of the License, or     *
*   (at your option) any later version.                                   *
*                                                                         *
***************************************************************************/
package games.stendhal.server.entity.npc.condition;

import games.stendhal.common.parser.ConvCtxForMatchingSource;
import games.stendhal.common.parser.ConversationContext;
import games.stendhal.common.parser.ConversationParser;
import games.stendhal.common.parser.Sentence;
import games.stendhal.common.parser.SimilarExprMatcher;
import games.stendhal.server.entity.Entity;
import games.stendhal.server.entity.npc.ChatCondition;
import games.stendhal.server.entity.player.Player;

import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;

/**
* Was one of these trigger phrases said exactly ignoring case? (Use with a ""-trigger in npc.add)
*/
public class TriggerExactlyInListCondition implements ChatCondition {
  private static final ConversationContext CONVERSION_CONTEXT = new ConvCtxForMatchingSource();

  private List<Sentence> triggers = new LinkedList<Sentence>();


  /**
   * Creates a new TriggerExactlyInListCondition.
   *
   * @param trigger list of triggers
   */
  public TriggerExactlyInListCondition(final String... trigger) {
    addTriggers(Arrays.asList(trigger));
  }

  /**
   * Creates a new TriggerExactlyInListCondition.
   *
   * @param triggers list of triggers
   */
  public TriggerExactlyInListCondition(final List<String> triggers) {
    addTriggers(triggers);
  }

  private void addTriggers(Iterable<String> triggers) {
    SimilarExprMatcher matcher = new SimilarExprMatcher();
    for (String trigger : triggers) {
      final Sentence expected = ConversationParser.parse(trigger, matcher);
      this.triggers.add(expected);
    }
  }

  public boolean fire(final Player player, final Sentence sentence, final Entity entity) {

    // TODO: lowercase "and" at the beginning of a sentence is ignored, even in full match mode: "and the other gold"

    final Sentence answer = ConversationParser.parse(sentence.getOriginalText(), CONVERSION_CONTEXT);
    for (Sentence trigger : triggers) {
      if (answer.matchesFull(trigger)) {
        return true;
      }
    }
    return false;
  }

  @Override
  public String toString() {
    return "trigger exactly <" + triggers.toString() + ">";
  }

  @Override
  public int hashCode() {
    return HashCodeBuilder.reflectionHashCode(this);
  }

  @Override
  public boolean equals(final Object obj) {
    return EqualsBuilder.reflectionEquals(this, obj, false,  TriggerExactlyInListCondition.class);
  }
}
TOP

Related Classes of games.stendhal.server.entity.npc.condition.TriggerExactlyInListCondition

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.