Skip to content

Commit e6ca223

Browse files
committed
refactor: fix critical bugs and code quality issues (1.21.x port)
Same fixes as on 1.20.x and dev branches: - fabric.mod.json: dynamic minecraft_version dep - StackableTools.kt: /stackabletools reload subcommand, gio open - ScreenHandlerMixin: extracted tryMerge helper - build.gradle.kts: processResources minecraft_dependency, resources srcDir - Deleted dead files: compile.sh, compile_config.*, gradle-mc-versions.properties
1 parent 162d3c7 commit e6ca223

15 files changed

Lines changed: 167 additions & 395 deletions

File tree

build.gradle.kts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,6 @@ loom {
3535
}
3636

3737
fabricApi {
38-
configureDataGeneration {
39-
client = true
40-
}
41-
4238
configureTests {
4339
createSourceSet = true
4440
modId = "stackabletools-test"
@@ -48,9 +44,10 @@ fabricApi {
4844
}
4945
}
5046

51-
sourceSets {
47+
sourceSets {
5248
named("main") {
5349
java.srcDir("src/mc-$mcVersion/kotlin")
50+
resources.srcDir("src/mc-$mcVersion/resources")
5451
}
5552
named("gametest") {
5653
java.setSrcDirs(listOf("src/mc-$mcVersion/gametest/kotlin"))
@@ -75,10 +72,13 @@ dependencies {
7572
// Afin d'empaqueter toml4j dans le JAR, on inclut le runtime dans la tâche jar.
7673

7774
tasks.processResources {
75+
val minecraftDep = providers.gradleProperty("minecraft_version").get()
76+
7877
inputs.property("version", version)
78+
inputs.property("minecraft_dependency", minecraftDep)
7979

8080
filesMatching("fabric.mod.json") {
81-
expand("version" to version)
81+
expand("version" to version, "minecraft_dependency" to minecraftDep)
8282
}
8383
}
8484

compile.sh

Lines changed: 0 additions & 192 deletions
This file was deleted.

compile_config_sample.json

Lines changed: 0 additions & 7 deletions
This file was deleted.

gradle-mc-versions.properties

Lines changed: 0 additions & 8 deletions
This file was deleted.

src/main/resources/fabric.mod.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
"mixins": ["stackabletools.mixins.json"],
3030
"depends": {
3131
"fabricloader": ">=0.18.5",
32-
"minecraft": ">=1.21.0",
32+
"minecraft": "${minecraft_dependency}",
3333
"java": ">=21",
3434
"fabric-api": "*",
3535
"fabric-language-kotlin": "*"

src/mc-1.21.0-1.21.3/kotlin/stackabletools/StackableTools.kt

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -123,13 +123,16 @@ object StackableTools : ModInitializer {
123123
)
124124

125125
dispatcher.register(
126-
CommandManager.literal("reload")
127-
.requires { it.hasPermissionLevel(2) }
128-
.executes { context ->
129-
ConfigManager.loadConfig()
130-
context.source.sendFeedback({ Text.literal("Configuration reloaded!") }, false)
131-
1
132-
}
126+
CommandManager.literal("stackabletools")
127+
.then(
128+
CommandManager.literal("reload")
129+
.requires { it.hasPermissionLevel(2) }
130+
.executes { context ->
131+
ConfigManager.loadConfig()
132+
context.source.sendFeedback({ Text.literal("Configuration reloaded!") }, false)
133+
1
134+
}
135+
)
133136
)
134137

135138
dispatcher.register(
@@ -164,7 +167,7 @@ object StackableTools : ModInitializer {
164167
// Attempt 3: gnome-open (Legacy systems)
165168
if (!opened) {
166169
try {
167-
Runtime.getRuntime().exec(arrayOf("gnome-open", configFile.path))
170+
Runtime.getRuntime().exec(arrayOf("gio", "open", configFile.path))
168171
opened = true
169172
} catch (_: Exception) {}
170173
}

src/mc-1.21.0-1.21.3/kotlin/stackabletools/mixin/ScreenHandlerMixin.kt

Lines changed: 23 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -28,39 +28,30 @@ abstract class ScreenHandlerMixin {
2828
* Injected into the onSlotClick function to monitor and modify player mouse clicks within inventories.
2929
* We manually handle the PICKUP action when the item is marked as stackable by the mod.
3030
*/
31+
private fun tryMerge(handler: ScreenHandler, slotIndex: Int): Boolean {
32+
if (slotIndex < 0 || slotIndex >= handler.slots.size) return false
33+
val slot = handler.getSlot(slotIndex)
34+
val cursorStack = handler.cursorStack
35+
val slotStack = slot.stack
36+
if (cursorStack.isEmpty || slotStack.isEmpty) return false
37+
if (!StackableToolsUtils.isStackableItem(cursorStack) || !StackableToolsUtils.canStackItems(cursorStack, slotStack)) return false
38+
39+
val cfg = ConfigManager.getConfig().stacking
40+
val maxStack = StackableToolsUtils.maxStackFor(cursorStack, cfg)
41+
42+
val canTransfer = (maxStack - slotStack.count).coerceAtLeast(0)
43+
if (canTransfer <= 0) return false
44+
val toTransfer = minOf(cursorStack.count, canTransfer)
45+
slotStack.count += toTransfer
46+
cursorStack.count -= toTransfer
47+
handler.syncState()
48+
return true
49+
}
50+
3151
@Inject(method = ["onSlotClick"], at = [At("HEAD")], cancellable = true)
32-
private fun onSlotClick(slotIndex: Int, button: Int, actionActionType: SlotActionType, player: PlayerEntity, ci: CallbackInfo) {
33-
// Run logic only on the server to prevent synchronization mismatches
52+
private fun onSlotClick(slotIndex: Int, button: Int, actionType: SlotActionType, player: PlayerEntity, ci: CallbackInfo) {
3453
if (player.getWorld().isClient) return
35-
36-
if (actionActionType == SlotActionType.PICKUP) {
37-
val handler = this as Any as ScreenHandler
38-
if (slotIndex < 0 || slotIndex >= handler.slots.size) return
39-
40-
val slot = handler.getSlot(slotIndex)
41-
val cursorStack = handler.cursorStack
42-
val slotStack = slot.stack
43-
44-
// Logic: if current cursor item and slot item are the same "stackable" tool/potion, merge them manually
45-
if (!cursorStack.isEmpty && !slotStack.isEmpty &&
46-
StackableToolsUtils.isStackableItem(cursorStack) &&
47-
StackableToolsUtils.canStackItems(cursorStack, slotStack)) {
48-
49-
val cfg = ConfigManager.getConfig().stacking
50-
// Determine limits for the category
51-
val maxStack = StackableToolsUtils.maxStackFor(cursorStack, cfg)
52-
53-
val canTransfer = (maxStack - slotStack.count).coerceAtLeast(0)
54-
if (canTransfer > 0) {
55-
val toTransfer = minOf(cursorStack.count, canTransfer)
56-
slotStack.count += toTransfer
57-
cursorStack.count -= toTransfer
58-
59-
// Sync the state with client side to ensure visual representation is correct
60-
handler.syncState()
61-
ci.cancel() // Interrupt vanilla behavior
62-
}
63-
}
64-
}
54+
val handler = this as Any as ScreenHandler
55+
if (actionType == SlotActionType.PICKUP && tryMerge(handler, slotIndex)) ci.cancel()
6556
}
6657
}

src/mc-1.21.11/kotlin/stackabletools/StackableTools.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ object StackableTools : ModInitializer {
144144

145145
if (!opened) {
146146
try {
147-
Runtime.getRuntime().exec(arrayOf("gnome-open", configFile.path))
147+
Runtime.getRuntime().exec(arrayOf("gio", "open", configFile.path))
148148
opened = true
149149
} catch (_: Exception) {}
150150
}

0 commit comments

Comments
 (0)