-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rework object resolvers into registry resolvers
Signed-off-by: TheSilkMiner <thesilkminer@outlook.com>
- Loading branch information
1 parent
78b676b
commit a984d3e
Showing
12 changed files
with
129 additions
and
102 deletions.
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
7 changes: 5 additions & 2 deletions
7
Common/src/main/java/com/blamejared/contenttweaker/core/api/ApiBridge.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 |
---|---|---|
@@ -1,13 +1,16 @@ | ||
package com.blamejared.contenttweaker.core.api; | ||
|
||
import com.blamejared.contenttweaker.core.api.object.ObjectType; | ||
import com.blamejared.contenttweaker.core.api.object.RegistryResolver; | ||
import com.blamejared.contenttweaker.core.api.registry.GameRegistry; | ||
import com.blamejared.contenttweaker.core.api.registry.RegistryButler; | ||
import com.blamejared.contenttweaker.core.api.resource.ResourceManager; | ||
import net.minecraft.resources.ResourceLocation; | ||
import net.minecraft.core.Registry; | ||
import net.minecraft.resources.ResourceKey; | ||
|
||
public interface ApiBridge { | ||
RegistryButler registryButler(); | ||
ResourceManager resourceManager(); | ||
|
||
<T> T resolve(final ObjectType<T> type, final ResourceLocation id); | ||
<T> RegistryResolver<T> findResolver(final ObjectType<T> type); | ||
} |
41 changes: 0 additions & 41 deletions
41
Common/src/main/java/com/blamejared/contenttweaker/core/api/object/ObjectResolver.java
This file was deleted.
Oops, something went wrong.
64 changes: 64 additions & 0 deletions
64
Common/src/main/java/com/blamejared/contenttweaker/core/api/object/RegistryResolver.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,64 @@ | ||
package com.blamejared.contenttweaker.core.api.object; | ||
|
||
import com.blamejared.contenttweaker.core.api.ContentTweakerApi; | ||
import com.blamejared.contenttweaker.core.api.registry.GameRegistry; | ||
import com.google.common.base.Suppliers; | ||
import net.minecraft.core.Registry; | ||
import net.minecraft.resources.ResourceKey; | ||
import net.minecraft.resources.ResourceLocation; | ||
|
||
import java.util.Objects; | ||
import java.util.function.Function; | ||
import java.util.function.Supplier; | ||
|
||
public interface RegistryResolver<T> extends Function<ResourceLocation, T> { | ||
static <T> RegistryResolver<T> of(final ObjectType<T> type) { | ||
final ResourceKey<? extends Registry<T>> key = Objects.requireNonNull(Objects.requireNonNull(type).key()); | ||
return of(type, () -> GameRegistry.findFromKey(type, key)); | ||
} | ||
|
||
static <T> RegistryResolver<T> of(final ObjectType<T> type, final Supplier<? extends GameRegistry<T>> registryResolver) { | ||
Objects.requireNonNull(type); | ||
Objects.requireNonNull(registryResolver); | ||
final Supplier<? extends GameRegistry<T>> resolver = Suppliers.memoize(registryResolver::get); | ||
return new RegistryResolver<>() { | ||
@Override | ||
public ObjectType<T> type() { | ||
return type; | ||
} | ||
|
||
@Override | ||
public GameRegistry<T> registry() { | ||
return resolver.get(); | ||
} | ||
}; | ||
} | ||
|
||
static <T> RegistryResolver<T> find(final ObjectType<T> type) { | ||
return ContentTweakerApi.get().findResolver(type); | ||
} | ||
|
||
ObjectType<T> type(); | ||
GameRegistry<T> registry(); | ||
|
||
default T resolve(final ResourceLocation id) { | ||
return this.registry().get(Objects.requireNonNull(id)); | ||
} | ||
|
||
default ResourceLocation nameOf(final T object) { | ||
return this.registry().nameOf(Objects.requireNonNull(object)); | ||
} | ||
|
||
default void enqueueRegistration(final ResourceLocation name, final Supplier<T> objectCreator) { | ||
this.registry().enqueueRegistration(name, objectCreator); | ||
} | ||
|
||
default void enqueueRegistration(final ObjectHolder<T> holder) { | ||
this.registry().enqueueRegistration(holder); | ||
} | ||
|
||
@Override | ||
default T apply(final ResourceLocation location) { | ||
return this.resolve(location); | ||
} | ||
} |
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
8 changes: 8 additions & 0 deletions
8
...main/java/com/blamejared/contenttweaker/core/api/plugin/RegistryResolverRegistration.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,8 @@ | ||
package com.blamejared.contenttweaker.core.api.plugin; | ||
|
||
import com.blamejared.contenttweaker.core.api.object.RegistryResolver; | ||
import com.blamejared.contenttweaker.core.api.object.ObjectType; | ||
|
||
public interface RegistryResolverRegistration { | ||
<T> void register(final ObjectType<T> type, final RegistryResolver<T> resolver); | ||
} |
8 changes: 0 additions & 8 deletions
8
Common/src/main/java/com/blamejared/contenttweaker/core/api/plugin/ResolverRegistration.java
This file was deleted.
Oops, something went wrong.
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
33 changes: 33 additions & 0 deletions
33
...n/java/com/blamejared/contenttweaker/core/plugin/RegistryResolverRegistrationManager.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,33 @@ | ||
package com.blamejared.contenttweaker.core.plugin; | ||
|
||
import com.blamejared.contenttweaker.core.api.object.RegistryResolver; | ||
import com.blamejared.contenttweaker.core.api.object.ObjectType; | ||
import com.blamejared.contenttweaker.core.api.plugin.RegistryResolverRegistration; | ||
|
||
import java.util.Collections; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.function.Consumer; | ||
|
||
public final class RegistryResolverRegistrationManager implements RegistryResolverRegistration { | ||
private final Map<ObjectType<?>, RegistryResolver<?>> resolvers; | ||
|
||
private RegistryResolverRegistrationManager() { | ||
this.resolvers = new HashMap<>(); | ||
} | ||
|
||
public static Map<ObjectType<?>, RegistryResolver<?>> get(final Consumer<RegistryResolverRegistration> consumer) { | ||
final RegistryResolverRegistrationManager registration = new RegistryResolverRegistrationManager(); | ||
consumer.accept(registration); | ||
return Collections.unmodifiableMap(registration.resolvers); | ||
} | ||
|
||
@Override | ||
public <T> void register(final ObjectType<T> type, final RegistryResolver<T> resolver) { | ||
final RegistryResolver<?> previous = this.resolvers.get(type); | ||
if (previous != null) { | ||
throw new IllegalArgumentException("Attempted double resolvers for type " + type + " through " + previous + " and " + resolver); | ||
} | ||
this.resolvers.put(type, resolver); | ||
} | ||
} |
36 changes: 0 additions & 36 deletions
36
.../src/main/java/com/blamejared/contenttweaker/core/plugin/ResolverRegistrationManager.java
This file was deleted.
Oops, something went wrong.
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