mirror of
https://github.com/Creators-of-Create/Create.git
synced 2025-03-04 06:44:40 +01:00
add method to query by (Block/Fluid)State
This commit is contained in:
parent
af365a8337
commit
404ce0212c
3 changed files with 32 additions and 0 deletions
|
@ -2,6 +2,7 @@ package com.simibubi.create.api.registry;
|
|||
|
||||
import java.util.function.Function;
|
||||
|
||||
import org.jetbrains.annotations.ApiStatus;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import com.simibubi.create.impl.registry.SimpleRegistryImpl;
|
||||
|
@ -11,11 +12,13 @@ import net.minecraft.core.Holder;
|
|||
import net.minecraft.tags.TagKey;
|
||||
import net.minecraft.world.item.Item;
|
||||
import net.minecraft.world.level.block.Block;
|
||||
import net.minecraft.world.level.block.state.StateHolder;
|
||||
|
||||
/**
|
||||
* A simple registry mapping between objects. Provides simple registration functionality, as well as lazy providers.
|
||||
* This class is thread-safe, and may be safely used during parallel mod init.
|
||||
*/
|
||||
@ApiStatus.NonExtendable
|
||||
public interface SimpleRegistry<K, V> {
|
||||
/**
|
||||
* Register an association between a key and a value.
|
||||
|
@ -43,6 +46,12 @@ public interface SimpleRegistry<K, V> {
|
|||
@Nullable
|
||||
V get(K object);
|
||||
|
||||
/**
|
||||
* Shortcut for {@link #get(Object)} that accepts a StateHolder, such as BlockState or FluidState.
|
||||
*/
|
||||
@Nullable
|
||||
V get(StateHolder<K, ?> state);
|
||||
|
||||
static <K, V> SimpleRegistry<K, V> create() {
|
||||
return new SimpleRegistryImpl<>();
|
||||
}
|
||||
|
|
|
@ -0,0 +1,12 @@
|
|||
package com.simibubi.create.foundation.mixin.accessor;
|
||||
|
||||
import org.spongepowered.asm.mixin.Mixin;
|
||||
import org.spongepowered.asm.mixin.gen.Accessor;
|
||||
|
||||
import net.minecraft.world.level.block.state.StateHolder;
|
||||
|
||||
@Mixin(StateHolder.class)
|
||||
public interface StateHolderAccessor<O, S> {
|
||||
@Accessor
|
||||
O getOwner();
|
||||
}
|
|
@ -9,6 +9,9 @@ import java.util.Objects;
|
|||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import com.simibubi.create.api.registry.SimpleRegistry;
|
||||
import com.simibubi.create.foundation.mixin.accessor.StateHolderAccessor;
|
||||
|
||||
import net.minecraft.world.level.block.state.StateHolder;
|
||||
|
||||
// methods are synchronized since registrations can happen during parallel mod loading
|
||||
public class SimpleRegistryImpl<K, V> implements SimpleRegistry<K, V> {
|
||||
|
@ -74,6 +77,14 @@ public class SimpleRegistryImpl<K, V> implements SimpleRegistry<K, V> {
|
|||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
@Nullable
|
||||
@SuppressWarnings("unchecked")
|
||||
public synchronized V get(StateHolder<K, ?> state) {
|
||||
K owner = ((StateHolderAccessor<K, ?>) state).getOwner();
|
||||
return this.get(owner);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private static <T> T nullMarker() {
|
||||
return (T) nullMarker;
|
||||
|
|
Loading…
Add table
Reference in a new issue