mirror of
https://github.com/GTNewHorizons/ForestryMC.git
synced 2026-11-17 10:55:06 +00:00
Fix #1112 Improve IItemHandler Integration and Backpack
This commit is contained in:
1 parent
a312afedd3
commit
0ae2e36e90
33 files changed
+312
-1157
No files matched your search
+1
-1
Submodule lang updated: e26246bb7a...371d98e450.
@@ -6,10 +6,11 @@
|
||||
package forestry.api.storage;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
import net.minecraft.item.ItemStack;
|
||||
|
||||
public interface IBackpackDefinition {
|
||||
public interface IBackpackDefinition extends Predicate<ItemStack> {
|
||||
|
||||
/**
|
||||
* @return Human-readable name of the backpack.
|
||||
@@ -38,8 +39,9 @@ public interface IBackpackDefinition {
|
||||
void addValidOreDictName(String oreDictName);
|
||||
|
||||
/**
|
||||
* Returns true if the itemstack is a valid item for this backpack type.
|
||||
* Returns true if the ItemStack is a valid item for this backpack type.
|
||||
*/
|
||||
boolean isValidItem(ItemStack itemstack);
|
||||
@Override
|
||||
boolean test(ItemStack itemstack);
|
||||
|
||||
}
|
||||
@@ -32,8 +32,6 @@ import forestry.apiculture.worldgen.Hive;
|
||||
import forestry.apiculture.worldgen.HiveDecorator;
|
||||
import forestry.apiculture.worldgen.HiveDescriptionSwarmer;
|
||||
import forestry.core.inventory.IInventoryAdapter;
|
||||
import forestry.core.inventory.iterators.IExtInvSlot;
|
||||
import forestry.core.inventory.iterators.InventoryIterator;
|
||||
import forestry.core.proxy.Proxies;
|
||||
import forestry.core.tiles.IActivatable;
|
||||
import forestry.core.utils.ItemStackUtil;
|
||||
@@ -116,11 +114,11 @@ public class TileAlvearySwarmer extends TileAlveary implements ISidedInventory,
|
||||
return 0;
|
||||
}
|
||||
|
||||
for (IExtInvSlot slot : InventoryIterator.getIterable(getInternalInventory())) {
|
||||
ItemStack stack = slot.getStackInSlot();
|
||||
for (int slotIndex = 0; slotIndex < getSizeInventory(); slotIndex++) {
|
||||
ItemStack stack = getStackInSlot(slotIndex);
|
||||
for (Entry<ItemStack, Integer> entry : BeeManager.inducers.entrySet()) {
|
||||
if (ItemStackUtil.isIdenticalItem(entry.getKey(), stack)) {
|
||||
slot.decreaseStackInSlot();
|
||||
decrStackSize(slotIndex, 1);
|
||||
return entry.getValue();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,10 +17,11 @@ import java.util.Comparator;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
import net.minecraft.inventory.IInventory;
|
||||
import net.minecraft.tileentity.TileEntity;
|
||||
import net.minecraft.util.EnumFacing;
|
||||
|
||||
import net.minecraftforge.items.IItemHandler;
|
||||
|
||||
import forestry.core.tiles.AdjacentTileCache;
|
||||
import forestry.core.tiles.TileUtil;
|
||||
|
||||
@@ -35,9 +36,9 @@ public final class AdjacentInventoryCache implements AdjacentTileCache.ICacheLis
|
||||
|
||||
private final AdjacentTileCache cache;
|
||||
private boolean changed = true;
|
||||
private final List<IInventory> invs = new LinkedList<>();
|
||||
private final IInventory[] sides = new IInventory[6];
|
||||
private final Comparator<IInventory> sorter;
|
||||
private final List<IItemHandler> invs = new LinkedList<>();
|
||||
private final IItemHandler[] sides = new IItemHandler[6];
|
||||
private final Comparator<IItemHandler> sorter;
|
||||
private final ITileFilter filter;
|
||||
|
||||
public AdjacentInventoryCache(TileEntity tile, AdjacentTileCache cache) {
|
||||
@@ -48,19 +49,19 @@ public final class AdjacentInventoryCache implements AdjacentTileCache.ICacheLis
|
||||
this(tile, cache, filter, null);
|
||||
}
|
||||
|
||||
public AdjacentInventoryCache(TileEntity tile, AdjacentTileCache cache, ITileFilter filter, Comparator<IInventory> sorter) {
|
||||
public AdjacentInventoryCache(TileEntity tile, AdjacentTileCache cache, ITileFilter filter, Comparator<IItemHandler> sorter) {
|
||||
this.cache = cache;
|
||||
this.filter = filter;
|
||||
this.sorter = sorter;
|
||||
cache.addListener(this);
|
||||
}
|
||||
|
||||
public IInventory getAdjacentInventory(EnumFacing side) {
|
||||
public IItemHandler getAdjacentInventory(EnumFacing side) {
|
||||
checkChanged();
|
||||
return sides[side.ordinal()];
|
||||
}
|
||||
|
||||
public Collection<IInventory> getAdjacentInventories() {
|
||||
public Collection<IItemHandler> getAdjacentInventories() {
|
||||
checkChanged();
|
||||
return invs;
|
||||
}
|
||||
@@ -84,7 +85,7 @@ public final class AdjacentInventoryCache implements AdjacentTileCache.ICacheLis
|
||||
for (EnumFacing side : EnumFacing.values()) {
|
||||
TileEntity tile = cache.getTileOnSide(side);
|
||||
if (tile != null && (filter == null || filter.matches(tile))) {
|
||||
IInventory inv = TileUtil.getInventoryFromTile(tile, side.getOpposite());
|
||||
IItemHandler inv = TileUtil.getInventoryFromTile(tile, side.getOpposite());
|
||||
if (inv != null) {
|
||||
sides[side.ordinal()] = inv;
|
||||
invs.add(inv);
|
||||
|
||||
@@ -1,36 +0,0 @@
|
||||
/******************************************************************************
|
||||
* Copyright (c) CovertJaguar, 2011-2016 *
|
||||
* http://railcraft.info *
|
||||
* *
|
||||
* This code is the property of CovertJaguar *
|
||||
* and may only be used with explicit written *
|
||||
* permission unless otherwise specified on the *
|
||||
* license page at http://railcraft.info/wiki/info:license. *
|
||||
******************************************************************************/
|
||||
|
||||
package forestry.core.inventory;
|
||||
|
||||
import net.minecraft.inventory.IInventory;
|
||||
|
||||
import net.minecraftforge.items.IItemHandler;
|
||||
|
||||
/**
|
||||
* Created by CovertJaguar on 3/15/2016 for Railcraft.
|
||||
*
|
||||
* @author CovertJaguar <http://www.railcraft.info>
|
||||
*/
|
||||
public class InventoryObject {
|
||||
private final Object inventory;
|
||||
|
||||
public InventoryObject(IInventory inventory) {
|
||||
this.inventory = inventory;
|
||||
}
|
||||
|
||||
public InventoryObject(IItemHandler inventory) {
|
||||
this.inventory = inventory;
|
||||
}
|
||||
|
||||
public Object getObject() {
|
||||
return inventory;
|
||||
}
|
||||
}
|
||||
@@ -34,7 +34,7 @@ public abstract class ItemInventory implements IInventory, IFilterSlotDelegate,
|
||||
private static final String KEY_UID = "UID";
|
||||
private static final Random rand = new Random();
|
||||
|
||||
private final IItemHandler capabilityHandler = new InvWrapper(this);
|
||||
private final IItemHandler itemHandler = new InvWrapper(this);
|
||||
private final EntityPlayer player;
|
||||
private final ItemStack parent;
|
||||
private final ItemStack[] inventoryStacks;
|
||||
@@ -302,8 +302,7 @@ public abstract class ItemInventory implements IInventory, IFilterSlotDelegate,
|
||||
@Override
|
||||
public <T> T getCapability(Capability<T> capability, EnumFacing facing) {
|
||||
if (capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY) {
|
||||
//noinspection unchecked
|
||||
return (T) capabilityHandler;
|
||||
return CapabilityItemHandler.ITEM_HANDLER_CAPABILITY.cast(itemHandler);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
@@ -312,4 +311,8 @@ public abstract class ItemInventory implements IInventory, IFilterSlotDelegate,
|
||||
public boolean hasCapability(Capability<?> capability, EnumFacing facing) {
|
||||
return capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY;
|
||||
}
|
||||
|
||||
public IItemHandler getItemHandler() {
|
||||
return itemHandler;
|
||||
}
|
||||
}
|
||||
@@ -1,48 +0,0 @@
|
||||
/*******************************************************************************
|
||||
* Copyright (c) 2011-2014 SirSengir.
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the GNU Lesser Public License v3
|
||||
* which accompanies this distribution, and is available at
|
||||
* http://www.gnu.org/licenses/lgpl-3.0.txt
|
||||
*
|
||||
* Various Contributors including, but not limited to:
|
||||
* SirSengir (original work), CovertJaguar, Player, Binnie, MysteriousAges
|
||||
******************************************************************************/
|
||||
package forestry.core.inventory.filters;
|
||||
|
||||
import net.minecraft.item.ItemStack;
|
||||
|
||||
import forestry.core.utils.InventoryUtil;
|
||||
|
||||
/**
|
||||
* @author CovertJaguar <http://www.railcraft.info>
|
||||
*/
|
||||
public class ArrayStackFilter extends StackFilter {
|
||||
|
||||
private final ItemStack[] stacks;
|
||||
|
||||
public ArrayStackFilter(ItemStack... stacks) {
|
||||
this.stacks = stacks;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean apply(final ItemStack stack) {
|
||||
if (stacks.length == 0 || !hasFilter()) {
|
||||
return true;
|
||||
}
|
||||
return InventoryUtil.isItemEqual(stack, stacks);
|
||||
}
|
||||
|
||||
public ItemStack[] getStacks() {
|
||||
return stacks;
|
||||
}
|
||||
|
||||
public boolean hasFilter() {
|
||||
for (ItemStack filter : stacks) {
|
||||
if (filter != null) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -1,31 +0,0 @@
|
||||
/*******************************************************************************
|
||||
* Copyright (c) 2011-2014 SirSengir.
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the GNU Lesser Public License v3
|
||||
* which accompanies this distribution, and is available at
|
||||
* http://www.gnu.org/licenses/lgpl-3.0.txt
|
||||
*
|
||||
* Various Contributors including, but not limited to:
|
||||
* SirSengir (original work), CovertJaguar, Player, Binnie, MysteriousAges
|
||||
******************************************************************************/
|
||||
package forestry.core.inventory.filters;
|
||||
|
||||
import com.google.common.base.Predicate;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
|
||||
import net.minecraft.item.ItemStack;
|
||||
|
||||
/**
|
||||
* This interface is used to provide a convenient means of dealing with entire classes of items without having to specify each item individually.
|
||||
*
|
||||
* @author CovertJaguar <http://www.railcraft.info>
|
||||
*/
|
||||
public interface IStackFilter extends Predicate<ItemStack> {
|
||||
|
||||
StackFilter and(@Nonnull Predicate<? super ItemStack>... other);
|
||||
|
||||
StackFilter or(@Nonnull Predicate<? super ItemStack>... other);
|
||||
|
||||
StackFilter negate();
|
||||
}
|
||||
@@ -1,123 +0,0 @@
|
||||
/*******************************************************************************
|
||||
* Copyright (c) 2011-2014 SirSengir.
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the GNU Lesser Public License v3
|
||||
* which accompanies this distribution, and is available at
|
||||
* http://www.gnu.org/licenses/lgpl-3.0.txt
|
||||
*
|
||||
* Various Contributors including, but not limited to:
|
||||
* SirSengir (original work), CovertJaguar, Player, Binnie, MysteriousAges
|
||||
******************************************************************************/
|
||||
package forestry.core.inventory.filters;
|
||||
|
||||
import com.google.common.base.Predicate;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import javax.annotation.Nullable;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
|
||||
import net.minecraft.item.ItemStack;
|
||||
|
||||
/**
|
||||
* This class is used to provide a convenient means of dealing with entire classes of items without having to specify each item individually.
|
||||
*
|
||||
* @author CovertJaguar <http://www.railcraft.info>
|
||||
*/
|
||||
@SuppressWarnings("unused")
|
||||
public class StackFilter implements IStackFilter {
|
||||
/**
|
||||
* Railcraft adds the following IItemTypes during preInit: ALL, FUEL, TRACK, MINECART, BALLAST, FEED
|
||||
* <p/>
|
||||
* Feel free to grab them from here or define your own.
|
||||
*/
|
||||
public static final Map<String, IStackFilter> standardFilters = new HashMap<>();
|
||||
|
||||
@Override
|
||||
public boolean apply(@Nullable final ItemStack input) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public final StackFilter and(@Nonnull final Predicate<? super ItemStack>... other) {
|
||||
Objects.requireNonNull(other);
|
||||
return new StackFilter() {
|
||||
@Override
|
||||
public boolean apply(ItemStack stack) {
|
||||
for (Predicate<? super ItemStack> filter : other) {
|
||||
if (!filter.apply(stack)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return StackFilter.this.apply(stack);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
public final StackFilter or(@Nonnull final Predicate<? super ItemStack>... other) {
|
||||
Objects.requireNonNull(other);
|
||||
return new StackFilter() {
|
||||
@Override
|
||||
public boolean apply(ItemStack stack) {
|
||||
for (Predicate<? super ItemStack> filter : other) {
|
||||
if (filter.apply(stack)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return StackFilter.this.apply(stack);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
public final StackFilter negate() {
|
||||
return new StackFilter() {
|
||||
@Override
|
||||
public boolean apply(ItemStack stack) {
|
||||
return !StackFilter.this.apply(stack);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
public static StackFilter buildAnd(@Nonnull final Predicate<? super ItemStack>... filters) {
|
||||
return new StackFilter() {
|
||||
@Override
|
||||
public boolean apply(ItemStack stack) {
|
||||
Objects.requireNonNull(stack);
|
||||
for (Predicate<? super ItemStack> filter : filters) {
|
||||
if (!filter.apply(stack)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
public static StackFilter buildOr(@Nonnull final Predicate<? super ItemStack>... filters) {
|
||||
return new StackFilter() {
|
||||
@Override
|
||||
public boolean apply(ItemStack stack) {
|
||||
Objects.requireNonNull(stack);
|
||||
for (Predicate<? super ItemStack> filter : filters) {
|
||||
if (filter.apply(stack)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
public static StackFilter invert(@Nonnull final Predicate<? super ItemStack> filter) {
|
||||
return new StackFilter() {
|
||||
@Override
|
||||
public boolean apply(ItemStack stack) {
|
||||
return !filter.apply(stack);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
}
|
||||
@@ -10,10 +10,7 @@
|
||||
******************************************************************************/
|
||||
package forestry.core.inventory.filters;
|
||||
|
||||
import com.google.common.base.Predicate;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import java.util.Objects;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
import net.minecraft.init.Items;
|
||||
import net.minecraft.item.ItemFood;
|
||||
@@ -28,76 +25,24 @@ import net.minecraft.tileentity.TileEntityFurnace;
|
||||
*
|
||||
* @author CovertJaguar <http://www.railcraft.info>
|
||||
*/
|
||||
public enum StandardStackFilters implements IStackFilter {
|
||||
public enum StandardStackFilters implements Predicate<ItemStack> {
|
||||
|
||||
ALL {
|
||||
@Override
|
||||
public boolean apply(ItemStack stack) {
|
||||
public boolean test(ItemStack stack) {
|
||||
return true;
|
||||
}
|
||||
},
|
||||
FUEL {
|
||||
@Override
|
||||
public boolean apply(ItemStack stack) {
|
||||
public boolean test(ItemStack stack) {
|
||||
return TileEntityFurnace.getItemBurnTime(stack) > 0;
|
||||
}
|
||||
},
|
||||
FEED {
|
||||
@Override
|
||||
public boolean apply(ItemStack stack) {
|
||||
public boolean test(ItemStack stack) {
|
||||
return stack.getItem() instanceof ItemFood || stack.getItem() == Items.WHEAT || stack.getItem() instanceof ItemSeeds;
|
||||
}
|
||||
};
|
||||
|
||||
public static void initialize() {
|
||||
for (StandardStackFilters type : StandardStackFilters.values()) {
|
||||
StackFilter.standardFilters.put(type.name(), type);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public abstract boolean apply(ItemStack stack);
|
||||
|
||||
@Override
|
||||
public final StackFilter and(@Nonnull final Predicate<? super ItemStack>... other) {
|
||||
Objects.requireNonNull(other);
|
||||
return new StackFilter() {
|
||||
@Override
|
||||
public boolean apply(ItemStack stack) {
|
||||
for (Predicate<? super ItemStack> filter : other) {
|
||||
if (!filter.apply(stack)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return StandardStackFilters.this.apply(stack);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
public final StackFilter or(@Nonnull final Predicate<? super ItemStack>... other) {
|
||||
Objects.requireNonNull(other);
|
||||
return new StackFilter() {
|
||||
@Override
|
||||
public boolean apply(ItemStack stack) {
|
||||
for (Predicate<? super ItemStack> filter : other) {
|
||||
if (filter.apply(stack)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return StandardStackFilters.this.apply(stack);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
public final StackFilter negate() {
|
||||
return new StackFilter() {
|
||||
@Override
|
||||
public boolean apply(ItemStack stack) {
|
||||
return !StandardStackFilters.this.apply(stack);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,30 +0,0 @@
|
||||
/*******************************************************************************
|
||||
* Copyright (c) 2011-2014 SirSengir.
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the GNU Lesser Public License v3
|
||||
* which accompanies this distribution, and is available at
|
||||
* http://www.gnu.org/licenses/lgpl-3.0.txt
|
||||
*
|
||||
* Various Contributors including, but not limited to:
|
||||
* SirSengir (original work), CovertJaguar, Player, Binnie, MysteriousAges
|
||||
******************************************************************************/
|
||||
|
||||
package forestry.core.inventory.iterators;
|
||||
|
||||
import net.minecraft.item.ItemStack;
|
||||
|
||||
/**
|
||||
* This interface extends IInvSlot by allowing you to modify a slot directly.
|
||||
* This is only valid on inventories backed by IInventory.
|
||||
* <p/>
|
||||
* <p/>
|
||||
* Created by CovertJaguar on 3/16/2016 for Railcraft.
|
||||
*
|
||||
* @author CovertJaguar <http://www.railcraft.info>
|
||||
*/
|
||||
public interface IExtInvSlot extends IInvSlot {
|
||||
/**
|
||||
* Sets the current ItemStack in the slot.
|
||||
*/
|
||||
void setStackInSlot(ItemStack stack);
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package forestry.core.inventory.iterators;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
import net.minecraftforge.items.IItemHandler;
|
||||
|
||||
public class InvIterator implements Iterator<IInvSlot> {
|
||||
private final IItemHandler inv;
|
||||
private int slot = 0;
|
||||
|
||||
public InvIterator(IItemHandler inv) {
|
||||
this.inv = inv;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasNext() {
|
||||
return slot < inv.getSlots();
|
||||
}
|
||||
|
||||
@Override
|
||||
public IInvSlot next() {
|
||||
if (!hasNext()) {
|
||||
throw new NoSuchElementException();
|
||||
}
|
||||
return new InvSlot(inv, slot++);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
package forestry.core.inventory.iterators;
|
||||
|
||||
import net.minecraft.item.ItemStack;
|
||||
|
||||
import net.minecraftforge.items.IItemHandler;
|
||||
|
||||
public class InvSlot implements IInvSlot {
|
||||
|
||||
private final IItemHandler inv;
|
||||
protected final int slot;
|
||||
|
||||
public InvSlot(IItemHandler inv, int slot) {
|
||||
this.inv = inv;
|
||||
this.slot = slot;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getIndex() {
|
||||
return slot;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canPutStackInSlot(ItemStack stack) {
|
||||
ItemStack remainder = inv.insertItem(slot, stack, true);
|
||||
return remainder == null || remainder.stackSize < stack.stackSize;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canTakeStackFromSlot(ItemStack stack) {
|
||||
return inv.extractItem(slot, 1, true) != null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ItemStack decreaseStackInSlot() {
|
||||
return inv.extractItem(slot, 1, false);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ItemStack getStackInSlot() {
|
||||
ItemStack stack = inv.getStackInSlot(slot);
|
||||
if (stack == null || stack.stackSize <= 0) {
|
||||
return null;
|
||||
}
|
||||
return stack;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,33 +0,0 @@
|
||||
/*******************************************************************************
|
||||
* Copyright (c) 2011-2014 SirSengir.
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the GNU Lesser Public License v3
|
||||
* which accompanies this distribution, and is available at
|
||||
* http://www.gnu.org/licenses/lgpl-3.0.txt
|
||||
*
|
||||
* Various Contributors including, but not limited to:
|
||||
* SirSengir (original work), CovertJaguar, Player, Binnie, MysteriousAges
|
||||
******************************************************************************/
|
||||
package forestry.core.inventory.iterators;
|
||||
|
||||
import net.minecraft.inventory.IInventory;
|
||||
import net.minecraft.inventory.ISidedInventory;
|
||||
|
||||
import net.minecraftforge.items.IItemHandler;
|
||||
|
||||
/**
|
||||
* @author CovertJaguar <http://www.railcraft.info>
|
||||
*/
|
||||
public abstract class InventoryIterator<T extends IInvSlot> implements Iterable<T> {
|
||||
|
||||
public static InventoryIterator<IExtInvSlot> getIterable(IInventory inv) {
|
||||
if (inv instanceof ISidedInventory) {
|
||||
return new SidedInventoryIterator((ISidedInventory) inv);
|
||||
}
|
||||
return new StandardInventoryIterator(inv);
|
||||
}
|
||||
|
||||
public static InventoryIterator<IInvSlot> getIterable(IItemHandler inv) {
|
||||
return new ItemHandlerInventoryIterator(inv);
|
||||
}
|
||||
}
|
||||
@@ -1,89 +0,0 @@
|
||||
/*******************************************************************************
|
||||
* Copyright (c) 2011-2014 SirSengir.
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the GNU Lesser Public License v3
|
||||
* which accompanies this distribution, and is available at
|
||||
* http://www.gnu.org/licenses/lgpl-3.0.txt
|
||||
*
|
||||
* Various Contributors including, but not limited to:
|
||||
* SirSengir (original work), CovertJaguar, Player, Binnie, MysteriousAges
|
||||
******************************************************************************/
|
||||
package forestry.core.inventory.iterators;
|
||||
|
||||
import java.util.Iterator;
|
||||
|
||||
import net.minecraft.item.ItemStack;
|
||||
|
||||
import net.minecraftforge.items.IItemHandler;
|
||||
|
||||
import forestry.core.utils.InventoryUtil;
|
||||
|
||||
/**
|
||||
* @author CovertJaguar <http://www.railcraft.info>
|
||||
*/
|
||||
public class ItemHandlerInventoryIterator extends InventoryIterator<IInvSlot> {
|
||||
|
||||
private final IItemHandler inv;
|
||||
|
||||
protected ItemHandlerInventoryIterator(IItemHandler inv) {
|
||||
this.inv = inv;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterator<IInvSlot> iterator() {
|
||||
return new Iterator<IInvSlot>() {
|
||||
int slot = 0;
|
||||
|
||||
@Override
|
||||
public boolean hasNext() {
|
||||
return slot < inv.getSlots();
|
||||
}
|
||||
|
||||
@Override
|
||||
public IInvSlot next() {
|
||||
return new InvSlot(slot++);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void remove() {
|
||||
throw new UnsupportedOperationException("Remove not supported.");
|
||||
}
|
||||
|
||||
};
|
||||
}
|
||||
|
||||
private class InvSlot implements IInvSlot {
|
||||
|
||||
protected final int slot;
|
||||
|
||||
public InvSlot(int slot) {
|
||||
this.slot = slot;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getIndex() {
|
||||
return slot;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canPutStackInSlot(ItemStack stack) {
|
||||
return inv.insertItem(slot, stack, true) == null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canTakeStackFromSlot(ItemStack stack) {
|
||||
return inv.extractItem(slot, 1, true) != null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ItemStack decreaseStackInSlot() {
|
||||
return inv.extractItem(slot, 1, false);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ItemStack getStackInSlot() {
|
||||
return InventoryUtil.makeSafe(inv.getStackInSlot(slot));
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1,72 +0,0 @@
|
||||
/*******************************************************************************
|
||||
* Copyright (c) 2011-2014 SirSengir.
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the GNU Lesser Public License v3
|
||||
* which accompanies this distribution, and is available at
|
||||
* http://www.gnu.org/licenses/lgpl-3.0.txt
|
||||
*
|
||||
* Various Contributors including, but not limited to:
|
||||
* SirSengir (original work), CovertJaguar, Player, Binnie, MysteriousAges
|
||||
******************************************************************************/
|
||||
package forestry.core.inventory.iterators;
|
||||
|
||||
import java.util.Iterator;
|
||||
|
||||
import net.minecraft.inventory.ISidedInventory;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.util.EnumFacing;
|
||||
|
||||
/**
|
||||
* @author CovertJaguar <http://www.railcraft.info>
|
||||
*/
|
||||
public class SidedInventoryIterator extends StandardInventoryIterator {
|
||||
|
||||
private final ISidedInventory inv;
|
||||
|
||||
protected SidedInventoryIterator(ISidedInventory inv) {
|
||||
super(inv);
|
||||
this.inv = inv;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterator<IExtInvSlot> iterator() {
|
||||
return new Iterator<IExtInvSlot>() {
|
||||
int[] slots = inv.getSlotsForFace(EnumFacing.DOWN);
|
||||
int index = 0;
|
||||
|
||||
@Override
|
||||
public boolean hasNext() {
|
||||
return slots != null && index < slots.length;
|
||||
}
|
||||
|
||||
@Override
|
||||
public IExtInvSlot next() {
|
||||
return new InvSlot(slots[index++]);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void remove() {
|
||||
throw new UnsupportedOperationException("Remove not supported.");
|
||||
}
|
||||
|
||||
};
|
||||
}
|
||||
|
||||
private class InvSlot extends StandardInventoryIterator.InvSlot implements IExtInvSlot {
|
||||
|
||||
public InvSlot(int slot) {
|
||||
super(slot);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canPutStackInSlot(ItemStack stack) {
|
||||
return inv.canInsertItem(slot, stack, EnumFacing.DOWN);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canTakeStackFromSlot(ItemStack stack) {
|
||||
return inv.canExtractItem(slot, stack, EnumFacing.DOWN);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1,100 +0,0 @@
|
||||
/*******************************************************************************
|
||||
* Copyright (c) 2011-2014 SirSengir.
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the GNU Lesser Public License v3
|
||||
* which accompanies this distribution, and is available at
|
||||
* http://www.gnu.org/licenses/lgpl-3.0.txt
|
||||
*
|
||||
* Various Contributors including, but not limited to:
|
||||
* SirSengir (original work), CovertJaguar, Player, Binnie, MysteriousAges
|
||||
******************************************************************************/
|
||||
|
||||
package forestry.core.inventory.iterators;
|
||||
|
||||
import java.util.Iterator;
|
||||
|
||||
import net.minecraft.inventory.IInventory;
|
||||
import net.minecraft.item.ItemStack;
|
||||
|
||||
/**
|
||||
* @author CovertJaguar <http://www.railcraft.info>
|
||||
*/
|
||||
public class StandardInventoryIterator extends InventoryIterator<IExtInvSlot> {
|
||||
|
||||
private final IInventory inv;
|
||||
private final int invSize;
|
||||
|
||||
protected StandardInventoryIterator(IInventory inv) {
|
||||
this.inv = inv;
|
||||
this.invSize = inv.getSizeInventory();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterator<IExtInvSlot> iterator() {
|
||||
return new Iterator<IExtInvSlot>() {
|
||||
int slot = 0;
|
||||
|
||||
@Override
|
||||
public boolean hasNext() {
|
||||
return slot < invSize;
|
||||
}
|
||||
|
||||
@Override
|
||||
public IExtInvSlot next() {
|
||||
return new InvSlot(slot++);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void remove() {
|
||||
throw new UnsupportedOperationException("Remove not supported.");
|
||||
}
|
||||
|
||||
};
|
||||
}
|
||||
|
||||
class InvSlot implements IExtInvSlot {
|
||||
|
||||
protected final int slot;
|
||||
|
||||
public InvSlot(int slot) {
|
||||
this.slot = slot;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ItemStack getStackInSlot() {
|
||||
return inv.getStackInSlot(slot);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setStackInSlot(ItemStack stack) {
|
||||
inv.setInventorySlotContents(slot, stack);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canPutStackInSlot(ItemStack stack) {
|
||||
return inv.isItemValidForSlot(slot, stack);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canTakeStackFromSlot(ItemStack stack) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ItemStack decreaseStackInSlot() {
|
||||
return inv.decrStackSize(slot, 1);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getIndex() {
|
||||
return slot;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
ItemStack stack = getStackInSlot();
|
||||
return "SlotNum = " + slot + " Stack = " + (stack == null ? "null" : getStackInSlot().toString());
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1,77 +0,0 @@
|
||||
/*******************************************************************************
|
||||
* Copyright (c) 2011-2014 SirSengir.
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the GNU Lesser Public License v3
|
||||
* which accompanies this distribution, and is available at
|
||||
* http://www.gnu.org/licenses/lgpl-3.0.txt
|
||||
*
|
||||
* Various Contributors including, but not limited to:
|
||||
* SirSengir (original work), CovertJaguar, Player, Binnie, MysteriousAges
|
||||
******************************************************************************/
|
||||
package forestry.core.inventory.manipulators;
|
||||
|
||||
import net.minecraft.inventory.IInventory;
|
||||
import net.minecraft.item.ItemStack;
|
||||
|
||||
import net.minecraftforge.items.IItemHandler;
|
||||
|
||||
import forestry.core.inventory.InventoryObject;
|
||||
import forestry.core.inventory.filters.IStackFilter;
|
||||
import forestry.core.inventory.iterators.IExtInvSlot;
|
||||
import forestry.core.inventory.iterators.IInvSlot;
|
||||
|
||||
/**
|
||||
* @author CovertJaguar <http://www.railcraft.info>
|
||||
*/
|
||||
public abstract class InventoryManipulator<T extends IInvSlot> implements Iterable<T> {
|
||||
|
||||
public static InventoryManipulator<IExtInvSlot> get(IInventory inv) {
|
||||
return new StandardInventoryManipulator(inv);
|
||||
}
|
||||
|
||||
public static InventoryManipulator<IInvSlot> get(IItemHandler inv) {
|
||||
return new ItemHandlerInventoryManipulator(inv);
|
||||
}
|
||||
|
||||
public static InventoryManipulator<? extends IInvSlot> get(InventoryObject inv) {
|
||||
if (inv.getObject() instanceof IInventory) {
|
||||
return new StandardInventoryManipulator((IInventory) inv.getObject());
|
||||
}
|
||||
if (inv.getObject() instanceof IItemHandler) {
|
||||
return new ItemHandlerInventoryManipulator((IItemHandler) inv.getObject());
|
||||
}
|
||||
throw new RuntimeException("Invalid Inventory Object");
|
||||
}
|
||||
|
||||
protected InventoryManipulator() {
|
||||
}
|
||||
|
||||
public ItemStack tryAddStack(ItemStack stack) {
|
||||
return addStack(stack, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Attempt to add the stack to the inventory.
|
||||
*
|
||||
* @return The remainder
|
||||
*/
|
||||
public ItemStack addStack(ItemStack stack) {
|
||||
return addStack(stack, true);
|
||||
}
|
||||
|
||||
protected abstract ItemStack addStack(ItemStack stack, boolean doAdd);
|
||||
|
||||
/**
|
||||
* Removed an item matching the filter.
|
||||
*/
|
||||
public ItemStack removeItem(IStackFilter filter) {
|
||||
for (IInvSlot slot : this) {
|
||||
ItemStack stack = slot.getStackInSlot();
|
||||
if (stack != null && stack.stackSize > 0 && slot.canTakeStackFromSlot(stack) && filter.apply(stack)) {
|
||||
return slot.decreaseStackInSlot();
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
+80
-11
@@ -10,34 +10,101 @@
|
||||
******************************************************************************/
|
||||
package forestry.core.inventory.manipulators;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
import net.minecraft.item.ItemStack;
|
||||
|
||||
import net.minecraftforge.items.IItemHandler;
|
||||
|
||||
import forestry.core.inventory.iterators.IInvSlot;
|
||||
import forestry.core.inventory.iterators.InventoryIterator;
|
||||
import forestry.core.inventory.iterators.InvIterator;
|
||||
|
||||
/**
|
||||
* @author CovertJaguar <http://www.railcraft.info>
|
||||
*/
|
||||
public class ItemHandlerInventoryManipulator extends InventoryManipulator<IInvSlot> {
|
||||
public class ItemHandlerInventoryManipulator implements Iterable<IInvSlot> {
|
||||
|
||||
private final IItemHandler inv;
|
||||
|
||||
protected ItemHandlerInventoryManipulator(IItemHandler inv) {
|
||||
public ItemHandlerInventoryManipulator(IItemHandler inv) {
|
||||
this.inv = inv;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterator<IInvSlot> iterator() {
|
||||
return InventoryIterator.getIterable(inv).iterator();
|
||||
return new InvIterator(inv);
|
||||
}
|
||||
|
||||
@Override
|
||||
/**
|
||||
* Simulate adding the stack to the inventory.
|
||||
*
|
||||
* @return the remainder
|
||||
*/
|
||||
@Nullable
|
||||
public ItemStack tryAddStack(ItemStack stack) {
|
||||
return addStack(stack, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Attempt to add the stack to the inventory.
|
||||
*
|
||||
* @return The remainder
|
||||
*/
|
||||
@Nullable
|
||||
public ItemStack addStack(ItemStack stack) {
|
||||
return addStack(stack, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Removed an item matching the filter.
|
||||
*/
|
||||
public ItemStack removeItem(Predicate<ItemStack> filter) {
|
||||
for (IInvSlot slot : this) {
|
||||
ItemStack stack = slot.getStackInSlot();
|
||||
if (stack != null && stack.stackSize > 0 && slot.canTakeStackFromSlot(stack) && filter.test(stack)) {
|
||||
return slot.decreaseStackInSlot();
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public boolean transferOneStack(IItemHandler dest, Predicate<ItemStack> filter) {
|
||||
return transferStacks(dest, filter, true);
|
||||
}
|
||||
|
||||
public boolean transferStacks(IItemHandler dest, Predicate<ItemStack> filter) {
|
||||
return transferStacks(dest, filter, false);
|
||||
}
|
||||
|
||||
private boolean transferStacks(IItemHandler dest, Predicate<ItemStack> filter, boolean singleStack) {
|
||||
ItemHandlerInventoryManipulator destManipulator = new ItemHandlerInventoryManipulator(dest);
|
||||
boolean stacksMoved = false;
|
||||
for (int slotIndex = 0; slotIndex < inv.getSlots(); slotIndex++) {
|
||||
ItemStack targetStack = inv.extractItem(slotIndex, Integer.MAX_VALUE, true);
|
||||
if (targetStack != null && targetStack.stackSize > 0 && filter.test(targetStack)) {
|
||||
int extractStackSize = targetStack.stackSize;
|
||||
ItemStack remaining = destManipulator.tryAddStack(targetStack);
|
||||
if (remaining != null) {
|
||||
extractStackSize -= remaining.stackSize;
|
||||
}
|
||||
if (extractStackSize > 0) {
|
||||
ItemStack extracted = inv.extractItem(slotIndex, extractStackSize, false);
|
||||
destManipulator.addStack(extracted);
|
||||
stacksMoved = true;
|
||||
if (singleStack) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return stacksMoved;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
protected ItemStack addStack(ItemStack stack, boolean doAdd) {
|
||||
if (stack == null || stack.stackSize <= 0) {
|
||||
return null;
|
||||
@@ -45,7 +112,7 @@ public class ItemHandlerInventoryManipulator extends InventoryManipulator<IInvSl
|
||||
stack = stack.copy();
|
||||
List<IInvSlot> filledSlots = new ArrayList<>(inv.getSlots());
|
||||
List<IInvSlot> emptySlots = new ArrayList<>(inv.getSlots());
|
||||
for (IInvSlot slot : InventoryIterator.getIterable(inv)) {
|
||||
for (IInvSlot slot : new ItemHandlerInventoryManipulator(inv)) {
|
||||
if (slot.canPutStackInSlot(stack)) {
|
||||
if (slot.getStackInSlot() == null) {
|
||||
emptySlots.add(slot);
|
||||
@@ -70,13 +137,15 @@ public class ItemHandlerInventoryManipulator extends InventoryManipulator<IInvSl
|
||||
return injected;
|
||||
}
|
||||
for (IInvSlot slot : slots) {
|
||||
ItemStack stackToInsert = stack.copy();
|
||||
stackToInsert.stackSize = stack.stackSize - injected;
|
||||
ItemStack remainder = inv.insertItem(slot.getIndex(), stackToInsert, !doAdd);
|
||||
final ItemStack stackToInsert = stack.copy();
|
||||
final int stackToInsertSize = stack.stackSize - injected;
|
||||
stackToInsert.stackSize = stackToInsertSize;
|
||||
|
||||
final ItemStack remainder = inv.insertItem(slot.getIndex(), stackToInsert, !doAdd);
|
||||
if (remainder == null) {
|
||||
return injected;
|
||||
return stack.stackSize;
|
||||
}
|
||||
injected += remainder.stackSize;
|
||||
injected += stackToInsertSize - remainder.stackSize;
|
||||
if (injected >= stack.stackSize) {
|
||||
return injected;
|
||||
}
|
||||
|
||||
@@ -1,124 +0,0 @@
|
||||
/*******************************************************************************
|
||||
* Copyright (c) 2011-2014 SirSengir.
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the GNU Lesser Public License v3
|
||||
* which accompanies this distribution, and is available at
|
||||
* http://www.gnu.org/licenses/lgpl-3.0.txt
|
||||
*
|
||||
* Various Contributors including, but not limited to:
|
||||
* SirSengir (original work), CovertJaguar, Player, Binnie, MysteriousAges
|
||||
******************************************************************************/
|
||||
package forestry.core.inventory.manipulators;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
import net.minecraft.inventory.IInventory;
|
||||
import net.minecraft.item.ItemStack;
|
||||
|
||||
import forestry.core.inventory.iterators.IExtInvSlot;
|
||||
import forestry.core.inventory.iterators.InventoryIterator;
|
||||
import forestry.core.utils.InventoryUtil;
|
||||
|
||||
/**
|
||||
* @author CovertJaguar <http://www.railcraft.info>
|
||||
*/
|
||||
public class StandardInventoryManipulator extends InventoryManipulator<IExtInvSlot> {
|
||||
|
||||
private final IInventory inv;
|
||||
|
||||
protected StandardInventoryManipulator(IInventory inv) {
|
||||
this.inv = inv;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterator<IExtInvSlot> iterator() {
|
||||
return InventoryIterator.getIterable(inv).iterator();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected ItemStack addStack(ItemStack stack, boolean doAdd) {
|
||||
if (stack == null || stack.stackSize <= 0) {
|
||||
return null;
|
||||
}
|
||||
stack = stack.copy();
|
||||
List<IExtInvSlot> filledSlots = new ArrayList<>(inv.getSizeInventory());
|
||||
List<IExtInvSlot> emptySlots = new ArrayList<>(inv.getSizeInventory());
|
||||
for (IExtInvSlot slot : this) {
|
||||
if (slot.canPutStackInSlot(stack)) {
|
||||
if (slot.getStackInSlot() == null) {
|
||||
emptySlots.add(slot);
|
||||
} else {
|
||||
filledSlots.add(slot);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int injected = 0;
|
||||
injected = tryPut(filledSlots, stack, injected, doAdd);
|
||||
injected = tryPut(emptySlots, stack, injected, doAdd);
|
||||
stack.stackSize -= injected;
|
||||
if (stack.stackSize <= 0) {
|
||||
return null;
|
||||
}
|
||||
return stack;
|
||||
}
|
||||
|
||||
private int tryPut(List<IExtInvSlot> slots, ItemStack stack, int injected, boolean doAdd) {
|
||||
if (injected >= stack.stackSize) {
|
||||
return injected;
|
||||
}
|
||||
for (IExtInvSlot slot : slots) {
|
||||
ItemStack stackInSlot = slot.getStackInSlot();
|
||||
if (stackInSlot == null || InventoryUtil.isItemEqual(stackInSlot, stack)) {
|
||||
int used = addToSlot(slot, stack, stack.stackSize - injected, doAdd);
|
||||
if (used > 0) {
|
||||
injected += used;
|
||||
if (injected >= stack.stackSize) {
|
||||
return injected;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return injected;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param available Amount we can move
|
||||
* @return Return the number of items moved.
|
||||
*/
|
||||
private int addToSlot(IExtInvSlot slot, ItemStack stack, int available, boolean doAdd) {
|
||||
int max = Math.min(stack.getMaxStackSize(), inv.getInventoryStackLimit());
|
||||
|
||||
ItemStack stackInSlot = slot.getStackInSlot();
|
||||
if (stackInSlot == null) {
|
||||
int wanted = Math.min(available, max);
|
||||
if (doAdd) {
|
||||
stackInSlot = stack.copy();
|
||||
stackInSlot.stackSize = wanted;
|
||||
slot.setStackInSlot(stackInSlot);
|
||||
}
|
||||
return wanted;
|
||||
}
|
||||
|
||||
if (!InventoryUtil.isItemEqual(stack, stackInSlot)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int wanted = max - stackInSlot.stackSize;
|
||||
if (wanted <= 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (wanted > available) {
|
||||
wanted = available;
|
||||
}
|
||||
|
||||
if (doAdd) {
|
||||
stackInSlot.stackSize += wanted;
|
||||
slot.setStackInSlot(stackInSlot);
|
||||
}
|
||||
return wanted;
|
||||
}
|
||||
}
|
||||
@@ -1,66 +0,0 @@
|
||||
/*******************************************************************************
|
||||
* Copyright (c) 2011-2014 SirSengir.
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the GNU Lesser Public License v3
|
||||
* which accompanies this distribution, and is available at
|
||||
* http://www.gnu.org/licenses/lgpl-3.0.txt
|
||||
*
|
||||
* Various Contributors including, but not limited to:
|
||||
* SirSengir (original work), CovertJaguar, Player, Binnie, MysteriousAges
|
||||
******************************************************************************/
|
||||
package forestry.core.inventory.wrappers;
|
||||
|
||||
import net.minecraft.inventory.IInventory;
|
||||
import net.minecraft.item.ItemStack;
|
||||
|
||||
import forestry.core.inventory.InventoryPlain;
|
||||
import forestry.core.inventory.iterators.IExtInvSlot;
|
||||
import forestry.core.inventory.iterators.InventoryIterator;
|
||||
|
||||
/**
|
||||
* Creates a deep copy of an existing IInventory.
|
||||
* <p/>
|
||||
* Useful for performing inventory manipulations and then examining the results
|
||||
* without affecting the original inventory.
|
||||
*
|
||||
* @author CovertJaguar <http://www.railcraft.info>
|
||||
*/
|
||||
public class InventoryCopy extends InvWrapperBase {
|
||||
|
||||
private final InventoryPlain copy;
|
||||
|
||||
public InventoryCopy(IInventory original) {
|
||||
super(original);
|
||||
this.copy = new InventoryPlain(original.getSizeInventory());
|
||||
for (IExtInvSlot slot : InventoryIterator.getIterable(original)) {
|
||||
ItemStack stack = slot.getStackInSlot();
|
||||
if (stack != null) {
|
||||
copy.setInventorySlotContents(slot.getIndex(), stack.copy());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setInventorySlotContents(int slot, ItemStack itemstack) {
|
||||
copy.setInventorySlotContents(slot, itemstack);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ItemStack getStackInSlot(int slot) {
|
||||
return copy.getStackInSlot(slot);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ItemStack decrStackSize(int slot, int amount) {
|
||||
return copy.decrStackSize(slot, amount);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ItemStack removeStackFromSlot(int slot) {
|
||||
return copy.removeStackFromSlot(slot);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void markDirty() {
|
||||
}
|
||||
}
|
||||
@@ -15,8 +15,8 @@ import java.util.LinkedHashSet;
|
||||
import java.util.Set;
|
||||
|
||||
import net.minecraft.tileentity.TileEntity;
|
||||
import net.minecraft.util.math.BlockPos;
|
||||
import net.minecraft.util.EnumFacing;
|
||||
import net.minecraft.util.math.BlockPos;
|
||||
import net.minecraft.world.World;
|
||||
|
||||
/**
|
||||
@@ -56,7 +56,7 @@ public final class AdjacentTileCache {
|
||||
private TileEntity searchSide(EnumFacing side) {
|
||||
World world = source.getWorld();
|
||||
BlockPos pos = source.getPos().offset(side);
|
||||
if (world.isBlockLoaded(pos) && world.isAirBlock(pos)) {
|
||||
if (world.isBlockLoaded(pos) && !world.isAirBlock(pos)) {
|
||||
return world.getTileEntity(pos);
|
||||
}
|
||||
return null;
|
||||
|
||||
@@ -37,8 +37,6 @@ import forestry.core.fluids.tanks.FilteredTank;
|
||||
import forestry.core.gui.ContainerAnalyzer;
|
||||
import forestry.core.gui.GuiAnalyzer;
|
||||
import forestry.core.inventory.InventoryAnalyzer;
|
||||
import forestry.core.inventory.iterators.IExtInvSlot;
|
||||
import forestry.core.inventory.iterators.InventoryIterator;
|
||||
import forestry.core.inventory.wrappers.InventoryMapper;
|
||||
import forestry.core.network.DataInputStreamForestry;
|
||||
import forestry.core.network.DataOutputStreamForestry;
|
||||
@@ -132,11 +130,11 @@ public class TileAnalyzer extends TilePowered implements ISidedInventory, ILiqui
|
||||
return true;
|
||||
}
|
||||
|
||||
private IExtInvSlot getInputSlot() {
|
||||
for (IExtInvSlot slot : InventoryIterator.getIterable(invInput)) {
|
||||
ItemStack inputStack = slot.getStackInSlot();
|
||||
private Integer getInputSlotIndex() {
|
||||
for (int slotIndex = 0; slotIndex < invInput.getSizeInventory(); slotIndex++) {
|
||||
ItemStack inputStack = invInput.getStackInSlot(slotIndex);
|
||||
if (AlleleManager.alleleRegistry.isIndividual(inputStack)) {
|
||||
return slot;
|
||||
return slotIndex;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
@@ -197,12 +195,12 @@ public class TileAnalyzer extends TilePowered implements ISidedInventory, ILiqui
|
||||
return;
|
||||
}
|
||||
|
||||
IExtInvSlot slot = getInputSlot();
|
||||
if (slot == null) {
|
||||
Integer slotIndex = getInputSlotIndex();
|
||||
if (slotIndex == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
ItemStack inputStack = slot.getStackInSlot();
|
||||
ItemStack inputStack = invInput.getStackInSlot(slotIndex);
|
||||
if (inputStack == null) {
|
||||
return;
|
||||
}
|
||||
@@ -220,7 +218,7 @@ public class TileAnalyzer extends TilePowered implements ISidedInventory, ILiqui
|
||||
}
|
||||
|
||||
setInventorySlotContents(InventoryAnalyzer.SLOT_ANALYZE, inputStack);
|
||||
slot.setStackInSlot(null);
|
||||
invInput.setInventorySlotContents(slotIndex, null);
|
||||
|
||||
if (specimenToAnalyze.isAnalyzed()) {
|
||||
setTicksPerWorkCycle(1);
|
||||
|
||||
@@ -10,17 +10,21 @@
|
||||
******************************************************************************/
|
||||
package forestry.core.tiles;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
import net.minecraft.entity.player.EntityPlayer;
|
||||
import net.minecraft.inventory.IInventory;
|
||||
import net.minecraft.inventory.ISidedInventory;
|
||||
import net.minecraft.tileentity.TileEntity;
|
||||
import net.minecraft.tileentity.TileEntityChest;
|
||||
import net.minecraft.util.math.BlockPos;
|
||||
import net.minecraft.util.EnumFacing;
|
||||
import net.minecraft.util.math.BlockPos;
|
||||
import net.minecraft.world.IBlockAccess;
|
||||
import net.minecraft.world.World;
|
||||
|
||||
import forestry.core.inventory.wrappers.ChestWrapper;
|
||||
import forestry.core.utils.InventoryUtil;
|
||||
import net.minecraftforge.items.CapabilityItemHandler;
|
||||
import net.minecraftforge.items.IItemHandler;
|
||||
import net.minecraftforge.items.wrapper.InvWrapper;
|
||||
import net.minecraftforge.items.wrapper.SidedInvWrapper;
|
||||
|
||||
public abstract class TileUtil {
|
||||
|
||||
@@ -52,15 +56,23 @@ public abstract class TileUtil {
|
||||
}
|
||||
}
|
||||
|
||||
public static IInventory getInventoryFromTile(TileEntity tile, EnumFacing side) {
|
||||
if (!(tile instanceof IInventory)) {
|
||||
public static IItemHandler getInventoryFromTile(@Nullable TileEntity tile, @Nullable EnumFacing side) {
|
||||
if (tile == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (tile instanceof TileEntityChest) {
|
||||
TileEntityChest chest = (TileEntityChest) tile;
|
||||
return new ChestWrapper(chest);
|
||||
if (tile.hasCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, side)) {
|
||||
return tile.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, side);
|
||||
}
|
||||
return InventoryUtil.getInventory((IInventory) tile, side);
|
||||
|
||||
if (tile instanceof ISidedInventory) {
|
||||
return new SidedInvWrapper((ISidedInventory) tile, side);
|
||||
}
|
||||
|
||||
if (tile instanceof IInventory) {
|
||||
return new InvWrapper((IInventory) tile);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -15,7 +15,6 @@ import java.util.Collection;
|
||||
import net.minecraft.entity.item.EntityItem;
|
||||
import net.minecraft.entity.player.EntityPlayer;
|
||||
import net.minecraft.inventory.IInventory;
|
||||
import net.minecraft.inventory.ISidedInventory;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.nbt.NBTTagCompound;
|
||||
import net.minecraft.nbt.NBTTagList;
|
||||
@@ -25,32 +24,18 @@ import net.minecraft.world.World;
|
||||
|
||||
import net.minecraftforge.common.ForgeHooks;
|
||||
import net.minecraftforge.fml.common.Optional;
|
||||
import net.minecraftforge.items.IItemHandler;
|
||||
import net.minecraftforge.oredict.OreDictionary;
|
||||
|
||||
import forestry.api.core.ForestryAPI;
|
||||
import forestry.core.circuits.ISocketable;
|
||||
import forestry.core.inventory.filters.ArrayStackFilter;
|
||||
import forestry.core.inventory.filters.IStackFilter;
|
||||
import forestry.core.inventory.filters.StandardStackFilters;
|
||||
import forestry.core.inventory.iterators.IExtInvSlot;
|
||||
import forestry.core.inventory.iterators.InventoryIterator;
|
||||
import forestry.core.inventory.manipulators.InventoryManipulator;
|
||||
import forestry.core.inventory.wrappers.SidedInventoryMapper;
|
||||
import forestry.core.inventory.manipulators.ItemHandlerInventoryManipulator;
|
||||
import forestry.core.tiles.AdjacentTileCache;
|
||||
import forestry.plugins.ForestryPluginUids;
|
||||
|
||||
public abstract class InventoryUtil {
|
||||
|
||||
public static IInventory getInventory(IInventory inv, EnumFacing side) {
|
||||
if (inv == null) {
|
||||
return null;
|
||||
}
|
||||
if (inv instanceof ISidedInventory) {
|
||||
inv = new SidedInventoryMapper((ISidedInventory) inv, side);
|
||||
}
|
||||
return inv;
|
||||
}
|
||||
|
||||
public static ItemStack depleteItem(ItemStack stack) {
|
||||
if (stack.stackSize == 1) {
|
||||
return stack.getItem().getContainerItem(stack);
|
||||
@@ -61,54 +46,7 @@ public abstract class InventoryUtil {
|
||||
}
|
||||
|
||||
public static boolean isWildcard(ItemStack stack) {
|
||||
return isWildcard(stack.getItemDamage());
|
||||
}
|
||||
|
||||
public static boolean isWildcard(int damage) {
|
||||
return damage == -1 || damage == OreDictionary.WILDCARD_VALUE;
|
||||
}
|
||||
|
||||
public static ItemStack makeSafe(ItemStack stack) {
|
||||
if (stack.stackSize <= 0) {
|
||||
return null;
|
||||
}
|
||||
return stack;
|
||||
}
|
||||
|
||||
/**
|
||||
* A more robust item comparison function.
|
||||
*
|
||||
* Compares stackSize as well.
|
||||
*
|
||||
* Two null stacks will return true, unlike the other functions.
|
||||
*
|
||||
* This function is primarily intended to be used to track changes to an
|
||||
* ItemStack.
|
||||
*
|
||||
* @param a An ItemStack
|
||||
* @param b An ItemStack
|
||||
* @return True if equal
|
||||
*/
|
||||
public static boolean isItemEqualStrict(ItemStack a, ItemStack b) {
|
||||
if (a == null && b == null) {
|
||||
return true;
|
||||
}
|
||||
if (a == null || b == null) {
|
||||
return false;
|
||||
}
|
||||
if (a.getItem() != b.getItem()) {
|
||||
return false;
|
||||
}
|
||||
if (a.stackSize != b.stackSize) {
|
||||
return false;
|
||||
}
|
||||
if (a.getItemDamage() != b.getItemDamage()) {
|
||||
return false;
|
||||
}
|
||||
if (a.hasTagCompound() && !a.getTagCompound().equals(b.getTagCompound())) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
return stack.getItemDamage() == OreDictionary.WILDCARD_VALUE;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -171,20 +109,6 @@ public abstract class InventoryUtil {
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Places an ItemStack in a destination IInventory. Will attempt to move as
|
||||
* much of the stack as possible, returning any remainder.
|
||||
*
|
||||
* @param stack The ItemStack to put in the inventory.
|
||||
* @param dest The destination IInventory.
|
||||
* @return Null if itemStack was completely moved, a new itemStack with
|
||||
* remaining stackSize if part or none of the stack was moved.
|
||||
*/
|
||||
public static ItemStack moveItemStack(ItemStack stack, IInventory dest) {
|
||||
InventoryManipulator im = InventoryManipulator.get(dest);
|
||||
return im.addStack(stack);
|
||||
}
|
||||
|
||||
/**
|
||||
* Attempts to move an ItemStack from one inventory to another.
|
||||
@@ -193,17 +117,9 @@ public abstract class InventoryUtil {
|
||||
* @param dest The destination IInventory.
|
||||
* @return true if any items were moved
|
||||
*/
|
||||
public static boolean moveItemStack(IInventory source, IInventory dest) {
|
||||
InventoryManipulator im = InventoryManipulator.get(dest);
|
||||
for (IExtInvSlot slot : InventoryIterator.getIterable(source)) {
|
||||
ItemStack stack = slot.getStackInSlot();
|
||||
if (stack != null) {
|
||||
ItemStack remainder = im.addStack(stack);
|
||||
slot.setStackInSlot(remainder);
|
||||
return !isItemEqualStrict(stack, remainder);
|
||||
}
|
||||
}
|
||||
return false;
|
||||
public static boolean moveItemStack(IItemHandler source, IItemHandler dest) {
|
||||
ItemHandlerInventoryManipulator manipulator = new ItemHandlerInventoryManipulator(source);
|
||||
return manipulator.transferOneStack(dest, StandardStackFilters.ALL);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -213,8 +129,8 @@ public abstract class InventoryUtil {
|
||||
* @param destinations The destination IInventory.
|
||||
* @return true if any items were moved
|
||||
*/
|
||||
public static boolean moveItemStack(IInventory source, Iterable<IInventory> destinations) {
|
||||
for (IInventory dest : destinations) {
|
||||
public static boolean moveItemStack(IItemHandler source, Iterable<IItemHandler> destinations) {
|
||||
for (IItemHandler dest : destinations) {
|
||||
if (moveItemStack(source, dest)) {
|
||||
return true;
|
||||
}
|
||||
@@ -222,41 +138,6 @@ public abstract class InventoryUtil {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes and returns a single item from the inventory.
|
||||
*
|
||||
* @param inv The inventory
|
||||
* @return An ItemStack
|
||||
*/
|
||||
public static ItemStack removeOneItem(IInventory inv) {
|
||||
return removeOneItem(inv, StandardStackFilters.ALL);
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes and returns a single item from the inventory that matches the
|
||||
* filter.
|
||||
*
|
||||
* @param inv The inventory
|
||||
* @param filter ItemStack to match against
|
||||
* @return An ItemStack
|
||||
*/
|
||||
public static ItemStack removeOneItem(IInventory inv, ItemStack... filter) {
|
||||
return removeOneItem(inv, new ArrayStackFilter(filter));
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes and returns a single item from the inventory that matches the
|
||||
* filter.
|
||||
*
|
||||
* @param inv The inventory
|
||||
* @param filter EnumItemType to match against
|
||||
* @return An ItemStack
|
||||
*/
|
||||
public static ItemStack removeOneItem(IInventory inv, IStackFilter filter) {
|
||||
InventoryManipulator im = InventoryManipulator.get(inv);
|
||||
return im.removeItem(filter);
|
||||
}
|
||||
|
||||
/**
|
||||
* Attempts to move a single item from the source inventory into a adjacent Buildcraft pipe.
|
||||
* If the attempt fails, the source Inventory will not be modified.
|
||||
@@ -265,11 +146,11 @@ public abstract class InventoryUtil {
|
||||
* @param tileCache The tile cache of the source block.
|
||||
* @return true if an item was inserted, otherwise false.
|
||||
*/
|
||||
public static boolean moveOneItemToPipe(IInventory source, AdjacentTileCache tileCache) {
|
||||
public static boolean moveOneItemToPipe(IItemHandler source, AdjacentTileCache tileCache) {
|
||||
return moveOneItemToPipe(source, tileCache, EnumFacing.values());
|
||||
}
|
||||
|
||||
public static boolean moveOneItemToPipe(IInventory source, AdjacentTileCache tileCache, EnumFacing[] directions) {
|
||||
public static boolean moveOneItemToPipe(IItemHandler source, AdjacentTileCache tileCache, EnumFacing[] directions) {
|
||||
if (ForestryAPI.enabledPlugins.contains(ForestryPluginUids.BUILDCRAFT_TRANSPORT)) {
|
||||
return internal_moveOneItemToPipe(source, tileCache, directions);
|
||||
}
|
||||
@@ -279,7 +160,7 @@ public abstract class InventoryUtil {
|
||||
|
||||
//TODO Buildcraft for 1.9
|
||||
@Optional.Method(modid = "BuildCraftAPI|transport")
|
||||
private static boolean internal_moveOneItemToPipe(IInventory source, AdjacentTileCache tileCache, EnumFacing[] directions) {
|
||||
private static boolean internal_moveOneItemToPipe(IItemHandler source, AdjacentTileCache tileCache, EnumFacing[] directions) {
|
||||
// IInventory invClone = new InventoryCopy(source);
|
||||
// ItemStack stackToMove = removeOneItem(invClone);
|
||||
// if (stackToMove == null) {
|
||||
|
||||
@@ -18,6 +18,10 @@ import net.minecraft.inventory.ISidedInventory;
|
||||
import net.minecraft.item.Item;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.nbt.NBTTagCompound;
|
||||
|
||||
import net.minecraftforge.items.IItemHandler;
|
||||
import net.minecraftforge.items.wrapper.InvWrapper;
|
||||
|
||||
import forestry.api.fuels.FuelManager;
|
||||
import forestry.core.PluginCore;
|
||||
import forestry.core.config.Constants;
|
||||
@@ -223,8 +227,10 @@ public class TileEnginePeat extends TileEngine implements ISidedInventory {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!InventoryUtil.moveOneItemToPipe(wasteInventory, getTileCache())) {
|
||||
InventoryUtil.moveItemStack(wasteInventory, inventoryCache.getAdjacentInventories());
|
||||
IItemHandler wasteItemHandler = new InvWrapper(wasteInventory);
|
||||
|
||||
if (!InventoryUtil.moveOneItemToPipe(wasteItemHandler, getTileCache())) {
|
||||
InventoryUtil.moveItemStack(wasteItemHandler, inventoryCache.getAdjacentInventories());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -15,6 +15,9 @@ import net.minecraft.inventory.ISidedInventory;
|
||||
import net.minecraft.tileentity.TileEntity;
|
||||
import net.minecraft.util.EnumFacing;
|
||||
|
||||
import net.minecraftforge.items.IItemHandler;
|
||||
import net.minecraftforge.items.wrapper.InvWrapper;
|
||||
|
||||
import forestry.api.multiblock.IFarmComponent;
|
||||
import forestry.core.inventory.AdjacentInventoryCache;
|
||||
import forestry.core.tiles.AdjacentTileCache;
|
||||
@@ -48,9 +51,10 @@ public class TileFarmHatch extends TileFarm implements ISidedInventory, IFarmCom
|
||||
public void updateServer(int tickCount) {
|
||||
if (tickCount % 40 == 0) {
|
||||
IInventory productInventory = getMultiblockLogic().getController().getFarmInventory().getProductInventory();
|
||||
IItemHandler productItemHandler = new InvWrapper(productInventory);
|
||||
|
||||
if (!InventoryUtil.moveOneItemToPipe(productInventory, tileCache, dumpDirections)) {
|
||||
InventoryUtil.moveItemStack(productInventory, inventoryCache.getAdjacentInventories());
|
||||
if (!InventoryUtil.moveOneItemToPipe(productItemHandler, tileCache, dumpDirections)) {
|
||||
InventoryUtil.moveItemStack(productItemHandler, inventoryCache.getAdjacentInventories());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,12 +14,11 @@ import javax.annotation.Nonnull;
|
||||
import javax.annotation.Nullable;
|
||||
import java.util.List;
|
||||
|
||||
import net.minecraft.inventory.IInventory;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.nbt.NBTTagCompound;
|
||||
import net.minecraft.tileentity.TileEntity;
|
||||
import net.minecraft.util.math.BlockPos;
|
||||
import net.minecraft.util.EnumFacing;
|
||||
import net.minecraft.util.math.BlockPos;
|
||||
|
||||
import com.mojang.authlib.GameProfile;
|
||||
|
||||
@@ -30,6 +29,7 @@ import net.minecraftforge.fluids.FluidStack;
|
||||
import net.minecraftforge.fluids.FluidTankInfo;
|
||||
import net.minecraftforge.fluids.IFluidHandler;
|
||||
import net.minecraftforge.items.CapabilityItemHandler;
|
||||
import net.minecraftforge.items.IItemHandler;
|
||||
|
||||
import forestry.api.core.EnumCamouflageType;
|
||||
import forestry.api.core.ICamouflageHandler;
|
||||
@@ -208,7 +208,7 @@ public class TileGreenhouseHatch extends MultiblockTileEntityBase<MultiblockLogi
|
||||
return EnumCamouflageType.DEFAULT;
|
||||
}
|
||||
|
||||
private IInventory getOutwardsInventory() {
|
||||
private IItemHandler getOutwardsInventory() {
|
||||
if (getOutwardsTile() == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -105,16 +105,8 @@ public class BackpackDefinition implements IBackpackDefinition {
|
||||
}
|
||||
}
|
||||
|
||||
public Set<String> getValidItemStacks() {
|
||||
return validItemStacks;
|
||||
}
|
||||
|
||||
public Set<Integer> getValidOreIds() {
|
||||
return validOreIds;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isValidItem(ItemStack itemStack) {
|
||||
public boolean test(ItemStack itemStack) {
|
||||
if (itemStack == null) {
|
||||
return false;
|
||||
}
|
||||
@@ -158,7 +150,7 @@ public class BackpackDefinition implements IBackpackDefinition {
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isValidItem(ItemStack itemStack) {
|
||||
public boolean test(ItemStack itemStack) {
|
||||
ISpeciesRoot speciesRoot = AlleleManager.alleleRegistry.getSpeciesRoot(speciesRootUid);
|
||||
return speciesRoot.isMember(itemStack);
|
||||
}
|
||||
|
||||
@@ -10,6 +10,23 @@
|
||||
******************************************************************************/
|
||||
package forestry.storage;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
public enum BackpackMode {
|
||||
NORMAL, LOCKED, RECEIVE, RESUPPLY
|
||||
NORMAL(null),
|
||||
LOCKED("for.storage.backpack.mode.locked"),
|
||||
RECEIVE("for.storage.backpack.mode.locked"),
|
||||
RESUPPLY("for.storage.backpack.mode.locked");
|
||||
|
||||
@Nullable
|
||||
private final String unlocalizedInfo;
|
||||
|
||||
BackpackMode(@Nullable String unlocalizedInfo) {
|
||||
this.unlocalizedInfo = unlocalizedInfo;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public String getUnlocalizedInfo() {
|
||||
return unlocalizedInfo;
|
||||
}
|
||||
}
|
||||
@@ -54,7 +54,7 @@ public class PickupHandlerStorage implements IPickupHandler {
|
||||
|
||||
ItemBackpack backpack = (ItemBackpack) pack.getItem();
|
||||
IBackpackDefinition backpackDefinition = backpack.getDefinition();
|
||||
if (backpackDefinition.isValidItem(itemstack)) {
|
||||
if (backpackDefinition.test(itemstack)) {
|
||||
ItemBackpack.tryStowing(player, pack, itemstack);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -33,6 +33,6 @@ public class ItemInventoryBackpack extends ItemInventory {
|
||||
|
||||
@Override
|
||||
public boolean canSlotAccept(int slotIndex, ItemStack itemStack) {
|
||||
return backpackDefinition.isValidItem(itemStack);
|
||||
return backpackDefinition.test(itemStack);
|
||||
}
|
||||
}
|
||||
@@ -17,22 +17,21 @@ import net.minecraft.client.renderer.ItemMeshDefinition;
|
||||
import net.minecraft.client.renderer.block.model.ModelResourceLocation;
|
||||
import net.minecraft.client.renderer.color.IItemColor;
|
||||
import net.minecraft.entity.player.EntityPlayer;
|
||||
import net.minecraft.inventory.IInventory;
|
||||
import net.minecraft.item.Item;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.tileentity.TileEntity;
|
||||
import net.minecraft.util.ActionResult;
|
||||
import net.minecraft.util.EnumActionResult;
|
||||
import net.minecraft.util.EnumHand;
|
||||
import net.minecraft.util.math.BlockPos;
|
||||
import net.minecraft.util.EnumFacing;
|
||||
import net.minecraft.util.EnumHand;
|
||||
import net.minecraft.util.ResourceLocation;
|
||||
import net.minecraft.util.math.BlockPos;
|
||||
import net.minecraft.world.World;
|
||||
|
||||
import net.minecraftforge.common.MinecraftForge;
|
||||
import net.minecraftforge.fml.common.eventhandler.Event;
|
||||
import net.minecraftforge.fml.relauncher.Side;
|
||||
import net.minecraftforge.fml.relauncher.SideOnly;
|
||||
import net.minecraftforge.items.IItemHandler;
|
||||
|
||||
import forestry.api.core.IModelManager;
|
||||
import forestry.api.storage.BackpackStowEvent;
|
||||
@@ -42,11 +41,10 @@ import forestry.core.config.Config;
|
||||
import forestry.core.config.Constants;
|
||||
import forestry.core.gui.GuiHandler;
|
||||
import forestry.core.inventory.ItemInventory;
|
||||
import forestry.core.inventory.iterators.IExtInvSlot;
|
||||
import forestry.core.inventory.iterators.InventoryIterator;
|
||||
import forestry.core.inventory.filters.StandardStackFilters;
|
||||
import forestry.core.inventory.manipulators.ItemHandlerInventoryManipulator;
|
||||
import forestry.core.items.ItemWithGui;
|
||||
import forestry.core.tiles.TileUtil;
|
||||
import forestry.core.utils.InventoryUtil;
|
||||
import forestry.core.utils.Translator;
|
||||
import forestry.storage.BackpackMode;
|
||||
import forestry.storage.gui.ContainerBackpack;
|
||||
@@ -102,30 +100,29 @@ public class ItemBackpack extends ItemWithGui implements IItemColor {
|
||||
return EnumActionResult.FAIL;
|
||||
}
|
||||
|
||||
return evaluateTileHit(stack, player, world, pos, side) ? EnumActionResult.SUCCESS : EnumActionResult.FAIL;
|
||||
return evaluateTileHit(stack, player, world, pos, side) ? EnumActionResult.PASS : EnumActionResult.FAIL;
|
||||
}
|
||||
|
||||
public static ItemStack tryStowing(EntityPlayer player, ItemStack backpackStack, ItemStack stack) {
|
||||
public static void tryStowing(EntityPlayer player, ItemStack backpackStack, ItemStack stack) {
|
||||
if (getMode(backpackStack) == BackpackMode.LOCKED) {
|
||||
return;
|
||||
}
|
||||
|
||||
ItemBackpack backpack = (ItemBackpack) backpackStack.getItem();
|
||||
ItemInventory inventory = new ItemInventoryBackpack(player, backpack.getBackpackSize(), backpackStack);
|
||||
if (backpackStack.getItemDamage() == 1) {
|
||||
return stack;
|
||||
}
|
||||
|
||||
Event event = new BackpackStowEvent(player, backpack.getDefinition(), inventory, stack);
|
||||
MinecraftForge.EVENT_BUS.post(event);
|
||||
if (MinecraftForge.EVENT_BUS.post(new BackpackStowEvent(player, backpack.getDefinition(), inventory, stack))) {
|
||||
return;
|
||||
}
|
||||
if (stack.stackSize <= 0) {
|
||||
return null;
|
||||
}
|
||||
if (event.isCanceled()) {
|
||||
return stack;
|
||||
return;
|
||||
}
|
||||
|
||||
ItemStack remainder = InventoryUtil.moveItemStack(stack, inventory);
|
||||
IItemHandler itemHandler = inventory.getItemHandler();
|
||||
ItemHandlerInventoryManipulator manipulator = new ItemHandlerInventoryManipulator(itemHandler);
|
||||
ItemStack remainder = manipulator.addStack(stack);
|
||||
|
||||
stack.stackSize = remainder == null ? 0 : remainder.stackSize;
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private static void switchMode(ItemStack itemstack) {
|
||||
@@ -138,7 +135,7 @@ public class ItemBackpack extends ItemWithGui implements IItemColor {
|
||||
itemstack.setItemDamage(nextMode);
|
||||
}
|
||||
|
||||
private static IInventory getInventoryHit(World world, BlockPos pos, EnumFacing side) {
|
||||
private static IItemHandler getInventoryHit(World world, BlockPos pos, EnumFacing side) {
|
||||
TileEntity targeted = world.getTileEntity(pos);
|
||||
return TileUtil.getInventoryFromTile(targeted, side);
|
||||
}
|
||||
@@ -147,23 +144,25 @@ public class ItemBackpack extends ItemWithGui implements IItemColor {
|
||||
|
||||
// Shift right-clicking on an inventory tile will attempt to transfer
|
||||
// items contained in the backpack
|
||||
IInventory inventory = getInventoryHit(world, pos, side);
|
||||
IItemHandler inventory = getInventoryHit(world, pos, side);
|
||||
// Process only inventories
|
||||
if (inventory != null) {
|
||||
|
||||
// Must have inventory slots
|
||||
if (inventory.getSizeInventory() <= 0) {
|
||||
if (inventory.getSlots() <= 0) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// Create our own backpack inventory
|
||||
ItemInventoryBackpack backpackInventory = new ItemInventoryBackpack(player, getBackpackSize(), stack);
|
||||
if (!world.isRemote) {
|
||||
// Create our own backpack inventory
|
||||
ItemInventoryBackpack backpackInventory = new ItemInventoryBackpack(player, getBackpackSize(), stack);
|
||||
|
||||
BackpackMode mode = getMode(stack);
|
||||
if (mode == BackpackMode.RECEIVE) {
|
||||
tryChestReceive(backpackInventory, inventory);
|
||||
} else {
|
||||
tryChestTransfer(backpackInventory, inventory);
|
||||
BackpackMode mode = getMode(stack);
|
||||
if (mode == BackpackMode.RECEIVE) {
|
||||
receiveFromChest(backpackInventory, inventory);
|
||||
} else {
|
||||
transferToChest(backpackInventory, inventory);
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
@@ -172,34 +171,14 @@ public class ItemBackpack extends ItemWithGui implements IItemColor {
|
||||
return false;
|
||||
}
|
||||
|
||||
private static void tryChestTransfer(ItemInventoryBackpack backpackInventory, IInventory target) {
|
||||
|
||||
for (IExtInvSlot slot : InventoryIterator.getIterable(backpackInventory)) {
|
||||
ItemStack packStack = slot.getStackInSlot();
|
||||
if (packStack == null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
ItemStack remaining = InventoryUtil.moveItemStack(packStack, target);
|
||||
slot.setStackInSlot(remaining);
|
||||
}
|
||||
private static void transferToChest(ItemInventoryBackpack backpackInventory, IItemHandler target) {
|
||||
ItemHandlerInventoryManipulator manipulator = new ItemHandlerInventoryManipulator(backpackInventory.getItemHandler());
|
||||
manipulator.transferStacks(target, StandardStackFilters.ALL);
|
||||
}
|
||||
|
||||
private void tryChestReceive(ItemInventoryBackpack backpackInventory, IInventory target) {
|
||||
|
||||
for (IExtInvSlot slot : InventoryIterator.getIterable(target)) {
|
||||
ItemStack targetStack = slot.getStackInSlot();
|
||||
if (targetStack == null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!definition.isValidItem(targetStack)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
ItemStack remaining = InventoryUtil.moveItemStack(targetStack, backpackInventory);
|
||||
slot.setStackInSlot(remaining);
|
||||
}
|
||||
private void receiveFromChest(ItemInventoryBackpack backpackInventory, IItemHandler target) {
|
||||
ItemHandlerInventoryManipulator manipulator = new ItemHandlerInventoryManipulator(target);
|
||||
manipulator.transferStacks(backpackInventory.getItemHandler(), definition);
|
||||
}
|
||||
|
||||
public int getBackpackSize() {
|
||||
@@ -211,15 +190,11 @@ public class ItemBackpack extends ItemWithGui implements IItemColor {
|
||||
int occupied = ItemInventory.getOccupiedSlotCount(itemstack);
|
||||
|
||||
BackpackMode mode = getMode(itemstack);
|
||||
if (mode == BackpackMode.LOCKED) {
|
||||
list.add(Translator.translateToLocal("for.storage.backpack.mode.locked"));
|
||||
} else if (mode == BackpackMode.RECEIVE) {
|
||||
list.add(Translator.translateToLocal("for.storage.backpack.mode.receiving"));
|
||||
} else if (mode == BackpackMode.RESUPPLY) {
|
||||
list.add(Translator.translateToLocal("for.storage.backpack.mode.resupply"));
|
||||
String infoKey = mode.getUnlocalizedInfo();
|
||||
if (infoKey != null) {
|
||||
list.add(Translator.translateToLocal(infoKey));
|
||||
}
|
||||
list.add(Translator.translateToLocal("for.gui.slots").replaceAll("%USED", String.valueOf(occupied)).replaceAll("%SIZE", String.valueOf(getBackpackSize())));
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -253,7 +228,8 @@ public class ItemBackpack extends ItemWithGui implements IItemColor {
|
||||
|
||||
@Override
|
||||
public ModelResourceLocation getModelLocation(ItemStack stack) {
|
||||
return models[stack.getItemDamage()];
|
||||
BackpackMode mode = getMode(stack);
|
||||
return models[mode.ordinal()];
|
||||
}
|
||||
|
||||
}
|
||||
@@ -296,6 +272,11 @@ public class ItemBackpack extends ItemWithGui implements IItemColor {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean shouldCauseReequipAnimation(ItemStack oldStack, ItemStack newStack, boolean slotChanged) {
|
||||
return getMode(oldStack) != getMode(newStack);
|
||||
}
|
||||
|
||||
@SuppressWarnings("incomplete-switch")
|
||||
@Override
|
||||
public Object getGui(EntityPlayer player, ItemStack heldItem, int data) {
|
||||
|
||||
Reference in new issue
Block a user