-
Notifications
You must be signed in to change notification settings - Fork 811
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
61 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
common/src/main/java/net/caffeinemc/mods/sodium/client/util/WeightedRandomListExtension.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package net.caffeinemc.mods.sodium.client.util; | ||
|
||
import net.minecraft.util.RandomSource; | ||
|
||
public interface WeightedRandomListExtension<T> { | ||
T sodium$getQuick(RandomSource random); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
.../java/net/caffeinemc/mods/sodium/mixin/fabric/features/model/WeightedRandomListMixin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package net.caffeinemc.mods.sodium.mixin.fabric.features.model; | ||
|
||
import com.google.common.collect.ImmutableList; | ||
import net.caffeinemc.mods.sodium.client.util.WeightedRandomListExtension; | ||
import net.minecraft.util.RandomSource; | ||
import net.minecraft.util.random.WeightedEntry; | ||
import net.minecraft.util.random.WeightedRandomList; | ||
import org.spongepowered.asm.mixin.Final; | ||
import org.spongepowered.asm.mixin.Mixin; | ||
import org.spongepowered.asm.mixin.Shadow; | ||
|
||
@Mixin(WeightedRandomList.class) | ||
public class WeightedRandomListMixin<E extends WeightedEntry> implements WeightedRandomListExtension<E> { | ||
@Shadow | ||
@Final | ||
private ImmutableList<E> items; | ||
|
||
@Shadow | ||
@Final | ||
private int totalWeight; | ||
|
||
@Override | ||
public E sodium$getQuick(RandomSource random) { | ||
int randomValue = Math.abs((int) random.nextLong()) % this.totalWeight; | ||
|
||
int i = 0; | ||
int len = items.size(); | ||
|
||
E weighted; | ||
|
||
do { | ||
if (i >= len) { | ||
return null; | ||
} | ||
|
||
weighted = items.get(i++); | ||
randomValue -= weighted.getWeight().asInt(); | ||
} while (randomValue >= 0); | ||
|
||
return weighted; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters