Skip to content

Commit

Permalink
Added Registry wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
LatvianModder committed Jul 3, 2024
1 parent fad66ff commit e17804a
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import dev.latvian.mods.kubejs.bindings.JavaWrapper;
import dev.latvian.mods.kubejs.bindings.KMath;
import dev.latvian.mods.kubejs.bindings.ParticleOptionsWrapper;
import dev.latvian.mods.kubejs.bindings.RegistryWrapper;
import dev.latvian.mods.kubejs.bindings.SizedIngredientWrapper;
import dev.latvian.mods.kubejs.bindings.TextIcons;
import dev.latvian.mods.kubejs.bindings.TextWrapper;
Expand Down Expand Up @@ -423,6 +424,7 @@ public void registerBindings(BindingRegistry bindings) {
bindings.add("SizedIngredient", SizedIngredientWrapper.class);
bindings.add("ChancedItem", ChancedItem.class);
bindings.add("ParticleOptions", ParticleOptionsWrapper.class);
bindings.add("Registry", RegistryWrapper.class);

bindings.add("Fluid", FluidWrapper.class);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package dev.latvian.mods.kubejs.bindings;

import dev.latvian.mods.kubejs.script.KubeJSContext;
import dev.latvian.mods.rhino.Context;
import net.minecraft.core.Registry;
import net.minecraft.resources.ResourceLocation;
import org.jetbrains.annotations.Nullable;

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

public record RegistryWrapper<T>(Registry<T> registry) {
public static RegistryWrapper<?> of(Context cx, ResourceLocation id) {
return ((KubeJSContext) cx).getRegistries().wrapRegistry(id);
}

public T get(ResourceLocation id) {
return registry.get(id);
}

public boolean contains(ResourceLocation id) {
return registry.containsKey(id);
}

public Set<Map.Entry<ResourceLocation, T>> getEntrySet() {
return registry.entrySet().stream().map(e -> Map.entry(e.getKey().location(), e.getValue())).collect(Collectors.toSet());
}

public Map<ResourceLocation, T> getValueMap() {
return registry.entrySet().stream().collect(Collectors.toMap(e -> e.getKey().location(), Map.Entry::getValue));
}

public List<T> getValues() {
return registry.stream().collect(Collectors.toList());
}

public Set<ResourceLocation> getKeys() {
return registry.keySet();
}

@Nullable
public ResourceLocation getId(T value) {
return registry.getKey(value);
}
}
18 changes: 0 additions & 18 deletions src/main/java/dev/latvian/mods/kubejs/bindings/UtilsWrapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import dev.latvian.mods.kubejs.util.UtilsJS;
import dev.latvian.mods.kubejs.util.WrappedJS;
import net.minecraft.Util;
import net.minecraft.resources.ResourceKey;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.sounds.SoundEvent;
import net.minecraft.stats.Stat;
Expand Down Expand Up @@ -131,23 +130,6 @@ static String toTitleCase(String s, boolean ignoreSpecial) {
return UtilsJS.toTitleCase(s, ignoreSpecial);
}

@Info("Gets the specified registry")
static RegistryInfo<?> getRegistry(ResourceLocation id) {
return RegistryInfo.of(ResourceKey.createRegistryKey(id));
}

@Info("Gets all ids from the registry with the specified id")
static List<ResourceLocation> getRegistryIds(ResourceLocation id) {
var entries = getRegistry(id).entrySet();
var list = new ArrayList<ResourceLocation>(entries.size());

for (var entry : entries) {
list.add(entry.getKey().location());
}

return list;
}

@Info("Returns a lazy value with the supplier function as its value factory")
static <T> Lazy<T> lazy(Supplier<T> supplier) {
return Lazy.of(supplier);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.mojang.serialization.JavaOps;
import com.mojang.serialization.JsonOps;
import com.mojang.serialization.MapCodec;
import dev.latvian.mods.kubejs.bindings.RegistryWrapper;
import dev.latvian.mods.kubejs.recipe.CachedTagLookup;
import dev.latvian.mods.rhino.Context;
import net.minecraft.core.Registry;
Expand Down Expand Up @@ -42,6 +43,7 @@ public final class RegistryAccessContainer {
public CachedTagLookup<Item> cachedItemTags;
public CachedTagLookup<Item> cachedBlockTags;
public CachedTagLookup<Item> cachedFluidTags;
private Map<ResourceLocation, RegistryWrapper> cachedRegistryWrappers;

public RegistryAccessContainer(RegistryAccess.Frozen access) {
this.access = access;
Expand Down Expand Up @@ -124,4 +126,12 @@ public <T> T decodeMap(Context cx, MapCodec<T> codec, Object o) {
return codec.decode(json, json.getMap(MapJS.json(cx, o)).getOrThrow()).result().orElseThrow();
}
}

public RegistryWrapper wrapRegistry(ResourceLocation id) {
if (cachedRegistryWrappers == null) {
cachedRegistryWrappers = new HashMap<>();
}

return cachedRegistryWrappers.computeIfAbsent(id, i -> new RegistryWrapper(access.registryOrThrow(ResourceKey.createRegistryKey(i))));
}
}

0 comments on commit e17804a

Please sign in to comment.