Skip to content

Commit

Permalink
feat: add chemical conduit locking gui
Browse files Browse the repository at this point in the history
  • Loading branch information
ferriarnus authored Jan 3, 2025
1 parent 8c7952c commit 27ea326
Show file tree
Hide file tree
Showing 6 changed files with 189 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -89,21 +89,21 @@ public boolean canConnectTo(ConduitNode selfNode, ConduitNode otherNode) {
var selfData = selfNode.getOrCreateData(MekanismModule.CHEMICAL_DATA_TYPE.get());
var otherData = otherNode.getOrCreateData(MekanismModule.CHEMICAL_DATA_TYPE.get());

return selfData.lockedChemical.isEmpty() || otherData.lockedChemical.isEmpty() || selfData.lockedChemical.equals(otherData.lockedChemical);
return selfData.lockedChemical().isEmpty() || otherData.lockedChemical().isEmpty() || selfData.lockedChemical().equals(otherData.lockedChemical());
}

@Override
public void onConnectTo(ConduitNode selfNode, ConduitNode otherNode) {
var selfData = selfNode.getOrCreateData(MekanismModule.CHEMICAL_DATA_TYPE.get());
var otherData = otherNode.getOrCreateData(MekanismModule.CHEMICAL_DATA_TYPE.get());

if (!selfData.lockedChemical.isEmpty()) {
if (!selfData.lockedChemical().isEmpty()) {
// if (!otherData.lockedChemical.isEmpty() && !selfData.lockedChemical.equals(otherData.lockedChemical)) {
// //EnderIO.LOGGER.warn("incompatible chemical conduits merged");
// }
otherData.setlockedChemical(selfData.lockedChemical);
} else if (!otherData.lockedChemical.isEmpty()) {
selfData.setlockedChemical(otherData.lockedChemical);
otherData.setLockedChemical(selfData.lockedChemical());
} else if (!otherData.lockedChemical().isEmpty()) {
selfData.setLockedChemical(otherData.lockedChemical());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,36 +10,52 @@
import net.minecraft.network.codec.ByteBufCodecs;
import net.minecraft.network.codec.StreamCodec;

import java.util.Optional;
import java.util.Objects;

public class ChemicalConduitData implements ConduitData<ChemicalConduitData> {

public static MapCodec<ChemicalConduitData> CODEC = RecordCodecBuilder.mapCodec(
instance -> instance.group(
Codec.BOOL.fieldOf("should_reset").forGetter(i -> i.shouldReset),
ChemicalStack.OPTIONAL_CODEC
.optionalFieldOf("locked_fluid")
.forGetter(i -> Optional.of(i.lockedChemical))
.optionalFieldOf("locked_fluid", ChemicalStack.EMPTY)
.forGetter(i -> i.lockedChemical)
).apply(instance, ChemicalConduitData::new)
);

public static StreamCodec<RegistryFriendlyByteBuf, ChemicalConduitData> STREAM_CODEC = StreamCodec.composite(
ByteBufCodecs.BOOL,
i -> i.shouldReset,
ByteBufCodecs.optional(ChemicalStack.OPTIONAL_STREAM_CODEC),
i -> Optional.of(i.lockedChemical),
ChemicalStack.OPTIONAL_STREAM_CODEC,
i -> i.lockedChemical,
ChemicalConduitData::new
);

ChemicalStack lockedChemical = ChemicalStack.EMPTY;
boolean shouldReset = false;
private ChemicalStack lockedChemical = ChemicalStack.EMPTY;
private boolean shouldReset = false;

public ChemicalConduitData() {
}

public ChemicalConduitData(boolean shouldReset, Optional<ChemicalStack> lockedChemical) {
public ChemicalConduitData(boolean shouldReset, ChemicalStack lockedChemical) {
this.shouldReset = shouldReset;
this.lockedChemical = lockedChemical.orElse(ChemicalStack.EMPTY);
this.lockedChemical = lockedChemical;
}

public boolean shouldReset() {
return shouldReset;
}

public void setShouldReset(boolean shouldReset) {
this.shouldReset = shouldReset;
}

public ChemicalStack lockedChemical() {
return lockedChemical;
}

public void setLockedChemical(ChemicalStack lockedChemical) {
this.lockedChemical = lockedChemical;
}

@Override
Expand All @@ -49,15 +65,20 @@ public ConduitDataType<ChemicalConduitData> type() {

@Override
public ChemicalConduitData withClientChanges(ChemicalConduitData guiData) {
return new ChemicalConduitData(guiData.shouldReset, Optional.ofNullable(lockedChemical));
this.shouldReset = guiData.shouldReset;

// TODO: Soon we will swap to records which will mean this will be a new instance.
// This API has been designed with this pending change in mind.
return this;
}

@Override
public ChemicalConduitData deepCopy() {
return new ChemicalConduitData(shouldReset, Optional.ofNullable(lockedChemical));
return new ChemicalConduitData(shouldReset, lockedChemical);
}

public void setlockedChemical(ChemicalStack lockedChemical) {
this.lockedChemical = lockedChemical;
@Override
public int hashCode() {
return Objects.hash(shouldReset, lockedChemical);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
package com.enderio.modconduits.mods.mekanism;

import com.enderio.base.api.EnderIO;
import com.enderio.conduits.api.ConduitDataAccessor;
import com.enderio.conduits.api.screen.ConduitScreenExtension;
import com.enderio.core.common.util.TooltipUtil;
import com.mojang.blaze3d.systems.RenderSystem;
import mekanism.api.chemical.ChemicalStack;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.gui.components.AbstractWidget;
import net.minecraft.client.gui.components.Tooltip;
import net.minecraft.client.gui.narration.NarrationElementOutput;
import net.minecraft.client.gui.screens.Screen;
import net.minecraft.client.renderer.texture.AbstractTexture;
import net.minecraft.client.renderer.texture.TextureAtlas;
import net.minecraft.client.renderer.texture.TextureAtlasSprite;
import net.minecraft.core.Direction;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.MutableComponent;
import net.minecraft.resources.ResourceLocation;
import org.joml.Vector2i;

import java.util.List;
import java.util.function.Supplier;

public final class ChemicalConduitScreenExtension implements ConduitScreenExtension {

private static final ResourceLocation WIDGET_TEXTURE = EnderIO.loc("textures/gui/chemicalbackground.png");

@Override
public List<AbstractWidget> createWidgets(Screen screen, ConduitDataAccessor conduitDataAccessor, UpdateDispatcher updateConduitData,
Supplier<Direction> direction, Vector2i widgetsStart) {
if (conduitDataAccessor.getOrCreateData(MekanismModule.CHEMICAL_DATA_TYPE.get()).lockedChemical().isEmpty()) {
return List.of();
}
return List.of(
new ChemicalWidget(widgetsStart.add(0, 20),
() -> conduitDataAccessor.getOrCreateData(MekanismModule.CHEMICAL_DATA_TYPE.get()).lockedChemical(),
() -> {
ChemicalConduitData data = conduitDataAccessor.getOrCreateData(MekanismModule.CHEMICAL_DATA_TYPE.get());
data.setShouldReset(true);
updateConduitData.sendUpdate();
})
);
}

private static class ChemicalWidget extends AbstractWidget {
private final Runnable onPress;
private final Supplier<ChemicalStack> currentChemical;

ChemicalWidget(Vector2i pos, Supplier<ChemicalStack> chemical, Runnable onPress) {
super(pos.x(), pos.y(), 14, 14, Component.empty());
this.onPress = onPress;
this.currentChemical = chemical;
}

@Override
public void updateWidgetNarration(NarrationElementOutput pNarrationElementOutput) {
}

@Override
public void renderWidget(GuiGraphics guiGraphics, int pMouseX, int pMouseY, float pPartialTick) {
if (isHoveredOrFocused()) {
MutableComponent tooltip = MekanismModule.CHEMICAL_CONDUIT_CHANGE_FLUID1.copy();
tooltip.append("\n").append(MekanismModule.CHEMICAL_CONDUIT_CHANGE_FLUID2);
if (!currentChemical.get().isEmpty()) {
tooltip.append("\n").append(TooltipUtil.withArgs(MekanismModule.CHEMICAL_CONDUIT_CHANGE_FLUID3, currentChemical.get().getChemical().getTextComponent()));
}
setTooltip(Tooltip.create(TooltipUtil.style(tooltip)));
}

RenderSystem.enableBlend();
RenderSystem.defaultBlendFunc();
RenderSystem.enableDepthTest();
guiGraphics.blit(WIDGET_TEXTURE, getX(), getY(), 0, 0, this.width, this.height);
if (currentChemical.get().isEmpty()) {
return;
}

ResourceLocation still = currentChemical.get().getChemical().getIcon();
AbstractTexture texture = Minecraft.getInstance().getTextureManager().getTexture(TextureAtlas.LOCATION_BLOCKS);
if (texture instanceof TextureAtlas atlas) {
TextureAtlasSprite sprite = atlas.getSprite(still);

int color = currentChemical.get().getChemicalTint();
RenderSystem.setShaderColor( ((color >> 16) & 0xFF) / 255.0F, ((color >> 8) & 0xFF) / 255.0F, (color & 0xFF) / 255.0F, 1);
RenderSystem.enableBlend();


int atlasWidth = (int)(sprite.contents().width() / (sprite.getU1() - sprite.getU0()));
int atlasHeight = (int)(sprite.contents().height() / (sprite.getV1() - sprite.getV0()));

guiGraphics.blit(TextureAtlas.LOCATION_BLOCKS, getX() + 1, getY() + 1, 0, sprite.getU0()*atlasWidth, sprite.getV0()*atlasHeight, 12, 12, atlasWidth, atlasHeight);

RenderSystem.setShaderColor(1, 1, 1, 1);
}

RenderSystem.disableBlend();
RenderSystem.disableDepthTest();
}

@Override
public void onClick(double pMouseX, double pMouseY) {
onPress.run();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,27 @@ private int getScaledTransferRate(ChemicalConduit conduit, CapabilityConnection
return (int)Math.ceil(conduit.transferRatePerTick() * (20.0 / conduit.graphTickRate()));
}

@Override
public void tickGraph(ServerLevel level, ChemicalConduit conduit, List<ConduitNode> loadedNodes, ConduitNetwork graph,
ColoredRedstoneProvider coloredRedstoneProvider) {
boolean shouldReset = false;
for (var loadedNode : loadedNodes) {
ChemicalConduitData fluidExtendedData = loadedNode.getOrCreateData(MekanismModule.CHEMICAL_DATA_TYPE.get());
if (fluidExtendedData.shouldReset()) {
shouldReset = true;
fluidExtendedData.setShouldReset(false);
}
}

if (shouldReset) {
for (var loadedNode : loadedNodes) {
ChemicalConduitData fluidExtendedData = loadedNode.getOrCreateData(MekanismModule.CHEMICAL_DATA_TYPE.get());
fluidExtendedData.setLockedChemical(ChemicalStack.EMPTY);
}
}
super.tickGraph(level, conduit, loadedNodes, graph, coloredRedstoneProvider);
}

@Override
protected void tickCapabilityGraph(ServerLevel level,
ChemicalConduit conduit,
Expand All @@ -45,12 +66,12 @@ private void tickExtractCapability(ChemicalConduit conduit, CapabilityConnection
Chemical extractType = extractHandler.extractChemical(Long.MAX_VALUE, Action.SIMULATE).getChemical();
ChemicalStack result;

if (!data.lockedChemical.isEmpty()) {
if (data.lockedChemical.getChemical() != extractType) {
if (!data.lockedChemical().isEmpty()) {
if (data.lockedChemical().getChemical() != extractType) {
return;
}

result = extractHandler.extractChemical(data.lockedChemical.getChemical().getStack(transferRate), Action.SIMULATE);
result = extractHandler.extractChemical(data.lockedChemical().getChemical().getStack(transferRate), Action.SIMULATE);
} else {
result = extractHandler.extractChemical(transferRate, Action.SIMULATE);
}
Expand All @@ -74,13 +95,16 @@ private void tickExtractCapability(ChemicalConduit conduit, CapabilityConnection
}
IChemicalHandler destinationHandler = insert.capability();
ChemicalStack transferredChemical;
if (!data.lockedChemical.isEmpty()) {
transferredChemical = tryChemicalTransfer(destinationHandler, extractHandler, data.lockedChemical.getChemical().getStack(transferRate - transferred), true);
if (!data.lockedChemical().isEmpty()) {
transferredChemical = tryChemicalTransfer(destinationHandler, extractHandler, data.lockedChemical().getChemical().getStack(transferRate - transferred), true);
} else {
transferredChemical = tryChemicalTransfer(destinationHandler, extractHandler, transferRate - transferred, true);
}

transferred += transferredChemical.getAmount();
if (!conduit.isMultiChemical() && data.lockedChemical().isEmpty()) {
data.setLockedChemical(extractType.getStack(1));
}
if (transferred >= transferRate) {
break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.enderio.conduits.api.ConduitDataType;
import com.enderio.conduits.api.ConduitType;
import com.enderio.conduits.api.EnderIOConduitsRegistries;
import com.enderio.conduits.api.screen.RegisterConduitScreenExtensionsEvent;
import com.enderio.conduits.common.conduit.ConduitApiImpl;
import com.enderio.conduits.common.recipe.ConduitIngredient;
import com.enderio.modconduits.ConduitModule;
Expand Down Expand Up @@ -131,6 +132,13 @@ public static class Item {
EnderIO.loc("conduit.chemical.multi"),
"Allows multiple chemical types to be transported on the same line");

public static final Component CHEMICAL_CONDUIT_CHANGE_FLUID1 = addTranslation("gui",
EnderIO.loc("chemical_conduit.change_fluid1"), "Locked Chemical:");
public static final Component CHEMICAL_CONDUIT_CHANGE_FLUID2 = addTranslation("gui",
EnderIO.loc("chemical_conduit.change_fluid2"), "Click to reset!");
public static final MutableComponent CHEMICAL_CONDUIT_CHANGE_FLUID3 = addTranslation("gui",
EnderIO.loc("chemical_conduit.change_fluid3"), "Chemical: %s");

private static final TagKey<Item> OSMIUM = ItemTags
.create(ResourceLocation.fromNamespaceAndPath("c", "ingots/osmium"));

Expand All @@ -145,6 +153,11 @@ public void register(IEventBus modEventBus) {
DATA_COMPONENT_TYPES.register(modEventBus);
ITEM_REGISTRY.register(modEventBus);
MENU_REGISTRY.register(modEventBus);
modEventBus.addListener(this::registerScreen);
}

public void registerScreen(RegisterConduitScreenExtensionsEvent event) {
event.register(Types.CHEMICAL.get(), ChemicalConduitScreenExtension::new);
}

@Override
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 27ea326

Please sign in to comment.