Skip to content

Commit

Permalink
Backport to 1.20.1, closes #5
Browse files Browse the repository at this point in the history
  • Loading branch information
Pyrofab committed Oct 24, 2023
1 parent 1b6ad49 commit 20b8803
Show file tree
Hide file tree
Showing 12 changed files with 93 additions and 29 deletions.
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
10 changes: 5 additions & 5 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
8 changes: 4 additions & 4 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -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"}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@
import java.util.stream.IntStream;

public class BlabberDialogueScreen extends HandledScreen<DialogueScreenHandler> {
public static final List<Identifier> DIALOGUE_ARROWS = IntStream.range(1, 6).mapToObj(i -> Blabber.id("container/dialogue/dialogue_arrow_" + i)).toList();
public static final List<Identifier> DIALOGUE_LOCKS = IntStream.range(1, 4).mapToObj(i -> Blabber.id("container/dialogue/dialogue_lock_" + i)).toList();
public static final List<Identifier> DIALOGUE_ARROWS = IntStream.range(1, 6).mapToObj(i -> Blabber.id("textures/gui/sprites/container/dialogue/dialogue_arrow_" + i + ".png")).toList();
public static final List<Identifier> 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;
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;

Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
}
}
});
Expand Down
Original file line number Diff line number Diff line change
@@ -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 <https://www.gnu.org/licenses>.
*/
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<A> extends OptionalFieldCodec<A> {
public static <A> MapCodec<Optional<A>> of(Codec<A> elementCodec, String name) {
return new FailingOptionalFieldCodec<>(name, elementCodec);
}

public static <A> MapCodec<A> of(Codec<A> 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<A> elementCodec;

private FailingOptionalFieldCodec(String name, Codec<A> elementCodec) {
super(name, elementCodec);
this.name = name;
this.elementCodec = elementCodec;
}

@Override
public <T> DataResult<Optional<A>> decode(final DynamicOps<T> ops, final MapLike<T> input) {
final T value = input.get(name);
if (value == null) {
return DataResult.success(Optional.empty());
}

return elementCodec.parse(ops, value).map(Optional::ofNullable);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -36,10 +37,10 @@ public record DialogueState(
) {
public static final Codec<DialogueState> 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));


Expand All @@ -61,7 +62,7 @@ public record Choice(Text text, String next, Optional<DialogueChoiceCondition> c
public static final Codec<Choice> 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Text> message) {
public static final Codec<UnavailableAction> 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));
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<UnavailableDisplay> CODEC = StringIdentifiable.createCodec(UnavailableDisplay::values);
public static final com.mojang.serialization.Codec<UnavailableDisplay> CODEC = StringIdentifiable.createCodec(UnavailableDisplay::values);

private final String id;

Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/fabric.mod.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down

0 comments on commit 20b8803

Please sign in to comment.