Package games.stendhal.server.entity.npc.behaviour.journal

Source Code of games.stendhal.server.entity.npc.behaviour.journal.ProducerRegister

/* $Id: ProducerRegister.java,v 1.12 2011/04/02 15:44:19 kymara 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.behaviour.journal;

import games.stendhal.common.grammar.Grammar;
import games.stendhal.server.core.engine.SingletonRepository;
import games.stendhal.server.entity.item.Item;
import games.stendhal.server.entity.npc.behaviour.impl.ProducerBehaviour;
import games.stendhal.server.entity.player.Player;

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

import marauroa.common.Pair;

public class ProducerRegister {
 
  private static ProducerRegister instance;
 
  private final List<Pair<String, ProducerBehaviour>> producers;
 
  public static ProducerRegister get() {
    if (instance == null) {
      new ProducerRegister();
    }
    return instance;
  }

  protected ProducerRegister() {
    instance = this;
    producers  = new LinkedList<Pair<String, ProducerBehaviour>>();
  }
 
  /**
   * Adds an NPC to the NPCList. Does nothing if an NPC with the same name
   * already exists. This makes sure that each NPC can be uniquely identified
   * by his/her name.
   *
   * @param npcName
   *            The NPC that should be added
   * @param behaviour  
   *            The ProducerBehaviour of that NPC
   */
  public void add(final String npcName, final ProducerBehaviour behaviour) {
    // insert lower case names ?
    // final String name = npcName.toLowerCase();
    Pair<String, ProducerBehaviour> pair = new Pair<String, ProducerBehaviour>(npcName, behaviour);
    producers.add(pair);
  }
 
  public List<Pair<String, ProducerBehaviour>> getProducers() {
    return producers;
  }
 
 
  public String listWorkingProducers(final Player player) {
    final StringBuilder sb = new StringBuilder("");

    // Open orders - do not state if ready to collect or not, yet

    for (final Pair<String, ProducerBehaviour> producer : producers) {
      String npcName = producer.first();
      ProducerBehaviour behaviour = producer.second();
      String questSlot =  behaviour.getQuestSlot();
      String activity =  behaviour.getProductionActivity();
      String product =  behaviour.getProductName();
      if (player.hasQuest(questSlot) && !player.isQuestCompleted(questSlot)) {
        int amount = behaviour.getNumberOfProductItems(player);
        if (behaviour.isOrderReady(player)) {
          // put all completed orders first - player wants to collect these!
          sb.insert(0,"\n" + npcName + " has finished " + Grammar.gerundForm(activity)
              + " your " + Grammar.plnoun(amount,product) + ".");
        } else {
          String timeleft = behaviour.getApproximateRemainingTime(player);
          // put all ongoing orders last
          sb.append("\n" + npcName + " is " + Grammar.gerundForm(activity)
              + " " + Grammar.quantityplnoun(amount, product, "a") + " and will be ready in " + timeleft + ".");
        }
           
      }
    }

    if (!"".equals(sb.toString())) {
      sb.insert(0,"\r\nOrders: ");
    } else {
      sb.append("You have no ongoing or uncollected orders.");
    }
    return sb.toString();
  }
 
  public List<String> getWorkingProducerNames(final Player player) {
    List<String> res = new LinkedList<String>();

    for (final Pair<String, ProducerBehaviour> producer : producers) {
      String npcName = producer.first();
      ProducerBehaviour behaviour = producer.second();
      String questSlot =  behaviour.getQuestSlot();
      if (player.hasQuest(questSlot) && !player.isQuestCompleted(questSlot)) {
        if (behaviour.isOrderReady(player)) {
          // put all completed orders first - player wants to collect these!
          res.add(0, npcName);
        } else {
          res.add(npcName);
        }
           
      }
    }

    return res;
  }
 
  /**
   * gets description of the production
   *
   * @param player player to get the description for
   * @param npcName name of quest
   * @return details
   */
  public String getProductionDescription(final Player player, final String npcName) {
    for (final Pair<String, ProducerBehaviour> producer : producers) {
      if(npcName.equals(producer.first())) {
        ProducerBehaviour behaviour = producer.second();
        String activity =  behaviour.getProductionActivity();
        String product =  behaviour.getProductName();
        return npcName + " " + Grammar.plural(activity) + " " + Grammar.plural(product) + ".";
      }
    }
    return "";
  }
       
  /**
   * gets details on the progress of the production
   *
   * @param player player to get the details for
   * @param npcName name of quest
   * @return details
   */
  public List<String> getProductionDetails(final Player player, final String npcName) {
    List<String> res = new LinkedList<String>();
    for (final Pair<String, ProducerBehaviour> producer : producers) {
      if(npcName.equals(producer.first())) {
        ProducerBehaviour behaviour = producer.second();
        String questSlot =  behaviour.getQuestSlot();
        String activity =  behaviour.getProductionActivity();
        String product =  behaviour.getProductName();
        if (player.hasQuest(questSlot) && !player.isQuestCompleted(questSlot)) {
          int amount = behaviour.getNumberOfProductItems(player);
          if (behaviour.isOrderReady(player)) {
            // put all completed orders first - player wants to collect these!
            res.add(npcName + " has finished " + Grammar.gerundForm(activity)
              + " your " + Grammar.plnoun(amount,product) + ".");
          } else {
            String timeleft = behaviour.getApproximateRemainingTime(player);
            // put all ongoing orders last
            res.add("\n" + npcName + " is " + Grammar.gerundForm(activity)
              + " " + Grammar.quantityplnoun(amount, product, "a") + " and will be ready in " + timeleft + ".");
          }
           
        }
      }
    }
    return res;
  }
 
  /**
   * gets names of all items produced, which are of the given item class (i.e. food, drink)
   *
   * @param clazz Item class to check
   * @return list of item names
   */
  public List<String> getProducedItemNames(final String clazz) {
    List<String> res = new LinkedList<String>();
    for (final Pair<String, ProducerBehaviour> producer : producers) {
      final ProducerBehaviour behaviour = producer.second();
      final String product =  behaviour.getProductName();
      final Item item = SingletonRepository.getEntityManager().getItem(product);
      if (item.isOfClass(clazz)) {
        res.add(product);
      }
    }
    return res;
  }
 
  /**
   * gets description of the produced item
   *
   * Note: if more than one NPC makes the item, just the details of the first NPC in the list who makes it are returned
   *
   * @param itemName produced item
   * @return details about the produced item
   */
  public String getProducedItemDetails(final String itemName) {
    for (final Pair<String, ProducerBehaviour> producer : producers) {
      ProducerBehaviour behaviour = producer.second();
      String product =  behaviour.getProductName();

      if(itemName.equals(product)) {
        String npcName = producer.first();
        String activity =  behaviour.getProductionActivity();
        String resources = behaviour.getRequiredResourceNames(1);
        return  npcName + " " + Grammar.plural(activity) + " " + Grammar.plural(product) + ", which need " + resources + " each.";
      }
    }
    return "";
  }
 
}

TOP

Related Classes of games.stendhal.server.entity.npc.behaviour.journal.ProducerRegister

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.