Actually make custom machine names working

This commit is contained in:
DBotThePony 2023-06-26 23:35:59 +07:00
parent 0704da4be0
commit f7af09b62e
Signed by: DBot
GPG Key ID: DCC23B5715498507
2 changed files with 19 additions and 2 deletions

View File

@ -6,6 +6,7 @@ import it.unimi.dsi.fastutil.objects.Object2ObjectFunction
import net.minecraft.core.BlockPos import net.minecraft.core.BlockPos
import net.minecraft.core.Direction import net.minecraft.core.Direction
import net.minecraft.core.particles.DustParticleOptions import net.minecraft.core.particles.DustParticleOptions
import net.minecraft.network.chat.Component
import net.minecraft.util.RandomSource import net.minecraft.util.RandomSource
import net.minecraft.world.Containers import net.minecraft.world.Containers
import net.minecraft.world.InteractionHand 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.component1
import ru.dbotthepony.mc.otm.core.math.component2 import ru.dbotthepony.mc.otm.core.math.component2
import ru.dbotthepony.mc.otm.core.math.component3 import ru.dbotthepony.mc.otm.core.math.component3
import ru.dbotthepony.mc.otm.core.tagNotNull
import ru.dbotthepony.mc.otm.once import ru.dbotthepony.mc.otm.once
fun Block.getShapeForEachState(properties: List<Property<*>>, fn: (BlockState) -> VoxelShape): Map<BlockState, VoxelShape> { fun Block.getShapeForEachState(properties: List<Property<*>>, fn: (BlockState) -> VoxelShape): Map<BlockState, VoxelShape> {
@ -71,8 +73,13 @@ abstract class MatteryBlock @JvmOverloads constructor(
if (this is EntityBlock && itemStack.hasCustomHoverName() && !level.isClientSide) { if (this is EntityBlock && itemStack.hasCustomHoverName() && !level.isClientSide) {
val tile = level.getBlockEntity(blockPos) val tile = level.getBlockEntity(blockPos)
if (tile is MatteryDeviceBlockEntity) if (tile is MatteryDeviceBlockEntity) {
tile.customDisplayName = itemStack.displayName try {
tile.customDisplayName = Component.Serializer.fromJson(itemStack.tagNotNull.getCompound("display").getString("Name"))
} catch(_: Exception) {
}
}
} }
super.setPlacedBy(level, blockPos, blockState, entity, itemStack) super.setPlacedBy(level, blockPos, blockState, entity, itemStack)

View File

@ -7,6 +7,7 @@ import com.google.gson.JsonObject
import com.google.gson.JsonPrimitive import com.google.gson.JsonPrimitive
import com.google.gson.JsonSerializationContext import com.google.gson.JsonSerializationContext
import net.minecraft.nbt.CompoundTag import net.minecraft.nbt.CompoundTag
import net.minecraft.network.chat.Component
import net.minecraft.world.item.BlockItem import net.minecraft.world.item.BlockItem
import net.minecraft.world.item.ItemStack import net.minecraft.world.item.ItemStack
import net.minecraft.world.level.storage.loot.LootContext 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.stream
import ru.dbotthepony.mc.otm.core.tagNotNull import ru.dbotthepony.mc.otm.core.tagNotNull
import ru.dbotthepony.mc.otm.core.collect.stream import ru.dbotthepony.mc.otm.core.collect.stream
import ru.dbotthepony.mc.otm.core.nbt.getJson
import ru.dbotthepony.mc.otm.registry.MItemFunctionTypes import ru.dbotthepony.mc.otm.registry.MItemFunctionTypes
import java.util.stream.Stream import java.util.stream.Stream
@ -56,6 +58,14 @@ class CopyTileNbtFunction(filter: Stream<out String> = 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 return t
} }