Utils ported

This commit is contained in:
Sebastian Hartte
2020-06-28 13:28:14 +02:00
parent fee5b7fdfc
commit 46955643b9
105 changed files with 239 additions and 379 deletions
@@ -1,134 +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 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,61 +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 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;
public class AdaptorItemHandlerPlayerInv extends AdaptorFixedInv {
public AdaptorItemHandlerPlayerInv(final PlayerEntity playerInv) {
super(new FixedInventoryVanillaWrapper(playerInv.inventory));
}
/**
* Tries to fill existing stacks first
*/
@Override
protected ItemStack addItems(final ItemStack itemsToAdd, final boolean simulate) {
if (itemsToAdd.isEmpty()) {
return ItemStack.EMPTY;
}
Simulation sim = simulate ? Simulation.SIMULATE : Simulation.ACTION;
ItemStack left = itemsToAdd.copy();
// 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 = itemHandler.getSlot(slot).attemptInsertion(left, sim);
}
if (left.isEmpty()) {
return ItemStack.EMPTY;
}
}
return transferable.attemptInsertion(left, sim);
}
}
@@ -1,191 +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 java.util.List;
import net.minecraft.item.ItemStack;
import appeng.api.config.FuzzyMode;
import appeng.util.InventoryAdaptor;
import appeng.util.Platform;
import appeng.util.iterators.StackToSlotIterator;
public class AdaptorList extends InventoryAdaptor {
private final List<ItemStack> i;
public AdaptorList(final List<ItemStack> s) {
this.i = s;
}
@Override
public boolean hasSlots() {
return !this.i.isEmpty();
}
@Override
public ItemStack removeItems(int amount, final ItemStack filter, final IInventoryDestination destination) {
final int s = this.i.size();
for (int x = 0; x < s; x++) {
final ItemStack is = this.i.get(x);
if (!is.isEmpty() && (filter.isEmpty() || Platform.itemComparisons().isSameItem(is, filter))) {
if (amount > is.getCount()) {
amount = is.getCount();
}
if (destination != null && !destination.canInsert(is)) {
amount = 0;
}
if (amount > 0) {
final ItemStack rv = is.copy();
rv.setCount(amount);
is.increment(-amount);
// remove it..
if (is.getCount() <= 0) {
this.i.remove(x);
}
return rv;
}
}
}
return ItemStack.EMPTY;
}
@Override
public ItemStack simulateRemove(int amount, final ItemStack filter, final IInventoryDestination destination) {
for (final ItemStack is : this.i) {
if (!is.isEmpty() && (filter.isEmpty() || Platform.itemComparisons().isSameItem(is, filter))) {
if (amount > is.getCount()) {
amount = is.getCount();
}
if (destination != null && !destination.canInsert(is)) {
amount = 0;
}
if (amount > 0) {
final ItemStack rv = is.copy();
rv.setCount(amount);
return rv;
}
}
}
return ItemStack.EMPTY;
}
@Override
public ItemStack removeSimilarItems(int amount, final ItemStack filter, final FuzzyMode fuzzyMode,
final IInventoryDestination destination) {
final int s = this.i.size();
for (int x = 0; x < s; x++) {
final ItemStack is = this.i.get(x);
if (!is.isEmpty()
&& (filter.isEmpty() || Platform.itemComparisons().isFuzzyEqualItem(is, filter, fuzzyMode))) {
if (amount > is.getCount()) {
amount = is.getCount();
}
if (destination != null && !destination.canInsert(is)) {
amount = 0;
}
if (amount > 0) {
final ItemStack rv = is.copy();
rv.setCount(amount);
is.increment(-amount);
// remove it..
if (is.getCount() <= 0) {
this.i.remove(x);
}
return rv;
}
}
}
return ItemStack.EMPTY;
}
@Override
public ItemStack simulateSimilarRemove(int amount, final ItemStack filter, final FuzzyMode fuzzyMode,
final IInventoryDestination destination) {
for (final ItemStack is : this.i) {
if (!is.isEmpty()
&& (filter.isEmpty() || Platform.itemComparisons().isFuzzyEqualItem(is, filter, fuzzyMode))) {
if (amount > is.getCount()) {
amount = is.getCount();
}
if (destination != null && !destination.canInsert(is)) {
amount = 0;
}
if (amount > 0) {
final ItemStack rv = is.copy();
rv.setCount(amount);
return rv;
}
}
}
return ItemStack.EMPTY;
}
@Override
public ItemStack addItems(final ItemStack toBeAdded) {
if (toBeAdded.isEmpty()) {
return ItemStack.EMPTY;
}
if (toBeAdded.getCount() == 0) {
return ItemStack.EMPTY;
}
final ItemStack left = toBeAdded.copy();
for (final ItemStack is : this.i) {
if (ItemStack.areItemsEqual(is, left)) {
is.increment(left.getCount());
return ItemStack.EMPTY;
}
}
this.i.add(left);
return ItemStack.EMPTY;
}
@Override
public ItemStack simulateAdd(final ItemStack toBeSimulated) {
return ItemStack.EMPTY;
}
@Override
public boolean containsItems() {
for (final ItemStack is : this.i) {
if (!is.isEmpty()) {
return true;
}
}
return false;
}
@Override
public Iterator<ItemSlot> iterator() {
return new StackToSlotIterator(this.i.iterator());
}
}
@@ -1,28 +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 alexiil.mc.lib.attributes.item.FixedItemInv;
import net.minecraft.item.ItemStack;
public interface IAEAppEngInventory {
void saveChanges();
void onChangeInventory(FixedItemInv inv, int slot, InvOperation mc, ItemStack removedStack, ItemStack newStack);
}
@@ -1,26 +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 net.minecraft.item.ItemStack;
public interface IInventoryDestination {
boolean canInsert(ItemStack stack);
}
@@ -1,177 +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 com.google.common.collect.ImmutableList;
import net.minecraft.item.ItemStack;
import appeng.api.AEApi;
import appeng.api.config.Actionable;
import appeng.api.config.FuzzyMode;
import appeng.api.networking.security.IActionSource;
import appeng.api.storage.IMEInventory;
import appeng.api.storage.channels.IItemStorageChannel;
import appeng.api.storage.data.IAEItemStack;
import appeng.api.storage.data.IItemList;
import appeng.util.InventoryAdaptor;
import appeng.util.item.AEItemStack;
public class IMEAdaptor extends InventoryAdaptor {
private final IMEInventory<IAEItemStack> target;
private final IActionSource src;
private int maxSlots = 0;
public IMEAdaptor(final IMEInventory<IAEItemStack> input, final IActionSource src) {
this.target = input;
this.src = src;
}
@Override
public boolean hasSlots() {
return true;
}
@Override
public Iterator<ItemSlot> iterator() {
return new IMEAdaptorIterator(this, this.getList());
}
private IItemList<IAEItemStack> getList() {
return this.target.getAvailableItems(
AEApi.instance().storage().getStorageChannel(IItemStorageChannel.class).createList());
}
@Override
public ItemStack removeItems(final int amount, final ItemStack filter, final IInventoryDestination destination) {
return this.doRemoveItems(amount, filter, destination, Actionable.MODULATE);
}
private ItemStack doRemoveItems(final int amount, final ItemStack filter, final IInventoryDestination destination,
final Actionable type) {
IAEItemStack req = null;
if (filter.isEmpty()) {
final IItemList<IAEItemStack> list = this.getList();
if (!list.isEmpty()) {
req = list.getFirstItem();
}
} else {
req = AEItemStack.fromItemStack(filter);
}
IAEItemStack out = null;
if (req != null) {
req.setStackSize(amount);
out = this.target.extractItems(req, type, this.src);
}
if (out != null) {
return out.createItemStack();
}
return ItemStack.EMPTY;
}
@Override
public ItemStack simulateRemove(final int amount, final ItemStack filter, final IInventoryDestination destination) {
return this.doRemoveItems(amount, filter, destination, Actionable.SIMULATE);
}
@Override
public ItemStack removeSimilarItems(final int amount, final ItemStack filter, final FuzzyMode fuzzyMode,
final IInventoryDestination destination) {
if (filter.isEmpty()) {
return this.doRemoveItems(amount, null, destination, Actionable.MODULATE);
}
return this.doRemoveItemsFuzzy(amount, filter, destination, Actionable.MODULATE, fuzzyMode);
}
private ItemStack doRemoveItemsFuzzy(final int amount, final ItemStack filter,
final IInventoryDestination destination, final Actionable type, final FuzzyMode fuzzyMode) {
final IAEItemStack reqFilter = AEItemStack.fromItemStack(filter);
if (reqFilter == null) {
return ItemStack.EMPTY;
}
IAEItemStack out = null;
for (final IAEItemStack req : ImmutableList.copyOf(this.getList().findFuzzy(reqFilter, fuzzyMode))) {
if (req != null) {
req.setStackSize(amount);
out = this.target.extractItems(req, type, this.src);
if (out != null) {
return out.createItemStack();
}
}
}
return ItemStack.EMPTY;
}
@Override
public ItemStack simulateSimilarRemove(final int amount, final ItemStack filter, final FuzzyMode fuzzyMode,
final IInventoryDestination destination) {
if (filter.isEmpty()) {
return this.doRemoveItems(amount, ItemStack.EMPTY, destination, Actionable.SIMULATE);
}
return this.doRemoveItemsFuzzy(amount, filter, destination, Actionable.SIMULATE, fuzzyMode);
}
@Override
public ItemStack addItems(final ItemStack toBeAdded) {
final IAEItemStack in = AEItemStack.fromItemStack(toBeAdded);
if (in != null) {
final IAEItemStack out = this.target.injectItems(in, Actionable.MODULATE, this.src);
if (out != null) {
return out.createItemStack();
}
}
return ItemStack.EMPTY;
}
@Override
public ItemStack simulateAdd(final ItemStack toBeSimulated) {
final IAEItemStack in = AEItemStack.fromItemStack(toBeSimulated);
if (in != null) {
final IAEItemStack out = this.target.injectItems(in, Actionable.SIMULATE, this.src);
if (out != null) {
return out.createItemStack();
}
}
return ItemStack.EMPTY;
}
@Override
public boolean containsItems() {
return !this.getList().isEmpty();
}
int getMaxSlots() {
return this.maxSlots;
}
void setMaxSlots(final int maxSlots) {
this.maxSlots = maxSlots;
}
}
@@ -1,73 +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 appeng.api.storage.data.IAEItemStack;
import appeng.api.storage.data.IItemList;
public final class IMEAdaptorIterator implements Iterator<ItemSlot> {
private final Iterator<IAEItemStack> stack;
private final ItemSlot slot = new ItemSlot();
private final IMEAdaptor parent;
private final int containerSize;
private int offset = 0;
private boolean hasNext;
public IMEAdaptorIterator(final IMEAdaptor parent, final IItemList<IAEItemStack> availableItems) {
this.stack = availableItems.iterator();
this.containerSize = parent.getMaxSlots();
this.parent = parent;
}
@Override
public boolean hasNext() {
this.hasNext = this.stack.hasNext();
return this.offset < this.containerSize || this.hasNext;
}
@Override
public ItemSlot next() {
this.slot.setSlot(this.offset);
this.offset++;
this.slot.setExtractable(true);
if (this.parent.getMaxSlots() < this.offset) {
this.parent.setMaxSlots(this.offset);
}
if (this.hasNext) {
final IAEItemStack item = this.stack.next();
this.slot.setAEItemStack(item);
return this.slot;
}
this.slot.setItemStack(ItemStack.EMPTY);
return this.slot;
}
@Override
public void remove() {
throw new UnsupportedOperationException();
}
}
@@ -1,50 +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 net.minecraft.item.ItemStack;
import appeng.api.config.Actionable;
import appeng.api.storage.IMEInventory;
import appeng.api.storage.data.IAEItemStack;
import appeng.util.item.AEItemStack;
public class IMEInventoryDestination implements IInventoryDestination {
private final IMEInventory<IAEItemStack> me;
public IMEInventoryDestination(final IMEInventory<IAEItemStack> o) {
this.me = o;
}
@Override
public boolean canInsert(final ItemStack stack) {
if (stack.isEmpty()) {
return false;
}
final IAEItemStack failed = this.me.injectItems(AEItemStack.fromItemStack(stack), Actionable.SIMULATE, null);
if (failed == null) {
return true;
}
return failed.getStackSize() != stack.getCount();
}
}
@@ -1,23 +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;
public enum InvOperation {
EXTRACT, INSERT, SET
}
@@ -1,56 +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 java.util.NoSuchElementException;
import alexiil.mc.lib.attributes.Simulation;
import alexiil.mc.lib.attributes.item.FixedItemInv;
class ItemHandlerIterator implements Iterator<ItemSlot> {
private final FixedItemInv itemHandler;
private final ItemSlot itemSlot = new ItemSlot();
private int slot = 0;
ItemHandlerIterator(FixedItemInv itemHandler) {
this.itemHandler = itemHandler;
}
@Override
public boolean hasNext() {
return this.slot < this.itemHandler.getSlotCount();
}
@Override
public ItemSlot next() {
if (this.slot >= this.itemHandler.getSlotCount()) {
throw new NoSuchElementException();
}
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;
}
}
@@ -1,95 +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.Collection;
import java.util.Iterator;
import appeng.api.config.FuzzyMode;
import appeng.api.storage.data.IAEStack;
import appeng.api.storage.data.IItemList;
public class ItemListIgnoreCrafting<T extends IAEStack<T>> implements IItemList<T> {
private final IItemList<T> target;
public ItemListIgnoreCrafting(final IItemList<T> cla) {
this.target = cla;
}
@Override
public void add(T option) {
if (option != null && option.isCraftable()) {
option = option.copy();
option.setCraftable(false);
}
this.target.add(option);
}
@Override
public T findPrecise(final T i) {
return this.target.findPrecise(i);
}
@Override
public Collection<T> findFuzzy(final T input, final FuzzyMode fuzzy) {
return this.target.findFuzzy(input, fuzzy);
}
@Override
public boolean isEmpty() {
return this.target.isEmpty();
}
@Override
public void addStorage(final T option) {
this.target.addStorage(option);
}
@Override
public void addCrafting(final T option) {
// nothing.
}
@Override
public void addRequestable(final T option) {
this.target.addRequestable(option);
}
@Override
public T getFirstItem() {
return this.target.getFirstItem();
}
@Override
public int size() {
return this.target.size();
}
@Override
public Iterator<T> iterator() {
return this.target.iterator();
}
@Override
public void resetStatus() {
this.target.resetStatus();
}
}
@@ -1,71 +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 net.minecraft.item.ItemStack;
import appeng.api.storage.data.IAEItemStack;
import appeng.util.item.AEItemStack;
public class ItemSlot {
private int slot;
private boolean isExtractable;
// one or the other..
private IAEItemStack aeItemStack;
private ItemStack itemStack;
public ItemStack getItemStack() {
return this.itemStack.isEmpty()
? (this.aeItemStack == null ? ItemStack.EMPTY : (this.itemStack = this.aeItemStack.createItemStack()))
: this.itemStack;
}
public void setItemStack(final ItemStack is) {
this.aeItemStack = null;
this.itemStack = is;
}
public IAEItemStack getAEItemStack() {
return this.aeItemStack == null
? (this.itemStack.isEmpty() ? null : (this.aeItemStack = AEItemStack.fromItemStack(this.itemStack)))
: this.aeItemStack;
}
void setAEItemStack(final IAEItemStack is) {
this.aeItemStack = is;
this.itemStack = ItemStack.EMPTY;
}
public boolean isExtractable() {
return this.isExtractable;
}
void setExtractable(final boolean isExtractable) {
this.isExtractable = isExtractable;
}
public int getSlot() {
return this.slot;
}
public void setSlot(final int slot) {
this.slot = slot;
}
}
@@ -1,34 +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 alexiil.mc.lib.attributes.item.FixedItemInv;
import alexiil.mc.lib.attributes.item.impl.CombinedFixedItemInv;
import alexiil.mc.lib.attributes.item.impl.DelegatingFixedItemInv;
import java.util.Arrays;
// FIXME could be replaced by CombinedFixedItemInv directly
public class WrapperChainedItemHandler extends DelegatingFixedItemInv {
public WrapperChainedItemHandler(FixedItemInv... itemHandler) {
super(CombinedFixedItemInv.create(Arrays.asList(itemHandler)));
}
}
@@ -1,31 +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 alexiil.mc.lib.attributes.item.compat.FixedInventoryVanillaWrapper;
import alexiil.mc.lib.attributes.item.impl.DelegatingFixedItemInv;
import net.minecraft.entity.player.PlayerInventory;
public class WrapperCursorItemHandler extends DelegatingFixedItemInv {
public WrapperCursorItemHandler(PlayerInventory inventory) {
super(new FixedInventoryVanillaWrapper(inventory).getSubInv(0, 1));
}
}
@@ -1,80 +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 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.FixedItemInv;
import appeng.util.inv.filter.IAEItemFilter;
// 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 FixedItemInv handler, @Nonnull IAEItemFilter filter) {
this.handler = handler;
this.filter = filter;
}
@Override
public ItemStack getInvStack(int slot) {
return this.handler.getInvStack(slot);
}
@Override
public boolean setInvStack(int slot, ItemStack to, Simulation simulation) {
return this.handler.setInvStack(slot, to, simulation);
}
@Override
public int getSlotCount() {
return this.handler.getSlotCount();
}
@Override
public boolean isItemValidForSlot(int slot, ItemStack stack) {
return filter.allowInsert(handler, slot, stack);
}
@Override
public ItemFilter getFilterForSlot(int slot) {
return stack -> filter.allowInsert(handler, slot, stack);
}
@Override
public int getChangeValue() {
return this.handler.getChangeValue();
}
@Nullable
@Override
public ListenerToken addListener(InvMarkDirtyListener listener, ListenerRemovalToken removalToken) {
return this.handler.addListener(listener, removalToken);
}
}
@@ -1,36 +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 alexiil.mc.lib.attributes.item.FixedItemInv;
import alexiil.mc.lib.attributes.item.compat.InventoryFixedWrapper;
import net.minecraft.entity.player.PlayerEntity;
public class WrapperInvItemHandler extends InventoryFixedWrapper {
public WrapperInvItemHandler(final FixedItemInv inv) {
super(inv);
}
@Override
public boolean canPlayerUse(PlayerEntity player) {
return false;
}
}
@@ -1,77 +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 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.FixedItemInv;
import javax.annotation.Nullable;
public class WrapperSupplierItemHandler implements FixedItemInv {
private final Supplier<FixedItemInv> sourceHandler;
public WrapperSupplierItemHandler(Supplier<FixedItemInv> source) {
this.sourceHandler = source;
}
@Override
public GroupedItemInv getGroupedInv() {
return new DelegatingGroupedItemInv(this.sourceHandler.get().getGroupedInv());
}
@Override
public ItemStack getInvStack(int slot) {
return this.sourceHandler.get().getInvStack(slot);
}
@Override
public boolean setInvStack(int slot, ItemStack to, Simulation simulation) {
return this.sourceHandler.get().setInvStack(slot, to, simulation);
}
@Override
public int getSlotCount() {
return this.sourceHandler.get().getSlotCount();
}
@Override
public boolean isItemValidForSlot(int slot, ItemStack stack) {
return this.sourceHandler.get().isItemValidForSlot(slot, stack);
}
@Override
public int getChangeValue() {
return this.sourceHandler.get().getChangeValue();
}
@Nullable
@Override
public ListenerToken addListener(InvMarkDirtyListener listener, ListenerRemovalToken removalToken) {
return this.sourceHandler.get().addListener(listener, removalToken);
}
}
@@ -1,43 +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.filter;
import alexiil.mc.lib.attributes.item.FixedItemInv;
import net.minecraft.item.ItemStack;
import appeng.api.definitions.IItemDefinition;
public class AEItemDefinitionFilter implements IAEItemFilter {
private final IItemDefinition definition;
public AEItemDefinitionFilter(IItemDefinition definition) {
this.definition = definition;
}
@Override
public boolean allowExtract(FixedItemInv inv, int slot, int amount) {
return true;
}
@Override
public boolean allowInsert(FixedItemInv inv, int slot, ItemStack stack) {
return this.definition.isSameAs(stack);
}
}
@@ -1,54 +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.filter;
import alexiil.mc.lib.attributes.item.FixedItemInv;
import net.minecraft.item.ItemStack;
public class AEItemFilters {
public static final IAEItemFilter INSERT_ONLY = new InsertOnlyFilter();
public static final IAEItemFilter EXTRACT_ONLY = new ExtractOnlyFilter();
private AEItemFilters() {
}
private static class InsertOnlyFilter implements IAEItemFilter {
@Override
public boolean allowExtract(FixedItemInv inv, int slot, int amount) {
return false;
}
@Override
public boolean allowInsert(FixedItemInv inv, int slot, ItemStack stack) {
return true;
}
}
private static class ExtractOnlyFilter implements IAEItemFilter {
@Override
public boolean allowExtract(FixedItemInv inv, int slot, int amount) {
return true;
}
@Override
public boolean allowInsert(FixedItemInv inv, int slot, ItemStack stack) {
return false;
}
}
}
@@ -1,28 +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.filter;
import alexiil.mc.lib.attributes.item.FixedItemInv;
import net.minecraft.item.ItemStack;
public interface IAEItemFilter {
boolean allowExtract(FixedItemInv inv, int slot, int amount);
boolean allowInsert(FixedItemInv inv, int slot, ItemStack stack);
}