-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
df0630a
commit 6ade139
Showing
21 changed files
with
388 additions
and
1 deletion.
There are no files selected for viewing
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
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.
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,2 @@ | ||
minVersionExcluded=1.10.2 | ||
maxVersionIncluded=1.12.2 |
23 changes: 23 additions & 0 deletions
23
.../main/java/net/legacyfabric/fabric/mixin/registry/sync/versioned/SimpleRegistryMixin.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,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); | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
legacy-fabric-registry-sync-api-v2/1.12.2/src/main/resources/fabric.mod.json
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,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" | ||
} | ||
} | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
...stry-sync-api-v2/1.12.2/src/main/resources/legacy-fabric-registry-sync-api-v2.mixins.json
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,13 @@ | ||
{ | ||
"required": true, | ||
"package": "net.legacyfabric.fabric.mixin.registry.sync", | ||
"compatibilityLevel": "JAVA_8", | ||
"injectors": { | ||
"defaultRequire": 1 | ||
}, | ||
"mixins": [ | ||
"versioned.SimpleRegistryMixin" | ||
], | ||
"client": [ | ||
] | ||
} |
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,5 @@ | ||
moduleDependencies(project, [ | ||
"legacy-fabric-api-base", | ||
"legacy-fabric-networking-api-v1", | ||
"legacy-fabric-resource-loader-v1" | ||
]) |
Empty file.
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,2 @@ | ||
minVersionIncluded=1.7 | ||
maxVersionIncluded=1.12.2 |
28 changes: 28 additions & 0 deletions
28
...c-api-v2/common/src/main/java/net/legacyfabric/fabric/api/registry/v2/RegistryHelper.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,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); | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
...c-api-v2/common/src/main/java/net/legacyfabric/fabric/api/registry/v2/RegistryHolder.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,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); | ||
} |
19 changes: 19 additions & 0 deletions
19
...rc/main/java/net/legacyfabric/fabric/api/registry/v2/event/RegistryBeforeAddCallback.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,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(); | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
...c/main/java/net/legacyfabric/fabric/api/registry/v2/event/RegistryEntryAddedCallback.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,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(); | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
...src/main/java/net/legacyfabric/fabric/api/registry/v2/event/RegistryInitializedEvent.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,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); | ||
} | ||
} |
65 changes: 65 additions & 0 deletions
65
...mon/src/main/java/net/legacyfabric/fabric/impl/registry/RegistryHelperImplementation.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,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; | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
...common/src/main/java/net/legacyfabric/fabric/impl/registry/accessor/RegistryIdSetter.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,7 @@ | ||
package net.legacyfabric.fabric.impl.registry.accessor; | ||
|
||
import net.legacyfabric.fabric.api.util.Identifier; | ||
|
||
public interface RegistryIdSetter { | ||
void fabric$setId(Identifier identifier); | ||
} |
67 changes: 67 additions & 0 deletions
67
...mmon/src/main/java/net/legacyfabric/fabric/mixin/registry/sync/SimpleRegistryMixinV2.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 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(); | ||
} | ||
} |
Oops, something went wrong.