Skip to content

Commit

Permalink
Migrate from Megane to WTHIT API (#516)
Browse files Browse the repository at this point in the history
* migrate from megane to wthit api

* fix storage tooltip missing rom fluid to energy gen and large tank
make pipe energy color match machine energy color

* no need to bootstrap now

* yes
  • Loading branch information
deirn authored Jul 30, 2023
1 parent 4e58e88 commit 37798a6
Show file tree
Hide file tree
Showing 42 changed files with 344 additions and 942 deletions.
29 changes: 8 additions & 21 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@ repositories {
}
// for WTHIT
maven {
url 'https://maven.bai.lol'
url "https://maven2.bai.lol"
content {
includeGroup "lol.bai"
includeGroup "mcp.mobius.waila"
}
}
// for AE2 and JEI
maven {
Expand Down Expand Up @@ -209,9 +213,9 @@ dependencies {
exclude(group: "net.fabricmc.fabric-api")
}

modImplementation("mcp.mobius.waila:wthit:fabric-${project.wthit_version}") {
exclude(group: "net.fabricmc.fabric-api")
}
modCompileOnly "mcp.mobius.waila:wthit-api:fabric-${project.wthit_version}"
modRuntimeOnly "mcp.mobius.waila:wthit:fabric-${project.wthit_version}"
modRuntimeOnly "lol.bai:badpackets:fabric-${project.badpackets_version}"

modImplementation("com.terraformersmc:modmenu:${project.mod_menu_version}") {
exclude(group: "net.fabricmc.fabric-api")
Expand Down Expand Up @@ -239,21 +243,6 @@ dependencies {
// Include No Indium? https://github.com/Luligabi1/NoIndium
include "me.luligabi:NoIndium:${project.no_indium_version}"


modImplementation("lol.bai.megane:megane-api:${project.megane_api_version}") {
exclude(group: "net.fabricmc.fabric-api")
}

modRuntimeOnly("lol.bai.megane:megane-runtime:${project.megane_runtime_version}") {
exclude(group: "net.fabricmc.fabric-api")
exclude(group: "mcp.mobius.waila")
}

modRuntimeOnly("lol.bai.megane:megane-vanilla:${project.megane_vanilla_version}") {
exclude(group: "net.fabricmc.fabric-api")
exclude(group: "mcp.mobius.waila")
}

modCompileOnly('net.oskarstrom:DashLoader:2.1-dev4') {
exclude(group: "net.fabricmc.fabric-api")
}
Expand Down Expand Up @@ -453,7 +442,6 @@ if (System.getenv("CURSEFORGE_API_KEY") && !version.endsWith("-SNAPSHOT")) {
requiredDependency "fabric-api"
requiredDependency "cloth-config"
optionalDependency "wthit"
optionalDependency "megane"
optionalDependency "emi"
optionalDependency "jei"
optionalDependency "roughly-enough-items"
Expand Down Expand Up @@ -489,7 +477,6 @@ modrinth {
required.project "fabric-api"
required.project "cloth-config"
optional.project "wthit"
optional.project "megane"
optional.project "emi"
optional.project "jei"
optional.project "rei"
Expand Down
6 changes: 2 additions & 4 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,12 @@ org.gradle.jvmargs=-Xmx1G \
emi_version=1.0.4
patchouli_version=1.19.2-77-FABRIC
cloth_config_version=7.0.74
wthit_version=5.12.0
wthit_version=5.17.0
badpackets_version=0.2.1
mod_menu_version=4.1.2
tr_energy_version=2.3.0
magna_version=1.8.1-1.19
pal_version=1.6.0
megane_api_version=8.1.0
megane_runtime_version=8.1.0
megane_vanilla_version=8.1.0
ae2_version=12.9.5
no_indium_version=1.1.0+1.19
ftb_quests_version=1902.4.16-build.235
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,35 @@
*/
package aztech.modern_industrialization.compat.waila.client;

import aztech.modern_industrialization.ModernIndustrialization;
import aztech.modern_industrialization.machines.MachineBlockEntity;
import aztech.modern_industrialization.pipes.impl.PipeBlockEntity;
import mcp.mobius.waila.api.IRegistrar;
import mcp.mobius.waila.api.IWailaPlugin;
import mcp.mobius.waila.api.TooltipPosition;
import mcp.mobius.waila.api.WailaHelper;
import mcp.mobius.waila.api.data.EnergyData;

public class MIWailaClientPlugin implements IWailaPlugin {
public static final int ENERGY_COLOR = 0xFFB70000;

@Override
public void register(IRegistrar r) {
EnergyData.describe(ModernIndustrialization.MOD_ID).color(ENERGY_COLOR).unit("EU");

PipeComponentProvider pipeComponentProvider = new PipeComponentProvider();
r.addComponent(pipeComponentProvider, TooltipPosition.HEAD, PipeBlockEntity.class);
r.addComponent(pipeComponentProvider, TooltipPosition.BODY, PipeBlockEntity.class);

OverclockComponentProvider overclockComponentProvider = new OverclockComponentProvider();
r.addComponent(overclockComponentProvider, TooltipPosition.BODY, MachineBlockEntity.class);
}

static float ratio(double current, double max) {
return (float) (current / max);
}

static String fraction(double current, double max) {
return WailaHelper.suffix((long) current) + "/" + WailaHelper.suffix((long) max);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@
package aztech.modern_industrialization.compat.waila.client;

import aztech.modern_industrialization.MIText;
import aztech.modern_industrialization.compat.waila.client.component.BarComponent;
import mcp.mobius.waila.api.IBlockAccessor;
import mcp.mobius.waila.api.IBlockComponentProvider;
import mcp.mobius.waila.api.IPluginConfig;
import mcp.mobius.waila.api.ITooltip;
import mcp.mobius.waila.api.component.BarComponent;
import mcp.mobius.waila.api.component.PairComponent;
import mcp.mobius.waila.api.component.WrappedComponent;
import net.minecraft.nbt.CompoundTag;
Expand All @@ -37,8 +37,7 @@ public class OverclockComponentProvider implements IBlockComponentProvider {

@Override
public void appendBody(ITooltip tooltip, IBlockAccessor accessor, IPluginConfig config) {

CompoundTag tag = accessor.getServerData();
CompoundTag tag = accessor.getData().raw();
if (tag.contains("efficiencyTicks") && tag.contains("maxEfficiencyTicks") && tag.contains("baseRecipeEu")
&& tag.contains("currentRecipeEu")) {

Expand All @@ -49,14 +48,13 @@ public void appendBody(ITooltip tooltip, IBlockAccessor accessor, IPluginConfig

double mult = (double) currentEu / baseRecipeEu;

tooltip.addLine(
new PairComponent(
new WrappedComponent(MIText.Efficiency.text()),
new BarComponent(0x61c928, efficiencyTicks, maxEfficiencyTicks, "", false, true)));
tooltip.addLine(
new WrappedComponent(MIText.EuTOverclocked.text(
String.format("%.1f", mult),
String.format("%d", currentEu))));
tooltip.addLine(new PairComponent(
new WrappedComponent(MIText.Efficiency.text()),
new BarComponent(MIWailaClientPlugin.ratio(efficiencyTicks, maxEfficiencyTicks), 0xFF61C928,
MIWailaClientPlugin.fraction(efficiencyTicks, maxEfficiencyTicks))));

tooltip.addLine(new WrappedComponent(MIText.EuTOverclocked.text(
String.format("%.1f", mult), String.format("%d", currentEu))));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,35 +25,36 @@

import aztech.modern_industrialization.MIText;
import aztech.modern_industrialization.MITooltips;
import aztech.modern_industrialization.compat.waila.client.component.BarComponent;
import aztech.modern_industrialization.compat.waila.client.component.CenteredTextComponent;
import aztech.modern_industrialization.pipes.MIPipes;
import aztech.modern_industrialization.pipes.impl.PipeBlockEntity;
import aztech.modern_industrialization.pipes.impl.PipeVoxelShape;
import aztech.modern_industrialization.pipes.item.ItemNetwork;
import aztech.modern_industrialization.util.FluidHelper;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.List;
import lol.bai.megane.api.provider.FluidInfoProvider;
import mcp.mobius.waila.api.*;
import java.util.Objects;
import mcp.mobius.waila.api.IBlockAccessor;
import mcp.mobius.waila.api.IBlockComponentProvider;
import mcp.mobius.waila.api.IPluginConfig;
import mcp.mobius.waila.api.ITooltip;
import mcp.mobius.waila.api.IWailaConfig;
import mcp.mobius.waila.api.WailaConstants;
import mcp.mobius.waila.api.component.BarComponent;
import mcp.mobius.waila.api.component.PairComponent;
import mcp.mobius.waila.api.component.SpriteBarComponent;
import mcp.mobius.waila.api.component.WrappedComponent;
import net.fabricmc.fabric.api.transfer.v1.client.fluid.FluidVariantRendering;
import net.fabricmc.fabric.api.transfer.v1.fluid.FluidVariant;
import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.client.resources.language.I18n;
import net.minecraft.core.BlockPos;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.chat.*;
import net.minecraft.world.level.material.Fluid;
import net.minecraft.network.chat.Component;
import net.minecraft.world.level.material.Fluids;
import net.minecraft.world.phys.AABB;
import net.minecraft.world.phys.Vec3;
import org.jetbrains.annotations.Nullable;

/**
* Overrides the name of the pipes in Waila to prevent
* "block.modern_industrialization.pipe" being displayed.
* Overrides the name of the pipes in Waila to prevent "block.modern_industrialization.pipe" being displayed.
*/
public class PipeComponentProvider implements IBlockComponentProvider {

Expand Down Expand Up @@ -88,42 +89,45 @@ public void appendHead(ITooltip tooltip, IBlockAccessor accessor, IPluginConfig
public void appendBody(ITooltip tooltip, IBlockAccessor accessor, IPluginConfig config) {
PipeVoxelShape shape = getHitShape(accessor);
if (shape != null) {
CompoundTag tag = accessor.getServerData().getCompound(shape.type.getIdentifier().toString());
CompoundTag tag = accessor.getData().raw().getCompound(shape.type.getIdentifier().toString());

if (tag.contains("fluid")) {
FluidVariant fluid = FluidVariant.fromNbt(tag.getCompound("fluid"));
long stored = tag.getLong("amount");
long capacity = tag.getInt("capacity");
long transfer = tag.getLong("transfer");
long maxTransfer = tag.getLong("maxTransfer");
double stored = tag.getLong("amount") / 81.0;
double capacity = tag.getInt("capacity") / 81.0;
double transfer = tag.getLong("transfer") / 81.0;
double maxTransfer = tag.getLong("maxTransfer") / 81.0;

if (fluid.isBlank()) {
// Show "Empty"
tooltip.addLine(MIText.Empty.text());
} else {
var fluidName = FluidHelper.getFluidName(fluid, true);
var sprite = FluidVariantRendering.getSprite(fluid);
int color = FluidVariantRendering.getColor(fluid);

if (FabricLoader.getInstance().isModLoaded("megane-runtime")) {
color = improveFluidColor(accessor, color, fluid);
if (sprite == null) {
sprite = Objects.requireNonNull(FluidVariantRendering.getSprite(FluidVariant.of(Fluids.WATER)));
}

// Total fluid
tooltip.addLine(new PairComponent(
new WrappedComponent(MIText.NetworkAmount.text()),
new BarComponent(color, stored / 81.0, capacity / 81.0, "mb", false)));
new SpriteBarComponent(MIWailaClientPlugin.ratio(stored, capacity), sprite.atlas().location(),
sprite.getU0(), sprite.getU1(), sprite.getV0(), sprite.getV1(), 16, 16, color,
Component.literal(MIWailaClientPlugin.fraction(stored, capacity) + " mB"))));

// Which fluid
tooltip.addLine(new PairComponent(
new WrappedComponent(MIText.NetworkFluid.text()),
new CenteredTextComponent(
fluidName)));
new CenteredTextComponent(fluidName)));

// Transfer rate
tooltip.addLine(new PairComponent(
new WrappedComponent(MIText.NetworkTransfer.text()),
new BarComponent(color, transfer / 81.0, maxTransfer / 81.0, "mb/t", false)));

new SpriteBarComponent(MIWailaClientPlugin.ratio(transfer, maxTransfer), sprite.atlas().location(),
sprite.getU0(), sprite.getU1(), sprite.getV0(), sprite.getV1(), 16, 16, color,
Component.literal(MIWailaClientPlugin.fraction(transfer, maxTransfer) + " mB/t"))));
}
}

Expand All @@ -136,7 +140,8 @@ public void appendBody(ITooltip tooltip, IBlockAccessor accessor, IPluginConfig
// Total EU
tooltip.addLine(new PairComponent(
new WrappedComponent(MIText.NetworkEnergy.text()),
new BarComponent(0x710C00, stored, capacity, "EU", false)));
new BarComponent(MIWailaClientPlugin.ratio(stored, capacity), MIWailaClientPlugin.ENERGY_COLOR,
MIWailaClientPlugin.fraction(stored, capacity) + " EU")));

// Voltage tier
tooltip.addLine(new PairComponent(
Expand All @@ -148,17 +153,23 @@ public void appendBody(ITooltip tooltip, IBlockAccessor accessor, IPluginConfig
// EU/t
tooltip.addLine(new PairComponent(
new WrappedComponent(MIText.NetworkTransfer.text()),
new BarComponent(0x710C00, transfer, maxTransfer, "EU/t", false)));
new BarComponent(
MIWailaClientPlugin.ratio(transfer, maxTransfer), MIWailaClientPlugin.ENERGY_COLOR,
MIWailaClientPlugin.fraction(transfer, maxTransfer) + " EU/t")));
}

if (tag.contains("items")) {
long items = tag.getLong("items");
int pulse = tag.getInt("pulse");

double delay = (ItemNetwork.TICK_RATE - pulse) / 20.0;
double maxDelay = ItemNetwork.TICK_RATE / 20.0;

// Delay
tooltip.addLine(new PairComponent(
new WrappedComponent(MIText.NetworkDelay.text()),
new BarComponent(0xFFD14A, (ItemNetwork.TICK_RATE - pulse) / 20.0, ItemNetwork.TICK_RATE / 20.0, "s", false)));
new BarComponent(MIWailaClientPlugin.ratio(delay, maxDelay), 0xFFFFD14A,
MIWailaClientPlugin.fraction(delay, maxDelay) + " s")));

// Moved items
tooltip.addLine(new PairComponent(
Expand All @@ -168,28 +179,4 @@ public void appendBody(ITooltip tooltip, IBlockAccessor accessor, IPluginConfig
}
}

/**
* Try to find a better color if possible by reflecting into megane's internals ;-)
*/
private static int improveFluidColor(IBlockAccessor accessor, int color, FluidVariant fluid) {
try {
Class<?> registrarClass = Class.forName("lol.bai.megane.runtime.registry.Registrar");
Field fluidInfoField = registrarClass.getField("FLUID_INFO");
fluidInfoField.setAccessible(true);
Object registry = fluidInfoField.get(null);
Method getMethod = registry.getClass().getMethod("get", Object.class);
List<FluidInfoProvider<Fluid>> providers = (List<FluidInfoProvider<Fluid>>) getMethod.invoke(registry, fluid.getFluid());

for (var provider : providers) {
provider.setContext(accessor.getWorld(), accessor.getPosition(), accessor.getHitResult(), accessor.getPlayer(), fluid.getFluid());
provider.setFluidInfoContext(fluid.getNbt());
if (provider.hasFluidInfo()) {
return provider.getColor();
}
}
} catch (Exception ignored) {
}

return color;
}
}
Loading

0 comments on commit 37798a6

Please sign in to comment.