From: Astrid S. <ast...@us...> - 2008-09-08 21:42:46
|
Update of /cvsroot/arianne/stendhal/src/games/stendhal/server/entity/item/timed In directory sc8-pr-cvs11.sourceforge.net:/tmp/cvs-serv5703/src/games/stendhal/server/entity/item/timed Modified Files: TimedStackableItem.java Log Message: XPairtise Session: "stackableItem" Last driver: durkham Last navigator: madmetzger Index: TimedStackableItem.java =================================================================== RCS file: /cvsroot/arianne/stendhal/src/games/stendhal/server/entity/item/timed/TimedStackableItem.java,v retrieving revision 1.8 retrieving revision 1.9 diff -C2 -d -r1.8 -r1.9 *** TimedStackableItem.java 12 Jul 2008 14:43:56 -0000 1.8 --- TimedStackableItem.java 8 Sep 2008 21:42:54 -0000 1.9 *************** *** 1,133 **** ! package games.stendhal.server.entity.item.timed; ! ! import games.stendhal.common.Grammar; ! import games.stendhal.server.core.engine.SingletonRepository; ! import games.stendhal.server.core.events.TurnNotifier; ! import games.stendhal.server.core.events.UseListener; ! import games.stendhal.server.entity.Entity; ! import games.stendhal.server.entity.RPEntity; ! import games.stendhal.server.entity.item.Stackable; ! import games.stendhal.server.entity.item.StackableItem; ! import games.stendhal.server.entity.player.Player; ! ! import java.lang.ref.WeakReference; ! import java.util.Map; ! ! import marauroa.common.Log4J; ! import marauroa.common.Logger; ! import marauroa.common.game.RPObject; ! ! /** ! * Abstract base class for a stackable timed item. Extend this class and ! * implement methods useItem(Player) and itemFinished(Player). ! * ! * @author johnnnny ! */ ! public abstract class TimedStackableItem extends StackableItem implements ! UseListener { ! ! private static Logger logger = Log4J.getLogger(TimedStackableItem.class); ! ! private WeakReference<Player> player; ! ! /** ! * Creates a TimedItem. ! * ! * @param name ! * @param clazz ! * @param subclass ! * @param attributes ! */ ! public TimedStackableItem(final String name, final String clazz, final String subclass, ! final Map<String, String> attributes) { ! super(name, clazz, subclass, attributes); ! } ! ! /** ! * copy constructor. ! * ! * @param item ! * item to copy ! */ ! public TimedStackableItem(final TimedStackableItem item) { ! super(item); ! } ! ! public boolean onUsed(final RPEntity user) { ! RPObject base = this; ! boolean result = false; ! ! /* Find the top container */ ! while (base.isContained()) { ! base = base.getContainer(); ! } ! ! if (user.nextTo((Entity) base)) { ! if (useItem((Player) user)) { ! /* set the timer for the duration */ ! final TurnNotifier notifier = SingletonRepository.getTurnNotifier(); ! notifier.notifyInTurns(getAmount(), this); ! player = new WeakReference<Player>((Player) user); ! this.removeOne(); ! user.notifyWorldAboutChanges(); ! } ! result = true; ! } else { ! user.sendPrivateText(getTitle() + " is too far away"); ! logger.debug(getTitle() + " is too far away"); ! } ! ! return result; ! } ! ! @Override ! public void onTurnReached(final int currentTurn) { ! itemFinished(player.get()); ! } ! ! @Override ! public String describe() { ! String text = "You see " + Grammar.a_noun(getTitle()) + "."; ! if (hasDescription()) { ! text = getDescription(); ! } ! ! final String boundTo = getBoundTo(); ! ! if (isBound()) { ! text = text + " It is a special quest reward for " + boundTo ! + ", and cannot be used by others."; ! } ! ! return text; ! } ! ! @Override ! public boolean isStackable(final Stackable other) { ! return (other.getClass() == this.getClass()); ! } ! ! /** ! * Get the length of the timed event in turns. ! * ! * @return length in turns ! */ ! public int getAmount() { ! return getInt("amount"); ! } ! ! /** ! * Called when the player uses the item. Implement this in a subclass. ! * ! * @param player ! * @return true if the usage is successful ! */ ! public abstract boolean useItem(Player player); ! ! /** ! * Called when the used item is finished. Implement this in a subclass. ! * ! * @param player ! */ ! public abstract void itemFinished(Player player); ! } --- 1,127 ---- ! package games.stendhal.server.entity.item.timed; ! ! import games.stendhal.common.Grammar; ! import games.stendhal.server.core.engine.SingletonRepository; ! import games.stendhal.server.core.events.TurnNotifier; ! import games.stendhal.server.core.events.UseListener; ! import games.stendhal.server.entity.Entity; ! import games.stendhal.server.entity.RPEntity; ! import games.stendhal.server.entity.item.StackableItem; ! import games.stendhal.server.entity.player.Player; ! ! import java.lang.ref.WeakReference; ! import java.util.Map; ! ! import marauroa.common.Log4J; ! import marauroa.common.Logger; ! import marauroa.common.game.RPObject; ! ! /** ! * Abstract base class for a stackable timed item. Extend this class and ! * implement methods useItem(Player) and itemFinished(Player). ! * ! * @author johnnnny ! */ ! public abstract class TimedStackableItem extends StackableItem implements ! UseListener { ! ! private static Logger logger = Log4J.getLogger(TimedStackableItem.class); ! ! private WeakReference<Player> player; ! ! /** ! * Creates a TimedItem. ! * ! * @param name ! * @param clazz ! * @param subclass ! * @param attributes ! */ ! public TimedStackableItem(final String name, final String clazz, final String subclass, ! final Map<String, String> attributes) { ! super(name, clazz, subclass, attributes); ! } ! ! /** ! * copy constructor. ! * ! * @param item ! * item to copy ! */ ! public TimedStackableItem(final TimedStackableItem item) { ! super(item); ! } ! ! public boolean onUsed(final RPEntity user) { ! RPObject base = this; ! boolean result = false; ! ! /* Find the top container */ ! while (base.isContained()) { ! base = base.getContainer(); ! } ! ! if (user.nextTo((Entity) base)) { ! if (useItem((Player) user)) { ! /* set the timer for the duration */ ! final TurnNotifier notifier = SingletonRepository.getTurnNotifier(); ! notifier.notifyInTurns(getAmount(), this); ! player = new WeakReference<Player>((Player) user); ! this.removeOne(); ! user.notifyWorldAboutChanges(); ! } ! result = true; ! } else { ! user.sendPrivateText(getTitle() + " is too far away"); ! logger.debug(getTitle() + " is too far away"); ! } ! ! return result; ! } ! ! @Override ! public void onTurnReached(final int currentTurn) { ! itemFinished(player.get()); ! } ! ! @Override ! public String describe() { ! String text = "You see " + Grammar.a_noun(getTitle()) + "."; ! if (hasDescription()) { ! text = getDescription(); ! } ! ! final String boundTo = getBoundTo(); ! ! if (isBound()) { ! text = text + " It is a special quest reward for " + boundTo ! + ", and cannot be used by others."; ! } ! ! return text; ! } ! ! /** ! * Get the length of the timed event in turns. ! * ! * @return length in turns ! */ ! public int getAmount() { ! return getInt("amount"); ! } ! ! /** ! * Called when the player uses the item. Implement this in a subclass. ! * ! * @param player ! * @return true if the usage is successful ! */ ! public abstract boolean useItem(Player player); ! ! /** ! * Called when the used item is finished. Implement this in a subclass. ! * ! * @param player ! */ ! public abstract void itemFinished(Player player); ! } |