Skip to content

Commit

Permalink
fix(minecraft): Fix cancelling scheduler tasks when unregistering player
Browse files Browse the repository at this point in the history
  • Loading branch information
GeorgeV220 committed Sep 9, 2023
1 parent 9b5d648 commit 3685281
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package com.georgev22.library.minecraft.inventory;

import com.georgev22.library.maps.ObjectMap;
import com.georgev22.library.minecraft.inventory.handlers.PagedInventoryClickHandler;
import com.georgev22.library.minecraft.inventory.handlers.PagedInventoryCloseHandler;
import com.georgev22.library.minecraft.inventory.handlers.PagedInventorySwitchPageHandler;
import com.georgev22.library.minecraft.inventory.navigationitems.NavigationItem;
import com.georgev22.library.minecraft.scheduler.SchedulerTask;
import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
Expand Down Expand Up @@ -252,4 +254,18 @@ default boolean contains(Inventory inventory) {
*/
List<Inventory> getPages();

/**
* Gets the map that associates players with scheduler tasks for frames.
*
* @return A map where keys are players and values are scheduler tasks for frames.
*/
ObjectMap<Player, SchedulerTask> getPlayerSchedulerFramesMap();

/**
* Gets the map that associates players with scheduler tasks for animations.
*
* @return A map where keys are players and values are scheduler tasks for animations.
*/
ObjectMap<Player, SchedulerTask> getPlayerSchedulerAnimatedMap();

}
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ public void register(Player player, IPagedInventory pagedInventory, Inventory in
}

void unregister(Player player) {
pagedInventoryRegistrar.get(player.getUniqueId()).getPlayerSchedulerAnimatedMap().get(player).cancel();
pagedInventoryRegistrar.get(player.getUniqueId()).getPlayerSchedulerFramesMap().get(player).cancel();
registrar.remove(player.getUniqueId());
pagedInventoryRegistrar.remove(player.getUniqueId());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.georgev22.library.minecraft.inventory;

import com.georgev22.library.maps.ConcurrentObjectMap;
import com.georgev22.library.maps.ObjectMap;
import com.georgev22.library.minecraft.BukkitMinecraftUtils;
import com.georgev22.library.minecraft.colors.Animation;
Expand All @@ -14,6 +15,7 @@
import com.georgev22.library.minecraft.scheduler.MinecraftBukkitScheduler;
import com.georgev22.library.minecraft.scheduler.MinecraftFoliaScheduler;
import com.georgev22.library.minecraft.scheduler.MinecraftScheduler;
import com.georgev22.library.minecraft.scheduler.SchedulerTask;
import com.georgev22.library.utilities.Color;
import com.georgev22.library.utilities.KryoUtils;
import com.georgev22.library.utilities.Utils;
Expand Down Expand Up @@ -42,6 +44,9 @@ public class PagedInventory implements IPagedInventory {

private final MinecraftScheduler minecraftScheduler;

private final ObjectMap<Player, SchedulerTask> playerSchedulerFramesMap;
private final ObjectMap<Player, SchedulerTask> playerSchedulerAnimatedMap;

private boolean kryo = false;

protected PagedInventory(InventoryRegistrar registrar, NavigationRow navigationRow) {
Expand All @@ -52,6 +57,8 @@ protected PagedInventory(InventoryRegistrar registrar, NavigationRow navigationR
this.switchHandlers = new ArrayList<>(3);
this.navigationRow = navigationRow;
this.minecraftScheduler = BukkitMinecraftUtils.isFolia() ? new MinecraftFoliaScheduler() : new MinecraftBukkitScheduler();
this.playerSchedulerFramesMap = new ConcurrentObjectMap<>();
this.playerSchedulerAnimatedMap = new ConcurrentObjectMap<>();
}

@Deprecated
Expand Down Expand Up @@ -237,7 +244,7 @@ public boolean open(Player player, int index, boolean animated) {
slotFrame.append(slot, 0);
}
}
minecraftScheduler.createRepeatingTask(registrar.getPlugin(), () -> {
playerSchedulerFramesMap.append(player, minecraftScheduler.createRepeatingTask(registrar.getPlugin(), () -> {
if (player.getOpenInventory().getTopInventory() != null && player.getOpenInventory().getTopInventory().equals(openInventory)) {
for (int i = 0; i < openInventory.getSize(); ++i) {

Expand Down Expand Up @@ -266,9 +273,9 @@ public boolean open(Player player, int index, boolean animated) {
}
}
}
}, 1L, 20L);
}, 1L, 20L));
if (animated) {
minecraftScheduler.createRepeatingTask(registrar.getPlugin(), () -> {
playerSchedulerAnimatedMap.append(player, minecraftScheduler.createRepeatingTask(registrar.getPlugin(), () -> {
if (player.getOpenInventory().getTopInventory() != null && player.getOpenInventory().getTopInventory().equals(openInventory)) {
for (int i = 0; i < openInventory.getSize(); i++) {
ItemStack itemStack = openInventory.getItem(i);
Expand Down Expand Up @@ -309,7 +316,7 @@ public boolean open(Player player, int index, boolean animated) {

player.updateInventory();
}
}, 1L, 1L);
}, 1L, 1L));
}
return true;
}
Expand Down Expand Up @@ -643,4 +650,20 @@ private static NavigationRow getFromMap(Map<Integer, NavigationItem> navigation)

return new NavigationRow(nextItem, previousItem, closeItem, navigationItems.toArray(new NavigationItem[0]));
}

/**
* {@inheritDoc}
*/
@Override
public ObjectMap<Player, SchedulerTask> getPlayerSchedulerFramesMap() {
return playerSchedulerFramesMap;
}

/**
* {@inheritDoc}
*/
@Override
public ObjectMap<Player, SchedulerTask> getPlayerSchedulerAnimatedMap() {
return playerSchedulerAnimatedMap;
}
}

0 comments on commit 3685281

Please sign in to comment.