Skip to content

Commit 0103e5f

Browse files
committed
Fix crash when locating newly supported items + use try/finally for freeing memory
1 parent fb967e6 commit 0103e5f

File tree

4 files changed

+79
-71
lines changed

4 files changed

+79
-71
lines changed

src/main/java/dev/xpple/seedmapper/command/commands/HighlightCommand.java

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -106,29 +106,32 @@ private static int highlightBlock(CustomClientCommandSource source, Pair<Integer
106106
MemorySegment pos3List = Cubiomes.generateOres(arena, generator, surfaceNoise, oreConfig, chunkX, chunkZ);
107107
int size = Pos3List.size(pos3List);
108108
MemorySegment pos3s = Pos3List.pos3s(pos3List);
109-
for (int i = 0; i < size; i++) {
110-
MemorySegment pos3 = Pos3.asSlice(pos3s, i);
111-
BlockPos pos = new BlockPos(Pos3.x(pos3), Pos3.y(pos3), Pos3.z(pos3));
112-
if (doAirCheck && chunk.getBlockState(pos).isAir()) {
113-
continue;
114-
}
115-
Integer previouslyGeneratedOre = generatedOres.get(pos);
116-
if (previouslyGeneratedOre != null) {
117-
boolean contains = false;
118-
for (int j = 0; j < numReplaceBlocks; j++) {
119-
int replaceBlock = replaceBlocks.getAtIndex(Cubiomes.C_INT, j);
120-
if (replaceBlock == previouslyGeneratedOre) {
121-
contains = true;
122-
break;
123-
}
124-
}
125-
if (!contains) {
109+
try {
110+
for (int i = 0; i < size; i++) {
111+
MemorySegment pos3 = Pos3.asSlice(pos3s, i);
112+
BlockPos pos = new BlockPos(Pos3.x(pos3), Pos3.y(pos3), Pos3.z(pos3));
113+
if (doAirCheck && chunk.getBlockState(pos).isAir()) {
126114
continue;
127115
}
116+
Integer previouslyGeneratedOre = generatedOres.get(pos);
117+
if (previouslyGeneratedOre != null) {
118+
boolean contains = false;
119+
for (int j = 0; j < numReplaceBlocks; j++) {
120+
int replaceBlock = replaceBlocks.getAtIndex(Cubiomes.C_INT, j);
121+
if (replaceBlock == previouslyGeneratedOre) {
122+
contains = true;
123+
break;
124+
}
125+
}
126+
if (!contains) {
127+
continue;
128+
}
129+
}
130+
generatedOres.put(pos, oreBlock);
128131
}
129-
generatedOres.put(pos, oreBlock);
132+
} finally {
133+
Cubiomes.freePos3List(pos3List);
130134
}
131-
Cubiomes.freePos3List(pos3List);
132135
});
133136

134137
int block = blockPair.getFirst();

src/main/java/dev/xpple/seedmapper/command/commands/LocateCommand.java

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -376,32 +376,34 @@ record StructureIterationState(MemorySegment structureConfig, StructureChecks.Ge
376376
continue;
377377
}
378378
MemorySegment lootTableContext = LootTableContext.allocate(arena);
379-
// it seems caching the loot tables is not faster
380-
if (Cubiomes.init_loot_table_name(lootTableContext, Piece.lootTable(piece), version) == 0) {
381-
continue;
382-
}
383-
if (Cubiomes.has_item(lootTableContext, itemPredicate.item()) == 0) {
384-
Cubiomes.free_loot_table_pools(lootTableContext);
385-
Set<String> structureIgnoredLootTables = ignoredLootTables.computeIfAbsent(structure, _ -> new HashSet<>());
386-
structureIgnoredLootTables.add(Piece.lootTable(piece).getString(0));
387-
// if structure has no loot tables with the desired item, remove structure from state loop
388-
if (structureIgnoredLootTables.size() == lootTableCount.get(structure)) {
389-
return;
379+
try {
380+
// it seems caching the loot tables is not faster
381+
if (Cubiomes.init_loot_table_name(lootTableContext, Piece.lootTable(piece), version) == 0) {
382+
continue;
390383
}
391-
continue;
392-
}
393-
for (int j = 0; j < chestCount; j++) {
394-
Cubiomes.set_loot_seed(lootTableContext, Piece.lootSeeds(piece).getAtIndex(Cubiomes.C_LONG_LONG, j));
395-
Cubiomes.generate_loot(lootTableContext);
396-
int lootCount = LootTableContext.generated_item_count(lootTableContext);
397-
for (int k = 0; k < lootCount; k++) {
398-
MemorySegment itemStack = ItemStack.asSlice(LootTableContext.generated_items(lootTableContext), k);
399-
if (Cubiomes.get_global_item_id(lootTableContext, ItemStack.item(itemStack)) == itemPredicate.item() && itemPredicate.enchantmensPredicate().test(itemStack)) {
400-
foundInStructure += ItemStack.count(itemStack);
384+
if (Cubiomes.has_item(lootTableContext, itemPredicate.item()) == 0) {
385+
Set<String> structureIgnoredLootTables = ignoredLootTables.computeIfAbsent(structure, _ -> new HashSet<>());
386+
structureIgnoredLootTables.add(Piece.lootTable(piece).getString(0));
387+
// if structure has no loot tables with the desired item, remove structure from state loop
388+
if (structureIgnoredLootTables.size() == lootTableCount.get(structure)) {
389+
return;
390+
}
391+
continue;
392+
}
393+
for (int j = 0; j < chestCount; j++) {
394+
Cubiomes.set_loot_seed(lootTableContext, Piece.lootSeeds(piece).getAtIndex(Cubiomes.C_LONG_LONG, j));
395+
Cubiomes.generate_loot(lootTableContext);
396+
int lootCount = LootTableContext.generated_item_count(lootTableContext);
397+
for (int k = 0; k < lootCount; k++) {
398+
MemorySegment itemStack = ItemStack.asSlice(LootTableContext.generated_items(lootTableContext), k);
399+
if (Cubiomes.get_global_item_id(lootTableContext, ItemStack.item(itemStack)) == itemPredicate.item() && itemPredicate.enchantmensPredicate().test(itemStack)) {
400+
foundInStructure += ItemStack.count(itemStack);
401+
}
401402
}
402403
}
404+
} finally {
405+
Cubiomes.free_loot_table_pools(lootTableContext);
403406
}
404-
Cubiomes.free_loot_table_pools(lootTableContext);
405407
}
406408
if (foundInStructure > 0) {
407409
found[0] += foundInStructure;

src/main/java/dev/xpple/seedmapper/seedmap/SeedMapScreen.java

Lines changed: 32 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -771,38 +771,41 @@ private void showLoot(FeatureWidget widget) {
771771
MemorySegment lootTableInternal = Piece.lootTable(piece);
772772
String lootTable = lootTableInternal.getString(0);
773773
MemorySegment lootTableContext = LootTableContext.allocate(tempArena);
774-
if (Cubiomes.init_loot_table_name(lootTableContext, lootTableInternal, this.version) == 0) {
775-
continue;
776-
}
777-
MemorySegment chestPoses = Piece.chestPoses(piece);
778-
MemorySegment lootSeeds = Piece.lootSeeds(piece);
779-
for (int chestIdx = 0; chestIdx < chestCount; chestIdx++) {
780-
MemorySegment chestPosInternal = Pos.asSlice(chestPoses, chestIdx);
781-
BlockPos chestPos = new BlockPos(Pos.x(chestPosInternal), 0, Pos.z(chestPosInternal));
782-
long lootSeed = lootSeeds.getAtIndex(Cubiomes.C_LONG_LONG, chestIdx);
783-
Cubiomes.set_loot_seed(lootTableContext, lootSeed);
784-
Cubiomes.generate_loot(lootTableContext);
785-
int lootCount = LootTableContext.generated_item_count(lootTableContext);
786-
SimpleContainer container = new SimpleContainer(3 * 9);
787-
for (int lootIdx = 0; lootIdx < lootCount; lootIdx++) {
788-
MemorySegment itemStackInternal = ItemStack.asSlice(LootTableContext.generated_items(lootTableContext), lootIdx);
789-
int itemId = Cubiomes.get_global_item_id(lootTableContext, ItemStack.item(itemStackInternal));
790-
Item item = ItemAndEnchantmentsPredicateArgument.ITEM_ID_TO_MC.get(itemId);
791-
net.minecraft.world.item.ItemStack itemStack = new net.minecraft.world.item.ItemStack(item, ItemStack.count(itemStackInternal));
792-
MemorySegment enchantments = ItemStack.enchantments(itemStackInternal);
793-
int enchantmentCount = ItemStack.enchantment_count(itemStackInternal);
794-
for (int enchantmentIdx = 0; enchantmentIdx < enchantmentCount; enchantmentIdx++) {
795-
MemorySegment enchantInstance = EnchantInstance.asSlice(enchantments, enchantmentIdx);
796-
int itemEnchantment = EnchantInstance.enchantment(enchantInstance);
797-
ResourceKey<Enchantment> enchantmentResourceKey = ItemAndEnchantmentsPredicateArgument.ENCHANTMENT_ID_TO_MC.get(itemEnchantment);
798-
Holder.Reference<Enchantment> enchantmentReference = this.enchantmentsRegistry.getOrThrow(enchantmentResourceKey);
799-
itemStack.enchant(enchantmentReference, EnchantInstance.level(enchantInstance));
774+
try {
775+
if (Cubiomes.init_loot_table_name(lootTableContext, lootTableInternal, this.version) == 0) {
776+
continue;
777+
}
778+
MemorySegment chestPoses = Piece.chestPoses(piece);
779+
MemorySegment lootSeeds = Piece.lootSeeds(piece);
780+
for (int chestIdx = 0; chestIdx < chestCount; chestIdx++) {
781+
MemorySegment chestPosInternal = Pos.asSlice(chestPoses, chestIdx);
782+
BlockPos chestPos = new BlockPos(Pos.x(chestPosInternal), 0, Pos.z(chestPosInternal));
783+
long lootSeed = lootSeeds.getAtIndex(Cubiomes.C_LONG_LONG, chestIdx);
784+
Cubiomes.set_loot_seed(lootTableContext, lootSeed);
785+
Cubiomes.generate_loot(lootTableContext);
786+
int lootCount = LootTableContext.generated_item_count(lootTableContext);
787+
SimpleContainer container = new SimpleContainer(3 * 9);
788+
for (int lootIdx = 0; lootIdx < lootCount; lootIdx++) {
789+
MemorySegment itemStackInternal = ItemStack.asSlice(LootTableContext.generated_items(lootTableContext), lootIdx);
790+
int itemId = Cubiomes.get_global_item_id(lootTableContext, ItemStack.item(itemStackInternal));
791+
Item item = ItemAndEnchantmentsPredicateArgument.ITEM_ID_TO_MC.get(itemId);
792+
net.minecraft.world.item.ItemStack itemStack = new net.minecraft.world.item.ItemStack(item, ItemStack.count(itemStackInternal));
793+
MemorySegment enchantments = ItemStack.enchantments(itemStackInternal);
794+
int enchantmentCount = ItemStack.enchantment_count(itemStackInternal);
795+
for (int enchantmentIdx = 0; enchantmentIdx < enchantmentCount; enchantmentIdx++) {
796+
MemorySegment enchantInstance = EnchantInstance.asSlice(enchantments, enchantmentIdx);
797+
int itemEnchantment = EnchantInstance.enchantment(enchantInstance);
798+
ResourceKey<Enchantment> enchantmentResourceKey = ItemAndEnchantmentsPredicateArgument.ENCHANTMENT_ID_TO_MC.get(itemEnchantment);
799+
Holder.Reference<Enchantment> enchantmentReference = this.enchantmentsRegistry.getOrThrow(enchantmentResourceKey);
800+
itemStack.enchant(enchantmentReference, EnchantInstance.level(enchantInstance));
801+
}
802+
container.addItem(itemStack);
800803
}
801-
container.addItem(itemStack);
804+
chestLootDataList.add(new ChestLootData(structure, pieceName, chestPos, lootSeed, lootTable, container));
802805
}
803-
chestLootDataList.add(new ChestLootData(structure, pieceName, chestPos, lootSeed, lootTable, container));
806+
} finally {
807+
Cubiomes.free_loot_table_pools(lootTableContext);
804808
}
805-
Cubiomes.free_loot_table_pools(lootTableContext);
806809
}
807810
if (!chestLootDataList.isEmpty()) {
808811
this.chestLootWidget = new ChestLootWidget(widget.x + widget.width() / 2, widget.y + widget.height() / 2, chestLootDataList);

0 commit comments

Comments
 (0)