Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix itemstack count in 1.20.5 and higher #272

Merged
merged 2 commits into from
Jun 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,11 @@ object DatabaseManager {

private fun getDefaultDatasource(): DataSource {
val dbFilepath = config.getDatabasePath().resolve("ledger.sqlite").pathString
return SQLiteDataSource(SQLiteConfig().apply {
return SQLiteDataSource(
SQLiteConfig().apply {
setJournalMode(SQLiteConfig.JournalMode.WAL)
}).apply {
}
).apply {
url = "jdbc:sqlite:$dbFilepath"
}
}
Expand Down Expand Up @@ -455,7 +457,8 @@ object DatabaseManager {
.innerJoin(
Tables.oldObjectTable,
{ Tables.Actions.oldObjectId },
{ Tables.oldObjectTable[Tables.ObjectIdentifiers.id] })
{ Tables.oldObjectTable[Tables.ObjectIdentifiers.id] }
)
.innerJoin(Tables.ObjectIdentifiers, { Tables.Actions.objectId }, { Tables.ObjectIdentifiers.id })
.innerJoin(Tables.Sources)
.selectAll()
Expand Down Expand Up @@ -497,7 +500,8 @@ object DatabaseManager {
.innerJoin(
Tables.oldObjectTable,
{ Tables.Actions.oldObjectId },
{ Tables.oldObjectTable[Tables.ObjectIdentifiers.id] })
{ Tables.oldObjectTable[Tables.ObjectIdentifiers.id] }
)
.innerJoin(Tables.ObjectIdentifiers, { Tables.Actions.objectId }, { Tables.ObjectIdentifiers.id })
.innerJoin(Tables.Sources)
.selectAll()
Expand All @@ -518,7 +522,8 @@ object DatabaseManager {
.innerJoin(
Tables.oldObjectTable,
{ Tables.Actions.oldObjectId },
{ Tables.oldObjectTable[Tables.ObjectIdentifiers.id] })
{ Tables.oldObjectTable[Tables.ObjectIdentifiers.id] }
)
.innerJoin(Tables.ObjectIdentifiers, { Tables.Actions.objectId }, { Tables.ObjectIdentifiers.id })
.innerJoin(Tables.Sources)
.selectAll()
Expand Down Expand Up @@ -546,7 +551,8 @@ object DatabaseManager {
.innerJoin(
Tables.oldObjectTable,
{ Tables.Actions.oldObjectId },
{ Tables.oldObjectTable[Tables.ObjectIdentifiers.id] })
{ Tables.oldObjectTable[Tables.ObjectIdentifiers.id] }
)
.innerJoin(Tables.ObjectIdentifiers, { Tables.Actions.objectId }, { Tables.ObjectIdentifiers.id })
.innerJoin(Tables.Sources)
.selectAll()
Expand Down Expand Up @@ -618,7 +624,6 @@ object DatabaseManager {
private fun getOrCreateSourceId(source: String): Int =
getOrCreateObjectId(source, cache.sourceKeys, Tables.Source, Tables.Sources, Tables.Sources.name)


private fun getOrCreateActionId(actionTypeId: String): Int =
getOrCreateObjectId(
actionTypeId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,9 @@ import net.minecraft.util.Identifier
const val ITEM_NBT_DATA_VERSION = 3817
const val ITEM_COMPONENTS_DATA_VERSION = 3825

const val COMPONENTS = "components" // ItemStack
const val PROPERTIES = "Properties" // BlockState
const val COUNT = "Count" // ItemStack
const val TAG = "tag" // ItemStack
const val COUNT_PRE_1_20_5 = "Count" // ItemStack
const val COUNT = "count" // ItemStack
const val UUID = "UUID" // Entity

object NbtUtils {
Expand All @@ -47,26 +46,18 @@ object NbtUtils {

fun itemFromProperties(tag: String?, name: Identifier, registries: RegistryWrapper.WrapperLookup): ItemStack {
val extraDataTag = StringNbtReader.parse(tag ?: "{}")
var itemTag: NbtElement
if (extraDataTag.contains(COMPONENTS)) {
itemTag = extraDataTag
} else {
itemTag = NbtCompound()
var itemTag = extraDataTag
if (!extraDataTag.contains(COUNT)) {
// 1.20.4 and lower (need data fixing)
itemTag.putString("id", name.toString())
if (extraDataTag.contains(COUNT)) {
itemTag.putByte(COUNT, extraDataTag.getByte(COUNT))
} else {
itemTag.putByte(COUNT, 1)
}

if (extraDataTag.contains(TAG)) {
itemTag.put(TAG, extraDataTag.getCompound(TAG))

itemTag = Schemas.getFixer().update(
TypeReferences.ITEM_STACK,
Dynamic(NbtOps.INSTANCE, itemTag), ITEM_NBT_DATA_VERSION, ITEM_COMPONENTS_DATA_VERSION
).cast(NbtOps.INSTANCE)
if (!itemTag.contains(COUNT_PRE_1_20_5)) {
// Ledger ItemStack in 1.20.4 and earlier had "Count" omitted if it was 1
itemTag.putByte(COUNT_PRE_1_20_5, 1)
}
itemTag = Schemas.getFixer().update(
TypeReferences.ITEM_STACK,
Dynamic(NbtOps.INSTANCE, itemTag), ITEM_NBT_DATA_VERSION, ITEM_COMPONENTS_DATA_VERSION
).cast(NbtOps.INSTANCE) as NbtCompound?
}

return ItemStack.fromNbt(registries, itemTag).get()
Expand Down