Repository navigation
fix: let written books be copied despite MMOItems #53
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
4 commits
Select commit
Hold shift + click to select a range
165972d
fix: let written books be copied despite MMOItems
XxFran10xX 28a97e6
fix: only undo MMOItems' own book copy cancel
XxFran10xX acaf23a
fix: make the MMOItems wrap reload-safe and catch a late MMOItems
XxFran10xX 4c57105
fix: keep MMOItems' handler in its place when wrapping
XxFran10xX 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
137 changes: 137 additions & 0 deletions
137
src/main/java/net/tfminecraft/tfmccore/books/BookCopyUnblocker.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,137 @@ | ||
| package net.tfminecraft.tfmccore.books; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.Arrays; | ||
| import java.util.List; | ||
|
|
||
| import org.bukkit.Keyed; | ||
| import org.bukkit.NamespacedKey; | ||
| import org.bukkit.event.Cancellable; | ||
| import org.bukkit.event.Event; | ||
| import org.bukkit.event.EventHandler; | ||
| import org.bukkit.event.HandlerList; | ||
| import org.bukkit.event.Listener; | ||
| import org.bukkit.event.inventory.CraftItemEvent; | ||
| import org.bukkit.event.inventory.InventoryClickEvent; | ||
| import org.bukkit.event.server.PluginEnableEvent; | ||
| import org.bukkit.plugin.EventExecutor; | ||
| import org.bukkit.plugin.RegisteredListener; | ||
| import org.bukkit.plugin.java.JavaPlugin; | ||
|
|
||
| import net.tfminecraft.tfmccore.cache.Cache; | ||
|
|
||
| /** | ||
| * Lets players copy written books at a crafting table. MIReplacer makes every Book and | ||
| * Quill an MMOItem with disable-crafting, and MMOItems cancels any vanilla recipe that | ||
| * uses one, so the copy showed but could not be taken. MMOItems' own click handlers are | ||
| * wrapped so that only a book copy cancel made by that handler call is undone; cancels | ||
| * from every other plugin, at any priority, stay. | ||
| */ | ||
| public final class BookCopyUnblocker implements Listener { | ||
|
|
||
| static final NamespacedKey BOOK_CLONING = NamespacedKey.minecraft("book_cloning"); | ||
| static final String MMOITEMS_LISTENER = "net.Indyuce.mmoitems.listener.DisableInteractions"; | ||
|
|
||
| private final JavaPlugin plugin; | ||
| private final HandlerList handlers; | ||
| private final List<Wrapped> wrapped = new ArrayList<>(); | ||
|
|
||
| BookCopyUnblocker(JavaPlugin plugin, HandlerList handlers) { | ||
| this.plugin = plugin; | ||
| this.handlers = handlers; | ||
| } | ||
|
|
||
| public static BookCopyUnblocker register(JavaPlugin plugin) { | ||
| BookCopyUnblocker unblocker = new BookCopyUnblocker(plugin, InventoryClickEvent.getHandlerList()); | ||
| plugin.getServer().getPluginManager().registerEvents(unblocker, plugin); | ||
| // MMOItems is not a dependency: it registers its handlers in its own onEnable, before or after ours | ||
| if (plugin.getServer().getPluginManager().isPluginEnabled("MMOItems")) unblocker.wrapAndLog(); | ||
| return unblocker; | ||
| } | ||
|
|
||
| @EventHandler | ||
| public void onPluginEnable(PluginEnableEvent event) { | ||
| if (event.getPlugin().getName().equals("MMOItems")) wrapAndLog(); | ||
| } | ||
|
|
||
| void wrapAndLog() { | ||
| int count = wrap(); | ||
| if (wrapped.isEmpty()) { | ||
| plugin.getLogger().warning("MMOItems craft handler not found; book copies stay blocked"); | ||
| } else if (count > 0) { | ||
| plugin.getLogger().info("Book copies: wrapped " + count + " MMOItems click handlers"); | ||
| } | ||
| } | ||
|
|
||
| /** Replaces each unwrapped MMOItems DisableInteractions registration with one that undoes its book copy cancels. */ | ||
| int wrap() { | ||
| int count = 0; | ||
| for (RegisteredListener original : handlers.getRegisteredListeners()) { | ||
| if (original instanceof Wrapped | ||
| || !original.getListener().getClass().getName().equals(MMOITEMS_LISTENER)) continue; | ||
| Wrapped wrapper = new Wrapped(original); | ||
| replace(original, wrapper); | ||
| wrapped.add(wrapper); | ||
| count++; | ||
| } | ||
| return count; | ||
| } | ||
|
|
||
| /** | ||
| * Swaps one registration for another in the same place. HandlerList only appends, so the | ||
| * listeners after it in its priority slot are taken out and added back behind the replacement. | ||
| */ | ||
| private void replace(RegisteredListener old, RegisteredListener replacement) { | ||
| List<RegisteredListener> later = new ArrayList<>(); | ||
| boolean found = false; | ||
| for (RegisteredListener registered : handlers.getRegisteredListeners()) { | ||
| if (registered == old) { | ||
| found = true; | ||
| } else if (found && registered.getPriority() == old.getPriority()) { | ||
| later.add(registered); | ||
| } | ||
| } | ||
| handlers.unregister(old); | ||
| later.forEach(handlers::unregister); | ||
| handlers.register(replacement); | ||
| later.forEach(handlers::register); | ||
| } | ||
|
|
||
| /** Puts MMOItems' own registrations back, so a TFMCCore disable or reload leaves nothing of ours behind. */ | ||
| public void unwrap() { | ||
| List<RegisteredListener> current = Arrays.asList(handlers.getRegisteredListeners()); | ||
| for (Wrapped wrapper : wrapped) { | ||
| // Gone when MMOItems was disabled meanwhile; its original must not come back then | ||
| if (!current.contains(wrapper)) continue; | ||
| replace(wrapper, wrapper.original); | ||
| } | ||
| wrapped.clear(); | ||
| } | ||
|
|
||
| static final class Wrapped extends RegisteredListener { | ||
| final RegisteredListener original; | ||
|
|
||
| Wrapped(RegisteredListener original) { | ||
| super(original.getListener(), executor(original), original.getPriority(), | ||
| original.getPlugin(), original.isIgnoringCancelled()); | ||
| this.original = original; | ||
| } | ||
| } | ||
|
|
||
| static EventExecutor executor(RegisteredListener original) { | ||
| return (listener, event) -> { | ||
| boolean copy = Cache.allowBookCopies && isBookCopy(event); | ||
| boolean cancelledBefore = copy && ((Cancellable) event).isCancelled(); | ||
| original.callEvent(event); | ||
| if (copy && !cancelledBefore && ((Cancellable) event).isCancelled()) { | ||
| ((Cancellable) event).setCancelled(false); | ||
| } | ||
| }; | ||
| } | ||
|
|
||
| private static boolean isBookCopy(Event event) { | ||
| return event instanceof CraftItemEvent craft | ||
| && craft.getRecipe() instanceof Keyed keyed | ||
| && BOOK_CLONING.equals(keyed.getKey()); | ||
| } | ||
| } | ||
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
6 changes: 6 additions & 0 deletions
6
src/test/java/net/Indyuce/mmoitems/listener/DisableInteractions.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,6 @@ | ||
| package net.Indyuce.mmoitems.listener; | ||
|
|
||
| import org.bukkit.event.Listener; | ||
|
|
||
| /** Stands in for MMOItems' listener, which BookCopyUnblocker finds by class name. */ | ||
| public class DisableInteractions implements Listener {} |
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.
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.
Uh oh!
There was an error while loading. Please reload this page.