Improve crafting preview system a bit.

This commit is contained in:
raoulvdberge
2020-10-18 17:23:32 +02:00
parent cab5ff2d12
commit 74280eae99
14 changed files with 85 additions and 141 deletions

View File

@@ -150,7 +150,7 @@ public interface IRSAPI {
* @param type the type * @param type the type
* @return a set of external storage providers * @return a set of external storage providers
*/ */
Set<IExternalStorageProvider<?>> getExternalStorageProviders(StorageType type); <T> Set<IExternalStorageProvider<T>> getExternalStorageProviders(StorageType type);
/** /**
* @param world the world * @param world the world

View File

@@ -10,12 +10,7 @@ import net.minecraftforge.api.distmarker.OnlyIn;
/** /**
* Represents a crafting preview element. * Represents a crafting preview element.
*/ */
public interface ICraftingPreviewElement<T> { public interface ICraftingPreviewElement {
/**
* @return the underlying element to display
*/
T getElement();
/** /**
* @param matrixStack the matrix stack * @param matrixStack the matrix stack
* @param x position on the x axis to render * @param x position on the x axis to render
@@ -26,21 +21,9 @@ public interface ICraftingPreviewElement<T> {
void draw(MatrixStack matrixStack, int x, int y, IElementDrawers drawers); void draw(MatrixStack matrixStack, int x, int y, IElementDrawers drawers);
/** /**
* @return available amount of the {@link #getElement()} * @return true when this crafting preview elements signifies an error that disables starting a task
*/ */
int getAvailable(); boolean doesDisableTaskStarting();
/**
* @return the amount to craft or missing (depends on {@link #hasMissing()} amount of the {@link #getElement()}
*/
int getToCraft();
/**
* When this is true {@link #getToCraft()} will be the missing items.
*
* @return true when items are missing, false otherwise
*/
boolean hasMissing();
/** /**
* @param buf buffer to write to * @param buf buffer to write to

View File

@@ -16,7 +16,7 @@ public interface ICraftingPreviewElementRegistry {
* @param id the id, as specified in {@link ICraftingPreviewElement#getId()} * @param id the id, as specified in {@link ICraftingPreviewElement#getId()}
* @param factory the factory * @param factory the factory
*/ */
void add(ResourceLocation id, Function<PacketBuffer, ICraftingPreviewElement<?>> factory); void add(ResourceLocation id, Function<PacketBuffer, ICraftingPreviewElement> factory);
/** /**
* Returns a factory from the registry. * Returns a factory from the registry.
@@ -25,5 +25,5 @@ public interface ICraftingPreviewElementRegistry {
* @return the factory, or null if no factory was found * @return the factory, or null if no factory was found
*/ */
@Nullable @Nullable
Function<PacketBuffer, ICraftingPreviewElement<?>> get(ResourceLocation id); Function<PacketBuffer, ICraftingPreviewElement> get(ResourceLocation id);
} }

View File

@@ -16,9 +16,9 @@ public interface ICalculationResult {
CalculationResultType getType(); CalculationResultType getType();
/** /**
* @return get a list of {@link ICraftingPreviewElement}s * @return get a list of preview elements
*/ */
List<ICraftingPreviewElement<?>> getPreviewElements(); List<ICraftingPreviewElement> getPreviewElements();
/** /**
* @return the task if the calculation {@link #isOk()}, otherwise null * @return the task if the calculation {@link #isOk()}, otherwise null

View File

@@ -11,16 +11,16 @@ import java.util.Map;
import java.util.function.Function; import java.util.function.Function;
public class CraftingPreviewElementRegistry implements ICraftingPreviewElementRegistry { public class CraftingPreviewElementRegistry implements ICraftingPreviewElementRegistry {
private final Map<ResourceLocation, Function<PacketBuffer, ICraftingPreviewElement<?>>> registry = new HashMap<>(); private final Map<ResourceLocation, Function<PacketBuffer, ICraftingPreviewElement>> registry = new HashMap<>();
@Override @Override
public void add(ResourceLocation id, Function<PacketBuffer, ICraftingPreviewElement<?>> factory) { public void add(ResourceLocation id, Function<PacketBuffer, ICraftingPreviewElement> factory) {
registry.put(id, factory); registry.put(id, factory);
} }
@Nullable @Nullable
@Override @Override
public Function<PacketBuffer, ICraftingPreviewElement<?>> get(ResourceLocation id) { public Function<PacketBuffer, ICraftingPreviewElement> get(ResourceLocation id) {
return registry.get(id); return registry.get(id);
} }
} }

View File

@@ -9,20 +9,19 @@ import net.minecraft.item.ItemStack;
import net.minecraft.network.PacketBuffer; import net.minecraft.network.PacketBuffer;
import net.minecraft.util.ResourceLocation; import net.minecraft.util.ResourceLocation;
public class ErrorCraftingPreviewElement implements ICraftingPreviewElement<ItemStack> { public class ErrorCraftingPreviewElement implements ICraftingPreviewElement {
public static final ResourceLocation ID = new ResourceLocation(RS.ID, "error"); public static final ResourceLocation ID = new ResourceLocation(RS.ID, "error");
private final CalculationResultType type; private final CalculationResultType type;
private final ItemStack stack; private final ItemStack recursedPattern;
public ErrorCraftingPreviewElement(CalculationResultType type, ItemStack stack) { public ErrorCraftingPreviewElement(CalculationResultType type, ItemStack recursedPattern) {
this.type = type; this.type = type;
this.stack = stack; this.recursedPattern = recursedPattern;
} }
@Override public ItemStack getRecursedPattern() {
public ItemStack getElement() { return recursedPattern;
return stack;
} }
@Override @Override
@@ -31,24 +30,14 @@ public class ErrorCraftingPreviewElement implements ICraftingPreviewElement<Item
} }
@Override @Override
public int getAvailable() { public boolean doesDisableTaskStarting() {
return 0; return true;
}
@Override
public int getToCraft() {
return 0;
}
@Override
public boolean hasMissing() {
return false;
} }
@Override @Override
public void write(PacketBuffer buf) { public void write(PacketBuffer buf) {
buf.writeInt(type.ordinal()); buf.writeInt(type.ordinal());
buf.writeItemStack(stack); buf.writeItemStack(recursedPattern);
} }
public CalculationResultType getType() { public CalculationResultType getType() {

View File

@@ -14,7 +14,7 @@ import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn; import net.minecraftforge.api.distmarker.OnlyIn;
import net.minecraftforge.fluids.FluidStack; import net.minecraftforge.fluids.FluidStack;
public class FluidCraftingPreviewElement implements ICraftingPreviewElement<FluidStack> { public class FluidCraftingPreviewElement implements ICraftingPreviewElement {
public static final ResourceLocation ID = new ResourceLocation(RS.ID, "fluid"); public static final ResourceLocation ID = new ResourceLocation(RS.ID, "fluid");
private final FluidStack stack; private final FluidStack stack;
@@ -51,8 +51,7 @@ public class FluidCraftingPreviewElement implements ICraftingPreviewElement<Flui
return new FluidCraftingPreviewElement(stack, available, missing, toCraft); return new FluidCraftingPreviewElement(stack, available, missing, toCraft);
} }
@Override public FluidStack getStack() {
public FluidStack getElement() {
return stack; return stack;
} }
@@ -66,7 +65,7 @@ public class FluidCraftingPreviewElement implements ICraftingPreviewElement<Flui
x += 5; x += 5;
y += 7; y += 7;
drawers.getFluidDrawer().draw(matrixStack, x, y, getElement()); drawers.getFluidDrawer().draw(matrixStack, x, y, getStack());
float scale = Minecraft.getInstance().getForceUnicodeFont() ? 1F : 0.5F; float scale = Minecraft.getInstance().getForceUnicodeFont() ? 1F : 0.5F;
@@ -75,15 +74,15 @@ public class FluidCraftingPreviewElement implements ICraftingPreviewElement<Flui
matrixStack.push(); matrixStack.push();
matrixStack.scale(scale, scale, 1); matrixStack.scale(scale, scale, 1);
if (getToCraft() > 0) { if (toCraft > 0) {
String format = hasMissing() ? "gui.refinedstorage.crafting_preview.missing" : "gui.refinedstorage.crafting_preview.to_craft"; String format = doesDisableTaskStarting() ? "gui.refinedstorage.crafting_preview.missing" : "gui.refinedstorage.crafting_preview.to_craft";
drawers.getStringDrawer().draw(matrixStack, RenderUtils.getOffsetOnScale(x + 23, scale), RenderUtils.getOffsetOnScale(y, scale), I18n.format(format, API.instance().getQuantityFormatter().formatInBucketForm(getToCraft()))); drawers.getStringDrawer().draw(matrixStack, RenderUtils.getOffsetOnScale(x + 23, scale), RenderUtils.getOffsetOnScale(y, scale), I18n.format(format, API.instance().getQuantityFormatter().formatInBucketForm(toCraft)));
y += 7; y += 7;
} }
if (getAvailable() > 0) { if (available > 0) {
drawers.getStringDrawer().draw(matrixStack, RenderUtils.getOffsetOnScale(x + 23, scale), RenderUtils.getOffsetOnScale(y, scale), I18n.format("gui.refinedstorage.crafting_preview.available", API.instance().getQuantityFormatter().formatInBucketForm(getAvailable()))); drawers.getStringDrawer().draw(matrixStack, RenderUtils.getOffsetOnScale(x + 23, scale), RenderUtils.getOffsetOnScale(y, scale), I18n.format("gui.refinedstorage.crafting_preview.available", API.instance().getQuantityFormatter().formatInBucketForm(available)));
} }
matrixStack.pop(); matrixStack.pop();
@@ -93,26 +92,16 @@ public class FluidCraftingPreviewElement implements ICraftingPreviewElement<Flui
this.available += amount; this.available += amount;
} }
@Override
public int getAvailable() {
return available;
}
public void addToCraft(int amount) { public void addToCraft(int amount) {
this.toCraft += amount; this.toCraft += amount;
} }
@Override
public int getToCraft() {
return this.toCraft;
}
public void setMissing(boolean missing) { public void setMissing(boolean missing) {
this.missing = missing; this.missing = missing;
} }
@Override @Override
public boolean hasMissing() { public boolean doesDisableTaskStarting() {
return missing; return missing;
} }

View File

@@ -14,7 +14,7 @@ import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn; import net.minecraftforge.api.distmarker.OnlyIn;
import net.minecraftforge.items.ItemHandlerHelper; import net.minecraftforge.items.ItemHandlerHelper;
public class ItemCraftingPreviewElement implements ICraftingPreviewElement<ItemStack> { public class ItemCraftingPreviewElement implements ICraftingPreviewElement {
public static final ResourceLocation ID = new ResourceLocation(RS.ID, "item"); public static final ResourceLocation ID = new ResourceLocation(RS.ID, "item");
private final ItemStack stack; private final ItemStack stack;
@@ -34,6 +34,10 @@ public class ItemCraftingPreviewElement implements ICraftingPreviewElement<ItemS
this.toCraft = toCraft; this.toCraft = toCraft;
} }
public ItemStack getStack() {
return stack;
}
@Override @Override
public void write(PacketBuffer buf) { public void write(PacketBuffer buf) {
buf.writeItemStack(stack); buf.writeItemStack(stack);
@@ -51,11 +55,6 @@ public class ItemCraftingPreviewElement implements ICraftingPreviewElement<ItemS
return new ItemCraftingPreviewElement(stack, available, missing, toCraft); return new ItemCraftingPreviewElement(stack, available, missing, toCraft);
} }
@Override
public ItemStack getElement() {
return stack;
}
@Override @Override
@OnlyIn(Dist.CLIENT) @OnlyIn(Dist.CLIENT)
public void draw(MatrixStack matrixStack, int x, int y, IElementDrawers drawers) { public void draw(MatrixStack matrixStack, int x, int y, IElementDrawers drawers) {
@@ -66,7 +65,7 @@ public class ItemCraftingPreviewElement implements ICraftingPreviewElement<ItemS
x += 5; x += 5;
y += 7; y += 7;
drawers.getItemDrawer().draw(matrixStack, x, y, getElement()); drawers.getItemDrawer().draw(matrixStack, x, y, stack);
float scale = Minecraft.getInstance().getForceUnicodeFont() ? 1F : 0.5F; float scale = Minecraft.getInstance().getForceUnicodeFont() ? 1F : 0.5F;
@@ -75,15 +74,15 @@ public class ItemCraftingPreviewElement implements ICraftingPreviewElement<ItemS
matrixStack.push(); matrixStack.push();
matrixStack.scale(scale, scale, 1); matrixStack.scale(scale, scale, 1);
if (getToCraft() > 0) { if (toCraft > 0) {
String format = hasMissing() ? "gui.refinedstorage.crafting_preview.missing" : "gui.refinedstorage.crafting_preview.to_craft"; String format = doesDisableTaskStarting() ? "gui.refinedstorage.crafting_preview.missing" : "gui.refinedstorage.crafting_preview.to_craft";
drawers.getStringDrawer().draw(matrixStack, RenderUtils.getOffsetOnScale(x + 23, scale), RenderUtils.getOffsetOnScale(y, scale), I18n.format(format, getToCraft())); drawers.getStringDrawer().draw(matrixStack, RenderUtils.getOffsetOnScale(x + 23, scale), RenderUtils.getOffsetOnScale(y, scale), I18n.format(format, toCraft));
y += 7; y += 7;
} }
if (getAvailable() > 0) { if (available > 0) {
drawers.getStringDrawer().draw(matrixStack, RenderUtils.getOffsetOnScale(x + 23, scale), RenderUtils.getOffsetOnScale(y, scale), I18n.format("gui.refinedstorage.crafting_preview.available", getAvailable())); drawers.getStringDrawer().draw(matrixStack, RenderUtils.getOffsetOnScale(x + 23, scale), RenderUtils.getOffsetOnScale(y, scale), I18n.format("gui.refinedstorage.crafting_preview.available", available));
} }
matrixStack.pop(); matrixStack.pop();
@@ -93,26 +92,16 @@ public class ItemCraftingPreviewElement implements ICraftingPreviewElement<ItemS
this.available += amount; this.available += amount;
} }
@Override
public int getAvailable() {
return available;
}
public void addToCraft(int amount) { public void addToCraft(int amount) {
this.toCraft += amount; this.toCraft += amount;
} }
@Override
public int getToCraft() {
return this.toCraft;
}
public void setMissing(boolean missing) { public void setMissing(boolean missing) {
this.missing = missing; this.missing = missing;
} }
@Override @Override
public boolean hasMissing() { public boolean doesDisableTaskStarting() {
return missing; return missing;
} }

View File

@@ -13,7 +13,7 @@ import java.util.List;
public class CalculationResult implements ICalculationResult { public class CalculationResult implements ICalculationResult {
private final CalculationResultType type; private final CalculationResultType type;
private final ICraftingPattern recursedPattern; private final ICraftingPattern recursedPattern;
private final List<ICraftingPreviewElement<?>> previewElements; private final List<ICraftingPreviewElement> previewElements;
private final ICraftingTask craftingTask; private final ICraftingTask craftingTask;
public CalculationResult(CalculationResultType type) { public CalculationResult(CalculationResultType type) {
@@ -30,7 +30,7 @@ public class CalculationResult implements ICalculationResult {
this.craftingTask = null; this.craftingTask = null;
} }
public CalculationResult(CalculationResultType type, List<ICraftingPreviewElement<?>> previewElements, @Nullable ICraftingTask craftingTask) { public CalculationResult(CalculationResultType type, List<ICraftingPreviewElement> previewElements, @Nullable ICraftingTask craftingTask) {
this.type = type; this.type = type;
this.recursedPattern = null; this.recursedPattern = null;
this.previewElements = previewElements; this.previewElements = previewElements;
@@ -43,7 +43,7 @@ public class CalculationResult implements ICalculationResult {
} }
@Override @Override
public List<ICraftingPreviewElement<?>> getPreviewElements() { public List<ICraftingPreviewElement> getPreviewElements() {
return previewElements; return previewElements;
} }

View File

@@ -76,7 +76,7 @@ public class CraftingCalculator {
craftingPreviewInfo.getToCraftFluids().add(StackUtils.copy(requested.getFluid(), qty * qtyPerCraft)); craftingPreviewInfo.getToCraftFluids().add(StackUtils.copy(requested.getFluid(), qty * qtyPerCraft));
} }
List<ICraftingPreviewElement<?>> previewElements = new CraftingPreviewElementFactory().getElements(craftingPreviewInfo); List<ICraftingPreviewElement> previewElements = new CraftingPreviewElementFactory().getElements(craftingPreviewInfo);
if (craftingPreviewInfo.hasMissing()) { if (craftingPreviewInfo.hasMissing()) {
return new CalculationResult(CalculationResultType.MISSING, previewElements, null); return new CalculationResult(CalculationResultType.MISSING, previewElements, null);

View File

@@ -15,7 +15,7 @@ import java.util.List;
import java.util.Map; import java.util.Map;
public class CraftingPreviewElementFactory { public class CraftingPreviewElementFactory {
public List<ICraftingPreviewElement<?>> getElements(CraftingPreviewInfo info) { public List<ICraftingPreviewElement> getElements(CraftingPreviewInfo info) {
Map<Integer, ItemCraftingPreviewElement> map = new LinkedHashMap<>(); Map<Integer, ItemCraftingPreviewElement> map = new LinkedHashMap<>();
Map<Integer, FluidCraftingPreviewElement> mapFluids = new LinkedHashMap<>(); Map<Integer, FluidCraftingPreviewElement> mapFluids = new LinkedHashMap<>();
@@ -63,7 +63,7 @@ public class CraftingPreviewElementFactory {
previewStack.addAvailable(stack.getStack().getAmount()); previewStack.addAvailable(stack.getStack().getAmount());
} }
List<ICraftingPreviewElement<?>> elements = new ArrayList<>(); List<ICraftingPreviewElement> elements = new ArrayList<>();
elements.addAll(map.values()); elements.addAll(map.values());
elements.addAll(mapFluids.values()); elements.addAll(mapFluids.values());

View File

@@ -212,17 +212,17 @@ public class ExternalStorageNetworkNode extends NetworkNode implements IStorageP
if (facing != null) { if (facing != null) {
if (type == IType.ITEMS) { if (type == IType.ITEMS) {
for (IExternalStorageProvider<?> provider : API.instance().getExternalStorageProviders(StorageType.ITEM)) { for (IExternalStorageProvider<ItemStack> provider : API.instance().<ItemStack>getExternalStorageProviders(StorageType.ITEM)) {
if (provider.canProvide(facing, getDirection())) { if (provider.canProvide(facing, getDirection())) {
itemStorages.add((IExternalStorage<ItemStack>) provider.provide(this, getFacingTile(), getDirection())); itemStorages.add(provider.provide(this, getFacingTile(), getDirection()));
break; break;
} }
} }
} else if (type == IType.FLUIDS) { } else if (type == IType.FLUIDS) {
for (IExternalStorageProvider<?> provider : API.instance().getExternalStorageProviders(StorageType.FLUID)) { for (IExternalStorageProvider<FluidStack> provider : API.instance().<FluidStack>getExternalStorageProviders(StorageType.FLUID)) {
if (provider.canProvide(facing, getDirection())) { if (provider.canProvide(facing, getDirection())) {
fluidStorages.add((IExternalStorage<FluidStack>) provider.provide(this, getFacingTile(), getDirection())); fluidStorages.add(provider.provide(this, getFacingTile(), getDirection()));
break; break;
} }

View File

@@ -13,19 +13,19 @@ import java.util.UUID;
import java.util.function.Supplier; import java.util.function.Supplier;
public class GridCraftingPreviewResponseMessage { public class GridCraftingPreviewResponseMessage {
private final List<ICraftingPreviewElement<?>> elements; private final List<ICraftingPreviewElement> elements;
private final UUID id; private final UUID id;
private final int quantity; private final int quantity;
private final boolean fluids; private final boolean fluids;
public GridCraftingPreviewResponseMessage(List<ICraftingPreviewElement<?>> elements, UUID id, int quantity, boolean fluids) { public GridCraftingPreviewResponseMessage(List<ICraftingPreviewElement> elements, UUID id, int quantity, boolean fluids) {
this.elements = elements; this.elements = elements;
this.id = id; this.id = id;
this.quantity = quantity; this.quantity = quantity;
this.fluids = fluids; this.fluids = fluids;
} }
public List<ICraftingPreviewElement<?>> getElements() { public List<ICraftingPreviewElement> getElements() {
return elements; return elements;
} }
@@ -46,16 +46,16 @@ public class GridCraftingPreviewResponseMessage {
int quantity = buf.readInt(); int quantity = buf.readInt();
boolean fluids = buf.readBoolean(); boolean fluids = buf.readBoolean();
List<ICraftingPreviewElement<?>> stacks = new LinkedList<>(); List<ICraftingPreviewElement> elements = new LinkedList<>();
int size = buf.readInt(); int size = buf.readInt();
for (int i = 0; i < size; i++) { for (int i = 0; i < size; i++) {
ResourceLocation type = buf.readResourceLocation(); ResourceLocation type = buf.readResourceLocation();
stacks.add(API.instance().getCraftingPreviewElementRegistry().get(type).apply(buf)); elements.add(API.instance().getCraftingPreviewElementRegistry().get(type).apply(buf));
} }
return new GridCraftingPreviewResponseMessage(stacks, id, quantity, fluids); return new GridCraftingPreviewResponseMessage(elements, id, quantity, fluids);
} }
public static void encode(GridCraftingPreviewResponseMessage message, PacketBuffer buf) { public static void encode(GridCraftingPreviewResponseMessage message, PacketBuffer buf) {
@@ -64,9 +64,9 @@ public class GridCraftingPreviewResponseMessage {
buf.writeBoolean(message.fluids); buf.writeBoolean(message.fluids);
buf.writeInt(message.elements.size()); buf.writeInt(message.elements.size());
for (ICraftingPreviewElement<?> stack : message.elements) { for (ICraftingPreviewElement element : message.elements) {
buf.writeResourceLocation(stack.getId()); buf.writeResourceLocation(element.getId());
stack.write(buf); element.write(buf);
} }
} }

View File

@@ -5,7 +5,6 @@ import com.mojang.blaze3d.systems.RenderSystem;
import com.refinedmods.refinedstorage.RS; import com.refinedmods.refinedstorage.RS;
import com.refinedmods.refinedstorage.api.autocrafting.ICraftingPattern; import com.refinedmods.refinedstorage.api.autocrafting.ICraftingPattern;
import com.refinedmods.refinedstorage.api.autocrafting.preview.ICraftingPreviewElement; import com.refinedmods.refinedstorage.api.autocrafting.preview.ICraftingPreviewElement;
import com.refinedmods.refinedstorage.api.autocrafting.task.CalculationResultType;
import com.refinedmods.refinedstorage.api.render.IElementDrawers; import com.refinedmods.refinedstorage.api.render.IElementDrawers;
import com.refinedmods.refinedstorage.apiimpl.autocrafting.preview.ErrorCraftingPreviewElement; import com.refinedmods.refinedstorage.apiimpl.autocrafting.preview.ErrorCraftingPreviewElement;
import com.refinedmods.refinedstorage.apiimpl.autocrafting.preview.FluidCraftingPreviewElement; import com.refinedmods.refinedstorage.apiimpl.autocrafting.preview.FluidCraftingPreviewElement;
@@ -39,7 +38,7 @@ import java.util.UUID;
public class CraftingPreviewScreen extends BaseScreen<Container> { public class CraftingPreviewScreen extends BaseScreen<Container> {
private static final int VISIBLE_ROWS = 5; private static final int VISIBLE_ROWS = 5;
private final List<ICraftingPreviewElement<?>> stacks; private final List<ICraftingPreviewElement> elements;
private final Screen parent; private final Screen parent;
private final ScrollbarWidget scrollbar; private final ScrollbarWidget scrollbar;
@@ -53,7 +52,7 @@ public class CraftingPreviewScreen extends BaseScreen<Container> {
private final IElementDrawers drawers = new CraftingPreviewElementDrawers(this, font); private final IElementDrawers drawers = new CraftingPreviewElementDrawers(this, font);
public CraftingPreviewScreen(Screen parent, List<ICraftingPreviewElement<?>> stacks, UUID id, int quantity, boolean fluids, ITextComponent title) { public CraftingPreviewScreen(Screen parent, List<ICraftingPreviewElement> elements, UUID id, int quantity, boolean fluids, ITextComponent title) {
super(new Container(null, 0) { super(new Container(null, 0) {
@Override @Override
public boolean canInteractWith(@Nonnull PlayerEntity player) { public boolean canInteractWith(@Nonnull PlayerEntity player) {
@@ -61,7 +60,7 @@ public class CraftingPreviewScreen extends BaseScreen<Container> {
} }
}, 254, 201, null, title); }, 254, 201, null, title);
this.stacks = new ArrayList<>(stacks); this.elements = new ArrayList<>(elements);
this.parent = parent; this.parent = parent;
this.id = id; this.id = id;
@@ -76,7 +75,7 @@ public class CraftingPreviewScreen extends BaseScreen<Container> {
addButton(x + 55, y + 201 - 20 - 7, 50, 20, new TranslationTextComponent("gui.cancel"), true, true, btn -> close()); addButton(x + 55, y + 201 - 20 - 7, 50, 20, new TranslationTextComponent("gui.cancel"), true, true, btn -> close());
Button startButton = addButton(x + 129, y + 201 - 20 - 7, 50, 20, new TranslationTextComponent("misc.refinedstorage.start"), true, true, btn -> startRequest()); Button startButton = addButton(x + 129, y + 201 - 20 - 7, 50, 20, new TranslationTextComponent("misc.refinedstorage.start"), true, true, btn -> startRequest());
startButton.active = stacks.stream().noneMatch(ICraftingPreviewElement::hasMissing) && getErrorType() == null; startButton.active = elements.stream().noneMatch(ICraftingPreviewElement::doesDisableTaskStarting);
} }
@Override @Override
@@ -86,9 +85,9 @@ public class CraftingPreviewScreen extends BaseScreen<Container> {
} }
@Nullable @Nullable
private CalculationResultType getErrorType() { private ErrorCraftingPreviewElement getError() {
if (stacks.size() == 1 && stacks.get(0) instanceof ErrorCraftingPreviewElement) { if (elements.size() == 1 && elements.get(0) instanceof ErrorCraftingPreviewElement) {
return ((ErrorCraftingPreviewElement) stacks.get(0)).getType(); return (ErrorCraftingPreviewElement) elements.get(0);
} }
return null; return null;
@@ -100,7 +99,7 @@ public class CraftingPreviewScreen extends BaseScreen<Container> {
blit(matrixStack, x, y, 0, 0, xSize, ySize); blit(matrixStack, x, y, 0, 0, xSize, ySize);
if (getErrorType() != null) { if (getError() != null) {
fill(matrixStack, x + 7, y + 20, x + 228, y + 169, 0xFFDBDBDB); fill(matrixStack, x + 7, y + 20, x + 228, y + 169, 0xFFDBDBDB);
} }
@@ -116,9 +115,9 @@ public class CraftingPreviewScreen extends BaseScreen<Container> {
float scale = Minecraft.getInstance().getForceUnicodeFont() ? 1F : 0.5F; float scale = Minecraft.getInstance().getForceUnicodeFont() ? 1F : 0.5F;
CalculationResultType errorType = getErrorType(); ErrorCraftingPreviewElement error = getError();
if (errorType != null) { if (error != null) {
renderError(matrixStack, x, y, scale, errorType); renderError(matrixStack, x, y, scale, error);
} else { } else {
renderPreview(matrixStack, mouseX, mouseY, x, y); renderPreview(matrixStack, mouseX, mouseY, x, y);
} }
@@ -134,8 +133,8 @@ public class CraftingPreviewScreen extends BaseScreen<Container> {
this.hoveringFluid = null; this.hoveringFluid = null;
for (int i = 0; i < 3 * 5; ++i) { for (int i = 0; i < 3 * 5; ++i) {
if (slot < stacks.size()) { if (slot < elements.size()) {
renderElement(matrixStack, mouseX, mouseY, x, y, stacks.get(slot)); renderElement(matrixStack, mouseX, mouseY, x, y, elements.get(slot));
} }
if ((i + 1) % 3 == 0) { if ((i + 1) % 3 == 0) {
@@ -149,35 +148,33 @@ public class CraftingPreviewScreen extends BaseScreen<Container> {
} }
} }
private void renderElement(MatrixStack matrixStack, int mouseX, int mouseY, int x, int y, ICraftingPreviewElement<?> element) { private void renderElement(MatrixStack matrixStack, int mouseX, int mouseY, int x, int y, ICraftingPreviewElement element) {
element.draw(matrixStack, x, y + 5, drawers); element.draw(matrixStack, x, y + 5, drawers);
if (RenderUtils.inBounds(x + 5, y + 7, 16, 16, mouseX, mouseY)) { if (RenderUtils.inBounds(x + 5, y + 7, 16, 16, mouseX, mouseY)) {
this.hoveringStack = element.getId().equals(ItemCraftingPreviewElement.ID) ? (ItemStack) element.getElement() : null; this.hoveringStack = element instanceof ItemCraftingPreviewElement ? ((ItemCraftingPreviewElement) element).getStack() : null;
if (this.hoveringStack == null) { if (this.hoveringStack == null) {
this.hoveringFluid = element.getId().equals(FluidCraftingPreviewElement.ID) ? (FluidStack) element.getElement() : null; this.hoveringFluid = element instanceof FluidCraftingPreviewElement ? ((FluidCraftingPreviewElement) element).getStack() : null;
} }
} }
} }
private void renderError(MatrixStack matrixStack, int x, int y, float scale, CalculationResultType errorType) { private void renderError(MatrixStack matrixStack, int x, int y, float scale, ErrorCraftingPreviewElement errorElement) {
matrixStack.push(); matrixStack.push();
matrixStack.scale(scale, scale, 1); matrixStack.scale(scale, scale, 1);
renderString(matrixStack, RenderUtils.getOffsetOnScale(x + 5, scale), RenderUtils.getOffsetOnScale(y + 11, scale), I18n.format("gui.refinedstorage.crafting_preview.error")); renderString(matrixStack, RenderUtils.getOffsetOnScale(x + 5, scale), RenderUtils.getOffsetOnScale(y + 11, scale), I18n.format("gui.refinedstorage.crafting_preview.error"));
switch (errorType) { switch (errorElement.getType()) {
case RECURSIVE: case RECURSIVE:
renderRecursiveError(matrixStack, x, y, scale); renderRecursiveError(matrixStack, x, y, scale, errorElement.getRecursedPattern());
break;
case OK:
case MISSING:
case NO_PATTERN:
break; break;
case TOO_COMPLEX: case TOO_COMPLEX:
renderTooComplexError(matrixStack, x, y, scale); renderTooComplexError(matrixStack, x, y, scale);
break; break;
default:
break;
} }
matrixStack.pop(); matrixStack.pop();
@@ -188,7 +185,7 @@ public class CraftingPreviewScreen extends BaseScreen<Container> {
renderString(matrixStack, RenderUtils.getOffsetOnScale(x + 5, scale), RenderUtils.getOffsetOnScale(y + 31, scale), I18n.format("gui.refinedstorage.crafting_preview.error.too_complex.1")); renderString(matrixStack, RenderUtils.getOffsetOnScale(x + 5, scale), RenderUtils.getOffsetOnScale(y + 31, scale), I18n.format("gui.refinedstorage.crafting_preview.error.too_complex.1"));
} }
private void renderRecursiveError(MatrixStack matrixStack, int x, int y, float scale) { private void renderRecursiveError(MatrixStack matrixStack, int x, int y, float scale, ItemStack recursedPattern) {
renderString(matrixStack, RenderUtils.getOffsetOnScale(x + 5, scale), RenderUtils.getOffsetOnScale(y + 21, scale), I18n.format("gui.refinedstorage.crafting_preview.error.recursive.0")); renderString(matrixStack, RenderUtils.getOffsetOnScale(x + 5, scale), RenderUtils.getOffsetOnScale(y + 21, scale), I18n.format("gui.refinedstorage.crafting_preview.error.recursive.0"));
renderString(matrixStack, RenderUtils.getOffsetOnScale(x + 5, scale), RenderUtils.getOffsetOnScale(y + 31, scale), I18n.format("gui.refinedstorage.crafting_preview.error.recursive.1")); renderString(matrixStack, RenderUtils.getOffsetOnScale(x + 5, scale), RenderUtils.getOffsetOnScale(y + 31, scale), I18n.format("gui.refinedstorage.crafting_preview.error.recursive.1"));
renderString(matrixStack, RenderUtils.getOffsetOnScale(x + 5, scale), RenderUtils.getOffsetOnScale(y + 41, scale), I18n.format("gui.refinedstorage.crafting_preview.error.recursive.2")); renderString(matrixStack, RenderUtils.getOffsetOnScale(x + 5, scale), RenderUtils.getOffsetOnScale(y + 41, scale), I18n.format("gui.refinedstorage.crafting_preview.error.recursive.2"));
@@ -196,15 +193,12 @@ public class CraftingPreviewScreen extends BaseScreen<Container> {
renderString(matrixStack, RenderUtils.getOffsetOnScale(x + 5, scale), RenderUtils.getOffsetOnScale(y + 61, scale), I18n.format("gui.refinedstorage.crafting_preview.error.recursive.4")); renderString(matrixStack, RenderUtils.getOffsetOnScale(x + 5, scale), RenderUtils.getOffsetOnScale(y + 61, scale), I18n.format("gui.refinedstorage.crafting_preview.error.recursive.4"));
ICraftingPattern pattern = PatternItem.fromCache(parent.getMinecraft().world, (ItemStack) stacks.get(0).getElement()); ICraftingPattern pattern = PatternItem.fromCache(parent.getMinecraft().world, recursedPattern);
int yy = 83; int yy = 83;
for (ItemStack output : pattern.getOutputs()) { for (ItemStack output : pattern.getOutputs()) {
if (output != null) { if (output != null) {
matrixStack.push();
matrixStack.scale(scale, scale, 1);
renderString(matrixStack, RenderUtils.getOffsetOnScale(x + 25, scale), RenderUtils.getOffsetOnScale(yy + 6, scale), output.getDisplayName().getString()); renderString(matrixStack, RenderUtils.getOffsetOnScale(x + 25, scale), RenderUtils.getOffsetOnScale(yy + 6, scale), output.getDisplayName().getString());
matrixStack.pop();
RenderHelper.setupGui3DDiffuseLighting(); RenderHelper.setupGui3DDiffuseLighting();
RenderSystem.enableDepthTest(); RenderSystem.enableDepthTest();
@@ -282,7 +276,7 @@ public class CraftingPreviewScreen extends BaseScreen<Container> {
} }
private int getRows() { private int getRows() {
return Math.max(0, (int) Math.ceil((float) stacks.size() / 3F)); return Math.max(0, (int) Math.ceil((float) elements.size() / 3F));
} }
private void close() { private void close() {