1313import fr .maxlego08 .menu .api .utils .CompatibilityUtil ;
1414import fr .maxlego08 .menu .hooks .packetevents .animation .PacketPlayerTitleAnimation ;
1515import org .bukkit .Bukkit ;
16+ import org .bukkit .Location ;
1617import org .bukkit .entity .Player ;
1718import org .bukkit .inventory .Inventory ;
1819import org .bukkit .inventory .InventoryHolder ;
20+ import org .jetbrains .annotations .NotNull ;
1921
2022import java .util .Arrays ;
2123import java .util .HashMap ;
@@ -55,107 +57,131 @@ public void setWindowId(int windowId) {
5557 }
5658 }
5759
60+ private void runInventoryTask (@ NotNull Player player ,@ NotNull Inventory inventory ,@ NotNull Runnable task ) {
61+ if (Bukkit .isPrimaryThread ()) {
62+ task .run ();
63+ return ;
64+ }
65+
66+ if (!this .plugin .isEnabled ()) {
67+ return ;
68+ }
69+
70+ Location location = this .resolveSafeLocation (inventory );
71+
72+ if (location != null ) {
73+ this .plugin .getScheduler ().runAtLocation (location , w -> task .run ());
74+ } else {
75+ this .plugin .getScheduler ().runAtEntity (player , w -> task .run ());
76+ }
77+ }
78+
79+ private Location resolveSafeLocation (@ NotNull Inventory inventory ) {
80+ try {
81+ Location location = inventory .getLocation ();
82+ if (location != null && location .getWorld () != null ) {
83+ return location ;
84+ }
85+ } catch (Exception ignored ) {
86+ }
87+ return null ;
88+ }
89+
5890 @ Override
5991 public void onPacketSend (PacketSendEvent event ) {
6092 PacketTypeCommon packetType = event .getPacketType ();
61- if (packetType == PacketType .Play .Server .OPEN_WINDOW ){
62- WrapperPlayServerOpenWindow wrapper = new WrapperPlayServerOpenWindow (event );
63- int containerId = wrapper .getContainerId ();
64- Player player = event .getPlayer ();
65- if (player == null ) return ;
66- UUID playerUniqueId = player .getUniqueId ();
67-
68- PlayerAnimationData data = this .playerAnimationData .get (playerUniqueId );
69- if (data != null && data .getContainerId () == containerId ){
70- return ;
71- }
72- Inventory topInventory = CompatibilityUtil .getTopInventory (player );
73- if (topInventory == null ) {
74- return ;
75- }
93+ switch (packetType ) {
94+ case PacketType .Play .Server .OPEN_WINDOW -> {
95+ WrapperPlayServerOpenWindow wrapper = new WrapperPlayServerOpenWindow (event );
96+ int containerId = wrapper .getContainerId ();
97+ Player player = event .getPlayer ();
98+ if (player == null ) return ;
99+ UUID playerUniqueId = player .getUniqueId ();
100+
101+ PlayerAnimationData data = this .playerAnimationData .get (playerUniqueId );
102+ if (data != null && data .getContainerId () == containerId ) {
103+ return ;
104+ }
105+ Inventory topInventory = CompatibilityUtil .getTopInventory (player );
106+ if (topInventory == null ) {
107+ return ;
108+ }
76109
77- Runnable task = () -> {
78- InventoryHolder holder = topInventory .getHolder ();
79- if (holder instanceof BaseInventory baseInventory ){
80- TitleAnimation titleAnimation = baseInventory .getTitleAnimation ();
81- if (titleAnimation == null ) {
82- return ;
83- }
84- Inventory inventory = baseInventory .getInventory ();
85- PlayerTitleAnimation playerTitleAnimation = titleAnimation .playTitleAnimation (this .plugin , containerId , inventory .getType (), inventory .getSize (), wrapper );
86- if (playerTitleAnimation != null ){
87- playerTitleAnimation .start (player , Arrays .asList (inventory .getContents ()));
110+ Runnable task = () -> {
111+ InventoryHolder holder = topInventory .getHolder ();
112+ if (holder instanceof BaseInventory baseInventory ) {
113+ TitleAnimation titleAnimation = baseInventory .getTitleAnimation ();
114+ if (titleAnimation == null ) {
115+ return ;
116+ }
117+ Inventory inventory = baseInventory .getInventory ();
118+ PlayerTitleAnimation playerTitleAnimation = titleAnimation .playTitleAnimation (this .plugin , containerId , inventory .getType (), inventory .getSize (), wrapper );
119+ if (playerTitleAnimation != null ) {
120+ playerTitleAnimation .start (player , Arrays .asList (inventory .getContents ()));
121+ }
122+ baseInventory .setPlayerTitleAnimation (playerTitleAnimation );
123+ this .playerAnimationData .put (playerUniqueId , new PlayerAnimationData (containerId ));
88124 }
89- baseInventory .setPlayerTitleAnimation (playerTitleAnimation );
90- this .playerAnimationData .put (playerUniqueId , new PlayerAnimationData (containerId ));
91- }
92- };
125+ };
93126
94- if (Bukkit .isPrimaryThread ()) {
95- task .run ();
96- } else if (this .plugin .isEnabled ()) {
97- this .plugin .getScheduler ().runAtEntity (player , w -> task .run ());
98- }
99- } else if (packetType == PacketType .Play .Server .CLOSE_WINDOW ){
100- Player player = event .getPlayer ();
101- if (player == null ) return ;
102- Inventory topInventory = CompatibilityUtil .getTopInventory (player );
103- if (topInventory == null ) {
104- return ;
127+ this .runInventoryTask (player , topInventory , task );
105128 }
106-
107- Runnable task = () -> {
108- InventoryHolder holder = topInventory . getHolder () ;
109- if ( holder instanceof BaseInventory ){
110- UUID playerUniqueId = player . getUniqueId ();
111- this . playerAnimationData . remove ( playerUniqueId ) ;
129+ case PacketType . Play . Server . CLOSE_WINDOW -> {
130+ Player player = event . getPlayer ();
131+ if ( player == null ) return ;
132+ Inventory topInventory = CompatibilityUtil . getTopInventory ( player );
133+ if ( topInventory == null ) {
134+ return ;
112135 }
113- };
114136
115- if (Bukkit .isPrimaryThread ()) {
116- task .run ();
117- } else if (this .plugin .isEnabled ()) {
118- this .plugin .getScheduler ().runAtEntity (player , w -> task .run ());
137+ Runnable task = () -> {
138+ InventoryHolder holder = topInventory .getHolder ();
139+ if (holder instanceof BaseInventory ) {
140+ UUID playerUniqueId = player .getUniqueId ();
141+ this .playerAnimationData .remove (playerUniqueId );
142+ }
143+ };
144+
145+ this .runInventoryTask (player , topInventory , task );
119146 }
120- } else if ( packetType == PacketType .Play .Server .WINDOW_ITEMS ) {
121- WrapperPlayServerWindowItems wrapper = new WrapperPlayServerWindowItems (event );
122- Player player = event .getPlayer ();
123- if (player == null ) return ;
124- UUID playerUniqueId = player .getUniqueId ();
147+ case PacketType .Play .Server .WINDOW_ITEMS -> {
148+ WrapperPlayServerWindowItems wrapper = new WrapperPlayServerWindowItems (event );
149+ Player player = event .getPlayer ();
150+ if (player == null ) return ;
151+ UUID playerUniqueId = player .getUniqueId ();
125152
126- int windowId = wrapper .getWindowId ();
153+ int windowId = wrapper .getWindowId ();
127154
128- PlayerAnimationData data = this .playerAnimationData .get (playerUniqueId );
129- if (data != null && data .getWindowId () == windowId ){
130- return ;
131- }
155+ PlayerAnimationData data = this .playerAnimationData .get (playerUniqueId );
156+ if (data != null && data .getWindowId () == windowId ) {
157+ return ;
158+ }
132159
133- if (data == null ) {
134- data = new PlayerAnimationData (0 );
135- this .playerAnimationData .put (playerUniqueId , data );
136- }
137- data .setWindowId (windowId );
160+ if (data == null ) {
161+ data = new PlayerAnimationData (0 );
162+ this .playerAnimationData .put (playerUniqueId , data );
163+ }
164+ data .setWindowId (windowId );
138165
139- Inventory topInventory = CompatibilityUtil .getTopInventory (player );
140- if (topInventory == null ) {
141- return ;
142- }
166+ Inventory topInventory = CompatibilityUtil .getTopInventory (player );
167+ if (topInventory == null ) {
168+ return ;
169+ }
143170
144- Runnable task = () -> {
145- InventoryHolder holder = topInventory .getHolder ();
146- if (holder instanceof BaseInventory baseInventory && baseInventory .getPlayerTitleAnimation () instanceof PacketPlayerTitleAnimation playerTitleAnimation ){
147- playerTitleAnimation .setWrapperPlayServerWindowItems (wrapper );
148- if (playerTitleAnimation .getSettings ().showItemsAfterAnimation ()){
149- event .setCancelled (true );
171+ Runnable task = () -> {
172+ InventoryHolder holder = topInventory .getHolder ();
173+ if (holder instanceof BaseInventory baseInventory && baseInventory .getPlayerTitleAnimation () instanceof PacketPlayerTitleAnimation playerTitleAnimation ) {
174+ playerTitleAnimation .setWrapperPlayServerWindowItems (wrapper );
175+ if (playerTitleAnimation .getSettings ().showItemsAfterAnimation ()) {
176+ event .setCancelled (true );
177+ }
150178 }
151- }
152- };
179+ };
153180
154- if (Bukkit .isPrimaryThread ()) {
155- task .run ();
156- } else if (this .plugin .isEnabled ()) {
157- this .plugin .getScheduler ().runAtEntity (player , w -> task .run ());
181+ this .runInventoryTask (player , topInventory , task );
182+ }
183+ default -> {
158184 }
159185 }
160186 }
161- }
187+ }
0 commit comments