Skip to content

Commit

Permalink
feat: Add isStackable utility method (#11)
Browse files Browse the repository at this point in the history
* feat: Add `isStackable` utility method
* feat: Add convenience methods for EntityRef and Prefab
* chore: Use `isStackable` instead of computing it manually
  • Loading branch information
skaldarnar authored May 23, 2020
1 parent 3715692 commit 9251201
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 22 deletions.
70 changes: 55 additions & 15 deletions src/main/java/org/terasology/logic/inventory/InventoryUtils.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2014 MovingBlocks
* Copyright 2020 MovingBlocks
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -17,6 +17,7 @@

import org.terasology.entitySystem.Component;
import org.terasology.entitySystem.entity.EntityRef;
import org.terasology.entitySystem.prefab.Prefab;
import org.terasology.logic.inventory.events.BeforeItemPutInInventory;
import org.terasology.logic.inventory.events.BeforeItemRemovedFromInventory;
import org.terasology.logic.inventory.events.InventorySlotChangedEvent;
Expand Down Expand Up @@ -91,15 +92,7 @@ public static boolean isSameItem(EntityRef item1, EntityRef item2) {
return false;
}

if (!isSameStackId(itemComp1, itemComp2)) {
return false;
}

if (!hasSameAttributes(item1, item2)) {
return false;
}

return true;
return isSameStackId(itemComp1, itemComp2) && hasSameAttributes(item1, item2);
}

private static boolean hasSameAttributes(EntityRef from, EntityRef to) {
Expand Down Expand Up @@ -135,12 +128,59 @@ private static boolean hasSameAttributes(EntityRef from, EntityRef to) {
return true;
}

private static boolean isSameStackId(ItemComponent item1, ItemComponent item2) {
if (item1.stackId == null || item1.stackId.isEmpty() || item2.stackId == null || item2.stackId.isEmpty()) {
return false;
}
/**
* Determine whether an {@link EntityRef} denotes a stackable item.
* <p>
* An entity describes a stackable item iff it has an {@link ItemComponent} for which {@link
* #isStackable(ItemComponent)} holds.
*
* @param entity the entity to check whether it describes a stackable item
* @return true iff the entity has an item component which is stackable
*/
public static boolean isStackable(EntityRef entity) {
return isStackable(entity.getComponent(ItemComponent.class));
}

return item1.stackId.equals(item2.stackId);
/**
* Determine whether a {@link Prefab} is a stackable item.
* <p>
* A prefab describes a stackable item iff it has an {@link ItemComponent} for which {@link
* #isStackable(ItemComponent)} holds.
*
* @param prefab the prefab to check whether it describes a stackable item
* @return true iff the prefab has an item component which is stackable
*/
public static boolean isStackable(Prefab prefab) {
return isStackable(prefab.getComponent(ItemComponent.class));
}

/**
* Determine whether an {@link ItemComponent} is stackable or not.
* <p>
* An item is <emph>stackable</emph> iff it has a non-empty {@link ItemComponent#stackId} and a {@link
* ItemComponent#maxStackSize} greater than one.
*
* @param item the item to check whether it is stackable
* @return true iff the item exists and is stackable
*/
public static boolean isStackable(ItemComponent item) {
return hasStackId(item) && item.maxStackSize > 1;
}

/**
* Determine whether an item has a non-empty {@link ItemComponent#stackId}.
* <p>
* A non-empty stack id is a necessity for an item to be stackable.
*
* @param item the item to check for a non-empty stack id
* @return true iff the item exists and has a non-empty stack id
*/
private static boolean hasStackId(ItemComponent item) {
return item != null && item.stackId != null && !item.stackId.isEmpty();
}

private static boolean isSameStackId(ItemComponent item1, ItemComponent item2) {
return hasStackId(item1) && hasStackId(item2) && item1.stackId.equals(item2.stackId);
}

private static boolean validateMove(EntityRef instigator, EntityRef from, int slotFrom, EntityRef to, int slotTo) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,8 @@
import org.terasology.world.block.loader.BlockFamilyDefinition;
import org.terasology.world.block.shapes.BlockShape;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Optional;
import java.util.Set;

@RegisterSystem
Expand Down Expand Up @@ -315,7 +312,7 @@ private int removeItem(Prefab prefab, String displayName, final int removalQuant
if (slotPrefab != null && slotPrefab.equals(prefab)) {
quantityLeft = removeObjectFromSlot(slot,
playerEntity,
!prefab.getComponent(ItemComponent.class).stackId.isEmpty(),
InventoryUtils.isStackable(prefab),
displayName,
quantityLeft);

Expand Down Expand Up @@ -353,4 +350,4 @@ private int removeBlock(BlockFamily blockFamily, String displayName, final int r

return removalQuantity - quantityLeft;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ private boolean tryAddItems(EntityRef entityRef,
logger.error("Failed to find ItemComponent for {}", uri);
return false;
}
if (component.stackId == null || component.stackId.length() == 0) {
if (!InventoryUtils.isStackable(component)) {
// Item is not stackable, one slot used per item
if (available >= quantity) {
for (int i = 0; i < quantity; ++i) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public void copyBlockInventory(OnBlockToItem event, EntityRef blockEntity) {
inventoryManager.switchItem(blockEntity, blockEntity, i, inventoryItem, i);
}
ItemComponent itemComponent = inventoryItem.getComponent(ItemComponent.class);
if (itemComponent != null && !itemComponent.stackId.isEmpty()) {
if (InventoryUtils.isStackable(itemComponent)) {
itemComponent.stackId = "";
inventoryItem.saveComponent(itemComponent);
}
Expand Down

0 comments on commit 9251201

Please sign in to comment.