Skip to content

Instantly share code, notes, and snippets.

@hYdos
Created January 5, 2022 02:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hYdos/df72bdb3d470dd68db5e792420b32cd3 to your computer and use it in GitHub Desktop.
Save hYdos/df72bdb3d470dd68db5e792420b32cd3 to your computer and use it in GitHub Desktop.
e
static class NamespacedWrapper<T extends IForgeRegistryEntry<T>> extends MappedRegistry<T> implements ILockableRegistry {
private static final Logger LOGGER = LogManager.getLogger();
private boolean locked = false;
public final ForgeRegistry<T> delegate;
public NamespacedWrapper(ForgeRegistry<T> owner) {
super(owner.getRegistryKey(), Lifecycle.experimental());
this.delegate = owner;
}
public <V extends T> V registerMapping(int id, ResourceKey<T> key, V value, Lifecycle lifecycle) {
if (this.locked) {
throw new IllegalStateException("Can not register to a locked registry. Modder should use Forge Register methods.");
} else {
Validate.notNull(value);
if (value.getRegistryName() == null) {
value.setRegistryName(key.location());
}
//int realId = this.delegate.add(id, value);
int realId;
try {
Method add = ForgeRegistry.class.getDeclaredMethod("add", int.class, IForgeRegistryEntry.class);
add.setAccessible(true);
realId = (int) add.invoke(this.delegate, id, value);
} catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) {
throw new RuntimeException("Failed to add entry to registry.", e);
}
if (realId != id && id != -1) {
LOGGER.warn("Registered object did not get ID it asked for. Name: {} Type: {} Expected: {} Got: {}", key, value.getRegistryType().getName(), id, realId);
}
return value;
}
}
public <R extends T> R register(ResourceKey<T> key, R value, Lifecycle lifecycle) {
return this.registerMapping(-1, key, value, lifecycle);
}
public <V extends T> V registerOrOverride(OptionalInt id, ResourceKey<T> key, V value, Lifecycle lifecycle) {
int wanted = -1;
if (id.isPresent() && this.byId(id.getAsInt()) != null) {
wanted = id.getAsInt();
}
return this.registerMapping(wanted, key, value, lifecycle);
}
@Nullable
public T get(@Nullable ResourceLocation name) {
return this.delegate.getRaw(name);
}
public Optional<T> getOptional(@Nullable ResourceLocation name) {
return Optional.ofNullable(this.delegate.getRaw(name));
}
@Nullable
public T get(@Nullable ResourceKey<T> name) {
return name == null ? null : this.delegate.getRaw(name.location());
}
@Nullable
public ResourceLocation getKey(T value) {
return this.delegate.getKey(value);
}
public Optional<ResourceKey<T>> getResourceKey(T p_122755_) {
return this.delegate.getResourceKey(p_122755_);
}
public boolean containsKey(ResourceLocation name) {
return this.delegate.containsKey(name);
}
public int getId(@Nullable T value) {
return this.delegate.getID(value);
}
@Nullable
public T byId(int id) {
return this.delegate.getValue(id);
}
public Lifecycle lifecycle(T p_122764_) {
return Lifecycle.stable();
}
public Iterator<T> iterator() {
return this.delegate.iterator();
}
public Set<ResourceLocation> keySet() {
return this.delegate.getKeys();
}
public Set<Map.Entry<ResourceKey<T>, T>> entrySet() {
return this.delegate.getEntries();
}
@Nullable
public T getRandom(Random random) {
Collection<T> values = this.delegate.getValues();
return values.stream().skip(random.nextInt(values.size())).findFirst().orElse(null);
}
public void lock() {
this.locked = true;
}
public static class Factory<V extends IForgeRegistryEntry<V>> implements IForgeRegistry.CreateCallback<V> {
public static final ResourceLocation ID = new ResourceLocation("forge", "registry_defaulted_wrapper");
public void onCreate(IForgeRegistryInternal<V> owner, RegistryManager stage) {
owner.setSlaveMap(ID, new NamespacedWrapper((ForgeRegistry) owner));
}
}
}
private static <T extends IForgeRegistryEntry<T>> RegistryBuilder<T> makeRegistry(ResourceKey<? extends Registry<T>> key, Class<T> type) {
return (new RegistryBuilder()).setName(key.location()).setType(type).setMaxID(2147483646).addCallback(new NamespacedWrapper.Factory());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment