-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch '1.21' of github.com:glisco03/owo-lib into feature/remap…
…-to-yalmm
- Loading branch information
Showing
27 changed files
with
439 additions
and
64 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
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
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
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
64 changes: 64 additions & 0 deletions
64
src/main/java/io/wispforest/owo/config/ui/ConfigScreenProviders.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 io.wispforest.owo.config.ui; | ||
|
||
import net.minecraft.client.gui.screens.Screen; | ||
import org.jetbrains.annotations.ApiStatus; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.function.BiConsumer; | ||
import java.util.function.Function; | ||
|
||
public class ConfigScreenProviders { | ||
|
||
private static final Map<String, Function<Screen, ? extends Screen>> PROVIDERS = new HashMap<>(); | ||
private static final Map<String, Function<Screen, ? extends ConfigScreen>> OWO_SCREEN_PROVIDERS = new HashMap<>(); | ||
|
||
/** | ||
* Register the given config screen provider. This is primarily | ||
* used for making a config screen available in ModMenu and to the | ||
* {@code /owo-config} command, although other places my use it as well | ||
* | ||
* @param modId The mod id for which to supply a config screen | ||
* @param supplier The supplier to register - this gets the parent screen | ||
* as argument | ||
* @throws IllegalArgumentException If a config screen provider is | ||
* already registered for the given mod id | ||
*/ | ||
public static <S extends Screen> void register(String modId, Function<Screen, S> supplier) { | ||
if (PROVIDERS.put(modId, supplier) != null) { | ||
throw new IllegalArgumentException("Tried to register config screen provider for mod id " + modId + " twice"); | ||
} | ||
} | ||
|
||
/** | ||
* Get the config screen provider associated with | ||
* the given mod id | ||
* | ||
* @return The associated config screen provider, or {@code null} if | ||
* none is registered | ||
*/ | ||
public static @Nullable Function<Screen, ? extends Screen> get(String modId) { | ||
return PROVIDERS.get(modId); | ||
} | ||
|
||
public static void forEach(BiConsumer<String, Function<Screen, ? extends Screen>> action) { | ||
PROVIDERS.forEach(action); | ||
} | ||
|
||
// -- internal methods for backwards-compat in ConfigScreen -- | ||
|
||
@ApiStatus.Internal | ||
public static <S extends ConfigScreen> void registerOwoConfigScreen(String modId, Function<Screen, S> supplier) { | ||
register(modId, supplier); | ||
OWO_SCREEN_PROVIDERS.put(modId, supplier); | ||
} | ||
|
||
static @Nullable Function<Screen, ? extends ConfigScreen> getOwoProvider(String modId) { | ||
return OWO_SCREEN_PROVIDERS.get(modId); | ||
} | ||
|
||
static void forEachOwoProvider(BiConsumer<String, Function<Screen, ? extends ConfigScreen>> action) { | ||
OWO_SCREEN_PROVIDERS.forEach(action); | ||
} | ||
} |
64 changes: 64 additions & 0 deletions
64
src/main/java/io/wispforest/owo/ext/DerivedComponentMap.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 io.wispforest.owo.ext; | ||
|
||
import net.minecraft.core.component.DataComponentMap; | ||
import net.minecraft.core.component.DataComponentPatch; | ||
import net.minecraft.core.component.DataComponentType; | ||
import net.minecraft.core.component.PatchedDataComponentMap; | ||
import net.minecraft.world.item.ItemStack; | ||
import org.jetbrains.annotations.ApiStatus; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
import java.util.Set; | ||
|
||
@ApiStatus.Internal | ||
public class DerivedComponentMap implements DataComponentMap { | ||
private final DataComponentMap base; | ||
private final PatchedDataComponentMap delegate; | ||
|
||
public DerivedComponentMap(DataComponentMap base) { | ||
this.base = base; | ||
this.delegate = new PatchedDataComponentMap(base); | ||
} | ||
|
||
public static DataComponentMap reWrapIfNeeded(DataComponentMap original) { | ||
if (original instanceof DerivedComponentMap derived) { | ||
return new DerivedComponentMap(derived.base); | ||
} else { | ||
return original; | ||
} | ||
} | ||
|
||
public void derive(ItemStack owner) { | ||
delegate.restorePatch(DataComponentPatch.EMPTY); | ||
var builder = DataComponentPatch.builder(); | ||
owner.getItem().deriveStackComponents(owner.getComponents(), builder); | ||
delegate.restorePatch(builder.build()); | ||
} | ||
|
||
@Nullable | ||
@Override | ||
public <T> T get(DataComponentType<? extends T> type) { | ||
return delegate.get(type); | ||
} | ||
|
||
@Override | ||
public Set<DataComponentType<?>> keySet() { | ||
return delegate.keySet(); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) return true; | ||
if (o == null || getClass() != o.getClass()) return false; | ||
|
||
DerivedComponentMap that = (DerivedComponentMap) o; | ||
return base.equals(that.base) && delegate.equals(that.delegate); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
int result = base.hashCode(); | ||
result = 31 * result + delegate.hashCode(); | ||
return result; | ||
} | ||
} |
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,15 @@ | ||
package io.wispforest.owo.ext; | ||
|
||
import net.minecraft.core.component.DataComponentMap; | ||
import net.minecraft.core.component.DataComponentPatch; | ||
import org.jetbrains.annotations.ApiStatus; | ||
|
||
public interface OwoItem { | ||
/** | ||
* Generates component-derived-components from the stack's components | ||
* @param source a map containing the item stack's non-derived components | ||
* @param target a builder for the derived component map | ||
*/ | ||
@ApiStatus.Experimental | ||
default void deriveStackComponents(DataComponentMap source, DataComponentPatch.Builder target) { } | ||
} |
Oops, something went wrong.