From 4109a24bd489496ed01c8768c7c57cd0d25fa3d2 Mon Sep 17 00:00:00 2001 From: mfnalex <1122571+mfnalex@users.noreply.github.com> Date: Wed, 7 Jul 2021 09:19:30 +0200 Subject: [PATCH] Updated ChestSortAPI to 11.0.0-SNAPSHOT --- CHANGELOG.md | 5 + CODE_OF_CONDUCT.md | 46 ----- HOW_TO_USE_API.md | 24 +-- pom.xml | 18 +- .../chestsort/api/ChestSortAPI.java | 29 +++ .../chestsort/api/ChestSortEvent.java | 166 ++++++++++++++++++ .../jeff_media/chestsort/api/ISortable.java | 9 + .../de/jeff_media/chestsort/api/Sortable.java | 44 +++++ .../handlers/ChestSortOrganizer.java | 17 +- .../chestsort/listeners/Listener.java | 13 +- 10 files changed, 288 insertions(+), 83 deletions(-) delete mode 100644 CODE_OF_CONDUCT.md create mode 100644 src/main/java/de/jeff_media/chestsort/api/ChestSortAPI.java create mode 100644 src/main/java/de/jeff_media/chestsort/api/ChestSortEvent.java create mode 100644 src/main/java/de/jeff_media/chestsort/api/ISortable.java create mode 100644 src/main/java/de/jeff_media/chestsort/api/Sortable.java diff --git a/CHANGELOG.md b/CHANGELOG.md index 5abe447..ef2062f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ # Changelog +## 11.0.0 +- Enhanced API +- Added Chinese (Simplified) translation +- Updated Dutch translation + ## 10.3.2 - Updated Hungarian translation diff --git a/CODE_OF_CONDUCT.md b/CODE_OF_CONDUCT.md deleted file mode 100644 index fafcb8d..0000000 --- a/CODE_OF_CONDUCT.md +++ /dev/null @@ -1,46 +0,0 @@ -# Contributor Covenant Code of Conduct - -## Our Pledge - -In the interest of fostering an open and welcoming environment, we as contributors and maintainers pledge to making participation in our project and our community a harassment-free experience for everyone, regardless of age, body size, disability, ethnicity, gender identity and expression, level of experience, nationality, personal appearance, race, religion, or sexual identity and orientation. - -## Our Standards - -Examples of behavior that contributes to creating a positive environment include: - -* Using welcoming and inclusive language -* Being respectful of differing viewpoints and experiences -* Gracefully accepting constructive criticism -* Focusing on what is best for the community -* Showing empathy towards other community members - -Examples of unacceptable behavior by participants include: - -* The use of sexualized language or imagery and unwelcome sexual attention or advances -* Trolling, insulting/derogatory comments, and personal or political attacks -* Public or private harassment -* Publishing others' private information, such as a physical or electronic address, without explicit permission -* Other conduct which could reasonably be considered inappropriate in a professional setting - -## Our Responsibilities - -Project maintainers are responsible for clarifying the standards of acceptable behavior and are expected to take appropriate and fair corrective action in response to any instances of unacceptable behavior. - -Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, or to ban temporarily or permanently any contributor for other behaviors that they deem inappropriate, threatening, offensive, or harmful. - -## Scope - -This Code of Conduct applies both within project spaces and in public spaces when an individual is representing the project or its community. Examples of representing a project or community include using an official project e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event. Representation of a project may be further defined and clarified by project maintainers. - -## Enforcement - -Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by contacting the project team at mail@jeff-media.de. The project team will review and investigate all complaints, and will respond in a way that it deems appropriate to the circumstances. The project team is obligated to maintain confidentiality with regard to the reporter of an incident. Further details of specific enforcement policies may be posted separately. - -Project maintainers who do not follow or enforce the Code of Conduct in good faith may face temporary or permanent repercussions as determined by other members of the project's leadership. - -## Attribution - -This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, available at [http://contributor-covenant.org/version/1/4][version] - -[homepage]: http://contributor-covenant.org -[version]: http://contributor-covenant.org/version/1/4/ diff --git a/HOW_TO_USE_API.md b/HOW_TO_USE_API.md index e00605c..b7578ac 100644 --- a/HOW_TO_USE_API.md +++ b/HOW_TO_USE_API.md @@ -23,34 +23,12 @@ You can use maven to add ChestSort as a dependency to your Spigot-/Bukkit-Plugin de.jeff_media ChestSortAPI - 4.0.1 + 11.0.0-SNAPSHOT compile ``` -If you use the `Sortable`class or the `ISortable` interface, you must also shade the ChestSortAPI into your plugin: - -```xml - - - - org.apache.maven.plugins - maven-shade-plugin - 3.1.0 - - - package - - shade - - - - - - -``` - ## Accessing the API Then you can access the API via the plugin manager: diff --git a/pom.xml b/pom.xml index fa1d2a9..123f0ac 100644 --- a/pom.xml +++ b/pom.xml @@ -9,7 +9,7 @@ ChestSort https://www.chestsort.de Allows automatic chest sorting! - 10.3.2 + 11.0.0-SNAPSHOT jar @@ -91,8 +91,17 @@ org.jetbrains:* org.intellij.lang:* org.slf4j + de.jeff_media.chestsortapi:de.jeff_media.chestsortapi.ChestSortAPI + + + *:* + + de/jeff_media/chestsortapi/** + + + @@ -168,13 +177,6 @@ provided - - de.jeff_media - ChestSortAPI - 4.0.1 - compile - - de.jeff_media SpigotUpdateChecker diff --git a/src/main/java/de/jeff_media/chestsort/api/ChestSortAPI.java b/src/main/java/de/jeff_media/chestsort/api/ChestSortAPI.java new file mode 100644 index 0000000..ae5d215 --- /dev/null +++ b/src/main/java/de/jeff_media/chestsort/api/ChestSortAPI.java @@ -0,0 +1,29 @@ +package de.jeff_media.chestsort.api; + +import de.jeff_media.chestsort.ChestSortPlugin; +import org.bukkit.entity.Player; +import org.bukkit.inventory.Inventory; + +public class ChestSortAPI { + + public static void sortInventory(Inventory inventory) { + ChestSortPlugin.getInstance().getOrganizer().sortInventory(inventory); + } + + public static void sortInventory(Inventory inventory, int startSlot, int endSlot) { + ChestSortPlugin.getInstance().getOrganizer().sortInventory(inventory, startSlot, endSlot); + } + + public static boolean hasSortingEnabled(Player player) { + return ChestSortPlugin.getInstance().isSortingEnabled(player); + } + + public static void setSortable(Inventory inv) { + ChestSortPlugin.getInstance().getOrganizer().setSortable(inv); + } + + public static void setUnsortable(Inventory inv) { + ChestSortPlugin.getInstance().getOrganizer().setUnsortable(inv); + } + +} diff --git a/src/main/java/de/jeff_media/chestsort/api/ChestSortEvent.java b/src/main/java/de/jeff_media/chestsort/api/ChestSortEvent.java new file mode 100644 index 0000000..1b838aa --- /dev/null +++ b/src/main/java/de/jeff_media/chestsort/api/ChestSortEvent.java @@ -0,0 +1,166 @@ +package de.jeff_media.chestsort.api; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + +import org.bukkit.Location; +import org.bukkit.entity.HumanEntity; +import org.bukkit.event.Cancellable; +import org.bukkit.event.Event; +import org.bukkit.event.HandlerList; +import org.bukkit.inventory.Inventory; +import org.bukkit.inventory.ItemStack; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +/** + * This event is called whenever ChestSort attempts to sort an inventory. Can be cancelled to prevent ChestSort from manipulating this inventory. + */ +public class ChestSortEvent extends Event implements Cancellable { + + private static final HandlerList HANDLERS = new HandlerList(); + final Inventory inv; + // For each ItemStack, a map of "{placeholder}", "sortString" pairs. + Map> invSortableMaps; + boolean cancelled = false; + Location loc; + HumanEntity p; + + public List getUnmovableItemStacks() { + return unmovableItemStacks; + } + + public List getUnmovableSlots() { + return unmovableSlots; + } + + List unmovableItemStacks; + List unmovableSlots; + + public ChestSortEvent(Inventory inv) { + this.inv = inv; + this.unmovableItemStacks = new ArrayList<>(); + this.unmovableSlots = new ArrayList<>(); + } + + public static HandlerList getHandlerList() { + return HANDLERS; + } + + /** + * Returns the location associated with this event. Might be null + * @return Location associated with this event, or null if no location has been set + */ + @Nullable + public Location getLocation() { + return loc; + } + + /** + * Sets the location associated with this event + * @param loc + */ + public void setLocation(Location loc) { this.loc=loc; } + + /** + * Returns the inventory associated with this event + * @return Inventory to be sorted + */ + public Inventory getInventory() { + return inv; + } + + public Map> getSortableMaps() { + return invSortableMaps; + } + + public void setSortableMaps(Map> sortableMap) { + invSortableMaps = sortableMap; + } + + /** + * Returns the player associated with this event. Might be null + * @return Player associated with this event, or null if no player has been set + */ + @Nullable + public HumanEntity getPlayer() { + return p; + } + + /** + * Sets the player associated with this event + * @param p Player associated with this event, can be null + */ + public void setPlayer(@Nullable HumanEntity p) { this.p=p; } + + /** + * Prevents ChestSort from sorting/moving this specific slot + * @param slot + */ + public void setUnmovable(int slot) { + unmovableSlots.add(slot); + } + + /** + * Prevents ChestSort from sorting/moving matching ItemStacks + * @param itemStack + */ + public void setUnmovable(ItemStack itemStack) { + unmovableItemStacks.add(itemStack); + } + + /** + * Removes a slot number from the list of unmovable slots + * @param slot + */ + public void removeUnmovable(int slot) { + unmovableSlots.remove(slot); + } + + /** + * Removes an ItemStack from the list of unmovable ItemStacks + * @param itemStack + */ + public void removeUnmovable(ItemStack itemStack) { + unmovableItemStacks.remove(itemStack); + } + + /** + * Checks whether a slot number is set as unmovable + * @param slot + * @return true if the slot number has been set unmovable, otherwise false + */ + public boolean isUnmovable(int slot) { + return unmovableSlots.contains(slot); + } + + /** + * Checks whether an ItemStack is set as unmovable + * @param itemStack + * @return true if the ItemStack has been set unmovable, otherwise false + */ + public boolean isUnmovable(ItemStack itemStack) { + return unmovableItemStacks.contains(itemStack); + } + + public @NotNull HandlerList getHandlers() { + return HANDLERS; + } + + /** + * Checks whether this event is cancelled. If true, the Inventory will not be sorted + * @return true when the event has been cancelled, otherwise false + */ + @Override + public boolean isCancelled() { + return cancelled; + } + + @Override + public void setCancelled(boolean cancel) { + cancelled = cancel; + } + + +} diff --git a/src/main/java/de/jeff_media/chestsort/api/ISortable.java b/src/main/java/de/jeff_media/chestsort/api/ISortable.java new file mode 100644 index 0000000..887a1a6 --- /dev/null +++ b/src/main/java/de/jeff_media/chestsort/api/ISortable.java @@ -0,0 +1,9 @@ +package de.jeff_media.chestsort.api; + +import org.bukkit.inventory.InventoryHolder; + +/** + * Public interface that can be used as InventoryHolder to tell ChestSort that the associated inventory is sortable + */ +public interface ISortable extends InventoryHolder { +} diff --git a/src/main/java/de/jeff_media/chestsort/api/Sortable.java b/src/main/java/de/jeff_media/chestsort/api/Sortable.java new file mode 100644 index 0000000..ae01547 --- /dev/null +++ b/src/main/java/de/jeff_media/chestsort/api/Sortable.java @@ -0,0 +1,44 @@ +package de.jeff_media.chestsort.api; + +import org.bukkit.inventory.Inventory; +import org.bukkit.inventory.InventoryHolder; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +/** + * Public class that can be used as InventoryHolder to tell ChestSort that the associated inventory is sortable. + */ +public class Sortable implements ISortable { + private Inventory inv; + private InventoryHolder h = null; + + public Sortable() { + + } + + public Sortable(InventoryHolder h) { + this.h=h; + } + + public void setHolder(@NotNull InventoryHolder player) { + this.h=h; + } + + public void removeHolder() { + this.h=null; + } + + @Nullable + public InventoryHolder getHolder() { + return h; + } + + @Override + public Inventory getInventory() { + return inv; + } + + public void setInventory(Inventory inv) { + this.inv=inv; + } +} diff --git a/src/main/java/de/jeff_media/chestsort/handlers/ChestSortOrganizer.java b/src/main/java/de/jeff_media/chestsort/handlers/ChestSortOrganizer.java index b61bc98..00d6fe4 100644 --- a/src/main/java/de/jeff_media/chestsort/handlers/ChestSortOrganizer.java +++ b/src/main/java/de/jeff_media/chestsort/handlers/ChestSortOrganizer.java @@ -1,6 +1,6 @@ package de.jeff_media.chestsort.handlers; -import de.jeff_media.chestsort.ChestSortEvent; +import de.jeff_media.chestsort.api.ChestSortEvent; import de.jeff_media.chestsort.ChestSortPlugin; import de.jeff_media.chestsort.data.Category; import de.jeff_media.chestsort.hooks.CrackShotHook; @@ -59,6 +59,8 @@ public class ChestSortOrganizer { // We store a list of all Category objects public final ArrayList categories = new ArrayList<>(); final ArrayList stickyCategoryNames = new ArrayList<>(); + private final HashSet sortableInventories = new HashSet<>(); + private final HashSet unsortableInventories = new HashSet<>(); public ChestSortOrganizer(ChestSortPlugin plugin) { this.plugin = plugin; @@ -149,6 +151,18 @@ public class ChestSortOrganizer { } } + public void setSortable(Inventory inv) { + sortableInventories.add(inv); + } + + public void setUnsortable(Inventory inv) { + unsortableInventories.add(inv); + } + + public boolean isMarkedAsSortable(Inventory inv) { + return sortableInventories.contains(inv); + } + static int getNumberOfEnchantments(ItemStack is) { int totalEnchants = 0; @@ -464,6 +478,7 @@ public class ChestSortOrganizer { // Sort an inventory only between startSlot and endSlot public void sortInventory(@NotNull Inventory inv, int startSlot, int endSlot) { if(inv==null) return; + if(unsortableInventories.contains(inv)) return; plugin.debug("Attempting to sort an Inventory and calling ChestSortEvent."); Class invClass = inv.getClass(); ChestSortEvent chestSortEvent = new ChestSortEvent(inv); diff --git a/src/main/java/de/jeff_media/chestsort/listeners/Listener.java b/src/main/java/de/jeff_media/chestsort/listeners/Listener.java index e6b9563..5fab6b0 100644 --- a/src/main/java/de/jeff_media/chestsort/listeners/Listener.java +++ b/src/main/java/de/jeff_media/chestsort/listeners/Listener.java @@ -1,11 +1,11 @@ package de.jeff_media.chestsort.listeners; -import de.jeff_media.chestsort.ChestSortEvent; +import de.jeff_media.chestsort.api.ChestSortEvent; import de.jeff_media.chestsort.config.Messages; import de.jeff_media.chestsort.enums.Hotkey; import de.jeff_media.chestsort.handlers.Logger; import de.jeff_media.chestsort.ChestSortPlugin; -import de.jeff_media.chestsort.ISortable; +import de.jeff_media.chestsort.api.*; import de.jeff_media.chestsort.data.PlayerSetting; import de.jeff_media.chestsort.hooks.*; import de.jeff_media.chestsort.utils.LlamaUtils; @@ -186,7 +186,8 @@ public class Listener implements org.bukkit.event.Listener { if (!isAPICall(inventory) && !belongsToChestLikeBlock(inventory) && !plugin.getEnderContainersHook().isEnderchest(inventory) - && !LlamaUtils.belongsToLlama(inventory)) { + && !LlamaUtils.belongsToLlama(inventory) + && !plugin.getOrganizer().isMarkedAsSortable(inventory)) { return; } @@ -236,7 +237,8 @@ public class Listener implements org.bukkit.event.Listener { if (!isAPICall(inventory) && !belongsToChestLikeBlock(inventory) && !plugin.getEnderContainersHook().isEnderchest(inventory) - && !LlamaUtils.belongsToLlama(inventory)) { + && !LlamaUtils.belongsToLlama(inventory) + && !plugin.getOrganizer().isMarkedAsSortable(inventory)) { return; } @@ -515,6 +517,7 @@ public class Listener implements org.bukkit.event.Listener { if (isAPICall || belongsToChestLikeBlock(event.getClickedInventory()) + || plugin.getOrganizer().isMarkedAsSortable(event.getClickedInventory()) || LlamaUtils.belongsToLlama(event.getClickedInventory()) || minepacksHook.isMinepacksBackpack(event.getClickedInventory()) || plugin.getPlayerVaultsHook().isPlayerVault(event.getClickedInventory()) @@ -559,7 +562,7 @@ public class Listener implements org.bukkit.event.Listener { private boolean isAPICall(Inventory inv) { if(inv==null) return false; - return inv.getHolder() instanceof ISortable; + return inv.getHolder() instanceof ISortable || plugin.getOrganizer().isMarkedAsSortable(inv); } @EventHandler