package net.geforcemods.securitycraft.api;

import net.geforcemods.securitycraft.SCContent;
import net.geforcemods.securitycraft.util.Utils;
import net.minecraft.core.BlockPos;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.TextComponent;
import net.minecraft.world.level.block.entity.BlockEntityType;
import net.minecraft.world.level.block.state.BlockState;

public class NamedBlockEntity extends OwnableBlockEntity implements INameSetter {
	private Component customName = TextComponent.EMPTY;

	public NamedBlockEntity(BlockPos pos, BlockState state)
	{
		this(SCContent.beTypeAbstract, pos, state);
	}

	public NamedBlockEntity(BlockEntityType<?> type, BlockPos pos, BlockState state)
	{
		super(type, pos, state);
	}

	@Override
	public CompoundTag save(CompoundTag tag)
	{
		super.save(tag);
		tag.putString("customName", customName.getString());
		return tag;
	}

	@Override
	public void load(CompoundTag tag)
	{
		super.load(tag);

		if (tag.contains("customName"))
			customName = new TextComponent(tag.getString("customName"));
	}

	@Override
	public Component getName()
	{
		return hasCustomName() ? customName : getDefaultName();
	}

	@Override
	public boolean hasCustomName() {
		Component name = getCustomName();

		return name != null && !TextComponent.EMPTY.equals(name) && !getDefaultName().equals(name);
	}

	@Override
	public Component getCustomName() {
		return customName;
	}

	@Override
	public void setCustomName(Component customName) {
		this.customName = customName;
		level.sendBlockUpdated(worldPosition, getBlockState(), getBlockState(), 3);
	}

	public Component getDefaultName() {
		return Utils.localize(getBlockState().getBlock().getDescriptionId());
	}
}

@@ -1035,17 +1035,95 @@
 mName%22))
+ %7B
 %0A
+%09%09%09String name = tag.getString(%22customName%22);%0A%0A%09%09%09if(!name.equals(%22name%22))%0A%09
 %09%09%09custo
@@ -1152,37 +1152,18 @@
 ent(
-tag.getString(%22customName%22));
+name);%0A%09%09%7D
 %0A%09%7D%0A

fix preexisting blocks without a custom name being named "name"