Skip to content

Commit

Permalink
Java client: merge functions of StackableItem into Item
Browse files Browse the repository at this point in the history
No longer need to register StackableItem.class in EntityMap.
  • Loading branch information
AntumDeluge committed Oct 27, 2023
1 parent 86b9463 commit ee7b4cc
Show file tree
Hide file tree
Showing 9 changed files with 47 additions and 116 deletions.
19 changes: 18 additions & 1 deletion src/games/stendhal/client/entity/Item.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,19 @@ public class Item extends Entity {
*/
private RPSlot content;

/** Quantity property. */
public static final Property PROP_QUANTITY = new Property();
/** The item quantity. */
private int quantity;


/**
* Create an item.
*/
public Item() {
quantity = 0;
}

/**
* Initialize this entity for an object.
*
Expand Down Expand Up @@ -57,7 +70,7 @@ public RPSlot getContent() {
* @return The number of items.
*/
public int getQuantity() {
return 1;
return quantity;
}

/**
Expand All @@ -75,6 +88,10 @@ public void onChangedAdded(final RPObject object, final RPObject changes) {
if (changes.has("state")) {
fireChange(PROP_STATE);
}
if (changes.has("quantity")) {
quantity = changes.getInt("quantity");
fireChange(PROP_QUANTITY);
}
}

public int getState() {
Expand Down
73 changes: 0 additions & 73 deletions src/games/stendhal/client/entity/StackableItem.java

This file was deleted.

4 changes: 2 additions & 2 deletions src/games/stendhal/client/entity/UseableItem.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

/***************************************************************************
* (C) Copyright 2003 - Marauroa *
* (C) Copyright 2003-2023 - Marauroa *
***************************************************************************
***************************************************************************
* *
Expand All @@ -16,6 +16,6 @@
/**
* This is a useable stackable item.
*/
public class UseableItem extends StackableItem {
public class UseableItem extends Item {

}
13 changes: 0 additions & 13 deletions src/games/stendhal/client/entity/factory/EntityMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@
import games.stendhal.client.entity.SheepFood;
import games.stendhal.client.entity.Sign;
import games.stendhal.client.entity.Spell;
import games.stendhal.client.entity.StackableItem;
import games.stendhal.client.entity.StatefulEntity;
import games.stendhal.client.entity.UseableItem;
import games.stendhal.client.entity.UseableRing;
Expand Down Expand Up @@ -77,30 +76,18 @@ public final class EntityMap {
private static void register() {
// item
register("item", null, null, Item.class);
register("item", "ammunition", null, StackableItem.class);
register("item", "box", null, Box.class);
register("item", "club", "wizard_staff", UseableItem.class);
register("item", "container", null, StackableItem.class);
register("item", "documents", "coupon", StackableItem.class);
register("item", "drink", null, UseableItem.class);
register("item", "flower", null, StackableItem.class);
register("item", "food", null, UseableItem.class);
register("item", "herb", null, StackableItem.class);
register("item", "jewellery", null, StackableItem.class);
register("item", "misc", null, StackableItem.class);
register("item", "misc", "bulb", UseableItem.class);
register("item", "misc", "seed", UseableItem.class);
register("item", "missile", null, StackableItem.class);
register("item", "money", null, StackableItem.class);
register("item", "resource", null, StackableItem.class);
register("item", "ring", null, UseableRing.class);
register("item", "ring", "emerald-ring", BreakableRing.class);
register("item", "scroll", null, UseableItem.class);
register("item", "special", null, StackableItem.class);
register("item", "special", "mithril clasp", Item.class);
register("item", "token", null, Item.class);
register("item", "tool", "foodmill", UseableItem.class);
register("item", "tool", "rope", StackableItem.class);
register("item", "tool", "scrolleraser", UseableItem.class);
register("item", "tool", "sugarmill", UseableItem.class);

Expand Down
10 changes: 5 additions & 5 deletions src/games/stendhal/client/gui/DragLayer.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import javax.swing.SwingUtilities;

import games.stendhal.client.entity.IEntity;
import games.stendhal.client.entity.StackableItem;
import games.stendhal.client.entity.Item;
import games.stendhal.client.gui.j2d.entity.Entity2DView;
import games.stendhal.client.gui.j2d.entity.EntityViewFactory;
import games.stendhal.client.gui.j2d.entity.Item2DView;
Expand Down Expand Up @@ -115,7 +115,7 @@ public static DragLayer get() {
*
* @param entity dragged entity
*/
@SuppressWarnings("rawtypes") // cannot cast from <IEntity> to <? extends StackableItem> in Java 5
@SuppressWarnings("rawtypes") // cannot cast from <IEntity> to <? extends Item> in Java 5
public void startDrag(IEntity entity) {
if (entity != null) {
Entity2DView<IEntity> dragged = (Entity2DView<IEntity>) EntityViewFactory.create(entity);
Expand Down Expand Up @@ -186,7 +186,7 @@ private void stopDrag(MouseEvent event) {
Point componentPoint = SwingUtilities.convertPoint(this, point, (Component) target);
if (showAmountChooser(event, entity)) {
// Delegate dropping to the amount chooser
DropAmountChooser chooser = new DropAmountChooser((StackableItem) entity, target, componentPoint);
DropAmountChooser chooser = new DropAmountChooser((Item) entity, target, componentPoint);
chooser.show((Component) target, componentPoint);
} else {
// Dropping everything
Expand All @@ -211,8 +211,8 @@ private void stopDrag(MouseEvent event) {
*/
private boolean showAmountChooser(MouseEvent event, IEntity entity) {
if (((event.getModifiersEx() & (InputEvent.CTRL_DOWN_MASK|InputEvent.META_DOWN_MASK)) != 0)
&& (entity instanceof StackableItem)) {
return ((StackableItem) entity).getQuantity() > 1;
&& (entity instanceof Item)) {
return ((Item) entity).getQuantity() > 1;
}
return false;
}
Expand Down
8 changes: 4 additions & 4 deletions src/games/stendhal/client/gui/DropAmountChooser.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/***************************************************************************
* (C) Copyright 2003-2010 - Stendhal *
* (C) Copyright 2003-2023 - Stendhal *
***************************************************************************
***************************************************************************
* *
Expand Down Expand Up @@ -33,7 +33,7 @@

import org.apache.log4j.Logger;

import games.stendhal.client.entity.StackableItem;
import games.stendhal.client.entity.Item;
import games.stendhal.client.gui.layout.SBoxLayout;

/**
Expand All @@ -45,7 +45,7 @@ class DropAmountChooser {
private static final int BORDER = 2;

/** Item to be dropped. */
private final StackableItem item;
private final Item item;
/** Target where the user is dropping the item. */
private final DropTarget target;
/** Drop location within the target component. */
Expand All @@ -63,7 +63,7 @@ class DropAmountChooser {
* an amount greater than 0
* @param point drop location
*/
DropAmountChooser(StackableItem item, DropTarget target, Point point) {
DropAmountChooser(Item item, DropTarget target, Point point) {
this.item = item;
this.target = target;
location = point;
Expand Down
3 changes: 1 addition & 2 deletions src/games/stendhal/client/gui/j2d/entity/Item2DView.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import games.stendhal.client.entity.IEntity;
import games.stendhal.client.entity.Inspector;
import games.stendhal.client.entity.Item;
import games.stendhal.client.entity.StackableItem;
import games.stendhal.client.gui.InternalWindow;
import games.stendhal.client.gui.InternalWindow.CloseListener;
import games.stendhal.client.gui.SlotWindow;
Expand Down Expand Up @@ -215,7 +214,7 @@ void entityChanged(final Object property) {

if (property == IEntity.PROP_CLASS || property == IEntity.PROP_STATE) {
representationChanged = true;
} else if (property == StackableItem.PROP_QUANTITY) {
} else if (property == Item.PROP_QUANTITY) {
quantityChanged = true;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@
import java.util.List;

import games.stendhal.client.entity.ActionType;
import games.stendhal.client.entity.StackableItem;
import games.stendhal.client.entity.Item;
import games.stendhal.client.gui.styled.cursor.StendhalCursor;
import games.stendhal.client.gui.wt.core.WtWindowManager;
import marauroa.common.game.RPObject;

/**
* The 2D view of a useable item.
*/
class UseableItem2DView extends Item2DView<StackableItem> {
class UseableItem2DView extends Item2DView<Item> {

//
// Entity2DView
Expand Down
29 changes: 15 additions & 14 deletions tests/games/stendhal/client/entity/factory/EntityFactoryTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/***************************************************************************
* (C) Copyright 2003-2022 - Stendhal *
* (C) Copyright 2003-2023 - Stendhal *
***************************************************************************
***************************************************************************
* *
Expand Down Expand Up @@ -37,7 +37,6 @@
import games.stendhal.client.entity.Sheep;
import games.stendhal.client.entity.SheepFood;
import games.stendhal.client.entity.Sign;
import games.stendhal.client.entity.StackableItem;
import games.stendhal.client.entity.StatefulEntity;
import games.stendhal.client.entity.UseableItem;
import games.stendhal.server.maps.MockStendlRPWorld;
Expand Down Expand Up @@ -271,6 +270,8 @@ public final void item() {

@Test
public final void stackableItem() {
// stackable items are now handled in the `Item` class

RPObject rp = new MockRPObject("item", "drink");

IEntity en = EntityFactory.createEntity(rp);
Expand All @@ -290,28 +291,28 @@ public final void stackableItem() {
rp = new MockRPObject("item", "herb");
en = EntityFactory.createEntity(rp);
assertNotNull("entity should be created", en);
assertEquals("we should have created a StackableItem by now",
StackableItem.class, en.getClass());
assertEquals("we should have created a Item by now",
Item.class, en.getClass());
rp = new MockRPObject("item", "misc");
en = EntityFactory.createEntity(rp);
assertNotNull("entity should be created", en);
assertEquals("we should have created a StackableItem by now",
StackableItem.class, en.getClass());
assertEquals("we should have created a Item by now",
Item.class, en.getClass());
rp = new MockRPObject("item", "money");
en = EntityFactory.createEntity(rp);
assertNotNull("entity should be created", en);
assertEquals("we should have created a StackableItem by now",
StackableItem.class, en.getClass());
assertEquals("we should have created a Item by now",
Item.class, en.getClass());
rp = new MockRPObject("item", "ammunition");
en = EntityFactory.createEntity(rp);
assertNotNull("entity should be created", en);
assertEquals("we should have created a StackableItem by now",
StackableItem.class, en.getClass());
assertEquals("we should have created a Item by now",
Item.class, en.getClass());
rp = new MockRPObject("item", "resource");
en = EntityFactory.createEntity(rp);
assertNotNull("entity should be created", en);
assertEquals("we should have created a StackableItem by now",
StackableItem.class, en.getClass());
assertEquals("we should have created a Item by now",
Item.class, en.getClass());
rp = new MockRPObject("item", "scroll");
en = EntityFactory.createEntity(rp);
assertNotNull("entity should be created", en);
Expand All @@ -320,8 +321,8 @@ public final void stackableItem() {
rp = new MockRPObject("item", "jewellery");
en = EntityFactory.createEntity(rp);
assertNotNull("entity should be created", en);
assertEquals("we should have created a StackableItem by now",
StackableItem.class, en.getClass());
assertEquals("we should have created a Item by now",
Item.class, en.getClass());

}

Expand Down

0 comments on commit ee7b4cc

Please sign in to comment.