Skip to content

Commit

Permalink
fix network switch crash, data bank chaining (#1114)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
screret committed May 2, 2024
1 parent 6ae02f6 commit d424202
Show file tree
Hide file tree
Showing 9 changed files with 74 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ public boolean isRecipeSearchFilter() {
}

@Override
public @NotNull List<Object> createXEIContainerContents(List<Content> contents, GTRecipe recipe) {
public @NotNull List<Object> createXEIContainerContents(List<Content> contents, GTRecipe recipe, IO io) {
return contents.stream().map(content -> content.content)
.map(this::of)
.map(FluidRecipeCapability::mapFluid)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,14 +179,14 @@ public boolean isRecipeSearchFilter() {
}

@Override
public @NotNull List<Object> createXEIContainerContents(List<Content> contents, GTRecipe recipe) {
public @NotNull List<Object> createXEIContainerContents(List<Content> contents, GTRecipe recipe, IO io) {
var outputStacks = contents.stream().map(content -> content.content)
.map(this::of)
.map(ItemRecipeCapability::mapItem)
.collect(Collectors.toList());

List<Either<List<Pair<TagKey<Item>, Integer>>, List<ItemStack>>> 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<GTRecipeType, String> researchData = null;
Expand Down Expand Up @@ -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<ItemStack> 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<ItemStack> 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);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public void addXEIInfo(WidgetGroup group, int xOffset, List<Content> contents, b
}

@NotNull
public List<Object> createXEIContainerContents(List<Content> contents, GTRecipe recipe) {
public List<Object> createXEIContainerContents(List<Content> contents, GTRecipe recipe, IO io) {
return new ArrayList<>();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -79,7 +80,7 @@ public int requestCWUt(int cwut, boolean simulate, @NotNull Collection<IOpticalC
}
} else {
// Ask the attached Transmitter hatch, if it exists
IOpticalComputationProvider provider = getComputationProvider();
IOpticalComputationProvider provider = getOpticalNetProvider();
if (provider == null) return 0;
return provider.requestCWUt(cwut, simulate, seen);
}
Expand All @@ -102,6 +103,9 @@ public int getMaxCWUt(@NotNull Collection<IOpticalComputationProvider> seen) {
return 0;
}
for (IMultiController controller : part.getControllers()) {
if (!controller.isFormed()) {
continue;
}
if (controller instanceof IOpticalComputationProvider provider) {
return provider.getMaxCWUt(seen);
}
Expand All @@ -119,7 +123,7 @@ public int getMaxCWUt(@NotNull Collection<IOpticalComputationProvider> 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);
}
Expand All @@ -141,6 +145,9 @@ public boolean canBridge(@NotNull Collection<IOpticalComputationProvider> seen)
return false;
}
for (IMultiController controller : part.getControllers()) {
if (!controller.isFormed()) {
continue;
}
if (controller instanceof IOpticalComputationProvider provider) {
return provider.canBridge(seen);
}
Expand All @@ -158,7 +165,7 @@ public boolean canBridge(@NotNull Collection<IOpticalComputationProvider> 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);
}
Expand All @@ -169,7 +176,7 @@ public boolean canBridge(@NotNull Collection<IOpticalComputationProvider> seen)

@Override
public List<Integer> handleRecipeInner(IO io, GTRecipe recipe, List<Integer> 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);
Expand Down Expand Up @@ -225,20 +232,42 @@ public RecipeCapability<Integer> 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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,13 @@ public boolean isRecipeAvailable(@NotNull GTRecipe recipe, @NotNull Collection<I
if (part instanceof IDataAccessHatch hatch && PartAbility.DATA_ACCESS.isApplicable(block)) {
dataAccesses.add(hatch);
}
if (part instanceof IDataAccessHatch hatch && PartAbility.COMPUTATION_DATA_TRANSMISSION.isApplicable(block)) {
if (part instanceof IDataAccessHatch hatch && PartAbility.OPTICAL_DATA_RECEPTION.isApplicable(block)) {
transmitters.add(hatch);
}
}

return isRecipeAvailable(dataAccesses, seen, recipe) ||
isRecipeAvailable(transmitters, seen,
recipe);
isRecipeAvailable(transmitters, seen, recipe);
} else {
BlockEntity tileEntity = getLevel().getBlockEntity(getPos().relative(getFrontFacing()));
if (tileEntity == null) return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -612,7 +612,7 @@ public GTRecipeBuilder rpm(float rpm) {
private boolean applyResearchProperty(ResearchData.ResearchEntry researchEntry) {
if (!ConfigHolder.INSTANCE.machines.enableResearch) return false;
if (researchEntry == null) {
GTCEu.LOGGER.error("Assembly Line Research Entry cannot be empty.", new IllegalArgumentException());
GTCEu.LOGGER.error("Research Entry cannot be empty.", new IllegalArgumentException());
return false;
}

Expand Down Expand Up @@ -797,14 +797,13 @@ public int getSolderMultiplier() {
* @param EUt the EUt of the recipe
* @param CWUt how much computation per tick this recipe needs if in Research Station
*/
@Accessors(fluent = false)
public record ResearchRecipeEntry(
@NotNull String researchId,
@NotNull ItemStack researchStack,
@NotNull ItemStack dataStack,
@Getter int duration,
@Getter int EUt,
@Getter int CWUt) {
int duration,
int EUt,
int CWUt) {
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -277,14 +277,14 @@ public void collectStorage(Table<IO, RecipeCapability<?>, Object> extraTable, Ta
List<Content> 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<Content> 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);
Expand All @@ -300,14 +300,14 @@ public void collectStorage(Table<IO, RecipeCapability<?>, Object> extraTable, Ta
List<Content> 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<Content> 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);
Expand Down
5 changes: 4 additions & 1 deletion src/main/java/com/gregtechceu/gtceu/utils/GTStringUtils.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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();
}
}

0 comments on commit d424202

Please sign in to comment.