Skip to content

Commit

Permalink
Migrate slots
Browse files Browse the repository at this point in the history
  • Loading branch information
MrHell228 committed Sep 15, 2024
1 parent bd919dd commit 610ef28
Show file tree
Hide file tree
Showing 10 changed files with 56 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.spongepowered.api.item.inventory.EmptyInventory;
import org.spongepowered.api.item.inventory.Inventory;
import org.spongepowered.api.item.inventory.ItemStack;
import org.spongepowered.api.item.inventory.ItemStackLike;
import org.spongepowered.api.item.inventory.ItemStackSnapshot;
import org.spongepowered.api.item.inventory.Slot;
import org.spongepowered.api.item.inventory.query.Query;
Expand Down Expand Up @@ -105,12 +106,12 @@ public boolean hasChildren() {
}

@Override
public boolean contains(ItemStack stack) {
public boolean contains(ItemStackLike stack) {
return false;
}

@Override
public boolean containsAny(ItemStack stack) {
public boolean containsAny(ItemStackLike stack) {
return false;
}

Expand Down Expand Up @@ -193,7 +194,7 @@ public Inventory root() {
}

@Override
public InventoryTransactionResult offer(ItemStack... stacks) {
public InventoryTransactionResult offer(ItemStackLike... stacks) {
return InventoryTransactionResult.builder().type(Type.FAILURE).reject(stacks).build();
}

Expand All @@ -209,16 +210,16 @@ public Optional<ItemStack> peekAt(int index) {
return Optional.empty();
}

public InventoryTransactionResult offer(int index, ItemStack stack) {
public InventoryTransactionResult offer(int index, ItemStackLike stack) {
return InventoryTransactionResult.builder().type(Type.NO_SLOT).reject(stack).build();
}

public InventoryTransactionResult set(int index, ItemStack stack) {
public InventoryTransactionResult set(int index, ItemStackLike stack) {
return InventoryTransactionResult.builder().type(Type.NO_SLOT).reject(stack).build();
}

@Override
public boolean canFit(ItemStack stack) {
public boolean canFit(ItemStackLike stack) {
return false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.spongepowered.api.item.ItemType;
import org.spongepowered.api.item.inventory.Inventory;
import org.spongepowered.api.item.inventory.ItemStack;
import org.spongepowered.api.item.inventory.ItemStackLike;
import org.spongepowered.api.item.inventory.Slot;
import org.spongepowered.api.item.inventory.query.Query;
import org.spongepowered.api.item.inventory.query.QueryTypes;
Expand Down Expand Up @@ -97,17 +98,17 @@ default ItemStack peek() {
}

@Override
default InventoryTransactionResult offer(ItemStack... stacks) {
default InventoryTransactionResult offer(ItemStackLike... stacks) {
InventoryTransactionResult result = InventoryTransactionResult.successNoTransactions();
for (ItemStack stack : stacks) {
result = result.and(AdapterLogic.appendSequential(this.impl$getFabric(), this.impl$getLens(), stack));
for (ItemStackLike stack : stacks) {
result = result.and(AdapterLogic.appendSequential(this.impl$getFabric(), this.impl$getLens(), stack.asMutable()));
}
return result;
}

@Override
default boolean canFit(ItemStack stack) {
return AdapterLogic.canFit(this.impl$getFabric(), this.impl$getLens(), stack);
default boolean canFit(ItemStackLike stack) {
return AdapterLogic.canFit(this.impl$getFabric(), this.impl$getLens(), stack.asMutable());
}

@Override
Expand All @@ -131,13 +132,13 @@ default boolean hasChildren() {
}

@Override
default boolean contains(ItemStack stack) {
return AdapterLogic.contains(((InventoryBridge) this).bridge$getAdapter(), stack);
default boolean contains(ItemStackLike stack) {
return AdapterLogic.contains(((InventoryBridge) this).bridge$getAdapter(), stack.asMutable());
}

@Override
default boolean containsAny(ItemStack stack) {
return AdapterLogic.contains(((InventoryBridge) this).bridge$getAdapter(), stack, 1);
default boolean containsAny(ItemStackLike stack) {
return AdapterLogic.contains(((InventoryBridge) this).bridge$getAdapter(), stack.asMutable(), 1);
}

@Override
Expand Down Expand Up @@ -208,13 +209,13 @@ default Optional<ItemStack> peekAt(int index) {
}

@Override
default InventoryTransactionResult set(int index, ItemStack stack) {
return AdapterLogic.insertSequential(this.impl$getFabric(), this.impl$getLens().getSlotLens(this.impl$getFabric(), index), stack);
default InventoryTransactionResult set(int index, ItemStackLike stack) {
return AdapterLogic.insertSequential(this.impl$getFabric(), this.impl$getLens().getSlotLens(this.impl$getFabric(), index), stack.asMutable());
}

@Override
default InventoryTransactionResult offer(int index, ItemStack stack) {
return AdapterLogic.appendSequential(this.impl$getFabric(), this.impl$getLens().getSlotLens(this.impl$getFabric(), index), stack);
default InventoryTransactionResult offer(int index, ItemStackLike stack) {
return AdapterLogic.appendSequential(this.impl$getFabric(), this.impl$getLens().getSlotLens(this.impl$getFabric(), index), stack.asMutable());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.spongepowered.api.item.ItemType;
import org.spongepowered.api.item.inventory.Inventory;
import org.spongepowered.api.item.inventory.ItemStack;
import org.spongepowered.api.item.inventory.ItemStackLike;
import org.spongepowered.api.item.inventory.slot.FilteringSlot;
import org.spongepowered.api.item.inventory.transaction.InventoryTransactionResult;
import org.spongepowered.common.inventory.fabric.Fabric;
Expand All @@ -43,7 +44,7 @@ public FilteringSlotAdapter(Fabric fabric, FilteringSlotLens lens, Inventory par
}

@Override
public boolean isValidItem(ItemStack stack) {
public boolean isValidItem(ItemStackLike stack) {
FilteringSlotLens.ItemStackFilter filter = this.filteringSlot.getItemStackFilter();
return filter == null || filter.test(this.impl$getFabric(), stack);
}
Expand All @@ -68,11 +69,11 @@ public InventoryTransactionResult offer(ItemStack stack) {
*/

@Override
public InventoryTransactionResult set(ItemStack stack) {
public InventoryTransactionResult set(ItemStackLike stack) {
final boolean canSet = this.isValidItem(stack);
if (!canSet) {
final InventoryTransactionResult.Builder result = InventoryTransactionResult.builder().type(InventoryTransactionResult.Type.FAILURE);
result.reject(ItemStackUtil.cloneDefensive(stack));
result.reject(stack);
return result.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.spongepowered.api.item.ItemType;
import org.spongepowered.api.item.inventory.Inventory;
import org.spongepowered.api.item.inventory.ItemStack;
import org.spongepowered.api.item.inventory.ItemStackLike;
import org.spongepowered.api.item.inventory.equipment.EquipmentType;
import org.spongepowered.api.item.inventory.slot.EquipmentSlot;
import org.spongepowered.api.item.inventory.transaction.InventoryTransactionResult;
Expand All @@ -52,8 +53,8 @@ public boolean isValidItem(EquipmentType type) {
}

@Override
public boolean isValidItem(ItemStack stack) {
Predicate<ItemStack> filter = this.equipmentSlot.getItemStackFilter();
public boolean isValidItem(ItemStackLike stack) {
Predicate<ItemStackLike> filter = this.equipmentSlot.getItemStackFilter();
return filter == null || filter.test(stack);
}

Expand All @@ -64,11 +65,11 @@ public boolean isValidItem(ItemType type) {
}

@Override
public InventoryTransactionResult set(ItemStack stack) {
public InventoryTransactionResult set(ItemStackLike stack) {
final boolean canSet = this.isValidItem(stack);
if (!canSet) {
final InventoryTransactionResult.Builder result = InventoryTransactionResult.builder().type(InventoryTransactionResult.Type.FAILURE);
result.reject(ItemStackUtil.cloneDefensive(stack));
result.reject(stack);
return result.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.spongepowered.api.item.ItemTypes;
import org.spongepowered.api.item.inventory.Inventory;
import org.spongepowered.api.item.inventory.ItemStack;
import org.spongepowered.api.item.inventory.ItemStackLike;
import org.spongepowered.api.item.inventory.ItemStackSnapshot;
import org.spongepowered.api.item.inventory.Slot;
import org.spongepowered.api.item.inventory.transaction.InventoryTransactionResult;
Expand Down Expand Up @@ -89,11 +90,11 @@ public ItemStack peek() {
}

@Override
public InventoryTransactionResult offer(final ItemStack... stacks) {
public InventoryTransactionResult offer(final ItemStackLike... stacks) {

final InventoryTransactionResult.Builder result = InventoryTransactionResult.builder().type(InventoryTransactionResult.Type.SUCCESS);
for (ItemStack stack : stacks) {
final net.minecraft.world.item.ItemStack nativeStack = ItemStackUtil.toNative(stack);
for (ItemStackLike stack : stacks) {
final net.minecraft.world.item.ItemStack nativeStack = ItemStackUtil.fromLikeToNative(stack);

final int maxStackSize = this.slot.getMaxStackSize(this.inventoryAdapter$getFabric());
int remaining = stack.quantity();
Expand All @@ -105,7 +106,7 @@ public InventoryTransactionResult offer(final ItemStack... stacks) {
if (old.isEmpty() && this.slot.setStack(this.inventoryAdapter$getFabric(), ItemStackUtil.cloneDefensiveNative(nativeStack, push))) {
remaining -= push;
newStack = ItemStackUtil.snapshotOf(stack);
} else if (!old.isEmpty() && ItemStackUtil.compareIgnoreQuantity(old, stack) && maxStackSize > old.getCount()) {
} else if (!old.isEmpty() && ItemStackUtil.compareIgnoreQuantity(old, nativeStack) && maxStackSize > old.getCount()) {
this.inventoryAdapter$getFabric().fabric$markDirty();
push = Math.max(Math.min(maxStackSize - old.getCount(), remaining), 0); // max() accounts for oversized stacks
old.setCount(old.getCount() + push);
Expand All @@ -124,7 +125,7 @@ public InventoryTransactionResult offer(final ItemStack... stacks) {
}

@Override
public boolean canFit(final ItemStack stack) {
public boolean canFit(final ItemStackLike stack) {
if (stack.isEmpty()) {
return true;
}
Expand All @@ -133,13 +134,13 @@ public boolean canFit(final ItemStack stack) {
if (old.isEmpty()) {
return maxStackSize >= stack.quantity();
}
return ItemStackUtil.compareIgnoreQuantity(old, stack) && maxStackSize - old.getCount() >= stack.quantity();
return ItemStackUtil.compareIgnoreQuantity(old, stack.asMutable()) && maxStackSize - old.getCount() >= stack.quantity();
}

@Override
public InventoryTransactionResult set(final ItemStack stack) {
public InventoryTransactionResult set(final ItemStackLike stack) {
final InventoryTransactionResult.Builder result = InventoryTransactionResult.builder().type(InventoryTransactionResult.Type.SUCCESS);
final net.minecraft.world.item.ItemStack nativeStack = ItemStackUtil.toNative(stack);
final net.minecraft.world.item.ItemStack nativeStack = ItemStackUtil.fromLikeToNative(stack);

final net.minecraft.world.item.ItemStack old = this.slot.getStack(this.inventoryAdapter$getFabric());
ItemStackSnapshot oldSnap = ItemStackUtil.snapshotOf(old);
Expand Down Expand Up @@ -189,16 +190,16 @@ public boolean hasChildren() {
}

@Override
public boolean contains(final ItemStack stack) {
public boolean contains(final ItemStackLike stack) {
final net.minecraft.world.item.ItemStack slotStack = this.slot.getStack(this.inventoryAdapter$getFabric());
return slotStack.isEmpty() ? ItemStackUtil.toNative(stack).isEmpty() :
ItemStackUtil.compareIgnoreQuantity(slotStack, stack) && slotStack.getCount() >= stack.quantity();
return slotStack.isEmpty() ? ItemStackUtil.fromLikeToNative(stack).isEmpty() :
ItemStackUtil.compareIgnoreQuantity(slotStack, stack.asMutable()) && slotStack.getCount() >= stack.quantity();
}

@Override
public boolean containsAny(final ItemStack stack) {
public boolean containsAny(final ItemStackLike stack) {
final net.minecraft.world.item.ItemStack slotStack = this.slot.getStack(this.inventoryAdapter$getFabric());
return slotStack.isEmpty() ? ItemStackUtil.toNative(stack).isEmpty() : ItemStackUtil.compareIgnoreQuantity(slotStack, stack);
return slotStack.isEmpty() ? ItemStackUtil.fromLikeToNative(stack).isEmpty() : ItemStackUtil.compareIgnoreQuantity(slotStack, stack.asMutable());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import net.minecraft.world.Container;
import org.spongepowered.api.item.inventory.Inventory;
import org.spongepowered.api.item.inventory.ItemStack;
import org.spongepowered.api.item.inventory.ItemStackLike;
import org.spongepowered.api.item.inventory.Slot;
import org.spongepowered.common.bridge.world.inventory.InventoryBridge;
import org.spongepowered.common.inventory.adapter.impl.slots.FilteringSlotAdapter;
Expand Down Expand Up @@ -59,7 +60,7 @@ public Slot getAdapter(Fabric fabric, Inventory parent) {

@FunctionalInterface
public interface ItemStackFilter {
boolean test(Fabric fabric, ItemStack itemStack);
boolean test(Fabric fabric, ItemStackLike itemStack);

static ItemStackFilter filterNone() {
return (f, i) -> true;
Expand All @@ -69,7 +70,7 @@ static ItemStackFilter filterIInventory(int slot) {
return (fabric, item) -> {
InventoryBridge inventory = fabric.fabric$get(slot);
if (inventory instanceof Container) {
return ((Container) inventory).canPlaceItem(slot, ItemStackUtil.toNative(item));
return ((Container) inventory).canPlaceItem(slot, ItemStackUtil.fromLikeToNative(item));
}
return true;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.spongepowered.api.data.Key;
import org.spongepowered.api.item.ItemType;
import org.spongepowered.api.item.inventory.Inventory;
import org.spongepowered.api.item.inventory.ItemStackLike;
import org.spongepowered.api.item.inventory.Slot;
import org.spongepowered.api.item.inventory.equipment.EquipmentType;
import org.spongepowered.api.item.inventory.equipment.EquipmentTypes;
Expand Down Expand Up @@ -151,7 +152,7 @@ public Predicate<EquipmentType> getEquipmentTypeFilter() {
return (e) -> e == EquipmentTypes.MAINHAND.get();
}

public Predicate<org.spongepowered.api.item.inventory.ItemStack> getItemStackFilter() {
public Predicate<ItemStackLike> getItemStackFilter() {
return (i) -> true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ public static ItemStackSnapshot snapshotOf(net.minecraft.world.item.ItemStack it
return itemStack.isEmpty() ? ItemStackSnapshot.empty() : ItemStackUtil.fromNative(itemStack).asImmutable();
}

public static ItemStackSnapshot snapshotOf(@Nullable ItemStack itemStack) {
public static ItemStackSnapshot snapshotOf(@Nullable ItemStackLike itemStack) {
return itemStack == null ? ItemStackSnapshot.empty() : itemStack.isEmpty() ? ItemStackSnapshot.empty() : itemStack.asImmutable();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.spongepowered.api.entity.living.player.server.ServerPlayer;
import org.spongepowered.api.item.inventory.ContainerType;
import org.spongepowered.api.item.inventory.Inventory;
import org.spongepowered.api.item.inventory.ItemStackLike;
import org.spongepowered.api.item.inventory.menu.InventoryMenu;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
Expand Down Expand Up @@ -88,11 +89,11 @@ public List<Inventory> viewed() {
}

@Override
public boolean setCursor(org.spongepowered.api.item.inventory.ItemStack item) {
public boolean setCursor(ItemStackLike item) {
if (!this.isOpen()) {
return false;
}
ItemStack nativeStack = ItemStackUtil.toNative(item);
ItemStack nativeStack = ItemStackUtil.fromLikeToNative(item);
this.listeners().stream().findFirst()
.ifPresent(p -> p.containerMenu.setCarried(nativeStack));
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import net.minecraft.world.inventory.Slot;
import org.spongepowered.api.item.inventory.Inventory;
import org.spongepowered.api.item.inventory.ItemStack;
import org.spongepowered.api.item.inventory.ItemStackLike;
import org.spongepowered.api.item.inventory.ItemStackSnapshot;
import org.spongepowered.api.item.inventory.transaction.InventoryTransactionResult;
import org.spongepowered.api.item.inventory.transaction.SlotTransaction;
Expand Down Expand Up @@ -62,9 +63,9 @@ public org.spongepowered.api.item.inventory.Slot viewedSlot() {
}

@Override
public InventoryTransactionResult set(ItemStack stack) {
public InventoryTransactionResult set(ItemStackLike stack) {
final InventoryTransactionResult.Builder result = InventoryTransactionResult.builder().type(InventoryTransactionResult.Type.SUCCESS);
final net.minecraft.world.item.ItemStack nativeStack = ItemStackUtil.toNative(stack);
final net.minecraft.world.item.ItemStack nativeStack = ItemStackUtil.fromLikeToNative(stack);

final net.minecraft.world.item.ItemStack old = this.shadow$getItem();
ItemStackSnapshot oldSnap = ItemStackUtil.snapshotOf(old);
Expand Down

0 comments on commit 610ef28

Please sign in to comment.