Skip to content

Commit

Permalink
Conversion to client-side stacks in fake-entity metadata
Browse files Browse the repository at this point in the history
Fixes #283
  • Loading branch information
NichtStudioCode committed Oct 3, 2023
1 parent 6d51ace commit 00d98d3
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ package xyz.xenondevs.nova.world.fakeentity.metadata
import io.netty.buffer.Unpooled
import net.minecraft.network.FriendlyByteBuf
import net.minecraft.network.syncher.EntityDataSerializer
import net.minecraft.network.syncher.EntityDataSerializers
import net.minecraft.world.item.ItemStack
import xyz.xenondevs.nmsutils.network.PacketIdRegistry
import xyz.xenondevs.nova.item.logic.PacketItems
import java.util.*

abstract class Metadata internal constructor() {
Expand Down Expand Up @@ -49,7 +52,7 @@ abstract class Metadata internal constructor() {
}

internal fun <T, R> entry(index: Int, serializer: EntityDataSerializer<R>, default: T, toRaw: (T) -> R): MappedNonNullMetadataEntry<T, R>.MappedDelegate {
val entry = MappedNonNullMetadataEntry(index, serializer, toRaw, { default }, default)
val entry = MappedNonNullMetadataEntry(index, serializer, toRaw, { throw UnsupportedOperationException() }, default)
entries += entry
return entry.mappedDelegate
}
Expand All @@ -72,4 +75,15 @@ abstract class Metadata internal constructor() {
return entry
}

internal fun itemStack(index: Int, useName: Boolean, default: ItemStack): MappedNonNullMetadataEntry<ItemStack, ItemStack>.MappedDelegate {
val entry = MappedNonNullMetadataEntry(
index, EntityDataSerializers.ITEM_STACK,
{ PacketItems.getClientSideStack(null, it, useName)},
{ PacketItems.getServerSideStack(it) },
default
)
entries += entry
return entry.mappedDelegate
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import net.minecraft.world.item.ItemStack

class ItemDisplayMetadata : DisplayMetadata() {

var itemStack: ItemStack by entry(23, EntityDataSerializers.ITEM_STACK, ItemStack.EMPTY)
var itemStack: ItemStack by itemStack(23, true, ItemStack.EMPTY)
var itemDisplay: ItemDisplayContext by entry(24, EntityDataSerializers.BYTE, ItemDisplayContext.NONE) { it.ordinal.toByte() }

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import net.minecraft.world.item.ItemStack

class ItemFrameMetadata : EntityMetadata() {

var item: ItemStack by entry(8, EntityDataSerializers.ITEM_STACK, ItemStack.EMPTY)
var item: ItemStack by itemStack(8, false, ItemStack.EMPTY)
var rotation: Int by entry(9, EntityDataSerializers.INT, 0)

}
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package xyz.xenondevs.nova.world.fakeentity.metadata.impl

import net.minecraft.network.syncher.EntityDataSerializers
import net.minecraft.world.item.ItemStack

class ItemMetadata : EntityMetadata() {

var item: ItemStack by entry(8, EntityDataSerializers.ITEM_STACK, ItemStack.EMPTY)
var item: ItemStack by itemStack(8, true, ItemStack.EMPTY)

}

0 comments on commit 00d98d3

Please sign in to comment.