From 20b88037a07e129f2c524ed04dd4421d3b41b0fa Mon Sep 17 00:00:00 2001 From: Pyrofab Date: Tue, 24 Oct 2023 14:55:53 +0200 Subject: [PATCH] Backport to 1.20.1, closes #5 --- build.gradle | 4 +- gradle.properties | 10 +-- gradle/libs.versions.toml | 8 +-- .../impl/client/BlabberDialogueScreen.java | 14 +++-- .../blabber/impl/common/BlabberRegistrar.java | 2 +- .../common/FailingOptionalFieldCodec.java | 61 +++++++++++++++++++ .../impl/common/PlayerDialogueTracker.java | 2 +- .../impl/common/model/DialogueState.java | 11 ++-- .../impl/common/model/UnavailableAction.java | 3 +- .../impl/common/model/UnavailableDisplay.java | 3 +- src/main/resources/fabric.mod.json | 2 +- .../babblings/tests/BlabberTestSuite.java | 2 +- 12 files changed, 93 insertions(+), 29 deletions(-) create mode 100644 src/main/java/org/ladysnake/blabber/impl/common/FailingOptionalFieldCodec.java diff --git a/build.gradle b/build.gradle index f2a4813..0b38c89 100644 --- a/build.gradle +++ b/build.gradle @@ -42,8 +42,8 @@ dependencies { modIncludeImplementation(libs.cca.base) modIncludeImplementation(libs.cca.entity) modIncludeImplementation(libs.permissionsApi) - modLocalImplementation(libs.rei.api) - modLocalImplementation(libs.emi) + modCompileOnly(libs.rei.api) + modCompileOnly(libs.emi) compileOnly(libs.mcAnnotations) diff --git a/gradle.properties b/gradle.properties index 8a58723..5cb3f07 100644 --- a/gradle.properties +++ b/gradle.properties @@ -3,24 +3,24 @@ org.gradle.jvmargs=-Xmx2G # Fabric Properties # check these on https://fabricmc.net/versions.html - minecraft_version=1.20.2 - yarn_mappings=1.20.2+build.1 + minecraft_version=1.20.1 + yarn_mappings=1.20.1+build.10 loader_version=0.14.22 java_version=17 # Mod Properties - mod_version = 1.0.0 + mod_version = 1.0.0-mc1.20.1 maven_group = org.ladysnake archives_base_name = blabber # Dependencies - fabric_version=0.89.2+1.20.2 + fabric_version=0.90.4+1.20.1 # Publishing license_header = LGPL curseforge_id = 565396 modrinth_id = 2oRMVFgd - curseforge_versions = 1.20.2 + curseforge_versions = 1.20.1 cf_requirements = fabric-api cf_embeddeds = cardinal-components-api release_type = release diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index ee9c11a..aa6a77e 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -1,16 +1,16 @@ [versions] -cca = "5.3.0" -elmendorf = "0.12.0" +cca = "5.2.0" +elmendorf = "0.11.0" emi = "1.0.23+1.20.2" fpa = "0.2-SNAPSHOT" mcAnnotations = "1.0" -modmenu = "8.0.0-beta.2" +modmenu = "7.2.2" rei = "13.0.666" [libraries] cca-base = { module = "dev.onyxstudios.cardinal-components-api:cardinal-components-base", version.ref = "cca" } cca-entity = { module = "dev.onyxstudios.cardinal-components-api:cardinal-components-entity", version.ref = "cca" } -elmendorf = { module = "org.ladysnake:elmendorf", version.ref = "elmendorf" } +elmendorf = { module = "io.github.ladysnake:elmendorf", version.ref = "elmendorf" } emi = { module = "dev.emi:emi-fabric", version.ref = "emi" } mcAnnotations = { module = "com.demonwav.mcdev:annotations", version.ref = "mcAnnotations" } modmenu = { module = "com.terraformersmc:modmenu", version.ref = "modmenu"} diff --git a/src/main/java/org/ladysnake/blabber/impl/client/BlabberDialogueScreen.java b/src/main/java/org/ladysnake/blabber/impl/client/BlabberDialogueScreen.java index d68e2a1..3d80cbf 100644 --- a/src/main/java/org/ladysnake/blabber/impl/client/BlabberDialogueScreen.java +++ b/src/main/java/org/ladysnake/blabber/impl/client/BlabberDialogueScreen.java @@ -37,8 +37,8 @@ import java.util.stream.IntStream; public class BlabberDialogueScreen extends HandledScreen { - public static final List DIALOGUE_ARROWS = IntStream.range(1, 6).mapToObj(i -> Blabber.id("container/dialogue/dialogue_arrow_" + i)).toList(); - public static final List DIALOGUE_LOCKS = IntStream.range(1, 4).mapToObj(i -> Blabber.id("container/dialogue/dialogue_lock_" + i)).toList(); + public static final List DIALOGUE_ARROWS = IntStream.range(1, 6).mapToObj(i -> Blabber.id("textures/gui/sprites/container/dialogue/dialogue_arrow_" + i + ".png")).toList(); + public static final List DIALOGUE_LOCKS = IntStream.range(1, 4).mapToObj(i -> Blabber.id("textures/gui/sprites/container/dialogue/dialogue_lock_" + i + ".png")).toList(); public static final int MIN_RENDER_Y = 40; public static final int TITLE_GAP = 20; @@ -126,7 +126,7 @@ private void onBigChoiceMade(boolean yes) { } @Override - public boolean mouseScrolled(double mouseX, double mouseY, double horizontalAmount, double verticalAmount) { + public boolean mouseScrolled(double mouseX, double mouseY, double verticalAmount) { this.scrollDialogueChoice(verticalAmount); return true; } @@ -163,7 +163,7 @@ private int getTextBoundedHeight(Text text, int maxWidth) { @Override public void render(DrawContext context, int mouseX, int mouseY, float tickDelta) { - super.render(context, mouseX, mouseY, tickDelta); + this.renderBackground(context); assert client != null; @@ -183,16 +183,18 @@ public void render(DrawContext context, int mouseX, int mouseY, float tickDelta) if (selected) { int choiceIconSize = 16; if (choice.unavailabilityMessage().isPresent()) { - context.drawGuiTexture(DIALOGUE_LOCKS.get(0), 4, y - 4, choiceIconSize, choiceIconSize); + context.drawTexture(DIALOGUE_LOCKS.get(0), 4, y - 4, 0, 0, choiceIconSize, choiceIconSize, choiceIconSize, choiceIconSize); context.drawTooltip(this.textRenderer, choice.unavailabilityMessage().get(), this.hoveringChoice ? mouseX : MAX_TEXT_WIDTH, this.hoveringChoice ? mouseY : y); } else { - context.drawGuiTexture(DIALOGUE_ARROWS.get(0), 4, y - 4, choiceIconSize, choiceIconSize); + context.drawTexture(DIALOGUE_ARROWS.get(0), 4, y - 4, 0, 0, choiceIconSize, choiceIconSize, choiceIconSize, choiceIconSize); } } y += strHeight + CHOICE_GAP; } context.drawText(this.textRenderer, instructions, (this.width - this.textRenderer.getWidth(instructions)) / 2, this.height - 30, 0x808080, false); + + super.render(context, mouseX, mouseY, tickDelta); } @Override diff --git a/src/main/java/org/ladysnake/blabber/impl/common/BlabberRegistrar.java b/src/main/java/org/ladysnake/blabber/impl/common/BlabberRegistrar.java index b6f46d4..574b94e 100644 --- a/src/main/java/org/ladysnake/blabber/impl/common/BlabberRegistrar.java +++ b/src/main/java/org/ladysnake/blabber/impl/common/BlabberRegistrar.java @@ -63,7 +63,7 @@ public static void init() { server.execute(() -> { if (player.currentScreenHandler instanceof DialogueScreenHandler dialogueHandler) { if (!dialogueHandler.makeChoice(player, choice)) { - responseSender.sendPacket(new SelectedDialogueStatePacket(dialogueHandler.getCurrentStateKey())); + ServerPlayNetworking.send(player, new SelectedDialogueStatePacket(dialogueHandler.getCurrentStateKey())); } } }); diff --git a/src/main/java/org/ladysnake/blabber/impl/common/FailingOptionalFieldCodec.java b/src/main/java/org/ladysnake/blabber/impl/common/FailingOptionalFieldCodec.java new file mode 100644 index 0000000..9349be4 --- /dev/null +++ b/src/main/java/org/ladysnake/blabber/impl/common/FailingOptionalFieldCodec.java @@ -0,0 +1,61 @@ +/* + * Blabber + * Copyright (C) 2022-2023 Ladysnake + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; If not, see . + */ +package org.ladysnake.blabber.impl.common; + +import com.mojang.serialization.Codec; +import com.mojang.serialization.DataResult; +import com.mojang.serialization.DynamicOps; +import com.mojang.serialization.MapCodec; +import com.mojang.serialization.MapLike; +import com.mojang.serialization.codecs.OptionalFieldCodec; + +import java.util.Objects; +import java.util.Optional; + +/** Just like {@link com.mojang.serialization.codecs.OptionalFieldCodec}, except it errors if you got invalid input */ +public class FailingOptionalFieldCodec extends OptionalFieldCodec { + public static MapCodec> of(Codec elementCodec, String name) { + return new FailingOptionalFieldCodec<>(name, elementCodec); + } + + public static MapCodec of(Codec elementCodec, String name, A defaultValue) { + return of(elementCodec, name).xmap( + o -> o.orElse(defaultValue), + a -> Objects.equals(a, defaultValue) ? Optional.empty() : Optional.of(a) + ); + } + + private final String name; + private final Codec elementCodec; + + private FailingOptionalFieldCodec(String name, Codec elementCodec) { + super(name, elementCodec); + this.name = name; + this.elementCodec = elementCodec; + } + + @Override + public DataResult> decode(final DynamicOps ops, final MapLike input) { + final T value = input.get(name); + if (value == null) { + return DataResult.success(Optional.empty()); + } + + return elementCodec.parse(ops, value).map(Optional::ofNullable); + } +} diff --git a/src/main/java/org/ladysnake/blabber/impl/common/PlayerDialogueTracker.java b/src/main/java/org/ladysnake/blabber/impl/common/PlayerDialogueTracker.java index c8f3429..5bb02ac 100644 --- a/src/main/java/org/ladysnake/blabber/impl/common/PlayerDialogueTracker.java +++ b/src/main/java/org/ladysnake/blabber/impl/common/PlayerDialogueTracker.java @@ -125,7 +125,7 @@ public void serverTick() { .add(LootContextParameters.ORIGIN, player.getPos()) .addOptional(LootContextParameters.THIS_ENTITY, player) .build(LootContextTypes.COMMAND) - ).build(Optional.empty())); + ).build(null)); } return null; } diff --git a/src/main/java/org/ladysnake/blabber/impl/common/model/DialogueState.java b/src/main/java/org/ladysnake/blabber/impl/common/model/DialogueState.java index d325d67..8bf1ed2 100644 --- a/src/main/java/org/ladysnake/blabber/impl/common/model/DialogueState.java +++ b/src/main/java/org/ladysnake/blabber/impl/common/model/DialogueState.java @@ -22,6 +22,7 @@ import net.minecraft.text.Text; import net.minecraft.util.dynamic.Codecs; import org.apache.commons.lang3.StringUtils; +import org.ladysnake.blabber.impl.common.FailingOptionalFieldCodec; import org.ladysnake.blabber.impl.common.InstancedDialogueAction; import java.util.List; @@ -36,10 +37,10 @@ public record DialogueState( ) { public static final Codec CODEC = RecordCodecBuilder.create(instance -> instance.group( // Kinda optional, but we still want errors if you got it wrong >:( - Codecs.createStrictOptionalFieldCodec(Codecs.TEXT, "text", Text.empty()).forGetter(DialogueState::text), - Codecs.createStrictOptionalFieldCodec(Codec.list(Choice.CODEC), "choices", List.of()).forGetter(DialogueState::choices), - Codecs.createStrictOptionalFieldCodec(InstancedDialogueAction.CODEC, "action").forGetter(DialogueState::action), - Codecs.createStrictOptionalFieldCodec(Codec.STRING.xmap(s -> Enum.valueOf(ChoiceResult.class, s.toUpperCase(Locale.ROOT)), Enum::name), "type", ChoiceResult.DEFAULT).forGetter(DialogueState::type) + FailingOptionalFieldCodec.of(Codecs.TEXT, "text", Text.empty()).forGetter(DialogueState::text), + FailingOptionalFieldCodec.of(Codec.list(Choice.CODEC), "choices", List.of()).forGetter(DialogueState::choices), + FailingOptionalFieldCodec.of(InstancedDialogueAction.CODEC, "action").forGetter(DialogueState::action), + FailingOptionalFieldCodec.of(Codec.STRING.xmap(s -> Enum.valueOf(ChoiceResult.class, s.toUpperCase(Locale.ROOT)), Enum::name), "type", ChoiceResult.DEFAULT).forGetter(DialogueState::type) ).apply(instance, DialogueState::new)); @@ -61,7 +62,7 @@ public record Choice(Text text, String next, Optional c public static final Codec CODEC = RecordCodecBuilder.create(instance -> instance.group( Codecs.TEXT.fieldOf("text").forGetter(Choice::text), Codec.STRING.fieldOf("next").forGetter(Choice::next), - Codecs.createStrictOptionalFieldCodec(DialogueChoiceCondition.CODEC, "only_if").forGetter(Choice::condition) + FailingOptionalFieldCodec.of(DialogueChoiceCondition.CODEC, "only_if").forGetter(Choice::condition) ).apply(instance, Choice::new)); @Override diff --git a/src/main/java/org/ladysnake/blabber/impl/common/model/UnavailableAction.java b/src/main/java/org/ladysnake/blabber/impl/common/model/UnavailableAction.java index 04397ca..567ee43 100644 --- a/src/main/java/org/ladysnake/blabber/impl/common/model/UnavailableAction.java +++ b/src/main/java/org/ladysnake/blabber/impl/common/model/UnavailableAction.java @@ -21,12 +21,13 @@ import com.mojang.serialization.codecs.RecordCodecBuilder; import net.minecraft.text.Text; import net.minecraft.util.dynamic.Codecs; +import org.ladysnake.blabber.impl.common.FailingOptionalFieldCodec; import java.util.Optional; public record UnavailableAction(UnavailableDisplay display, Optional message) { public static final Codec CODEC = RecordCodecBuilder.create(instance -> instance.group( UnavailableDisplay.CODEC.fieldOf("display").forGetter(UnavailableAction::display), - Codecs.createStrictOptionalFieldCodec(Codecs.TEXT, "message").forGetter(UnavailableAction::message) + FailingOptionalFieldCodec.of(Codecs.TEXT, "message").forGetter(UnavailableAction::message) ).apply(instance, UnavailableAction::new)); } diff --git a/src/main/java/org/ladysnake/blabber/impl/common/model/UnavailableDisplay.java b/src/main/java/org/ladysnake/blabber/impl/common/model/UnavailableDisplay.java index b1a9f0d..34563de 100644 --- a/src/main/java/org/ladysnake/blabber/impl/common/model/UnavailableDisplay.java +++ b/src/main/java/org/ladysnake/blabber/impl/common/model/UnavailableDisplay.java @@ -17,13 +17,12 @@ */ package org.ladysnake.blabber.impl.common.model; -import com.mojang.serialization.Codec; import net.minecraft.util.StringIdentifiable; public enum UnavailableDisplay implements StringIdentifiable { GRAYED_OUT("grayed_out"), HIDDEN("hidden"); - public static final Codec CODEC = StringIdentifiable.createCodec(UnavailableDisplay::values); + public static final com.mojang.serialization.Codec CODEC = StringIdentifiable.createCodec(UnavailableDisplay::values); private final String id; diff --git a/src/main/resources/fabric.mod.json b/src/main/resources/fabric.mod.json index 53590f3..5c6e753 100644 --- a/src/main/resources/fabric.mod.json +++ b/src/main/resources/fabric.mod.json @@ -38,7 +38,7 @@ "depends": { "fabricloader": ">=0.11.3", "fabric": "*", - "minecraft": ">=1.20.2", + "minecraft": ">=1.20.1", "java": ">=17", "cardinal-components-entity": ">=3.0.0" }, diff --git a/src/testmod/java/org/ladysnake/babblings/tests/BlabberTestSuite.java b/src/testmod/java/org/ladysnake/babblings/tests/BlabberTestSuite.java index d59372f..5148334 100644 --- a/src/testmod/java/org/ladysnake/babblings/tests/BlabberTestSuite.java +++ b/src/testmod/java/org/ladysnake/babblings/tests/BlabberTestSuite.java @@ -22,6 +22,7 @@ import com.mojang.datafixers.util.Pair; import com.mojang.serialization.DataResult; import com.mojang.serialization.JsonOps; +import io.github.ladysnake.elmendorf.GameTestUtil; import net.fabricmc.fabric.api.gametest.v1.FabricGameTest; import net.minecraft.registry.entry.RegistryEntry; import net.minecraft.server.network.ServerPlayerEntity; @@ -32,7 +33,6 @@ import org.ladysnake.blabber.impl.common.BlabberRegistrar; import org.ladysnake.blabber.impl.common.DialogueScreenHandler; import org.ladysnake.blabber.impl.common.model.DialogueTemplate; -import org.ladysnake.elmendorf.GameTestUtil; import java.io.InputStreamReader; import java.util.List;