-
Notifications
You must be signed in to change notification settings - Fork 93
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #871 from LLytho/registry-wrapper-utils
Add some utils to `RegistryWrapper`
- Loading branch information
Showing
5 changed files
with
118 additions
and
13 deletions.
There are no files selected for viewing
67 changes: 67 additions & 0 deletions
67
src/main/java/dev/latvian/mods/kubejs/bindings/HolderSetWrapper.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,67 @@ | ||
package dev.latvian.mods.kubejs.bindings; | ||
|
||
import com.google.common.collect.Iterators; | ||
import dev.latvian.mods.kubejs.util.UtilsJS; | ||
import net.minecraft.core.Holder; | ||
import net.minecraft.core.HolderSet; | ||
import net.minecraft.core.Registry; | ||
import net.minecraft.resources.ResourceLocation; | ||
import net.minecraft.util.RandomSource; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
import java.util.Iterator; | ||
import java.util.List; | ||
import java.util.Objects; | ||
import java.util.Set; | ||
import java.util.stream.Collectors; | ||
|
||
public record HolderSetWrapper<T>(Registry<T> registry, HolderSet<T> holders) implements Iterable<T> { | ||
|
||
public int size() { | ||
return holders.size(); | ||
} | ||
|
||
public boolean isEmpty() { | ||
return holders.size() == 0; | ||
} | ||
|
||
public boolean contains(ResourceLocation id) { | ||
return registry.getHolder(id).filter(holders::contains).isPresent(); | ||
} | ||
|
||
public boolean containsValue(T value) { | ||
return holders.contains(registry.wrapAsHolder(value)); | ||
} | ||
|
||
public List<T> getValues() { | ||
return holders.stream().map(Holder::value).toList(); | ||
} | ||
|
||
public Set<ResourceLocation> getKeys() { | ||
return holders.stream().map(holder -> { | ||
var key = holder.getKey(); | ||
if (key == null) { | ||
return null; | ||
} | ||
|
||
return key.location(); | ||
}).filter(Objects::nonNull).collect(Collectors.toSet()); | ||
} | ||
|
||
@Nullable | ||
public T getRandom() { | ||
return getRandom(UtilsJS.RANDOM); | ||
} | ||
|
||
@Nullable | ||
public T getRandom(RandomSource random) { | ||
return holders.getRandomElement(random).map(Holder::value).orElse(null); | ||
} | ||
|
||
@NotNull | ||
@Override | ||
public Iterator<T> iterator() { | ||
return Iterators.transform(holders.iterator(), Holder::value); | ||
} | ||
} |
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
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