Skip to content

Commit ab79dca

Browse files
committed
feat: add Smaller Stacks module to reduce maximum item stack sizes
1 parent 06eeaa7 commit ab79dca

3 files changed

Lines changed: 60 additions & 1 deletion

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ The following modules are currently available:
3131
- **Bad Air Caves**: Sometimes, poisonous gas will fill caves, causing damage to players.
3232
- **Baby Creatures**: Any LivingEntity can spawn as a baby, babies are faster and smaller than their adult counterparts.
3333
- **Expensive Trades**: Villager trades are more expensive, making it harder to obtain valuable items.
34+
- **Smaller Stack Sizes**: Item stack sizes are reduced, making inventory management more challenging.
3435
- **More to come**: This plugin is a work in progress, with more modules planned for future releases.
3536

3637
## Configuration

src/main/kotlin/com/sybsuper/sybsafetyfirst/modules/ModuleManager.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ object ModuleManager {
3434
SkillBasedInventory(),
3535
BadAirCaves(),
3636
BabyCreatures(),
37-
ExpensiveTrades()
37+
ExpensiveTrades(),
38+
SmallerStacks()
3839
)
3940
private val currentModuleInstances = modules.associateBy { it.id }.toMutableMap()
4041
private val nameMap = modules.associateBy { it.name }
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package com.sybsuper.sybsafetyfirst.modules
2+
3+
import kotlinx.serialization.Serializable
4+
import org.bukkit.event.EventHandler
5+
import org.bukkit.event.inventory.InventoryClickEvent
6+
import org.bukkit.event.inventory.InventoryMoveItemEvent
7+
import org.bukkit.event.player.PlayerAttemptPickupItemEvent
8+
import org.bukkit.event.player.PlayerSwapHandItemsEvent
9+
import org.bukkit.inventory.ItemStack
10+
11+
class SmallerStacks : Module {
12+
override val description: String = "Reduces the maximum stack size of items."
13+
override var options: ModuleOptions = SmallerStacksOptions()
14+
val typeSafeOptions
15+
get() = (options as? SmallerStacksOptions)
16+
?: error("Options are not of type SmallerStacksOptions")
17+
18+
@Serializable
19+
data class SmallerStacksOptions(
20+
override var enabled: Boolean = true,
21+
/**
22+
* The maximum stack size for all items.
23+
*/
24+
var maxStackSize: Int = 31
25+
) : ModuleOptions
26+
27+
fun patchItem(item: ItemStack?) {
28+
val meta = item?.itemMeta ?: return
29+
val maxStackSize = if (meta.hasMaxStackSize()) {
30+
meta.maxStackSize
31+
} else {
32+
item.type.maxStackSize
33+
}
34+
meta.setMaxStackSize(maxStackSize.coerceAtMost(typeSafeOptions.maxStackSize))
35+
item.itemMeta = meta
36+
}
37+
38+
@EventHandler(ignoreCancelled = true)
39+
fun on(event: InventoryClickEvent) {
40+
patchItem(event.currentItem ?: return)
41+
}
42+
43+
@EventHandler(ignoreCancelled = true)
44+
fun on(event: PlayerAttemptPickupItemEvent) {
45+
patchItem(event.item.itemStack)
46+
}
47+
48+
@EventHandler(ignoreCancelled = true)
49+
fun on(event: PlayerSwapHandItemsEvent) {
50+
patchItem(event.mainHandItem ?: return)
51+
}
52+
53+
@EventHandler(ignoreCancelled = true)
54+
fun on(event: InventoryMoveItemEvent) {
55+
patchItem(event.item ?: return)
56+
}
57+
}

0 commit comments

Comments
 (0)