Utils ported

This commit is contained in:
Sebastian Hartte
2020-06-28 13:28:14 +02:00
parent fee5b7fdfc
commit 46955643b9
105 changed files with 239 additions and 379 deletions
@@ -1,84 +1,42 @@
package appeng.tile.inventory;
import net.minecraft.item.ItemStack;
import net.minecraftforge.items.IItemHandlerModifiable;
import alexiil.mc.lib.attributes.Simulation;
import alexiil.mc.lib.attributes.item.impl.DelegatingFixedItemInv;
import appeng.api.storage.cells.ICellInventory;
import appeng.api.storage.cells.ICellInventoryHandler;
import appeng.util.inv.IAEAppEngInventory;
import appeng.util.inv.filter.IAEItemFilter;
import net.minecraft.item.ItemStack;
public class AppEngCellInventory implements IItemHandlerModifiable {
private final AppEngInternalInventory inv;
private final ICellInventoryHandler handlerForSlot[];
public class AppEngCellInventory extends DelegatingFixedItemInv {
private final ICellInventoryHandler<?>[] handlerForSlot;
public AppEngCellInventory(final IAEAppEngInventory host, final int slots) {
this.inv = new AppEngInternalInventory(host, slots, 1);
super(new AppEngInternalInventory(host, slots, 1));
this.handlerForSlot = new ICellInventoryHandler[slots];
}
public void setHandler(final int slot, final ICellInventoryHandler handler) {
public void setHandler(final int slot, final ICellInventoryHandler<?> handler) {
this.handlerForSlot[slot] = handler;
}
public void setFilter(IAEItemFilter filter) {
this.inv.setFilter(filter);
}
@Override
public void setStackInSlot(int slot, ItemStack stack) {
public boolean setInvStack(int slot, ItemStack to, Simulation simulation) {
this.persist(slot);
this.inv.setStackInSlot(slot, stack);
boolean result = super.setInvStack(slot, to, simulation);
this.cleanup(slot);
return result;
}
@Override
public int getSlots() {
return this.inv.getSlots();
}
@Override
public ItemStack getStackInSlot(int slot) {
public ItemStack getInvStack(int slot) {
this.persist(slot);
return this.inv.getStackInSlot(slot);
}
@Override
public ItemStack insertItem(int slot, ItemStack stack, boolean simulate) {
this.persist(slot);
final ItemStack ret = this.inv.insertItem(slot, stack, simulate);
this.cleanup(slot);
return ret;
}
@Override
public ItemStack extractItem(int slot, int amount, boolean simulate) {
this.persist(slot);
final ItemStack ret = this.inv.extractItem(slot, amount, simulate);
this.cleanup(slot);
return ret;
}
@Override
public int getSlotLimit(int slot) {
return this.inv.getSlotLimit(slot);
}
@Override
public boolean isItemValid(int slot, ItemStack stack) {
return this.inv.isItemValid(slot, stack);
}
public void persist() {
for (int i = 0; i < this.getSlots(); ++i) {
this.persist(i);
}
return super.getInvStack(slot);
}
private void persist(int slot) {
if (this.handlerForSlot[slot] != null) {
final ICellInventory ci = this.handlerForSlot[slot].getCellInv();
final ICellInventory<?> ci = this.handlerForSlot[slot].getCellInv();
if (ci != null) {
ci.persist();
}
@@ -87,9 +45,9 @@ public class AppEngCellInventory implements IItemHandlerModifiable {
private void cleanup(int slot) {
if (this.handlerForSlot[slot] != null) {
final ICellInventory ci = this.handlerForSlot[slot].getCellInv();
final ICellInventory<?> ci = this.handlerForSlot[slot].getCellInv();
if (ci == null || ci.getItemStack() != this.inv.getStackInSlot(slot)) {
if (ci == null || ci.getItemStack() != super.getInvStack(slot)) {
this.handlerForSlot[slot] = null;
}
}
@@ -21,11 +21,15 @@ package appeng.tile.inventory;
import java.util.Iterator;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import alexiil.mc.lib.attributes.ListenerRemovalToken;
import alexiil.mc.lib.attributes.ListenerToken;
import alexiil.mc.lib.attributes.Simulation;
import alexiil.mc.lib.attributes.item.FixedItemInv;
import alexiil.mc.lib.attributes.item.InvMarkDirtyListener;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.CompoundTag;
import net.minecraftforge.items.IItemHandlerModifiable;
import net.minecraftforge.items.ItemHandlerHelper;
import appeng.api.AEApi;
import appeng.api.storage.channels.IItemStorageChannel;
@@ -35,10 +39,9 @@ import appeng.util.Platform;
import appeng.util.inv.IAEAppEngInventory;
import appeng.util.inv.InvOperation;
import appeng.util.item.AEItemStack;
import appeng.util.iterators.AEInvIterator;
import appeng.util.iterators.InvIterator;
public class AppEngInternalAEInventory implements IItemHandlerModifiable, Iterable<ItemStack> {
public class AppEngInternalAEInventory implements FixedItemInv, Iterable<ItemStack> {
private final IAEAppEngInventory te;
private final IAEItemStack[] inv;
private final int size;
@@ -103,105 +106,74 @@ public class AppEngInternalAEInventory implements IItemHandlerModifiable, Iterab
}
protected int getStackLimit(int slot, @Nonnull ItemStack stack) {
return Math.min(this.getSlotLimit(slot), stack.getMaxStackSize());
return Math.min(this.getMaxAmount(slot, stack), stack.getMaxCount());
}
@Override
public int getSlots() {
public ItemStack getInvStack(int slot) {
if (this.inv[slot] == null) {
return ItemStack.EMPTY;
}
return this.inv[slot].createItemStack();
}
@Override
public boolean setInvStack(int slot, ItemStack to, Simulation simulation) {
if (this.te == null || !Platform.isServer()) {
return false;
}
// FIXME: We need to implement the actual checks here, stacking /caninsert/canremove
if (true) {
throw new IllegalStateException();
}
if (simulation == Simulation.SIMULATE) {
return true;
}
ItemStack oldStack = this.getInvStack(slot).copy();
this.inv[slot] = AEApi.instance().storage().getStorageChannel(IItemStorageChannel.class)
.createStack(to);
ItemStack newStack = to.copy();
InvOperation op = InvOperation.SET;
if (ItemStack.areItemsEqual(oldStack, newStack)) {
if (newStack.getCount() > oldStack.getCount()) {
newStack.decrement(oldStack.getCount());
oldStack = ItemStack.EMPTY;
op = InvOperation.INSERT;
} else {
oldStack.decrement(newStack.getCount());
newStack = ItemStack.EMPTY;
op = InvOperation.EXTRACT;
}
}
this.fireOnChangeInventory(slot, op, oldStack, newStack);
return true;
}
@Override
public int getSlotCount() {
return this.size;
}
@Override
public ItemStack getStackInSlot(final int var1) {
if (this.inv[var1] == null) {
return ItemStack.EMPTY;
}
return this.inv[var1].createItemStack();
public boolean isItemValidForSlot(int slot, ItemStack stack) {
return true;
}
@Override
public ItemStack insertItem(int slot, ItemStack stack, boolean simulate) {
if (stack.isEmpty()) {
return ItemStack.EMPTY;
}
ItemStack existing = this.getStackInSlot(slot);
int limit = this.getStackLimit(slot, stack);
if (!existing.isEmpty()) {
if (!ItemHandlerHelper.canItemStacksStack(stack, existing)) {
return stack;
}
limit -= existing.getCount();
}
if (limit <= 0) {
return stack;
}
boolean reachedLimit = stack.getCount() > limit;
if (!simulate) {
if (existing.isEmpty()) {
this.inv[slot] = AEApi.instance().storage().getStorageChannel(IItemStorageChannel.class)
.createStack(reachedLimit ? Platform.copyStackWithSize(stack, limit) : stack);
} else {
existing.increment(reachedLimit ? limit : stack.getCount());
}
this.fireOnChangeInventory(slot, InvOperation.INSERT, ItemStack.EMPTY,
reachedLimit ? Platform.copyStackWithSize(stack, limit) : stack);
}
return reachedLimit ? Platform.copyStackWithSize(stack, stack.getCount() - limit) : ItemStack.EMPTY;
public int getChangeValue() {
return 0;
}
@Nullable
@Override
public ItemStack extractItem(int slot, int amount, boolean simulate) {
if (this.inv[slot] != null) {
final ItemStack split = this.getStackInSlot(slot);
if (amount >= split.getCount()) {
if (!simulate) {
this.inv[slot] = null;
this.fireOnChangeInventory(slot, InvOperation.EXTRACT, split, ItemStack.EMPTY);
}
return split;
} else {
if (!simulate) {
split.increment(-amount);
this.fireOnChangeInventory(slot, InvOperation.EXTRACT,
Platform.copyStackWithSize(split, amount), ItemStack.EMPTY);
}
return Platform.copyStackWithSize(split, amount);
}
}
return ItemStack.EMPTY;
}
@Override
public void setStackInSlot(final int slot, final ItemStack newItemStack) {
ItemStack oldStack = this.getStackInSlot(slot).copy();
this.inv[slot] = AEApi.instance().storage().getStorageChannel(IItemStorageChannel.class)
.createStack(newItemStack);
if (this.te != null && Platform.isServer()) {
ItemStack newStack = newItemStack.copy();
InvOperation op = InvOperation.SET;
if (ItemStack.areItemsEqual(oldStack, newStack)) {
if (newStack.getCount() > oldStack.getCount()) {
newStack.shrink(oldStack.getCount());
oldStack = ItemStack.EMPTY;
op = InvOperation.INSERT;
} else {
oldStack.shrink(newStack.getCount());
newStack = ItemStack.EMPTY;
op = InvOperation.EXTRACT;
}
}
this.fireOnChangeInventory(slot, op, oldStack, newStack);
}
public ListenerToken addListener(InvMarkDirtyListener listener, ListenerRemovalToken removalToken) {
return null;
}
private void fireOnChangeInventory(int slot, InvOperation op, ItemStack removed, ItemStack inserted) {
@@ -214,8 +186,8 @@ public class AppEngInternalAEInventory implements IItemHandlerModifiable, Iterab
}
@Override
public int getSlotLimit(int slot) {
return this.maxStack > 64 ? 64 : this.maxStack;
public int getMaxAmount(int slot, ItemStack is) {
return Math.min(this.maxStack, 64);
}
@Override
@@ -223,12 +195,4 @@ public class AppEngInternalAEInventory implements IItemHandlerModifiable, Iterab
return new InvIterator(this);
}
public Iterator<IAEItemStack> getNewAEIterator() {
return new AEInvIterator(this);
}
@Override
public boolean isItemValid(int slot, ItemStack stack) {
return true;
}
}
@@ -18,26 +18,28 @@
package appeng.tile.inventory;
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import javax.annotation.Nonnull;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.CompoundTag;
import net.minecraftforge.items.ItemStackHandler;
import alexiil.mc.lib.attributes.Simulation;
import alexiil.mc.lib.attributes.item.FixedItemInvView;
import alexiil.mc.lib.attributes.item.filter.ConstantItemFilter;
import alexiil.mc.lib.attributes.item.filter.ItemFilter;
import alexiil.mc.lib.attributes.item.impl.FullFixedItemInv;
import appeng.util.Platform;
import appeng.util.inv.IAEAppEngInventory;
import appeng.util.inv.InvOperation;
import appeng.util.inv.filter.IAEItemFilter;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.CompoundTag;
public class AppEngInternalInventory extends ItemStackHandler implements Iterable<ItemStack> {
import javax.annotation.Nonnull;
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
// FIXME: the filtering is not correctly implemented and need to be reworked
public class AppEngInternalInventory extends FullFixedItemInv implements Iterable<ItemStack> {
private boolean enableClientEvents = false;
private IAEAppEngInventory te;
private final int[] maxStack;
private ItemStack previousStack = ItemStack.EMPTY;
private IAEItemFilter filter;
private boolean dirtyFlag = false;
@@ -48,6 +50,8 @@ public class AppEngInternalInventory extends ItemStackHandler implements Iterabl
this.setFilter(filter);
this.maxStack = new int[size];
Arrays.fill(this.maxStack, maxStack);
setOwnerListener(this::onContentsChanged);
}
public AppEngInternalInventory(final IAEAppEngInventory inventory, final int size, final int maxStack) {
@@ -63,57 +67,24 @@ public class AppEngInternalInventory extends ItemStackHandler implements Iterabl
}
@Override
public int getSlotLimit(int slot) {
return this.maxStack[slot];
public int getMaxAmount(int slot, ItemStack stack) {
return Math.min(maxStack[slot], super.getMaxAmount(slot, stack));
}
@Override
public void setStackInSlot(int slot, @Nonnull ItemStack stack) {
this.previousStack = this.getStackInSlot(slot).copy();
super.setStackInSlot(slot, stack);
}
@Override
@Nonnull
public ItemStack insertItem(int slot, @Nonnull ItemStack stack, boolean simulate) {
if (this.filter != null && !this.filter.allowInsert(this, slot, stack)) {
return stack;
}
if (!simulate) {
this.previousStack = this.getStackInSlot(slot).copy();
}
return super.insertItem(slot, stack, simulate);
}
@Override
@Nonnull
public ItemStack extractItem(int slot, int amount, boolean simulate) {
if (this.filter != null && !this.filter.allowExtract(this, slot, amount)) {
return ItemStack.EMPTY;
}
if (!simulate) {
this.previousStack = this.getStackInSlot(slot).copy();
}
return super.extractItem(slot, amount, simulate);
}
@Override
protected void onContentsChanged(int slot) {
protected void onContentsChanged(FixedItemInvView inv, int slot, ItemStack previous, ItemStack current) {
if (this.getBlockEntity() != null && this.eventsEnabled() && !this.dirtyFlag) {
this.dirtyFlag = true;
ItemStack newStack = this.getStackInSlot(slot).copy();
ItemStack oldStack = this.previousStack;
ItemStack newStack = current.copy();
ItemStack oldStack = previous;
InvOperation op = InvOperation.SET;
if (newStack.isEmpty() || oldStack.isEmpty() || ItemStack.areItemsEqual(newStack, oldStack)) {
if (newStack.getCount() > oldStack.getCount()) {
newStack.shrink(oldStack.getCount());
newStack.decrement(oldStack.getCount());
oldStack = ItemStack.EMPTY;
op = InvOperation.INSERT;
} else {
oldStack.shrink(newStack.getCount());
oldStack.decrement(newStack.getCount());
newStack = ItemStack.EMPTY;
op = InvOperation.EXTRACT;
}
@@ -121,10 +92,8 @@ public class AppEngInternalInventory extends ItemStackHandler implements Iterabl
this.getBlockEntity().onChangeInventory(this, slot, op, oldStack, newStack);
this.getBlockEntity().saveChanges();
this.previousStack = ItemStack.EMPTY;
this.dirtyFlag = false;
}
super.onContentsChanged(slot);
}
protected boolean eventsEnabled() {
@@ -136,7 +105,25 @@ public class AppEngInternalInventory extends ItemStackHandler implements Iterabl
}
@Override
public boolean isItemValid(int slot, ItemStack stack) {
public ItemFilter getFilterForSlot(int slot) {
if (this.maxStack[slot] == 0) {
return ConstantItemFilter.NOTHING;
}
if (this.filter != null) {
return stack -> {
// FIXME: This is not correct...
if (stack == ItemStack.EMPTY) {
return filter.allowExtract(this, slot, this.getSlot(slot).get().getCount());
} else {
return filter.allowExtract(this, slot, this.getSlot(slot).get().getCount());
}
};
}
return ConstantItemFilter.ANYTHING;
}
@Override
public boolean isItemValidForSlot(int slot, ItemStack stack) {
if (this.maxStack[slot] == 0) {
return false;
}
@@ -147,7 +134,7 @@ public class AppEngInternalInventory extends ItemStackHandler implements Iterabl
}
public void writeToNBT(final CompoundTag data, final String name) {
data.put(name, this.serializeNBT());
data.put(name, this.toTag());
}
public void readFromNBT(final CompoundTag data, final String name) {
@@ -158,12 +145,12 @@ public class AppEngInternalInventory extends ItemStackHandler implements Iterabl
}
public void readFromNBT(final CompoundTag data) {
this.deserializeNBT(data);
this.fromTag(data);
}
@Override
public Iterator<ItemStack> iterator() {
return Collections.unmodifiableList(super.stacks).iterator();
return stackIterable().iterator();
}
private boolean isEnableClientEvents() {