From f7af09b62e90ac5ba88febb203eec1f0d5ca6a21 Mon Sep 17 00:00:00 2001 From: DBotThePony Date: Mon, 26 Jun 2023 23:35:59 +0700 Subject: [PATCH] Actually make custom machine names working --- .../ru/dbotthepony/mc/otm/block/MatteryBlock.kt | 11 +++++++++-- .../mc/otm/data/loot/CopyTileNbtFunction.kt | 10 ++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/block/MatteryBlock.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/block/MatteryBlock.kt index cb57cc668..86ad9bba1 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/block/MatteryBlock.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/block/MatteryBlock.kt @@ -6,6 +6,7 @@ import it.unimi.dsi.fastutil.objects.Object2ObjectFunction import net.minecraft.core.BlockPos import net.minecraft.core.Direction import net.minecraft.core.particles.DustParticleOptions +import net.minecraft.network.chat.Component import net.minecraft.util.RandomSource import net.minecraft.world.Containers import net.minecraft.world.InteractionHand @@ -32,6 +33,7 @@ import ru.dbotthepony.mc.otm.core.math.BlockRotationFreedom import ru.dbotthepony.mc.otm.core.math.component1 import ru.dbotthepony.mc.otm.core.math.component2 import ru.dbotthepony.mc.otm.core.math.component3 +import ru.dbotthepony.mc.otm.core.tagNotNull import ru.dbotthepony.mc.otm.once fun Block.getShapeForEachState(properties: List>, fn: (BlockState) -> VoxelShape): Map { @@ -71,8 +73,13 @@ abstract class MatteryBlock @JvmOverloads constructor( if (this is EntityBlock && itemStack.hasCustomHoverName() && !level.isClientSide) { val tile = level.getBlockEntity(blockPos) - if (tile is MatteryDeviceBlockEntity) - tile.customDisplayName = itemStack.displayName + if (tile is MatteryDeviceBlockEntity) { + try { + tile.customDisplayName = Component.Serializer.fromJson(itemStack.tagNotNull.getCompound("display").getString("Name")) + } catch(_: Exception) { + + } + } } super.setPlacedBy(level, blockPos, blockState, entity, itemStack) diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/data/loot/CopyTileNbtFunction.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/data/loot/CopyTileNbtFunction.kt index 4858c7cf3..b363b59a6 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/data/loot/CopyTileNbtFunction.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/data/loot/CopyTileNbtFunction.kt @@ -7,6 +7,7 @@ import com.google.gson.JsonObject import com.google.gson.JsonPrimitive import com.google.gson.JsonSerializationContext import net.minecraft.nbt.CompoundTag +import net.minecraft.network.chat.Component import net.minecraft.world.item.BlockItem import net.minecraft.world.item.ItemStack import net.minecraft.world.level.storage.loot.LootContext @@ -20,6 +21,7 @@ import ru.dbotthepony.mc.otm.core.set import ru.dbotthepony.mc.otm.core.stream import ru.dbotthepony.mc.otm.core.tagNotNull import ru.dbotthepony.mc.otm.core.collect.stream +import ru.dbotthepony.mc.otm.core.nbt.getJson import ru.dbotthepony.mc.otm.registry.MItemFunctionTypes import java.util.stream.Stream @@ -56,6 +58,14 @@ class CopyTileNbtFunction(filter: Stream = Stream.empty()) : LootIte } } + if ("Name" in data && "display" !in t.tagNotNull) { + t.tagNotNull["display"] = CompoundTag().also { + it["Name"] = Component.Serializer.toJson(Component.Serializer.fromJson(data.getJson("Name")!!)!!) + } + + data.remove("Name") + } + return t }