Skip to content

Commit

Permalink
Fix itemstack count in 1.20.5 and higher (#272)
Browse files Browse the repository at this point in the history
  • Loading branch information
DrexHD authored Jun 18, 2024
1 parent 0c0ebec commit d14b0a8
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 28 deletions.
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

0 comments on commit d14b0a8

Please sign in to comment.