Skip to content

Commit

Permalink
mekanism: progress and security
Browse files Browse the repository at this point in the history
  • Loading branch information
deirn committed Sep 8, 2024
1 parent 1780be4 commit 8cc29cd
Show file tree
Hide file tree
Showing 10 changed files with 189 additions and 11 deletions.
12 changes: 6 additions & 6 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.2",
"fabric.wthit.runtime" : "mcp.mobius.waila:wthit:fabric-5.28.2",
"fabric.wthit.api" : "mcp.mobius.waila:wthit-api:fabric-5.29.0",
"fabric.wthit.runtime" : "mcp.mobius.waila:wthit:fabric-5.29.0",
"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 @@ -9,7 +9,7 @@
"fabric.clothConfig" : "me.shedaniel.cloth:cloth-config-fabric:8.3.115",
"fabric.extraGen" : "maven.modrinth:VXtwLg17:2p7qWneI",
"fabric.fabricApi" : "net.fabricmc.fabric-api:fabric-api:0.77.0+1.19.2",
"fabric.flk" : "net.fabricmc:fabric-language-kotlin:1.11.0+kotlin.2.0.0",
"fabric.flk" : "net.fabricmc:fabric-language-kotlin:1.12.1+kotlin.2.0.20",
"fabric.kibe" : "maven.modrinth:OvlwmUdC:6GPdTrdx",
"fabric.modernDynamics" : "maven.modrinth:fMpvLrnF:uSorcoNB",
"fabric.modmenu" : "com.terraformersmc:modmenu:4.2.0-beta.2",
Expand All @@ -25,9 +25,9 @@
"fabric.techReborn" : "TechReborn:TechReborn-1.19:5.4.0",
"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.2",
"forge.wthit.runtime" : "mcp.mobius.waila:wthit:forge-5.28.2",
"fabric.resourceChickens" : "curse.api:632145:5632040",
"forge.wthit.api" : "mcp.mobius.waila:wthit-api:forge-5.29.0",
"forge.wthit.runtime" : "mcp.mobius.waila:wthit:forge-5.29.0",
"forge.badpackets" : "lol.bai:badpackets:forge-0.2.3",
"forge.ae2" : "appeng:appliedenergistics2-forge:12.9.9",
"forge.create" : "maven.modrinth:LNytGWDc:Vfzp1Xaz",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public void appendData(IDataWriter data, IServerAccessor<ResourceChickenEntity>
}

