Utils ported
This commit is contained in:
@@ -0,0 +1,134 @@
|
||||
/*
|
||||
* This file is part of Applied Energistics 2.
|
||||
* Copyright (c) 2013 - 2014, AlgorithmX2, All rights reserved.
|
||||
*
|
||||
* Applied Energistics 2 is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Applied Energistics 2 is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with Applied Energistics 2. If not, see <http://www.gnu.org/licenses/lgpl>.
|
||||
*/
|
||||
|
||||
package appeng.util.inv;
|
||||
|
||||
import alexiil.mc.lib.attributes.Simulation;
|
||||
import alexiil.mc.lib.attributes.item.FixedItemInv;
|
||||
import alexiil.mc.lib.attributes.item.ItemTransferable;
|
||||
import alexiil.mc.lib.attributes.item.filter.ItemFilter;
|
||||
import appeng.api.config.FuzzyMode;
|
||||
import appeng.util.InventoryAdaptor;
|
||||
import appeng.util.Platform;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.Iterator;
|
||||
|
||||
public class AdaptorFixedInv extends InventoryAdaptor {
|
||||
protected final FixedItemInv itemHandler;
|
||||
protected final ItemTransferable transferable;
|
||||
|
||||
public AdaptorFixedInv(FixedItemInv itemHandler) {
|
||||
this.itemHandler = itemHandler;
|
||||
this.transferable = itemHandler.getTransferable();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasSlots() {
|
||||
return this.itemHandler.getSlotCount() > 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ItemStack removeItems(int amount, ItemStack filter, IInventoryDestination destination) {
|
||||
ItemFilter itemFilter = createExactFilter(filter, destination);
|
||||
|
||||
return this.transferable.attemptExtraction(itemFilter, amount, Simulation.ACTION);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ItemStack simulateRemove(int amount, ItemStack filter, IInventoryDestination destination) {
|
||||
ItemFilter itemFilter = createExactFilter(filter, destination);
|
||||
|
||||
return this.transferable.attemptExtraction(itemFilter, amount, Simulation.SIMULATE);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private ItemFilter createExactFilter(ItemStack filter, IInventoryDestination destination) {
|
||||
ItemFilter itemFilter = destination != null ? destination::canInsert : stack -> true;
|
||||
if (!filter.isEmpty()) {
|
||||
if (destination != null) {
|
||||
itemFilter = stack -> Platform.itemComparisons().isSameItem(stack, filter) && destination.canInsert(stack);
|
||||
} else {
|
||||
itemFilter = stack -> Platform.itemComparisons().isSameItem(stack, filter);
|
||||
}
|
||||
}
|
||||
return itemFilter;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private ItemFilter createFuzzyFilter(ItemStack filter, FuzzyMode fuzzyMode, IInventoryDestination destination) {
|
||||
ItemFilter itemFilter = destination != null ? destination::canInsert : stack -> true;
|
||||
if (!filter.isEmpty()) {
|
||||
if (destination != null) {
|
||||
itemFilter = stack -> Platform.itemComparisons().isFuzzyEqualItem(stack, filter, fuzzyMode) && destination.canInsert(stack);
|
||||
} else {
|
||||
itemFilter = stack -> Platform.itemComparisons().isFuzzyEqualItem(stack, filter, fuzzyMode);
|
||||
}
|
||||
}
|
||||
return itemFilter;
|
||||
}
|
||||
|
||||
/**
|
||||
* For fuzzy extract, we will only ever extract one slot, since we're afraid of
|
||||
* merging two item stacks with different damage values.
|
||||
*/
|
||||
@Override
|
||||
public ItemStack removeSimilarItems(int amount, ItemStack filter, FuzzyMode fuzzyMode,
|
||||
IInventoryDestination destination) {
|
||||
ItemFilter itemFilter = createFuzzyFilter(filter, fuzzyMode, destination);
|
||||
|
||||
return this.transferable.attemptExtraction(itemFilter, amount, Simulation.ACTION);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ItemStack simulateSimilarRemove(int amount, ItemStack filter, FuzzyMode fuzzyMode,
|
||||
IInventoryDestination destination) {
|
||||
ItemFilter itemFilter = createFuzzyFilter(filter, fuzzyMode, destination);
|
||||
|
||||
return this.transferable.attemptExtraction(itemFilter, amount, Simulation.SIMULATE);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ItemStack addItems(ItemStack toBeAdded) {
|
||||
return this.addItems(toBeAdded, false);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ItemStack simulateAdd(ItemStack toBeSimulated) {
|
||||
return this.addItems(toBeSimulated, true);
|
||||
}
|
||||
|
||||
protected ItemStack addItems(final ItemStack itemsToAdd, final boolean simulate) {
|
||||
if (itemsToAdd.isEmpty()) {
|
||||
return ItemStack.EMPTY;
|
||||
}
|
||||
|
||||
return this.transferable.attemptInsertion(itemsToAdd, simulate ? Simulation.SIMULATE : Simulation.ACTION);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean containsItems() {
|
||||
return !transferable.attemptAnyExtraction(1, Simulation.SIMULATE).isEmpty();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterator<ItemSlot> iterator() {
|
||||
return new ItemHandlerIterator(this.itemHandler);
|
||||
}
|
||||
}
|
||||
@@ -1,226 +0,0 @@
|
||||
/*
|
||||
* This file is part of Applied Energistics 2.
|
||||
* Copyright (c) 2013 - 2014, AlgorithmX2, All rights reserved.
|
||||
*
|
||||
* Applied Energistics 2 is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Applied Energistics 2 is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with Applied Energistics 2. If not, see <http://www.gnu.org/licenses/lgpl>.
|
||||
*/
|
||||
|
||||
package appeng.util.inv;
|
||||
|
||||
import java.util.Iterator;
|
||||
|
||||
import net.minecraft.item.ItemStack;
|
||||
import alexiil.mc.lib.attributes.item.ItemTransferable;
|
||||
|
||||
import appeng.api.config.FuzzyMode;
|
||||
import appeng.util.InventoryAdaptor;
|
||||
import appeng.util.Platform;
|
||||
|
||||
public class AdaptorItemHandler extends InventoryAdaptor {
|
||||
protected final ItemTransferable itemHandler;
|
||||
|
||||
public AdaptorItemHandler(ItemTransferable itemHandler) {
|
||||
this.itemHandler = itemHandler;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasSlots() {
|
||||
return this.itemHandler.getSlots() > 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ItemStack removeItems(int amount, ItemStack filter, IInventoryDestination destination) {
|
||||
int slots = this.itemHandler.getSlots();
|
||||
ItemStack rv = ItemStack.EMPTY;
|
||||
|
||||
for (int slot = 0; slot < slots && amount > 0; slot++) {
|
||||
final ItemStack is = this.itemHandler.getStackInSlot(slot);
|
||||
if (is.isEmpty() || (!filter.isEmpty() && !Platform.itemComparisons().isSameItem(is, filter))) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (destination != null) {
|
||||
ItemStack extracted = this.itemHandler.extractItem(slot, amount, true);
|
||||
if (extracted.isEmpty()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!destination.canInsert(extracted)) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
// Attempt extracting it
|
||||
ItemStack extracted = this.itemHandler.extractItem(slot, amount, false);
|
||||
|
||||
if (extracted.isEmpty()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (rv.isEmpty()) {
|
||||
// Use the first stack as a template for the result
|
||||
rv = extracted;
|
||||
filter = extracted;
|
||||
amount -= extracted.getCount();
|
||||
} else {
|
||||
// Subsequent stacks will just increase the extracted size
|
||||
rv.grow(extracted.getCount());
|
||||
amount -= extracted.getCount();
|
||||
}
|
||||
}
|
||||
|
||||
return rv;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ItemStack simulateRemove(int amount, ItemStack filter, IInventoryDestination destination) {
|
||||
int slots = this.itemHandler.getSlots();
|
||||
ItemStack rv = ItemStack.EMPTY;
|
||||
|
||||
for (int slot = 0; slot < slots && amount > 0; slot++) {
|
||||
final ItemStack is = this.itemHandler.getStackInSlot(slot);
|
||||
if (!is.isEmpty() && (filter.isEmpty() || Platform.itemComparisons().isSameItem(is, filter))) {
|
||||
ItemStack extracted = this.itemHandler.extractItem(slot, amount, true);
|
||||
|
||||
if (extracted.isEmpty()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (destination != null) {
|
||||
if (!destination.canInsert(extracted)) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
if (rv.isEmpty()) {
|
||||
// Use the first stack as a template for the result
|
||||
rv = extracted.copy();
|
||||
filter = extracted;
|
||||
amount -= extracted.getCount();
|
||||
} else {
|
||||
// Subsequent stacks will just increase the extracted size
|
||||
rv.grow(extracted.getCount());
|
||||
amount -= extracted.getCount();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return rv;
|
||||
}
|
||||
|
||||
/**
|
||||
* For fuzzy extract, we will only ever extract one slot, since we're afraid of
|
||||
* merging two item stacks with different damage values.
|
||||
*/
|
||||
@Override
|
||||
public ItemStack removeSimilarItems(int amount, ItemStack filter, FuzzyMode fuzzyMode,
|
||||
IInventoryDestination destination) {
|
||||
int slots = this.itemHandler.getSlots();
|
||||
ItemStack extracted = ItemStack.EMPTY;
|
||||
|
||||
for (int slot = 0; slot < slots && extracted.isEmpty(); slot++) {
|
||||
final ItemStack is = this.itemHandler.getStackInSlot(slot);
|
||||
if (is.isEmpty()
|
||||
|| (!filter.isEmpty() && !Platform.itemComparisons().isFuzzyEqualItem(is, filter, fuzzyMode))) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (destination != null) {
|
||||
ItemStack simulated = this.itemHandler.extractItem(slot, amount, true);
|
||||
if (simulated.isEmpty()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!destination.canInsert(simulated)) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
// Attempt extracting it
|
||||
extracted = this.itemHandler.extractItem(slot, amount, false);
|
||||
}
|
||||
|
||||
return extracted;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ItemStack simulateSimilarRemove(int amount, ItemStack filter, FuzzyMode fuzzyMode,
|
||||
IInventoryDestination destination) {
|
||||
int slots = this.itemHandler.getSlots();
|
||||
ItemStack extracted = ItemStack.EMPTY;
|
||||
|
||||
for (int slot = 0; slot < slots && extracted.isEmpty(); slot++) {
|
||||
final ItemStack is = this.itemHandler.getStackInSlot(slot);
|
||||
if (is.isEmpty()
|
||||
|| (!filter.isEmpty() && !Platform.itemComparisons().isFuzzyEqualItem(is, filter, fuzzyMode))) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Attempt extracting it
|
||||
extracted = this.itemHandler.extractItem(slot, amount, true);
|
||||
|
||||
if (!extracted.isEmpty() && destination != null) {
|
||||
if (!destination.canInsert(extracted)) {
|
||||
extracted = ItemStack.EMPTY; // Keep on looking...
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return extracted;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ItemStack addItems(ItemStack toBeAdded) {
|
||||
return this.addItems(toBeAdded, false);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ItemStack simulateAdd(ItemStack toBeSimulated) {
|
||||
return this.addItems(toBeSimulated, true);
|
||||
}
|
||||
|
||||
protected ItemStack addItems(final ItemStack itemsToAdd, final boolean simulate) {
|
||||
if (itemsToAdd.isEmpty()) {
|
||||
return ItemStack.EMPTY;
|
||||
}
|
||||
|
||||
ItemStack left = itemsToAdd.copy();
|
||||
|
||||
for (int slot = 0; slot < this.itemHandler.getSlots(); slot++) {
|
||||
left = this.itemHandler.insertItem(slot, left, simulate);
|
||||
|
||||
if (left.isEmpty()) {
|
||||
return ItemStack.EMPTY;
|
||||
}
|
||||
}
|
||||
|
||||
return left;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean containsItems() {
|
||||
int slots = this.itemHandler.getSlots();
|
||||
for (int slot = 0; slot < slots; slot++) {
|
||||
if (!this.itemHandler.getStackInSlot(slot).isEmpty()) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterator<ItemSlot> iterator() {
|
||||
return new ItemHandlerIterator(this.itemHandler);
|
||||
}
|
||||
}
|
||||
@@ -18,15 +18,16 @@
|
||||
|
||||
package appeng.util.inv;
|
||||
|
||||
import alexiil.mc.lib.attributes.Simulation;
|
||||
import alexiil.mc.lib.attributes.item.compat.FixedInventoryVanillaWrapper;
|
||||
import appeng.util.Platform;
|
||||
import net.minecraft.entity.player.PlayerEntity;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraftforge.items.wrapper.PlayerMainInvWrapper;
|
||||
|
||||
import appeng.util.Platform;
|
||||
public class AdaptorItemHandlerPlayerInv extends AdaptorFixedInv {
|
||||
|
||||
public class AdaptorItemHandlerPlayerInv extends AdaptorItemHandler {
|
||||
public AdaptorItemHandlerPlayerInv(final PlayerEntity playerInv) {
|
||||
super(new PlayerMainInvWrapper(playerInv.inventory));
|
||||
super(new FixedInventoryVanillaWrapper(playerInv.inventory));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -38,27 +39,23 @@ public class AdaptorItemHandlerPlayerInv extends AdaptorItemHandler {
|
||||
return ItemStack.EMPTY;
|
||||
}
|
||||
|
||||
Simulation sim = simulate ? Simulation.SIMULATE : Simulation.ACTION;
|
||||
|
||||
ItemStack left = itemsToAdd.copy();
|
||||
|
||||
for (int slot = 0; slot < this.itemHandler.getSlots(); slot++) {
|
||||
ItemStack is = this.itemHandler.getStackInSlot(slot);
|
||||
// First try filling slots
|
||||
for (int slot = 0; slot < this.itemHandler.getSlotCount(); slot++) {
|
||||
ItemStack is = this.itemHandler.getInvStack(slot);
|
||||
|
||||
if (Platform.itemComparisons().isSameItem(is, left)) {
|
||||
left = this.itemHandler.insertItem(slot, left, simulate);
|
||||
left = itemHandler.getSlot(slot).attemptInsertion(left, sim);
|
||||
}
|
||||
if (left.isEmpty()) {
|
||||
return ItemStack.EMPTY;
|
||||
}
|
||||
}
|
||||
|
||||
for (int slot = 0; slot < this.itemHandler.getSlots(); slot++) {
|
||||
left = this.itemHandler.insertItem(slot, left, simulate);
|
||||
if (left.isEmpty()) {
|
||||
return ItemStack.EMPTY;
|
||||
}
|
||||
}
|
||||
|
||||
return left;
|
||||
return transferable.attemptInsertion(left, sim);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -57,7 +57,7 @@ public class AdaptorList extends InventoryAdaptor {
|
||||
if (amount > 0) {
|
||||
final ItemStack rv = is.copy();
|
||||
rv.setCount(amount);
|
||||
is.grow(-amount);
|
||||
is.increment(-amount);
|
||||
|
||||
// remove it..
|
||||
if (is.getCount() <= 0) {
|
||||
@@ -110,7 +110,7 @@ public class AdaptorList extends InventoryAdaptor {
|
||||
if (amount > 0) {
|
||||
final ItemStack rv = is.copy();
|
||||
rv.setCount(amount);
|
||||
is.grow(-amount);
|
||||
is.increment(-amount);
|
||||
|
||||
// remove it..
|
||||
if (is.getCount() <= 0) {
|
||||
@@ -160,7 +160,7 @@ public class AdaptorList extends InventoryAdaptor {
|
||||
|
||||
for (final ItemStack is : this.i) {
|
||||
if (ItemStack.areItemsEqual(is, left)) {
|
||||
is.grow(left.getCount());
|
||||
is.increment(left.getCount());
|
||||
return ItemStack.EMPTY;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,11 +18,11 @@
|
||||
|
||||
package appeng.util.inv;
|
||||
|
||||
import alexiil.mc.lib.attributes.item.ItemTransferable;
|
||||
import alexiil.mc.lib.attributes.item.FixedItemInv;
|
||||
import net.minecraft.item.ItemStack;
|
||||
|
||||
public interface IAEAppEngInventory {
|
||||
void saveChanges();
|
||||
|
||||
void onChangeInventory(ItemTransferable inv, int slot, InvOperation mc, ItemStack removedStack, ItemStack newStack);
|
||||
void onChangeInventory(FixedItemInv inv, int slot, InvOperation mc, ItemStack removedStack, ItemStack newStack);
|
||||
}
|
||||
|
||||
@@ -21,32 +21,33 @@ package appeng.util.inv;
|
||||
import java.util.Iterator;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
import alexiil.mc.lib.attributes.item.ItemTransferable;
|
||||
import alexiil.mc.lib.attributes.Simulation;
|
||||
import alexiil.mc.lib.attributes.item.FixedItemInv;
|
||||
|
||||
class ItemHandlerIterator implements Iterator<ItemSlot> {
|
||||
|
||||
private final ItemTransferable itemHandler;
|
||||
private final FixedItemInv itemHandler;
|
||||
|
||||
private final ItemSlot itemSlot = new ItemSlot();
|
||||
|
||||
private int slot = 0;
|
||||
|
||||
ItemHandlerIterator(ItemTransferable itemHandler) {
|
||||
ItemHandlerIterator(FixedItemInv itemHandler) {
|
||||
this.itemHandler = itemHandler;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasNext() {
|
||||
return this.slot < this.itemHandler.getSlots();
|
||||
return this.slot < this.itemHandler.getSlotCount();
|
||||
}
|
||||
|
||||
@Override
|
||||
public ItemSlot next() {
|
||||
if (this.slot >= this.itemHandler.getSlots()) {
|
||||
if (this.slot >= this.itemHandler.getSlotCount()) {
|
||||
throw new NoSuchElementException();
|
||||
}
|
||||
this.itemSlot.setExtractable(!this.itemHandler.extractItem(this.slot, 1, true).isEmpty());
|
||||
this.itemSlot.setItemStack(this.itemHandler.getStackInSlot(this.slot));
|
||||
this.itemSlot.setExtractable(!this.itemHandler.getSlot(this.slot).attemptAnyExtraction(1, Simulation.SIMULATE).isEmpty());
|
||||
this.itemSlot.setItemStack(this.itemHandler.getInvStack(this.slot));
|
||||
this.itemSlot.setSlot(this.slot);
|
||||
this.slot++;
|
||||
return this.itemSlot;
|
||||
|
||||
@@ -18,129 +18,17 @@
|
||||
|
||||
package appeng.util.inv;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import alexiil.mc.lib.attributes.item.FixedItemInv;
|
||||
import alexiil.mc.lib.attributes.item.impl.CombinedFixedItemInv;
|
||||
import alexiil.mc.lib.attributes.item.impl.DelegatingFixedItemInv;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import java.util.Arrays;
|
||||
|
||||
import alexiil.mc.lib.attributes.item.ItemTransferable;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraftforge.items.IItemHandlerModifiable;
|
||||
import alexiil.mc.lib.attributes.item.impl.EmptyFixedItemInv;
|
||||
// FIXME could be replaced by CombinedFixedItemInv directly
|
||||
public class WrapperChainedItemHandler extends DelegatingFixedItemInv {
|
||||
|
||||
import appeng.util.helpers.ItemHandlerUtil;
|
||||
|
||||
public class WrapperChainedItemHandler implements IItemHandlerModifiable {
|
||||
private ItemTransferable[] itemHandler; // the handlers
|
||||
private int[] baseIndex; // index-offsets of the different handlers
|
||||
private int slotCount; // number of total slots
|
||||
|
||||
public WrapperChainedItemHandler(ItemTransferable... itemHandler) {
|
||||
this.setItemHandlers(itemHandler);
|
||||
public WrapperChainedItemHandler(FixedItemInv... itemHandler) {
|
||||
super(CombinedFixedItemInv.create(Arrays.asList(itemHandler)));
|
||||
}
|
||||
|
||||
private void setItemHandlers(ItemTransferable[] handlers) {
|
||||
this.itemHandler = handlers;
|
||||
this.baseIndex = new int[this.itemHandler.length];
|
||||
int index = 0;
|
||||
for (int i = 0; i < this.itemHandler.length; i++) {
|
||||
index += this.itemHandler[i].getSlots();
|
||||
this.baseIndex[i] = index;
|
||||
}
|
||||
this.slotCount = index;
|
||||
}
|
||||
|
||||
// returns the handler index for the slot
|
||||
private int getIndexForSlot(int slot) {
|
||||
if (slot < 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
for (int i = 0; i < this.baseIndex.length; i++) {
|
||||
if (slot - this.baseIndex[i] < 0) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
private ItemTransferable getHandlerFromIndex(int index) {
|
||||
if (index < 0 || index >= this.itemHandler.length) {
|
||||
return EmptyFixedItemInv.INSTANCE;
|
||||
}
|
||||
return this.itemHandler[index];
|
||||
}
|
||||
|
||||
private int getSlotFromIndex(int slot, int index) {
|
||||
if (index <= 0 || index >= this.baseIndex.length) {
|
||||
return slot;
|
||||
}
|
||||
return slot - this.baseIndex[index - 1];
|
||||
}
|
||||
|
||||
public void cycleOrder() {
|
||||
if (this.itemHandler.length > 1) {
|
||||
ArrayList<ItemTransferable> newOrder = new ArrayList<>();
|
||||
newOrder.add(this.itemHandler[this.itemHandler.length - 1]);
|
||||
for (int i = 0; i < this.itemHandler.length - 1; ++i) {
|
||||
newOrder.add(this.itemHandler[i]);
|
||||
}
|
||||
this.setItemHandlers(newOrder.toArray(new ItemTransferable[this.itemHandler.length]));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getSlots() {
|
||||
return this.slotCount;
|
||||
}
|
||||
|
||||
@Override
|
||||
@Nonnull
|
||||
public ItemStack getStackInSlot(final int slot) {
|
||||
int index = this.getIndexForSlot(slot);
|
||||
ItemTransferable handler = this.getHandlerFromIndex(index);
|
||||
int targetSlot = this.getSlotFromIndex(slot, index);
|
||||
return handler.getStackInSlot(targetSlot);
|
||||
}
|
||||
|
||||
@Override
|
||||
@Nonnull
|
||||
public ItemStack insertItem(final int slot, @Nonnull ItemStack stack, boolean simulate) {
|
||||
int index = this.getIndexForSlot(slot);
|
||||
ItemTransferable handler = this.getHandlerFromIndex(index);
|
||||
int targetSlot = this.getSlotFromIndex(slot, index);
|
||||
return handler.insertItem(targetSlot, stack, simulate);
|
||||
}
|
||||
|
||||
@Override
|
||||
@Nonnull
|
||||
public ItemStack extractItem(int slot, int amount, boolean simulate) {
|
||||
int index = this.getIndexForSlot(slot);
|
||||
ItemTransferable handler = this.getHandlerFromIndex(index);
|
||||
int targetSlot = this.getSlotFromIndex(slot, index);
|
||||
return handler.extractItem(targetSlot, amount, simulate);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getSlotLimit(int slot) {
|
||||
int index = this.getIndexForSlot(slot);
|
||||
ItemTransferable handler = this.getHandlerFromIndex(index);
|
||||
int localSlot = this.getSlotFromIndex(slot, index);
|
||||
return handler.getSlotLimit(localSlot);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setStackInSlot(int slot, ItemStack stack) {
|
||||
int index = this.getIndexForSlot(slot);
|
||||
ItemTransferable handler = this.getHandlerFromIndex(index);
|
||||
int targetSlot = this.getSlotFromIndex(slot, index);
|
||||
ItemHandlerUtil.setStackInSlot(handler, targetSlot, stack);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isItemValid(int slot, ItemStack stack) {
|
||||
int index = this.getIndexForSlot(slot);
|
||||
ItemTransferable handler = this.getHandlerFromIndex(index);
|
||||
int targetSlot = this.getSlotFromIndex(slot, index);
|
||||
return handler.isItemValid(targetSlot, stack);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,21 +18,14 @@
|
||||
|
||||
package appeng.util.inv;
|
||||
|
||||
import alexiil.mc.lib.attributes.item.compat.FixedInventoryVanillaWrapper;
|
||||
import alexiil.mc.lib.attributes.item.impl.DelegatingFixedItemInv;
|
||||
import net.minecraft.entity.player.PlayerInventory;
|
||||
import net.minecraftforge.items.ItemStackHandler;
|
||||
|
||||
public class WrapperCursorItemHandler extends ItemStackHandler {
|
||||
private final PlayerInventory inv;
|
||||
public class WrapperCursorItemHandler extends DelegatingFixedItemInv {
|
||||
|
||||
public WrapperCursorItemHandler(PlayerInventory PlayerInventory) {
|
||||
super(1);
|
||||
|
||||
this.inv = PlayerInventory;
|
||||
this.setStackInSlot(0, PlayerInventory.getItemStack());
|
||||
public WrapperCursorItemHandler(PlayerInventory inventory) {
|
||||
super(new FixedInventoryVanillaWrapper(inventory).getSubInv(0, 1));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onContentsChanged(int slot) {
|
||||
this.inv.setItemStack(this.getStackInSlot(slot));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,66 +19,62 @@
|
||||
package appeng.util.inv;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
import alexiil.mc.lib.attributes.ListenerRemovalToken;
|
||||
import alexiil.mc.lib.attributes.ListenerToken;
|
||||
import alexiil.mc.lib.attributes.Simulation;
|
||||
import alexiil.mc.lib.attributes.item.InvMarkDirtyListener;
|
||||
import alexiil.mc.lib.attributes.item.filter.ItemFilter;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import alexiil.mc.lib.attributes.item.ItemTransferable;
|
||||
import net.minecraftforge.items.IItemHandlerModifiable;
|
||||
import alexiil.mc.lib.attributes.item.FixedItemInv;
|
||||
|
||||
import appeng.util.helpers.ItemHandlerUtil;
|
||||
import appeng.util.inv.filter.IAEItemFilter;
|
||||
|
||||
public class WrapperFilteredItemHandler implements IItemHandlerModifiable {
|
||||
private final ItemTransferable handler;
|
||||
// FIXME: Needs to be double checked, LBA has better ways of doing this
|
||||
public class WrapperFilteredItemHandler implements FixedItemInv {
|
||||
private final FixedItemInv handler;
|
||||
private final IAEItemFilter filter;
|
||||
|
||||
public WrapperFilteredItemHandler(@Nonnull ItemTransferable handler, @Nonnull IAEItemFilter filter) {
|
||||
public WrapperFilteredItemHandler(@Nonnull FixedItemInv handler, @Nonnull IAEItemFilter filter) {
|
||||
this.handler = handler;
|
||||
this.filter = filter;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setStackInSlot(int slot, ItemStack stack) {
|
||||
ItemHandlerUtil.setStackInSlot(this.handler, slot, stack);
|
||||
public ItemStack getInvStack(int slot) {
|
||||
return this.handler.getInvStack(slot);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getSlots() {
|
||||
return this.handler.getSlots();
|
||||
public boolean setInvStack(int slot, ItemStack to, Simulation simulation) {
|
||||
return this.handler.setInvStack(slot, to, simulation);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ItemStack getStackInSlot(int slot) {
|
||||
return this.handler.getStackInSlot(slot);
|
||||
public int getSlotCount() {
|
||||
return this.handler.getSlotCount();
|
||||
}
|
||||
|
||||
@Override
|
||||
public ItemStack insertItem(int slot, ItemStack stack, boolean simulate) {
|
||||
if (!this.filter.allowInsert(this.handler, slot, stack)) {
|
||||
return stack;
|
||||
}
|
||||
|
||||
return this.handler.insertItem(slot, stack, simulate);
|
||||
public boolean isItemValidForSlot(int slot, ItemStack stack) {
|
||||
return filter.allowInsert(handler, slot, stack);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ItemStack extractItem(int slot, int amount, boolean simulate) {
|
||||
if (!this.filter.allowExtract(this.handler, slot, amount)) {
|
||||
return ItemStack.EMPTY;
|
||||
}
|
||||
|
||||
return this.handler.extractItem(slot, amount, simulate);
|
||||
public ItemFilter getFilterForSlot(int slot) {
|
||||
return stack -> filter.allowInsert(handler, slot, stack);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getSlotLimit(int slot) {
|
||||
return this.handler.getSlotLimit(slot);
|
||||
public int getChangeValue() {
|
||||
return this.handler.getChangeValue();
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public boolean isItemValid(int slot, ItemStack stack) {
|
||||
if (!this.filter.allowInsert(this.handler, slot, stack)) {
|
||||
return false;
|
||||
}
|
||||
return this.handler.isItemValid(slot, stack);
|
||||
public ListenerToken addListener(InvMarkDirtyListener listener, ListenerRemovalToken removalToken) {
|
||||
return this.handler.addListener(listener, removalToken);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -18,87 +18,19 @@
|
||||
|
||||
package appeng.util.inv;
|
||||
|
||||
import alexiil.mc.lib.attributes.item.FixedItemInv;
|
||||
import alexiil.mc.lib.attributes.item.compat.InventoryFixedWrapper;
|
||||
import net.minecraft.entity.player.PlayerEntity;
|
||||
import net.minecraft.inventory.IInventory;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import alexiil.mc.lib.attributes.item.ItemTransferable;
|
||||
|
||||
import appeng.util.helpers.ItemHandlerUtil;
|
||||
public class WrapperInvItemHandler extends InventoryFixedWrapper {
|
||||
|
||||
public class WrapperInvItemHandler implements IInventory {
|
||||
private final ItemTransferable inv;
|
||||
|
||||
public WrapperInvItemHandler(final ItemTransferable inv) {
|
||||
this.inv = inv;
|
||||
public WrapperInvItemHandler(final FixedItemInv inv) {
|
||||
super(inv);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getSizeInventory() {
|
||||
return this.inv.getSlots();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isEmpty() {
|
||||
return ItemHandlerUtil.isEmpty(this.inv);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ItemStack getStackInSlot(int index) {
|
||||
return this.inv.getStackInSlot(index);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ItemStack decrStackSize(int index, int count) {
|
||||
return this.inv.extractItem(index, count, false);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ItemStack removeStackFromSlot(int index) {
|
||||
return this.inv.extractItem(index, this.inv.getSlotLimit(index), false);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setInventorySlotContents(int index, ItemStack stack) {
|
||||
ItemHandlerUtil.setStackInSlot(this.inv, index, stack);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getInventoryStackLimit() {
|
||||
int max = 0;
|
||||
for (int i = 0; i < this.inv.getSlots(); ++i) {
|
||||
max = Math.max(max, this.inv.getSlotLimit(i));
|
||||
}
|
||||
return max;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void markDirty() {
|
||||
// NOP
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isUsableByPlayer(PlayerEntity player) {
|
||||
public boolean canPlayerUse(PlayerEntity player) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void openInventory(PlayerEntity player) {
|
||||
// NOP
|
||||
}
|
||||
|
||||
@Override
|
||||
public void closeInventory(PlayerEntity player) {
|
||||
// NOP
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isItemValidForSlot(int index, ItemStack stack) {
|
||||
return this.inv.isItemValid(index, stack);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clear() {
|
||||
ItemHandlerUtil.clear(this.inv);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,102 +0,0 @@
|
||||
/*
|
||||
* This file is part of Applied Energistics 2.
|
||||
* Copyright (c) 2013 - 2017, AlgorithmX2, All rights reserved.
|
||||
*
|
||||
* Applied Energistics 2 is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Applied Energistics 2 is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with Applied Energistics 2. If not, see <http://www.gnu.org/licenses/lgpl>.
|
||||
*/
|
||||
|
||||
package appeng.util.inv;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
|
||||
import alexiil.mc.lib.attributes.item.ItemTransferable;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraftforge.items.IItemHandlerModifiable;
|
||||
|
||||
import appeng.util.helpers.ItemHandlerUtil;
|
||||
|
||||
public class WrapperRangeItemHandler implements IItemHandlerModifiable {
|
||||
private final ItemTransferable compose;
|
||||
private final int minSlot;
|
||||
private final int maxSlot;
|
||||
|
||||
public WrapperRangeItemHandler(ItemTransferable compose, int minSlot, int maxSlotExclusive) {
|
||||
this.compose = compose;
|
||||
this.minSlot = minSlot;
|
||||
this.maxSlot = maxSlotExclusive;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getSlots() {
|
||||
return this.maxSlot - this.minSlot;
|
||||
}
|
||||
|
||||
@Override
|
||||
@Nonnull
|
||||
public ItemStack getStackInSlot(int slot) {
|
||||
if (this.checkSlot(slot)) {
|
||||
return this.compose.getStackInSlot(slot + this.minSlot);
|
||||
}
|
||||
|
||||
return ItemStack.EMPTY;
|
||||
}
|
||||
|
||||
@Override
|
||||
@Nonnull
|
||||
public ItemStack insertItem(int slot, @Nonnull ItemStack stack, boolean simulate) {
|
||||
if (this.checkSlot(slot)) {
|
||||
return this.compose.insertItem(slot + this.minSlot, stack, simulate);
|
||||
}
|
||||
|
||||
return stack;
|
||||
}
|
||||
|
||||
@Override
|
||||
@Nonnull
|
||||
public ItemStack extractItem(int slot, int amount, boolean simulate) {
|
||||
if (this.checkSlot(slot)) {
|
||||
return this.compose.extractItem(slot + this.minSlot, amount, simulate);
|
||||
}
|
||||
|
||||
return ItemStack.EMPTY;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setStackInSlot(int slot, @Nonnull ItemStack stack) {
|
||||
if (this.checkSlot(slot)) {
|
||||
ItemHandlerUtil.setStackInSlot(this.compose, slot + this.minSlot, stack);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getSlotLimit(int slot) {
|
||||
if (this.checkSlot(slot)) {
|
||||
return this.compose.getSlotLimit(slot + this.minSlot);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
private boolean checkSlot(int localSlot) {
|
||||
return localSlot + this.minSlot < this.maxSlot;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isItemValid(int slot, ItemStack stack) {
|
||||
if (this.checkSlot(slot)) {
|
||||
return this.compose.isItemValid(slot + this.minSlot, stack);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -20,51 +20,58 @@ package appeng.util.inv;
|
||||
|
||||
import java.util.function.Supplier;
|
||||
|
||||
import alexiil.mc.lib.attributes.ListenerRemovalToken;
|
||||
import alexiil.mc.lib.attributes.ListenerToken;
|
||||
import alexiil.mc.lib.attributes.Simulation;
|
||||
import alexiil.mc.lib.attributes.item.GroupedItemInv;
|
||||
import alexiil.mc.lib.attributes.item.InvMarkDirtyListener;
|
||||
import alexiil.mc.lib.attributes.item.impl.DelegatingGroupedItemInv;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import alexiil.mc.lib.attributes.item.ItemTransferable;
|
||||
import net.minecraftforge.items.IItemHandlerModifiable;
|
||||
import alexiil.mc.lib.attributes.item.FixedItemInv;
|
||||
|
||||
import appeng.util.helpers.ItemHandlerUtil;
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
public class WrapperSupplierItemHandler implements IItemHandlerModifiable {
|
||||
private final Supplier<ItemTransferable> sourceHandler;
|
||||
public class WrapperSupplierItemHandler implements FixedItemInv {
|
||||
private final Supplier<FixedItemInv> sourceHandler;
|
||||
|
||||
public WrapperSupplierItemHandler(Supplier<ItemTransferable> source) {
|
||||
public WrapperSupplierItemHandler(Supplier<FixedItemInv> source) {
|
||||
this.sourceHandler = source;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getSlots() {
|
||||
return this.sourceHandler.get().getSlots();
|
||||
public GroupedItemInv getGroupedInv() {
|
||||
return new DelegatingGroupedItemInv(this.sourceHandler.get().getGroupedInv());
|
||||
}
|
||||
|
||||
@Override
|
||||
public ItemStack getStackInSlot(int slot) {
|
||||
return this.sourceHandler.get().getStackInSlot(slot);
|
||||
public ItemStack getInvStack(int slot) {
|
||||
return this.sourceHandler.get().getInvStack(slot);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ItemStack insertItem(int slot, ItemStack stack, boolean simulate) {
|
||||
return this.sourceHandler.get().insertItem(slot, stack, simulate);
|
||||
public boolean setInvStack(int slot, ItemStack to, Simulation simulation) {
|
||||
return this.sourceHandler.get().setInvStack(slot, to, simulation);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ItemStack extractItem(int slot, int amount, boolean simulate) {
|
||||
return this.sourceHandler.get().extractItem(slot, amount, simulate);
|
||||
public int getSlotCount() {
|
||||
return this.sourceHandler.get().getSlotCount();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getSlotLimit(int slot) {
|
||||
return this.sourceHandler.get().getSlotLimit(slot);
|
||||
public boolean isItemValidForSlot(int slot, ItemStack stack) {
|
||||
return this.sourceHandler.get().isItemValidForSlot(slot, stack);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setStackInSlot(int slot, ItemStack stack) {
|
||||
ItemHandlerUtil.setStackInSlot(this.sourceHandler.get(), slot, stack);
|
||||
public int getChangeValue() {
|
||||
return this.sourceHandler.get().getChangeValue();
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public boolean isItemValid(int slot, ItemStack stack) {
|
||||
return this.sourceHandler.get().isItemValid(slot, stack);
|
||||
public ListenerToken addListener(InvMarkDirtyListener listener, ListenerRemovalToken removalToken) {
|
||||
return this.sourceHandler.get().addListener(listener, removalToken);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -18,7 +18,7 @@
|
||||
|
||||
package appeng.util.inv.filter;
|
||||
|
||||
import alexiil.mc.lib.attributes.item.ItemTransferable;
|
||||
import alexiil.mc.lib.attributes.item.FixedItemInv;
|
||||
import net.minecraft.item.ItemStack;
|
||||
|
||||
import appeng.api.definitions.IItemDefinition;
|
||||
@@ -31,12 +31,12 @@ public class AEItemDefinitionFilter implements IAEItemFilter {
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean allowExtract(ItemTransferable inv, int slot, int amount) {
|
||||
public boolean allowExtract(FixedItemInv inv, int slot, int amount) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean allowInsert(ItemTransferable inv, int slot, ItemStack stack) {
|
||||
public boolean allowInsert(FixedItemInv inv, int slot, ItemStack stack) {
|
||||
return this.definition.isSameAs(stack);
|
||||
}
|
||||
|
||||
|
||||
@@ -18,7 +18,7 @@
|
||||
|
||||
package appeng.util.inv.filter;
|
||||
|
||||
import alexiil.mc.lib.attributes.item.ItemTransferable;
|
||||
import alexiil.mc.lib.attributes.item.FixedItemInv;
|
||||
import net.minecraft.item.ItemStack;
|
||||
|
||||
public class AEItemFilters {
|
||||
@@ -30,24 +30,24 @@ public class AEItemFilters {
|
||||
|
||||
private static class InsertOnlyFilter implements IAEItemFilter {
|
||||
@Override
|
||||
public boolean allowExtract(ItemTransferable inv, int slot, int amount) {
|
||||
public boolean allowExtract(FixedItemInv inv, int slot, int amount) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean allowInsert(ItemTransferable inv, int slot, ItemStack stack) {
|
||||
public boolean allowInsert(FixedItemInv inv, int slot, ItemStack stack) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
private static class ExtractOnlyFilter implements IAEItemFilter {
|
||||
@Override
|
||||
public boolean allowExtract(ItemTransferable inv, int slot, int amount) {
|
||||
public boolean allowExtract(FixedItemInv inv, int slot, int amount) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean allowInsert(ItemTransferable inv, int slot, ItemStack stack) {
|
||||
public boolean allowInsert(FixedItemInv inv, int slot, ItemStack stack) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,11 +18,11 @@
|
||||
|
||||
package appeng.util.inv.filter;
|
||||
|
||||
import alexiil.mc.lib.attributes.item.ItemTransferable;
|
||||
import alexiil.mc.lib.attributes.item.FixedItemInv;
|
||||
import net.minecraft.item.ItemStack;
|
||||
|
||||
public interface IAEItemFilter {
|
||||
boolean allowExtract(ItemTransferable inv, int slot, int amount);
|
||||
boolean allowExtract(FixedItemInv inv, int slot, int amount);
|
||||
|
||||
boolean allowInsert(ItemTransferable inv, int slot, ItemStack stack);
|
||||
boolean allowInsert(FixedItemInv inv, int slot, ItemStack stack);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user