Skip to content

Commit

Permalink
Initial work on registry-sync-v2
Browse files Browse the repository at this point in the history
  • Loading branch information
thecatcore committed Mar 17, 2024
1 parent df0630a commit 6ade139
Show file tree
Hide file tree
Showing 21 changed files with 388 additions and 1 deletion.
1 change: 1 addition & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,6 @@ legacy-fabric-logger-api-v1.version = 1.0.4
legacy-fabric-networking-api-v1.version = 2.0.1
legacy-fabric-permissions-api-v1.version = 1.0.1
legacy-fabric-registry-sync-api-v1.version = 2.2.0
legacy-fabric-registry-sync-api-v2.version = 1.0.0
legacy-fabric-rendering-api-v1.version = 1.0.0
legacy-fabric-resource-loader-v1.version = 2.1.1
3 changes: 2 additions & 1 deletion legacy-fabric-registry-sync-api-v1/common.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
moduleDependencies(project, [
"legacy-fabric-api-base",
"legacy-fabric-networking-api-v1",
"legacy-fabric-resource-loader-v1"
"legacy-fabric-resource-loader-v1",
"legacy-fabric-registry-sync-v2"
])
Empty file.
2 changes: 2 additions & 0 deletions legacy-fabric-registry-sync-api-v2/1.12.2/gradle.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
minVersionExcluded=1.10.2
maxVersionIncluded=1.12.2
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package net.legacyfabric.fabric.mixin.registry.sync.versioned;

import net.legacyfabric.fabric.api.registry.v2.RegistryHolder;

import net.legacyfabric.fabric.api.util.Identifier;

import net.minecraft.util.registry.SimpleRegistry;

import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;

