Skip to content

Commit c8caa26

Browse files
Implement range dispatch predicate mapping
1 parent 7059cdf commit c8caa26

File tree

3 files changed

+47
-22
lines changed

3 files changed

+47
-22
lines changed

src/main/java/org/geysermc/rainbow/mapping/BedrockItemMapper.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
import net.minecraft.client.renderer.item.properties.conditional.FishingRodCast;
1515
import net.minecraft.client.renderer.item.properties.conditional.HasComponent;
1616
import net.minecraft.client.renderer.item.properties.conditional.ItemModelPropertyTest;
17+
import net.minecraft.client.renderer.item.properties.numeric.BundleFullness;
18+
import net.minecraft.client.renderer.item.properties.numeric.Count;
19+
import net.minecraft.client.renderer.item.properties.numeric.Damage;
1720
import net.minecraft.client.renderer.item.properties.numeric.RangeSelectItemModelProperty;
1821
import net.minecraft.client.renderer.item.properties.select.Charge;
1922
import net.minecraft.client.renderer.item.properties.select.ContextDimension;
@@ -49,6 +52,7 @@
4952
import org.geysermc.rainbow.mapping.geyser.predicate.GeyserConditionPredicate;
5053
import org.geysermc.rainbow.mapping.geyser.predicate.GeyserMatchPredicate;
5154
import org.geysermc.rainbow.mapping.geyser.predicate.GeyserPredicate;
55+
import org.geysermc.rainbow.mapping.geyser.predicate.GeyserRangeDispatchPredicate;
5256
import org.geysermc.rainbow.mixin.LateBoundIdMapperAccessor;
5357
import org.geysermc.rainbow.mixin.RangeSelectItemModelAccessor;
5458
import org.geysermc.rainbow.mixin.TextureSlotsAccessor;
@@ -188,7 +192,25 @@ private static void mapConditionalModel(ConditionalItemModel.Unbaked model, Mapp
188192
}
189193

190194
private static void mapRangeSelectModel(RangeSelectItemModel.Unbaked model, MappingContext context) {
195+
RangeSelectItemModelProperty property = model.property();
196+
GeyserRangeDispatchPredicate.Property predicateProperty = switch (property) {
197+
case BundleFullness ignored -> GeyserRangeDispatchPredicate.BUNDLE_FULLNESS;
198+
case Count count -> new GeyserRangeDispatchPredicate.Count(count.normalize());
199+
// Mojang, why? :(
200+
case net.minecraft.client.renderer.item.properties.numeric.CustomModelDataProperty customModelData -> new GeyserRangeDispatchPredicate.CustomModelData(customModelData.index());
201+
case Damage damage -> new GeyserRangeDispatchPredicate.Damage(damage.normalize());
202+
default -> null;
203+
};
204+
205+
if (predicateProperty == null) {
206+
context.reporter.report(() -> "unsupported range dispatch model property " + property + ", only mapping fallback, if it is present");
207+
} else {
208+
for (RangeSelectItemModel.Entry entry : model.entries()) {
209+
mapItem(entry.model(), context.with(new GeyserRangeDispatchPredicate(predicateProperty, entry.threshold(), model.scale()), "threshold " + entry.threshold()));
210+
}
211+
}
191212

213+
model.fallback().ifPresent(fallback -> mapItem(fallback, context.child("range dispatch fallback")));
192214
}
193215

194216
@SuppressWarnings("unchecked")

src/main/java/org/geysermc/rainbow/mapping/geyser/predicate/GeyserConditionPredicate.java

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,7 @@ public MapCodec<? extends Property> codec() {
6464
}
6565

