Add default implementation for Property id lookups

Just in case mods implement their own Properties which are
neither boolean, enum, nor integer.
This commit is contained in:
Spottedleaf
2024-08-08 23:31:09 -07:00
parent 4b8778cbe7
commit f4f76707ed
5 changed files with 43 additions and 2 deletions

View File

@@ -49,6 +49,11 @@ abstract class BooleanPropertyMixin extends Property<Boolean> implements Propert
return false;
}
@Override
public final boolean moonrise$requiresDefaultImpl() {
return false;
}
@Override
public final int moonrise$getIdFor(final Boolean value) {
return value.booleanValue() ? 1 : 0;

View File

@@ -50,6 +50,11 @@ abstract class EnumPropertyMixin<T extends Enum<T> & StringRepresentable> extend
return this == obj;
}
@Override
public final boolean moonrise$requiresDefaultImpl() {
return false;
}
@Override
public final int moonrise$getIdFor(final T value) {
final Class<T> target = this.getValueClass();

View File

@@ -33,6 +33,11 @@ abstract class IntegerPropertyMixin extends Property<Integer> implements Propert
return this == obj;
}
@Override
public final boolean moonrise$requiresDefaultImpl() {
return false;
}
@Override
public final int moonrise$getIdFor(final Integer value) {
final int val = value.intValue();

View File

@@ -1,24 +1,33 @@
package ca.spottedleaf.moonrise.mixin.blockstate_propertyaccess;
import ca.spottedleaf.moonrise.patches.blockstate_propertyaccess.PropertyAccess;
import it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap;
import net.minecraft.world.level.block.state.properties.Property;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Overwrite;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import java.util.Collection;
import java.util.concurrent.atomic.AtomicInteger;
@Mixin(Property.class)
abstract class PropertyMixin<T extends Comparable<T>> implements PropertyAccess<T> {
@Shadow
public abstract Collection<T> getPossibleValues();
@Unique
private static final AtomicInteger ID_GENERATOR = new AtomicInteger();
@Unique
private int id;
@Unique
private Object2IntOpenHashMap<T> defaultById;
/**
* @reason Hook into constructor to init fields
* @author Spottedleaf
@@ -31,6 +40,17 @@ abstract class PropertyMixin<T extends Comparable<T>> implements PropertyAccess<
)
private void initId(final CallbackInfo ci) {
this.id = ID_GENERATOR.getAndIncrement();
final Collection<T> values = this.getPossibleValues();
if (this.moonrise$requiresDefaultImpl()) {
this.defaultById = new Object2IntOpenHashMap<>(values.size());
int id = 0;
for (final T value : values) {
this.defaultById.put(value, id++);
}
}
}
@Override
@@ -48,7 +68,9 @@ abstract class PropertyMixin<T extends Comparable<T>> implements PropertyAccess<
return this == obj;
}
// this is re-declared here so that calls to Property#getIdFor are a virtual method invoke, rather than an interface invoke
// add default implementation in case mods create their own Properties
@Override
public abstract int moonrise$getIdFor(final T value);
public int moonrise$getIdFor(final T value) {
return this.defaultById.getOrDefault(value, -1);
}
}

View File

@@ -6,4 +6,8 @@ public interface PropertyAccess<T> {
public int moonrise$getIdFor(final T value);
public default boolean moonrise$requiresDefaultImpl() {
return true;
}
}