Skip to content

Commit

Permalink
use block entity's CustomName if present (TehNut-Mods/HWYLA#262)
Browse files Browse the repository at this point in the history
  • Loading branch information
deirn committed Feb 4, 2021
1 parent c2093d6 commit b4c6459
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
27 changes: 24 additions & 3 deletions src/main/java/mcp/mobius/waila/addons/core/HUDHandlerBlocks.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,26 @@
import mcp.mobius.waila.api.IComponentProvider;
import mcp.mobius.waila.api.IDataAccessor;
import mcp.mobius.waila.api.IPluginConfig;
import mcp.mobius.waila.api.IServerDataProvider;
import mcp.mobius.waila.api.ITaggableList;
import mcp.mobius.waila.utils.ModIdentification;
import net.minecraft.block.BlockState;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.state.property.BooleanProperty;
import net.minecraft.text.LiteralText;
import net.minecraft.text.Style;
import net.minecraft.text.Text;
import net.minecraft.util.Formatting;
import net.minecraft.util.Identifier;
import net.minecraft.util.Nameable;
import net.minecraft.util.registry.Registry;
import net.minecraft.world.World;

public class HUDHandlerBlocks implements IComponentProvider {
public class HUDHandlerBlocks implements IComponentProvider, IServerDataProvider<BlockEntity> {

static final IComponentProvider INSTANCE = new HUDHandlerBlocks();
static final HUDHandlerBlocks INSTANCE = new HUDHandlerBlocks();
static final Identifier OBJECT_NAME_TAG = new Identifier(Waila.MODID, "object_name");
static final Identifier REGISTRY_NAME_TAG = new Identifier(Waila.MODID, "registry_name");
static final Identifier MOD_NAME_TAG = new Identifier(Waila.MODID, "mod_name");
Expand All @@ -30,7 +36,12 @@ public void appendHead(List<Text> tooltip, IDataAccessor accessor, IPluginConfig
if (accessor.getBlockState().getMaterial().isLiquid())
return;

((ITaggableList<Identifier, Text>) tooltip).setTag(OBJECT_NAME_TAG, new LiteralText(String.format(Waila.CONFIG.get().getFormatting().getBlockName(), accessor.getBlock().getName().getString())));
String name = accessor.getBlockEntity() != null ? accessor.getServerData().getString("customName") : "";
if (name.isEmpty()) {
name = accessor.getBlock().getName().getString();
}

((ITaggableList<Identifier, Text>) tooltip).setTag(OBJECT_NAME_TAG, new LiteralText(String.format(Waila.CONFIG.get().getFormatting().getBlockName(), name)));
if (config.get(PluginCore.CONFIG_SHOW_REGISTRY))
((ITaggableList<Identifier, Text>) tooltip).setTag(REGISTRY_NAME_TAG, new LiteralText(Registry.BLOCK.getId(accessor.getBlock()).toString()).setStyle(Style.EMPTY.withColor(Formatting.GRAY)));
}
Expand Down Expand Up @@ -59,4 +70,14 @@ public void appendTail(List<Text> tooltip, IDataAccessor accessor, IPluginConfig
}
}

@Override
public void appendServerData(CompoundTag data, ServerPlayerEntity player, World world, BlockEntity blockEntity) {
if (blockEntity instanceof Nameable) {
Text name = ((Nameable) blockEntity).getCustomName();
if (name != null) {
data.putString("customName", name.getString());
}
}
}

}
3 changes: 3 additions & 0 deletions src/main/java/mcp/mobius/waila/addons/core/PluginCore.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import mcp.mobius.waila.overlay.tooltiprenderers.TooltipRendererHealth;
import net.minecraft.block.Block;
import net.minecraft.block.FluidBlock;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.decoration.AbstractDecorationEntity;
import net.minecraft.entity.vehicle.AbstractMinecartEntity;
Expand All @@ -28,6 +29,8 @@ public void register(IRegistrar registrar) {
registrar.registerComponentProvider(HUDHandlerBlocks.INSTANCE, TooltipPosition.BODY, Block.class);
registrar.registerComponentProvider(HUDHandlerBlocks.INSTANCE, TooltipPosition.TAIL, Block.class);

registrar.registerBlockDataProvider(HUDHandlerBlocks.INSTANCE, BlockEntity.class);

registrar.registerStackProvider(HUDHandlerFluids.INSTANCE, FluidBlock.class);
registrar.registerComponentProvider(HUDHandlerFluids.INSTANCE, TooltipPosition.HEAD, FluidBlock.class);

Expand Down

0 comments on commit b4c6459

Please sign in to comment.