diff options
Diffstat (limited to 'common')
8 files changed, 62 insertions, 67 deletions
diff --git a/common/src/main/java/dev/isxander/yacl3/config/GsonConfigInstance.java b/common/src/main/java/dev/isxander/yacl3/config/GsonConfigInstance.java index deff6d7..4e7c931 100644 --- a/common/src/main/java/dev/isxander/yacl3/config/GsonConfigInstance.java +++ b/common/src/main/java/dev/isxander/yacl3/config/GsonConfigInstance.java @@ -1,6 +1,7 @@ package dev.isxander.yacl3.config; import com.google.gson.*; +import dev.isxander.yacl3.config.v2.impl.serializer.GsonConfigSerializer; import dev.isxander.yacl3.gui.utils.ItemRegistryHelper; import dev.isxander.yacl3.impl.utils.YACLConstants; import net.minecraft.core.registries.BuiltInRegistries; @@ -66,8 +67,8 @@ public class GsonConfigInstance<T> extends ConfigInstance<T> { this.path = path; this.gson = builder .setExclusionStrategies(new ConfigExclusionStrategy()) - .registerTypeHierarchyAdapter(Component.class, new Component.Serializer()) - .registerTypeHierarchyAdapter(Style.class, new Style.Serializer()) + .registerTypeHierarchyAdapter(Component.class, new Component.SerializerAdapter()) + .registerTypeHierarchyAdapter(Style.class, new GsonConfigSerializer.StyleTypeAdapter()) .registerTypeHierarchyAdapter(Color.class, new ColorTypeAdapter()) .registerTypeHierarchyAdapter(Item.class, new ItemTypeAdapter()) .serializeNulls() @@ -161,8 +162,8 @@ public class GsonConfigInstance<T> extends ConfigInstance<T> { private UnaryOperator<GsonBuilder> gsonBuilder = builder -> builder .setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES) .serializeNulls() - .registerTypeHierarchyAdapter(Component.class, new Component.Serializer()) - .registerTypeHierarchyAdapter(Style.class, new Style.Serializer()) + .registerTypeHierarchyAdapter(Component.class, new Component.SerializerAdapter()) + .registerTypeHierarchyAdapter(Style.class, new GsonConfigSerializer.StyleTypeAdapter()) .registerTypeHierarchyAdapter(Color.class, new ColorTypeAdapter()) .registerTypeHierarchyAdapter(Item.class, new ItemTypeAdapter()); diff --git a/common/src/main/java/dev/isxander/yacl3/config/v2/impl/serializer/GsonConfigSerializer.java b/common/src/main/java/dev/isxander/yacl3/config/v2/impl/serializer/GsonConfigSerializer.java index 49f2a47..d38fb73 100644 --- a/common/src/main/java/dev/isxander/yacl3/config/v2/impl/serializer/GsonConfigSerializer.java +++ b/common/src/main/java/dev/isxander/yacl3/config/v2/impl/serializer/GsonConfigSerializer.java @@ -1,11 +1,14 @@ package dev.isxander.yacl3.config.v2.impl.serializer; import com.google.gson.*; +import com.mojang.serialization.JsonOps; +import dev.isxander.yacl3.config.GsonConfigInstance; import dev.isxander.yacl3.config.v2.api.*; import dev.isxander.yacl3.config.v2.api.serializer.GsonConfigSerializerBuilder; import dev.isxander.yacl3.gui.utils.ItemRegistryHelper; import dev.isxander.yacl3.impl.utils.YACLConstants; import dev.isxander.yacl3.platform.YACLPlatform; +import net.minecraft.commands.ParserUtils; import net.minecraft.core.registries.BuiltInRegistries; import net.minecraft.network.chat.Component; import net.minecraft.network.chat.Style; @@ -173,6 +176,18 @@ public class GsonConfigSerializer<T> extends ConfigSerializer<T> { config.load(); } + public static class StyleTypeAdapter implements JsonSerializer<Style>, JsonDeserializer<Style> { + @Override + public Style deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { + return Style.Serializer.CODEC.parse(JsonOps.INSTANCE, json).result().orElse(Style.EMPTY); + } + + @Override + public JsonElement serialize(Style src, Type typeOfSrc, JsonSerializationContext context) { + return Style.Serializer.CODEC.encodeStart(JsonOps.INSTANCE, src).result().orElse(JsonNull.INSTANCE); + } + } + public static class ColorTypeAdapter implements JsonSerializer<Color>, JsonDeserializer<Color> { @Override public Color deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext) throws JsonParseException { @@ -205,8 +220,8 @@ public class GsonConfigSerializer<T> extends ConfigSerializer<T> { private UnaryOperator<GsonBuilder> gsonBuilder = builder -> builder .setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES) .serializeNulls() - .registerTypeHierarchyAdapter(Component.class, new Component.Serializer()) - .registerTypeHierarchyAdapter(Style.class, new Style.Serializer()) + .registerTypeHierarchyAdapter(Component.class, new Component.SerializerAdapter()) + .registerTypeHierarchyAdapter(Style.class, new StyleTypeAdapter()) .registerTypeHierarchyAdapter(Color.class, new ColorTypeAdapter()) .registerTypeHierarchyAdapter(Item.class, new ItemTypeAdapter()) .setPrettyPrinting(); diff --git a/common/src/main/java/dev/isxander/yacl3/gui/ElementListWidgetExt.java b/common/src/main/java/dev/isxander/yacl3/gui/ElementListWidgetExt.java index a36bd5e..91cda1f 100644 --- a/common/src/main/java/dev/isxander/yacl3/gui/ElementListWidgetExt.java +++ b/common/src/main/java/dev/isxander/yacl3/gui/ElementListWidgetExt.java @@ -14,17 +14,12 @@ import org.jetbrains.annotations.Nullable; import java.util.function.Consumer; public class ElementListWidgetExt<E extends ElementListWidgetExt.Entry<E>> extends ContainerObjectSelectionList<E> implements LayoutElement { - protected int x, y; - private double smoothScrollAmount = getScrollAmount(); private boolean returnSmoothAmount = false; private final boolean doSmoothScrolling; public ElementListWidgetExt(Minecraft client, int x, int y, int width, int height, boolean smoothScrolling) { - super(client, width, height, y, y + height, 22); - this.x = this.x0 = x; - this.y = y; - this.x1 = this.x0 + width; + super(client, x, y, width, height); this.doSmoothScrolling = smoothScrolling; setRenderBackground(true); setRenderHeader(false, 0); @@ -40,17 +35,17 @@ public class ElementListWidgetExt<E extends ElementListWidgetExt.Entry<E>> exten @Override protected int getScrollbarPosition() { // default implementation does not respect left/right - return this.x1 - 2; + return this.getX() + this.getWidth() - 2; } @Override - public void render(GuiGraphics graphics, int mouseX, int mouseY, float delta) { + public void renderWidget(GuiGraphics graphics, int mouseX, int mouseY, float delta) { smoothScrollAmount = Mth.lerp(Minecraft.getInstance().getDeltaFrameTime() * 0.5, smoothScrollAmount, getScrollAmount()); returnSmoothAmount = true; - graphics.enableScissor(x0, y0, x1, y1); + graphics.enableScissor(this.getX(), this.getY(), this.getX() + this.getWidth(), this.getY() + this.getHeight()); - super.render(graphics, mouseX, mouseY, delta); + super.renderWidget(graphics, mouseX, mouseY, delta); graphics.disableScissor(); @@ -58,12 +53,10 @@ public class ElementListWidgetExt<E extends ElementListWidgetExt.Entry<E>> exten } public void updateDimensions(ScreenRectangle rectangle) { - this.x0 = rectangle.left(); - this.y0 = rectangle.top(); - this.x1 = rectangle.right(); - this.y1 = rectangle.bottom(); - this.width = rectangle.width(); - this.height = rectangle.height(); + this.setX(rectangle.left()); + this.setY(rectangle.top()); + this.setWidth(rectangle.width()); + this.setHeight(rectangle.height()); } /** @@ -87,10 +80,10 @@ public class ElementListWidgetExt<E extends ElementListWidgetExt.Entry<E>> exten protected E getEntryAtPosition(double x, double y) { y += getScrollAmount(); - if (x < this.x0 || x > this.x1) + if (x < this.getX() || x > this.getX() + this.getWidth()) return null; - int currentY = this.y0 - headerHeight + 4; + int currentY = this.getY() - headerHeight + 4; for (E entry : children()) { if (y >= currentY && y <= currentY + entry.getItemHeight()) { return entry; @@ -124,7 +117,7 @@ public class ElementListWidgetExt<E extends ElementListWidgetExt.Entry<E>> exten @Override protected int getRowTop(int index) { - int integer = y0 + 4 - (int) this.getScrollAmount() + headerHeight; + int integer = getY() + 4 - (int) this.getScrollAmount() + headerHeight; for (int i = 0; i < children().size() && i < index; i++) integer += children().get(i).getItemHeight(); return integer; @@ -141,7 +134,7 @@ public class ElementListWidgetExt<E extends ElementListWidgetExt.Entry<E>> exten int top = this.getRowTop(i); int bottom = top + entry.getItemHeight(); int entryHeight = entry.getItemHeight() - 4; - if (bottom >= this.y0 && top <= this.y1) { + if (bottom >= this.getY() && top <= this.getY() + this.getHeight()) { this.renderItem(graphics, mouseX, mouseY, delta, i, left, top, right, entryHeight); } } @@ -150,38 +143,6 @@ public class ElementListWidgetExt<E extends ElementListWidgetExt.Entry<E>> exten /* END cloth config code */ @Override - public void setX(int i) { - this.x = x0 = i; - this.x1 = x0 + width; - } - - @Override - public void setY(int i) { - this.y = y0 = i; - this.y1 = y0 + height; - } - - @Override - public int getX() { - return x; - } - - @Override - public int getY() { - return y; - } - - @Override - public int getWidth() { - return width; - } - - @Override - public int getHeight() { - return height; - } - - @Override public void visitWidgets(Consumer<AbstractWidget> consumer) { } diff --git a/common/src/main/java/dev/isxander/yacl3/gui/OptionListWidget.java b/common/src/main/java/dev/isxander/yacl3/gui/OptionListWidget.java index 666c3c8..e8b4eb6 100644 --- a/common/src/main/java/dev/isxander/yacl3/gui/OptionListWidget.java +++ b/common/src/main/java/dev/isxander/yacl3/gui/OptionListWidget.java @@ -31,7 +31,7 @@ public class OptionListWidget extends ElementListWidgetExt<OptionListWidget.Entr private DescriptionWithName lastHoveredOption; public OptionListWidget(YACLScreen screen, ConfigCategory category, Minecraft client, int x, int y, int width, int height, Consumer<DescriptionWithName> hoverEvent) { - super(client, x, y, width, height, true); + super(client, width, height, x, y, true); this.yaclScreen = screen; this.category = category; this.hoverEvent = hoverEvent; @@ -183,7 +183,7 @@ public class OptionListWidget extends ElementListWidgetExt<OptionListWidget.Entr @Override protected int getScrollbarPosition() { - return x1 - (int)(width * 0.05f); + return getX() + getWidth() - (int)(width * 0.05f); } public void recacheViewableChildren() { diff --git a/common/src/main/java/dev/isxander/yacl3/gui/TooltipButtonWidget.java b/common/src/main/java/dev/isxander/yacl3/gui/TooltipButtonWidget.java index 396f20b..f439301 100644 --- a/common/src/main/java/dev/isxander/yacl3/gui/TooltipButtonWidget.java +++ b/common/src/main/java/dev/isxander/yacl3/gui/TooltipButtonWidget.java @@ -6,6 +6,7 @@ import net.minecraft.client.gui.screens.Screen; import net.minecraft.client.gui.screens.inventory.tooltip.ClientTooltipPositioner; import net.minecraft.network.chat.Component; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; public class TooltipButtonWidget extends Button { @@ -15,11 +16,6 @@ public class TooltipButtonWidget extends Button { super(x, y, width, height, message, onPress, DEFAULT_NARRATION); this.screen = screen; if (tooltip != null) - setTooltip(Tooltip.create(tooltip)); - } - - @Override - protected @NotNull ClientTooltipPositioner createTooltipPositioner() { - return new YACLTooltipPositioner(this); + setTooltip(new YACLTooltip(tooltip, this)); } } diff --git a/common/src/main/java/dev/isxander/yacl3/gui/YACLTooltip.java b/common/src/main/java/dev/isxander/yacl3/gui/YACLTooltip.java new file mode 100644 index 0000000..966b8a8 --- /dev/null +++ b/common/src/main/java/dev/isxander/yacl3/gui/YACLTooltip.java @@ -0,0 +1,20 @@ +package dev.isxander.yacl3.gui; + +import net.minecraft.client.gui.components.Tooltip; +import net.minecraft.client.gui.navigation.ScreenRectangle; +import net.minecraft.client.gui.screens.inventory.tooltip.ClientTooltipPositioner; +import net.minecraft.network.chat.Component; + +public class YACLTooltip extends Tooltip { + private final net.minecraft.client.gui.components.AbstractWidget widget; + + public YACLTooltip(Component tooltip, net.minecraft.client.gui.components.AbstractWidget widget) { + super(tooltip, tooltip); + this.widget = widget; + } + + @Override + protected ClientTooltipPositioner createTooltipPositioner(boolean bl, boolean bl2, ScreenRectangle screenRectangle) { + return new YACLTooltipPositioner(widget); + } +} diff --git a/common/src/main/java/dev/isxander/yacl3/impl/ConfigCategoryImpl.java b/common/src/main/java/dev/isxander/yacl3/impl/ConfigCategoryImpl.java index 195f6d7..e79d9eb 100644 --- a/common/src/main/java/dev/isxander/yacl3/impl/ConfigCategoryImpl.java +++ b/common/src/main/java/dev/isxander/yacl3/impl/ConfigCategoryImpl.java @@ -7,6 +7,7 @@ import net.minecraft.network.chat.CommonComponents; import net.minecraft.network.chat.Component; import net.minecraft.network.chat.ComponentContents; import net.minecraft.network.chat.MutableComponent; +import net.minecraft.network.chat.contents.PlainTextContents; import org.apache.commons.lang3.Validate; import org.jetbrains.annotations.ApiStatus; import org.jetbrains.annotations.NotNull; @@ -120,7 +121,7 @@ public final class ConfigCategoryImpl implements ConfigCategory { MutableComponent concatenatedTooltip = Component.empty(); boolean first = true; for (Component line : tooltipLines) { - if (line.getContents() == ComponentContents.EMPTY) + if (line.getContents() == PlainTextContents.EMPTY) continue; if (!first) concatenatedTooltip.append("\n"); diff --git a/common/src/main/resources/yacl.accesswidener b/common/src/main/resources/yacl.accesswidener index 286b6ab..7c0e2ca 100644 --- a/common/src/main/resources/yacl.accesswidener +++ b/common/src/main/resources/yacl.accesswidener @@ -9,3 +9,4 @@ accessible field net/minecraft/client/gui/components/tabs/TabNavigationBar width accessible field net/minecraft/client/gui/components/tabs/TabNavigationBar tabManager Lnet/minecraft/client/gui/components/tabs/TabManager; accessible field net/minecraft/client/gui/components/tabs/TabNavigationBar tabs Lcom/google/common/collect/ImmutableList; accessible field net/minecraft/client/gui/components/tabs/TabNavigationBar tabButtons Lcom/google/common/collect/ImmutableList; +accessible method net/minecraft/client/gui/components/Tooltip <init> (Lnet/minecraft/network/chat/Component;Lnet/minecraft/network/chat/Component;)V |