IItemStackList holds some behavior that I don't want to duplicate everywhere

This commit is contained in:
Raoul Van den Berge
2016-10-07 02:10:45 +02:00
parent 48bb49ceeb
commit 3ee04b6480
12 changed files with 159 additions and 117 deletions

View File

@@ -3,6 +3,7 @@ package refinedstorage.api;
import refinedstorage.api.autocrafting.craftingmonitor.ICraftingMonitorElementRegistry;
import refinedstorage.api.autocrafting.registry.ICraftingTaskRegistry;
import refinedstorage.api.solderer.ISoldererRegistry;
import refinedstorage.api.util.IItemStackList;
import javax.annotation.Nonnull;
@@ -27,4 +28,10 @@ public interface IAPI {
*/
@Nonnull
ICraftingMonitorElementRegistry getCraftingMonitorElementRegistry();
/**
* @return a empty fast item stack list
*/
@Nonnull
IItemStackList createItemStackList();
}

View File

@@ -2,11 +2,9 @@ package refinedstorage.api.storage.item;
import net.minecraft.item.ItemStack;
import refinedstorage.api.network.INetworkMaster;
import refinedstorage.api.storage.CompareUtils;
import refinedstorage.api.util.IItemStackList;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.Collection;
import java.util.List;
/**
@@ -47,34 +45,9 @@ public interface IGroupedItemStorage {
void remove(@Nonnull ItemStack stack);
/**
* Gets an item from the network.
*
* @param stack the stack to find
* @param flags the flags to compare on, see {@link CompareUtils}
* @return null if no item is found, or the stack, do NOT modify
* @return the list behind this grouped storage
*/
@Nullable
ItemStack get(@Nonnull ItemStack stack, int flags);
/**
* Gets an item from the network by hash, see {@link refinedstorage.api.network.NetworkUtils#getItemStackHashCode(ItemStack)}.
*
* @return null if no item is found matching the hash, or the stack, do NOT modify
*/
@Nullable
ItemStack get(int hash);
/**
* Copies a grouped item storage.
*
* @return the storage
*/
IGroupedItemStorage copy();
/**
* @return all items in this storage network
*/
Collection<ItemStack> getStacks();
IItemStackList getList();
/**
* @return the item storages connected to this network

View File

@@ -0,0 +1,27 @@
package refinedstorage.api.util;
import net.minecraft.item.ItemStack;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.Collection;
public interface IItemStackList {
void add(ItemStack stack);
boolean remove(@Nonnull ItemStack stack, boolean removeIfReachedZero);
@Nullable
ItemStack get(@Nonnull ItemStack stack, int flags);
@Nullable
ItemStack get(int hash);
void clear();
@Nonnull
Collection<ItemStack> getStacks();
@Nonnull
IItemStackList copy();
}

View File

@@ -4,9 +4,11 @@ import refinedstorage.api.IAPI;
import refinedstorage.api.autocrafting.craftingmonitor.ICraftingMonitorElementRegistry;
import refinedstorage.api.autocrafting.registry.ICraftingTaskRegistry;
import refinedstorage.api.solderer.ISoldererRegistry;
import refinedstorage.api.util.IItemStackList;
import refinedstorage.apiimpl.autocrafting.craftingmonitor.CraftingMonitorElementRegistry;
import refinedstorage.apiimpl.autocrafting.registry.CraftingTaskRegistry;
import refinedstorage.apiimpl.solderer.SoldererRegistry;
import refinedstorage.apiimpl.util.ItemStackList;
import javax.annotation.Nonnull;
@@ -34,4 +36,10 @@ public class API implements IAPI {
public ICraftingMonitorElementRegistry getCraftingMonitorElementRegistry() {
return craftingMonitorElementRegistry;
}
@Nonnull
@Override
public IItemStackList createItemStackList() {
return new ItemStackList();
}
}

View File

@@ -13,7 +13,7 @@ import refinedstorage.api.autocrafting.task.IProcessable;
import refinedstorage.api.network.INetworkMaster;
import refinedstorage.api.network.NetworkUtils;
import refinedstorage.api.storage.CompareUtils;
import refinedstorage.api.storage.item.IGroupedItemStorage;
import refinedstorage.api.util.IItemStackList;
import refinedstorage.apiimpl.autocrafting.craftingmonitor.CraftingMonitorElementRoot;
import refinedstorage.apiimpl.autocrafting.craftingmonitor.CraftingMonitorElementToTake;
@@ -41,22 +41,24 @@ public class CraftingTaskNormal implements ICraftingTask {
}
public void calculate() {
IItemStackList list = network.getItemStorage().getList().copy();
int newQuantity = quantity;
while (newQuantity > 0) {
calculate(network.getItemStorage().copy(), pattern, true);
calculate(list, pattern, true);
newQuantity -= requested == null ? newQuantity : pattern.getQuantityPerRequest(requested);
}
}
private void calculate(IGroupedItemStorage storage, ICraftingPattern pattern, boolean basePattern) {
private void calculate(IItemStackList list, ICraftingPattern pattern, boolean basePattern) {
if (pattern.isProcessing()) {
toProcess.add(new Processable(pattern));
}
for (ItemStack input : pattern.getInputs()) {
ItemStack inputInNetwork = storage.get(input, CompareUtils.COMPARE_DAMAGE | CompareUtils.COMPARE_NBT);
ItemStack inputInNetwork = list.get(input, CompareUtils.COMPARE_DAMAGE | CompareUtils.COMPARE_NBT);
if (inputInNetwork == null || inputInNetwork.stackSize == 0) {
if (getExtrasFor(input) != null) {
@@ -69,7 +71,7 @@ public class CraftingTaskNormal implements ICraftingTask {
addToCraft(output);
}
calculate(storage, inputPattern, false);
calculate(list, inputPattern, false);
} else {
addMissing(input);
}
@@ -79,7 +81,7 @@ public class CraftingTaskNormal implements ICraftingTask {
toTake.push(input);
}
storage.remove(input);
list.remove(input, true);
}
}

View File

@@ -22,7 +22,7 @@ public class ItemGridHandler implements IItemGridHandler {
@Override
public void onExtract(int hash, int flags, EntityPlayerMP player) {
ItemStack item = network.getItemStorage().get(hash);
ItemStack item = network.getItemStorage().getList().get(hash);
if (item == null) {
return;
@@ -120,7 +120,7 @@ public class ItemGridHandler implements IItemGridHandler {
@Override
public void onCraftingPreviewRequested(EntityPlayerMP player, int hash, int quantity) {
ItemStack stack = network.getItemStorage().get(hash);
ItemStack stack = network.getItemStorage().getList().get(hash);
if (stack != null) {
CraftingTaskNormal task = new CraftingTaskNormal(network, stack, NetworkUtils.getPattern(network, stack), quantity);

View File

@@ -1,27 +1,23 @@
package refinedstorage.apiimpl.storage.item;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Multimap;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import refinedstorage.api.RefinedStorageAPI;
import refinedstorage.api.autocrafting.ICraftingPattern;
import refinedstorage.api.network.INetworkMaster;
import refinedstorage.api.network.NetworkUtils;
import refinedstorage.api.storage.CompareUtils;
import refinedstorage.api.storage.item.IGroupedItemStorage;
import refinedstorage.api.storage.item.IItemStorage;
import refinedstorage.api.storage.item.IItemStorageProvider;
import refinedstorage.api.util.IItemStackList;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
public class GroupedItemStorage implements IGroupedItemStorage {
private INetworkMaster network;
private List<IItemStorage> storages = new ArrayList<>();
private Multimap<Item, ItemStack> stacks = ArrayListMultimap.create();
private IItemStackList list = RefinedStorageAPI.instance().createItemStackList();
public GroupedItemStorage(INetworkMaster network) {
this.network = network;
@@ -35,7 +31,7 @@ public class GroupedItemStorage implements IGroupedItemStorage {
.filter(node -> node.canUpdate() && node instanceof IItemStorageProvider)
.forEach(node -> ((IItemStorageProvider) node).addItemStorages(storages));
stacks.clear();
list.clear();
for (IItemStorage storage : storages) {
for (ItemStack stack : storage.getItems()) {
@@ -56,19 +52,7 @@ public class GroupedItemStorage implements IGroupedItemStorage {
@Override
public void add(@Nonnull ItemStack stack, boolean rebuilding) {
for (ItemStack otherStack : stacks.get(stack.getItem())) {
if (CompareUtils.compareStackNoQuantity(otherStack, stack)) {
otherStack.stackSize += stack.stackSize;
if (!rebuilding) {
network.sendItemStorageDeltaToClient(stack, stack.stackSize);
}
return;
}
}
stacks.put(stack.getItem(), stack.copy());
list.add(stack);
if (!rebuilding) {
network.sendItemStorageDeltaToClient(stack, stack.stackSize);
@@ -77,63 +61,14 @@ public class GroupedItemStorage implements IGroupedItemStorage {
@Override
public void remove(@Nonnull ItemStack stack) {
for (ItemStack otherStack : stacks.get(stack.getItem())) {
if (CompareUtils.compareStackNoQuantity(otherStack, stack)) {
otherStack.stackSize -= stack.stackSize;
if (otherStack.stackSize == 0) {
if (!NetworkUtils.hasPattern(network, stack)) {
stacks.remove(otherStack.getItem(), otherStack);
}
}
network.sendItemStorageDeltaToClient(stack, -stack.stackSize);
return;
}
if (list.remove(stack, !NetworkUtils.hasPattern(network, stack))) {
network.sendItemStorageDeltaToClient(stack, -stack.stackSize);
}
}
@Override
@Nullable
public ItemStack get(@Nonnull ItemStack stack, int flags) {
for (ItemStack otherStack : stacks.get(stack.getItem())) {
if (CompareUtils.compareStack(otherStack, stack, flags)) {
return otherStack;
}
}
return null;
}
@Override
@Nullable
public ItemStack get(int hash) {
for (ItemStack stack : this.stacks.values()) {
if (NetworkUtils.getItemStackHashCode(stack) == hash) {
return stack;
}
}
return null;
}
@Override
public IGroupedItemStorage copy() {
GroupedItemStorage copy = new GroupedItemStorage(network);
copy.storages = storages;
for (ItemStack stack : stacks.values()) {
copy.stacks.put(stack.getItem(), stack.copy());
}
return copy;
}
@Override
public Collection<ItemStack> getStacks() {
return stacks.values();
public IItemStackList getList() {
return list;
}
@Override

View File

@@ -0,0 +1,90 @@
package refinedstorage.apiimpl.util;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Multimap;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import refinedstorage.api.network.NetworkUtils;
import refinedstorage.api.storage.CompareUtils;
import refinedstorage.api.util.IItemStackList;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.Collection;
public class ItemStackList implements IItemStackList {
private Multimap<Item, ItemStack> stacks = ArrayListMultimap.create();
public void add(ItemStack stack) {
for (ItemStack otherStack : stacks.get(stack.getItem())) {
if (CompareUtils.compareStackNoQuantity(otherStack, stack)) {
otherStack.stackSize += stack.stackSize;
return;
}
}
stacks.put(stack.getItem(), stack.copy());
}
public boolean remove(@Nonnull ItemStack stack, boolean removeIfReachedZero) {
for (ItemStack otherStack : stacks.get(stack.getItem())) {
if (CompareUtils.compareStackNoQuantity(otherStack, stack)) {
otherStack.stackSize -= stack.stackSize;
if (otherStack.stackSize == 0 && removeIfReachedZero) {
stacks.remove(otherStack.getItem(), otherStack);
}
return true;
}
}
return false;
}
@Nullable
public ItemStack get(@Nonnull ItemStack stack, int flags) {
for (ItemStack otherStack : stacks.get(stack.getItem())) {
if (CompareUtils.compareStack(otherStack, stack, flags)) {
return otherStack;
}
}
return null;
}
@Nullable
public ItemStack get(int hash) {
for (ItemStack stack : this.stacks.values()) {
if (NetworkUtils.getItemStackHashCode(stack) == hash) {
return stack;
}
}
return null;
}
@Override
public void clear() {
stacks.clear();
}
@Nonnull
@Override
public Collection<ItemStack> getStacks() {
return stacks.values();
}
@Override
@Nonnull
public IItemStackList copy() {
ItemStackList list = new ItemStackList();
for (ItemStack stack : stacks.values()) {
list.add(stack.copy());
}
return list;
}
}

View File

@@ -35,9 +35,9 @@ public class MessageGridItemUpdate implements IMessage, IMessageHandler<MessageG
@Override
public void toBytes(ByteBuf buf) {
buf.writeInt(network.getItemStorage().getStacks().size());
buf.writeInt(network.getItemStorage().getList().getStacks().size());
for (ItemStack stack : network.getItemStorage().getStacks()) {
for (ItemStack stack : network.getItemStorage().getList().getStacks()) {
NetworkUtils.writeItemStack(buf, network, stack);
}
}

View File

@@ -427,7 +427,7 @@ public class TileController extends TileBase implements INetworkMaster, IEnergyR
int score = 0;
for (ItemStack input : patterns.get(i).getInputs()) {
ItemStack stored = itemStorage.get(input, CompareUtils.COMPARE_DAMAGE | CompareUtils.COMPARE_NBT);
ItemStack stored = itemStorage.getList().get(input, CompareUtils.COMPARE_DAMAGE | CompareUtils.COMPARE_NBT);
score += stored != null ? stored.stackSize : 0;
}

View File

@@ -128,7 +128,7 @@ public class TileDetector extends TileNode implements IComparable, IType {
powered = found;
} else {
ItemStack stack = network.getItemStorage().get(slot, compare);
ItemStack stack = network.getItemStorage().getList().get(slot, compare);
powered = isPowered(stack == null ? null : stack.stackSize);
}

View File

@@ -261,7 +261,7 @@ public class TileDiskManipulator extends TileNode implements IComparable, IFilte
if (IFilterable.isEmpty(itemFilters)) {
ItemStack toExtract = null;
ArrayList<ItemStack> networkItems = new ArrayList<>(network.getItemStorage().getStacks());
ArrayList<ItemStack> networkItems = new ArrayList<>(network.getItemStorage().getList().getStacks());
int j = 0;