@Mixin(SimpleRegistry.class)
public abstract class SimpleRegistryMixin<K, V> implements RegistryHolder<V> {
@Shadow
public abstract void add(int id, K identifier, V object);

@Override
public void register(int rawId, Identifier identifier, V value) {
getBeforeAddedCallback().invoker().onEntryAdding(rawId, identifier, value);
add(rawId, toKeyType(identifier), value);
getEntryAddedCallback().invoker().onEntryAdded(rawId, identifier, value);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{
"schemaVersion": 1,
"id": "legacy-fabric-registry-sync-api-v2",
"name": "Legacy Fabric Registry Sync API (V2)",
"version": "${version}",
"environment": "*",
"license": "Apache-2.0",
"icon": "assets/legacy-fabric/icon.png",
"contact": {
"homepage": "https://legacyfabric.net/",
"irc": "irc://irc.esper.net:6667/legacyfabric",
"issues": "https://github.com/Legacy-Fabric/fabric/issues",
"sources": "https://github.com/Legacy-Fabric/fabric"
},
"authors": [
"Legacy-Fabric"
],
"depends": {
"fabricloader": ">=0.4.0",
"minecraft": "${minecraft_version}"
},
"description": "Registry hooks",
"entrypoints": {
"preLaunch": [
]
},
"mixins": [
"legacy-fabric-registry-sync-api-v2.mixins.json"
],
"custom": {
"modmenu": {
"badges": [ "library" ],
"parent": {
"id": "legacy-fabric-api",
"name": "Legacy Fabric API",
"badges": [ "library" ],
"description": "Core API module providing key hooks and inter-compatibility features for Minecraft 1.7.10-1.12.2.",
"icon": "assets/legacy-fabric/icon.png"
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"required": true,
"package": "net.legacyfabric.fabric.mixin.registry.sync",
"compatibilityLevel": "JAVA_8",
"injectors": {
"defaultRequire": 1
},
"mixins": [
"versioned.SimpleRegistryMixin"
],
"client": [
]
}
5 changes: 5 additions & 0 deletions legacy-fabric-registry-sync-api-v2/common.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
moduleDependencies(project, [
"legacy-fabric-api-base",
"legacy-fabric-networking-api-v1",
"legacy-fabric-resource-loader-v1"
])
Empty file.
2 changes: 2 additions & 0 deletions legacy-fabric-registry-sync-api-v2/common/gradle.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
minVersionIncluded=1.7
maxVersionIncluded=1.12.2
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package net.legacyfabric.fabric.api.registry.v2;

import net.legacyfabric.fabric.api.util.Identifier;
import net.legacyfabric.fabric.impl.registry.RegistryHelperImplementation;

import java.util.function.Function;

public class RegistryHelper {
public static <T> void register(RegistryHolder<T> registry, Identifier identifier, T value) {
RegistryHelperImplementation.register(registry, identifier, value);
}

public static <T> void registry(Identifier registryId, Identifier identifier, T value) {
register(RegistryHelperImplementation.getRegistry(registryId), identifier, value);
}

public static <T> T register(RegistryHolder<T> registry, Identifier identifier, Function<Integer, T> valueConstructor) {
return RegistryHelperImplementation.register(registry, identifier, valueConstructor);
}

public static <T> T registry(Identifier registryId, Identifier identifier, Function<Integer, T> valueConstructor) {
return register(RegistryHelperImplementation.<T>getRegistry(registryId), identifier, valueConstructor);
}

public static void addRegistry(Identifier identifier, RegistryHolder<?> registryHolder) {
RegistryHelperImplementation.registerRegistry(identifier, registryHolder);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package net.legacyfabric.fabric.api.registry.v2;

import net.legacyfabric.fabric.api.event.Event;
import net.legacyfabric.fabric.api.registry.v2.event.RegistryBeforeAddCallback;
import net.legacyfabric.fabric.api.registry.v2.event.RegistryEntryAddedCallback;
import net.legacyfabric.fabric.api.util.Identifier;

public interface RegistryHolder<T> {
Identifier getId();
Event<RegistryEntryAddedCallback<T>> getEntryAddedCallback();
Event<RegistryBeforeAddCallback<T>> getBeforeAddedCallback();

void register(int rawId, Identifier identifier, T value);

<K> K toKeyType(Object o);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package net.legacyfabric.fabric.api.registry.v2.event;

import net.legacyfabric.fabric.api.event.Event;
import net.legacyfabric.fabric.api.registry.v2.RegistryHolder;
import net.legacyfabric.fabric.api.util.Identifier;
import net.legacyfabric.fabric.impl.registry.RegistryHelperImplementation;

@FunctionalInterface
public interface RegistryBeforeAddCallback<T> {
void onEntryAdding(int rawId, Identifier id, T object);

static <T> Event<RegistryBeforeAddCallback<T>> event(Identifier registryId) {
return event(RegistryHelperImplementation.getRegistry(registryId));
}

static <T> Event<RegistryBeforeAddCallback<T>> event(RegistryHolder<T> registry) {
return registry.getBeforeAddedCallback();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package net.legacyfabric.fabric.api.registry.v2.event;

import net.legacyfabric.fabric.api.event.Event;
import net.legacyfabric.fabric.api.registry.v2.RegistryHolder;
import net.legacyfabric.fabric.api.util.Identifier;
import net.legacyfabric.fabric.impl.registry.RegistryHelperImplementation;

@FunctionalInterface
public interface RegistryEntryAddedCallback<T> {
void onEntryAdded(int rawId, Identifier id, T object);

static <T> Event<RegistryEntryAddedCallback<T>> event(Identifier registryId) {
return event(RegistryHelperImplementation.getRegistry(registryId));
}

static <T> Event<RegistryEntryAddedCallback<T>> event(RegistryHolder<T> registry) {
return registry.getEntryAddedCallback();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package net.legacyfabric.fabric.api.registry.v2.event;

import net.legacyfabric.fabric.api.event.Event;
import net.legacyfabric.fabric.api.registry.v2.RegistryHolder;
import net.legacyfabric.fabric.api.util.Identifier;
import net.legacyfabric.fabric.impl.registry.RegistryHelperImplementation;

/**
* Triggered only when a new registry is registered.
*/
@FunctionalInterface
public interface RegistryInitializedEvent {
<T> void initialized(RegistryHolder<T> registry);

static Event<RegistryInitializedEvent> event(Identifier registryId) {
return RegistryHelperImplementation.getInitializationEvent(registryId);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package net.legacyfabric.fabric.impl.registry;

import net.legacyfabric.fabric.api.event.Event;
import net.legacyfabric.fabric.api.event.EventFactory;
import net.legacyfabric.fabric.api.registry.v2.RegistryHolder;
import net.legacyfabric.fabric.api.registry.v2.event.RegistryInitializedEvent;
import net.legacyfabric.fabric.api.util.Identifier;

import net.legacyfabric.fabric.api.util.VersionUtils;
import net.legacyfabric.fabric.impl.registry.accessor.RegistryIdSetter;

import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;

public class RegistryHelperImplementation {
public static final boolean hasFlatteningBegun = VersionUtils.matches(">=1.8 <=1.12.2");
public static final Map<Identifier, Event<RegistryInitializedEvent>> INITIALIZATION_EVENTS = new HashMap<>();
private static final Map<Identifier, RegistryHolder<?>> REGISTRIES = new HashMap<>();

public static Event<RegistryInitializedEvent> getInitializationEvent(Identifier registryId) {
Event<RegistryInitializedEvent> event;

if (INITIALIZATION_EVENTS.containsKey(registryId)) {
event = INITIALIZATION_EVENTS.get(registryId);
} else {
event = EventFactory.createArrayBacked(RegistryInitializedEvent.class,
(callbacks) -> new RegistryInitializedEvent() {
@Override
public <T> void initialized(RegistryHolder<T> registry) {
for (RegistryInitializedEvent callback : callbacks) {
callback.initialized(registry);
}
}
}
);
INITIALIZATION_EVENTS.put(registryId, event);
}

return event;
}

public static <T> RegistryHolder<T> getRegistry(Identifier identifier) {
return (RegistryHolder<T>) REGISTRIES.get(identifier);
}

public static void registerRegistry(Identifier identifier, RegistryHolder<?> holder) {
if (REGISTRIES.containsKey(identifier)) throw new IllegalArgumentException("Attempted to register registry " + identifier.toString() + " twices!");
REGISTRIES.put(identifier, holder);

if (holder instanceof RegistryIdSetter) ((RegistryIdSetter) holder).fabric$setId(identifier);
}

public static <T> void register(RegistryHolder<T> registry, Identifier identifier, T value) {
int computedId = -1;
registry.register(computedId, identifier, value);
}

public static <T> T register(RegistryHolder<T> registry, Identifier identifier, Function<Integer, T> valueConstructor) {
int computedId = -1;
T value = valueConstructor.apply(computedId);
registry.register(computedId, identifier, value);
return value;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package net.legacyfabric.fabric.impl.registry.accessor;

import net.legacyfabric.fabric.api.util.Identifier;

public interface RegistryIdSetter {
void fabric$setId(Identifier identifier);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package net.legacyfabric.fabric.mixin.registry.sync;

import net.legacyfabric.fabric.api.event.Event;
import net.legacyfabric.fabric.api.event.EventFactory;
import net.legacyfabric.fabric.api.registry.v2.event.RegistryBeforeAddCallback;
import net.legacyfabric.fabric.api.registry.v2.event.RegistryEntryAddedCallback;
import net.legacyfabric.fabric.api.registry.v2.RegistryHolder;

import net.legacyfabric.fabric.api.util.Identifier;

import net.legacyfabric.fabric.impl.registry.RegistryHelperImplementation;
import net.legacyfabric.fabric.impl.registry.accessor.RegistryIdSetter;

import net.minecraft.util.registry.SimpleRegistry;

import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Unique;

@Mixin(SimpleRegistry.class)
public abstract class SimpleRegistryMixinV2<K, V> implements RegistryHolder<V>, RegistryIdSetter {
@Unique
private final Event<RegistryEntryAddedCallback<V>> fabric_addObjectEvent = EventFactory.createArrayBacked(RegistryEntryAddedCallback.class,
(callbacks) -> (rawId, id, object) -> {
for (RegistryEntryAddedCallback<V> callback : callbacks) {
callback.onEntryAdded(rawId, id, object);
}
}
);

@Unique
private final Event<RegistryBeforeAddCallback<V>> fabric_beforeAddObjectEvent = EventFactory.createArrayBacked(RegistryBeforeAddCallback.class,
(callbacks) -> (rawId, id, object) -> {
for (RegistryBeforeAddCallback<V> callback : callbacks) {
callback.onEntryAdding(rawId, id, object);
}
}
);

@Unique
private Identifier fabric_id;

@Override
public Event<RegistryEntryAddedCallback<V>> getEntryAddedCallback() {
return this.fabric_addObjectEvent;
}

@Override
public Event<RegistryBeforeAddCallback<V>> getBeforeAddedCallback() {
return this.fabric_beforeAddObjectEvent;
}

@Override
public Identifier getId() {
return this.fabric_id;
}

@Override
public void fabric$setId(Identifier identifier) {
assert this.fabric_id == null;
this.fabric_id = identifier;
}

@Override
public K toKeyType(Object o) {
return RegistryHelperImplementation.hasFlatteningBegun ? (K) new net.minecraft.util.Identifier(o.toString()) : (K) o.toString();
}
}
Loading

0 comments on commit 6ade139

Please sign in to comment.