Make property changes more robust (#2962)

This commit is contained in:
Hannes Greule
2024-11-04 12:22:06 +01:00
committed by GitHub
parent bd94632c47
commit 7281fa360f
3 changed files with 21 additions and 1 deletions

View File

@ -137,4 +137,9 @@ public class PropertyKey implements Comparable<PropertyKey> {
return Integer.compare(this.id, o.id); return Integer.compare(this.id, o.id);
} }
@Override
public String toString() {
return "PropertyKey[" + getName() + "]";
}
} }

View File

@ -351,7 +351,7 @@ public class BlockState implements BlockStateHolder<BlockState>, Pattern {
BlockState newState = this; BlockState newState = this;
for (Property<?> prop : ot.getProperties()) { for (Property<?> prop : ot.getProperties()) {
PropertyKey key = prop.getKey(); PropertyKey key = prop.getKey();
if (blockType.hasProperty(key)) { if (blockType.hasPropertyOfType(key, prop.getClass())) {
newState = newState.with(key, other.getState(key)); newState = newState.with(key, other.getState(key));
} }
} }

View File

@ -247,6 +247,21 @@ public class BlockType implements Keyed, Pattern {
return this.settings.propertiesMapArr.length > ordinal && this.settings.propertiesMapArr[ordinal] != null; return this.settings.propertiesMapArr.length > ordinal && this.settings.propertiesMapArr[ordinal] != null;
} }
/**
* {@return whether this block type has a property with given key of the given type}
*
* @param key the key identifying the property
* @param propertyType the expected type of the property
* @since TODO
*/
public boolean hasPropertyOfType(PropertyKey key, Class<?> propertyType) {
int ordinal = key.getId();
Property<?> property;
return this.settings.propertiesMapArr.length > ordinal
&& (property = this.settings.propertiesMapArr[ordinal]) != null
&& property.getClass() == propertyType;
}
public <V> Property<V> getProperty(PropertyKey key) { public <V> Property<V> getProperty(PropertyKey key) {
try { try {
return (Property<V>) this.settings.propertiesMapArr[key.getId()]; return (Property<V>) this.settings.propertiesMapArr[key.getId()];