-
Notifications
You must be signed in to change notification settings - Fork 5
/
PlayerDialogueTracker.java
210 lines (186 loc) · 8.94 KB
/
PlayerDialogueTracker.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/*
* 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;
import com.google.common.base.Preconditions;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import dev.onyxstudios.cca.api.v3.component.ComponentKey;
import dev.onyxstudios.cca.api.v3.component.ComponentRegistry;
import dev.onyxstudios.cca.api.v3.component.tick.ServerTickingComponent;
import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.loot.context.LootContext;
import net.minecraft.loot.context.LootContextParameterSet;
import net.minecraft.loot.context.LootContextParameters;
import net.minecraft.loot.context.LootContextTypes;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.nbt.NbtElement;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.text.Text;
import net.minecraft.util.Identifier;
import org.jetbrains.annotations.Nullable;
import org.ladysnake.blabber.Blabber;
import org.ladysnake.blabber.impl.common.machine.DialogueStateMachine;
import org.ladysnake.blabber.impl.common.model.DialogueTemplate;
import org.ladysnake.blabber.impl.common.packets.ChoiceAvailabilityPacket;
import java.util.Optional;
import java.util.UUID;
public final class PlayerDialogueTracker implements ServerTickingComponent {
public static final ComponentKey<PlayerDialogueTracker> KEY = ComponentRegistry.getOrCreate(Blabber.id("dialogue_tracker"), PlayerDialogueTracker.class);
private final PlayerEntity player;
private @Nullable DialogueStateMachine currentDialogue;
private @Nullable Entity interlocutor;
private @Nullable DeserializedState deserializedState;
private int resumptionAttempts = 0;
public PlayerDialogueTracker(PlayerEntity player) {
this.player = player;
}
public static PlayerDialogueTracker get(PlayerEntity player) {
return KEY.get(player);
}
public void startDialogue(Identifier id, @Nullable Entity interlocutor) throws CommandSyntaxException {
DialogueTemplate template = DialogueRegistry.getOrEmpty(id)
.orElseThrow(() -> new IllegalArgumentException("Unknown dialogue " + id));
this.startDialogue0(
id,
template,
template.start(),
interlocutor
);
}
private void startDialogue0(Identifier id, DialogueTemplate template, @Nullable String start, @Nullable Entity interlocutor) throws CommandSyntaxException {
ServerPlayerEntity serverPlayer = ((ServerPlayerEntity) this.player);
this.interlocutor = interlocutor;
try {
DialogueTemplate parsedTemplate = template.parseText(CommandDialogueAction.getSource(serverPlayer), serverPlayer);
this.currentDialogue = new DialogueStateMachine(id, parsedTemplate, start);
this.updateConditions(serverPlayer, this.currentDialogue);
this.openDialogueScreen();
} catch (CommandSyntaxException e) {
this.interlocutor = null;
throw e;
}
}
public void endDialogue() {
this.currentDialogue = null;
this.interlocutor = null;
if (this.player instanceof ServerPlayerEntity sp && this.player.currentScreenHandler instanceof DialogueScreenHandler) {
sp.closeHandledScreen();
}
}
public Optional<DialogueStateMachine> getCurrentDialogue() {
return Optional.ofNullable(this.currentDialogue);
}
public Optional<Entity> getInterlocutor() {
return Optional.ofNullable(this.interlocutor);
}
public void updateDialogue() {
DialogueStateMachine oldDialogue = this.currentDialogue;
Entity oldInterlocutor = this.interlocutor;
if (oldDialogue != null) {
this.endDialogue();
DialogueRegistry.getOrEmpty(oldDialogue.getId())
.ifPresent(template -> this.tryStartDialogue(
oldDialogue.getId(),
template,
oldDialogue.getCurrentStateKey(),
oldInterlocutor
));
}
}
@Override
public void readFromNbt(NbtCompound tag) {
if (tag.contains("current_dialogue_id", NbtElement.STRING_TYPE)) {
Identifier dialogueId = Identifier.tryParse(tag.getString("current_dialogue_id"));
if (dialogueId != null) {
Optional<DialogueTemplate> dialogueTemplate = DialogueRegistry.getOrEmpty(dialogueId);
if (dialogueTemplate.isPresent()) {
UUID interlocutorUuid = tag.containsUuid("interlocutor") ? tag.getUuid("interlocutor") : null;
String selectedState = tag.contains("current_dialogue_state", NbtElement.STRING_TYPE) ? tag.getString("current_dialogue_state") : null;
this.deserializedState = new DeserializedState(dialogueId, dialogueTemplate.get(), selectedState, interlocutorUuid);
}
}
}
}
@Override
public void writeToNbt(NbtCompound tag) {
if (this.currentDialogue != null) {
tag.putString("current_dialogue_id", this.currentDialogue.getId().toString());
tag.putString("current_dialogue_state", this.currentDialogue.getCurrentStateKey());
if (this.interlocutor != null) {
tag.putUuid("interlocutor", this.interlocutor.getUuid());
}
}
}
@Override
public void serverTick() {
DeserializedState saved = this.deserializedState;
ServerPlayerEntity serverPlayer = (ServerPlayerEntity) this.player;
if (saved != null) {
if (resumptionAttempts++ < 200) { // only try for like, 10 seconds after joining the world
Entity interlocutor;
if (saved.interlocutorUuid() != null) {
interlocutor = serverPlayer.getServerWorld().getEntity(saved.interlocutorUuid());
if (interlocutor == null) return; // no one to talk to
} else {
interlocutor = null;
}
tryStartDialogue(saved.dialogueId(), saved.template(), saved.selectedState(), interlocutor);
}
this.resumptionAttempts = 0;
this.deserializedState = null;
}
if (this.currentDialogue != null) {
if (this.player.currentScreenHandler == this.player.playerScreenHandler) {
if (this.currentDialogue.isUnskippable()) {
this.openDialogueScreen();
} else {
this.endDialogue();
return;
}
}
ChoiceAvailabilityPacket update = this.updateConditions(serverPlayer, this.currentDialogue);
if (update != null) {
ServerPlayNetworking.send(serverPlayer, update);
}
}
}
private void tryStartDialogue(Identifier id, DialogueTemplate template, String selectedState, Entity interlocutor) {
try {
this.startDialogue0(id, template, selectedState, interlocutor);
} catch (CommandSyntaxException e) {
Blabber.LOGGER.error("(Blabber) Failed to load dialogue template " + id, e);
}
}
private @Nullable ChoiceAvailabilityPacket updateConditions(ServerPlayerEntity player, DialogueStateMachine currentDialogue) {
if (currentDialogue.hasConditions()) {
return currentDialogue.updateConditions(new LootContext.Builder(
new LootContextParameterSet.Builder(player.getServerWorld())
.add(LootContextParameters.ORIGIN, player.getPos())
.addOptional(LootContextParameters.THIS_ENTITY, player)
.build(LootContextTypes.COMMAND)
).build(Optional.empty()));
}
return null;
}
private void openDialogueScreen() {
Preconditions.checkState(this.currentDialogue != null);
this.player.openHandledScreen(new DialogueScreenHandlerFactory(this.currentDialogue, Text.of("Blabber Dialogue Screen"), this.interlocutor));
}
private record DeserializedState(Identifier dialogueId, DialogueTemplate template, String selectedState, @Nullable UUID interlocutorUuid) { }
}