Skip to content

Commit

Permalink
Simplify Inventory#add and make more use of Player#tryAddToInvOrDrop …
Browse files Browse the repository at this point in the history
…(#630)
  • Loading branch information
Litorom authored Jul 16, 2024
2 parents 18eb277 + 8ce5855 commit c21d581
Show file tree
Hide file tree
Showing 10 changed files with 116 additions and 134 deletions.
6 changes: 1 addition & 5 deletions src/client/java/minicraft/entity/furniture/DeathChest.java
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,7 @@ public void touchedBy(Entity other) {
if (other instanceof Player) {
Inventory playerInv = ((Player) other).getInventory();
for (Item i : inventory.getItems()) {
int total = 1;
if (i instanceof StackableItem) total = ((StackableItem) i).count;

int returned = playerInv.add(i);
if (returned < total) {
if (playerInv.add(i) != null) {
Game.notifications.add("Your inventory is full!");
return;
}
Expand Down
87 changes: 42 additions & 45 deletions src/client/java/minicraft/entity/mob/Player.java
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,8 @@ public Item remove(int idx) {
}

@Override
public int add(int slot, Item item) {
int res = super.add(slot, item);
public @Nullable Item add(Item item) {
Item res = super.add(item);
triggerTrigger();
return res;
}
Expand Down Expand Up @@ -492,16 +492,16 @@ public void tick() {
if (activeItem != null && (input.inputPressed("drop-one") || input.inputPressed("drop-stack"))) {
Item drop = activeItem.copy();

if (input.inputPressed("drop-one") && drop instanceof StackableItem && ((StackableItem) drop).count > 1) {
// Drop one from stack
((StackableItem) activeItem).count--;
((StackableItem) drop).count = 1;
} else {
if (!input.inputPressed("drop-stack") || !(drop instanceof StackableItem) || ((StackableItem) drop).count <= 1) {
activeItem = null; // Remove it from the "inventory"
if (isFishing) {
isFishing = false;
fishingTicks = maxFishingTicks;
}
} else {
// Drop one from stack
((StackableItem) activeItem).count--;
((StackableItem) drop).count = 1;
}

level.dropItem(x, y, drop);
Expand All @@ -515,15 +515,7 @@ public void tick() {
}

if ((input.inputPressed("menu") || input.inputPressed("craft")) && activeItem != null) {
int returned = inventory.add(0, activeItem);
if (activeItem instanceof StackableItem) {
StackableItem stackable = (StackableItem) activeItem;
if (stackable.count > 0) {
getLevel().dropItem(x, y, stackable.copy());
}
} else if (returned <= 0) {
getLevel().dropItem(x, y, activeItem);
}
tryAddToInvOrDrop(activeItem);

activeItem = null;
if (isFishing) {
Expand Down Expand Up @@ -587,14 +579,7 @@ public void tick() {
public void resolveHeldItem() {
if (!(activeItem instanceof PowerGloveItem)) { // If you are now holding something other than a power glove...
if (prevItem != null) { // and you had a previous item that we should care about...
int returned = inventory.add(0, prevItem); // Then add that previous item to your inventory so it isn't lost.
if (prevItem instanceof StackableItem) {
if (((StackableItem) prevItem).count > 0) {
getLevel().dropItem(x, y, prevItem.copy());
}
} else if (returned == 0) {
getLevel().dropItem(x, y, prevItem);
}
tryAddToInvOrDrop(prevItem); // Then add that previous item to your inventory so it isn't lost.
} // If something other than a power glove is being held, but the previous item is null, then nothing happens; nothing added to inventory, and current item remains as the new one.
} else
activeItem = prevItem; // Otherwise, if you're holding a power glove, then the held item didn't change, so we can remove the power glove and make it what it was before.
Expand Down Expand Up @@ -999,24 +984,41 @@ public void render(Screen screen) {
}
}

/**
* What happens when the player interacts with a itemEntity
*/
/** What happens when the player interacts with a itemEntity */
public void pickupItem(ItemEntity itemEntity) {
int picked = 0;
int total = 1;
boolean successful = false; // If there is any item successfully added to the player
boolean remove = false; // Whether to remove the item entity (when empty)
if (itemEntity.item instanceof StackableItem && ((StackableItem) itemEntity.item).stacksWith(activeItem)) { // Picked up item equals the one in your hand
((StackableItem) activeItem).count += ((StackableItem) itemEntity.item).count;
picked = ((StackableItem) itemEntity.item).count;
} else {
if (itemEntity.item instanceof StackableItem) total = ((StackableItem) itemEntity.item).count;
picked = inventory.add(itemEntity.item); // Add item to inventory
int toAdd = Math.min(((StackableItem) activeItem).count + ((StackableItem) itemEntity.item).count, ((StackableItem) activeItem).maxCount)
- ((StackableItem) activeItem).count;
if (toAdd > 0) {
((StackableItem) activeItem).count += toAdd;
((StackableItem) itemEntity.item).count -= toAdd;
successful = true;
}
if (((StackableItem) itemEntity.item).count == 0) { // Empty
remove = true; // Remove the item entity
}
}

if (picked == total) {
Sound.play("pickup");
if (!(itemEntity.item instanceof StackableItem && ((StackableItem) itemEntity.item).count == 0)) {
// Add item to inventory
Item remaining;
if (itemEntity.item instanceof StackableItem) {
int orig = ((StackableItem) itemEntity.item).count;
remaining = inventory.add(itemEntity.item);
if (remaining != null && ((StackableItem) remaining).count != orig) {
successful = true;
}
} else remaining = inventory.add(itemEntity.item);
if (remaining == null) {
successful = remove = true;
}
}

itemEntity.remove();
if (remove) itemEntity.remove();
if (successful) {
Sound.play("pickup");
addScore(1);
}
}
Expand Down Expand Up @@ -1243,17 +1245,12 @@ public String getDebugHunger() {
}

/**
* Trying to add item(s) to the player inventory.
* If no more item(s) can be added to the inventory, drop the item(s) near the player.
* Trying to add a stack of item(s) to the top of player inventory.
* If there is/are no more item(s) can be added to the inventory, drop the item(s) near the player.
*/
public void tryAddToInvOrDrop(@Nullable Item item) {
if (item != null) {
int returned = inventory.add(0, item);
if (item instanceof StackableItem) {
if (((StackableItem) item).count > 0) {
getLevel().dropItem(x, y, item);
}
} else if (returned == 0) {
if (inventory.add(item) != null) {
getLevel().dropItem(x, y, item);
}
}
Expand Down
4 changes: 1 addition & 3 deletions src/client/java/minicraft/item/BucketItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,7 @@ private BucketItem editBucket(Player player, Fill newFill) {

// This item object is a stack of buckets.
count--;
if (player.getInventory().add(new BucketItem(newFill)) == 0) {
player.getLevel().dropItem(player.x, player.y, new BucketItem(newFill));
}
player.tryAddToInvOrDrop(new BucketItem(newFill));
return this;
}

Expand Down
67 changes: 28 additions & 39 deletions src/client/java/minicraft/item/Inventory.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package minicraft.item;

import minicraft.util.Logging;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
Expand Down Expand Up @@ -51,99 +52,87 @@ public Item remove(int idx) {
return items.remove(idx);
}

/**
* Adds an item to the inventory
*/
public int add(@Nullable Item item) {
if (item != null)
return add(items.size(), item); // Adds the item to the end of the inventory list
return 0;
}

/**
* Adds several copies of the same item to the end of the inventory.
* @param item Item to be added.
* @param num Amount of items to add.
* @return the remaining item not being added; empty if whole stack of items has been added successfully
*/
public int add(Item item, int num) {
int total = 0;
for (int i = 0; i < num; i++)
total += add(item.copy());
return total;
public List<Item> add(@NotNull Item item, int num) {
ArrayList<Item> remaining = new ArrayList<>();
for (int i = 0; i < num; i++) {
Item remain = add(item.copy());
if (remain != null) remaining.add(remain);
}
return remaining;
}

/**
* Adds an item to a specific spot in the inventory.
* @param slot Index to place item at.
* Adds an item at the end of the inventory.
* @param item Item to be added.
* @return The number of items added.
* @return the remaining item not being added; {@code null} if whole stack of items has been added successfully
*/
public int add(int slot, Item item) {

public @Nullable Item add(@Nullable Item item) {
if (item == null) return null;
// Do not add to inventory if it is a PowerGlove
if (item instanceof PowerGloveItem) {
Logging.INVENTORY.warn("Tried to add power glove to inventory. stack trace:", new Exception());
return 0;
return null;
}

if (item instanceof StackableItem) { // If the item is a item...
StackableItem toTake = (StackableItem) item; // ...convert it into a StackableItem object.
int total = toTake.count;

for (Item value : items) {
if (toTake.stacksWith(value)) {
StackableItem stack = (StackableItem) value;

if (!unlimited) {
if (stack.count < stack.maxCount) {
int r = stack.maxCount - stack.count;
if (r >= toTake.count) {
// Matching implies that the other item is stackable, too.
stack.count += toTake.count;
return total;
return null;
} else {
toTake.count -= r;
stack.count += r;
}
}
} else {
stack.count += toTake.count;
return total;
return null;
}
}
}

if (!unlimited) {
if (items.size() < maxItem) {
int c = (int) Math.ceil(toTake.count / 100.0);
for (int i = 0; i < c; i++) {
while (toTake.count > 0) {
if (items.size() == maxItem) return toTake;
StackableItem adding = toTake.copy();
adding.count = i + 1 == c && toTake.count % 100 > 0 ? toTake.count % 100 : 100;
if (adding.count == 0) break;
if (items.size() == maxItem) return total - toTake.count;
adding.count = Math.min(toTake.count, toTake.maxCount);
items.add(adding); // Add the item to the items list
toTake.count -= adding.count;
}
return total;
return null;
} else {
return total - toTake.count;
return toTake;
}
} else {
items.add(slot, toTake);
return total;
items.add(toTake);
return null;
}
}

if (!unlimited) {
if (items.size() < maxItem) {
items.add(slot, item); // Add the item to the items list
return 1;
items.add(item); // Add the item to the items list
return null;
} else {
return 0;
return item;
}
} else {
items.add(slot, item);
return 1;
items.add(item);
return null;
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/client/java/minicraft/item/Recipe.java
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public boolean craft(Player player) {
// Rdd the crafted items.
for (int i = 0; i < amount; i++) {
Item product = getProduct();
if (player.getInventory().add(product) == 0)
if (player.getInventory().add(product) != null)
player.getLevel().dropItem(player.x, player.y, product);
}

Expand Down
6 changes: 1 addition & 5 deletions src/client/java/minicraft/saveload/LegacyLoad.java
Original file line number Diff line number Diff line change
Expand Up @@ -344,11 +344,7 @@ public void loadItemToInventory(String item, Inventory inventory) {
}

private void loadItem(Inventory inventory, Item item) {
int total = 1;
if (item instanceof StackableItem) total = ((StackableItem) item).count;
int loaded = inventory.add(item);

if (loaded < total) {
if (inventory.add(item) != null) {
deathChest.getInventory().add(item.copy());
}
}
Expand Down
8 changes: 2 additions & 6 deletions src/client/java/minicraft/saveload/Load.java
Original file line number Diff line number Diff line change
Expand Up @@ -829,7 +829,7 @@ public void loadPlayer(Player player, List<String> origData) {
if (worldVer.compareTo(new Version("2.0.4-dev7")) < 0) {
int arrowCount = Integer.parseInt(data.remove(0));
if (worldVer.compareTo(new Version("2.0.1-dev1")) < 0)
player.getInventory().add(Items.get("arrow"), arrowCount);
player.getInventory().add(Items.get("arrow"), arrowCount).forEach(deathChest.getInventory()::add);
}

Game.currentLevel = Integer.parseInt(data.remove(0));
Expand Down Expand Up @@ -995,11 +995,7 @@ public void loadInventory(Inventory inventory, List<String> data) {
}

private void loadItem(Inventory inventory, Item item) {
int total = 1;
if (item instanceof StackableItem) total = ((StackableItem) item).count;
int loaded = inventory.add(item);

if (loaded < total) {
if (inventory.add(item) != null) {
deathChest.getInventory().add(item.copy());
}
}
Expand Down
24 changes: 11 additions & 13 deletions src/client/java/minicraft/screen/ContainerDisplay.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,10 @@ public class ContainerDisplay extends Display {
private Chest chest;

public ContainerDisplay(Player player, Chest chest) {
super(
new InventoryMenu(player, player.getInventory(), "minicraft.display.menus.inventory", RelPos.LEFT),
new InventoryMenu(chest, chest.getInventory(), chest.name, RelPos.RIGHT)
);

//pInv = player.getInventory();
//cInv = chest.getInventory();
menus = new Menu[] {
new InventoryMenu(player, player.getInventory(), "minicraft.display.menus.inventory", RelPos.RIGHT, this::update),
new InventoryMenu(chest, chest.getInventory(), chest.name, RelPos.LEFT, this::update)
};
this.player = player;
this.chest = chest;

Expand Down Expand Up @@ -331,28 +328,29 @@ public void tick(InputHandler input) {
if (!transferAll) {
((StackableItem) toItem).count = 1;
} else {
move = ((StackableItem) fromItem).count;
move = ((StackableItem) toItem).count;
}

int moved = to.add(toSel, toItem);
if (moved < move) {
((StackableItem) fromItem).count -= moved;
if (to.add(toItem) != null) {
((StackableItem)fromItem).count -= move - ((StackableItem) toItem).count;
} else if (!transferAll) {
((StackableItem) fromItem).count--;
} else {
from.remove(fromSel);
}
update();
} else {
int moved = to.add(toSel, toItem);
if (moved == 1) {
if (to.add(toItem) == null) {
from.remove(fromSel);
update();
}
}
}
}

/** @deprecated This method is no longer in use by the removal of multiplayer system.
* Also, the game is paused when the display is shown, so it is not possible for the player to pickup items during this period. */
@Deprecated
public void onInvUpdate(ItemHolder holder) {
if (holder == player || holder == chest) {
update();
Expand Down
Loading

0 comments on commit c21d581

Please sign in to comment.