11package dev .emortal .velocity .misc .listener ;
22
3+ import com .velocitypowered .api .event .Continuation ;
34import com .velocitypowered .api .event .Subscribe ;
45import com .velocitypowered .api .event .connection .DisconnectEvent ;
56import com .velocitypowered .api .event .player .PlayerResourcePackStatusEvent ;
2122import java .util .HashSet ;
2223import java .util .Set ;
2324import java .util .UUID ;
25+ import java .util .concurrent .ConcurrentHashMap ;
2426import java .util .concurrent .TimeUnit ;
2527
2628final class ResourcePackSender {
@@ -31,6 +33,7 @@ final class ResourcePackSender {
3133 private ResourcePackInfo resourcePackInfo ;
3234
3335 private final Set <UUID > rpAcceptedPlayers = new HashSet <>();
36+ private final ConcurrentHashMap <UUID , Continuation > pendingPack = new ConcurrentHashMap <>();
3437
3538 ResourcePackSender (@ NotNull ResourcePackProvider resourcePackProvider , @ NotNull EmortalScheduler scheduler ) {
3639 this .updateResourcePackInfo (resourcePackProvider );
@@ -76,21 +79,35 @@ private byte[] fetchSha1() throws NoSuchAlgorithmException, IOException {
7679 }
7780
7881 @ Subscribe
79- void onPlayerConfiguration (PlayerConfigurationEvent event ) {
80- if (this .rpAcceptedPlayers .contains (event .player ().getUniqueId ())) return ; // Don't send the resource pack if the player has already got it
82+ void onPlayerConfiguration (PlayerConfigurationEvent event , Continuation continuation ) {
83+ if (this .rpAcceptedPlayers .contains (event .player ().getUniqueId ())) {
84+ continuation .resume ();
85+ return ; // Don't send the resource pack if the player has already got it
86+ }
8187
8288 event .player ().sendResourcePackOffer (this .resourcePackInfo );
89+ pendingPack .put (event .player ().getUniqueId (), continuation );
8390 }
8491
8592 @ Subscribe
8693 void onPlayerResourceStatus (@ NotNull PlayerResourcePackStatusEvent event ) {
8794 LOGGER .info ("Player {} resource pack status {}" , event .getPlayer ().getUsername (), event .getStatus ());
8895 Player player = event .getPlayer ();
8996 switch (event .getStatus ()) {
90- case SUCCESSFUL -> this .rpAcceptedPlayers .add (player .getUniqueId ());
91- case DECLINED -> ChatMessages .RESOURCE_PACK_DECLINED .send (player );
92- case FAILED_DOWNLOAD -> ChatMessages .RESOURCE_PACK_FAILED .send (player );
97+ case ACCEPTED -> {
98+ this .rpAcceptedPlayers .add (event .getPlayer ().getUniqueId ());
99+ this .pendingPack .get (player .getUniqueId ()).resume ();
100+ }
101+ case DECLINED -> {
102+ player .disconnect (ChatMessages .RESOURCE_PACK_DECLINED .get ());
103+ this .pendingPack .get (player .getUniqueId ()).resume ();
104+ }
105+ case FAILED_DOWNLOAD , INVALID_URL , DISCARDED -> {
106+ ChatMessages .RESOURCE_PACK_FAILED .send (player );
107+ this .pendingPack .get (player .getUniqueId ()).resume ();
108+ }
93109 }
110+ this .pendingPack .remove (player .getUniqueId ());
94111 }
95112
96113 @ Subscribe
0 commit comments