6666
public record CustomModelData(int index) implements Property {
67-
public static final MapCodec<CustomModelData> CODEC = RecordCodecBuilder.mapCodec(instance ->
68-
instance.group(
69-
ExtraCodecs.NON_NEGATIVE_INT.optionalFieldOf("index", 0).forGetter(CustomModelData::index)
70-
).apply(instance, CustomModelData::new)
71-
);
67+
public static final MapCodec<CustomModelData> CODEC = ExtraCodecs.NON_NEGATIVE_INT.optionalFieldOf("index", 0).xmap(CustomModelData::new, CustomModelData::index);
7268

7369
@Override
7470
public Type type() {
@@ -77,11 +73,7 @@ public Type type() {
7773
}
7874

7975
public record HasComponent(DataComponentType<?> component) implements Property {
80-
public static final MapCodec<HasComponent> CODEC = RecordCodecBuilder.mapCodec(instance ->
81-
instance.group(
82-
DataComponentType.CODEC.fieldOf("component").forGetter(HasComponent::component)
83-
).apply(instance, HasComponent::new)
84-
);
76+
public static final MapCodec<HasComponent> CODEC = DataComponentType.CODEC.fieldOf("component").xmap(HasComponent::new, HasComponent::component);
8577

8678
@Override
8779
public Type type() {

src/main/java/org/geysermc/rainbow/mapping/geyser/predicate/GeyserRangeDispatchPredicate.java

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,17 @@
1010

1111
import java.util.function.Supplier;
1212

13-
public record GeyserRangeDispatchPredicate(Property property, float threshold, float scale, boolean normalise) implements GeyserPredicate {
13+
public record GeyserRangeDispatchPredicate(Property property, float threshold, float scale) implements GeyserPredicate {
1414

1515
public static final MapCodec<GeyserRangeDispatchPredicate> CODEC = RecordCodecBuilder.mapCodec(instance ->
1616
instance.group(
1717
Property.CODEC.forGetter(GeyserRangeDispatchPredicate::property),
1818
Codec.FLOAT.fieldOf("threshold").forGetter(GeyserRangeDispatchPredicate::threshold),
19-
Codec.FLOAT.fieldOf("scale").forGetter(GeyserRangeDispatchPredicate::scale),
20-
Codec.BOOL.fieldOf("normalize").forGetter(GeyserRangeDispatchPredicate::normalise)
19+
Codec.FLOAT.fieldOf("scale").forGetter(GeyserRangeDispatchPredicate::scale)
2120
).apply(instance, GeyserRangeDispatchPredicate::new)
2221
);
2322

2423
public static final Property BUNDLE_FULLNESS = unit(Property.Type.BUNDLE_FULLNESS);
25-
public static final Property DAMAGE = unit(Property.Type.DAMAGE);
26-
public static final Property COUNT = unit(Property.Type.COUNT);
2724

2825
@Override
2926
public Type type() {
@@ -38,8 +35,8 @@ public interface Property {
3835

3936
enum Type implements StringRepresentable {
4037
BUNDLE_FULLNESS("bundle_fullness", () -> MapCodec.unit(GeyserRangeDispatchPredicate.BUNDLE_FULLNESS)),
41-
DAMAGE("damage", () -> MapCodec.unit(GeyserRangeDispatchPredicate.DAMAGE)),
42-
COUNT("count", () -> MapCodec.unit(GeyserRangeDispatchPredicate.COUNT)),
38+
DAMAGE("damage", () -> Damage.CODEC),
39+
COUNT("count", () -> Count.CODEC),
4340
CUSTOM_MODEL_DATA("custom_model_data", () -> CustomModelData.CODEC);
4441

4542
public static final Codec<Type> CODEC = StringRepresentable.fromEnum(Type::values);
@@ -63,12 +60,26 @@ public MapCodec<? extends Property> codec() {
6360
}
6461
}
6562

63+
public record Damage(boolean normalize) implements Property {
64+
public static final MapCodec<Damage> CODEC = Codec.BOOL.fieldOf("normalize").xmap(Damage::new, Damage::normalize);
65+
66+
@Override
67+
public Type type() {
68+
return Type.DAMAGE;
69+
}
70+
}
71+
72+
public record Count(boolean normalize) implements Property {
73+
public static final MapCodec<Count> CODEC = Codec.BOOL.fieldOf("normalize").xmap(Count::new, Count::normalize);
74+
75+
@Override
76+
public Type type() {
77+
return Type.COUNT;
78+
}
79+
}
80+
6681
public record CustomModelData(int index) implements Property {
67-
public static final MapCodec<CustomModelData> CODEC = RecordCodecBuilder.mapCodec(instance ->
68-
instance.group(
69-
ExtraCodecs.NON_NEGATIVE_INT.optionalFieldOf("index", 0).forGetter(CustomModelData::index)
70-
).apply(instance, CustomModelData::new)
71-
);
82+
public static final MapCodec<CustomModelData> CODEC = ExtraCodecs.NON_NEGATIVE_INT.optionalFieldOf("index", 0).xmap(CustomModelData::new, CustomModelData::index);
7283

7384
@Override
7485
public Type type() {

0 commit comments

Comments
 (0)