mirror of
https://github.com/Thorfusion/Mekanism-Community-Edition.git
synced 2026-11-26 23:03:01 +00:00
Prioritize IItemHandler over IInventory as it is the newer standard
This commit is contained in:
6 files changed
+180
-181
No files matched your search
@@ -54,7 +54,20 @@ public final class InvStack {
|
||||
}
|
||||
|
||||
public void use(int amount) {
|
||||
if (tileEntity instanceof IInventory) {
|
||||
if (InventoryUtils.isItemHandler(tileEntity, side)) {
|
||||
IItemHandler handler = InventoryUtils.getItemHandler(tileEntity, side);
|
||||
|
||||
for (int i = 0; i < slotIDs.size(); i++) {
|
||||
ItemStack stack = itemStacks.get(i);
|
||||
int toUse = Math.min(amount, stack.getCount());
|
||||
handler.extractItem(slotIDs.get(i), toUse, false);
|
||||
amount -= toUse;
|
||||
|
||||
if (amount == 0) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
} else if (tileEntity instanceof IInventory) {
|
||||
IInventory inventory = InventoryUtils.checkChestInv((IInventory) tileEntity);
|
||||
|
||||
for (int i = 0; i < slotIDs.size(); i++) {
|
||||
@@ -71,19 +84,6 @@ public final class InvStack {
|
||||
amount -= stack.getCount();
|
||||
}
|
||||
|
||||
if (amount == 0) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
} else if (InventoryUtils.isItemHandler(tileEntity, side)) {
|
||||
IItemHandler handler = InventoryUtils.getItemHandler(tileEntity, side);
|
||||
|
||||
for (int i = 0; i < slotIDs.size(); i++) {
|
||||
ItemStack stack = itemStacks.get(i);
|
||||
int toUse = Math.min(amount, stack.getCount());
|
||||
handler.extractItem(slotIDs.get(i), toUse, false);
|
||||
amount -= toUse;
|
||||
|
||||
if (amount == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -20,19 +20,28 @@ public class StackSearcher {
|
||||
tileEntity = tile;
|
||||
side = direction;
|
||||
|
||||
if (tile instanceof ISidedInventory) {
|
||||
if (InventoryUtils.isItemHandler(tile, direction.getOpposite())) {
|
||||
i = InventoryUtils.getItemHandler(tile, direction.getOpposite()).getSlots();
|
||||
} else if (tile instanceof ISidedInventory) {
|
||||
slots = ((ISidedInventory) tile).getSlotsForFace(side.getOpposite());
|
||||
|
||||
i = slots.length;
|
||||
} else if (tile instanceof IInventory) {
|
||||
i = ((IInventory) tile).getSizeInventory();
|
||||
} else if (InventoryUtils.isItemHandler(tile, direction.getOpposite())) {
|
||||
i = InventoryUtils.getItemHandler(tile, direction.getOpposite()).getSlots();
|
||||
}
|
||||
}
|
||||
|
||||
public InvStack takeTopStack(Finder id) {
|
||||
if (tileEntity instanceof ISidedInventory) {
|
||||
if (InventoryUtils.isItemHandler(tileEntity, side.getOpposite())) {
|
||||
IItemHandler inventory = InventoryUtils.getItemHandler(tileEntity, side.getOpposite());
|
||||
|
||||
for (i = i - 1; i >= 0; i--) {
|
||||
ItemStack stack = inventory.extractItem(i, 64, true);
|
||||
|
||||
if (!stack.isEmpty() && id.modifies(stack)) {
|
||||
return new InvStack(tileEntity, i, stack, side.getOpposite());
|
||||
}
|
||||
}
|
||||
} else if (tileEntity instanceof ISidedInventory) {
|
||||
ISidedInventory inventory = (ISidedInventory) tileEntity;
|
||||
|
||||
if (slots != null && slots.length != 0) {
|
||||
@@ -57,16 +66,6 @@ public class StackSearcher {
|
||||
return new InvStack(tileEntity, i, toSend, side.getOpposite());
|
||||
}
|
||||
}
|
||||
} else if (InventoryUtils.isItemHandler(tileEntity, side.getOpposite())) {
|
||||
IItemHandler inventory = InventoryUtils.getItemHandler(tileEntity, side.getOpposite());
|
||||
|
||||
for (i = i - 1; i >= 0; i--) {
|
||||
ItemStack stack = inventory.extractItem(i, 64, true);
|
||||
|
||||
if (!stack.isEmpty() && id.modifies(stack)) {
|
||||
return new InvStack(tileEntity, i, stack, side.getOpposite());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
@@ -75,7 +74,29 @@ public class StackSearcher {
|
||||
public InvStack takeDefinedItem(ItemStack type, int min, int max) {
|
||||
InvStack ret = new InvStack(tileEntity, side.getOpposite());
|
||||
|
||||
if (tileEntity instanceof ISidedInventory) {
|
||||
if (InventoryUtils.isItemHandler(tileEntity, side.getOpposite())) {
|
||||
IItemHandler inventory = InventoryUtils.getItemHandler(tileEntity, side.getOpposite());
|
||||
|
||||
for (i = i - 1; i >= 0; i--) {
|
||||
ItemStack stack = inventory.extractItem(i, max, true);
|
||||
|
||||
if (!stack.isEmpty() && StackUtils.equalsWildcard(stack, type)) {
|
||||
int current = !ret.getStack().isEmpty() ? ret.getStack().getCount() : 0;
|
||||
|
||||
if (current + stack.getCount() <= max) {
|
||||
ret.appendStack(i, stack.copy());
|
||||
} else {
|
||||
ItemStack copy = stack.copy();
|
||||
copy.setCount(max - current);
|
||||
ret.appendStack(i, copy);
|
||||
}
|
||||
|
||||
if (!ret.getStack().isEmpty() && ret.getStack().getCount() == max) {
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
}
|
||||
} else if (tileEntity instanceof ISidedInventory) {
|
||||
ISidedInventory sidedInventory = (ISidedInventory) tileEntity;
|
||||
int[] slots = sidedInventory.getSlotsForFace(side.getOpposite());
|
||||
|
||||
@@ -126,28 +147,6 @@ public class StackSearcher {
|
||||
ret.appendStack(i, copy);
|
||||
}
|
||||
|
||||
if (!ret.getStack().isEmpty() && ret.getStack().getCount() == max) {
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
}
|
||||
} else if (InventoryUtils.isItemHandler(tileEntity, side.getOpposite())) {
|
||||
IItemHandler inventory = InventoryUtils.getItemHandler(tileEntity, side.getOpposite());
|
||||
|
||||
for (i = i - 1; i >= 0; i--) {
|
||||
ItemStack stack = inventory.extractItem(i, max, true);
|
||||
|
||||
if (!stack.isEmpty() && StackUtils.equalsWildcard(stack, type)) {
|
||||
int current = !ret.getStack().isEmpty() ? ret.getStack().getCount() : 0;
|
||||
|
||||
if (current + stack.getCount() <= max) {
|
||||
ret.appendStack(i, stack.copy());
|
||||
} else {
|
||||
ItemStack copy = stack.copy();
|
||||
copy.setCount(max - current);
|
||||
ret.appendStack(i, copy);
|
||||
}
|
||||
|
||||
if (!ret.getStack().isEmpty() && ret.getStack().getCount() == max) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
@@ -33,7 +33,17 @@ public class TransitRequest {
|
||||
public static TransitRequest getTopStacks(TileEntity tile, EnumFacing side, int amount, Finder finder) {
|
||||
TransitRequest ret = new TransitRequest();
|
||||
|
||||
if (tile instanceof ISidedInventory) {
|
||||
if (InventoryUtils.isItemHandler(tile, side.getOpposite())) {
|
||||
IItemHandler inventory = InventoryUtils.getItemHandler(tile, side.getOpposite());
|
||||
|
||||
for (int i = inventory.getSlots() - 1; i >= 0; i--) {
|
||||
ItemStack stack = inventory.extractItem(i, amount, true);
|
||||
|
||||
if (!stack.isEmpty() && !ret.hasType(stack) && finder.modifies(stack)) {
|
||||
ret.setItem(stack, i);
|
||||
}
|
||||
}
|
||||
} else if (tile instanceof ISidedInventory) {
|
||||
ISidedInventory sidedInventory = (ISidedInventory) tile;
|
||||
int[] slots = sidedInventory.getSlotsForFace(side.getOpposite());
|
||||
|
||||
@@ -64,16 +74,6 @@ public class TransitRequest {
|
||||
}
|
||||
}
|
||||
}
|
||||
} else if (InventoryUtils.isItemHandler(tile, side.getOpposite())) {
|
||||
IItemHandler inventory = InventoryUtils.getItemHandler(tile, side.getOpposite());
|
||||
|
||||
for (int i = inventory.getSlots() - 1; i >= 0; i--) {
|
||||
ItemStack stack = inventory.extractItem(i, amount, true);
|
||||
|
||||
if (!stack.isEmpty() && !ret.hasType(stack) && finder.modifies(stack)) {
|
||||
ret.setItem(stack, i);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return ret;
|
||||
|
||||
@@ -133,7 +133,63 @@ public class TransporterManager {
|
||||
// return;
|
||||
// }
|
||||
|
||||
if (tile instanceof ISidedInventory) {
|
||||
if (InventoryUtils.isItemHandler(tile, side.getOpposite())) {
|
||||
IItemHandler inv = InventoryUtils.getItemHandler(tile, side.getOpposite());
|
||||
|
||||
for (int i = 0; i <= inv.getSlots() - 1; i++) {
|
||||
if (stack.pathType != Path.HOME) {
|
||||
ItemStack rejectStack = inv.insertItem(i, toInsert, true);
|
||||
|
||||
if (!TransporterManager.didEmit(toInsert, rejectStack)) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
ItemStack inSlot = copy.inventory.get(i);
|
||||
|
||||
if (inSlot.isEmpty()) {
|
||||
if (toInsert.getCount() <= inv.getSlotLimit(i)) {
|
||||
copy.inventory.set(i, toInsert);
|
||||
return;
|
||||
} else {
|
||||
int rejects = toInsert.getCount() - inv.getSlotLimit(i);
|
||||
|
||||
ItemStack toSet = toInsert.copy();
|
||||
toSet.setCount(inv.getSlotLimit(i));
|
||||
|
||||
ItemStack remains = toInsert.copy();
|
||||
remains.setCount(rejects);
|
||||
|
||||
copy.inventory.set(i, toSet);
|
||||
|
||||
toInsert = remains;
|
||||
}
|
||||
} else if (InventoryUtils.areItemsStackable(toInsert, inSlot) && inSlot.getCount() < Math
|
||||
.min(inSlot.getMaxStackSize(), inv.getSlotLimit(i))) {
|
||||
int max = Math.min(inSlot.getMaxStackSize(), inv.getSlotLimit(i));
|
||||
|
||||
if (inSlot.getCount() + toInsert.getCount() <= max) {
|
||||
ItemStack toSet = toInsert.copy();
|
||||
toSet.grow(inSlot.getCount());
|
||||
|
||||
copy.inventory.set(i, toSet);
|
||||
return;
|
||||
} else {
|
||||
int rejects = (inSlot.getCount() + toInsert.getCount()) - max;
|
||||
|
||||
ItemStack toSet = toInsert.copy();
|
||||
toSet.setCount(max);
|
||||
|
||||
ItemStack remains = toInsert.copy();
|
||||
remains.setCount(rejects);
|
||||
|
||||
copy.inventory.set(i, toSet);
|
||||
|
||||
toInsert = remains;
|
||||
}
|
||||
}
|
||||
}
|
||||
} else if (tile instanceof ISidedInventory) {
|
||||
ISidedInventory sidedInventory = (ISidedInventory) tile;
|
||||
int[] slots = sidedInventory.getSlotsForFace(side.getOpposite());
|
||||
|
||||
@@ -245,62 +301,6 @@ public class TransporterManager {
|
||||
.min(inSlot.getMaxStackSize(), inv.getInventoryStackLimit())) {
|
||||
int max = Math.min(inSlot.getMaxStackSize(), inv.getInventoryStackLimit());
|
||||
|
||||
if (inSlot.getCount() + toInsert.getCount() <= max) {
|
||||
ItemStack toSet = toInsert.copy();
|
||||
toSet.grow(inSlot.getCount());
|
||||
|
||||
copy.inventory.set(i, toSet);
|
||||
return;
|
||||
} else {
|
||||
int rejects = (inSlot.getCount() + toInsert.getCount()) - max;
|
||||
|
||||
ItemStack toSet = toInsert.copy();
|
||||
toSet.setCount(max);
|
||||
|
||||
ItemStack remains = toInsert.copy();
|
||||
remains.setCount(rejects);
|
||||
|
||||
copy.inventory.set(i, toSet);
|
||||
|
||||
toInsert = remains;
|
||||
}
|
||||
}
|
||||
}
|
||||
} else if (InventoryUtils.isItemHandler(tile, side.getOpposite())) {
|
||||
IItemHandler inv = InventoryUtils.getItemHandler(tile, side.getOpposite());
|
||||
|
||||
for (int i = 0; i <= inv.getSlots() - 1; i++) {
|
||||
if (stack.pathType != Path.HOME) {
|
||||
ItemStack rejectStack = inv.insertItem(i, toInsert, true);
|
||||
|
||||
if (!TransporterManager.didEmit(toInsert, rejectStack)) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
ItemStack inSlot = copy.inventory.get(i);
|
||||
|
||||
if (inSlot.isEmpty()) {
|
||||
if (toInsert.getCount() <= inv.getSlotLimit(i)) {
|
||||
copy.inventory.set(i, toInsert);
|
||||
return;
|
||||
} else {
|
||||
int rejects = toInsert.getCount() - inv.getSlotLimit(i);
|
||||
|
||||
ItemStack toSet = toInsert.copy();
|
||||
toSet.setCount(inv.getSlotLimit(i));
|
||||
|
||||
ItemStack remains = toInsert.copy();
|
||||
remains.setCount(rejects);
|
||||
|
||||
copy.inventory.set(i, toSet);
|
||||
|
||||
toInsert = remains;
|
||||
}
|
||||
} else if (InventoryUtils.areItemsStackable(toInsert, inSlot) && inSlot.getCount() < Math
|
||||
.min(inSlot.getMaxStackSize(), inv.getSlotLimit(i))) {
|
||||
int max = Math.min(inSlot.getMaxStackSize(), inv.getSlotLimit(i));
|
||||
|
||||
if (inSlot.getCount() + toInsert.getCount() <= max) {
|
||||
ItemStack toSet = toInsert.copy();
|
||||
toSet.grow(inSlot.getCount());
|
||||
@@ -359,10 +359,10 @@ public class TransporterManager {
|
||||
|
||||
InventoryCopy copy = null;
|
||||
|
||||
if (tileEntity instanceof IInventory) {
|
||||
copy = copyInvFromSide(InventoryUtils.checkChestInv((IInventory) tileEntity), side);
|
||||
} else if (InventoryUtils.isItemHandler(tileEntity, side.getOpposite())) {
|
||||
if (InventoryUtils.isItemHandler(tileEntity, side.getOpposite())) {
|
||||
copy = copyInv(InventoryUtils.getItemHandler(tileEntity, side.getOpposite()));
|
||||
} else if (tileEntity instanceof IInventory) {
|
||||
copy = copyInvFromSide(InventoryUtils.checkChestInv((IInventory) tileEntity), side);
|
||||
}
|
||||
|
||||
if (copy == null) {
|
||||
@@ -378,7 +378,50 @@ public class TransporterManager {
|
||||
for (Map.Entry<ItemStack, Integer> requestEntry : request.itemMap.entrySet()) {
|
||||
ItemStack toInsert = requestEntry.getKey().copy();
|
||||
|
||||
if (tileEntity instanceof ISidedInventory) {
|
||||
if (InventoryUtils.isItemHandler(tileEntity, side.getOpposite())) {
|
||||
IItemHandler inventory = InventoryUtils.getItemHandler(tileEntity, side.getOpposite());
|
||||
|
||||
for (int i = 0; i <= inventory.getSlots() - 1; i++) {
|
||||
ItemStack rejectStack = inventory.insertItem(i, toInsert, true);
|
||||
|
||||
if (!TransporterManager.didEmit(toInsert, rejectStack)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
ItemStack inSlot = copy.inventory.get(i);
|
||||
|
||||
if (rejectStack.isEmpty()) {
|
||||
return new TransitResponse(requestEntry.getValue(), requestEntry.getKey());
|
||||
} else if (inSlot.isEmpty()) {
|
||||
if (toInsert.getCount() <= inventory.getSlotLimit(i)) {
|
||||
return new TransitResponse(requestEntry.getValue(), requestEntry.getKey());
|
||||
} else {
|
||||
int rejects = toInsert.getCount() - inventory.getSlotLimit(i);
|
||||
|
||||
if (rejects < toInsert.getCount()) {
|
||||
toInsert = StackUtils.size(toInsert, rejects);
|
||||
}
|
||||
}
|
||||
} else if (InventoryUtils.areItemsStackable(toInsert, inSlot) && inSlot.getCount() < Math
|
||||
.min(inSlot.getMaxStackSize(), inventory.getSlotLimit(i))) {
|
||||
int max = Math.min(inSlot.getMaxStackSize(), inventory.getSlotLimit(i));
|
||||
|
||||
if (inSlot.getCount() + toInsert.getCount() <= max) {
|
||||
return new TransitResponse(requestEntry.getValue(), requestEntry.getKey());
|
||||
} else {
|
||||
int rejects = (inSlot.getCount() + toInsert.getCount()) - max;
|
||||
|
||||
if (rejects < toInsert.getCount()) {
|
||||
toInsert = StackUtils.size(toInsert, rejects);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (TransporterManager.didEmit(requestEntry.getKey(), toInsert)) {
|
||||
return new TransitResponse(requestEntry.getValue(), getToUse(requestEntry.getKey(), toInsert));
|
||||
}
|
||||
} else if (tileEntity instanceof ISidedInventory) {
|
||||
ISidedInventory sidedInventory = (ISidedInventory) tileEntity;
|
||||
int[] slots = sidedInventory.getSlotsForFace(side.getOpposite());
|
||||
|
||||
@@ -484,49 +527,6 @@ public class TransporterManager {
|
||||
}
|
||||
}
|
||||
|
||||
if (TransporterManager.didEmit(requestEntry.getKey(), toInsert)) {
|
||||
return new TransitResponse(requestEntry.getValue(), getToUse(requestEntry.getKey(), toInsert));
|
||||
}
|
||||
} else if (InventoryUtils.isItemHandler(tileEntity, side.getOpposite())) {
|
||||
IItemHandler inventory = InventoryUtils.getItemHandler(tileEntity, side.getOpposite());
|
||||
|
||||
for (int i = 0; i <= inventory.getSlots() - 1; i++) {
|
||||
ItemStack rejectStack = inventory.insertItem(i, toInsert, true);
|
||||
|
||||
if (!TransporterManager.didEmit(toInsert, rejectStack)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
ItemStack inSlot = copy.inventory.get(i);
|
||||
|
||||
if (toInsert.isEmpty()) {
|
||||
return new TransitResponse(requestEntry.getValue(), requestEntry.getKey());
|
||||
} else if (inSlot.isEmpty()) {
|
||||
if (toInsert.getCount() <= inventory.getSlotLimit(i)) {
|
||||
return new TransitResponse(requestEntry.getValue(), requestEntry.getKey());
|
||||
} else {
|
||||
int rejects = toInsert.getCount() - inventory.getSlotLimit(i);
|
||||
|
||||
if (rejects < toInsert.getCount()) {
|
||||
toInsert = StackUtils.size(toInsert, rejects);
|
||||
}
|
||||
}
|
||||
} else if (InventoryUtils.areItemsStackable(toInsert, inSlot) && inSlot.getCount() < Math
|
||||
.min(inSlot.getMaxStackSize(), inventory.getSlotLimit(i))) {
|
||||
int max = Math.min(inSlot.getMaxStackSize(), inventory.getSlotLimit(i));
|
||||
|
||||
if (inSlot.getCount() + toInsert.getCount() <= max) {
|
||||
return new TransitResponse(requestEntry.getValue(), requestEntry.getKey());
|
||||
} else {
|
||||
int rejects = (inSlot.getCount() + toInsert.getCount()) - max;
|
||||
|
||||
if (rejects < toInsert.getCount()) {
|
||||
toInsert = StackUtils.size(toInsert, rejects);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (TransporterManager.didEmit(requestEntry.getKey(), toInsert)) {
|
||||
return new TransitResponse(requestEntry.getValue(), getToUse(requestEntry.getKey(), toInsert));
|
||||
}
|
||||
|
||||
@@ -67,7 +67,17 @@ public final class InventoryUtils {
|
||||
ItemStack toInsert = requestEntry.getKey().copy();
|
||||
|
||||
//prioritize other implementations first to allow item forcing
|
||||
if (tile instanceof ISidedInventory) {
|
||||
if (isItemHandler(tile, side.getOpposite())) {
|
||||
IItemHandler inventory = getItemHandler(tile, side.getOpposite());
|
||||
|
||||
for (int i = 0; i < inventory.getSlots(); i++) {
|
||||
toInsert = inventory.insertItem(i, toInsert, false);
|
||||
|
||||
if (toInsert.isEmpty()) {
|
||||
return new TransitResponse(requestEntry.getValue(), requestEntry.getKey());
|
||||
}
|
||||
}
|
||||
} else if (tile instanceof ISidedInventory) {
|
||||
ISidedInventory sidedInventory = (ISidedInventory) tile;
|
||||
int[] slots = sidedInventory.getSlotsForFace(side.getOpposite());
|
||||
|
||||
@@ -197,16 +207,6 @@ public final class InventoryUtils {
|
||||
}
|
||||
}
|
||||
}
|
||||
} else if (isItemHandler(tile, side.getOpposite())) {
|
||||
IItemHandler inventory = getItemHandler(tile, side.getOpposite());
|
||||
|
||||
for (int i = 0; i < inventory.getSlots(); i++) {
|
||||
toInsert = inventory.insertItem(i, toInsert, false);
|
||||
|
||||
if (toInsert.isEmpty()) {
|
||||
return new TransitResponse(requestEntry.getValue(), requestEntry.getKey());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (TransporterManager.didEmit(requestEntry.getKey(), toInsert)) {
|
||||
|
||||
@@ -29,7 +29,9 @@ public final class TransporterUtils {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (tile instanceof IInventory) {
|
||||
if (InventoryUtils.isItemHandler(tile, side.getOpposite())) {
|
||||
return true;
|
||||
} else if (tile instanceof IInventory) {
|
||||
IInventory inventory = (IInventory) tile;
|
||||
|
||||
if (inventory.getSizeInventory() > 0) {
|
||||
@@ -41,8 +43,6 @@ public final class TransporterUtils {
|
||||
|
||||
return slots.length > 0;
|
||||
}
|
||||
} else {
|
||||
return InventoryUtils.isItemHandler(tile, side.getOpposite());
|
||||
}
|
||||
|
||||
return false;
|
||||
|
||||
Reference in new issue
Block a user