if (config.getBoolean(MeganeResourceChickens.CONFIG_SHOW_CONVERSION) && chicken.conversionProgress != 0) {
data.addImmediate(new Mutation(chicken.conversionProgress, chicken.conversionType));
data.addImmediate(new Mutation(chicken.conversionProgress, chicken.chickenDataCONV.ID));
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,29 @@
package lol.bai.megane.module.mekanism;

import lol.bai.megane.module.mekanism.provider.ChemicalProvider;
import lol.bai.megane.module.mekanism.provider.FactoryProvider;
import lol.bai.megane.module.mekanism.provider.FluidProvider;
import lol.bai.megane.module.mekanism.provider.MultiblockProvider;
import lol.bai.megane.module.mekanism.provider.SecurityProvider;
import lol.bai.megane.module.mekanism.provider.StrictEnergyProvider;
import mcp.mobius.waila.api.IBlockComponentProvider;
import mcp.mobius.waila.api.IEntityComponentProvider;
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 mekanism.common.tile.factory.TileEntityFactory;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.entity.BlockEntity;

public class MeganeMekaninsm implements IWailaPlugin {

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

public static ResourceLocation id(String path) {
return new ResourceLocation("megane", "mekanism." + path);
Expand All @@ -25,10 +33,10 @@ public static ResourceLocation id(String path) {
public void register(IRegistrar registrar) {
EnergyData.describe("mekanism").color(0x3CFE9A);

var chemicalProvider = new ChemicalProvider();
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);

Expand All @@ -38,6 +46,16 @@ public void register(IRegistrar registrar) {
registrar.addBlockData(fluidProvider, TileEntityUpdateable.class);

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

registrar.addBlockData(new FactoryProvider(), TileEntityFactory.class);

var securityProvider = new SecurityProvider<>();
registrar.addFeatureConfig(CONFIG_SHOW_SECURITY, false);
registrar.addDataType(SecurityProvider.DATA, SecurityProvider.Data.class, SecurityProvider.Data::new);
registrar.addComponent((IBlockComponentProvider) securityProvider, TooltipPosition.BODY, Block.class);
registrar.addComponent((IEntityComponentProvider) securityProvider, TooltipPosition.BODY, Entity.class);
registrar.addBlockData(new SecurityProvider<>(), BlockEntity.class);
registrar.addEntityData(new SecurityProvider<>(), Entity.class);
}

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

import mekanism.common.inventory.container.slot.ContainerSlotType;
import mekanism.common.inventory.slot.BasicInventorySlot;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.gen.Invoker;

@Mixin(BasicInventorySlot.class)
public interface AccessBasicInventorySlot {

@Invoker("getSlotType")
ContainerSlotType megane_getSlotType();

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

import lol.bai.megane.module.mekanism.mixin.AccessBasicInventorySlot;
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.data.ProgressData;
import mekanism.api.recipes.MekanismRecipe;
import mekanism.common.tile.factory.TileEntityFactory;

public class FactoryProvider implements IDataProvider<TileEntityFactory<MekanismRecipe>> {

@Override
public void appendData(IDataWriter data, IServerAccessor<TileEntityFactory<MekanismRecipe>> accessor, IPluginConfig config) {
data.add(ProgressData.class, res -> {
var factory = accessor.getTarget();
var ratio = 0d;

for (var i = 0; i < factory.tier.processes; i++) {
ratio = Math.max(ratio, factory.getScaledProgress(1, i));
}

if (ratio > 0) {
var progressData = ProgressData.ratio((float) ratio);
var slots = factory.getInventorySlots(null);

for (var slot : slots) {
if (slot instanceof AccessBasicInventorySlot basic) {
switch (basic.megane_getSlotType()) {
case INPUT -> progressData.input(slot.getStack());
case OUTPUT -> progressData.output(slot.getStack());
}
}
}

res.add(progressData);
}
});
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.fluids.capability.IFluidHandler;

public class FluidProvider implements IDataProvider<BlockEntity>, LookingAtHelper {
public class FluidProvider implements IDataProvider<BlockEntity>, LookingAtHelper {

private FluidData.PlatformDependant<FluidStack> fluidData;

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

import lol.bai.megane.module.mekanism.MeganeMekaninsm;
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.IDataReader;
import mcp.mobius.waila.api.IDataWriter;
import mcp.mobius.waila.api.IEntityAccessor;
import mcp.mobius.waila.api.IEntityComponentProvider;
import mcp.mobius.waila.api.IPluginConfig;
import mcp.mobius.waila.api.IServerAccessor;
import mcp.mobius.waila.api.ITooltip;
import mcp.mobius.waila.api.component.PairComponent;
import mekanism.api.MekanismAPI;
import mekanism.api.security.SecurityMode;
import mekanism.api.text.APILang;
import mekanism.common.capabilities.Capabilities;
import mekanism.common.util.MekanismUtils;
import net.minecraft.ChatFormatting;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraftforge.common.capabilities.ICapabilityProvider;
import org.jetbrains.annotations.Nullable;

public class SecurityProvider<T extends ICapabilityProvider> implements IBlockComponentProvider, IEntityComponentProvider, IDataProvider<T> {

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

private void appendBody(ITooltip tooltip, IDataReader data, IPluginConfig config) {
if (!config.getBoolean(MeganeMekaninsm.CONFIG_SHOW_SECURITY)) return;

var security = data.get(Data.class);
if (security == null || security.mode == SecurityMode.PUBLIC) return;

tooltip.addLine((switch (security.mode) {
case PRIVATE -> APILang.PRIVATE;
case TRUSTED -> APILang.TRUSTED;
default -> throw new AssertionError();
}).translate().withStyle(security.canAccess ? ChatFormatting.GREEN : ChatFormatting.RED));

var owner = security.owner;
if (owner != null) tooltip.addLine(new PairComponent(
Component.translatable("megane.mekanism.owner"),
Component.literal(owner)));
}

@Override
public void appendBody(ITooltip tooltip, IBlockAccessor accessor, IPluginConfig config) {
appendBody(tooltip, accessor.getData(), config);
}

@Override
public void appendBody(ITooltip tooltip, IEntityAccessor accessor, IPluginConfig config) {
appendBody(tooltip, accessor.getData(), config);
}

@Override
public void appendData(IDataWriter data, IServerAccessor<T> accessor, IPluginConfig config) {
if (!config.getBoolean(MeganeMekaninsm.CONFIG_SHOW_SECURITY)) return;

var be = accessor.getTarget();
be.getCapability(Capabilities.OWNER_OBJECT).ifPresent(owner -> {
var api = MekanismAPI.getSecurityUtils();
var security = be.getCapability(Capabilities.SECURITY_OBJECT).resolve().orElse(null);
var securityMode = security == null ? SecurityMode.PUBLIC : api.getEffectiveSecurityMode(security, false);
var canAccess = security == null || api.canAccessObject(accessor.getPlayer(), security);
data.addImmediate(new Data(securityMode, MekanismUtils.getLastKnownUsername(owner.getOwnerUUID()), canAccess));
});
}

public record Data(
SecurityMode mode,
@Nullable String owner,
boolean canAccess
) implements IData {

public Data(FriendlyByteBuf buf) {
this(
buf.readEnum(SecurityMode.class),
buf.readNullable(FriendlyByteBuf::readUtf),
buf.readBoolean());
}

@Override
public void write(FriendlyByteBuf buf) {
buf.writeEnum(mode);
buf.writeNullable(owner, FriendlyByteBuf::writeUtf);
buf.writeBoolean(canAccess);
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public static void addEnergy(IDataWriter.Result<EnergyData> res, IStrictEnergyHa
var count = handler.getEnergyContainerCount();
var stored = 0.0;
var max = 0.0;
for (int i = 0; i < count; i++) {
for (var i = 0; i < count; i++) {
stored += handler.getEnergy(i).doubleValue();
max += handler.getMaxEnergy(i).doubleValue();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public void appendData(IDataWriter data, IServerAccessor<ResourceChickenEntity>
}

if (config.getBoolean(MeganeResourceChickens.CONFIG_SHOW_CONVERSION) && chicken.conversionProgress != 0) {
data.addImmediate(new Mutation(chicken.conversionProgress, chicken.conversionType));
data.addImmediate(new Mutation(chicken.conversionProgress, chicken.chickenDataCONV.ID));
}
}

Expand Down
8 changes: 8 additions & 0 deletions src/translation/resources/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,14 @@
"config.waila.plugin_megane.ie.assembler_recipes": "Show Assembler Recipes"
},

"mekanism" : {
"megane.mekanism.owner" : "Owner",

"config.waila.plugin_megane.mekanism" : "Mekanism",
"config.waila.plugin_megane.mekanism.chemical": "Show Block Chemical Contents",
"config.waila.plugin_megane.mekanism.security": "Show Block Security"
},

"productive-bees" : {
"megane.productive_bees.bee.type" : "Type",
"megane.productive_bees.bee.productivity" : "Productivity",
Expand Down

0 comments on commit 8cc29cd

Please sign in to comment.