Skip to content

Commit f6338a2

Browse files
committed
Add predicate conditional
1 parent 4b4b17d commit f6338a2

File tree

3 files changed

+75
-2
lines changed

3 files changed

+75
-2
lines changed

README.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ A vanilla-like implementation of Optifine's CIT, shaped how I believe it would b
1313
- has_enchantments (if stack has all the listed enchantments/stored enchantments)
1414
- fields:
1515
- enchantments: List of enchantment identifier [REQUIRED]
16+
- predicate
17+
- fields
18+
- predicate: A item sub predicate [REQUIRED]
1619

1720
#### Range Dispatch:
1821

@@ -116,4 +119,30 @@ bone.json
116119
}
117120
}
118121
}
122+
```
123+
124+
iron_sword.json
125+
126+
```json
127+
{
128+
"model": {
129+
"type": "minecraft:condition",
130+
"property": "chit:predicate",
131+
"predicate": {
132+
"minecraft:damage": {
133+
"damage": {
134+
"min": 125
135+
}
136+
}
137+
},
138+
"on_true": {
139+
"type": "minecraft:model",
140+
"model": "minecraft:item/stone_sword"
141+
},
142+
"on_false": {
143+
"type": "minecraft:model",
144+
"model": "minecraft:item/iron_sword"
145+
}
146+
}
147+
}
119148
```

src/main/kotlin/btw/lowercase/chit/ChitClientMod.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package btw.lowercase.chit
22

33
import btw.lowercase.chit.property.conditional.HasEnchantmentConditional
44
import btw.lowercase.chit.property.conditional.HasEnchantmentsConditional
5+
import btw.lowercase.chit.property.conditional.PredicateConditional
56
import btw.lowercase.chit.property.numeric.EnchantmentLevelNumeric
67
import btw.lowercase.chit.property.select.RaritySelect
78
import net.fabricmc.api.ClientModInitializer
@@ -20,9 +21,8 @@ object ChitClientMod : ClientModInitializer {
2021
// Components
2122
SelectItemModelProperties.ID_MAPPER.put(id("rarity"), RaritySelect.CODEC)
2223

23-
// Custom Data
24-
2524
// Other
25+
ConditionalItemModelProperties.ID_MAPPER.put(id("predicate"), PredicateConditional.CODEC)
2626
}
2727

2828
fun id(path: String): ResourceLocation {
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package btw.lowercase.chit.property.conditional
2+
3+
import com.mojang.serialization.MapCodec
4+
import com.mojang.serialization.codecs.RecordCodecBuilder
5+
import net.minecraft.advancements.critereon.ItemSubPredicate
6+
import net.minecraft.advancements.critereon.SingleComponentItemPredicate
7+
import net.minecraft.client.multiplayer.ClientLevel
8+
import net.minecraft.client.renderer.item.properties.conditional.ConditionalItemModelProperty
9+
import net.minecraft.world.entity.LivingEntity
10+
import net.minecraft.world.item.ItemDisplayContext
11+
import net.minecraft.world.item.ItemStack
12+
13+
class PredicateConditional(val predicate: Map<ItemSubPredicate.Type<*>, ItemSubPredicate>) :
14+
ConditionalItemModelProperty {
15+
companion object {
16+
val CODEC = RecordCodecBuilder.mapCodec { instance ->
17+
instance.group(
18+
SingleComponentItemPredicate.CODEC.fieldOf("predicate").forGetter(PredicateConditional::predicate)
19+
).apply(instance, ::PredicateConditional)
20+
}
21+
}
22+
23+
override fun get(
24+
stack: ItemStack,
25+
clientLevel: ClientLevel?,
26+
livingEntity: LivingEntity?,
27+
layer: Int,
28+
itemDisplayContext: ItemDisplayContext,
29+
): Boolean {
30+
// TODO/NOTE: Enchantments/Potions have registry errors
31+
// TODO/NOTE: Best to use the other conditional
32+
for (entry in predicate.entries) {
33+
if (!entry.value.matches(stack)) {
34+
return false
35+
}
36+
}
37+
38+
return true
39+
}
40+
41+
override fun type(): MapCodec<out ConditionalItemModelProperty?> {
42+
return CODEC
43+
}
44+
}

0 commit comments

Comments
 (0)