-
Notifications
You must be signed in to change notification settings - Fork 5
/
DialogueTemplate.java
69 lines (60 loc) · 3.06 KB
/
DialogueTemplate.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/*
* 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.impl.common.model;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import com.mojang.serialization.Codec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import net.minecraft.entity.Entity;
import net.minecraft.network.PacketByteBuf;
import net.minecraft.server.command.ServerCommandSource;
import org.jetbrains.annotations.Nullable;
import java.util.HashMap;
import java.util.Map;
public record DialogueTemplate(String start, boolean unskippable, Map<String, DialogueState> states, DialogueLayout layout) {
public static final Codec<DialogueTemplate> CODEC = RecordCodecBuilder.create(instance -> instance.group(
Codec.STRING.fieldOf("start_at").forGetter(DialogueTemplate::start),
Codec.BOOL.optionalFieldOf("unskippable", false).forGetter(DialogueTemplate::unskippable),
Codec.unboundedMap(Codec.STRING, DialogueState.CODEC).fieldOf("states").forGetter(DialogueTemplate::states),
DialogueLayout.CODEC.optionalFieldOf("layout", DialogueLayout.DEFAULT).forGetter(DialogueTemplate::layout)
).apply(instance, DialogueTemplate::new));
public static void writeToPacket(PacketByteBuf buf, DialogueTemplate dialogue) {
buf.writeString(dialogue.start());
buf.writeBoolean(dialogue.unskippable());
buf.writeMap(dialogue.states(), PacketByteBuf::writeString, DialogueState::writeToPacket);
DialogueLayout.writeToPacket(buf, dialogue.layout());
}
public DialogueTemplate(PacketByteBuf buf) {
this(buf.readString(), buf.readBoolean(), buf.readMap(PacketByteBuf::readString, DialogueState::new), new DialogueLayout(buf));
}
public DialogueTemplate parseText(@Nullable ServerCommandSource source, @Nullable Entity sender) throws CommandSyntaxException {
Map<String, DialogueState> parsedStates = new HashMap<>();
for (Map.Entry<String, DialogueState> state : states().entrySet()) {
parsedStates.put(state.getKey(), state.getValue().parseText(source, sender));
}
return new DialogueTemplate(
start(),
unskippable(),
parsedStates,
layout()
);
}
@Override
public String toString() {
return "DialogueTemplate[start=%s, states=%s%s]".formatted(start, states, unskippable ? " (unskippable)" : "");
}
}