Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add dialogue illustrations #11

Merged
merged 1 commit into from
Feb 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ Updated to 1.20.4
**Mod Interactions**
- REI no longer appears on the RPG dialogue screen variant

**Additions**
- Dialogues can now render extra features, such as entities (either non-existent, or via a selector), or items.

------------------------------------------------------
Version 1.3.1
------------------------------------------------------
Expand Down
27 changes: 21 additions & 6 deletions src/main/java/org/ladysnake/blabber/Blabber.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Blabber
* Copyright (C) 2022-2023 Ladysnake
* Copyright (C) 2022-2024 Ladysnake
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
Expand Down Expand Up @@ -32,11 +32,12 @@
import org.apache.logging.log4j.Logger;
import org.jetbrains.annotations.Nullable;
import org.ladysnake.blabber.api.DialogueActionV2;
import org.ladysnake.blabber.impl.common.BlabberCommand;
import org.ladysnake.blabber.impl.common.BlabberRegistrar;
import org.ladysnake.blabber.impl.common.CommandDialogueAction;
import org.ladysnake.blabber.impl.common.DialogueInitializationException;
import org.ladysnake.blabber.impl.common.PlayerDialogueTracker;
import org.ladysnake.blabber.api.DialogueIllustrationType;
import org.ladysnake.blabber.impl.common.*;
import org.ladysnake.blabber.impl.common.illustrations.DialogueIllustrationCollection;
import org.ladysnake.blabber.impl.common.illustrations.DialogueIllustrationItem;
import org.ladysnake.blabber.impl.common.illustrations.DialogueIllustrationNbtEntity;
import org.ladysnake.blabber.impl.common.illustrations.DialogueIllustrationSelectorEntity;
import org.ladysnake.blabber.impl.common.machine.DialogueStateMachine;

public final class Blabber implements ModInitializer {
Expand Down Expand Up @@ -137,10 +138,24 @@ public static void registerAction(Identifier actionId, Codec<? extends DialogueA
Registry.register(BlabberRegistrar.ACTION_REGISTRY, actionId, codec);
}

/**
* Register a configurable {@link DialogueIllustrationType} to draw extra features in dialogues.
*
* @param illustrationId the identifier used to reference the illustration type in dialogue definition files
* @param type the dialogue illustration type
*/
public static void registerIllustration(Identifier illustrationId, DialogueIllustrationType<?> type) {
Registry.register(BlabberRegistrar.ILLUSTRATION_REGISTRY, illustrationId, type);
}

@Override
public void onInitialize() {
BlabberRegistrar.init();
CommandRegistrationCallback.EVENT.register((dispatcher, registryAccess, environment) -> BlabberCommand.register(dispatcher));
registerAction(id("command"), CommandDialogueAction.CODEC);
registerIllustration(id("group"), DialogueIllustrationCollection.TYPE);
registerIllustration(id("item"), DialogueIllustrationItem.TYPE);
registerIllustration(id("fake_entity"), DialogueIllustrationNbtEntity.TYPE);
registerIllustration(id("entity"), DialogueIllustrationSelectorEntity.TYPE);
}
}
2 changes: 1 addition & 1 deletion src/main/java/org/ladysnake/blabber/DialogueAction.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Blabber
* Copyright (C) 2022-2023 Ladysnake
* Copyright (C) 2022-2024 Ladysnake
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Blabber
* Copyright (C) 2022-2023 Ladysnake
* Copyright (C) 2022-2024 Ladysnake
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
Expand Down
57 changes: 57 additions & 0 deletions src/main/java/org/ladysnake/blabber/api/DialogueIllustration.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Blabber
* Copyright (C) 2022-2024 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.api;

import net.minecraft.client.font.TextRenderer;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.entity.Entity;
import net.minecraft.server.command.ServerCommandSource;
import org.jetbrains.annotations.Nullable;

