-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
75d22df
commit 6468bfa
Showing
15 changed files
with
507 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
src/main/java/org/ladysnake/blabber/api/DialogueIllustration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package org.ladysnake.blabber.api; | ||
|
||
import com.mojang.brigadier.exceptions.CommandSyntaxException; | ||
import net.minecraft.client.font.TextRenderer; | ||
import net.minecraft.client.gui.DrawContext; | ||
import net.minecraft.entity.Entity; | ||
import net.minecraft.network.PacketByteBuf; | ||
import net.minecraft.server.command.ServerCommandSource; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
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); | ||
|
||
/** | ||
* Write the data this illustration needs to be drawn client-side to a packet | ||
* @param packetByteBuf the packet to write to | ||
*/ | ||
void writeToPacket(PacketByteBuf packetByteBuf); | ||
|
||
/** | ||
* @return the DialogueIllustrationType that corresponds to this illustration | ||
*/ | ||
DialogueIllustrationType<?> 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) | ||
* @throws CommandSyntaxException if the parsing fails | ||
*/ | ||
default DialogueIllustration parseText(@Nullable ServerCommandSource source, @Nullable Entity sender) throws CommandSyntaxException { | ||
return this; | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
src/main/java/org/ladysnake/blabber/api/DialogueIllustrationType.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package org.ladysnake.blabber.api; | ||
|
||
import com.mojang.brigadier.exceptions.CommandSyntaxException; | ||
import com.mojang.serialization.Codec; | ||
import net.minecraft.entity.Entity; | ||
import net.minecraft.network.PacketByteBuf; | ||
import net.minecraft.server.command.ServerCommandSource; | ||
import org.jetbrains.annotations.Nullable; | ||
import org.ladysnake.blabber.impl.common.BlabberRegistrar; | ||
|
||
import java.util.List; | ||
|
||
public abstract class DialogueIllustrationType<T extends DialogueIllustration> { | ||
public static final Codec<DialogueIllustration> CODEC = BlabberRegistrar.ILLUSTRATION_REGISTRY.getCodec() | ||
.dispatch("type", DialogueIllustration::getType, DialogueIllustrationType::getCodec); | ||
|
||
public static List<DialogueIllustration> readIllustrationList(PacketByteBuf buf) { | ||
return buf.readList(b -> { | ||
int rawId = b.readInt(); | ||
DialogueIllustrationType<?> type = BlabberRegistrar.ILLUSTRATION_REGISTRY.getEntry(rawId).orElseThrow().value(); | ||
assert type != null; | ||
return type.readFromPacket(b); | ||
}); | ||
} | ||
|
||
public static void writeIllustrationList(PacketByteBuf buf, List<DialogueIllustration> illustrations) { | ||
buf.writeCollection(illustrations, (b, i) -> { | ||
// Write the raw ID, then the packet itself. | ||
b.writeInt(BlabberRegistrar.ILLUSTRATION_REGISTRY.getRawId(i.getType())); | ||
i.writeToPacket(b); | ||
}); | ||
} | ||
|
||
/** | ||
* @return A codec to serialize and deserialize this DialogueIllustration | ||
*/ | ||
public abstract Codec<T> getCodec(); | ||
|
||
/** | ||
* 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 | ||
*/ | ||
|
||
public abstract T readFromPacket(PacketByteBuf buf); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
src/main/java/org/ladysnake/blabber/impl/common/model/DialogueIllustrationItem.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package org.ladysnake.blabber.impl.common.model; | ||
|
||
import com.mojang.serialization.Codec; | ||
import com.mojang.serialization.codecs.RecordCodecBuilder; | ||
import net.minecraft.client.font.TextRenderer; | ||
import net.minecraft.client.gui.DrawContext; | ||
import net.minecraft.item.ItemStack; | ||
import net.minecraft.nbt.NbtCompound; | ||
import net.minecraft.network.PacketByteBuf; | ||
import org.ladysnake.blabber.api.DialogueIllustration; | ||
import org.ladysnake.blabber.api.DialogueIllustrationType; | ||
|
||
public record DialogueIllustrationItem(ItemStack stack, int x, int y) implements DialogueIllustration { | ||
private static final Codec<DialogueIllustrationItem> CODEC = RecordCodecBuilder.create(instance -> instance.group( | ||
ItemStack.CODEC.fieldOf("item").forGetter(DialogueIllustrationItem::stack), | ||
Codec.INT.fieldOf("x").forGetter(DialogueIllustrationItem::x), | ||
Codec.INT.fieldOf("y").forGetter(DialogueIllustrationItem::y) | ||
).apply(instance, DialogueIllustrationItem::new)); | ||
|
||
@Override | ||
public void render(DrawContext context, TextRenderer textRenderer, int x, int y, int mouseX, int mouseY, float tickDelta) { | ||
// We draw the actual item, then the count and bar and such. | ||
context.drawItem(stack, this.x + x, this.y + y); | ||
context.drawItemInSlot(textRenderer, stack, this.x + x, this.y + y); | ||
} | ||
|
||
@Override | ||
public void writeToPacket(PacketByteBuf buf) { | ||
buf.writeNbt(stack.writeNbt(new NbtCompound())); | ||
buf.writeInt(x); | ||
buf.writeInt(y); | ||
} | ||
|
||
@Override | ||
public DialogueIllustrationType<?> getType() { | ||
return IllustrationType.INSTANCE; | ||
} | ||
|
||
public static class IllustrationType extends DialogueIllustrationType<DialogueIllustrationItem> { | ||
public static final IllustrationType INSTANCE = new IllustrationType(); | ||
|
||
private IllustrationType() {} | ||
|
||
@Override | ||
public Codec<DialogueIllustrationItem> getCodec() { | ||
return DialogueIllustrationItem.CODEC; | ||
} | ||
|
||
@Override | ||
public DialogueIllustrationItem readFromPacket(PacketByteBuf buf) { | ||
return new DialogueIllustrationItem(ItemStack.fromNbt(buf.readNbt()), buf.readInt(), buf.readInt()); | ||
} | ||
} | ||
} |
Oops, something went wrong.