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

player/dialogue: Basic implementation of NPC dialogues #937

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

TwistedAsylumMC
Copy link
Member

Kind of a continuation from #666 however a lot of it is different.

Whilst this code works, we need a better method of implementing the dialogueProvider interface to allow entities such as players (often used for NPCs) to be a provider. We could just make it always implement the interface but I'm open to other ideas. There are also a few names I am not sure about, like the CanShowDialogue() method to implement this interface.

I have also decided to completely hide the idea of "opening" and "closing" buttons, they only exist in Minecraft as a way of detecting opening/closing of the menus which we can already achieve through calling SendDialogue() for opening and implementing dialogue.Closer for closing.

Example usage in combination with df-mc/npc where p is a *player.Player (modifying Player to implement dialogueProvider):

type TestDialogue struct {
	Button1 dialogue.Button
	Button2 dialogue.Button
	Button3 dialogue.Button
}

func (t TestDialogue) Submit(s dialogue.Submitter, pressed dialogue.Button) {
	fmt.Println("Pressed", pressed)
	s.CloseDialogue() // The client will not close the menu automatically
}

func (t TestDialogue) Close(s dialogue.Submitter) {
	fmt.Println("Closed")
}
settings := npc.Settings{
	Name:     "Test NPC",
	Position: p.Position(),
	Scale:    1,
	Skin:     p.Skin(),
}
n := npc.Create(settings, p.World(), nil)

d := dialogue.New(TestDialogue{
	Button1: dialogue.Button{Text: "Button 1"},
	Button2: dialogue.Button{Text: "Button 2"},
	Button3: dialogue.Button{Text: "Button 3"},
}, "Dialogue Title").
	WithBody("Welcome to my dialogue!")
p.SendDialogue(d, n)

@TwistedAsylumMC TwistedAsylumMC added the feature New feature or request label Nov 17, 2024
Copy link
Member

@Sandertv Sandertv left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I really like how this looks so far. Simple, clean, similar to existing APIs. Thumbs up for that! I've left some suggestions on how to update it to use transactions.

Additionally, I believe we discussed some things on Discord about how the entity should be shown in the dialogue, and how sending (altered) metadata before a dialogue to achieve the settings could solve it. Not sure if you wanted that in this PR too?

server/player/dialogue/dialogue.go Outdated Show resolved Hide resolved
server/player/dialogue/submit.go Outdated Show resolved Hide resolved
server/player/dialogue/submit.go Outdated Show resolved Hide resolved
@@ -161,6 +161,9 @@ func (s *Session) addSpecificMetadata(e any, m protocol.EntityMetadata) {
}
m[protocol.EntityDataKeyVisibleMobEffects] = packedEffects
}
if d, ok := e.(dialogueProvider); ok {
m[protocol.EntityDataKeyHasNPC] = boolByte(d.CanShowDialogue())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As discussed before, I think it might be a good idea to just always set this to true and remove the dialogueProvider interface, unless there's any real repercussions from that.

@Sandertv Sandertv added this to the v0.10.0 milestone Nov 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants