Skip to content

Commit a68d724

Browse files
authored
Add rollLootTable method for loot generation and update version (#367)
2 parents 7cd2588 + 16e935a commit a68d724

5 files changed

Lines changed: 73 additions & 1 deletion

File tree

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ org.jetbrains.dokka.experimental.gradle.pluginMode=V2Enabled
77
javaVersion=25
88
mcVersion=26.1.2
99
group=dev.slne.surf.api
10-
version=3.17.2
10+
version=3.18.0
1111
relocationPrefix=dev.slne.surf.api.libs
1212
snapshot=false

surf-api-paper/surf-api-paper-nms/surf-api-paper-nms-v1-21-11/src/main/kotlin/dev/slne/surf/api/paper/server/nms/v1_21_11/bridges/V1_21_11SurfPaperNmsLootTableBridgeImpl.kt

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,36 @@ class V1_21_11SurfPaperNmsLootTableBridgeImpl : SurfPaperNmsLootTableBridge {
5252
return lootTable.getRandomItems(lootParams, nmsEntity.lootTableSeed)
5353
.mapTo(mutableObjectListOf()) { it.toBukkit() }
5454
}
55+
56+
override fun rollLootTable(
57+
entity: LivingEntity,
58+
damageSource: DamageSource,
59+
causedByPlayer: Boolean
60+
): Collection<ItemStack> {
61+
val nmsEntity = entity.toNms()
62+
val lootTableKey = nmsEntity.lootTable.getOrNull() ?: return emptyObjectList()
63+
val lootTable = MinecraftServer.getServer().reloadableRegistries().getLootTable(lootTableKey)
64+
val nmsDamageSource = damageSource.toNms()
65+
66+
val paramBuilder = LootParams.Builder(nmsEntity.level() as ServerLevel)
67+
.withParameter(LootContextParams.THIS_ENTITY, nmsEntity)
68+
.withParameter(LootContextParams.ORIGIN, nmsEntity.position())
69+
.withParameter(LootContextParams.DAMAGE_SOURCE, nmsDamageSource)
70+
.withOptionalParameter(LootContextParams.ATTACKING_ENTITY, nmsDamageSource.entity)
71+
.withOptionalParameter(
72+
LootContextParams.DIRECT_ATTACKING_ENTITY,
73+
nmsDamageSource.directEntity
74+
)
75+
76+
val lastHurtByPlayer = nmsEntity.getLastHurtByPlayer()
77+
if (causedByPlayer && lastHurtByPlayer != null) {
78+
paramBuilder.withParameter(LootContextParams.LAST_DAMAGE_PLAYER, lastHurtByPlayer)
79+
.withLuck(lastHurtByPlayer.luck)
80+
}
81+
82+
val lootParams = paramBuilder.create(LootContextParamSets.ENTITY)
83+
84+
return lootTable.getRandomItems(lootParams, nmsEntity.lootTableSeed)
85+
.mapTo(mutableObjectListOf()) { it.toBukkit() }
86+
}
5587
}

