Skip to content

Commit

Permalink
Merge branch '1.21' of github.com:glisco03/owo-lib into feature/remap…
Browse files Browse the repository at this point in the history
…-to-yalmm
  • Loading branch information
BasiqueEvangelist committed Sep 7, 2024
2 parents af06355 + 212005b commit 92cc60b
Show file tree
Hide file tree
Showing 27 changed files with 439 additions and 64 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ parchment_version=2024.07.28
yalmm_version=1.21+build.10
loader_version=0.15.11
# Mod Properties
mod_version=0.12.11
mod_version=0.12.12
maven_group=io.wispforest
archives_base_name=owo-lib
# Dependencies
Expand Down
18 changes: 15 additions & 3 deletions src/main/java/io/wispforest/owo/client/screens/SyncedProperty.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,26 @@
package io.wispforest.owo.client.screens;

import io.wispforest.endec.Endec;
import io.wispforest.endec.SerializationContext;
import io.wispforest.owo.serialization.RegistriesAttribute;
import io.wispforest.owo.util.Observable;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.world.inventory.AbstractContainerMenu;
import org.jetbrains.annotations.ApiStatus;

public class SyncedProperty<T> extends Observable<T> {
private final int index;
private final Endec<T> endec;
private final AbstractContainerMenu owner;
private boolean needsSync;

@ApiStatus.Internal
public SyncedProperty(int index, Endec<T> endec, T initial) {
public SyncedProperty(int index, Endec<T> endec, T initial, AbstractContainerMenu owner) {
super(initial);

this.index = index;
this.endec = endec;
this.owner = owner;
}

public int index() {
Expand All @@ -30,12 +35,12 @@ public boolean needsSync() {
@ApiStatus.Internal
public void write(FriendlyByteBuf buf) {
needsSync = false;
buf.write(this.endec, value);
buf.write(serializationContext(), this.endec, value);
}

@ApiStatus.Internal
public void read(FriendlyByteBuf buf) {
this.set(buf.read(this.endec));
this.set(buf.read(serializationContext(), this.endec));
}

@Override
Expand All @@ -48,4 +53,11 @@ protected void notifyObservers(T value) {
public void markDirty() {
notifyObservers(value);
}

private SerializationContext serializationContext() {
var player = this.owner.player();
if (player == null) return SerializationContext.empty();

return SerializationContext.attributes(RegistriesAttribute.of(player.registryAccess()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import com.google.common.collect.ForwardingMap;
import com.terraformersmc.modmenu.api.ConfigScreenFactory;
import com.terraformersmc.modmenu.api.ModMenuApi;
import io.wispforest.owo.config.ui.ConfigScreen;
import io.wispforest.owo.config.ui.ConfigScreenProviders;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;

Expand All @@ -19,7 +19,7 @@ public class OwoModMenuPlugin implements ModMenuApi {
protected @NotNull Map<String, ConfigScreenFactory<?>> delegate() {
return Util.make(
new HashMap<>(),
map -> ConfigScreen.forEachProvider((s, provider) -> map.put(s, provider::apply))
map -> ConfigScreenProviders.forEach((s, provider) -> map.put(s, provider::apply))
);
}
};
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/io/wispforest/owo/config/ConfigWrapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import io.wispforest.owo.config.Option.SyncMode;
import io.wispforest.owo.config.annotation.*;
import io.wispforest.owo.config.ui.ConfigScreen;
import io.wispforest.owo.config.ui.ConfigScreenProviders;
import io.wispforest.owo.serialization.endec.MinecraftEndecs;
import io.wispforest.owo.ui.core.Color;
import io.wispforest.owo.util.NumberReflection;
Expand Down Expand Up @@ -95,7 +96,7 @@ protected ConfigWrapper(Class<C> clazz, Consumer<Jankson.Builder> janksonBuilder

if (FabricLoader.getInstance().getEnvironmentType() == EnvType.CLIENT && clazz.isAnnotationPresent(Modmenu.class)) {
var modmenuAnnotation = clazz.getAnnotation(Modmenu.class);
ConfigScreen.registerProvider(
ConfigScreenProviders.registerOwoConfigScreen(
modmenuAnnotation.modId(),
screen -> ConfigScreen.createWithCustomModel(Identifier.parse(modmenuAnnotation.uiModelId()), this, screen)
);
Expand Down
11 changes: 6 additions & 5 deletions src/main/java/io/wispforest/owo/config/OwoConfigCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,19 @@
import com.mojang.brigadier.suggestion.SuggestionsBuilder;
import io.wispforest.owo.Owo;
import io.wispforest.owo.config.ui.ConfigScreen;
import io.wispforest.owo.config.ui.ConfigScreenProviders;
import io.wispforest.owo.ops.TextOps;
import net.fabricmc.fabric.api.client.command.v2.ClientCommandManager;
import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.screens.Screen;
import net.minecraft.commands.CommandBuildContext;
import net.minecraft.commands.SharedSuggestionProvider;
import net.minecraft.network.chat.Text;
import org.jetbrains.annotations.ApiStatus;

import java.util.ArrayList;
import java.util.concurrent.CompletableFuture;
import java.util.function.Function;

@ApiStatus.Internal
public class OwoConfigCommand {
Expand All @@ -36,15 +37,15 @@ public static void register(CommandDispatcher<FabricClientCommandSource> dispatc
})));
}

private static class ConfigScreenArgumentType implements ArgumentType<ConfigScreen> {
private static class ConfigScreenArgumentType implements ArgumentType<Screen> {

private static final SimpleCommandExceptionType NO_SUCH_CONFIG_SCREEN = new SimpleCommandExceptionType(
TextOps.concat(Owo.PREFIX, Text.literal("no config screen with that id"))
);

@Override
public ConfigScreen parse(StringReader reader) throws CommandSyntaxException {
var provider = ConfigScreen.getProvider(reader.readString());
public Screen parse(StringReader reader) throws CommandSyntaxException {
var provider = ConfigScreenProviders.get(reader.readString());
if (provider == null) throw NO_SUCH_CONFIG_SCREEN.create();

return provider.apply(null);
Expand All @@ -53,7 +54,7 @@ public ConfigScreen parse(StringReader reader) throws CommandSyntaxException {
@Override
public <S> CompletableFuture<Suggestions> listSuggestions(CommandContext<S> context, SuggestionsBuilder builder) {
var configNames = new ArrayList<String>();
ConfigScreen.forEachProvider((s, screenFunction) -> configNames.add(s));
ConfigScreenProviders.forEach((s, screenFunction) -> configNames.add(s));
return SharedSuggestionProvider.suggest(configNames, builder);
}
}
Expand Down
35 changes: 12 additions & 23 deletions src/main/java/io/wispforest/owo/config/ui/ConfigScreen.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import io.wispforest.owo.util.NumberReflection;
import io.wispforest.owo.util.ReflectionUtils;
import org.apache.commons.lang3.mutable.MutableBoolean;
import org.apache.commons.lang3.mutable.MutableInt;
import org.jetbrains.annotations.Nullable;
import org.lwjgl.glfw.GLFW;

Expand Down Expand Up @@ -58,8 +57,6 @@ public class ConfigScreen extends BaseUIModelScreen<FlowLayout> {

public static final Identifier DEFAULT_MODEL_ID = Identifier.of("owo", "config");

private static final Map<String, Function<Screen, ? extends ConfigScreen>> CONFIG_SCREEN_PROVIDERS = new HashMap<>();

private static final Map<Predicate<Option<?>>, OptionComponentFactory<?>> DEFAULT_FACTORIES = new HashMap<>();
/**
* A set of extra option factories - add to this if you want to override
Expand Down Expand Up @@ -107,35 +104,27 @@ public static ConfigScreen createWithCustomModel(Identifier modelId, ConfigWrapp
}

/**
* Register the given config screen provider. This is primarily
* used for making your config 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
* @deprecated Use {@link ConfigScreenProviders#register(String, Function)} instead
*/
@Deprecated(forRemoval = true)
public static <S extends ConfigScreen> void registerProvider(String modId, Function<Screen, S> supplier) {
if (CONFIG_SCREEN_PROVIDERS.put(modId, supplier) != null) {
throw new IllegalArgumentException("Tried to register config screen provider for mod id " + modId + " twice");
}
ConfigScreenProviders.registerOwoConfigScreen(modId, supplier);
}

/**
* Get the config screen provider associated with
* the given mod id
*
* @return The associated config screen provider, or {@code null} if
* none is registered
* @deprecated Use {@link ConfigScreenProviders#get(String)} instead
*/
@Deprecated(forRemoval = true)
public static @Nullable Function<Screen, ? extends ConfigScreen> getProvider(String modId) {
return CONFIG_SCREEN_PROVIDERS.get(modId);
return ConfigScreenProviders.getOwoProvider(modId);
}

/**
* @deprecated Use {@link ConfigScreenProviders#forEach(BiConsumer)} instead
*/
@Deprecated(forRemoval = true)
public static void forEachProvider(BiConsumer<String, Function<Screen, ? extends ConfigScreen>> action) {
CONFIG_SCREEN_PROVIDERS.forEach(action);
ConfigScreenProviders.forEachOwoProvider(action);
}

@Override
Expand Down Expand Up @@ -465,7 +454,7 @@ public void removed() {
UIParsing.registerFactory("config-text-box", element -> new ConfigTextBox());
}

private record SearchMatches(String query, List<SearchAnchorComponent> matches) {}
protected record SearchMatches(String query, List<SearchAnchorComponent> matches) {}

public static class SearchHighlighterComponent extends BaseComponent {

Expand Down
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 src/main/java/io/wispforest/owo/ext/DerivedComponentMap.java
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;
}
}
15 changes: 15 additions & 0 deletions src/main/java/io/wispforest/owo/ext/OwoItem.java
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) { }
}
Loading

0 comments on commit 92cc60b

Please sign in to comment.