/**
* A renderable illustration in dialogues, such as entities or items.
*/
public interface DialogueIllustration {
/**
* Draw this illustration to the screen.
*
* @param context a context to draw in
* @param textRenderer a text renderer
* @param x the x position it should be drawn relative to
* @param y the y position it should be drawn relative to
* @param mouseX the current x mouse position
* @param mouseY the current y mouse position
* @param tickDelta how much time has passed since last frame
*/
void render(DrawContext context, TextRenderer textRenderer, int x, int y, int mouseX, int mouseY, float tickDelta);

/**
* @return the DialogueIllustrationType that corresponds to this illustration
*/
DialogueIllustrationType<? extends DialogueIllustration> getType();

/**
* If this illustration contains some text, this will be parsed *server-side*.
* @param source the context in which this is parsed
* @param sender the player that is going to dialogue
* @return a DialogueIllustration with the required text parsed (often, simply this)
*/
default DialogueIllustration parseText(@Nullable ServerCommandSource source, @Nullable Entity sender) {
return this;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
* Blabber
* Copyright (C) 2022-2024 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.api;

import com.mojang.serialization.Codec;
import net.minecraft.network.PacketByteBuf;
import net.minecraft.util.dynamic.Codecs;
import org.jetbrains.annotations.ApiStatus;
import org.ladysnake.blabber.impl.common.BlabberRegistrar;
import org.ladysnake.blabber.impl.common.illustrations.DialogueIllustrationCollection;

import java.util.function.BiConsumer;
import java.util.function.Function;

/**
* A type of {@link DialogueIllustration}, used for registration and abstraction.
* @param <T> the DialogueIllustration type this type creates
*/
public final class DialogueIllustrationType<T extends DialogueIllustration> {
public static final Codec<DialogueIllustration> CODEC = Codecs.createRecursive("illustration_type", self ->
Codecs.alternatively(
BlabberRegistrar.ILLUSTRATION_REGISTRY.getCodec()
.dispatch("type", DialogueIllustration::getType, DialogueIllustrationType::getCodec),
Codec.list(self).xmap(DialogueIllustrationCollection::new, DialogueIllustrationCollection::elements)
)
);

private final Codec<T> codec;
private final Function<PacketByteBuf, T> read;
private final BiConsumer<PacketByteBuf, T> write;

public DialogueIllustrationType(Codec<T> codec, Function<PacketByteBuf, T> read, BiConsumer<PacketByteBuf, T> write) {
this.codec = codec;
this.read = read;
this.write = write;
}

/**
* @return A codec to serialize and deserialize this DialogueIllustration
*/
public Codec<T> getCodec() {
return codec;
}

/**
* Parses this type of DialogueIllustration from a packet. The data within should be everything the client needs to render this
* @param buf the packet's data
* @return a newly parsed DialogueIllustration corresponding to this type
*/
@ApiStatus.Experimental
public T readFromPacket(PacketByteBuf buf) {
SekoiaTree marked this conversation as resolved.
Show resolved Hide resolved
return this.read.apply(buf);
}

/**
* Write the data this illustration needs to be drawn client-side to a packet
* @param buf the packet to write to
* @param toWrite the illustration to write
*/
@ApiStatus.Experimental
public void writeToPacket(PacketByteBuf buf, T toWrite) {
this.write.accept(buf, toWrite);
}

/**
* Same as writeToPacket, but does an unchecked cast, for when the type information is lost somewhere.
* Make sure it's safe.
*/
@ApiStatus.Internal
public void writeToPacketUnsafe(PacketByteBuf buf, DialogueIllustration illustration) {
SekoiaTree marked this conversation as resolved.
Show resolved Hide resolved
//noinspection unchecked
this.writeToPacket(buf, (T) illustration);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Blabber
* Copyright (C) 2022-2023 Ladysnake
* Copyright (C) 2022-2024 Ladysnake
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
Expand Down Expand Up @@ -30,6 +30,7 @@
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.Nullable;
import org.ladysnake.blabber.Blabber;
import org.ladysnake.blabber.api.DialogueIllustration;
import org.ladysnake.blabber.impl.common.DialogueScreenHandler;
import org.ladysnake.blabber.impl.common.machine.AvailableChoice;
import org.ladysnake.blabber.impl.common.model.ChoiceResult;
Expand Down Expand Up @@ -218,6 +219,14 @@ public void render(DrawContext context, int mouseX, int mouseY, float tickDelta)
assert client != null;

int y = mainTextMinY;

for (String illustrationName : this.handler.getCurrentIllustrations()) {
DialogueIllustration illustration = this.handler.getIllustration(illustrationName);
if (illustration != null) {
illustration.render(context, this.textRenderer, 0, 0, mouseX, mouseY, tickDelta);
}
}

Text mainText = this.handler.getCurrentText();

context.drawTextWrapped(this.textRenderer, mainText, mainTextMinX, y, mainTextMaxWidth, mainTextColor);
Expand All @@ -230,6 +239,14 @@ public void render(DrawContext context, int mouseX, int mouseY, float tickDelta)
boolean selected = i == this.selectedChoice;
int choiceColor = choice.unavailabilityMessage().isPresent() ? lockedChoiceColor : selected ? selectedChoiceColor : this.choiceColor;
context.drawTextWrapped(this.textRenderer, choice.text(), choiceListMinX, y, choiceListMaxWidth, choiceColor);

for (String illustrationName : choice.illustrations()) {
DialogueIllustration illustration = this.handler.getIllustration(illustrationName);
if (illustration != null) {
illustration.render(context, this.textRenderer, choiceListMinX, y, mouseX, mouseY, tickDelta);
}
}

if (selected) {
if (choice.unavailabilityMessage().isPresent()) {
context.drawGuiTexture(lockIconTexture, selectionIconMinX, y + selectionIconMarginTop, selectionIconSize, selectionIconSize);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Blabber
* Copyright (C) 2022-2023 Ladysnake
* Copyright (C) 2022-2024 Ladysnake
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Blabber
* Copyright (C) 2022-2023 Ladysnake
* Copyright (C) 2022-2024 Ladysnake
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Blabber
* Copyright (C) 2022-2023 Ladysnake
* Copyright (C) 2022-2024 Ladysnake
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Blabber
* Copyright (C) 2022-2023 Ladysnake
* Copyright (C) 2022-2024 Ladysnake
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Blabber
* Copyright (C) 2022-2023 Ladysnake
* Copyright (C) 2022-2024 Ladysnake
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Blabber
* Copyright (C) 2022-2023 Ladysnake
* Copyright (C) 2022-2024 Ladysnake
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Blabber
* Copyright (C) 2022-2023 Ladysnake
* Copyright (C) 2022-2024 Ladysnake
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
Expand Down Expand Up @@ -41,6 +41,7 @@
import net.minecraft.util.Identifier;
import org.ladysnake.blabber.Blabber;
import org.ladysnake.blabber.api.DialogueActionV2;
import org.ladysnake.blabber.api.DialogueIllustrationType;
import org.ladysnake.blabber.impl.common.machine.DialogueStateMachine;
import org.ladysnake.blabber.impl.common.packets.ChoiceAvailabilityPacket;
import org.ladysnake.blabber.impl.common.packets.DialogueListPacket;
Expand All @@ -62,6 +63,12 @@ public final class BlabberRegistrar implements EntityComponentInitializer {
public static final Registry<Codec<? extends DialogueActionV2>> ACTION_REGISTRY = FabricRegistryBuilder.from(
new SimpleRegistry<>(ACTION_REGISTRY_KEY, Lifecycle.stable(), false)
).buildAndRegister();

public static final RegistryKey<Registry<DialogueIllustrationType<?>>> ILLUSTRATION_REGISTRY_KEY = RegistryKey.ofRegistry(Blabber.id("dialogue_illustrations"));
public static final Registry<DialogueIllustrationType<?>> ILLUSTRATION_REGISTRY = FabricRegistryBuilder.from(
new SimpleRegistry<>(ILLUSTRATION_REGISTRY_KEY, Lifecycle.stable(), false)
).buildAndRegister();

public static final SuggestionProvider<ServerCommandSource> ALL_DIALOGUES = SuggestionProviders.register(
Blabber.id("available_dialogues"),
(context, builder) -> CommandSource.suggestIdentifiers(DialogueRegistry.getIds(), builder)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Blabber
* Copyright (C) 2022-2023 Ladysnake
* Copyright (C) 2022-2024 Ladysnake
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Blabber
* Copyright (C) 2022-2023 Ladysnake
* Copyright (C) 2022-2024 Ladysnake
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Blabber
* Copyright (C) 2022-2023 Ladysnake
* Copyright (C) 2022-2024 Ladysnake
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Blabber
* Copyright (C) 2022-2023 Ladysnake
* Copyright (C) 2022-2024 Ladysnake
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
Expand Down