surf-api-paper/surf-api-paper-nms/surf-api-paper-nms-v26-1/src/main/kotlin/dev/slne/surf/api/paper/server/nms/v26_1/bridges/V26_1SurfPaperNmsLootTableBridgeImpl.kt

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,36 @@ class V26_1SurfPaperNmsLootTableBridgeImpl : SurfPaperNmsLootTableBridge {
5353
return lootTable.getRandomItems(lootParams, nmsEntity.lootTableSeed)
5454
.mapTo(mutableObjectListOf()) { it.toBukkit() }
5555
}
56+
57+
override fun rollLootTable(
58+
entity: LivingEntity,
59+
damageSource: DamageSource,
60+
causedByPlayer: Boolean
61+
): Collection<ItemStack> {
62+
val nmsEntity = entity.toNms()
63+
val lootTableKey = nmsEntity.lootTable.getOrNull() ?: return emptyObjectList()
64+
val lootTable = MinecraftServer.getServer().reloadableRegistries().getLootTable(lootTableKey)
65+
val nmsDamageSource = damageSource.toNms()
66+
67+
val paramBuilder = LootParams.Builder(nmsEntity.level() as ServerLevel)
68+
.withParameter(LootContextParams.THIS_ENTITY, nmsEntity)
69+
.withParameter(LootContextParams.ORIGIN, nmsEntity.position())
70+
.withParameter(LootContextParams.DAMAGE_SOURCE, nmsDamageSource)
71+
.withOptionalParameter(LootContextParams.ATTACKING_ENTITY, nmsDamageSource.entity)
72+
.withOptionalParameter(
73+
LootContextParams.DIRECT_ATTACKING_ENTITY,
74+
nmsDamageSource.directEntity
75+
)
76+
77+
val lastHurtByPlayer = nmsEntity.getLastHurtByPlayer()
78+
if (causedByPlayer && lastHurtByPlayer != null) {
79+
paramBuilder.withParameter(LootContextParams.LAST_DAMAGE_PLAYER, lastHurtByPlayer)
80+
.withLuck(lastHurtByPlayer.luck)
81+
}
82+
83+
val lootParams = paramBuilder.create(LootContextParamSets.ENTITY)
84+
85+
return lootTable.getRandomItems(lootParams, nmsEntity.lootTableSeed)
86+
.mapTo(mutableObjectListOf()) { it.toBukkit() }
87+
}
5688
}

surf-api-paper/surf-api-paper/api/surf-api-paper.api

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1703,11 +1703,13 @@ public final class dev/slne/surf/api/paper/nms/bridges/SurfPaperNmsItemBridge$Co
17031703
public abstract interface class dev/slne/surf/api/paper/nms/bridges/SurfPaperNmsLootTableBridge {
17041704
public static final field Companion Ldev/slne/surf/api/paper/nms/bridges/SurfPaperNmsLootTableBridge$Companion;
17051705
public abstract fun getDifferentLootTable (Lorg/bukkit/entity/LivingEntity;Lorg/bukkit/damage/DamageSource;Lorg/bukkit/entity/EntityType;Z)Ljava/util/Collection;
1706+
public abstract fun rollLootTable (Lorg/bukkit/entity/LivingEntity;Lorg/bukkit/damage/DamageSource;Z)Ljava/util/Collection;
17061707
}
17071708

17081709
public final class dev/slne/surf/api/paper/nms/bridges/SurfPaperNmsLootTableBridge$Companion : dev/slne/surf/api/paper/nms/bridges/SurfPaperNmsLootTableBridge {
17091710
public fun getDifferentLootTable (Lorg/bukkit/entity/LivingEntity;Lorg/bukkit/damage/DamageSource;Lorg/bukkit/entity/EntityType;Z)Ljava/util/Collection;
17101711
public final fun getINSTANCE ()Ldev/slne/surf/api/paper/nms/bridges/SurfPaperNmsLootTableBridge;
1712+
public fun rollLootTable (Lorg/bukkit/entity/LivingEntity;Lorg/bukkit/damage/DamageSource;Z)Ljava/util/Collection;
17111713
}
17121714

17131715
public abstract interface class dev/slne/surf/api/paper/nms/bridges/SurfPaperNmsNbtBridge {

surf-api-paper/surf-api-paper/src/main/kotlin/dev/slne/surf/api/paper/nms/bridges/SurfPaperNmsLootTableBridge.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@ interface SurfPaperNmsLootTableBridge {
1717
causedByPlayer: Boolean,
1818
): Collection<ItemStack>
1919

20+
fun rollLootTable(
21+
entity: LivingEntity,
22+
damageSource: DamageSource,
23+
causedByPlayer: Boolean,
24+
): Collection<ItemStack>
25+
2026
companion object : SurfPaperNmsLootTableBridge by bridge {
2127
val INSTANCE get() = bridge
2228
}

0 commit comments

Comments
 (0)