Skip to content

Commit

Permalink
mekanism: basics
Browse files Browse the repository at this point in the history
  • Loading branch information
deirn committed Jul 26, 2024
1 parent 53276b6 commit 1780be4
Show file tree
Hide file tree
Showing 11 changed files with 405 additions and 13 deletions.
4 changes: 2 additions & 2 deletions buildSrc/src/main/kotlin/FetchLatestVersionsTask.kt
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,8 @@ abstract class FetchLatestVersionsTask : DefaultTask() {
}

fetcher(ModrinthVersionFetcher, "forge", forge.mekanism) {
fetch(forge.mekanism::core, mrIds.mekCore) { "mekanism:Mekanism:1.20.1-${it.version_number}" }
fetch(forge.mekanism::generators, mrIds.mekCore) { "mekanism:Mekanism:1.20.1-${it.version_number}:generators" }
fetch(forge.mekanism::core, mrIds.mekCore) { "mekanism:Mekanism:${versions.minecraft}-${it.version_number}" }
fetch(forge.mekanism::generators, mrIds.mekCore) { "mekanism:Mekanism:${versions.minecraft}-${it.version_number}:generators" }
}

fetcher(ModrinthVersionFetcher, "forge", forge.thermal) {
Expand Down
16 changes: 8 additions & 8 deletions dependencies.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"fabric.wthit.api" : "mcp.mobius.waila:wthit-api:fabric-5.28.0",
"fabric.wthit.runtime" : "mcp.mobius.waila:wthit:fabric-5.28.0",
"fabric.wthit.api" : "mcp.mobius.waila:wthit-api:fabric-5.28.2",
"fabric.wthit.runtime" : "mcp.mobius.waila:wthit:fabric-5.28.2",
"fabric.badpackets" : "lol.bai:badpackets:fabric-0.2.3",
"fabric.ae2" : "appeng:appliedenergistics2-fabric:12.9.9",
"fabric.alloyForge" : "maven.modrinth:jhl28YkY:q1L7hbA8",
Expand All @@ -26,21 +26,21 @@
"fabric.wirelessNet" : "curse.maven:cursemod-461316:3876132",
"fabric.lapisReserve" : "curse.maven:cursemod-399679:3345208",
"fabric.resourceChickens" : "curse.api:632145:4985700",
"forge.wthit.api" : "mcp.mobius.waila:wthit-api:forge-5.28.0",
"forge.wthit.runtime" : "mcp.mobius.waila:wthit:forge-5.28.0",
"forge.wthit.api" : "mcp.mobius.waila:wthit-api:forge-5.28.2",
"forge.wthit.runtime" : "mcp.mobius.waila:wthit:forge-5.28.2",
"forge.badpackets" : "lol.bai:badpackets:forge-0.2.3",
"forge.ae2" : "appeng:appliedenergistics2-forge:12.9.9",
"forge.create" : "maven.modrinth:LNytGWDc:Vfzp1Xaz",
"forge.ie" : "maven.modrinth:tIm2nV03:VzTXQKEm",
"forge.rs" : "maven.modrinth:KDvYkUg3:q3LiZwUb",
"forge.jei" : "maven.modrinth:u6dRKJwZ:2vJsVMlk",
"forge.mekanism.core" : "mekanism:Mekanism:1.20.1-10.3.9.13",
"forge.mekanism.generators" : "mekanism:Mekanism:1.20.1-10.3.9.13:generators",
"forge.jei" : "maven.modrinth:u6dRKJwZ:8HIXqeHh",
"forge.mekanism.core" : "mekanism:Mekanism:1.19.2-10.3.9.13",
"forge.mekanism.generators" : "mekanism:Mekanism:1.19.2-10.3.9.13:generators",
"forge.thermal.cofhCore" : "maven.modrinth:OWSRM4vD:ssRHxD6e",
"forge.thermal.foundation" : "maven.modrinth:Xvg6q5Wp:kaIOIjDc",
"forge.thermal.expansion" : "maven.modrinth:hmD6rrUJ:W9opx6mY",
"forge.lapisReserve" : "curse.maven:cursemod-399679:3787247",
"forge.resourceChickens" : "curse.api:632145:5088913",
"forge.resourceChickens" : "curse.api:632145:5512583",
"forge.productiveBees" : "curse.maven:cursemod-377897:4566382",
"forge.top" : "curse.maven:cursemod-245211:3965693"
}
12 changes: 10 additions & 2 deletions modules/forge/mekanism/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
metadata("lol.bai.megane.module.mekanism") {
waila("MeganeMekaninsm")
waila("MeganeMekaninsm") {
required("mekanism")
}

mixin {
callback(mixin::add, mixin::config)
require(forgeMod("mekanism"))
}

forgeFml()
packMcmeta()
language()

modsToml {
depends(
Expand All @@ -16,5 +25,4 @@ repositories {

dependencies {
implementation(fg.deobf(deps.forge.mekanism.core))
implementation(fg.deobf(deps.forge.mekanism.generators))
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,43 @@
package lol.bai.megane.module.mekanism;

import lol.bai.megane.module.mekanism.provider.ChemicalProvider;
import lol.bai.megane.module.mekanism.provider.FluidProvider;
import lol.bai.megane.module.mekanism.provider.MultiblockProvider;
import lol.bai.megane.module.mekanism.provider.StrictEnergyProvider;
import mcp.mobius.waila.api.IRegistrar;
import mcp.mobius.waila.api.IWailaPlugin;
import mcp.mobius.waila.api.TooltipPosition;
import mcp.mobius.waila.api.data.EnergyData;
import mekanism.common.lib.multiblock.IMultiblockBase;
import mekanism.common.tile.base.TileEntityUpdateable;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.level.block.entity.BlockEntity;

public class MeganeMekaninsm implements IWailaPlugin {

public static final ResourceLocation CONFIG_SHOW_CHEMICALS = id("chemical");

public static ResourceLocation id(String path) {
return new ResourceLocation("megane", "mekanism." + path);
}

@Override
public void register(IRegistrar registrar) {
EnergyData.describe("mekanism").color(0x3CFE9A);

registrar.addFeatureConfig(CONFIG_SHOW_CHEMICALS, false);
registrar.addDataType(ChemicalProvider.DATA, ChemicalProvider.Data.class, ChemicalProvider.Data::new);
registrar.addBlockData(new ChemicalProvider.Blocker(), BlockEntity.class, 0);
var chemicalProvider = new ChemicalProvider();
registrar.addBlockData(chemicalProvider, BlockEntity.class, 1500);
registrar.addComponent(chemicalProvider, TooltipPosition.BODY, BlockEntity.class, 600);

registrar.addBlockData(new StrictEnergyProvider(), BlockEntity.class, 1500);

var fluidProvider = new FluidProvider();
registrar.addBlockData(fluidProvider, TileEntityUpdateable.class);

registrar.addBlockData(new MultiblockProvider(fluidProvider), IMultiblockBase.class, 900);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package lol.bai.megane.module.mekanism.mixin;

import java.util.List;
import java.util.function.Function;

import mekanism.api.chemical.Chemical;
import mekanism.api.chemical.ChemicalStack;
import mekanism.api.chemical.IChemicalHandler;
import mekanism.api.chemical.IChemicalTank;
import mekanism.api.chemical.merged.MergedChemicalTank;
import mekanism.api.text.ILangEntry;
import mekanism.common.capabilities.merged.MergedTank;
import mekanism.common.integration.lookingat.LookingAtHelper;
import mekanism.common.integration.lookingat.LookingAtUtils;
import mekanism.common.lib.multiblock.MultiblockData;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.fluids.capability.IFluidHandler;
import org.jetbrains.annotations.Nullable;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.gen.Invoker;

@Mixin(LookingAtUtils.class)
public interface AccessLookingAtUtils {

@Invoker("getMultiblock")
static @Nullable MultiblockData megane_getMultiblock(BlockEntity tile) {
throw new AssertionError("mixin");
}

@Invoker("displayFluid")
static void megane_displayFluid(LookingAtHelper info, IFluidHandler fluidHandler) {

}

@Invoker("addInfo")
static <
CHEMICAL extends Chemical<CHEMICAL>,
STACK extends ChemicalStack<CHEMICAL>,
TANK extends IChemicalTank<CHEMICAL, STACK>,
HANDLER extends IChemicalHandler<CHEMICAL, STACK>
>
void megane_addInfo(
BlockEntity tile,
@Nullable MultiblockData structure,
Capability<HANDLER> capability,
Function<MultiblockData, List<TANK>> multiBlockToTanks,
LookingAtHelper info,
ILangEntry langEntry,
MergedChemicalTank.Current matchingCurrent,
MergedTank.CurrentType matchingCurrentType) {
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package lol.bai.megane.module.mekanism.mixin;

import mcp.mobius.waila.api.IRegistrar;
import mekanism.common.integration.lookingat.wthit.MekanismWTHITPlugin;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

@Mixin(MekanismWTHITPlugin.class)
public class MixinMekanismWTHITPlugin {

@Unique
private static final Logger megane_LOGGER = LoggerFactory.getLogger("MixinMekanismWTHITPlugin");

@Inject(method = "register", at = @At("HEAD"), remap = false, cancellable = true)
private void megane_disable(IRegistrar registration, CallbackInfo ci) {
megane_LOGGER.info("[megane-mekanism] Disabled Mekanism's builtin WTHIT compatibility in favor of Megane's");
ci.cancel();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
package lol.bai.megane.module.mekanism.provider;

import java.util.ArrayList;
import java.util.List;

import lol.bai.megane.module.mekanism.MeganeMekaninsm;
import lol.bai.megane.module.mekanism.mixin.AccessLookingAtUtils;
import mcp.mobius.waila.api.IBlockAccessor;
import mcp.mobius.waila.api.IBlockComponentProvider;
import mcp.mobius.waila.api.IData;
import mcp.mobius.waila.api.IDataProvider;
import mcp.mobius.waila.api.IDataWriter;
import mcp.mobius.waila.api.IPluginConfig;
import mcp.mobius.waila.api.IServerAccessor;
import mcp.mobius.waila.api.ITooltip;
import mcp.mobius.waila.api.WailaHelper;
import mcp.mobius.waila.api.component.PairComponent;
import mcp.mobius.waila.api.component.SpriteBarComponent;
import mcp.mobius.waila.api.component.WrappedComponent;
import mcp.mobius.waila.api.data.FluidData;
import mcp.mobius.waila.api.data.FluidData.Unit;
import mekanism.api.chemical.ChemicalStack;
import mekanism.api.chemical.ChemicalType;
import mekanism.api.chemical.ChemicalUtils;
import mekanism.api.chemical.merged.MergedChemicalTank;
import mekanism.api.math.FloatingLong;
import mekanism.client.render.MekanismRenderer;
import mekanism.common.MekanismLang;
import mekanism.common.capabilities.Capabilities;
import mekanism.common.capabilities.merged.MergedTank;
import mekanism.common.integration.lookingat.LookingAtHelper;
import mekanism.common.lib.multiblock.MultiblockData;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraftforge.fluids.FluidStack;
import org.jetbrains.annotations.Nullable;

public class ChemicalProvider implements IBlockComponentProvider, IDataProvider<BlockEntity> {

public static final ResourceLocation DATA = MeganeMekaninsm.id("chemical");

private static final String INFINITE = "∞";

@Override
public void appendBody(ITooltip tooltip, IBlockAccessor accessor, IPluginConfig config) {
var data = accessor.getData().get(Data.class);
if (data == null || !config.getBoolean(MeganeMekaninsm.CONFIG_SHOW_CHEMICALS)) return;

var displayUnit = config.<Unit>getEnum(FluidData.CONFIG_DISPLAY_UNIT);

for (var tank : data.tanks) {
var stack = tank.stack;
if (stack.isEmpty()) continue;

var stored = stack.getAmount();
var capacity = tank.capacity;
var ratio = Double.isInfinite(capacity) ? 1f : (float) stored / capacity;

String text;
if (Double.isInfinite(stored)) text = INFINITE;
else {
text = WailaHelper.suffix((long) Unit.convert(Unit.MILLIBUCKETS, displayUnit, stored));
if (Double.isFinite(capacity)) text += "/" + WailaHelper.suffix((long) Unit.convert(Unit.MILLIBUCKETS, displayUnit, capacity));
}

text += " " + displayUnit.symbol;

var sprite = MekanismRenderer.getChemicalTexture(stack.getType());
tooltip.addLine(new PairComponent(
new WrappedComponent(stack.getTextComponent()),
new SpriteBarComponent(ratio, sprite, 16, 16, stack.getChemicalTint(), Component.literal(text))));
}
}

@Override
public void appendData(IDataWriter data, IServerAccessor<BlockEntity> accessor, IPluginConfig config) {
data.add(Data.class, res -> addChemicals(res, accessor.getTarget(), null));
}

public static void addChemicals(IDataWriter.Result<Data> res, BlockEntity tile, @Nullable MultiblockData structure) {
var info = new Data(new ArrayList<>());
AccessLookingAtUtils.megane_addInfo(tile, structure, Capabilities.GAS_HANDLER, multiblock -> multiblock.getGasTanks(null), info, MekanismLang.GAS, MergedChemicalTank.Current.GAS, MergedTank.CurrentType.GAS);
AccessLookingAtUtils.megane_addInfo(tile, structure, Capabilities.INFUSION_HANDLER, multiblock -> multiblock.getInfusionTanks(null), info, MekanismLang.INFUSE_TYPE, MergedChemicalTank.Current.INFUSION, MergedTank.CurrentType.INFUSION);
AccessLookingAtUtils.megane_addInfo(tile, structure, Capabilities.PIGMENT_HANDLER, multiblock -> multiblock.getPigmentTanks(null), info, MekanismLang.PIGMENT, MergedChemicalTank.Current.PIGMENT, MergedTank.CurrentType.PIGMENT);
AccessLookingAtUtils.megane_addInfo(tile, structure, Capabilities.SLURRY_HANDLER, multiblock -> multiblock.getSlurryTanks(null), info, MekanismLang.SLURRY, MergedChemicalTank.Current.SLURRY, MergedTank.CurrentType.SLURRY);
if (!info.tanks.isEmpty()) res.add(info);
}

public static final class Blocker implements IDataProvider<BlockEntity> {

@Override
public void appendData(IDataWriter data, IServerAccessor<BlockEntity> accessor, IPluginConfig config) {
if (!config.getBoolean(MeganeMekaninsm.CONFIG_SHOW_CHEMICALS)) data.blockAll(Data.class);
}

}

public record Data(List<Tank> tanks) implements IData, LookingAtHelper {

public record Tank(ChemicalStack<?> stack, long capacity) {

}

public Data(FriendlyByteBuf buf) {
this(buf.readList(b -> {
var type = b.readEnum(ChemicalType.class);
var stack = switch (type) {
case GAS -> ChemicalUtils.readGasStack(b);
case INFUSION -> ChemicalUtils.readInfusionStack(b);
case PIGMENT -> ChemicalUtils.readPigmentStack(b);
case SLURRY -> ChemicalUtils.readSlurryStack(b);
};
var capacity = b.readVarLong();
return new Tank(stack, capacity);
}));
}

@Override
public void write(FriendlyByteBuf buf) {
buf.writeCollection(tanks, (b, t) -> {
b.writeEnum(ChemicalType.getTypeFor(t.stack));
ChemicalUtils.writeChemicalStack(b, t.stack);
b.writeVarLong(t.capacity);
});
}

@Override
public void addChemicalElement(ChemicalStack<?> stored, long capacity) {
tanks.add(new Tank(stored, capacity));
}

@Override
public void addText(Component text) {
}

@Override
public void addEnergyElement(FloatingLong energy, FloatingLong maxEnergy) {
}

@Override
public void addFluidElement(FluidStack stored, int capacity) {
}

}

}
Loading

0 comments on commit 1780be4

Please sign in to comment.