Skip to content

Commit

Permalink
Small Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
IntegerLimit committed Jan 13, 2025
1 parent 558b549 commit e30bc82
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import net.minecraft.client.renderer.texture.TextureAtlasSprite;
import net.minecraft.client.renderer.texture.TextureMap;
import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;

import org.jetbrains.annotations.NotNull;

Expand Down Expand Up @@ -40,17 +42,8 @@ public LabsFluidStackElement(@NotNull ByteBuf buf) {

@Override
public void render(int x, int y) {
String actualLocation = location;

// Gregtech fluids added by GRS do this for some reason
// As a consequence the fluid texture from /dull will not show up on anything from GRS.
if (location.contains("material_sets/fluid/") && (location.contains("/gas") || location.contains("/plasma"))) {
actualLocation = location.replace("material_sets/fluid/", "material_sets/dull/");
}

if (sprite == null) {
sprite = Minecraft.getMinecraft().getTextureMapBlocks().getAtlasSprite(actualLocation);
}
if (sprite == null)
sprite = getFluidAtlasSprite(location);

GlStateManager.enableBlend();
Minecraft.getMinecraft().renderEngine.bindTexture(TextureMap.LOCATION_BLOCKS_TEXTURE);
Expand Down Expand Up @@ -95,4 +88,18 @@ public void toBytes(@NotNull ByteBuf buf) {
public int getID() {
return LabsTOPManager.FLUID_STACK_ELEMENT;
}

@SideOnly(Side.CLIENT)
public static TextureAtlasSprite getFluidAtlasSprite(String stillLocation) {
String actualLocation = stillLocation;

// Gregtech fluids added by GRS do this for some reason
// As a consequence the fluid texture from /dull will not show up on anything from GRS.
if (stillLocation.contains("material_sets/fluid/") &&
(stillLocation.contains("/gas") || stillLocation.contains("/plasma"))) {
actualLocation = stillLocation.replace("material_sets/fluid/", "material_sets/dull/");
}

return Minecraft.getMinecraft().getTextureMapBlocks().getAtlasSprite(actualLocation);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

import com.nomiceu.nomilabs.NomiLabs;
import com.nomiceu.nomilabs.integration.top.LabsFluidNameElement;
import com.nomiceu.nomilabs.integration.top.LabsFluidStackElement;
import com.nomiceu.nomilabs.util.LabsTranslate;

import gregtech.api.util.TextFormattingUtil;
Expand Down Expand Up @@ -77,48 +79,28 @@ public class ElementTankGaugeMixin {

@Inject(method = "render", at = @At(value = "HEAD"), cancellable = true)
private void newRenderLogic(int x, int y, CallbackInfo ci) {
boolean hasFluid = capacity > 0 && amount > 0;
boolean hasFluid = capacity > 0 && amount > 0 && fluidName != null && !fluidName.isEmpty();
boolean expand = sneaking && hasFluid;
int barHeight = expand ? 12 : 8;

ci.cancel();

// Update sprite if needed
if (hasFluid && labs$sprite == null) {
Fluid fluid = FluidRegistry.getFluid(fluidName);
if (fluid != null) {
labs$sprite = Minecraft.getMinecraft().getTextureMapBlocks()
.getAtlasSprite(fluid.getStill().toString());
}
if (fluid == null)
NomiLabs.LOGGER.error("Received Fluid Info Packet ElementTankGauge with Unknown Fluid {}!", fluidName);
else
labs$sprite = LabsFluidStackElement.getFluidAtlasSprite(fluid.getStill().toString());
}

// Box
int borderColor = hasFluid ? color2 : 0xff969696;
RenderHelper.drawThickBeveledBox(x, y, x + 100, y + barHeight, 1, borderColor, borderColor, 0x44969696);

// Render fluid
// Render fluid (Adaptation of RenderUtil#drawFluidForGui)
if (hasFluid && labs$sprite != null) {
GlStateManager.enableBlend();
Minecraft.getMinecraft().renderEngine.bindTexture(TextureMap.LOCATION_BLOCKS_TEXTURE);

RenderUtil.setGlColorFromInt(color1, 0xFF);

final int scaledAmount = (int) ((long) amount * 98 / capacity);

final int xTileCount = scaledAmount / 16;
final int xRemainder = scaledAmount - xTileCount * 16;

for (int xTile = 0; xTile <= xTileCount; xTile++) {
int width = xTile == xTileCount ? xRemainder : 16;
int fluidX = x + 1 + (xTile + 1) * 16 - 16;
if (width > 0) {
int maskTop = 16 - barHeight + 2;
int maskRight = 16 - width;

RenderUtil.drawFluidTexture(fluidX, y - 16 + barHeight - 1, labs$sprite, maskTop, maskRight,
0.0);
}
}

GlStateManager.disableBlend();
labs$renderFluidTexture(x, y, barHeight);
}

for (int i = 1; i < 10; i++) {
Expand All @@ -132,8 +114,6 @@ private void newRenderLogic(int x, int y, CallbackInfo ci) {
} else {
drawSmallText(x + 2, y + 2, labs$getTranslatedTankName(), 0xffffffff);
}

ci.cancel();
}

@Inject(method = "getWidth", at = @At("HEAD"), cancellable = true)
Expand All @@ -151,6 +131,33 @@ private void newHeightLogic(CallbackInfoReturnable<Integer> cir) {
cir.setReturnValue(8);
}

@Unique
private void labs$renderFluidTexture(int x, int y, int barHeight) {
GlStateManager.enableBlend();
Minecraft.getMinecraft().renderEngine.bindTexture(TextureMap.LOCATION_BLOCKS_TEXTURE);

RenderUtil.setGlColorFromInt(color1, 0xFF);

int scaledAmount = (int) ((long) amount * 98 / capacity);

int xTileCount = scaledAmount / 16;
int xRemainder = scaledAmount - xTileCount * 16;

for (int xTile = 0; xTile <= xTileCount; xTile++) {
int width = xTile == xTileCount ? xRemainder : 16;
int fluidX = x + 1 + (xTile + 1) * 16 - 16;
if (width > 0) {
int maskTop = 16 - barHeight + 2;
int maskRight = 16 - width;

RenderUtil.drawFluidTexture(fluidX, y - 16 + barHeight - 1, labs$sprite, maskTop, maskRight,
0.0);
}
}

GlStateManager.disableBlend();
}

@Unique
private String labs$getTankFluidTitle() {
if (labs$tankFluidTitle != null) return labs$tankFluidTitle;
Expand Down

0 comments on commit e30bc82

Please sign in to comment.