Skip to content

Commit

Permalink
Merge pull request #871 from LLytho/registry-wrapper-utils
Browse files Browse the repository at this point in the history
Add some utils to `RegistryWrapper`
  • Loading branch information
LatvianModder authored Jul 23, 2024
2 parents d8a6589 + 19d1a17 commit 1e8750a
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 13 deletions.
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);
}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
package dev.latvian.mods.kubejs.bindings;

import dev.latvian.mods.kubejs.holder.HolderWrapper;
import dev.latvian.mods.kubejs.util.RegistryAccessContainer;
import dev.latvian.mods.kubejs.util.UtilsJS;
import dev.latvian.mods.rhino.Context;
import net.minecraft.core.Holder;
import net.minecraft.core.HolderSet;
import net.minecraft.core.Registry;
import net.minecraft.resources.ResourceKey;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.util.RandomSource;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.List;
import java.util.ListIterator;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

Expand All @@ -27,6 +33,10 @@ public boolean contains(ResourceLocation id) {
return registry.containsKey(id);
}

public boolean containsValue(T value) {
return registry.containsValue(value);
}

public Set<Map.Entry<ResourceLocation, T>> getEntrySet() {
return registry.entrySet().stream().map(e -> Map.entry(e.getKey().location(), e.getValue())).collect(Collectors.toSet());
}
Expand All @@ -35,6 +45,11 @@ public Map<ResourceLocation, T> getValueMap() {
return registry.entrySet().stream().collect(Collectors.toMap(e -> e.getKey().location(), Map.Entry::getValue));
}

public HolderSetWrapper<T> getValues(Object filter) {
var holderSet = HolderWrapper.wrapSimpleSet(registry, filter);
return new HolderSetWrapper<>(registry, Objects.requireNonNullElseGet(holderSet, HolderSet::empty));
}

public List<T> getValues() {
return registry.stream().collect(Collectors.toList());
}
Expand All @@ -43,6 +58,16 @@ public Set<ResourceLocation> getKeys() {
return registry.keySet();
}

@Nullable
public T getRandom() {
return getRandom(UtilsJS.RANDOM);
}

@Nullable
public T getRandom(RandomSource random) {
return registry.getRandom(random).map(Holder::value).orElse(null);
}

@Nullable
public ResourceLocation getId(T value) {
return registry.getKey(value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import net.minecraft.sounds.SoundEvent;
import net.minecraft.stats.Stat;
import net.minecraft.stats.Stats;
import net.minecraft.util.RandomSource;
import net.minecraft.world.item.CreativeModeTab;
import org.jetbrains.annotations.Nullable;

Expand All @@ -28,13 +29,13 @@
@Info("A collection of utilities")
public interface UtilsWrapper {
@Info("Get a Random, for generating random numbers. Note this will always return the same Random instance")
static Random getRandom() {
static RandomSource getRandom() {
return UtilsJS.RANDOM;
}

@Info("Get a new random with the specified seed")
static Random newRandom(long seed) {
return new Random(seed);
static RandomSource newRandom(long seed) {
return RandomSource.create(seed);
}

@Info("Get an immutable empty list")
Expand Down
29 changes: 20 additions & 9 deletions src/main/java/dev/latvian/mods/kubejs/holder/HolderWrapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@
import dev.latvian.mods.rhino.type.TypeInfo;
import net.minecraft.core.Holder;
import net.minecraft.core.HolderSet;
import net.minecraft.resources.ResourceKey;
import net.minecraft.core.Registry;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.tags.TagKey;
import org.jetbrains.annotations.Nullable;

import java.util.List;

Expand Down Expand Up @@ -59,6 +60,22 @@ static HolderSet<?> wrapSet(KubeJSContext cx, Object from, TypeInfo param) {

var registry = cx.lookupRegistry(param, from);

var simpleHolders = wrapSimpleSet(registry, from);
if (simpleHolders != null) {
return simpleHolders;
}

if (from instanceof Iterable<?>) {
var holder = (List) cx.jsToJava(from, TypeInfo.RAW_LIST.withParams(HOLDER.withParams(param)));
return HolderSet.direct(holder);
} else {
var holder = wrap(cx, from, param);
return HolderSet.direct(holder);
}
}

@Nullable
static <T> HolderSet<T> wrapSimpleSet(Registry<T> registry, Object from) {
var regex = RegExpKJS.wrap(from);

if (regex != null) {
Expand All @@ -73,17 +90,11 @@ static HolderSet<?> wrapSet(KubeJSContext cx, Object from, TypeInfo param) {
} else if (s.charAt(0) == '@') {
return new NamespaceHolderSet<>(registry.asLookup(), s.substring(1));
} else if (s.charAt(0) == '#') {
var tagKey = TagKey.create((ResourceKey) registry.key(), ResourceLocation.parse(s.substring(1)));
var tagKey = TagKey.create(registry.key(), ResourceLocation.parse(s.substring(1)));
return registry.getOrCreateTag(tagKey);
}
}

if (from instanceof Iterable<?>) {
var holder = (List) cx.jsToJava(from, TypeInfo.RAW_LIST.withParams(HOLDER.withParams(param)));
return HolderSet.direct(holder);
} else {
var holder = wrap(cx, from, param);
return HolderSet.direct(holder);
}
return null;
}
}
3 changes: 2 additions & 1 deletion src/main/java/dev/latvian/mods/kubejs/util/UtilsJS.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import net.minecraft.nbt.StringTag;
import net.minecraft.nbt.Tag;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.util.RandomSource;
import net.minecraft.world.item.CreativeModeTab;
import org.jetbrains.annotations.Nullable;

Expand All @@ -48,7 +49,7 @@
import java.util.regex.Pattern;

public class UtilsJS {
public static final Random RANDOM = new Random();
public static final RandomSource RANDOM = RandomSource.create();
public static final ResourceLocation AIR_LOCATION = ResourceLocation.parse("minecraft:air");
public static final Pattern SNAKE_CASE_SPLIT = Pattern.compile("[:_/]");
public static final Set<String> ALWAYS_LOWER_CASE = new HashSet<>(Arrays.asList("a", "an", "the", "of", "on", "in", "and", "or", "but", "for"));
Expand Down

0 comments on commit 1e8750a

Please sign in to comment.