From d4242026e41fa8c9420c80b496942d922718bfd3 Mon Sep 17 00:00:00 2001 From: screret <68943070+screret@users.noreply.github.com> Date: Mon, 15 Apr 2024 18:46:11 +0300 Subject: [PATCH] fix network switch crash, data bank chaining (#1114) * fix network switch sometimes crashing when the GUI is opened * fix optical cables sometimes not functioning at all * fix research recipe ids being weird sometimes (e.g. when using GT material items as input) * fix advanced data access hatch GUI being offcenter * fix scanner & research station recipes having both inputs and outputs be the output (this was introduced in #1108) * fix data bank chaining --- .../recipe/FluidRecipeCapability.java | 2 +- .../recipe/ItemRecipeCapability.java | 43 +++++++++---------- .../capability/recipe/RecipeCapability.java | 2 +- .../trait/NotifiableComputationContainer.java | 41 +++++++++++++++--- .../part/DataAccessHatchMachine.java | 3 +- .../part/OpticalDataHatchMachine.java | 5 +-- .../data/recipe/builder/GTRecipeBuilder.java | 9 ++-- .../gtceu/integration/GTRecipeWidget.java | 8 ++-- .../gtceu/utils/GTStringUtils.java | 5 ++- 9 files changed, 74 insertions(+), 44 deletions(-) diff --git a/src/main/java/com/gregtechceu/gtceu/api/capability/recipe/FluidRecipeCapability.java b/src/main/java/com/gregtechceu/gtceu/api/capability/recipe/FluidRecipeCapability.java index 43c8ff5c28..fd766e7b4e 100644 --- a/src/main/java/com/gregtechceu/gtceu/api/capability/recipe/FluidRecipeCapability.java +++ b/src/main/java/com/gregtechceu/gtceu/api/capability/recipe/FluidRecipeCapability.java @@ -132,7 +132,7 @@ public boolean isRecipeSearchFilter() { } @Override - public @NotNull List createXEIContainerContents(List contents, GTRecipe recipe) { + public @NotNull List createXEIContainerContents(List contents, GTRecipe recipe, IO io) { return contents.stream().map(content -> content.content) .map(this::of) .map(FluidRecipeCapability::mapFluid) diff --git a/src/main/java/com/gregtechceu/gtceu/api/capability/recipe/ItemRecipeCapability.java b/src/main/java/com/gregtechceu/gtceu/api/capability/recipe/ItemRecipeCapability.java index c8619aac3f..cb73c1cb56 100644 --- a/src/main/java/com/gregtechceu/gtceu/api/capability/recipe/ItemRecipeCapability.java +++ b/src/main/java/com/gregtechceu/gtceu/api/capability/recipe/ItemRecipeCapability.java @@ -179,14 +179,14 @@ public boolean isRecipeSearchFilter() { } @Override - public @NotNull List createXEIContainerContents(List contents, GTRecipe recipe) { + public @NotNull List createXEIContainerContents(List contents, GTRecipe recipe, IO io) { var outputStacks = contents.stream().map(content -> content.content) .map(this::of) .map(ItemRecipeCapability::mapItem) .collect(Collectors.toList()); List, Integer>>, List>> scannerPossibilities = null; - if (recipe.recipeType.isScanner()) { + if (io == IO.OUT && recipe.recipeType.isScanner()) { scannerPossibilities = new ArrayList<>(); // Scanner Output replacing, used for cycling research outputs Pair researchData = null; @@ -268,27 +268,26 @@ public void applyWidgetInfo(@NotNull Widget widget, slot.setIngredientIO(io == IO.IN ? IngredientIO.INPUT : IngredientIO.OUTPUT); slot.setCanTakeItems(!isXEI); slot.setCanPutItems(false); - - // 1 over container size. - // If in a recipe viewer and a research slot can be added, add it. - if (isXEI && recipeType.isHasResearchSlot() && index == items.getSlots()) { - if (ConfigHolder.INSTANCE.machines.enableResearch) { - ResearchCondition condition = recipeHolder.conditions().stream().filter(ResearchCondition.class::isInstance).findAny().map(ResearchCondition.class::cast).orElse(null); - if (condition == null) { - return; - } - List dataItems = new ArrayList<>(); - for (ResearchData.ResearchEntry entry : condition.data) { - ItemStack dataStick = entry.getDataItem().copy(); - ResearchManager.writeResearchToNBT(dataStick.getOrCreateTag(), entry.getResearchId(), recipeType); - dataItems.add(dataStick); - } - CycleItemStackHandler handler = new CycleItemStackHandler(List.of(dataItems)); - slot.setHandlerSlot(handler, 0); - slot.setIngredientIO(IngredientIO.INPUT); - slot.setCanTakeItems(false); - slot.setCanPutItems(false); + } + // 1 over container size. + // If in a recipe viewer and a research slot can be added, add it. + if (isXEI && recipeType.isHasResearchSlot() && index == items.getSlots()) { + if (ConfigHolder.INSTANCE.machines.enableResearch) { + ResearchCondition condition = recipeHolder.conditions().stream().filter(ResearchCondition.class::isInstance).findAny().map(ResearchCondition.class::cast).orElse(null); + if (condition == null) { + return; + } + List dataItems = new ArrayList<>(); + for (ResearchData.ResearchEntry entry : condition.data) { + ItemStack dataStick = entry.getDataItem().copy(); + ResearchManager.writeResearchToNBT(dataStick.getOrCreateTag(), entry.getResearchId(), recipeType); + dataItems.add(dataStick); } + CycleItemStackHandler handler = new CycleItemStackHandler(List.of(dataItems)); + slot.setHandlerSlot(handler, 0); + slot.setIngredientIO(IngredientIO.INPUT); + slot.setCanTakeItems(false); + slot.setCanPutItems(false); } } } diff --git a/src/main/java/com/gregtechceu/gtceu/api/capability/recipe/RecipeCapability.java b/src/main/java/com/gregtechceu/gtceu/api/capability/recipe/RecipeCapability.java index dae5e21c8c..b4232f5cd0 100644 --- a/src/main/java/com/gregtechceu/gtceu/api/capability/recipe/RecipeCapability.java +++ b/src/main/java/com/gregtechceu/gtceu/api/capability/recipe/RecipeCapability.java @@ -117,7 +117,7 @@ public void addXEIInfo(WidgetGroup group, int xOffset, List contents, b } @NotNull - public List createXEIContainerContents(List contents, GTRecipe recipe) { + public List createXEIContainerContents(List contents, GTRecipe recipe, IO io) { return new ArrayList<>(); } diff --git a/src/main/java/com/gregtechceu/gtceu/api/machine/trait/NotifiableComputationContainer.java b/src/main/java/com/gregtechceu/gtceu/api/machine/trait/NotifiableComputationContainer.java index ebf075f938..bdf3f0a9f7 100644 --- a/src/main/java/com/gregtechceu/gtceu/api/machine/trait/NotifiableComputationContainer.java +++ b/src/main/java/com/gregtechceu/gtceu/api/machine/trait/NotifiableComputationContainer.java @@ -14,6 +14,7 @@ import com.gregtechceu.gtceu.api.machine.feature.multiblock.IMultiController; import com.gregtechceu.gtceu.api.machine.feature.multiblock.IMultiPart; import com.gregtechceu.gtceu.api.recipe.GTRecipe; +import com.gregtechceu.gtceu.common.blockentity.OpticalPipeBlockEntity; import com.gregtechceu.gtceu.utils.GTUtil; import lombok.Getter; import net.minecraft.core.Direction; @@ -79,7 +80,7 @@ public int requestCWUt(int cwut, boolean simulate, @NotNull Collection seen) { return 0; } for (IMultiController controller : part.getControllers()) { + if (!controller.isFormed()) { + continue; + } if (controller instanceof IOpticalComputationProvider provider) { return provider.getMaxCWUt(seen); } @@ -119,7 +123,7 @@ public int getMaxCWUt(@NotNull Collection seen) { } } else { // Ask the attached Transmitter hatch, if it exists - IOpticalComputationProvider provider = getComputationProvider(); + IOpticalComputationProvider provider = getOpticalNetProvider(); if (provider == null) return 0; return provider.getMaxCWUt(seen); } @@ -141,6 +145,9 @@ public boolean canBridge(@NotNull Collection seen) return false; } for (IMultiController controller : part.getControllers()) { + if (!controller.isFormed()) { + continue; + } if (controller instanceof IOpticalComputationProvider provider) { return provider.canBridge(seen); } @@ -158,7 +165,7 @@ public boolean canBridge(@NotNull Collection seen) } } else { // Ask the attached Transmitter hatch, if it exists - IOpticalComputationProvider provider = getComputationProvider(); + IOpticalComputationProvider provider = getOpticalNetProvider(); if (provider == null) return true; // nothing found, so don't report a problem, just pass quietly return provider.canBridge(seen); } @@ -169,7 +176,7 @@ public boolean canBridge(@NotNull Collection seen) @Override public List handleRecipeInner(IO io, GTRecipe recipe, List left, @Nullable String slotName, boolean simulate) { - IOpticalComputationProvider provider = getComputationProvider(); + IOpticalComputationProvider provider = getOpticalNetProvider(); if (provider == null) return left; int sum = left.stream().reduce(0, Integer::sum); @@ -225,20 +232,42 @@ public RecipeCapability getCapability() { @Nullable @Override public IOpticalComputationProvider getComputationProvider() { + if (this.handlerIO.support(IO.OUT)) { + return this; + } if (machine instanceof IOpticalComputationReceiver receiver) { return receiver.getComputationProvider(); } else if (machine instanceof IOpticalComputationProvider provider) { return provider; } else if (machine instanceof IRecipeCapabilityHolder recipeCapabilityHolder) { if (recipeCapabilityHolder.getCapabilitiesProxy().contains(IO.IN, CWURecipeCapability.CAP) && !recipeCapabilityHolder.getCapabilitiesProxy().get(IO.IN, CWURecipeCapability.CAP).isEmpty()) { - return (IOpticalComputationProvider) recipeCapabilityHolder.getCapabilitiesProxy().get(IO.IN, CWURecipeCapability.CAP).get(0); + var provider = (IOpticalComputationProvider) recipeCapabilityHolder.getCapabilitiesProxy().get(IO.IN, CWURecipeCapability.CAP).get(0); + if (provider != this) { + return provider; + } } } for (Direction direction : GTUtil.DIRECTIONS) { BlockEntity blockEntity = machine.getLevel().getBlockEntity(machine.getPos().relative(direction)); if (blockEntity == null) continue; - return blockEntity.getCapability(GTCapability.CAPABILITY_COMPUTATION_PROVIDER, direction.getOpposite()).orElse(null); + //noinspection DataFlowIssue can be null just fine. + IOpticalComputationProvider provider = blockEntity.getCapability(GTCapability.CAPABILITY_COMPUTATION_PROVIDER, direction.getOpposite()).orElse(null); + //noinspection ConstantValue can be null because above. + if (provider != null && provider != this) { + return provider; + } + } + return null; + } + + @Nullable + private IOpticalComputationProvider getOpticalNetProvider() { + for (Direction direction : GTUtil.DIRECTIONS) { + BlockEntity blockEntity = machine.getLevel().getBlockEntity(machine.getPos().relative(direction)); + if (blockEntity instanceof OpticalPipeBlockEntity) { + return blockEntity.getCapability(GTCapability.CAPABILITY_COMPUTATION_PROVIDER, direction.getOpposite()).orElse(null); + } } return null; } diff --git a/src/main/java/com/gregtechceu/gtceu/common/machine/multiblock/part/DataAccessHatchMachine.java b/src/main/java/com/gregtechceu/gtceu/common/machine/multiblock/part/DataAccessHatchMachine.java index ae37a8eec6..2e46caf5f7 100644 --- a/src/main/java/com/gregtechceu/gtceu/common/machine/multiblock/part/DataAccessHatchMachine.java +++ b/src/main/java/com/gregtechceu/gtceu/common/machine/multiblock/part/DataAccessHatchMachine.java @@ -84,13 +84,14 @@ public ItemStack insertItem(int slot, @NotNull ItemStack stack, boolean simulate @Override public Widget createUIWidget() { int rowSize = (int) Math.sqrt(getInventorySize()); + int xOffset = 18 * rowSize / 2; WidgetGroup group = new WidgetGroup(0, 0, 18 * rowSize, 18 * rowSize); for (int y = 0; y < rowSize; y++) { for (int x = 0; x < rowSize; x++) { int index = y * rowSize + x; group.addWidget(new SlotWidget(importItems, index, - rowSize * 9 + x * 18 - 27, y * 18, true, true) + rowSize * 9 + x * 18 - xOffset, y * 18, true, true) .setBackgroundTexture(GuiTextures.SLOT)); } } diff --git a/src/main/java/com/gregtechceu/gtceu/common/machine/multiblock/part/OpticalDataHatchMachine.java b/src/main/java/com/gregtechceu/gtceu/common/machine/multiblock/part/OpticalDataHatchMachine.java index 492258a094..a96aae70b2 100644 --- a/src/main/java/com/gregtechceu/gtceu/common/machine/multiblock/part/OpticalDataHatchMachine.java +++ b/src/main/java/com/gregtechceu/gtceu/common/machine/multiblock/part/OpticalDataHatchMachine.java @@ -51,14 +51,13 @@ public boolean isRecipeAvailable(@NotNull GTRecipe recipe, @NotNull Collection, Object> extraTable, Ta List contents = entry.getValue(); extraContents.put(IO.IN, cap, contents); - inputCapabilities.put(cap, cap.createXEIContainerContents(contents, recipe)); + inputCapabilities.put(cap, cap.createXEIContainerContents(contents, recipe, IO.IN)); } for (var entry : recipe.tickInputs.entrySet()) { RecipeCapability cap = entry.getKey(); List contents = entry.getValue(); extraContents.put(IO.IN, cap, contents); - inputCapabilities.put(cap, cap.createXEIContainerContents(contents, recipe)); + inputCapabilities.put(cap, cap.createXEIContainerContents(contents, recipe, IO.IN)); } for (var entry : inputCapabilities.entrySet()) { while (entry.getValue().size() < recipe.recipeType.getMaxInputs(entry.getKey())) entry.getValue().add(null); @@ -300,14 +300,14 @@ public void collectStorage(Table, Object> extraTable, Ta List contents = entry.getValue(); extraContents.put(IO.OUT, cap, contents); - outputCapabilities.put(cap, cap.createXEIContainerContents(contents, recipe)); + outputCapabilities.put(cap, cap.createXEIContainerContents(contents, recipe, IO.OUT)); } for (var entry : recipe.tickOutputs.entrySet()) { RecipeCapability cap = entry.getKey(); List contents = entry.getValue(); extraContents.put(IO.OUT, cap, contents); - outputCapabilities.put(cap, cap.createXEIContainerContents(contents, recipe)); + outputCapabilities.put(cap, cap.createXEIContainerContents(contents, recipe, IO.OUT)); } for (var entry : outputCapabilities.entrySet()) { while (entry.getValue().size() < recipe.recipeType.getMaxOutputs(entry.getKey())) entry.getValue().add(null); diff --git a/src/main/java/com/gregtechceu/gtceu/utils/GTStringUtils.java b/src/main/java/com/gregtechceu/gtceu/utils/GTStringUtils.java index 004be7bfdb..3255c52200 100644 --- a/src/main/java/com/gregtechceu/gtceu/utils/GTStringUtils.java +++ b/src/main/java/com/gregtechceu/gtceu/utils/GTStringUtils.java @@ -1,5 +1,7 @@ package com.gregtechceu.gtceu.utils; +import net.minecraft.core.registries.BuiltInRegistries; +import net.minecraft.resources.ResourceLocation; import net.minecraft.world.item.ItemStack; import org.jetbrains.annotations.NotNull; @@ -16,6 +18,7 @@ public class GTStringUtils { */ @NotNull public static String itemStackToString(@NotNull ItemStack stack) { - return stack.getCount() + "x" + stack.getItem().getDescriptionId(stack); + ResourceLocation itemId = BuiltInRegistries.ITEM.getKey(stack.getItem()); + return stack.getCount() + "x_" + itemId.getNamespace() + "_" + itemId.getPath(); } }