Repository navigation
refactor: own shared inventory scanning in TLibs #30
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
src/main/java/net/tfminecraft/tlibs/itemscan/ItemScanHandler.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| package net.tfminecraft.tlibs.itemscan; | ||
|
|
||
| import org.bukkit.entity.Player; | ||
| import org.bukkit.inventory.Inventory; | ||
| import org.bukkit.inventory.ItemStack; | ||
|
|
||
| public interface ItemScanHandler { | ||
|
|
||
| boolean matches(ItemStack stack); | ||
|
|
||
| void update(Player player, Inventory inventory, int slot, ItemStack stack); | ||
| } |
155 changes: 155 additions & 0 deletions
155
src/main/java/net/tfminecraft/tlibs/itemscan/ItemScanService.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,155 @@ | ||
| package net.tfminecraft.tlibs.itemscan; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.concurrent.CopyOnWriteArrayList; | ||
|
|
||
| import org.bukkit.Bukkit; | ||
| import org.bukkit.Material; | ||
| import org.bukkit.block.BlockState; | ||
| import org.bukkit.block.DoubleChest; | ||
| import org.bukkit.entity.Entity; | ||
| import org.bukkit.entity.Item; | ||
| import org.bukkit.entity.Player; | ||
| import org.bukkit.event.EventHandler; | ||
| import org.bukkit.event.HandlerList; | ||
| import org.bukkit.event.Listener; | ||
| import org.bukkit.event.entity.EntityPickupItemEvent; | ||
| import org.bukkit.event.inventory.InventoryOpenEvent; | ||
| import org.bukkit.event.inventory.InventoryType; | ||
| import org.bukkit.inventory.Inventory; | ||
| import org.bukkit.inventory.InventoryHolder; | ||
| import org.bukkit.inventory.InventoryView; | ||
| import org.bukkit.inventory.ItemStack; | ||
| import org.bukkit.plugin.java.JavaPlugin; | ||
| import org.bukkit.scheduler.BukkitTask; | ||
|
|
||
| public final class ItemScanService implements Listener { | ||
|
|
||
| private static ItemScanService instance; | ||
|
|
||
| private final List<ItemScanHandler> handlers = new CopyOnWriteArrayList<>(); | ||
| private int roundRobin; | ||
| private BukkitTask task; | ||
|
|
||
| public static ItemScanService get() { | ||
| return instance; | ||
| } | ||
|
|
||
| public static void start(JavaPlugin plugin) { | ||
| if (instance != null) { | ||
| instance.stop(); | ||
| } | ||
| instance = new ItemScanService(); | ||
| plugin.getServer().getPluginManager().registerEvents(instance, plugin); | ||
| instance.task = Bukkit.getScheduler().runTaskTimer(plugin, instance::pulse, 0L, 2L); | ||
| } | ||
|
|
||
| public void stop() { | ||
| if (task != null) { | ||
| task.cancel(); | ||
| task = null; | ||
| } | ||
| HandlerList.unregisterAll(this); | ||
| handlers.clear(); | ||
| if (instance == this) { | ||
| instance = null; | ||
| } | ||
| } | ||
|
|
||
| public void subscribe(ItemScanHandler handler) { | ||
| if (handler != null && !handlers.contains(handler)) { | ||
| handlers.add(handler); | ||
| } | ||
| } | ||
|
|
||
| public void unsubscribe(ItemScanHandler handler) { | ||
| handlers.remove(handler); | ||
| } | ||
|
|
||
| private void pulse() { | ||
| if (handlers.isEmpty()) { | ||
| return; | ||
| } | ||
| List<? extends Player> players = new ArrayList<>(Bukkit.getOnlinePlayers()); | ||
| if (players.isEmpty()) { | ||
| roundRobin = 0; | ||
| return; | ||
| } | ||
| if (roundRobin >= players.size()) { | ||
| roundRobin = 0; | ||
| } | ||
| Player player = players.get(roundRobin); | ||
| roundRobin++; | ||
| scanPlayerView(player); | ||
| } | ||
|
|
||
| @EventHandler | ||
| public void onInventoryOpen(InventoryOpenEvent event) { | ||
| if (!(event.getPlayer() instanceof Player player)) { | ||
| return; | ||
| } | ||
| scanOpenView(player, event.getView()); | ||
| } | ||
|
|
||
| @EventHandler | ||
| public void onPickup(EntityPickupItemEvent event) { | ||
| if (!(event.getEntity() instanceof Player player)) { | ||
| return; | ||
| } | ||
| Item entity = event.getItem(); | ||
| ItemStack stack = entity.getItemStack(); | ||
| applyHandlers(player, null, -1, stack); | ||
| entity.setItemStack(stack); | ||
| } | ||
|
|
||
| private void scanPlayerView(Player player) { | ||
| InventoryView view = player.getOpenInventory(); | ||
| InventoryType topType = view.getTopInventory().getType(); | ||
| if (topType == InventoryType.CRAFTING || topType == InventoryType.CREATIVE) { | ||
| scanInventory(player, player.getInventory()); | ||
| return; | ||
| } | ||
| scanOpenView(player, view); | ||
| } | ||
|
|
||
| private void scanOpenView(Player player, InventoryView view) { | ||
| scanInventory(player, view.getTopInventory()); | ||
| scanInventory(player, player.getInventory()); | ||
| } | ||
|
|
||
| private void scanInventory(Player player, Inventory inventory) { | ||
| if (inventory == null || !isScannable(inventory)) { | ||
| return; | ||
| } | ||
| int size = inventory.getSize(); | ||
| for (int slot = 0; slot < size; slot++) { | ||
| scanSlot(player, inventory, slot); | ||
| } | ||
| } | ||
|
|
||
| private static boolean isScannable(Inventory inventory) { | ||
| InventoryHolder holder = inventory.getHolder(); | ||
| if (holder == null) { | ||
| return true; | ||
| } | ||
| return holder instanceof Player || holder instanceof BlockState | ||
| || holder instanceof Entity || holder instanceof DoubleChest; | ||
| } | ||
|
|
||
| private void scanSlot(Player player, Inventory inventory, int slot) { | ||
| ItemStack stack = inventory.getItem(slot); | ||
| applyHandlers(player, inventory, slot, stack); | ||
| } | ||
|
|
||
| private void applyHandlers(Player player, Inventory inventory, int slot, ItemStack stack) { | ||
| if (stack == null || stack.getType() == Material.AIR || handlers.isEmpty()) { | ||
| return; | ||
| } | ||
| for (ItemScanHandler handler : handlers) { | ||
| if (handler.matches(stack)) { | ||
| handler.update(player, inventory, slot, stack); | ||
| } | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🗄️ Data Integrity & Integration | 🟠 Major | ⚡ Quick win
🔎 Supported by static analysis
🏁 Script executed:
Repository: TF-Minecraft/TLibs
Length of output: 6556
Exclude only holderless inventories that are known plugin menus.
isScannableaccepts every inventory whoseInventoryHolderisnull. BothonInventoryOpenand the periodic scan pass such top inventories toscanInventory, so a holderless plugin menu with matching items can be modified by subscribed handlers.Do not reject all holderless inventories. The repository also uses holderless inventories as valid scan targets, and no reliable distinction between valid holderless inventories and plugin menus is established. Add an explicit, repository-supported way to identify plugin menus before excluding them.
🤖 Prompt for AI Agents