Fix separation between crafting and non-crafting stacks in the Grid

This commit is contained in:
raoulvdberge
2019-11-01 22:46:45 +01:00
parent 68590425c2
commit 3f8556e8b0
21 changed files with 167 additions and 20 deletions

View File

@@ -66,6 +66,16 @@ public interface IStackList<T> {
@Nullable
T get(@Nonnull T stack, int flags);
/**
* Returns a stack entry.
*
* @param stack the stack to search for
* @param flags the flags to compare on, see {@link IComparer}
* @return the stack entry, or null if no stack entry was found
*/
@Nullable
StackListEntry<T> getEntry(@Nonnull T stack, int flags);
/**
* Returns a stack.
*

View File

@@ -96,6 +96,20 @@ public class FluidStackList implements IStackList<FluidStack> {
return null;
}
@Nullable
@Override
public StackListEntry<FluidStack> getEntry(@Nonnull FluidStack stack, int flags) {
for (StackListEntry<FluidStack> entry : stacks.get(stack.getFluid())) {
FluidStack otherStack = entry.getStack();
if (API.instance().getComparer().isEqual(otherStack, stack, flags)) {
return entry;
}
}
return null;
}
@Override
@Nullable
public FluidStack get(UUID id) {

View File

@@ -94,6 +94,20 @@ public class ItemStackList implements IStackList<ItemStack> {
return null;
}
@Nullable
@Override
public StackListEntry<ItemStack> getEntry(@Nonnull ItemStack stack, int flags) {
for (StackListEntry<ItemStack> entry : stacks.get(stack.getItem())) {
ItemStack otherStack = entry.getStack();
if (API.instance().getComparer().isEqual(otherStack, stack, flags)) {
return entry;
}
}
return null;
}
@Override
@Nullable
public ItemStack get(UUID id) {

View File

@@ -1,6 +1,8 @@
package com.raoulvdberge.refinedstorage.network.grid;
import com.raoulvdberge.refinedstorage.api.network.INetwork;
import com.raoulvdberge.refinedstorage.api.util.IComparer;
import com.raoulvdberge.refinedstorage.api.util.StackListEntry;
import com.raoulvdberge.refinedstorage.api.util.StackListResult;
import com.raoulvdberge.refinedstorage.screen.BaseScreen;
import com.raoulvdberge.refinedstorage.screen.grid.GridScreen;
@@ -52,7 +54,9 @@ public class GridFluidDeltaMessage {
for (StackListResult<FluidStack> delta : message.deltas) {
buf.writeInt(delta.getChange());
StackUtils.writeFluidGridStack(buf, delta.getStack(), delta.getId(), false, message.network, message.network.getFluidStorageTracker().get(delta.getStack()));
StackListEntry<FluidStack> craftingEntry = message.network.getFluidStorageCache().getCraftablesList().getEntry(delta.getStack(), IComparer.COMPARE_NBT);
StackUtils.writeFluidGridStack(buf, delta.getStack(), delta.getId(), craftingEntry != null ? craftingEntry.getId() : null, false, message.network.getFluidStorageTracker().get(delta.getStack()));
}
}

View File

@@ -1,6 +1,7 @@
package com.raoulvdberge.refinedstorage.network.grid;
import com.raoulvdberge.refinedstorage.api.network.INetwork;
import com.raoulvdberge.refinedstorage.api.util.IComparer;
import com.raoulvdberge.refinedstorage.api.util.StackListEntry;
import com.raoulvdberge.refinedstorage.screen.BaseScreen;
import com.raoulvdberge.refinedstorage.screen.grid.GridScreen;
@@ -53,11 +54,15 @@ public class GridFluidUpdateMessage {
buf.writeInt(size);
for (StackListEntry<FluidStack> stack : message.network.getFluidStorageCache().getList().getStacks()) {
StackUtils.writeFluidGridStack(buf, stack.getStack(), stack.getId(), false, message.network, message.network.getFluidStorageTracker().get(stack.getStack()));
StackListEntry<FluidStack> craftingEntry = message.network.getFluidStorageCache().getCraftablesList().getEntry(stack.getStack(), IComparer.COMPARE_NBT);
StackUtils.writeFluidGridStack(buf, stack.getStack(), stack.getId(), craftingEntry != null ? craftingEntry.getId() : null, false, message.network.getFluidStorageTracker().get(stack.getStack()));
}
for (StackListEntry<FluidStack> stack : message.network.getFluidStorageCache().getCraftablesList().getStacks()) {
StackUtils.writeFluidGridStack(buf, stack.getStack(), stack.getId(), true, message.network, message.network.getFluidStorageTracker().get(stack.getStack()));
StackListEntry<FluidStack> regularEntry = message.network.getFluidStorageCache().getList().getEntry(stack.getStack(), IComparer.COMPARE_NBT);
StackUtils.writeFluidGridStack(buf, stack.getStack(), stack.getId(), regularEntry != null ? regularEntry.getId() : null, true, message.network.getFluidStorageTracker().get(stack.getStack()));
}
}

View File

@@ -1,6 +1,8 @@
package com.raoulvdberge.refinedstorage.network.grid;
import com.raoulvdberge.refinedstorage.api.network.INetwork;
import com.raoulvdberge.refinedstorage.api.util.IComparer;
import com.raoulvdberge.refinedstorage.api.util.StackListEntry;
import com.raoulvdberge.refinedstorage.api.util.StackListResult;
import com.raoulvdberge.refinedstorage.screen.BaseScreen;
import com.raoulvdberge.refinedstorage.screen.grid.GridScreen;
@@ -52,7 +54,9 @@ public class GridItemDeltaMessage {
for (StackListResult<ItemStack> delta : message.deltas) {
buf.writeInt(delta.getChange());
StackUtils.writeItemGridStack(buf, delta.getStack(), delta.getId(), false, message.network, message.network.getItemStorageTracker().get(delta.getStack()));
StackListEntry<ItemStack> craftingEntry = message.network.getItemStorageCache().getCraftablesList().getEntry(delta.getStack(), IComparer.COMPARE_NBT);
StackUtils.writeItemGridStack(buf, delta.getStack(), delta.getId(), craftingEntry != null ? craftingEntry.getId() : null, false, message.network.getItemStorageTracker().get(delta.getStack()));
}
}

View File

@@ -1,6 +1,7 @@
package com.raoulvdberge.refinedstorage.network.grid;
import com.raoulvdberge.refinedstorage.api.network.INetwork;
import com.raoulvdberge.refinedstorage.api.util.IComparer;
import com.raoulvdberge.refinedstorage.api.util.StackListEntry;
import com.raoulvdberge.refinedstorage.screen.BaseScreen;
import com.raoulvdberge.refinedstorage.screen.grid.GridScreen;
@@ -53,11 +54,15 @@ public class GridItemUpdateMessage {
buf.writeInt(size);
for (StackListEntry<ItemStack> stack : message.network.getItemStorageCache().getList().getStacks()) {
StackUtils.writeItemGridStack(buf, stack.getStack(), stack.getId(), false, message.network, message.network.getItemStorageTracker().get(stack.getStack()));
StackListEntry<ItemStack> craftingEntry = message.network.getItemStorageCache().getCraftablesList().getEntry(stack.getStack(), IComparer.COMPARE_NBT);
StackUtils.writeItemGridStack(buf, stack.getStack(), stack.getId(), craftingEntry != null ? craftingEntry.getId() : null, false, message.network.getItemStorageTracker().get(stack.getStack()));
}
for (StackListEntry<ItemStack> stack : message.network.getItemStorageCache().getCraftablesList().getStacks()) {
StackUtils.writeItemGridStack(buf, stack.getStack(), stack.getId(), true, message.network, message.network.getItemStorageTracker().get(stack.getStack()));
StackListEntry<ItemStack> regularEntry = message.network.getItemStorageCache().getList().getEntry(stack.getStack(), IComparer.COMPARE_NBT);
StackUtils.writeItemGridStack(buf, stack.getStack(), stack.getId(), regularEntry != null ? regularEntry.getId() : null, true, message.network.getItemStorageTracker().get(stack.getStack()));
}
}

View File

@@ -50,7 +50,7 @@ public class PortableGridFluidDeltaMessage {
for (StackListResult<FluidStack> delta : message.deltas) {
buf.writeInt(delta.getChange());
StackUtils.writeFluidGridStack(buf, delta.getStack(), delta.getId(), false, null, message.portableGrid.getFluidStorageTracker().get(delta.getStack()));
StackUtils.writeFluidGridStack(buf, delta.getStack(), delta.getId(), null, false, message.portableGrid.getFluidStorageTracker().get(delta.getStack()));
}
}

View File

@@ -46,7 +46,7 @@ public class PortableGridFluidUpdateMessage {
buf.writeInt(size);
for (StackListEntry<FluidStack> stack : message.portableGrid.getFluidCache().getList().getStacks()) {
StackUtils.writeFluidGridStack(buf, stack.getStack(), stack.getId(), false, null, message.portableGrid.getFluidStorageTracker().get(stack.getStack()));
StackUtils.writeFluidGridStack(buf, stack.getStack(), stack.getId(), null, false, message.portableGrid.getFluidStorageTracker().get(stack.getStack()));
}
}

View File

@@ -52,7 +52,7 @@ public class PortableGridItemDeltaMessage {
for (StackListResult<ItemStack> delta : message.deltas) {
buf.writeInt(delta.getChange());
StackUtils.writeItemGridStack(buf, delta.getStack(), delta.getId(), false, null, message.portableGrid.getItemStorageTracker().get(delta.getStack()));
StackUtils.writeItemGridStack(buf, delta.getStack(), delta.getId(), null, false, message.portableGrid.getItemStorageTracker().get(delta.getStack()));
}
}

View File

@@ -46,7 +46,7 @@ public class PortableGridItemUpdateMessage {
buf.writeInt(size);
for (StackListEntry<ItemStack> stack : message.portableGrid.getItemCache().getList().getStacks()) {
StackUtils.writeItemGridStack(buf, stack.getStack(), stack.getId(), false, null, message.portableGrid.getItemStorageTracker().get(stack.getStack()));
StackUtils.writeItemGridStack(buf, stack.getStack(), stack.getId(), null, false, message.portableGrid.getItemStorageTracker().get(stack.getStack()));
}
}

View File

@@ -455,8 +455,10 @@ public class GridScreen extends BaseScreen<GridContainer> implements IScreenInfo
IGridStack stack = view.getStacks().get(slotNumber);
if (isPulling) {
if (stack.isCraftable() && view.canCraft()) {
if (view.canCraft() && stack.isCraftable()) {
minecraft.displayGuiScreen(new CraftingSettingsScreen(this, playerInventory.player, stack));
} else if (view.canCraft() && !stack.isCraftable() && stack.getOtherId() != null && hasShiftDown() && hasControlDown()) {
minecraft.displayGuiScreen(new CraftingSettingsScreen(this, playerInventory.player, view.get(stack.getOtherId())));
} else if (grid.getGridType() == GridType.FLUID && held.isEmpty()) {
RS.NETWORK_HANDLER.sendToServer(new GridFluidPullMessage(view.getStacks().get(slotNumber).getId(), hasShiftDown()));
} else if (grid.getGridType() != GridType.FLUID) {

View File

@@ -14,6 +14,7 @@ public class CraftableGridFilter implements Predicate<IGridStack> {
@Override
public boolean test(IGridStack stack) {
// TODO Make working with fluids.
return stack instanceof ItemGridStack && stack.isCraftable() == craftable;
}
}

View File

@@ -20,6 +20,8 @@ public class FluidGridStack implements IGridStack {
private Logger logger = LogManager.getLogger(getClass());
private UUID id;
@Nullable
private UUID otherId;
private FluidStack stack;
@Nullable
private StorageTrackerEntry entry;
@@ -31,8 +33,9 @@ public class FluidGridStack implements IGridStack {
private String cachedModId;
private String cachedModName;
public FluidGridStack(UUID id, FluidStack stack, @Nullable StorageTrackerEntry entry, boolean craftable) {
public FluidGridStack(UUID id, @Nullable UUID otherId, FluidStack stack, @Nullable StorageTrackerEntry entry, boolean craftable) {
this.id = id;
this.otherId = otherId;
this.stack = stack;
this.entry = entry;
this.craftable = craftable;
@@ -52,6 +55,17 @@ public class FluidGridStack implements IGridStack {
return id;
}
@Nullable
@Override
public UUID getOtherId() {
return otherId;
}
@Override
public void updateOtherId(@Nullable UUID otherId) {
this.otherId = otherId;
}
@Override
public String getName() {
if (cachedName == null) {

View File

@@ -10,6 +10,11 @@ import java.util.UUID;
public interface IGridStack {
UUID getId();
@Nullable
UUID getOtherId();
void updateOtherId(@Nullable UUID otherId);
String getName();
String getModId();

View File

@@ -24,14 +24,15 @@ public class ItemGridStack implements IGridStack {
private Logger logger = LogManager.getLogger(getClass());
private UUID id;
@Nullable
private UUID otherId;
private ItemStack stack;
private String cachedName;
private boolean craftable;
private String[] oreIds = null;
@Nullable
private StorageTrackerEntry entry;
private Set<String> cachedTags;
private String cachedName;
private String cachedModId;
private String cachedModName;
private String cachedTooltip;
@@ -40,8 +41,9 @@ public class ItemGridStack implements IGridStack {
this.stack = stack;
}
public ItemGridStack(UUID id, ItemStack stack, boolean craftable, StorageTrackerEntry entry) {
public ItemGridStack(UUID id, @Nullable UUID otherId, ItemStack stack, boolean craftable, StorageTrackerEntry entry) {
this.id = id;
this.otherId = otherId;
this.stack = stack;
this.craftable = craftable;
this.entry = entry;
@@ -68,6 +70,17 @@ public class ItemGridStack implements IGridStack {
return id;
}
@Nullable
@Override
public UUID getOtherId() {
return otherId;
}
@Override
public void updateOtherId(@Nullable UUID otherId) {
this.otherId = otherId;
}
@Override
public String getName() {
if (cachedName == null) {

View File

@@ -7,6 +7,7 @@ import com.raoulvdberge.refinedstorage.screen.grid.sorting.IGridSorter;
import com.raoulvdberge.refinedstorage.screen.grid.sorting.SortingDirection;
import com.raoulvdberge.refinedstorage.screen.grid.stack.IGridStack;
import javax.annotation.Nullable;
import java.util.*;
import java.util.function.Predicate;
@@ -31,6 +32,12 @@ public abstract class BaseGridView implements IGridView {
return stacks;
}
@Nullable
@Override
public IGridStack get(UUID id) {
return map.get(id);
}
@Override
public void sort() {
List<IGridStack> stacks = new ArrayList<>();
@@ -51,6 +58,15 @@ public abstract class BaseGridView implements IGridView {
while (it.hasNext()) {
IGridStack stack = it.next();
// TODO Make working with grid sorting mode.
if (stack.isCraftable() &&
stack.getOtherId() != null &&
map.containsKey(stack.getOtherId())) {
it.remove();
continue;
}
for (Predicate<IGridStack> filter : filters) {
if (!filter.test(stack)) {
it.remove();

View File

@@ -27,6 +27,14 @@ public class FluidGridView extends BaseGridView {
return;
}
// Update the other id reference if needed.
// Taking a stack out - and then re-inserting it - gives the new stack a new ID
// With that new id, the reference for the crafting stack would be outdated.
if (!stack.isCraftable() &&
stack.getOtherId() != null) {
map.get(stack.getOtherId()).updateOtherId(stack.getId());
}
FluidGridStack existing = (FluidGridStack) map.get(stack.getId());
if (existing == null) {

View File

@@ -2,11 +2,16 @@ package com.raoulvdberge.refinedstorage.screen.grid.view;
import com.raoulvdberge.refinedstorage.screen.grid.stack.IGridStack;
import javax.annotation.Nullable;
import java.util.List;
import java.util.UUID;
public interface IGridView {
List<IGridStack> getStacks();
@Nullable
IGridStack get(UUID id);
void setStacks(List<IGridStack> stacks);
void postChange(IGridStack stack, int delta);

View File

@@ -27,6 +27,14 @@ public class ItemGridView extends BaseGridView {
return;
}
// Update the other id reference if needed.
// Taking a stack out - and then re-inserting it - gives the new stack a new ID
// With that new id, the reference for the crafting stack would be outdated.
if (!stack.isCraftable() &&
stack.getOtherId() != null) {
map.get(stack.getOtherId()).updateOtherId(stack.getId());
}
ItemGridStack existing = (ItemGridStack) map.get(stack.getId());
if (existing == null) {

View File

@@ -1,6 +1,5 @@
package com.raoulvdberge.refinedstorage.util;
import com.raoulvdberge.refinedstorage.api.network.INetwork;
import com.raoulvdberge.refinedstorage.api.storage.disk.IStorageDisk;
import com.raoulvdberge.refinedstorage.api.storage.disk.IStorageDiskProvider;
import com.raoulvdberge.refinedstorage.api.storage.tracker.StorageTrackerEntry;
@@ -82,12 +81,17 @@ public final class StackUtils {
}
}
public static void writeItemGridStack(PacketBuffer buf, ItemStack stack, UUID id, boolean craftable, @Nullable INetwork network, @Nullable StorageTrackerEntry entry) {
public static void writeItemGridStack(PacketBuffer buf, ItemStack stack, UUID id, @Nullable UUID otherId, boolean craftable, @Nullable StorageTrackerEntry entry) {
writeItemStack(buf, stack);
buf.writeBoolean(craftable);
buf.writeUniqueId(id);
buf.writeBoolean(otherId != null);
if (otherId != null) {
buf.writeUniqueId(otherId);
}
if (entry == null) {
buf.writeBoolean(false);
} else {
@@ -104,20 +108,30 @@ public final class StackUtils {
boolean craftable = buf.readBoolean();
UUID id = buf.readUniqueId();
UUID otherId = null;
if (buf.readBoolean()) {
otherId = buf.readUniqueId();
}
StorageTrackerEntry entry = null;
if (buf.readBoolean()) {
entry = new StorageTrackerEntry(buf.readLong(), PacketBufferUtils.readString(buf));
}
return new ItemGridStack(id, stack, craftable, entry);
return new ItemGridStack(id, otherId, stack, craftable, entry);
}
public static void writeFluidGridStack(PacketBuffer buf, FluidStack stack, UUID id, boolean craftable, @Nullable INetwork network, @Nullable StorageTrackerEntry entry) {
public static void writeFluidGridStack(PacketBuffer buf, FluidStack stack, UUID id, @Nullable UUID otherId, boolean craftable, @Nullable StorageTrackerEntry entry) {
stack.writeToPacket(buf);
buf.writeBoolean(craftable);
buf.writeUniqueId(id);
buf.writeBoolean(otherId != null);
if (otherId != null) {
buf.writeUniqueId(otherId);
}
if (entry == null) {
buf.writeBoolean(false);
} else {
@@ -133,12 +147,17 @@ public final class StackUtils {
boolean craftable = buf.readBoolean();
UUID id = buf.readUniqueId();
UUID otherId = null;
if (buf.readBoolean()) {
otherId = buf.readUniqueId();
}
StorageTrackerEntry entry = null;
if (buf.readBoolean()) {
entry = new StorageTrackerEntry(buf.readLong(), PacketBufferUtils.readString(buf));
}
return new FluidGridStack(id, stack, entry, craftable);
return new FluidGridStack(id, otherId, stack, entry, craftable);
}
@SuppressWarnings("unchecked")