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 a function to stop packs from being downloaded #155

Merged
merged 5 commits into from Oct 4, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
9 changes: 9 additions & 0 deletions minecraft/conn.go
Expand Up @@ -119,6 +119,9 @@ type Conn struct {
// be able to join the server. If they don't accept, they can only leave the server.
texturePacksRequired bool
packQueue *resourcePackQueue
// downloadResourcePack is an optional function passed to a Dial() call. If set, each resource pack received
// from the server will call this function to see if it should be downloaded or not.
downloadResourcePack func(packUUID uuid.UUID, version string) bool

cacheEnabled bool

Expand Down Expand Up @@ -820,6 +823,9 @@ func (conn *Conn) handleResourcePacksInfo(pk *packet.ResourcePacksInfo) error {
conn.packQueue.packAmount--
continue
}
if conn.downloadResourcePack != nil && !conn.downloadResourcePack(uuid.MustParse(pack.UUID), pack.Version) {
continue
}
// This UUID_Version is a hack Mojang put in place.
packsToDownload = append(packsToDownload, pack.UUID+"_"+pack.Version)
conn.packQueue.downloadingPacks[pack.UUID] = downloadingPack{
Expand All @@ -835,6 +841,9 @@ func (conn *Conn) handleResourcePacksInfo(pk *packet.ResourcePacksInfo) error {
conn.packQueue.packAmount--
continue
}
if conn.downloadResourcePack != nil && !conn.downloadResourcePack(uuid.MustParse(pack.UUID), pack.Version) {
continue
Copy link
Collaborator

Choose a reason for hiding this comment

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

Shouldn't you decrement packAmount here?

Copy link
Owner

@Sandertv Sandertv Oct 4, 2022

Choose a reason for hiding this comment

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

Hmmm yeah this seems to be an issue. I think currently this will break if some of the packs are ignored, but not all of them.

}
// This UUID_Version is a hack Mojang put in place.
packsToDownload = append(packsToDownload, pack.UUID+"_"+pack.Version)
conn.packQueue.downloadingPacks[pack.UUID] = downloadingPack{
Expand Down
6 changes: 6 additions & 0 deletions minecraft/dial.go
Expand Up @@ -54,6 +54,11 @@ type Dialer struct {
// from which the packet originated, and the destination address.
PacketFunc func(header packet.Header, payload []byte, src, dst net.Addr)

// DownloadResourcePack is called individually for every texture and behaviour pack sent by the connection when
// using Dialer.Dial(), and can be used to stop the pack from being downloaded. The function is called with the UUID
// and version of the resource pack, and the boolean returned determines if the pack will be downloaded or not.
DownloadResourcePack func(packUUID uuid.UUID, version string) bool
Copy link
Owner

Choose a reason for hiding this comment

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

Would rename packUUID as id but otherwise this is ready to go I think.


// Protocol is the Protocol version used to communicate with the target server. By default, this field is
// set to the current protocol as implemented in the minecraft/protocol package. Note that packets written
// to and read from the Conn are always any of those found in the protocol/packet package, as packets
Expand Down Expand Up @@ -159,6 +164,7 @@ func (d Dialer) DialContext(ctx context.Context, network, address string) (conn
conn.identityData = d.IdentityData
conn.clientData = d.ClientData
conn.packetFunc = d.PacketFunc
conn.downloadResourcePack = d.DownloadResourcePack
conn.cacheEnabled = d.EnableClientCache

// Disable the batch packet limit so that the server can send packets as often as it wants to.
Expand Down