Skip to content

Commit

Permalink
Add overwrite for StateHolder#trySetValue
Browse files Browse the repository at this point in the history
Now that we do not initialise the Vanilla table field,
we need to overwrite this method as well with our own
implementation.
  • Loading branch information
Spottedleaf committed Aug 10, 2024
1 parent 65481ae commit c577819
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,20 @@ private void loadTable(final Map<Map<Property<?>, Comparable<?>>, S> map, final
public <T extends Comparable<T>, V extends T> S setValue(final Property<T> property, final V value) {
final S ret = this.optimisedTable.set(this.tableIndex, property, value);
if (ret == null) {
throw new IllegalArgumentException("Cannot set property " + property + " to " + value + " on " + this.owner + ", it is not an allowed value");
throw new IllegalArgumentException("Cannot set property " + property + " to " + value + " on " + this.owner);
}
return ret;
}

/**
* @reason Replace with optimisedTable
* @author Spottedleaf
*/
@Overwrite
public <T extends Comparable<T>, V extends T> S trySetValue(final Property<T> property, final V value) {
final S ret = this.optimisedTable.trySet(this.tableIndex, property, value, (S)(StateHolder<O, S>)(Object)this);
if (ret == null) {
throw new IllegalArgumentException("Cannot set property " + property + " to " + value + " on " + this.owner);
}
return ret;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,28 @@ public <T extends Comparable<T>> S set(final long index, final Property<T> prope
return this.lookup[(int)newIndex];
}

public <T extends Comparable<T>> S trySet(final long index, final Property<T> property, final T with, final S dfl) {
final Indexer indexer = this.propertyToIndexer.get(((PropertyAccess<T>)property).moonrise$getId());
if (indexer == null) {
return dfl;
}

final int newValueId = ((PropertyAccess<T>)property).moonrise$getIdFor(with);
if (newValueId < 0) {
return null;
}

final long divided = (index * indexer.multipleDivMagic) >>> 32;
final long modded = (((divided * indexer.modMagic) & 0xFFFFFFFFL) * indexer.totalValues) >>> 32;
// equiv to: divided = index / multiple
// modded = divided % totalValues

// subtract out the old value, add in the new
final long newIndex = (((long)newValueId - modded) * indexer.multiple) + index;

return this.lookup[(int)newIndex];
}

private static final record Indexer(
int totalValues, int multiple, long multipleDivMagic, long modMagic
) {}
Expand Down

0 comments on commit c577819

Please sign in to comment.