Skip to content

Commit

Permalink
Show Renamed AE2 Parts and Blocks in TOP
Browse files Browse the repository at this point in the history
  • Loading branch information
IntegerLimit committed Dec 29, 2024
1 parent d6d7ebe commit 5f27b9d
Show file tree
Hide file tree
Showing 5 changed files with 216 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package com.nomiceu.nomilabs.integration.top;

import static mcjty.theoneprobe.api.TextStyleClass.MODNAME;

import net.minecraft.block.state.IBlockState;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.world.World;

import appeng.helpers.ICustomNameObject;
import appeng.integration.modules.theoneprobe.part.PartAccessor;
import appeng.tile.AEBaseTile;
import mcjty.theoneprobe.Tools;
import mcjty.theoneprobe.api.*;
import mcjty.theoneprobe.config.Config;

public class AECustomNameOverride implements IBlockDisplayOverride {

private static final PartAccessor accessor = new PartAccessor();

@Override
public boolean overrideStandardInfo(ProbeMode mode, IProbeInfo info, EntityPlayer player, World world,
IBlockState state, IProbeHitData data) {
var tile = world.getTileEntity(data.getPos());
var part = accessor.getMaybePart(tile, data);

String customName = null;

// noinspection SimplifyOptionalCallChains
if (!(tile instanceof AEBaseTile) && !part.isPresent()) return false;

if (part.isPresent()) {
if (part.get() instanceof ICustomNameObject nameObj) {
if (nameObj.hasCustomInventoryName())
customName = nameObj.getCustomInventoryName();
}
} else if (tile instanceof AEBaseTile base) {
if (base.hasCustomInventoryName())
customName = base.getCustomInventoryName();
}

if (customName == null) return false;

customName = customName.trim();
if (customName.isEmpty()) return false;

ItemStack pickBlock = data.getPickBlock();
IElement customNameElement = new CustomNameElement(pickBlock, customName);

if (Tools.show(mode, Config.getRealConfig().getShowModName()))
info.horizontal()
.item(pickBlock)
.vertical()
.element(customNameElement)
.text(MODNAME + Tools.getModName(state.getBlock()));
else
info.horizontal(info.defaultLayoutStyle()
.alignment(ElementAlignment.ALIGN_CENTER))
.item(pickBlock)
.element(customNameElement);

return true;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package com.nomiceu.nomilabs.integration.top;

import static mcjty.theoneprobe.apiimpl.elements.ElementVertical.SPACING;
import static net.minecraft.util.text.TextFormatting.ITALIC;
import static net.minecraft.util.text.TextFormatting.RESET;

import net.minecraft.item.ItemStack;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;

import io.netty.buffer.ByteBuf;
import mcjty.theoneprobe.api.IElement;
import mcjty.theoneprobe.apiimpl.client.ElementTextRender;
import mcjty.theoneprobe.network.NetworkTools;

/**
* An element that displays the normal ItemStack display name,
* and a custom name only IF it is not the same as the ItemStack display name.
*/
public class CustomNameElement implements IElement {

public static final int TEXT_HEIGHT = 10;

private final ItemStack pickStack;
private final String customName;

public CustomNameElement(ItemStack pickStack, String customName) {
this.pickStack = pickStack;
this.customName = customName;
}

public CustomNameElement(ByteBuf buf) {
if (buf.readBoolean()) {
pickStack = NetworkTools.readItemStack(buf);
} else {
pickStack = ItemStack.EMPTY;
}

customName = NetworkTools.readStringUTF8(buf);
}

@Override
@SideOnly(Side.CLIENT)
public void render(int x, int y) {
if (!pickStack.isEmpty())
ElementTextRender.render(pickStack.getDisplayName(), x, y);
if (shouldRender())
ElementTextRender.render(ITALIC + "(" + customName + ")" + RESET, x, y + TEXT_HEIGHT + SPACING);
}

@Override
public int getWidth() {
var pickStackWidth = pickStack.isEmpty() ? TEXT_HEIGHT : ElementTextRender.getWidth(pickStack.getDisplayName());

return shouldRender() ? Math.max(pickStackWidth, ElementTextRender.getWidth(customName)) : pickStackWidth;
}

@Override
public int getHeight() {
return shouldRender() ? TEXT_HEIGHT * 2 + SPACING : TEXT_HEIGHT;
}

@Override
public void toBytes(ByteBuf buf) {
if (!pickStack.isEmpty()) {
buf.writeBoolean(true);
NetworkTools.writeItemStack(buf, pickStack);
} else {
buf.writeBoolean(false);
}

NetworkTools.writeStringUTF8(buf, customName);
}

@Override
public int getID() {
return LabsTOPManager.CUSTOM_NAME_ELEMENT;
}

private boolean shouldRender() {
if (pickStack.isEmpty()) return true;

return !pickStack.getDisplayName().trim().equals(customName);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import net.minecraft.block.state.IBlockState;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.world.World;
import net.minecraftforge.fml.common.Loader;

import com.nomiceu.nomilabs.LabsValues;

Expand All @@ -13,12 +14,23 @@
public class LabsTOPManager {

public static int FLUID_NAME_ELEMENT;
public static int CUSTOM_NAME_ELEMENT;

public static void register() {
ITheOneProbe TOP = TheOneProbe.theOneProbeImp;
TOP.registerProvider(new TOPTooltipMessage());

// AE2 TOP Integration
if (Loader.isModLoaded(LabsValues.AE2_MODID))
TOP.registerBlockDisplayOverride(new AECustomNameOverride());

// GT TOP Integration
TOP.registerProvider(new SteamMachineInfoProvider());

// Labs TOP Integration
TOP.registerProvider(new TOPTooltipMessage());

FLUID_NAME_ELEMENT = TOP.registerElementFactory(LabsFluidNameElement::new);
CUSTOM_NAME_ELEMENT = TOP.registerElementFactory(CustomNameElement::new);
}

public static class TOPTooltipMessage implements IProbeInfoProvider {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package com.nomiceu.nomilabs.mixin.ae2;

import javax.annotation.Nullable;

import net.minecraft.block.material.Material;
import net.minecraft.block.state.IBlockState;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.RayTraceResult;
import net.minecraft.world.IBlockAccess;
import net.minecraft.world.World;

import org.jetbrains.annotations.NotNull;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.Unique;

import appeng.block.AEBaseBlock;
import appeng.block.AEBaseTileBlock;
import appeng.tile.AEBaseTile;
import appeng.util.SettingsFrom;

@Mixin(value = AEBaseTileBlock.class, remap = false)
public abstract class AEBaseTileBlockMixin extends AEBaseBlock {

@Shadow
@Nullable
public abstract <T extends AEBaseTile> T getTileEntity(IBlockAccess w, BlockPos pos);

/**
* Default Ignored Constructor
*/
private AEBaseTileBlockMixin(Material mat) {
super(mat);
}

@Override
@Unique
@NotNull
public ItemStack getPickBlock(@NotNull IBlockState state, @NotNull RayTraceResult target, @NotNull World world,
@NotNull BlockPos pos, @NotNull EntityPlayer player) {
ItemStack baseStack = super.getPickBlock(state, target, world, pos, player);

AEBaseTile tile = getTileEntity(world, pos);
if (tile == null) return baseStack;

NBTTagCompound tag = tile.downloadSettings(SettingsFrom.DISMANTLE_ITEM);
baseStack.setTagCompound(tag);
return baseStack;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"minVersion": "0.8",
"compatibilityLevel": "JAVA_8",
"mixins": [
"AEBaseTileBlockMixin",
"InscriberRecipeMixin"
],
"client": [
Expand Down

0 comments on commit 5f27b9d

Please sign in to comment.