Skip to content

Commit

Permalink
Merge pull request #155 from Sandertv/feature/downloadfunc
Browse files Browse the repository at this point in the history
Add a function to stop packs from being downloaded
  • Loading branch information
TwistedAsylumMC committed Oct 4, 2022
2 parents 603fc82 + 657c707 commit 020513b
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
27 changes: 27 additions & 0 deletions minecraft/conn.go
Expand Up @@ -120,6 +120,12 @@ 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(id uuid.UUID, version string) bool
// ignoredResourcePacks is a slice of resource packs that are not being downloaded due to the downloadResourcePack
// func returning false for the specific pack.
ignoredResourcePacks []exemptedResourcePack

cacheEnabled bool

Expand Down Expand Up @@ -825,6 +831,14 @@ func (conn *Conn) handleResourcePacksInfo(pk *packet.ResourcePacksInfo) error {
conn.packQueue.packAmount--
continue
}
if conn.downloadResourcePack != nil && !conn.downloadResourcePack(uuid.MustParse(pack.UUID), pack.Version) {
conn.ignoredResourcePacks = append(conn.ignoredResourcePacks, exemptedResourcePack{
uuid: pack.UUID,
version: pack.Version,
})
conn.packQueue.packAmount--
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 @@ -840,6 +854,14 @@ func (conn *Conn) handleResourcePacksInfo(pk *packet.ResourcePacksInfo) error {
conn.packQueue.packAmount--
continue
}
if conn.downloadResourcePack != nil && !conn.downloadResourcePack(uuid.MustParse(pack.UUID), pack.Version) {
conn.ignoredResourcePacks = append(conn.ignoredResourcePacks, exemptedResourcePack{
uuid: pack.UUID,
version: pack.Version,
})
conn.packQueue.packAmount--
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 Down Expand Up @@ -905,6 +927,11 @@ func (conn *Conn) hasPack(uuid string, version string, hasBehaviours bool) bool
conn.packMu.Lock()
defer conn.packMu.Unlock()

for _, ignored := range conn.ignoredResourcePacks {
if ignored.uuid == uuid && ignored.version == version {
return true
}
}
for _, pack := range conn.resourcePacks {
if pack.UUID() == uuid && pack.Version() == version && pack.HasBehaviours() == hasBehaviours {
return true
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(id uuid.UUID, version string) bool

// 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 @@ -169,6 +174,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

0 comments on commit 020513b

Please sign in to comment.