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,287 +0,0 @@
/*
* This file is part of Applied Energistics 2.
* Copyright (c) 2013 - 2020, AlgorithmX2, All rights reserved.
*
* Applied Energistics 2 is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Applied Energistics 2 is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Applied Energistics 2. If not, see <http://www.gnu.org/licenses/lgpl>.
*/
package appeng.util.item;
import java.util.List;
import java.util.Objects;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import net.fabricmc.api.Environment;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.PacketByteBuf;
import net.minecraft.text.Text;
import net.fabricmc.api.EnvType;
import appeng.api.AEApi;
import appeng.api.config.FuzzyMode;
import appeng.api.storage.IStorageChannel;
import appeng.api.storage.channels.IItemStorageChannel;
import appeng.api.storage.data.IAEItemStack;
import appeng.util.Platform;
import net.minecraft.util.registry.Registry;
public final class AEItemStack extends AEStack<IAEItemStack> implements IAEItemStack {
private static final String NBT_STACKSIZE = "cnt";
private static final String NBT_REQUESTABLE = "req";
private static final String NBT_CRAFTABLE = "craft";
private static final String NBT_ITEMSTACK = "is";
private final AESharedItemStack sharedStack;
@Environment(EnvType.CLIENT)
private Text displayName;
@Environment(EnvType.CLIENT)
private List<Text> tooltip;
private AEItemStack(final AEItemStack is) {
this.setStackSize(is.getStackSize());
this.setCraftable(is.isCraftable());
this.setCountRequestable(is.getCountRequestable());
this.sharedStack = is.sharedStack;
}
private AEItemStack(final AESharedItemStack is, long size) {
this.sharedStack = is;
this.setStackSize(size);
this.setCraftable(false);
this.setCountRequestable(0);
}
@Nullable
public static AEItemStack fromItemStack(@Nonnull final ItemStack stack) {
if (stack.isEmpty()) {
return null;
}
return new AEItemStack(AEItemStackRegistry.getRegisteredStack(stack), stack.getCount());
}
public static IAEItemStack fromNBT(final CompoundTag i) {
if (i == null) {
return null;
}
final ItemStack itemstack = ItemStack.fromTag(i.getCompound(NBT_ITEMSTACK));
if (itemstack.isEmpty()) {
return null;
}
final AEItemStack item = AEItemStack.fromItemStack(itemstack);
item.setStackSize(i.getLong(NBT_STACKSIZE));
item.setCountRequestable(i.getLong(NBT_REQUESTABLE));
item.setCraftable(i.getBoolean(NBT_CRAFTABLE));
return item;
}
@Override
public void writeToNBT(final CompoundTag i) {
final CompoundTag itemStack = new CompoundTag();
this.getDefinition().toTag(itemStack);
i.put(NBT_ITEMSTACK, itemStack);
i.putLong(NBT_STACKSIZE, this.getStackSize());
i.putLong(NBT_REQUESTABLE, this.getCountRequestable());
i.putBoolean(NBT_CRAFTABLE, this.isCraftable());
}
public static AEItemStack fromPacket(final PacketByteBuf buffer) {
final boolean isCraftable = buffer.readBoolean();
final long stackSize = buffer.readVarLong();
final long countRequestable = buffer.readVarLong();
final ItemStack itemstack = buffer.readItemStack();
if (itemstack.isEmpty()) {
return null;
}
final AEItemStack item = new AEItemStack(AEItemStackRegistry.getRegisteredStack(itemstack), stackSize);
item.setCountRequestable(countRequestable);
item.setCraftable(isCraftable);
return item;
}
@Override
public void writeToPacket(final PacketByteBuf buffer) {
buffer.writeBoolean(this.isCraftable());
buffer.writeVarLong(this.getStackSize());
buffer.writeVarLong(this.getCountRequestable());
buffer.writeItemStack(getDefinition());
}
@Override
public void add(final IAEItemStack option) {
if (option == null) {
return;
}
this.incStackSize(option.getStackSize());
this.setCountRequestable(this.getCountRequestable() + option.getCountRequestable());
this.setCraftable(this.isCraftable() || option.isCraftable());
}
@Override
public boolean fuzzyComparison(final IAEItemStack other, final FuzzyMode mode) {
final ItemStack itemStack = this.getDefinition();
final ItemStack otherStack = other.getDefinition();
return this.fuzzyItemStackComparison(itemStack, otherStack, mode);
}
@Override
public IAEItemStack copy() {
return new AEItemStack(this);
}
@Override
public boolean isItem() {
return true;
}
@Override
public boolean isFluid() {
return false;
}
@Override
public IStorageChannel<IAEItemStack> getChannel() {
return AEApi.instance().storage().getStorageChannel(IItemStorageChannel.class);
}
@Override
public ItemStack createItemStack() {
return Platform.copyStackWithSize(this.getDefinition(),
(int) Math.min(Integer.MAX_VALUE, this.getStackSize()));
}
@Override
public Item getItem() {
return this.getDefinition().getItem();
}
@Override
public int getItemDamage() {
return this.sharedStack.getItemDamage();
}
@Override
public boolean isSameType(final IAEItemStack otherStack) {
if (otherStack == null) {
return false;
}
return Objects.equals(this.sharedStack, ((AEItemStack) otherStack).sharedStack);
}
@Override
public boolean isSameType(final ItemStack otherStack) {
if (otherStack.isEmpty()) {
return false;
}
int oldSize = otherStack.getCount();
otherStack.setCount(1);
boolean ret = ItemStack.areEqual(this.getDefinition(), otherStack);
otherStack.setCount(oldSize);
return ret;
}
@Override
public int hashCode() {
return this.sharedStack.hashCode();
}
@Override
public boolean equals(final Object ia) {
if (ia instanceof AEItemStack) {
return this.isSameType((AEItemStack) ia);
} else if (ia instanceof ItemStack) {
return this.isSameType((ItemStack) ia);
}
return false;
}
@Override
public String toString() {
return this.getStackSize() + "x" + Registry.ITEM.getId(this.getDefinition().getItem());
}
@Environment(EnvType.CLIENT)
public List<Text> getToolTip() {
if (this.tooltip == null) {
this.tooltip = Platform.getTooltip(this.asItemStackRepresentation());
}
return this.tooltip;
}
@Environment(EnvType.CLIENT)
public Text getDisplayName() {
if (this.displayName == null) {
this.displayName = Platform.getItemDisplayName(this.asItemStackRepresentation());
}
return this.displayName;
}
@Environment(EnvType.CLIENT)
public String getModID() {
return Registry.ITEM.getId(this.getDefinition().getItem()).getNamespace();
}
@Override
public boolean hasTagCompound() {
return this.getDefinition().hasTag();
}
@Override
public ItemStack asItemStackRepresentation() {
return this.getDefinition().copy();
}
@Override
public ItemStack getDefinition() {
return this.sharedStack.getDefinition();
}
AESharedItemStack getSharedStack() {
return this.sharedStack;
}
private boolean fuzzyItemStackComparison(ItemStack a, ItemStack b, FuzzyMode mode) {
if (a.getItem() == b.getItem()) {
if (a.getItem().isDamageable()) {
if (mode == FuzzyMode.IGNORE_ALL) {
return true;
} else if (mode == FuzzyMode.PERCENT_99) {
return (a.getDamage() > 1) == (b.getDamage() > 1);
} else {
final float percentDamageOfA = (float) a.getDamage() / a.getMaxDamage();
final float percentDamageOfB = (float) b.getDamage() / b.getMaxDamage();
return (percentDamageOfA > mode.breakPoint) == (percentDamageOfB > mode.breakPoint);
}
}
}
return false;
}
}
@@ -1,74 +0,0 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2017 AlgorithmX2
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package appeng.util.item;
import java.lang.ref.WeakReference;
import java.util.WeakHashMap;
import javax.annotation.Nonnull;
import net.minecraft.item.ItemStack;
import appeng.util.Platform;
public final class AEItemStackRegistry {
private static final WeakHashMap<AESharedItemStack, WeakReference<AESharedItemStack>> SERVER_REGISTRY = new WeakHashMap<>();
private static final WeakHashMap<AESharedItemStack, WeakReference<AESharedItemStack>> CLIENT_REGISTRY = new WeakHashMap<>();
private AEItemStackRegistry() {
}
private static WeakHashMap<AESharedItemStack, WeakReference<AESharedItemStack>> registry() {
if (Platform.isClient()) {
return CLIENT_REGISTRY;
} else {
return SERVER_REGISTRY;
}
}
static synchronized AESharedItemStack getRegisteredStack(final @Nonnull ItemStack itemStack) {
if (itemStack.isEmpty()) {
throw new IllegalArgumentException("stack cannot be empty");
}
int oldStackSize = itemStack.getCount();
itemStack.setCount(1);
AESharedItemStack search = new AESharedItemStack(itemStack);
WeakReference<AESharedItemStack> weak = registry().get(search);
AESharedItemStack ret = null;
if (weak != null) {
ret = weak.get();
}
if (ret == null) {
ret = new AESharedItemStack(itemStack.copy());
registry().put(ret, new WeakReference<>(ret));
}
itemStack.setCount(oldStackSize);
return ret;
}
}
@@ -1,186 +0,0 @@
/*
* This file is part of Applied Energistics 2.
* Copyright (c) 2013 - 2014, AlgorithmX2, All rights reserved.
*
* Applied Energistics 2 is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Applied Energistics 2 is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Applied Energistics 2. If not, see <http://www.gnu.org/licenses/lgpl>.
*/
package appeng.util.item;
import java.util.Objects;
import com.google.common.base.Preconditions;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.CompoundTag;
import appeng.api.config.FuzzyMode;
final class AESharedItemStack implements Comparable<AESharedItemStack> {
private final ItemStack itemStack;
private final int itemId;
private final int itemDamage;
private final int hashCode;
public AESharedItemStack(final ItemStack itemStack) {
this.itemStack = itemStack;
this.itemId = Item.getRawId(itemStack.getItem());
this.itemDamage = itemStack.getDamage();
this.hashCode = this.makeHashCode();
}
Bounds getBounds(final FuzzyMode fuzzy) {
return new Bounds(this.itemStack, fuzzy);
}
ItemStack getDefinition() {
return this.itemStack;
}
int getItemDamage() {
return this.itemDamage;
}
int getItemID() {
return this.itemId;
}
@Override
public int hashCode() {
return this.hashCode;
}
@Override
public boolean equals(final Object obj) {
if (obj instanceof AESharedItemStack) {
final AESharedItemStack other = (AESharedItemStack) obj;
Preconditions.checkState(this.itemStack.getCount() == 1, "ItemStack#getCount() has to be 1");
Preconditions.checkArgument(other.getDefinition().getCount() == 1, "ItemStack#getCount() has to be 1");
if (this.itemStack == other.itemStack) {
return true;
}
return ItemStack.areEqual(this.itemStack, other.itemStack);
}
return false;
}
@Override
public int compareTo(final AESharedItemStack b) {
Preconditions.checkState(this.itemStack.getCount() == 1, "ItemStack#getCount() has to be 1");
Preconditions.checkArgument(b.getDefinition().getCount() == 1, "ItemStack#getCount() has to be 1");
if (this.itemStack == b.getDefinition()) {
return 0;
}
final int id = this.itemId - b.itemId;
if (id != 0) {
return id;
}
final int damageValue = this.itemDamage - b.itemDamage;
if (damageValue != 0) {
return damageValue;
}
return 0;
}
private int makeHashCode() {
return Objects.hash(this.itemId, this.itemDamage, this.itemStack.hasTag() ? this.itemStack.getTag() : 0);
}
/**
* Creates the lower and upper bounds for a specific shared itemstack.
*/
public static final class Bounds {
/**
* Bounds enforced by {@link ItemStack#isEmpty()}
*/
private static final int MIN_DAMAGE_VALUE = 0;
private static final int MAX_DAMAGE_VALUE = Short.MAX_VALUE;
private final AESharedItemStack lower;
private final AESharedItemStack upper;
public Bounds(final ItemStack stack, final FuzzyMode fuzzy) {
Preconditions.checkState(!stack.isEmpty(), "ItemStack#isEmpty() has to be false");
Preconditions.checkState(stack.getCount() == 1, "ItemStack#getCount() has to be 1");
final CompoundTag tag = stack.hasTag() ? stack.getTag() : null;
this.lower = this.makeLowerBound(stack, tag, fuzzy);
this.upper = this.makeUpperBound(stack, tag, fuzzy);
}
public AESharedItemStack lower() {
return this.lower;
}
public AESharedItemStack upper() {
return this.upper;
}
private AESharedItemStack makeLowerBound(final ItemStack itemStack, final CompoundTag tag,
final FuzzyMode fuzzy) {
final ItemStack newDef = itemStack.copy();
if (newDef.getItem().isDamageable()) {
if (fuzzy == FuzzyMode.IGNORE_ALL) {
newDef.setDamage(MIN_DAMAGE_VALUE);
} else if (fuzzy == FuzzyMode.PERCENT_99) {
if (itemStack.getDamage() == MIN_DAMAGE_VALUE) {
newDef.setDamage(MIN_DAMAGE_VALUE);
} else {
newDef.setDamage(MIN_DAMAGE_VALUE + 1);
}
} else {
final int breakpoint = fuzzy.calculateBreakPoint(itemStack.getMaxDamage());
final int damage = breakpoint <= itemStack.getDamage() ? breakpoint : 0;
newDef.setDamage(damage);
}
}
return new AESharedItemStack(newDef);
}
private AESharedItemStack makeUpperBound(final ItemStack itemStack, final CompoundTag tag,
final FuzzyMode fuzzy) {
final ItemStack newDef = itemStack.copy();
if (newDef.getItem().isDamageable()) {
if (fuzzy == FuzzyMode.IGNORE_ALL) {
newDef.setDamage(itemStack.getMaxDamage() + 1);
} else if (fuzzy == FuzzyMode.PERCENT_99) {
if (itemStack.getDamage() == MIN_DAMAGE_VALUE) {
newDef.setDamage(MIN_DAMAGE_VALUE);
} else {
newDef.setDamage(itemStack.getMaxDamage() + 1);
}
} else {
final int breakpoint = fuzzy.calculateBreakPoint(itemStack.getMaxDamage());
final int damage = itemStack.getDamage() < breakpoint ? breakpoint - 1
: itemStack.getMaxDamage() + 1;
newDef.setDamage(damage);
}
}
return new AESharedItemStack(newDef);
}
}
}
-103
View File
@@ -1,103 +0,0 @@
/*
* This file is part of Applied Energistics 2.
* Copyright (c) 2013 - 2014, AlgorithmX2, All rights reserved.
*
* Applied Energistics 2 is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Applied Energistics 2 is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Applied Energistics 2. If not, see <http://www.gnu.org/licenses/lgpl>.
*/
package appeng.util.item;
import appeng.api.storage.data.IAEStack;
public abstract class AEStack<T extends IAEStack<T>> implements IAEStack<T> {
private boolean isCraftable;
private long stackSize;
private long countRequestable;
@Override
public long getStackSize() {
return this.stackSize;
}
@Override
public T setStackSize(final long ss) {
this.stackSize = ss;
return (T) this;
}
@Override
public long getCountRequestable() {
return this.countRequestable;
}
@Override
public T setCountRequestable(final long countRequestable) {
this.countRequestable = countRequestable;
return (T) this;
}
@Override
public boolean isCraftable() {
return this.isCraftable;
}
@Override
public T setCraftable(final boolean isCraftable) {
this.isCraftable = isCraftable;
return (T) this;
}
@Override
public T reset() {
this.stackSize = 0;
this.setCountRequestable(0);
this.setCraftable(false);
return (T) this;
}
@Override
public T empty() {
final T dup = this.copy();
dup.reset();
return dup;
}
@Override
public boolean isMeaningful() {
return this.stackSize != 0 || this.countRequestable > 0 || this.isCraftable;
}
@Override
public void incStackSize(final long i) {
this.stackSize += i;
}
@Override
public void decStackSize(final long i) {
this.stackSize -= i;
}
@Override
public void incCountRequestable(final long i) {
this.countRequestable += i;
}
@Override
public void decCountRequestable(final long i) {
this.countRequestable -= i;
}
protected abstract boolean hasTagCompound();
}
@@ -1,171 +0,0 @@
/*
* This file is part of Applied Energistics 2.
* Copyright (c) 2013 - 2020, AlgorithmX2, All rights reserved.
*
* Applied Energistics 2 is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Applied Energistics 2 is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Applied Energistics 2. If not, see <http://www.gnu.org/licenses/lgpl>.
*/
package appeng.util.item;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.Map;
import appeng.api.config.FuzzyMode;
import appeng.api.storage.data.IAEItemStack;
import appeng.api.storage.data.IItemList;
abstract class AbstractItemList implements IItemList<IAEItemStack> {
@Override
public void add(final IAEItemStack option) {
if (option == null) {
return;
}
final IAEItemStack st = this.getRecords().get(((AEItemStack) option).getSharedStack());
if (st != null) {
st.add(option);
return;
}
final IAEItemStack opt = option.copy();
this.putItemRecord(opt);
}
@Override
public IAEItemStack findPrecise(final IAEItemStack itemStack) {
if (itemStack == null) {
return null;
}
return this.getRecords().get(((AEItemStack) itemStack).getSharedStack());
}
@Override
public Collection<IAEItemStack> findFuzzy(final IAEItemStack filter, final FuzzyMode fuzzy) {
if (filter == null) {
return Collections.emptyList();
}
return this.getRecords().values();
}
@Override
public boolean isEmpty() {
return !this.iterator().hasNext();
}
@Override
public void addStorage(final IAEItemStack option) {
if (option == null) {
return;
}
final IAEItemStack st = this.getRecords().get(((AEItemStack) option).getSharedStack());
if (st != null) {
st.incStackSize(option.getStackSize());
return;
}
final IAEItemStack opt = option.copy();
this.putItemRecord(opt);
}
@Override
public void addCrafting(final IAEItemStack option) {
if (option == null) {
return;
}
final IAEItemStack st = this.getRecords().get(((AEItemStack) option).getSharedStack());
if (st != null) {
st.setCraftable(true);
return;
}
final IAEItemStack opt = option.copy();
opt.setStackSize(0);
opt.setCraftable(true);
this.putItemRecord(opt);
}
@Override
public void addRequestable(final IAEItemStack option) {
if (option == null) {
return;
}
final IAEItemStack st = this.getRecords().get(((AEItemStack) option).getSharedStack());
if (st != null) {
st.setCountRequestable(st.getCountRequestable() + option.getCountRequestable());
return;
}
final IAEItemStack opt = option.copy();
opt.setStackSize(0);
opt.setCraftable(false);
opt.setCountRequestable(option.getCountRequestable());
this.putItemRecord(opt);
}
@Override
public IAEItemStack getFirstItem() {
for (final IAEItemStack stackType : this) {
return stackType;
}
return null;
}
@Override
public int size() {
int size = 0;
for (IAEItemStack entry : getRecords().values()) {
if (entry.isMeaningful()) {
size++;
}
}
return size;
}
@Override
public Iterator<IAEItemStack> iterator() {
return new MeaningfulItemIterator<>(this.getRecords().values());
}
@Override
public void resetStatus() {
for (final IAEItemStack i : this) {
i.reset();
}
}
abstract Map<AESharedItemStack, IAEItemStack> getRecords();
private IAEItemStack putItemRecord(final IAEItemStack itemStack) {
return this.getRecords().put(((AEItemStack) itemStack).getSharedStack(), itemStack);
}
}
@@ -1,55 +0,0 @@
/*
* This file is part of Applied Energistics 2.
* Copyright (c) 2013 - 2020, AlgorithmX2, All rights reserved.
*
* Applied Energistics 2 is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Applied Energistics 2 is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Applied Energistics 2. If not, see <http://www.gnu.org/licenses/lgpl>.
*/
package appeng.util.item;
import java.util.Collection;
import java.util.Collections;
import java.util.Map;
import java.util.NavigableMap;
import java.util.TreeMap;
import appeng.api.config.FuzzyMode;
import appeng.api.storage.data.IAEItemStack;
import appeng.util.item.AESharedItemStack.Bounds;
class FuzzyItemList extends AbstractItemList {
private final NavigableMap<AESharedItemStack, IAEItemStack> records = new TreeMap<>();
@Override
public Collection<IAEItemStack> findFuzzy(final IAEItemStack filter, final FuzzyMode fuzzy) {
if (filter == null) {
return Collections.emptyList();
}
return this.findFuzzyDamage(filter, fuzzy);
}
@Override
Map<AESharedItemStack, IAEItemStack> getRecords() {
return this.records;
}
private Collection<IAEItemStack> findFuzzyDamage(final IAEItemStack filter, final FuzzyMode fuzzy) {
final AEItemStack itemStack = (AEItemStack) filter;
final Bounds bounds = itemStack.getSharedStack().getBounds(fuzzy);
return this.records.subMap(bounds.lower(), true, bounds.upper(), true).descendingMap().values();
}
}
@@ -1,185 +0,0 @@
/*
* This file is part of Applied Energistics 2.
* Copyright (c) 2013 - 2020, AlgorithmX2, All rights reserved.
*
* Applied Energistics 2 is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Applied Energistics 2 is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Applied Energistics 2. If not, see <http://www.gnu.org/licenses/lgpl>.
*/
package appeng.util.item;
import java.util.Collection;
import java.util.Collections;
import java.util.IdentityHashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.NoSuchElementException;
import net.minecraft.item.Item;
import appeng.api.config.FuzzyMode;
import appeng.api.storage.data.IAEItemStack;
import appeng.api.storage.data.IItemList;
public final class ItemList implements IItemList<IAEItemStack> {
private final static IItemList<IAEItemStack> NULL_ITEMLIST = new NullItemList();
private final Map<Item, IItemList<IAEItemStack>> records = new IdentityHashMap<>();
@Override
public IAEItemStack findPrecise(final IAEItemStack itemStack) {
if (itemStack == null) {
return null;
}
return this.getRecord(itemStack.getItem()).findPrecise(itemStack);
}
@Override
public Collection<IAEItemStack> findFuzzy(final IAEItemStack filter, final FuzzyMode fuzzy) {
if (filter == null) {
return Collections.emptyList();
}
return this.getRecord(filter.getItem()).findFuzzy(filter, fuzzy);
}
@Override
public boolean isEmpty() {
return !this.iterator().hasNext();
}
@Override
public void add(final IAEItemStack itemStack) {
if (itemStack == null) {
return;
}
this.getOrCreateRecord(itemStack.getItem()).add(itemStack);
}
@Override
public void addStorage(final IAEItemStack itemStack) {
if (itemStack == null) {
return;
}
this.getOrCreateRecord(itemStack.getItem()).addStorage(itemStack);
}
@Override
public void addCrafting(final IAEItemStack itemStack) {
if (itemStack == null) {
return;
}
this.getOrCreateRecord(itemStack.getItem()).addCrafting(itemStack);
}
@Override
public void addRequestable(final IAEItemStack itemStack) {
if (itemStack == null) {
return;
}
this.getOrCreateRecord(itemStack.getItem()).addRequestable(itemStack);
}
@Override
public IAEItemStack getFirstItem() {
for (final IAEItemStack stackType : this) {
return stackType;
}
return null;
}
@Override
public int size() {
int size = 0;
for (IItemList<IAEItemStack> entry : records.values()) {
size += entry.size();
}
return size;
}
@Override
public Iterator<IAEItemStack> iterator() {
return new ChainedIterator(this.records.values().iterator());
}
@Override
public void resetStatus() {
for (final IAEItemStack i : this) {
i.reset();
}
}
private IItemList<IAEItemStack> getRecord(Item item) {
return this.records.getOrDefault(item, NULL_ITEMLIST);
}
private IItemList<IAEItemStack> getOrCreateRecord(Item item) {
return this.records.computeIfAbsent(item, this::makeRecordMap);
}
private IItemList<IAEItemStack> makeRecordMap(Item item) {
if (item.isDamageable()) {
return new FuzzyItemList();
} else {
return new StrictItemList();
}
}
private class ChainedIterator implements Iterator<IAEItemStack> {
private final Iterator<IItemList<IAEItemStack>> parent;
private Iterator<IAEItemStack> next;
public ChainedIterator(Iterator<IItemList<IAEItemStack>> iterator) {
this.parent = iterator;
if (this.parent.hasNext()) {
this.next = this.parent.next().iterator();
}
}
@Override
public boolean hasNext() {
while (this.next != null) {
if (this.next.hasNext()) {
return true;
}
if (this.parent.hasNext()) {
this.next = this.parent.next().iterator();
} else {
this.next = null;
}
}
return false;
}
@Override
public IAEItemStack next() {
if (this.next == null) {
throw new NoSuchElementException();
}
return this.next.next();
}
}
}
@@ -1,73 +0,0 @@
/*
* This file is part of Applied Energistics 2.
* Copyright (c) 2013 - 2014, AlgorithmX2, All rights reserved.
*
* Applied Energistics 2 is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Applied Energistics 2 is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Applied Energistics 2. If not, see <http://www.gnu.org/licenses/lgpl>.
*/
package appeng.util.item;
import java.util.Collection;
import appeng.api.AEApi;
import appeng.api.config.FuzzyMode;
import appeng.api.storage.channels.IItemStorageChannel;
import appeng.api.storage.data.IAEItemStack;
import appeng.api.storage.data.IItemContainer;
public class ItemModList implements IItemContainer<IAEItemStack> {
private final IItemContainer<IAEItemStack> backingStore;
private final IItemContainer<IAEItemStack> overrides = AEApi.instance().storage()
.getStorageChannel(IItemStorageChannel.class).createList();
public ItemModList(final IItemContainer<IAEItemStack> backend) {
this.backingStore = backend;
}
@Override
public void add(final IAEItemStack option) {
IAEItemStack over = this.overrides.findPrecise(option);
if (over == null) {
over = this.backingStore.findPrecise(option);
if (over == null) {
this.overrides.add(option);
} else {
option.add(over);
this.overrides.add(option);
}
} else {
this.overrides.add(option);
}
}
@Override
public IAEItemStack findPrecise(final IAEItemStack i) {
final IAEItemStack over = this.overrides.findPrecise(i);
if (over == null) {
return this.backingStore.findPrecise(i);
}
return over;
}
@Override
public Collection<IAEItemStack> findFuzzy(final IAEItemStack input, final FuzzyMode fuzzy) {
return this.overrides.findFuzzy(input, fuzzy);
}
@Override
public boolean isEmpty() {
return this.overrides.isEmpty() && this.backingStore.isEmpty();
}
}
@@ -1,74 +0,0 @@
/*
* This file is part of Applied Energistics 2.
* Copyright (c) 2013 - 2020, AlgorithmX2, All rights reserved.
*
* Applied Energistics 2 is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Applied Energistics 2 is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Applied Energistics 2. If not, see <http://www.gnu.org/licenses/lgpl>.
*/
package appeng.util.item;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.NoSuchElementException;
import appeng.api.storage.data.IAEItemStack;
public class MeaningfulItemIterator<T extends IAEItemStack> implements Iterator<T> {
private final Collection<T> collection;
private final Iterator<T> parent;
private T next;
private final Collection<T> toRemove = new ArrayList<>();
public MeaningfulItemIterator(final Collection<T> collection) {
this.collection = collection;
this.parent = collection.iterator();
}
@Override
public boolean hasNext() {
while (this.parent.hasNext()) {
this.next = this.parent.next();
if (this.next.isMeaningful()) {
return true;
} else {
// TODO: Avoid if possible
this.toRemove.add(this.next);
// this.parent.remove(); // self cleaning :3
}
}
// Cleanup afterwards to avoid CMEs
this.toRemove.forEach(entry -> this.collection.remove(entry));
this.next = null;
return false;
}
@Override
public T next() {
if (this.next == null) {
throw new NoSuchElementException();
}
return this.next;
}
@Override
public void remove() {
this.parent.remove();
}
}
@@ -1,81 +0,0 @@
/*
* This file is part of Applied Energistics 2.
* Copyright (c) 2013 - 2020, AlgorithmX2, All rights reserved.
*
* Applied Energistics 2 is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Applied Energistics 2 is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Applied Energistics 2. If not, see <http://www.gnu.org/licenses/lgpl>.
*/
package appeng.util.item;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import appeng.api.config.FuzzyMode;
import appeng.api.storage.data.IAEItemStack;
import appeng.api.storage.data.IItemList;
public class NullItemList implements IItemList<IAEItemStack> {
@Override
public void add(IAEItemStack option) {
}
@Override
public IAEItemStack findPrecise(IAEItemStack i) {
return null;
}
@Override
public Collection<IAEItemStack> findFuzzy(IAEItemStack input, FuzzyMode fuzzy) {
return Collections.emptyList();
}
@Override
public boolean isEmpty() {
return true;
}
@Override
public void addStorage(IAEItemStack option) {
}
@Override
public void addCrafting(IAEItemStack option) {
}
@Override
public void addRequestable(IAEItemStack option) {
}
@Override
public IAEItemStack getFirstItem() {
return null;
}
@Override
public int size() {
return 0;
}
@Override
public Iterator<IAEItemStack> iterator() {
return Collections.emptyIterator();
}
@Override
public void resetStatus() {
}
}
@@ -1,35 +0,0 @@
/*
* This file is part of Applied Energistics 2.
* Copyright (c) 2013 - 2020, AlgorithmX2, All rights reserved.
*
* Applied Energistics 2 is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Applied Energistics 2 is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Applied Energistics 2. If not, see <http://www.gnu.org/licenses/lgpl>.
*/
package appeng.util.item;
import java.util.IdentityHashMap;
import java.util.Map;
import appeng.api.storage.data.IAEItemStack;
class StrictItemList extends AbstractItemList {
private final Map<AESharedItemStack, IAEItemStack> records = new IdentityHashMap<>();
@Override
Map<AESharedItemStack, IAEItemStack> getRecords() {
return this.records;
}
}