diff --git a/src/main/java/com/nomiceu/nomilabs/integration/top/AECustomNameOverride.java b/src/main/java/com/nomiceu/nomilabs/integration/top/AECustomNameOverride.java new file mode 100644 index 00000000..1f371773 --- /dev/null +++ b/src/main/java/com/nomiceu/nomilabs/integration/top/AECustomNameOverride.java @@ -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; + } +} diff --git a/src/main/java/com/nomiceu/nomilabs/integration/top/CustomNameElement.java b/src/main/java/com/nomiceu/nomilabs/integration/top/CustomNameElement.java new file mode 100644 index 00000000..9dac6579 --- /dev/null +++ b/src/main/java/com/nomiceu/nomilabs/integration/top/CustomNameElement.java @@ -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); + } +} diff --git a/src/main/java/com/nomiceu/nomilabs/integration/top/LabsTOPManager.java b/src/main/java/com/nomiceu/nomilabs/integration/top/LabsTOPManager.java index 661d884e..9a0c9aa9 100644 --- a/src/main/java/com/nomiceu/nomilabs/integration/top/LabsTOPManager.java +++ b/src/main/java/com/nomiceu/nomilabs/integration/top/LabsTOPManager.java @@ -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; @@ -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 { diff --git a/src/main/java/com/nomiceu/nomilabs/mixin/ae2/AEBaseTileBlockMixin.java b/src/main/java/com/nomiceu/nomilabs/mixin/ae2/AEBaseTileBlockMixin.java new file mode 100644 index 00000000..355edd16 --- /dev/null +++ b/src/main/java/com/nomiceu/nomilabs/mixin/ae2/AEBaseTileBlockMixin.java @@ -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 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; + } +} diff --git a/src/main/resources/mixins.nomilabs.appliedenergistics2.json b/src/main/resources/mixins.nomilabs.appliedenergistics2.json index b52b064d..8e2fa2dc 100644 --- a/src/main/resources/mixins.nomilabs.appliedenergistics2.json +++ b/src/main/resources/mixins.nomilabs.appliedenergistics2.json @@ -5,6 +5,7 @@ "minVersion": "0.8", "compatibilityLevel": "JAVA_8", "mixins": [ + "AEBaseTileBlockMixin", "InscriberRecipeMixin" ], "client": [