├── .github ├── .kodiak.toml ├── dependabot.yml └── workflows │ ├── build.yml │ └── publish.yml ├── .gitignore ├── .idea ├── codeStyles │ ├── Project.xml │ └── codeStyleConfig.xml ├── copyright │ ├── MIT.xml │ └── profiles_settings.xml ├── fileTemplates │ └── code │ │ └── Implemented Method Body.java └── inspectionProfiles │ └── Project_Default.xml ├── .pdd ├── .run └── Build the Project.run.xml ├── LICENSE ├── README.md ├── checkstyle.xml ├── jitpack.yml ├── logo └── logo.svg ├── pom.xml └── src └── main └── java └── io └── github └── portlek ├── observer ├── Source.java ├── Target.java ├── package-info.java └── source │ ├── BasicSource.java │ └── package-info.java └── smartinventory ├── Handle.java ├── Icon.java ├── InventoryContents.java ├── InventoryOpener.java ├── InventoryProvider.java ├── Page.java ├── Pagination.java ├── SlotIterator.java ├── SmartHolder.java ├── SmartInventory.java ├── Type.java ├── content ├── BasicInventoryContents.java ├── BasicPagination.java ├── BasicSlotIterator.java └── package-info.java ├── event ├── IcClickEvent.java ├── IcDragEvent.java ├── PgBottomClickEvent.java ├── PgClickEvent.java ├── PgCloseEvent.java ├── PgInitEvent.java ├── PgOpenEvent.java ├── PgOutsideClickEvent.java ├── PgTickEvent.java ├── PgUpdateEvent.java ├── PlgnDisableEvent.java ├── PlyrQuitEvent.java ├── abs │ ├── BottomClickEvent.java │ ├── ClickEvent.java │ ├── CloseEvent.java │ ├── DisableEvent.java │ ├── DragEvent.java │ ├── IconEvent.java │ ├── InitEvent.java │ ├── OpenEvent.java │ ├── OutsideClickEvent.java │ ├── PageClickEvent.java │ ├── PageEvent.java │ ├── QuitEvent.java │ ├── SmartEvent.java │ ├── TickEvent.java │ ├── UpdateEvent.java │ └── package-info.java └── package-info.java ├── handle ├── BasicHandle.java └── package-info.java ├── holder ├── SmartInventoryHolder.java └── package-info.java ├── icon ├── BasicIcon.java └── package-info.java ├── listener ├── InventoryClickListener.java ├── InventoryCloseListener.java ├── InventoryDragListener.java ├── InventoryOpenListener.java ├── PlayerQuitListener.java ├── PluginDisableListener.java └── package-info.java ├── manager ├── BasicSmartInventory.java └── package-info.java ├── opener ├── ChestInventoryOpener.java └── package-info.java ├── package-info.java ├── page ├── BasicPage.java └── package-info.java └── util ├── Pattern.java ├── ReflectionUtils.java ├── SlotPos.java ├── TitleUpdater.java └── package-info.java /.github/.kodiak.toml: -------------------------------------------------------------------------------- 1 | version = 1 2 | [merge] 3 | method = "squash" 4 | delete_branch_on_merge = true 5 | notify_on_conflict = false 6 | [merge.message] 7 | body = "pull_request_body" 8 | -------------------------------------------------------------------------------- /.github/dependabot.yml: -------------------------------------------------------------------------------- 1 | version: 2 2 | updates: 3 | - package-ecosystem: maven 4 | labels: 5 | - "automerge" 6 | - "release/patch" 7 | directory: "/" 8 | schedule: 9 | interval: daily 10 | open-pull-requests-limit: 99 11 | -------------------------------------------------------------------------------- /.github/workflows/build.yml: -------------------------------------------------------------------------------- 1 | name: build 2 | 3 | on: 4 | push: 5 | branches: [ master ] 6 | pull_request: 7 | branches: [ master ] 8 | 9 | jobs: 10 | build: 11 | runs-on: ubuntu-latest 12 | 13 | steps: 14 | - uses: actions/checkout@v2 15 | 16 | - uses: actions/setup-java@v1 17 | with: 18 | java-version: 11 19 | 20 | - run: | 21 | mvn package -Pcheckstyle 22 | mvn test -Pcoverage jacoco:report 23 | 24 | - uses: codecov/codecov-action@v1.0.14 25 | -------------------------------------------------------------------------------- /.github/workflows/publish.yml: -------------------------------------------------------------------------------- 1 | name: publish 2 | 3 | on: 4 | pull_request: 5 | types: [ closed ] 6 | 7 | jobs: 8 | tag: 9 | if: github.event.pull_request.merged == true 10 | runs-on: ubuntu-latest 11 | steps: 12 | - uses: actions-ecosystem/action-release-label@v1 13 | id: release-label 14 | 15 | - if: ${{ steps.release-label.outputs.level == null }} 16 | run: exit 1 17 | 18 | - uses: softprops/turnstyle@v1 19 | with: 20 | poll-interval-seconds: 10 21 | env: 22 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 23 | 24 | - uses: actions/checkout@v2 25 | 26 | - uses: pozetroninc/github-action-get-latest-release@v0.5.0 27 | id: version 28 | with: 29 | owner: portlek 30 | repo: SmartInventory 31 | 32 | - uses: actions-ecosystem/action-bump-semver@v1 33 | id: bump-semver 34 | with: 35 | current_version: ${{ steps.version.outputs.release }} 36 | level: ${{ steps.release-label.outputs.level }} 37 | 38 | - run: | 39 | [[ "${{ steps.bump-semver.outputs.new_version }}" =~ ^[0-9]+(\.[0-9]+)*$ ]] || exit -1 40 | 41 | - uses: actions/create-release@v1.1.4 42 | env: 43 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 44 | with: 45 | tag_name: ${{ steps.bump-semver.outputs.new_version }} 46 | release_name: ${{ steps.bump-semver.outputs.new_version }} 47 | body: '[Changelog](https://github.com/portlek/SmartInventory/compare/${{ steps.version.outputs.release }}...${{ steps.bump-semver.outputs.new_version }})' 48 | 49 | - uses: badasintended/autojitpack@v0 50 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .idea/ 2 | target/ 3 | *.iml -------------------------------------------------------------------------------- /.idea/codeStyles/codeStyleConfig.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 5 | -------------------------------------------------------------------------------- /.idea/copyright/MIT.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 7 | -------------------------------------------------------------------------------- /.idea/copyright/profiles_settings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /.idea/fileTemplates/code/Implemented Method Body.java: -------------------------------------------------------------------------------- 1 | throw new UnsupportedOperationException(" @todo #1:10m Implement ${SIMPLE_CLASS_NAME}#${METHOD_NAME}."); -------------------------------------------------------------------------------- /.pdd: -------------------------------------------------------------------------------- 1 | --source=. 2 | --verbose 3 | --exclude target/**/* 4 | --exclude .idea/**/* -------------------------------------------------------------------------------- /.run/Build the Project.run.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021 Hasan Demirtaş 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | [![idea](https://www.elegantobjects.org/intellij-idea.svg)](https://www.jetbrains.com/idea/) 4 | 5 | ![master](https://github.com/portlek/SmartInventory/workflows/build/badge.svg) 6 | [![Release](https://jitpack.io/v/portlek/SmartInventory.svg)](https://jitpack.io/#portlek/SmartInventory) 7 | 8 | ## How to Use 9 | 10 | ### Maven 11 | 12 | ```xml 13 | 14 | 15 | 16 | org.apache.maven.plugins 17 | maven-shade-plugin 18 | 3.2.4 19 | 20 | 21 | package 22 | 23 | shade 24 | 25 | 26 | true 27 | false 28 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | ``` 43 | 44 | ```xml 45 | 46 | 47 | jitpack 48 | https://jitpack.io/ 49 | 50 | 51 | ``` 52 | 53 | ```xml 54 | 55 | 56 | com.github.portlek 57 | SmartInventory 58 | ${version} 59 | 60 | 61 | ``` 62 | 63 | ### Gradle 64 | 65 | ```groovy 66 | plugins { 67 | id "com.github.johnrengelman.shadow" version "7.0.0" 68 | } 69 | ``` 70 | 71 | ```groovy 72 | repositories { 73 | maven { 74 | url "https://jitpack.io" 75 | } 76 | } 77 | ``` 78 | 79 | ```groovy 80 | dependencies { 81 | implementation("com.github.portlek:SmartInventory:${version}") 82 | } 83 | ``` 84 | 85 | ## Getting Started 86 | 87 | ### Registering the library 88 | 89 | ```java 90 | final class Main extends JavaPlugin { 91 | 92 | private final SmartInventory inventory = new BasicSmartInventory(this); 93 | 94 | @Override 95 | public void onEnable() { 96 | this.inventory.init(); 97 | new SomeClassesThatNeedSmartInventory(this.inventory).foo(); 98 | new SomeOtherClasses(this.inventory).foo(); 99 | } 100 | } 101 | ``` 102 | 103 | ### Creating a Inventory Provider Class 104 | 105 | ```java 106 | final class ExampleInventoryProvider implements InventoryProvider { 107 | 108 | @Override 109 | public void init(@NotNull final InventoryContents contents) { 110 | // Runs when the page opens first. 111 | // An icon that which is empty(air). 112 | final Icon empty = Icon.EMPTY; 113 | // An icon that has not any effect. 114 | final Icon noEffect = Icon.from(new ItemStack(Material.DIAMOND)); 115 | // A simple static icon that player can't click it. 116 | final Icon cancel = Icon.cancel(new ItemStack(Material.DIAMOND)); 117 | final Icon click = Icon.click(new ItemStack(Material.DIAMOND), clickEvent -> { 118 | // Runs when the player click the icon. 119 | }, clickEvent -> { 120 | // It's array so, optional. 121 | // If the predicate is returning true, the consumer that above will run. 122 | return true; 123 | }); 124 | final Icon drag = Icon.drag(new ItemStack(Material.DIAMOND), dragEvent -> { 125 | // Runs when the player drag the icon. 126 | }); 127 | final Icon anIcon = Icon.from(new ItemStack(Material.DIAMOND)) 128 | .whenClick(clickEvent -> { 129 | // Runs when player clicks the icon. 130 | }, clickEvent -> { 131 | // If this predicate returns false, the consumer won't run. 132 | return false; 133 | }) 134 | .whenDrag(dragEvent -> { 135 | // Runs when player drags the icon. 136 | }) 137 | .whenInteract(dragEvent -> { 138 | // Runs when player interact to the icon. 139 | }) 140 | .canSee(cont -> { 141 | // If it's returning false, player will see the fallback icon on the page. 142 | return false; 143 | }) 144 | .fallback(new ItemStack(Material.AIR)) 145 | .canUse(cont -> { 146 | // If it's returning false, player can't use the icon on the page. 147 | return false; 148 | }); 149 | // Adding an icon into the inventory. 150 | contents.add(anIcon); 151 | // Adds to the certain slot. 152 | contents.set(SlotPos.of(0, 4), anIcon); 153 | // A pagination example. 154 | final Pagination pagination = contents.pagination(); 155 | final Icon[] icons = new Icon[22]; 156 | for (int index = 0; index < icons.length; index++) { 157 | icons[index] = Icon.cancel(new ItemStack(Material.CHORUS_FRUIT, index)); 158 | } 159 | pagination.setIcons(icons); 160 | pagination.setIconsPerPage(7); 161 | pagination.addToIterator(contents.newIterator(SlotIterator.Type.HORIZONTAL, 1, 1)); 162 | final Page page = contents.page(); 163 | final Player player = contents.player(); 164 | final Icon previousArrow = Icon.click(new ItemStack(Material.ARROW), clickEvent -> 165 | page.open(player, pagination.previous().getPage())); 166 | final Icon nextArrow = Icon.click(new ItemStack(Material.ARROW), clickEvent -> 167 | page.open(player, pagination.next().getPage())); 168 | contents.set(2, 3, previousArrow); 169 | contents.set(2, 5, nextArrow); 170 | // And other tons of methods will help you to make a awesome pages :) 171 | } 172 | 173 | @Override 174 | public void tick(@NotNull final InventoryContents contents) { 175 | // Runs every tick. 176 | // You have options that; 177 | // -> make it async or not (default is false) 178 | // -> set the tick's start delay (default is 1L) 179 | // -> set the tick period (default is 1L) 180 | } 181 | 182 | @Override 183 | public void update(@NotNull final InventoryContents contents) { 184 | // Runs when the notify update method called by you. 185 | // SmartInventory#notifyUpdate(Player) 186 | // -> Finds the player's page, if it's open, runs the update method. 187 | // InventoryContents#notifyUpdate() 188 | // -> Runs the update method of this class. 189 | } 190 | } 191 | ``` 192 | 193 | ### Creating a Page 194 | 195 | ```java 196 | final class CreateAPage { 197 | 198 | @NotNull 199 | final SmartInventory inventory; 200 | 201 | @NotNull 202 | final InventoryProvider provider; 203 | 204 | CreateAPage(@NotNull final SmartInventory inventory, @NotNull final InventoryProvider provider) { 205 | this.inventory = inventory; 206 | this.provider = provider; 207 | } 208 | 209 | void open(@NotNull final Page parentPage, @NotNull final Player player) { 210 | final Map properties = new HashMap<>(); 211 | properties.put("test-key", player.getName()); 212 | Page.build(this.inventory, this.provider) 213 | // Runs the update method as async. (default is false) 214 | .async(true) 215 | // If it's returning false, player's page will close and open immediately. (default is true) 216 | // Closing a page cannot be canceled. It just closes and opens again method. 217 | .canClose(true) 218 | .canClose(closeEvent -> true) 219 | // Set the page's column. (default is 9) 220 | // There is no any different page type, so it must be 9 for now. 221 | .column(9) 222 | // Set the page's parent page.(default is empty) 223 | // contents.page().parent().ifPresent(page -> ...) 224 | .parent(parentPage) 225 | // Set the page's row size. (default is 1) 226 | // The row's range is 1 to 6 227 | .row(3) 228 | // Set the page's start delay of the tick method. (default is 1L) 229 | .startDelay(10L) 230 | // Set the page's period time of the tick method. (default is 1L) 231 | .tick(1L) 232 | // Set the page's title. (default is Smart Inventory) 233 | .title("Title") 234 | // Runs after the page opened. If predicates cannot passed, the consumer won't run. 235 | .whenOpen(openEvent -> { 236 | openEvent.contents().player().sendMessage("The page opened."); 237 | openEvent.contents().player().sendMessage("This message will send to Player."); 238 | }, Arrays.asList( 239 | openEvent -> 240 | openEvent.contents().player().getName().equals("Player"), 241 | openEvent -> 242 | openEvent.contents().player().hasPermission("test.perm") 243 | )) 244 | // Runs after the page closed. If predicates cannot passed, the consumer won't run. 245 | .whenClose(closeEvent -> { 246 | closeEvent.contents().player().sendMessage("The page closed."); 247 | closeEvent.contents().player().sendMessage("This message will send to Player."); 248 | }, Arrays.asList( 249 | closeEvent -> 250 | closeEvent.contents().player().getName().equals("Player"), 251 | closeEvent -> 252 | closeEvent.contents().player().hasPermission("test.perm") 253 | )) 254 | // Opens the page for the player. 255 | // With properties. 256 | // You can get the properties with 257 | // Get a property that can be nullable > contents.getProperty("test-key"); 258 | // Get a property that cannot be nullable > contents.getPropertyOrDefault("test-key-2", "fallback"); 259 | // You can also set a property > contents.setProperty("test-key-2", "test-object"); 260 | // .open(player, properties); 261 | // With properties and pagination number. 262 | // .open(player, 2, properties); 263 | // With pagination number. 264 | // .open(player, 2) 265 | // Default open method. 266 | .open(player); 267 | } 268 | 269 | void openAnEmptyPage(@NotNull final Player player) { 270 | Page.build(this.inventory, this.provider) 271 | .title("Title") 272 | .row(3) 273 | .open(player); 274 | } 275 | } 276 | ``` 277 | 278 | ## Useful libraries with SmartInventory 279 | 280 | ### Simple Bukkit item builder library with builder pattern. 281 | 282 | [BukkitItemBuilder](https://github.com/portlek/BukkitItemBuilder) 283 | 284 | ### You can get inputs from players via chat. 285 | 286 | [Input](https://github.com/portlek/input) 287 | -------------------------------------------------------------------------------- /checkstyle.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 92 | 93 | 94 | 95 | -------------------------------------------------------------------------------- /jitpack.yml: -------------------------------------------------------------------------------- 1 | jdk: 2 | - openjdk11 3 | -------------------------------------------------------------------------------- /logo/logo.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 5 | 6 | 7 | 8 | 9 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | io.github.parentpom 8 | ppom 9 | 3.4.4 10 | 11 | io.github.portlek 12 | SmartInventory 13 | 1.0.0-SNAPSHOT 14 | SmartInventory 15 | An API for creating unique inventories for Bukkit servers. 16 | https://github.com/portlek/SmartInventory 17 | 18 | https://github.com/portlek/SmartInventory 19 | scm:git:git://github.com/portlek/SmartInventory.git 20 | scm:git:ssh://github.com/portlek/SmartInventory.git 21 | 22 | 23 | 11 24 | 25 | 26 | 27 | org.spigotmc 28 | spigot-api 29 | 1.16.5-R0.1-SNAPSHOT 30 | provided 31 | 32 | 33 | org.projectlombok 34 | lombok 35 | 36 | 37 | org.jetbrains 38 | annotations 39 | 40 | 41 | 42 | -------------------------------------------------------------------------------- /src/main/java/io/github/portlek/observer/Source.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2021 Hasan Demirtaş 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | * 24 | */ 25 | 26 | package io.github.portlek.observer; 27 | 28 | import org.jetbrains.annotations.NotNull; 29 | 30 | /** 31 | * a class that is the observer's source. 32 | * 33 | * @param type of the arguments. 34 | */ 35 | public interface Source { 36 | 37 | /** 38 | * notifies {@link Target#update(Object)} method all of the subscribes. 39 | * 40 | * @param argument the argument to notify. 41 | */ 42 | void notifyTargets(@NotNull T argument); 43 | 44 | /** 45 | * subscribes the given {@link Target} into the source. 46 | * 47 | * @param target the target to subscribe. 48 | */ 49 | void subscribe(@NotNull Target target); 50 | 51 | /** 52 | * remove the given {@link Target} from the subscriptions of the source. 53 | * 54 | * @param target the target to remove. 55 | */ 56 | void unsubscribe(@NotNull Target target); 57 | } 58 | -------------------------------------------------------------------------------- /src/main/java/io/github/portlek/observer/Target.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2021 Hasan Demirtaş 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | * 24 | */ 25 | 26 | package io.github.portlek.observer; 27 | 28 | import org.jetbrains.annotations.NotNull; 29 | 30 | /** 31 | * a class that runs the update method when the observer call it. 32 | * 33 | * @param type of the argument. 34 | */ 35 | public interface Target { 36 | 37 | /** 38 | * runs when the observer calls it. 39 | * 40 | * @param argument the argument to update. 41 | */ 42 | void update(@NotNull T argument); 43 | } 44 | -------------------------------------------------------------------------------- /src/main/java/io/github/portlek/observer/package-info.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2021 Hasan Demirtaş 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | * 24 | */ 25 | /** 26 | * the package that contains observer pattern's interfaces. 27 | */ 28 | package io.github.portlek.observer; 29 | -------------------------------------------------------------------------------- /src/main/java/io/github/portlek/observer/source/BasicSource.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2021 Hasan Demirtaş 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | * 24 | */ 25 | 26 | package io.github.portlek.observer.source; 27 | 28 | import io.github.portlek.observer.Source; 29 | import io.github.portlek.observer.Target; 30 | import java.util.ArrayList; 31 | import java.util.Collection; 32 | import org.jetbrains.annotations.NotNull; 33 | 34 | /** 35 | * an implementation for {@link Source}. 36 | * 37 | * @param type of the argument. 38 | */ 39 | public final class BasicSource implements Source { 40 | 41 | /** 42 | * the subscriptions. 43 | */ 44 | private final Collection> subscriptions = new ArrayList<>(); 45 | 46 | @Override 47 | public void notifyTargets(@NotNull final T argument) { 48 | this.subscriptions.forEach(target -> target.update(argument)); 49 | } 50 | 51 | @Override 52 | public void subscribe(@NotNull final Target target) { 53 | if (!this.subscriptions.contains(target)) { 54 | this.subscriptions.add(target); 55 | } 56 | } 57 | 58 | @Override 59 | public void unsubscribe(@NotNull final Target target) { 60 | this.subscriptions.remove(target); 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /src/main/java/io/github/portlek/observer/source/package-info.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2021 Hasan Demirtaş 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | * 24 | */ 25 | /** 26 | * the package that contains {@link io.github.portlek.observer.Source} implementations. 27 | */ 28 | package io.github.portlek.observer.source; 29 | -------------------------------------------------------------------------------- /src/main/java/io/github/portlek/smartinventory/Handle.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2021 Hasan Demirtaş 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | * 24 | */ 25 | 26 | package io.github.portlek.smartinventory; 27 | 28 | import io.github.portlek.smartinventory.event.abs.SmartEvent; 29 | import io.github.portlek.smartinventory.handle.BasicHandle; 30 | import java.util.Arrays; 31 | import java.util.List; 32 | import java.util.function.Consumer; 33 | import java.util.function.Predicate; 34 | import org.jetbrains.annotations.NotNull; 35 | 36 | /** 37 | * a class that handles and runs the given consumer after checking the requirements. 38 | * 39 | * @param type of the event. 40 | */ 41 | public interface Handle extends Consumer, Type { 42 | 43 | /** 44 | * creates a simple handler. 45 | * 46 | * @param clazz the class to determine the type of the event. 47 | * @param consumer the consumer to run. 48 | * @param requirements the requirements to check. 49 | * @param type of the {@link SmartEvent}. 50 | * 51 | * @return a simple handler instance. 52 | */ 53 | @NotNull 54 | static Handle from(@NotNull final Class clazz, @NotNull final Consumer consumer, 55 | @NotNull final List> requirements) { 56 | return new BasicHandle<>(clazz, consumer, requirements); 57 | } 58 | 59 | /** 60 | * creates a simple handler. 61 | * 62 | * @param clazz the class to determine the type of the event. 63 | * @param consumer the consumer to run. 64 | * @param requirements the requirements to check. 65 | * @param type of the {@link SmartEvent}. 66 | * 67 | * @return a simple handler instance. 68 | */ 69 | @SafeVarargs 70 | @NotNull 71 | static Handle from(@NotNull final Class clazz, @NotNull final Consumer consumer, 72 | @NotNull final Predicate... requirements) { 73 | return Handle.from(clazz, consumer, Arrays.asList(requirements)); 74 | } 75 | } 76 | -------------------------------------------------------------------------------- /src/main/java/io/github/portlek/smartinventory/Icon.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2021 Hasan Demirtaş 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | * 24 | */ 25 | 26 | package io.github.portlek.smartinventory; 27 | 28 | import io.github.portlek.smartinventory.event.abs.ClickEvent; 29 | import io.github.portlek.smartinventory.event.abs.DragEvent; 30 | import io.github.portlek.smartinventory.event.abs.IconEvent; 31 | import io.github.portlek.smartinventory.event.abs.SmartEvent; 32 | import io.github.portlek.smartinventory.icon.BasicIcon; 33 | import java.util.Arrays; 34 | import java.util.Collection; 35 | import java.util.Collections; 36 | import java.util.List; 37 | import java.util.function.Consumer; 38 | import java.util.function.Predicate; 39 | import org.bukkit.Material; 40 | import org.bukkit.inventory.ItemStack; 41 | import org.jetbrains.annotations.NotNull; 42 | 43 | /** 44 | * a class that holds the {@link ItemStack} to put the given inventory. 45 | */ 46 | public interface Icon { 47 | 48 | /** 49 | * an empty {@link Icon} instance. 50 | */ 51 | Icon EMPTY = Icon.from(new ItemStack(Material.AIR)); 52 | 53 | /** 54 | * creates a simple icon from the given {@link ItemStack} with {@link SmartEvent#cancel()} interaction. 55 | * 56 | * @param item the item to create. 57 | * 58 | * @return a simple icon instance. 59 | */ 60 | @NotNull 61 | static Icon cancel(@NotNull final ItemStack item) { 62 | return Icon.from(item) 63 | .whenInteract(SmartEvent::cancel); 64 | } 65 | 66 | /** 67 | * creates a simple icon from the given {@link ItemStack} with a {@link ClickEvent}. 68 | * 69 | * @param item the item to create. 70 | * @param consumer the consumer to run. 71 | * @param requirements the requirements to check. 72 | * 73 | * @return a simple icon instance. 74 | */ 75 | @SafeVarargs 76 | @NotNull 77 | static Icon click(@NotNull final ItemStack item, @NotNull final Consumer consumer, 78 | @NotNull final Predicate... requirements) { 79 | return Icon.from(item) 80 | .whenClick(consumer, Arrays.asList(requirements)); 81 | } 82 | 83 | /** 84 | * creates a simple icon from the given {@link ItemStack} with a {@link DragEvent}. 85 | * 86 | * @param item the item to create. 87 | * @param consumer the consumer to run. 88 | * @param requirements the requirements to check. 89 | * 90 | * @return a simple icon instance. 91 | */ 92 | @SafeVarargs 93 | @NotNull 94 | static Icon drag(@NotNull final ItemStack item, @NotNull final Consumer consumer, 95 | @NotNull final Predicate... requirements) { 96 | return Icon.from(item) 97 | .whenDrag(consumer, Arrays.asList(requirements)); 98 | } 99 | 100 | /** 101 | * creates a simple icon from the given {@link ItemStack}. 102 | * 103 | * @param item the item to create. 104 | * 105 | * @return a simple icon instance. 106 | */ 107 | @NotNull 108 | static Icon from(@NotNull final ItemStack item) { 109 | return new BasicIcon(item); 110 | } 111 | 112 | /** 113 | * accepts the upcoming event for all of the handles. 114 | * 115 | * @param event the event to accept. 116 | * @param type of the event. 117 | */ 118 | void accept(@NotNull T event); 119 | 120 | /** 121 | * calculates and returns the item of the icon. 122 | * tests the {@code canSee} with the given contents, and if it returns {@code true}, 123 | * returns {@link #getItem()} else, returns the fallback. 124 | * 125 | * @param contents the contents to calculate. 126 | * 127 | * @return the calculated item. 128 | */ 129 | @NotNull 130 | ItemStack calculateItem(@NotNull InventoryContents contents); 131 | 132 | /** 133 | * sets the canSee value of the icon to the given predicate. 134 | * 135 | * @param predicate the predicate to set. 136 | * 137 | * @return {@code this}, for chained calls. 138 | */ 139 | @NotNull 140 | Icon canSee(@NotNull Predicate predicate); 141 | 142 | /** 143 | * sets the canUse value of the icon to the given predicate. 144 | * 145 | * @param predicate the predicate to set. 146 | * 147 | * @return {@code this}, for chained calls. 148 | */ 149 | @NotNull 150 | Icon canUse(@NotNull Predicate predicate); 151 | 152 | /** 153 | * sets the fallback item of the icon to the given item. 154 | * 155 | * @param fallback the fallback to set. 156 | * 157 | * @return {@code this}, for chained calls. 158 | */ 159 | @NotNull 160 | Icon fallback(@NotNull ItemStack fallback); 161 | 162 | /** 163 | * obtains the icon's {@link ItemStack}. 164 | * 165 | * @return the icon's item. 166 | */ 167 | @NotNull 168 | ItemStack getItem(); 169 | 170 | /** 171 | * add the given event and requirements to the icon's handles. 172 | * 173 | * @param clazz the class to determine the type of the event. 174 | * @param consumer the consumer to add. 175 | * @param requirements the requirements to add. 176 | * @param type of the event. 177 | * 178 | * @return {@code this}, for chained calls. 179 | */ 180 | @NotNull 181 | default Icon handle(@NotNull final Class clazz, @NotNull final Consumer consumer, 182 | @NotNull final List> requirements) { 183 | return this.handle(Handle.from(clazz, consumer, requirements)); 184 | } 185 | 186 | /** 187 | * adds the given handle into the icon's handle list. 188 | * 189 | * @param handle the handle to add. 190 | * @param type of the event. 191 | * 192 | * @return {@code this}, for chained calls. 193 | */ 194 | @NotNull Icon handle(@NotNull Handle handle); 195 | 196 | /** 197 | * adds all the given handles into the icon's handle list. 198 | * 199 | * @param handles the handles to add. 200 | * 201 | * @return {@code this}, for chained calls. 202 | */ 203 | @NotNull 204 | Icon handles(@NotNull Collection> handles); 205 | 206 | /** 207 | * sets the item of the icon to the given item. 208 | * 209 | * @param item the item to set. 210 | * 211 | * @return {@code this}, for chained calls. 212 | */ 213 | @NotNull 214 | Icon item(@NotNull ItemStack item); 215 | 216 | /** 217 | * adds the given {@link ClickEvent} to the icon's handles. 218 | * 219 | * @param consumer the consumer to add. 220 | * 221 | * @return {@code this}, for chained calls. 222 | */ 223 | @NotNull 224 | default Icon whenClick(@NotNull final Consumer consumer) { 225 | return this.whenClick(consumer, Collections.emptyList()); 226 | } 227 | 228 | /** 229 | * adds the given {@link ClickEvent} with the requirement to the icon's handles. 230 | * 231 | * @param consumer the consumer to add. 232 | * @param requirement the requirement to add. 233 | * 234 | * @return {@code this}, for chained calls. 235 | */ 236 | @NotNull 237 | default Icon whenClick(@NotNull final Consumer consumer, 238 | @NotNull final Predicate requirement) { 239 | return this.handle(ClickEvent.class, consumer, Collections.singletonList(requirement)); 240 | } 241 | 242 | /** 243 | * adds the given {@link ClickEvent} with the requirements to the icon's handles. 244 | * 245 | * @param consumer the consumer to add. 246 | * @param requirements the requirements to add. 247 | * 248 | * @return {@code this}, for chained calls. 249 | */ 250 | @NotNull 251 | default Icon whenClick(@NotNull final Consumer consumer, 252 | @NotNull final List> requirements) { 253 | return this.handle(ClickEvent.class, consumer, requirements); 254 | } 255 | 256 | /** 257 | * adds the given {@link DragEvent} to the icon's handles. 258 | * 259 | * @param consumer the consumer to add. 260 | * 261 | * @return {@code this}, for chained calls. 262 | */ 263 | @NotNull 264 | default Icon whenDrag(@NotNull final Consumer consumer) { 265 | return this.whenDrag(consumer, Collections.emptyList()); 266 | } 267 | 268 | /** 269 | * adds the given {@link DragEvent} with the requirement to the icon's handles. 270 | * 271 | * @param consumer the consumer to add. 272 | * @param requirement the requirement to add. 273 | * 274 | * @return {@code this}, for chained calls. 275 | */ 276 | @NotNull 277 | default Icon whenDrag(@NotNull final Consumer consumer, 278 | @NotNull final Predicate requirement) { 279 | return this.whenDrag(consumer, Collections.singletonList(requirement)); 280 | } 281 | 282 | /** 283 | * adds the given {@link DragEvent} with the requirements to the icon's handles. 284 | * 285 | * @param consumer the consumer to add. 286 | * @param requirements the requirements to add. 287 | * 288 | * @return {@code this}, for chained calls. 289 | */ 290 | @NotNull 291 | default Icon whenDrag(@NotNull final Consumer consumer, 292 | @NotNull final List> requirements) { 293 | return this.handle(DragEvent.class, consumer, requirements); 294 | } 295 | 296 | /** 297 | * adds the given {@link IconEvent} to the icon's handles. 298 | * 299 | * @param consumer the consumer to add. 300 | * 301 | * @return {@code this}, for chained calls. 302 | */ 303 | @NotNull 304 | default Icon whenInteract(@NotNull final Consumer consumer) { 305 | return this.whenInteract(consumer, Collections.emptyList()); 306 | } 307 | 308 | /** 309 | * adds the given {@link IconEvent} with the requirement to the icon's handles. 310 | * 311 | * @param consumer the consumer to add. 312 | * @param requirement the requirement to add. 313 | * 314 | * @return {@code this}, for chained calls. 315 | */ 316 | @NotNull 317 | default Icon whenInteract(@NotNull final Consumer consumer, 318 | @NotNull final Predicate requirement) { 319 | return this.whenInteract(consumer, Collections.singletonList(requirement)); 320 | } 321 | 322 | /** 323 | * adds the given {@link IconEvent} with the requirements to the icon's handles. 324 | * 325 | * @param consumer the consumer to add. 326 | * @param requirements the requirements to add. 327 | * 328 | * @return {@code this}, for chained calls. 329 | */ 330 | @NotNull 331 | default Icon whenInteract(@NotNull final Consumer consumer, 332 | @NotNull final List> requirements) { 333 | return this.handle(IconEvent.class, consumer, requirements); 334 | } 335 | } 336 | -------------------------------------------------------------------------------- /src/main/java/io/github/portlek/smartinventory/InventoryOpener.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2021 Hasan Demirtaş 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | * 24 | */ 25 | 26 | package io.github.portlek.smartinventory; 27 | 28 | import org.bukkit.event.inventory.InventoryType; 29 | import org.bukkit.inventory.Inventory; 30 | import org.jetbrains.annotations.NotNull; 31 | 32 | /** 33 | * a class that opens {@link Inventory}s from the given {@link InventoryType}s. 34 | */ 35 | public interface InventoryOpener { 36 | 37 | /** 38 | * fills the given contents to the given inventory. 39 | * 40 | * @param inventory the inventory to fill. 41 | * @param contents the contents to fill. 42 | */ 43 | default void fill(@NotNull final Inventory inventory, @NotNull final InventoryContents contents) { 44 | final var items = contents.all(); 45 | for (var row = 0; row < items.length; row++) { 46 | for (var column = 0; column < items[row].length; column++) { 47 | if (items[row][column] != null) { 48 | inventory.setItem(9 * row + column, items[row][column].calculateItem(contents)); 49 | } 50 | } 51 | } 52 | } 53 | 54 | /** 55 | * opens the page for the given player. 56 | * 57 | * @param contents the contents to open. 58 | * 59 | * @return opened inventory itself. 60 | */ 61 | @NotNull 62 | Inventory open(@NotNull InventoryContents contents); 63 | 64 | /** 65 | * checks if the inventory type is supporting for {@code this}. 66 | * 67 | * @param type the type to check. 68 | * 69 | * @return {@code true} if the type supports the type.. 70 | */ 71 | boolean supports(@NotNull InventoryType type); 72 | } 73 | -------------------------------------------------------------------------------- /src/main/java/io/github/portlek/smartinventory/InventoryProvider.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2021 Hasan Demirtaş 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | * 24 | */ 25 | 26 | package io.github.portlek.smartinventory; 27 | 28 | import io.github.portlek.observer.Target; 29 | import org.jetbrains.annotations.NotNull; 30 | 31 | /** 32 | * a class that allows to manage player's inventory contents. 33 | */ 34 | public interface InventoryProvider extends Target { 35 | 36 | /** 37 | * an empty inventory provider. 38 | */ 39 | InventoryProvider EMPTY = new InventoryProvider() { 40 | }; 41 | 42 | /** 43 | * runs when the page has just opened. 44 | * 45 | * @param contents the contents to initiate. 46 | */ 47 | default void init(@NotNull final InventoryContents contents) { 48 | } 49 | 50 | /** 51 | * runs every tick. 52 | * 53 | * @param contents the contents to tick. 54 | */ 55 | default void tick(@NotNull final InventoryContents contents) { 56 | } 57 | 58 | /** 59 | * runs when {@link InventoryContents#notifyUpdate()} runs. 60 | *

61 | * came from {@link Target}'s method. 62 | * 63 | * @param contents the contents to update. 64 | */ 65 | @Override 66 | default void update(@NotNull final InventoryContents contents) { 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /src/main/java/io/github/portlek/smartinventory/Pagination.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2021 Hasan Demirtaş 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | * 24 | */ 25 | 26 | package io.github.portlek.smartinventory; 27 | 28 | import org.jetbrains.annotations.NotNull; 29 | 30 | /** 31 | * a class which lets you switch pages; 32 | * easily get icons in the given page, 33 | * easily manipulate the pages and 34 | * check if a page is the first or the last one 35 | * ({@link Pagination#isFirst()} / {@link Pagination#isLast()}). 36 | *

37 | * you must start by setting the {@link Pagination#setIcons(Icon...)} and the {@link Pagination#setIconsPerPage(int)}, 38 | * then you can manipulate the pages by using the 39 | * {@link Pagination#page(int)} / 40 | * {@link Pagination#first()} / 41 | * {@link Pagination#previous()} / 42 | * {@link Pagination#next()} / 43 | * {@link Pagination#last()} 44 | * methods. 45 | *

46 | * then, when you need to get all the icons of the current page, 47 | * either use the {@link Pagination#getPageIcons()} method, or directly 48 | * add the icons to your inventory with a {@link SlotIterator} and the 49 | * method {@link Pagination#addToIterator(SlotIterator)}. 50 | */ 51 | public interface Pagination { 52 | 53 | /** 54 | * adds all the current page icons to the given iterator. 55 | * 56 | * @param iterator the iterator. 57 | * 58 | * @return {@code this}, for chained calls. 59 | */ 60 | @NotNull 61 | default Pagination addToIterator(@NotNull final SlotIterator iterator) { 62 | for (final var item : this.getPageIcons()) { 63 | iterator.next().set(item); 64 | if (iterator.ended()) { 65 | break; 66 | } 67 | } 68 | return this; 69 | } 70 | 71 | /** 72 | * Sets the current page to the first page. 73 | *

74 | * this is equivalent to: {@code page(0)}. 75 | * 76 | * @return {@code this}, for chained calls. 77 | */ 78 | @NotNull 79 | Pagination first(); 80 | 81 | /** 82 | * gets the current page. 83 | * 84 | * @return the current page. 85 | */ 86 | int getPage(); 87 | 88 | /** 89 | * gets the icons of the current page. 90 | *

91 | * this returns an array of the size of the icons per page. 92 | * 93 | * @return the current page icons. 94 | */ 95 | @NotNull 96 | Icon[] getPageIcons(); 97 | 98 | /** 99 | * checks if the current page is the first page. 100 | *

101 | * this is equivalent to: {@code page == 0}. 102 | * 103 | * @return {@code true} if this page is the first page. 104 | */ 105 | boolean isFirst(); 106 | 107 | /** 108 | * checks if the current page is the last page. 109 | *

110 | * this is equivalent to: {@code page == iconsCount / iconsPerPage}. 111 | * 112 | * @return {@code true} if this page is the last page. 113 | */ 114 | boolean isLast(); 115 | 116 | /** 117 | * sets the current page to the last page. 118 | *

119 | * this is equivalent to: {@code page(iconsCount / iconsPerPage)}. 120 | * 121 | * @return {@code this}, for chained calls. 122 | */ 123 | @NotNull 124 | Pagination last(); 125 | 126 | /** 127 | * sets the current page to the next page, 128 | * if the current page is already the last page, this do nothing. 129 | * 130 | * @return {@code this}, for chained calls. 131 | */ 132 | @NotNull 133 | Pagination next(); 134 | 135 | /** 136 | * sets the current page. 137 | * 138 | * @param page the current page. 139 | * 140 | * @return {@code this}, for chained calls. 141 | */ 142 | @NotNull 143 | Pagination page(int page); 144 | 145 | /** 146 | * sets the current page to the previous page, 147 | * if the current page is already the first page, this do nothing. 148 | * 149 | * @return {@code this}, for chained calls. 150 | */ 151 | @NotNull 152 | Pagination previous(); 153 | 154 | /** 155 | * sets all the icons for this Pagination. 156 | * 157 | * @param icons the icons. 158 | * 159 | * @return {@code this}, for chained calls. 160 | */ 161 | @NotNull 162 | Pagination setIcons(@NotNull Icon... icons); 163 | 164 | /** 165 | * sets the maximum amount of icons per page. 166 | * 167 | * @param iconsPerPage the maximum amount of icons per page. 168 | * 169 | * @return {@code this}, for chained calls. 170 | */ 171 | @NotNull 172 | Pagination setIconsPerPage(int iconsPerPage); 173 | } 174 | -------------------------------------------------------------------------------- /src/main/java/io/github/portlek/smartinventory/SmartHolder.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2021 Hasan Demirtaş 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | * 24 | */ 25 | 26 | package io.github.portlek.smartinventory; 27 | 28 | import org.bukkit.entity.Player; 29 | import org.bukkit.inventory.InventoryHolder; 30 | import org.bukkit.plugin.Plugin; 31 | import org.jetbrains.annotations.NotNull; 32 | 33 | /** 34 | * an interface to determine inventory holders. 35 | */ 36 | public interface SmartHolder extends InventoryHolder { 37 | 38 | /** 39 | * obtains the contents. 40 | * 41 | * @return contents. 42 | */ 43 | @NotNull 44 | InventoryContents getContents(); 45 | 46 | /** 47 | * obtains the page. 48 | * 49 | * @return page. 50 | */ 51 | @NotNull 52 | Page getPage(); 53 | 54 | /** 55 | * obtains the player. 56 | * 57 | * @return player. 58 | */ 59 | @NotNull 60 | Player getPlayer(); 61 | 62 | /** 63 | * obtains the plugin. 64 | * 65 | * @return plugin. 66 | */ 67 | @NotNull 68 | Plugin getPlugin(); 69 | 70 | /** 71 | * checks if the holder is active. 72 | * 73 | * @return {@code true} if the holder is active. 74 | */ 75 | boolean isActive(); 76 | 77 | /** 78 | * sets the active. 79 | * 80 | * @param active the active to set. 81 | */ 82 | void setActive(boolean active); 83 | } 84 | -------------------------------------------------------------------------------- /src/main/java/io/github/portlek/smartinventory/SmartInventory.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2021 Hasan Demirtaş 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | * 24 | */ 25 | 26 | package io.github.portlek.smartinventory; 27 | 28 | import io.github.portlek.smartinventory.event.PgTickEvent; 29 | import io.github.portlek.smartinventory.listener.InventoryClickListener; 30 | import io.github.portlek.smartinventory.listener.InventoryCloseListener; 31 | import io.github.portlek.smartinventory.listener.InventoryDragListener; 32 | import io.github.portlek.smartinventory.listener.InventoryOpenListener; 33 | import io.github.portlek.smartinventory.listener.PlayerQuitListener; 34 | import io.github.portlek.smartinventory.listener.PluginDisableListener; 35 | import io.github.portlek.smartinventory.opener.ChestInventoryOpener; 36 | import java.util.Arrays; 37 | import java.util.Collection; 38 | import java.util.Collections; 39 | import java.util.List; 40 | import java.util.Map; 41 | import java.util.Optional; 42 | import java.util.UUID; 43 | import java.util.function.Consumer; 44 | import java.util.function.Function; 45 | import java.util.stream.Collectors; 46 | import java.util.stream.Stream; 47 | import org.bukkit.Bukkit; 48 | import org.bukkit.entity.Player; 49 | import org.bukkit.event.Listener; 50 | import org.bukkit.event.inventory.InventoryType; 51 | import org.bukkit.inventory.InventoryHolder; 52 | import org.bukkit.plugin.Plugin; 53 | import org.bukkit.scheduler.BukkitRunnable; 54 | import org.jetbrains.annotations.NotNull; 55 | 56 | /** 57 | * a class that manages all smart inventories. 58 | */ 59 | public interface SmartInventory { 60 | 61 | /** 62 | * default inventory openers. 63 | */ 64 | List DEFAULT_OPENERS = Collections.singletonList( 65 | new ChestInventoryOpener()); 66 | 67 | /** 68 | * all listener to register. 69 | */ 70 | Function, List> LISTENERS = function -> Arrays.asList( 71 | new InventoryClickListener(), 72 | new InventoryOpenListener(), 73 | new InventoryCloseListener(function), 74 | new PlayerQuitListener(function), 75 | new PluginDisableListener(), 76 | new InventoryDragListener()); 77 | 78 | /** 79 | * obtains the given {@code uniqueId}'s smart holder. 80 | * 81 | * @param uniqueId the unique id to obtain. 82 | * 83 | * @return smart holder. 84 | */ 85 | @NotNull 86 | static Optional getHolder(@NotNull final UUID uniqueId) { 87 | return Optional.ofNullable(Bukkit.getPlayer(uniqueId)) 88 | .flatMap(SmartInventory::getHolder); 89 | } 90 | 91 | /** 92 | * obtains the given {@code player}'s smart holder. 93 | * 94 | * @param player the player to obtain. 95 | * 96 | * @return smart holder. 97 | */ 98 | @NotNull 99 | static Optional getHolder(@NotNull final Player player) { 100 | final InventoryHolder holder = player.getOpenInventory().getTopInventory().getHolder(); 101 | if (!(holder instanceof SmartHolder)) { 102 | return Optional.empty(); 103 | } 104 | return Optional.of((SmartHolder) holder) 105 | .filter(SmartHolder::isActive); 106 | } 107 | 108 | /** 109 | * obtains the smart holders of all the online players. 110 | * 111 | * @return smart holders of online players. 112 | */ 113 | @NotNull 114 | static List getHolders() { 115 | return Bukkit.getOnlinePlayers().stream() 116 | .map(SmartInventory::getHolder) 117 | .filter(Optional::isPresent) 118 | .map(Optional::get) 119 | .collect(Collectors.toList()); 120 | } 121 | 122 | /** 123 | * obtains the players that see the given page. 124 | * 125 | * @param page the page to obtain. 126 | * 127 | * @return a player list. 128 | */ 129 | @NotNull 130 | static List getOpenedPlayers(@NotNull final Page page) { 131 | return SmartInventory.getHolders().stream() 132 | .filter(holder -> page.id().equals(holder.getPage().id())) 133 | .map(SmartHolder::getPlayer) 134 | .collect(Collectors.toList()); 135 | } 136 | 137 | /** 138 | * runs {@link InventoryProvider#update(InventoryContents)} method of the player's page. 139 | * 140 | * @param player the player to notify. 141 | */ 142 | static void notifyUpdate(@NotNull final Player player) { 143 | SmartInventory.getHolder(player).ifPresent(smartHolder -> 144 | smartHolder.getContents().notifyUpdate()); 145 | } 146 | 147 | /** 148 | * runs {@link InventoryProvider#update(InventoryContents)} method of the given provider's class. 149 | * 150 | * @param provider the provider to notify. 151 | * @param type of the class. 152 | */ 153 | static void notifyUpdateForAll(@NotNull final Class provider) { 154 | SmartInventory.getHolders().stream() 155 | .map(SmartHolder::getContents) 156 | .filter(contents -> provider.equals(contents.page().provider().getClass())) 157 | .forEach(InventoryContents::notifyUpdate); 158 | } 159 | 160 | /** 161 | * runs {@link InventoryProvider#update(InventoryContents)} method of the page called the given id. 162 | * 163 | * @param id the id to find and run the update method. 164 | */ 165 | static void notifyUpdateForAllById(@NotNull final String id) { 166 | SmartInventory.getHolders().stream() 167 | .map(SmartHolder::getPage) 168 | .filter(page -> page.id().equals(id)) 169 | .forEach(Page::notifyUpdateForAll); 170 | } 171 | 172 | /** 173 | * finds a {@link InventoryOpener} from the given {@link InventoryType}. 174 | * 175 | * @param type the type to find. 176 | * 177 | * @return the inventory opener from the given type. 178 | */ 179 | @NotNull 180 | default Optional findOpener(@NotNull final InventoryType type) { 181 | return Stream.of(this.getOpeners(), SmartInventory.DEFAULT_OPENERS) 182 | .flatMap(Collection::stream) 183 | .filter(opener -> opener.supports(type)) 184 | .findFirst(); 185 | } 186 | 187 | /** 188 | * obtains inventory openers. 189 | * 190 | * @return inventory openers. 191 | */ 192 | @NotNull 193 | Collection getOpeners(); 194 | 195 | /** 196 | * obtains the plugin. 197 | * 198 | * @return the plugin. 199 | */ 200 | @NotNull 201 | Plugin getPlugin(); 202 | 203 | /** 204 | * obtains the given uniqueId's task. 205 | * 206 | * @param uniqueId the uniqueId to obtain. 207 | * 208 | * @return a {@link BukkitRunnable} instance. 209 | */ 210 | @NotNull 211 | default Optional getTask(@NotNull final UUID uniqueId) { 212 | return Optional.ofNullable(this.getTasks().get(uniqueId)); 213 | } 214 | 215 | /** 216 | * obtains the tasks. 217 | * 218 | * @return tasks. 219 | */ 220 | @NotNull 221 | Map getTasks(); 222 | 223 | /** 224 | * initiates the manager. 225 | */ 226 | default void init() { 227 | SmartInventory.LISTENERS.apply(this::stopTick).forEach(listener -> 228 | Bukkit.getPluginManager().registerEvents(listener, this.getPlugin())); 229 | } 230 | 231 | /** 232 | * registers the given inventory openers. 233 | * 234 | * @param openers the openers to register. 235 | */ 236 | default void registerOpeners(@NotNull final InventoryOpener... openers) { 237 | this.getOpeners().addAll(Arrays.asList(openers)); 238 | } 239 | 240 | /** 241 | * removes given uniqueId of the ticking task. 242 | * 243 | * @param uniqueId the uniqueId to set. 244 | */ 245 | default void removeTask(@NotNull final UUID uniqueId) { 246 | this.getTasks().remove(uniqueId); 247 | } 248 | 249 | /** 250 | * sets the given player of the ticking task to the given task. 251 | * 252 | * @param uniqueId the unique id to set. 253 | * @param task the task to set. 254 | */ 255 | default void setTask(@NotNull final UUID uniqueId, @NotNull final BukkitRunnable task) { 256 | this.getTasks().put(uniqueId, task); 257 | } 258 | 259 | /** 260 | * stops the ticking of the given uniqueId. 261 | * 262 | * @param uniqueId the uniqueId to stop. 263 | */ 264 | default void stopTick(@NotNull final UUID uniqueId) { 265 | this.getTask(uniqueId).ifPresent(runnable -> { 266 | Bukkit.getScheduler().cancelTask(runnable.getTaskId()); 267 | this.removeTask(uniqueId); 268 | }); 269 | } 270 | 271 | /** 272 | * starts the ticking of the given player with the given page. 273 | * 274 | * @param uniqueId the unique id to start. 275 | * @param page the page to start. 276 | */ 277 | default void tick(@NotNull final UUID uniqueId, @NotNull final Page page) { 278 | final BukkitRunnable task = new BukkitRunnable() { 279 | @Override 280 | public void run() { 281 | SmartInventory.getHolder(uniqueId) 282 | .map(SmartHolder::getContents) 283 | .ifPresent(contents -> { 284 | page.accept(new PgTickEvent(contents)); 285 | page.provider().tick(contents); 286 | }); 287 | } 288 | }; 289 | this.setTask(uniqueId, task); 290 | if (page.async()) { 291 | task.runTaskTimerAsynchronously(this.getPlugin(), page.startDelay(), page.tick()); 292 | } else { 293 | task.runTaskTimer(this.getPlugin(), page.startDelay(), page.tick()); 294 | } 295 | } 296 | 297 | /** 298 | * unregisters the given inventory openers. 299 | * 300 | * @param openers the openers to unregister. 301 | */ 302 | default void unregisterOpeners(@NotNull final InventoryOpener... openers) { 303 | this.getOpeners().removeAll(Arrays.asList(openers)); 304 | } 305 | } 306 | -------------------------------------------------------------------------------- /src/main/java/io/github/portlek/smartinventory/Type.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2021 Hasan Demirtaş 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | * 24 | */ 25 | 26 | package io.github.portlek.smartinventory; 27 | 28 | import org.jetbrains.annotations.NotNull; 29 | 30 | /** 31 | * an interface to determine type of the classes at runtime. 32 | * 33 | * @param type of the class. 34 | */ 35 | public interface Type { 36 | 37 | /** 38 | * obtains the type of the class. 39 | * 40 | * @return the type of the class. 41 | */ 42 | @NotNull 43 | Class type(); 44 | } 45 | -------------------------------------------------------------------------------- /src/main/java/io/github/portlek/smartinventory/content/BasicInventoryContents.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2021 Hasan Demirtaş 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | * 24 | */ 25 | 26 | package io.github.portlek.smartinventory.content; 27 | 28 | import io.github.portlek.smartinventory.Icon; 29 | import io.github.portlek.smartinventory.InventoryContents; 30 | import io.github.portlek.smartinventory.Page; 31 | import io.github.portlek.smartinventory.Pagination; 32 | import io.github.portlek.smartinventory.SlotIterator; 33 | import io.github.portlek.smartinventory.SmartInventory; 34 | import io.github.portlek.smartinventory.util.SlotPos; 35 | import io.github.portlek.smartinventory.util.TitleUpdater; 36 | import java.util.Collections; 37 | import java.util.HashMap; 38 | import java.util.HashSet; 39 | import java.util.Map; 40 | import java.util.Optional; 41 | import java.util.Set; 42 | import lombok.RequiredArgsConstructor; 43 | import org.bukkit.entity.Player; 44 | import org.bukkit.inventory.ItemStack; 45 | import org.jetbrains.annotations.NotNull; 46 | import org.jetbrains.annotations.Nullable; 47 | 48 | /** 49 | * an implementation for {@link InventoryContents}. 50 | */ 51 | @RequiredArgsConstructor 52 | public final class BasicInventoryContents implements InventoryContents { 53 | 54 | /** 55 | * the contents. 56 | */ 57 | @Nullable 58 | private final Icon[][] contents; 59 | 60 | /** 61 | * the editable slots. 62 | */ 63 | private final Set editableSlots = new HashSet<>(); 64 | 65 | /** 66 | * the iterators. 67 | */ 68 | private final Map iterators = new HashMap<>(); 69 | 70 | /** 71 | * the page. 72 | */ 73 | @NotNull 74 | private final Page page; 75 | 76 | /** 77 | * the pagination. 78 | */ 79 | private final Pagination pagination = new BasicPagination(); 80 | 81 | /** 82 | * the player. 83 | */ 84 | @NotNull 85 | private final Player player; 86 | 87 | /** 88 | * the properties. 89 | */ 90 | private final Map properties = new HashMap<>(); 91 | 92 | /** 93 | * ctor. 94 | * 95 | * @param page the page. 96 | * @param player the player 97 | */ 98 | public BasicInventoryContents(@NotNull final Page page, @NotNull final Player player) { 99 | this(new Icon[page.row()][page.column()], page, player); 100 | } 101 | 102 | @NotNull 103 | @Override 104 | public Icon[][] all() { 105 | return this.contents.clone(); 106 | } 107 | 108 | @NotNull 109 | @Override 110 | public Map getProperties() { 111 | return Collections.unmodifiableMap(this.properties); 112 | } 113 | 114 | @Override 115 | public boolean isEditable(@NotNull final SlotPos slot) { 116 | return this.editableSlots.contains(slot); 117 | } 118 | 119 | @NotNull 120 | @Override 121 | public Optional iterator(@NotNull final String id) { 122 | return Optional.ofNullable(this.iterators.get(id)); 123 | } 124 | 125 | @NotNull 126 | @Override 127 | public SlotIterator newIterator(@NotNull final String id, @NotNull final SlotIterator.Type type, 128 | final int startRow, final int startColumn) { 129 | final var iterator = this.newIterator(type, startRow, startColumn); 130 | this.iterators.put(id, iterator); 131 | return iterator; 132 | } 133 | 134 | @NotNull 135 | @Override 136 | public Page page() { 137 | return this.page; 138 | } 139 | 140 | @NotNull 141 | @Override 142 | public Pagination pagination() { 143 | return this.pagination; 144 | } 145 | 146 | @NotNull 147 | @Override 148 | public Player player() { 149 | return this.player; 150 | } 151 | 152 | @NotNull 153 | @Override 154 | public InventoryContents set(final int row, final int column, @Nullable final Icon item) { 155 | if (row < 0 || row >= this.contents.length) { 156 | return this; 157 | } 158 | if (column < 0 || column >= this.contents[row].length) { 159 | return this; 160 | } 161 | this.contents[row][column] = item; 162 | if (item == null) { 163 | this.update(row, column, null); 164 | } else { 165 | this.update(row, column, item.calculateItem(this)); 166 | } 167 | return this; 168 | } 169 | 170 | @NotNull 171 | @Override 172 | public InventoryContents setEditable(@NotNull final SlotPos slot, final boolean editable) { 173 | if (editable) { 174 | this.editableSlots.add(slot); 175 | } else { 176 | this.editableSlots.remove(slot); 177 | } 178 | return this; 179 | } 180 | 181 | @NotNull 182 | @Override 183 | public InventoryContents setProperty(@NotNull final String name, @NotNull final Object value) { 184 | this.properties.put(name, value); 185 | return this; 186 | } 187 | 188 | @Override 189 | public void updateTitle(@NotNull final String newTitle) { 190 | TitleUpdater.updateInventory(this.player, newTitle); 191 | } 192 | 193 | /** 194 | * updates row and column of the inventory to the given item. 195 | * 196 | * @param row the row to update. 197 | * @param column the column to update. 198 | * @param item the item to update. 199 | */ 200 | private void update(final int row, final int column, @Nullable final ItemStack item) { 201 | if (SmartInventory.getOpenedPlayers(this.page).contains(this.player())) { 202 | this.getTopInventory().setItem(this.page.column() * row + column, item); 203 | } 204 | } 205 | } 206 | -------------------------------------------------------------------------------- /src/main/java/io/github/portlek/smartinventory/content/BasicPagination.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2021 Hasan Demirtaş 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | * 24 | */ 25 | 26 | package io.github.portlek.smartinventory.content; 27 | 28 | import io.github.portlek.smartinventory.Icon; 29 | import io.github.portlek.smartinventory.Pagination; 30 | import java.util.Arrays; 31 | import org.jetbrains.annotations.NotNull; 32 | 33 | /** 34 | * an implementation for {@link Pagination}. 35 | */ 36 | public final class BasicPagination implements Pagination { 37 | 38 | /** 39 | * the current page. 40 | */ 41 | private int currentPage; 42 | 43 | /** 44 | * the icons. 45 | */ 46 | @NotNull 47 | private Icon[] icons = new Icon[0]; 48 | 49 | /** 50 | * the icons per page. 51 | */ 52 | private int iconsPerPage = 5; 53 | 54 | @NotNull 55 | @Override 56 | public Pagination first() { 57 | this.currentPage = 0; 58 | return this; 59 | } 60 | 61 | @Override 62 | public int getPage() { 63 | return this.currentPage; 64 | } 65 | 66 | @NotNull 67 | @Override 68 | public Icon[] getPageIcons() { 69 | return Arrays.copyOfRange(this.icons, 70 | this.currentPage * this.iconsPerPage, 71 | (this.currentPage + 1) * this.iconsPerPage); 72 | } 73 | 74 | @Override 75 | public boolean isFirst() { 76 | return this.currentPage == 0; 77 | } 78 | 79 | @Override 80 | public boolean isLast() { 81 | return this.currentPage >= (int) Math.ceil((double) this.icons.length / (double) this.iconsPerPage) - 1; 82 | } 83 | 84 | @NotNull 85 | @Override 86 | public Pagination last() { 87 | return this.page(this.getPageIcons().length / this.iconsPerPage); 88 | } 89 | 90 | @NotNull 91 | @Override 92 | public Pagination next() { 93 | if (!this.isLast()) { 94 | this.currentPage++; 95 | } 96 | return this; 97 | } 98 | 99 | @NotNull 100 | @Override 101 | public Pagination page(final int page) { 102 | this.currentPage = page; 103 | return this; 104 | } 105 | 106 | @NotNull 107 | @Override 108 | public Pagination previous() { 109 | if (!this.isFirst()) { 110 | this.currentPage--; 111 | } 112 | return this; 113 | } 114 | 115 | @NotNull 116 | @Override 117 | public Pagination setIcons(@NotNull final Icon... icons) { 118 | this.icons = icons.clone(); 119 | return this; 120 | } 121 | 122 | @NotNull 123 | @Override 124 | public Pagination setIconsPerPage(final int iconsPerPage) { 125 | this.iconsPerPage = iconsPerPage; 126 | return this; 127 | } 128 | } 129 | -------------------------------------------------------------------------------- /src/main/java/io/github/portlek/smartinventory/content/package-info.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2021 Hasan Demirtaş 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | * 24 | */ 25 | /** 26 | * the package that contains {@link io.github.portlek.smartinventory.Page}'s contents. 27 | */ 28 | package io.github.portlek.smartinventory.content; 29 | -------------------------------------------------------------------------------- /src/main/java/io/github/portlek/smartinventory/event/IcClickEvent.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2021 Hasan Demirtaş 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | * 24 | */ 25 | 26 | package io.github.portlek.smartinventory.event; 27 | 28 | import io.github.portlek.smartinventory.Icon; 29 | import io.github.portlek.smartinventory.InventoryContents; 30 | import io.github.portlek.smartinventory.event.abs.ClickEvent; 31 | import java.util.Optional; 32 | import lombok.RequiredArgsConstructor; 33 | import org.bukkit.Bukkit; 34 | import org.bukkit.event.inventory.ClickType; 35 | import org.bukkit.event.inventory.InventoryAction; 36 | import org.bukkit.event.inventory.InventoryClickEvent; 37 | import org.bukkit.event.inventory.InventoryType; 38 | import org.bukkit.inventory.ItemStack; 39 | import org.bukkit.plugin.Plugin; 40 | import org.jetbrains.annotations.NotNull; 41 | 42 | /** 43 | * a class that represents icon click events. 44 | */ 45 | @RequiredArgsConstructor 46 | public final class IcClickEvent implements ClickEvent { 47 | 48 | /** 49 | * the contents. 50 | */ 51 | @NotNull 52 | private final InventoryContents contents; 53 | 54 | /** 55 | * the event. 56 | */ 57 | @NotNull 58 | private final InventoryClickEvent event; 59 | 60 | /** 61 | * the icon. 62 | */ 63 | @NotNull 64 | private final Icon icon; 65 | 66 | /** 67 | * the plugin. 68 | */ 69 | @NotNull 70 | private final Plugin plugin; 71 | 72 | @NotNull 73 | @Override 74 | public InventoryAction action() { 75 | return this.event.getAction(); 76 | } 77 | 78 | @NotNull 79 | @Override 80 | public ClickType click() { 81 | return this.event.getClick(); 82 | } 83 | 84 | @Override 85 | public int column() { 86 | return this.event.getSlot() % 9; 87 | } 88 | 89 | @NotNull 90 | @Override 91 | public Optional current() { 92 | return Optional.ofNullable(this.event.getCurrentItem()); 93 | } 94 | 95 | @NotNull 96 | @Override 97 | public Optional cursor() { 98 | return Optional.ofNullable(this.event.getCursor()); 99 | } 100 | 101 | @NotNull 102 | @Override 103 | public InventoryClickEvent getEvent() { 104 | return this.event; 105 | } 106 | 107 | @Override 108 | public int row() { 109 | return this.event.getSlot() / 9; 110 | } 111 | 112 | @NotNull 113 | @Override 114 | public InventoryType.SlotType slot() { 115 | return this.event.getSlotType(); 116 | } 117 | 118 | @Override 119 | public void cancel() { 120 | this.event.setCancelled(true); 121 | } 122 | 123 | @Override 124 | public void close() { 125 | Bukkit.getScheduler().runTask(this.plugin, () -> 126 | this.contents.page().close(this.contents.player())); 127 | } 128 | 129 | @NotNull 130 | @Override 131 | public InventoryContents contents() { 132 | return this.contents; 133 | } 134 | 135 | @NotNull 136 | @Override 137 | public Icon icon() { 138 | return this.icon; 139 | } 140 | } 141 | -------------------------------------------------------------------------------- /src/main/java/io/github/portlek/smartinventory/event/IcDragEvent.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2021 Hasan Demirtaş 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | * 24 | */ 25 | 26 | package io.github.portlek.smartinventory.event; 27 | 28 | import io.github.portlek.smartinventory.Icon; 29 | import io.github.portlek.smartinventory.InventoryContents; 30 | import io.github.portlek.smartinventory.event.abs.DragEvent; 31 | import java.util.Map; 32 | import java.util.Optional; 33 | import java.util.Set; 34 | import lombok.RequiredArgsConstructor; 35 | import org.bukkit.Bukkit; 36 | import org.bukkit.event.inventory.DragType; 37 | import org.bukkit.event.inventory.InventoryDragEvent; 38 | import org.bukkit.inventory.ItemStack; 39 | import org.bukkit.plugin.Plugin; 40 | import org.jetbrains.annotations.NotNull; 41 | 42 | /** 43 | * a class that represents icon drag events. 44 | */ 45 | @RequiredArgsConstructor 46 | public final class IcDragEvent implements DragEvent { 47 | 48 | /** 49 | * the contents. 50 | */ 51 | @NotNull 52 | private final InventoryContents contents; 53 | 54 | /** 55 | * the event. 56 | */ 57 | @NotNull 58 | private final InventoryDragEvent event; 59 | 60 | /** 61 | * the icon. 62 | */ 63 | @NotNull 64 | private final Icon icon; 65 | 66 | /** 67 | * the plugin. 68 | */ 69 | @NotNull 70 | private final Plugin plugin; 71 | 72 | @NotNull 73 | @Override 74 | public Map added() { 75 | return this.event.getNewItems(); 76 | } 77 | 78 | @NotNull 79 | @Override 80 | public DragType drag() { 81 | return this.event.getType(); 82 | } 83 | 84 | @NotNull 85 | @Override 86 | public InventoryDragEvent getEvent() { 87 | return this.event; 88 | } 89 | 90 | @NotNull 91 | @Override 92 | public Optional newCursor() { 93 | return Optional.ofNullable(this.event.getCursor()); 94 | } 95 | 96 | @NotNull 97 | @Override 98 | public Set slots() { 99 | return this.event.getInventorySlots(); 100 | } 101 | 102 | @Override 103 | public void cancel() { 104 | this.event.setCancelled(true); 105 | } 106 | 107 | @Override 108 | public void close() { 109 | Bukkit.getScheduler().runTask(this.plugin, () -> 110 | this.contents.page().close(this.contents.player())); 111 | } 112 | 113 | @NotNull 114 | @Override 115 | public InventoryContents contents() { 116 | return this.contents; 117 | } 118 | 119 | @NotNull 120 | @Override 121 | public Icon icon() { 122 | return this.icon; 123 | } 124 | } 125 | -------------------------------------------------------------------------------- /src/main/java/io/github/portlek/smartinventory/event/PgBottomClickEvent.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2021 Hasan Demirtaş 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | * 24 | */ 25 | 26 | package io.github.portlek.smartinventory.event; 27 | 28 | import io.github.portlek.smartinventory.InventoryContents; 29 | import io.github.portlek.smartinventory.event.abs.BottomClickEvent; 30 | import lombok.RequiredArgsConstructor; 31 | import org.bukkit.Bukkit; 32 | import org.bukkit.event.inventory.InventoryClickEvent; 33 | import org.bukkit.plugin.Plugin; 34 | import org.jetbrains.annotations.NotNull; 35 | 36 | /** 37 | * a class that represents page bottom click events. 38 | */ 39 | @RequiredArgsConstructor 40 | public final class PgBottomClickEvent implements BottomClickEvent { 41 | 42 | /** 43 | * the contents. 44 | */ 45 | @NotNull 46 | private final InventoryContents contents; 47 | 48 | /** 49 | * the event. 50 | */ 51 | @NotNull 52 | private final InventoryClickEvent event; 53 | 54 | /** 55 | * the plugin. 56 | */ 57 | @NotNull 58 | private final Plugin plugin; 59 | 60 | @Override 61 | public void cancel() { 62 | this.event.setCancelled(true); 63 | } 64 | 65 | @Override 66 | public void close() { 67 | Bukkit.getScheduler().runTask(this.plugin, () -> 68 | this.contents.page().close(this.contents.player())); 69 | } 70 | 71 | @NotNull 72 | @Override 73 | public InventoryContents contents() { 74 | return this.contents; 75 | } 76 | 77 | @NotNull 78 | @Override 79 | public InventoryClickEvent getEvent() { 80 | return this.event; 81 | } 82 | } 83 | -------------------------------------------------------------------------------- /src/main/java/io/github/portlek/smartinventory/event/PgClickEvent.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2021 Hasan Demirtaş 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | * 24 | */ 25 | 26 | package io.github.portlek.smartinventory.event; 27 | 28 | import io.github.portlek.smartinventory.InventoryContents; 29 | import io.github.portlek.smartinventory.event.abs.PageClickEvent; 30 | import lombok.RequiredArgsConstructor; 31 | import org.bukkit.Bukkit; 32 | import org.bukkit.event.inventory.InventoryClickEvent; 33 | import org.bukkit.plugin.Plugin; 34 | import org.jetbrains.annotations.NotNull; 35 | 36 | /** 37 | * a class that represents page click events. 38 | */ 39 | @RequiredArgsConstructor 40 | public final class PgClickEvent implements PageClickEvent { 41 | 42 | /** 43 | * the contents. 44 | */ 45 | @NotNull 46 | private final InventoryContents contents; 47 | 48 | /** 49 | * the event. 50 | */ 51 | @NotNull 52 | private final InventoryClickEvent event; 53 | 54 | /** 55 | * the plugins. 56 | */ 57 | @NotNull 58 | private final Plugin plugin; 59 | 60 | @Override 61 | public void cancel() { 62 | this.event.setCancelled(true); 63 | } 64 | 65 | @Override 66 | public void close() { 67 | Bukkit.getScheduler().runTask(this.plugin, () -> 68 | this.contents.page().close(this.contents.player())); 69 | } 70 | 71 | @NotNull 72 | @Override 73 | public InventoryContents contents() { 74 | return this.contents; 75 | } 76 | 77 | @NotNull 78 | @Override 79 | public InventoryClickEvent getEvent() { 80 | return this.event; 81 | } 82 | } 83 | -------------------------------------------------------------------------------- /src/main/java/io/github/portlek/smartinventory/event/PgCloseEvent.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2021 Hasan Demirtaş 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | * 24 | */ 25 | 26 | package io.github.portlek.smartinventory.event; 27 | 28 | import io.github.portlek.smartinventory.InventoryContents; 29 | import io.github.portlek.smartinventory.event.abs.CloseEvent; 30 | import lombok.RequiredArgsConstructor; 31 | import org.bukkit.event.inventory.InventoryCloseEvent; 32 | import org.jetbrains.annotations.NotNull; 33 | 34 | /** 35 | * a class that represents page close events. 36 | */ 37 | @RequiredArgsConstructor 38 | public final class PgCloseEvent implements CloseEvent { 39 | 40 | /** 41 | * the contents. 42 | */ 43 | @NotNull 44 | private final InventoryContents contents; 45 | 46 | /** 47 | * the event. 48 | */ 49 | @NotNull 50 | private final InventoryCloseEvent event; 51 | 52 | @NotNull 53 | @Override 54 | public InventoryContents contents() { 55 | return this.contents; 56 | } 57 | 58 | @NotNull 59 | @Override 60 | public InventoryCloseEvent getEvent() { 61 | return this.event; 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /src/main/java/io/github/portlek/smartinventory/event/PgInitEvent.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2021 Hasan Demirtaş 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | * 24 | */ 25 | 26 | package io.github.portlek.smartinventory.event; 27 | 28 | import io.github.portlek.smartinventory.InventoryContents; 29 | import io.github.portlek.smartinventory.event.abs.InitEvent; 30 | import lombok.RequiredArgsConstructor; 31 | import org.jetbrains.annotations.NotNull; 32 | 33 | /** 34 | * a class that represents page init events. 35 | */ 36 | @RequiredArgsConstructor 37 | public final class PgInitEvent implements InitEvent { 38 | 39 | /** 40 | * the contents. 41 | */ 42 | @NotNull 43 | private final InventoryContents contents; 44 | 45 | @NotNull 46 | @Override 47 | public InventoryContents contents() { 48 | return this.contents; 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /src/main/java/io/github/portlek/smartinventory/event/PgOpenEvent.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2021 Hasan Demirtaş 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | * 24 | */ 25 | 26 | package io.github.portlek.smartinventory.event; 27 | 28 | import io.github.portlek.smartinventory.InventoryContents; 29 | import io.github.portlek.smartinventory.event.abs.OpenEvent; 30 | import lombok.RequiredArgsConstructor; 31 | import org.bukkit.Bukkit; 32 | import org.bukkit.event.inventory.InventoryOpenEvent; 33 | import org.bukkit.plugin.Plugin; 34 | import org.jetbrains.annotations.NotNull; 35 | 36 | /** 37 | * a class that represents page open events. 38 | */ 39 | @RequiredArgsConstructor 40 | public final class PgOpenEvent implements OpenEvent { 41 | 42 | /** 43 | * the contents. 44 | */ 45 | @NotNull 46 | private final InventoryContents contents; 47 | 48 | /** 49 | * the event. 50 | */ 51 | @NotNull 52 | private final InventoryOpenEvent event; 53 | 54 | /** 55 | * the plugin. 56 | */ 57 | @NotNull 58 | private final Plugin plugin; 59 | 60 | @Override 61 | public void cancel() { 62 | this.event.setCancelled(true); 63 | } 64 | 65 | @Override 66 | public void close() { 67 | Bukkit.getScheduler().runTask(this.plugin, () -> 68 | this.contents.page().close(this.contents.player())); 69 | } 70 | 71 | @NotNull 72 | @Override 73 | public InventoryContents contents() { 74 | return this.contents; 75 | } 76 | 77 | @NotNull 78 | @Override 79 | public InventoryOpenEvent getEvent() { 80 | return this.event; 81 | } 82 | } 83 | -------------------------------------------------------------------------------- /src/main/java/io/github/portlek/smartinventory/event/PgOutsideClickEvent.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2021 Hasan Demirtaş 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | * 24 | */ 25 | 26 | package io.github.portlek.smartinventory.event; 27 | 28 | import io.github.portlek.smartinventory.InventoryContents; 29 | import io.github.portlek.smartinventory.event.abs.OutsideClickEvent; 30 | import lombok.RequiredArgsConstructor; 31 | import org.bukkit.Bukkit; 32 | import org.bukkit.event.inventory.InventoryClickEvent; 33 | import org.bukkit.plugin.Plugin; 34 | import org.jetbrains.annotations.NotNull; 35 | 36 | /** 37 | * a class that represents page outside click events. 38 | */ 39 | @RequiredArgsConstructor 40 | public final class PgOutsideClickEvent implements OutsideClickEvent { 41 | 42 | /** 43 | * the contents. 44 | */ 45 | @NotNull 46 | private final InventoryContents contents; 47 | 48 | /** 49 | * the event. 50 | */ 51 | @NotNull 52 | private final InventoryClickEvent event; 53 | 54 | /** 55 | * the plugin. 56 | */ 57 | @NotNull 58 | private final Plugin plugin; 59 | 60 | @Override 61 | public void cancel() { 62 | this.event.setCancelled(true); 63 | } 64 | 65 | @Override 66 | public void close() { 67 | Bukkit.getScheduler().runTask(this.plugin, () -> 68 | this.contents.page().close(this.contents.player())); 69 | } 70 | 71 | @NotNull 72 | @Override 73 | public InventoryContents contents() { 74 | return this.contents; 75 | } 76 | 77 | @NotNull 78 | @Override 79 | public InventoryClickEvent getEvent() { 80 | return this.event; 81 | } 82 | } 83 | -------------------------------------------------------------------------------- /src/main/java/io/github/portlek/smartinventory/event/PgTickEvent.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2021 Hasan Demirtaş 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | * 24 | */ 25 | 26 | package io.github.portlek.smartinventory.event; 27 | 28 | import io.github.portlek.smartinventory.InventoryContents; 29 | import io.github.portlek.smartinventory.event.abs.TickEvent; 30 | import lombok.RequiredArgsConstructor; 31 | import org.jetbrains.annotations.NotNull; 32 | 33 | /** 34 | * a class that represents page tick events. 35 | */ 36 | @RequiredArgsConstructor 37 | public final class PgTickEvent implements TickEvent { 38 | 39 | /** 40 | * the contents. 41 | */ 42 | @NotNull 43 | private final InventoryContents contents; 44 | 45 | @NotNull 46 | @Override 47 | public InventoryContents contents() { 48 | return this.contents; 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /src/main/java/io/github/portlek/smartinventory/event/PgUpdateEvent.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2021 Hasan Demirtaş 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | * 24 | */ 25 | 26 | package io.github.portlek.smartinventory.event; 27 | 28 | import io.github.portlek.smartinventory.InventoryContents; 29 | import io.github.portlek.smartinventory.event.abs.UpdateEvent; 30 | import lombok.RequiredArgsConstructor; 31 | import org.jetbrains.annotations.NotNull; 32 | 33 | /** 34 | * a class that represents page update events. 35 | */ 36 | @RequiredArgsConstructor 37 | public final class PgUpdateEvent implements UpdateEvent { 38 | 39 | /** 40 | * the contents. 41 | */ 42 | @NotNull 43 | private final InventoryContents contents; 44 | 45 | @NotNull 46 | @Override 47 | public InventoryContents contents() { 48 | return this.contents; 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /src/main/java/io/github/portlek/smartinventory/event/PlgnDisableEvent.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2021 Hasan Demirtaş 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | * 24 | */ 25 | 26 | package io.github.portlek.smartinventory.event; 27 | 28 | import io.github.portlek.smartinventory.InventoryContents; 29 | import io.github.portlek.smartinventory.event.abs.DisableEvent; 30 | import lombok.RequiredArgsConstructor; 31 | import org.bukkit.event.server.PluginDisableEvent; 32 | import org.jetbrains.annotations.NotNull; 33 | 34 | /** 35 | * a class that represents plugin disable events. 36 | */ 37 | @RequiredArgsConstructor 38 | public final class PlgnDisableEvent implements DisableEvent { 39 | 40 | /** 41 | * the contents. 42 | */ 43 | @NotNull 44 | private final InventoryContents contents; 45 | 46 | /** 47 | * the event. 48 | */ 49 | @NotNull 50 | private final PluginDisableEvent event; 51 | 52 | @NotNull 53 | @Override 54 | public InventoryContents contents() { 55 | return this.contents; 56 | } 57 | 58 | @NotNull 59 | @Override 60 | public PluginDisableEvent getEvent() { 61 | return this.event; 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /src/main/java/io/github/portlek/smartinventory/event/PlyrQuitEvent.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2021 Hasan Demirtaş 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | * 24 | */ 25 | 26 | package io.github.portlek.smartinventory.event; 27 | 28 | import io.github.portlek.smartinventory.InventoryContents; 29 | import io.github.portlek.smartinventory.event.abs.QuitEvent; 30 | import lombok.RequiredArgsConstructor; 31 | import org.bukkit.event.player.PlayerQuitEvent; 32 | import org.jetbrains.annotations.NotNull; 33 | 34 | /** 35 | * a class that represents player quit events. 36 | */ 37 | @RequiredArgsConstructor 38 | public final class PlyrQuitEvent implements QuitEvent { 39 | 40 | /** 41 | * the contents. 42 | */ 43 | @NotNull 44 | private final InventoryContents contents; 45 | 46 | /** 47 | * the event. 48 | */ 49 | @NotNull 50 | private final PlayerQuitEvent event; 51 | 52 | @NotNull 53 | @Override 54 | public InventoryContents contents() { 55 | return this.contents; 56 | } 57 | 58 | @NotNull 59 | @Override 60 | public PlayerQuitEvent getEvent() { 61 | return this.event; 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /src/main/java/io/github/portlek/smartinventory/event/abs/BottomClickEvent.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2021 Hasan Demirtaş 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | * 24 | */ 25 | 26 | package io.github.portlek.smartinventory.event.abs; 27 | 28 | /** 29 | * an interface to determine bottom click events. 30 | */ 31 | public interface BottomClickEvent extends PageClickEvent { 32 | 33 | } 34 | -------------------------------------------------------------------------------- /src/main/java/io/github/portlek/smartinventory/event/abs/ClickEvent.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2021 Hasan Demirtaş 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | * 24 | */ 25 | 26 | package io.github.portlek.smartinventory.event.abs; 27 | 28 | import java.util.Optional; 29 | import org.bukkit.event.inventory.ClickType; 30 | import org.bukkit.event.inventory.InventoryAction; 31 | import org.bukkit.event.inventory.InventoryClickEvent; 32 | import org.bukkit.event.inventory.InventoryType; 33 | import org.bukkit.inventory.ItemStack; 34 | import org.jetbrains.annotations.NotNull; 35 | 36 | /** 37 | * an interface to determine click events. 38 | */ 39 | public interface ClickEvent extends IconEvent { 40 | 41 | /** 42 | * obtains the action. 43 | * 44 | * @return action. 45 | */ 46 | @NotNull 47 | InventoryAction action(); 48 | 49 | /** 50 | * obtains the click. 51 | * 52 | * @return click. 53 | */ 54 | @NotNull 55 | ClickType click(); 56 | 57 | /** 58 | * obtains the column. 59 | * 60 | * @return column. 61 | */ 62 | int column(); 63 | 64 | /** 65 | * obtains the current. 66 | * 67 | * @return the current. 68 | */ 69 | @NotNull 70 | Optional current(); 71 | 72 | /** 73 | * obtains the cursor. 74 | * 75 | * @return cursor. 76 | */ 77 | @NotNull 78 | Optional cursor(); 79 | 80 | /** 81 | * obtains the event. 82 | * 83 | * @return event. 84 | */ 85 | @NotNull 86 | InventoryClickEvent getEvent(); 87 | 88 | /** 89 | * obtains the row. 90 | * 91 | * @return row. 92 | */ 93 | int row(); 94 | 95 | /** 96 | * obtains the slot. 97 | * 98 | * @return slot. 99 | */ 100 | @NotNull 101 | InventoryType.SlotType slot(); 102 | } 103 | -------------------------------------------------------------------------------- /src/main/java/io/github/portlek/smartinventory/event/abs/CloseEvent.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2021 Hasan Demirtaş 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | * 24 | */ 25 | 26 | package io.github.portlek.smartinventory.event.abs; 27 | 28 | import org.bukkit.event.inventory.InventoryCloseEvent; 29 | import org.jetbrains.annotations.NotNull; 30 | 31 | /** 32 | * an interface to determine close evet. 33 | */ 34 | public interface CloseEvent extends PageEvent { 35 | 36 | /** 37 | * obtains the event. 38 | * 39 | * @return event. 40 | */ 41 | @NotNull 42 | InventoryCloseEvent getEvent(); 43 | } 44 | -------------------------------------------------------------------------------- /src/main/java/io/github/portlek/smartinventory/event/abs/DisableEvent.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2021 Hasan Demirtaş 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | * 24 | */ 25 | 26 | package io.github.portlek.smartinventory.event.abs; 27 | 28 | import org.bukkit.event.server.PluginDisableEvent; 29 | import org.jetbrains.annotations.NotNull; 30 | 31 | /** 32 | * an interface to determine disable. 33 | */ 34 | public interface DisableEvent extends PageEvent { 35 | 36 | /** 37 | * obtains the event. 38 | * 39 | * @return event. 40 | */ 41 | @NotNull 42 | PluginDisableEvent getEvent(); 43 | } 44 | -------------------------------------------------------------------------------- /src/main/java/io/github/portlek/smartinventory/event/abs/DragEvent.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2021 Hasan Demirtaş 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | * 24 | */ 25 | 26 | package io.github.portlek.smartinventory.event.abs; 27 | 28 | import java.util.Map; 29 | import java.util.Optional; 30 | import java.util.Set; 31 | import org.bukkit.event.inventory.DragType; 32 | import org.bukkit.event.inventory.InventoryDragEvent; 33 | import org.bukkit.inventory.ItemStack; 34 | import org.jetbrains.annotations.NotNull; 35 | 36 | /** 37 | * an interface to determine drag events. 38 | */ 39 | public interface DragEvent extends IconEvent { 40 | 41 | /** 42 | * obtains the added. 43 | * 44 | * @return added. 45 | */ 46 | @NotNull 47 | Map added(); 48 | 49 | /** 50 | * obtains the drag. 51 | * 52 | * @return drag. 53 | */ 54 | @NotNull 55 | DragType drag(); 56 | 57 | /** 58 | * obtains the event. 59 | * 60 | * @return event. 61 | */ 62 | @NotNull 63 | InventoryDragEvent getEvent(); 64 | 65 | /** 66 | * obtains the new cursor. 67 | * 68 | * @return new cursor. 69 | */ 70 | @NotNull 71 | Optional newCursor(); 72 | 73 | /** 74 | * obtains the slots. 75 | * 76 | * @return slots. 77 | */ 78 | @NotNull 79 | Set slots(); 80 | } 81 | -------------------------------------------------------------------------------- /src/main/java/io/github/portlek/smartinventory/event/abs/IconEvent.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2021 Hasan Demirtaş 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | * 24 | */ 25 | 26 | package io.github.portlek.smartinventory.event.abs; 27 | 28 | import io.github.portlek.smartinventory.Icon; 29 | import org.jetbrains.annotations.NotNull; 30 | 31 | /** 32 | * an interface to determine icon events. 33 | */ 34 | public interface IconEvent extends SmartEvent { 35 | 36 | /** 37 | * obtains the icon. 38 | * 39 | * @return icon. 40 | */ 41 | @NotNull 42 | Icon icon(); 43 | } 44 | -------------------------------------------------------------------------------- /src/main/java/io/github/portlek/smartinventory/event/abs/InitEvent.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2021 Hasan Demirtaş 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | * 24 | */ 25 | 26 | package io.github.portlek.smartinventory.event.abs; 27 | 28 | /** 29 | * an interface to determine initiate event. 30 | */ 31 | public interface InitEvent extends PageEvent { 32 | 33 | } 34 | -------------------------------------------------------------------------------- /src/main/java/io/github/portlek/smartinventory/event/abs/OpenEvent.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2021 Hasan Demirtaş 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | * 24 | */ 25 | 26 | package io.github.portlek.smartinventory.event.abs; 27 | 28 | import org.bukkit.event.inventory.InventoryOpenEvent; 29 | import org.jetbrains.annotations.NotNull; 30 | 31 | /** 32 | * an interface to determine events. 33 | */ 34 | public interface OpenEvent extends PageEvent { 35 | 36 | /** 37 | * obtains the event. 38 | * 39 | * @return event. 40 | */ 41 | @NotNull 42 | InventoryOpenEvent getEvent(); 43 | } 44 | -------------------------------------------------------------------------------- /src/main/java/io/github/portlek/smartinventory/event/abs/OutsideClickEvent.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2021 Hasan Demirtaş 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | * 24 | */ 25 | 26 | package io.github.portlek.smartinventory.event.abs; 27 | 28 | /** 29 | * an interface to determine outside click events. 30 | */ 31 | public interface OutsideClickEvent extends PageClickEvent { 32 | 33 | } 34 | -------------------------------------------------------------------------------- /src/main/java/io/github/portlek/smartinventory/event/abs/PageClickEvent.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2021 Hasan Demirtaş 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | * 24 | */ 25 | 26 | package io.github.portlek.smartinventory.event.abs; 27 | 28 | import org.bukkit.event.inventory.InventoryClickEvent; 29 | import org.jetbrains.annotations.NotNull; 30 | 31 | /** 32 | * an interface to determine page click events. 33 | */ 34 | public interface PageClickEvent extends PageEvent { 35 | 36 | /** 37 | * obtains the event. 38 | * 39 | * @return event. 40 | */ 41 | @NotNull 42 | InventoryClickEvent getEvent(); 43 | } 44 | -------------------------------------------------------------------------------- /src/main/java/io/github/portlek/smartinventory/event/abs/PageEvent.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2021 Hasan Demirtaş 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | * 24 | */ 25 | 26 | package io.github.portlek.smartinventory.event.abs; 27 | 28 | /** 29 | * an interface to determine page events. 30 | */ 31 | public interface PageEvent extends SmartEvent { 32 | 33 | } 34 | -------------------------------------------------------------------------------- /src/main/java/io/github/portlek/smartinventory/event/abs/QuitEvent.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2021 Hasan Demirtaş 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | * 24 | */ 25 | 26 | package io.github.portlek.smartinventory.event.abs; 27 | 28 | import org.bukkit.event.player.PlayerQuitEvent; 29 | import org.jetbrains.annotations.NotNull; 30 | 31 | /** 32 | * an interface to determine quit events. 33 | */ 34 | public interface QuitEvent extends PageEvent { 35 | 36 | /** 37 | * obtains the event. 38 | * 39 | * @return event. 40 | */ 41 | @NotNull 42 | PlayerQuitEvent getEvent(); 43 | } 44 | -------------------------------------------------------------------------------- /src/main/java/io/github/portlek/smartinventory/event/abs/SmartEvent.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2021 Hasan Demirtaş 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | * 24 | */ 25 | 26 | package io.github.portlek.smartinventory.event.abs; 27 | 28 | import io.github.portlek.smartinventory.InventoryContents; 29 | import org.jetbrains.annotations.NotNull; 30 | 31 | /** 32 | * an interface to determine smart events. 33 | */ 34 | public interface SmartEvent { 35 | 36 | /** 37 | * cancels the vent. 38 | */ 39 | default void cancel() { 40 | } 41 | 42 | /** 43 | * closes the inventory. 44 | */ 45 | default void close() { 46 | } 47 | 48 | /** 49 | * obtains the contents. 50 | * 51 | * @return contents. 52 | */ 53 | @NotNull 54 | InventoryContents contents(); 55 | } 56 | -------------------------------------------------------------------------------- /src/main/java/io/github/portlek/smartinventory/event/abs/TickEvent.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2021 Hasan Demirtaş 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | * 24 | */ 25 | 26 | package io.github.portlek.smartinventory.event.abs; 27 | 28 | /** 29 | * an interface to determine tick events. 30 | */ 31 | public interface TickEvent extends PageEvent { 32 | 33 | } 34 | -------------------------------------------------------------------------------- /src/main/java/io/github/portlek/smartinventory/event/abs/UpdateEvent.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2021 Hasan Demirtaş 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | * 24 | */ 25 | 26 | package io.github.portlek.smartinventory.event.abs; 27 | 28 | /** 29 | * an interface to determine update events. 30 | */ 31 | public interface UpdateEvent extends PageEvent { 32 | 33 | } 34 | -------------------------------------------------------------------------------- /src/main/java/io/github/portlek/smartinventory/event/abs/package-info.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2021 Hasan Demirtaş 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | * 24 | */ 25 | /** 26 | * the package that contains interfaces of all the events. 27 | */ 28 | package io.github.portlek.smartinventory.event.abs; 29 | -------------------------------------------------------------------------------- /src/main/java/io/github/portlek/smartinventory/event/package-info.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2021 Hasan Demirtaş 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | * 24 | */ 25 | /** 26 | * the package that contains event classes. 27 | */ 28 | package io.github.portlek.smartinventory.event; 29 | -------------------------------------------------------------------------------- /src/main/java/io/github/portlek/smartinventory/handle/BasicHandle.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2021 Hasan Demirtaş 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | * 24 | */ 25 | 26 | package io.github.portlek.smartinventory.handle; 27 | 28 | import io.github.portlek.smartinventory.Handle; 29 | import io.github.portlek.smartinventory.event.abs.SmartEvent; 30 | import java.util.List; 31 | import java.util.function.Consumer; 32 | import java.util.function.Predicate; 33 | import lombok.RequiredArgsConstructor; 34 | import org.jetbrains.annotations.NotNull; 35 | 36 | /** 37 | * an implementation for {@link Handle}. 38 | * 39 | * @param type of the event. 40 | */ 41 | @RequiredArgsConstructor 42 | public final class BasicHandle implements Handle { 43 | 44 | /** 45 | * the class. 46 | */ 47 | @NotNull 48 | private final Class clazz; 49 | 50 | /** 51 | * the consumer. 52 | */ 53 | @NotNull 54 | private final Consumer consumer; 55 | 56 | /** 57 | * the requirements. 58 | */ 59 | @NotNull 60 | private final List> requirements; 61 | 62 | @Override 63 | public void accept(@NotNull final T t) { 64 | if (this.requirements.stream().allMatch(req -> req.test(t))) { 65 | this.consumer.accept(t); 66 | } 67 | } 68 | 69 | @NotNull 70 | @Override 71 | public Class type() { 72 | return this.clazz; 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /src/main/java/io/github/portlek/smartinventory/handle/package-info.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2021 Hasan Demirtaş 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | * 24 | */ 25 | /** 26 | * the package that contains {@link io.github.portlek.smartinventory.Handle} implementations. 27 | */ 28 | package io.github.portlek.smartinventory.handle; 29 | -------------------------------------------------------------------------------- /src/main/java/io/github/portlek/smartinventory/holder/SmartInventoryHolder.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2021 Hasan Demirtaş 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | * 24 | */ 25 | 26 | package io.github.portlek.smartinventory.holder; 27 | 28 | import io.github.portlek.smartinventory.InventoryContents; 29 | import io.github.portlek.smartinventory.Page; 30 | import io.github.portlek.smartinventory.SmartHolder; 31 | import lombok.Getter; 32 | import lombok.RequiredArgsConstructor; 33 | import lombok.Setter; 34 | import org.bukkit.entity.Player; 35 | import org.bukkit.inventory.Inventory; 36 | import org.bukkit.plugin.Plugin; 37 | import org.jetbrains.annotations.NotNull; 38 | 39 | /** 40 | * a class that implements {@link SmartHolder}. 41 | */ 42 | @Getter 43 | @RequiredArgsConstructor 44 | public final class SmartInventoryHolder implements SmartHolder { 45 | 46 | /** 47 | * the contents. 48 | */ 49 | @NotNull 50 | private final InventoryContents contents; 51 | 52 | /** 53 | * the active. 54 | */ 55 | @Setter 56 | private boolean active = true; 57 | 58 | @NotNull 59 | @Override 60 | public Inventory getInventory() { 61 | return this.contents.getTopInventory(); 62 | } 63 | 64 | @NotNull 65 | @Override 66 | public Page getPage() { 67 | return this.contents.page(); 68 | } 69 | 70 | @NotNull 71 | @Override 72 | public Player getPlayer() { 73 | return this.contents.player(); 74 | } 75 | 76 | @NotNull 77 | @Override 78 | public Plugin getPlugin() { 79 | return this.getPage().inventory().getPlugin(); 80 | } 81 | } 82 | -------------------------------------------------------------------------------- /src/main/java/io/github/portlek/smartinventory/holder/package-info.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2021 Hasan Demirtaş 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | * 24 | */ 25 | /** 26 | * the package that contains implementation of {@link io.github.portlek.smartinventory.SmartHolder}. 27 | */ 28 | package io.github.portlek.smartinventory.holder; 29 | -------------------------------------------------------------------------------- /src/main/java/io/github/portlek/smartinventory/icon/BasicIcon.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2021 Hasan Demirtaş 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | * 24 | */ 25 | 26 | package io.github.portlek.smartinventory.icon; 27 | 28 | import io.github.portlek.smartinventory.Handle; 29 | import io.github.portlek.smartinventory.Icon; 30 | import io.github.portlek.smartinventory.InventoryContents; 31 | import io.github.portlek.smartinventory.event.abs.IconEvent; 32 | import java.util.ArrayList; 33 | import java.util.Collection; 34 | import java.util.function.Predicate; 35 | import lombok.RequiredArgsConstructor; 36 | import org.bukkit.Material; 37 | import org.bukkit.inventory.ItemStack; 38 | import org.jetbrains.annotations.NotNull; 39 | 40 | /** 41 | * an implementation for {@link Icon}. 42 | */ 43 | @RequiredArgsConstructor 44 | public final class BasicIcon implements Icon { 45 | 46 | /** 47 | * the handle list. 48 | */ 49 | private final Collection> handles = new ArrayList<>(); 50 | 51 | /** 52 | * the can see. 53 | */ 54 | @NotNull 55 | private Predicate canSee = contents -> true; 56 | 57 | /** 58 | * the can use. 59 | */ 60 | @NotNull 61 | private Predicate canUse = contents -> true; 62 | 63 | /** 64 | * the fallback. 65 | */ 66 | @NotNull 67 | private ItemStack fallback = new ItemStack(Material.AIR); 68 | 69 | /** 70 | * the item. 71 | */ 72 | @NotNull 73 | private ItemStack item; 74 | 75 | @Override 76 | public void accept(@NotNull final T event) { 77 | final var contents = event.contents(); 78 | if (this.canSee.test(contents) && this.canUse.test(contents)) { 79 | this.handles.stream() 80 | .filter(target -> target.type().isAssignableFrom(event.getClass())) 81 | .map(target -> (Handle) target) 82 | .forEach(target -> target.accept(event)); 83 | } 84 | } 85 | 86 | @NotNull 87 | @Override 88 | public ItemStack calculateItem(@NotNull final InventoryContents contents) { 89 | final ItemStack calculated; 90 | if (this.canSee.test(contents)) { 91 | calculated = this.getItem(); 92 | } else { 93 | calculated = this.fallback; 94 | } 95 | return calculated; 96 | } 97 | 98 | @NotNull 99 | @Override 100 | public Icon canSee(@NotNull final Predicate predicate) { 101 | this.canSee = predicate; 102 | return this; 103 | } 104 | 105 | @NotNull 106 | @Override 107 | public Icon canUse(@NotNull final Predicate predicate) { 108 | this.canUse = predicate; 109 | return this; 110 | } 111 | 112 | @NotNull 113 | @Override 114 | public Icon fallback(@NotNull final ItemStack fallback) { 115 | this.fallback = fallback; 116 | return this; 117 | } 118 | 119 | @NotNull 120 | @Override 121 | public ItemStack getItem() { 122 | return this.item; 123 | } 124 | 125 | @NotNull 126 | @Override 127 | public Icon handle(@NotNull final Handle handle) { 128 | this.handles.add(handle); 129 | return this; 130 | } 131 | 132 | @NotNull 133 | @Override 134 | public Icon handles(@NotNull final Collection> handles) { 135 | this.handles.addAll(handles); 136 | return this; 137 | } 138 | 139 | @NotNull 140 | @Override 141 | public Icon item(@NotNull final ItemStack item) { 142 | this.item = item; 143 | return this; 144 | } 145 | } 146 | -------------------------------------------------------------------------------- /src/main/java/io/github/portlek/smartinventory/icon/package-info.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2021 Hasan Demirtaş 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | * 24 | */ 25 | /** 26 | * the package that contains {@link io.github.portlek.smartinventory.Icon} implementations. 27 | */ 28 | package io.github.portlek.smartinventory.icon; 29 | -------------------------------------------------------------------------------- /src/main/java/io/github/portlek/smartinventory/listener/InventoryClickListener.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2021 Hasan Demirtaş 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | * 24 | */ 25 | 26 | package io.github.portlek.smartinventory.listener; 27 | 28 | import io.github.portlek.smartinventory.SmartHolder; 29 | import io.github.portlek.smartinventory.event.IcClickEvent; 30 | import io.github.portlek.smartinventory.event.PgBottomClickEvent; 31 | import io.github.portlek.smartinventory.event.PgClickEvent; 32 | import io.github.portlek.smartinventory.event.PgOutsideClickEvent; 33 | import io.github.portlek.smartinventory.util.SlotPos; 34 | import org.bukkit.Material; 35 | import org.bukkit.entity.Player; 36 | import org.bukkit.event.EventHandler; 37 | import org.bukkit.event.Listener; 38 | import org.bukkit.event.inventory.InventoryAction; 39 | import org.bukkit.event.inventory.InventoryClickEvent; 40 | 41 | /** 42 | * a class that represents inventory click listeners. 43 | */ 44 | public final class InventoryClickListener implements Listener { 45 | 46 | /** 47 | * listens inventory click events. 48 | * 49 | * @param event the event to listen. 50 | */ 51 | @EventHandler 52 | public void onInventoryClick(final InventoryClickEvent event) { 53 | final var holder = event.getInventory().getHolder(); 54 | if (!(holder instanceof SmartHolder)) { 55 | return; 56 | } 57 | final var smartHolder = (SmartHolder) holder; 58 | if (event.getAction() == InventoryAction.COLLECT_TO_CURSOR) { 59 | event.setCancelled(true); 60 | return; 61 | } 62 | final var page = smartHolder.getPage(); 63 | final var contents = smartHolder.getContents(); 64 | final var plugin = smartHolder.getPlugin(); 65 | final var clicked = event.getClickedInventory(); 66 | if (clicked == null) { 67 | page.accept(new PgOutsideClickEvent(contents, event, plugin)); 68 | return; 69 | } 70 | final var player = event.getWhoClicked(); 71 | if (clicked.equals(player.getOpenInventory().getBottomInventory())) { 72 | page.accept(new PgBottomClickEvent(contents, event, plugin)); 73 | return; 74 | } 75 | final var current = event.getCurrentItem(); 76 | if (current == null || current.getType() == Material.AIR) { 77 | page.accept(new PgClickEvent(contents, event, plugin)); 78 | return; 79 | } 80 | final var slot = event.getSlot(); 81 | final var row = slot / 9; 82 | final var column = slot % 9; 83 | if (!page.checkBounds(row, column)) { 84 | return; 85 | } 86 | final var slotPos = SlotPos.of(row, column); 87 | if (!contents.isEditable(slotPos)) { 88 | event.setCancelled(true); 89 | } 90 | contents.get(slotPos).ifPresent(item -> 91 | item.accept(new IcClickEvent(contents, event, item, plugin))); 92 | if (!contents.isEditable(slotPos) && player instanceof Player) { 93 | ((Player) player).updateInventory(); 94 | } 95 | } 96 | } 97 | -------------------------------------------------------------------------------- /src/main/java/io/github/portlek/smartinventory/listener/InventoryCloseListener.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2021 Hasan Demirtaş 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | * 24 | */ 25 | 26 | package io.github.portlek.smartinventory.listener; 27 | 28 | import io.github.portlek.smartinventory.SmartHolder; 29 | import io.github.portlek.smartinventory.event.PgCloseEvent; 30 | import java.util.UUID; 31 | import java.util.function.Consumer; 32 | import lombok.RequiredArgsConstructor; 33 | import org.bukkit.Bukkit; 34 | import org.bukkit.event.EventHandler; 35 | import org.bukkit.event.Listener; 36 | import org.bukkit.event.inventory.InventoryCloseEvent; 37 | import org.jetbrains.annotations.NotNull; 38 | 39 | /** 40 | * a class that represents inventory close listeners. 41 | */ 42 | @RequiredArgsConstructor 43 | public final class InventoryCloseListener implements Listener { 44 | 45 | /** 46 | * the stop tick function. 47 | */ 48 | @NotNull 49 | private final Consumer stopTickFunction; 50 | 51 | /** 52 | * listens inventory close events. 53 | * 54 | * @param event the event to listen. 55 | */ 56 | @EventHandler 57 | public void onInventoryClose(final InventoryCloseEvent event) { 58 | final var holder = event.getInventory().getHolder(); 59 | if (!(holder instanceof SmartHolder)) { 60 | return; 61 | } 62 | final var smartHolder = (SmartHolder) holder; 63 | final var inventory = event.getInventory(); 64 | final var page = smartHolder.getPage(); 65 | final var close = new PgCloseEvent(smartHolder.getContents(), event); 66 | page.accept(close); 67 | if (!page.canClose(close)) { 68 | Bukkit.getScheduler().runTask(smartHolder.getPlugin(), () -> 69 | event.getPlayer().openInventory(inventory)); 70 | return; 71 | } 72 | inventory.clear(); 73 | this.stopTickFunction.accept(event.getPlayer().getUniqueId()); 74 | } 75 | } 76 | -------------------------------------------------------------------------------- /src/main/java/io/github/portlek/smartinventory/listener/InventoryDragListener.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2021 Hasan Demirtaş 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | * 24 | */ 25 | 26 | package io.github.portlek.smartinventory.listener; 27 | 28 | import io.github.portlek.smartinventory.SmartHolder; 29 | import io.github.portlek.smartinventory.event.IcDragEvent; 30 | import io.github.portlek.smartinventory.util.SlotPos; 31 | import org.bukkit.event.EventHandler; 32 | import org.bukkit.event.EventPriority; 33 | import org.bukkit.event.Listener; 34 | import org.bukkit.event.inventory.InventoryDragEvent; 35 | 36 | /** 37 | * a class that represents inventory drag listeners. 38 | */ 39 | public final class InventoryDragListener implements Listener { 40 | 41 | /** 42 | * listens inventory drag events. 43 | * 44 | * @param event the event to listen. 45 | */ 46 | @EventHandler(priority = EventPriority.LOW) 47 | public void onInventoryDrag(final InventoryDragEvent event) { 48 | final var holder = event.getInventory().getHolder(); 49 | if (!(holder instanceof SmartHolder)) { 50 | return; 51 | } 52 | final var smartHolder = (SmartHolder) holder; 53 | final var inventory = event.getInventory(); 54 | final var contents = smartHolder.getContents(); 55 | for (final var slot : event.getRawSlots()) { 56 | final var pos = SlotPos.of(slot / 9, slot % 9); 57 | contents.get(pos).ifPresent(icon -> 58 | icon.accept(new IcDragEvent(contents, event, icon, smartHolder.getPlugin()))); 59 | if (slot >= inventory.getSize() || contents.isEditable(pos)) { 60 | continue; 61 | } 62 | event.setCancelled(true); 63 | break; 64 | } 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /src/main/java/io/github/portlek/smartinventory/listener/InventoryOpenListener.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2021 Hasan Demirtaş 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | * 24 | */ 25 | 26 | package io.github.portlek.smartinventory.listener; 27 | 28 | import io.github.portlek.smartinventory.SmartHolder; 29 | import io.github.portlek.smartinventory.event.PgOpenEvent; 30 | import org.bukkit.event.EventHandler; 31 | import org.bukkit.event.Listener; 32 | import org.bukkit.event.inventory.InventoryOpenEvent; 33 | 34 | /** 35 | * a class that represents inventory open listeners. 36 | */ 37 | public final class InventoryOpenListener implements Listener { 38 | 39 | /** 40 | * listens the inventory open events. 41 | * 42 | * @param event the event to listen. 43 | */ 44 | @EventHandler 45 | public void onInventoryOpen(final InventoryOpenEvent event) { 46 | final var holder = event.getInventory().getHolder(); 47 | if (!(holder instanceof SmartHolder)) { 48 | return; 49 | } 50 | final var smartHolder = (SmartHolder) holder; 51 | smartHolder.getPage().accept(new PgOpenEvent(smartHolder.getContents(), event, smartHolder.getPlugin())); 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /src/main/java/io/github/portlek/smartinventory/listener/PlayerQuitListener.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2021 Hasan Demirtaş 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | * 24 | */ 25 | 26 | package io.github.portlek.smartinventory.listener; 27 | 28 | import io.github.portlek.smartinventory.SmartInventory; 29 | import io.github.portlek.smartinventory.event.PlyrQuitEvent; 30 | import java.util.UUID; 31 | import java.util.function.Consumer; 32 | import lombok.RequiredArgsConstructor; 33 | import org.bukkit.event.EventHandler; 34 | import org.bukkit.event.Listener; 35 | import org.bukkit.event.player.PlayerQuitEvent; 36 | import org.jetbrains.annotations.NotNull; 37 | 38 | /** 39 | * a class that represents player quit listeners. 40 | */ 41 | @RequiredArgsConstructor 42 | public final class PlayerQuitListener implements Listener { 43 | 44 | /** 45 | * the stop tick function. 46 | */ 47 | @NotNull 48 | private final Consumer stopTickFunction; 49 | 50 | /** 51 | * listens the player quit event. 52 | * 53 | * @param event the event to listen. 54 | */ 55 | @EventHandler 56 | public void onPlayerQuit(final PlayerQuitEvent event) { 57 | SmartInventory.getHolder(event.getPlayer()).ifPresent(holder -> { 58 | holder.getPage().accept(new PlyrQuitEvent(holder.getContents(), event)); 59 | this.stopTickFunction.accept(event.getPlayer().getUniqueId()); 60 | }); 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /src/main/java/io/github/portlek/smartinventory/listener/PluginDisableListener.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2021 Hasan Demirtaş 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | * 24 | */ 25 | 26 | package io.github.portlek.smartinventory.listener; 27 | 28 | import io.github.portlek.smartinventory.SmartInventory; 29 | import io.github.portlek.smartinventory.event.PlgnDisableEvent; 30 | import org.bukkit.event.EventHandler; 31 | import org.bukkit.event.Listener; 32 | import org.bukkit.event.server.PluginDisableEvent; 33 | 34 | /** 35 | * a class that represents plugin disable events. 36 | */ 37 | public final class PluginDisableListener implements Listener { 38 | 39 | /** 40 | * listens the plugin disable events. 41 | * 42 | * @param event the event to listen. 43 | */ 44 | @EventHandler 45 | public void onPluginDisable(final PluginDisableEvent event) { 46 | SmartInventory.getHolders().forEach(holder -> { 47 | final var page = holder.getPage(); 48 | page.accept(new PlgnDisableEvent(holder.getContents(), event)); 49 | page.close(holder.getPlayer()); 50 | }); 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /src/main/java/io/github/portlek/smartinventory/listener/package-info.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2021 Hasan Demirtaş 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | * 24 | */ 25 | /** 26 | * the package that contains {@link org.bukkit.event.Listener} classes. 27 | */ 28 | package io.github.portlek.smartinventory.listener; 29 | -------------------------------------------------------------------------------- /src/main/java/io/github/portlek/smartinventory/manager/BasicSmartInventory.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2021 Hasan Demirtaş 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | * 24 | */ 25 | 26 | package io.github.portlek.smartinventory.manager; 27 | 28 | import io.github.portlek.smartinventory.InventoryOpener; 29 | import io.github.portlek.smartinventory.SmartInventory; 30 | import java.util.ArrayList; 31 | import java.util.Collection; 32 | import java.util.Map; 33 | import java.util.UUID; 34 | import java.util.concurrent.ConcurrentHashMap; 35 | import lombok.Getter; 36 | import lombok.RequiredArgsConstructor; 37 | import org.bukkit.plugin.Plugin; 38 | import org.bukkit.scheduler.BukkitRunnable; 39 | import org.jetbrains.annotations.NotNull; 40 | 41 | /** 42 | * an implementation for {@link SmartInventory}. 43 | */ 44 | @Getter 45 | @RequiredArgsConstructor 46 | public final class BasicSmartInventory implements SmartInventory { 47 | 48 | /** 49 | * the openers. 50 | */ 51 | private final Collection openers = new ArrayList<>(); 52 | 53 | /** 54 | * the plugin. 55 | */ 56 | @NotNull 57 | private final Plugin plugin; 58 | 59 | /** 60 | * the tasks. 61 | */ 62 | private final Map tasks = new ConcurrentHashMap<>(); 63 | 64 | static { 65 | try { 66 | Class.forName("io.github.portlek.smartinventory.event.PlgnDisableEvent"); 67 | } catch (final ClassNotFoundException e) { 68 | e.printStackTrace(); 69 | } 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /src/main/java/io/github/portlek/smartinventory/manager/package-info.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2021 Hasan Demirtaş 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | * 24 | */ 25 | /** 26 | * the package that contains {@link io.github.portlek.smartinventory.SmartInventory} implementations. 27 | */ 28 | package io.github.portlek.smartinventory.manager; 29 | -------------------------------------------------------------------------------- /src/main/java/io/github/portlek/smartinventory/opener/ChestInventoryOpener.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2021 Hasan Demirtaş 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | * 24 | */ 25 | 26 | package io.github.portlek.smartinventory.opener; 27 | 28 | import io.github.portlek.smartinventory.InventoryContents; 29 | import io.github.portlek.smartinventory.InventoryOpener; 30 | import io.github.portlek.smartinventory.holder.SmartInventoryHolder; 31 | import org.bukkit.Bukkit; 32 | import org.bukkit.event.inventory.InventoryType; 33 | import org.bukkit.inventory.Inventory; 34 | import org.jetbrains.annotations.NotNull; 35 | 36 | /** 37 | * an {@link InventoryType#CHEST} implementation for {@link InventoryOpener}. 38 | */ 39 | public final class ChestInventoryOpener implements InventoryOpener { 40 | 41 | @NotNull 42 | @Override 43 | public Inventory open(@NotNull final InventoryContents contents) { 44 | final var page = contents.page(); 45 | if (page.column() != 9) { 46 | throw new IllegalArgumentException( 47 | String.format("The column count for the chest inventory must be 9, found: %s.", page.column())); 48 | } 49 | if (page.row() < 1 && page.row() > 6) { 50 | throw new IllegalArgumentException( 51 | String.format("The row count for the chest inventory must be between 1 and 6, found: %s", page.row())); 52 | } 53 | final var holder = new SmartInventoryHolder(contents); 54 | holder.setActive(true); 55 | final var handle = Bukkit.createInventory(holder, page.row() * page.column(), page.title()); 56 | this.fill(handle, contents); 57 | contents.player().openInventory(handle); 58 | return handle; 59 | } 60 | 61 | @Override 62 | public boolean supports(@NotNull final InventoryType type) { 63 | return type == InventoryType.CHEST || type == InventoryType.ENDER_CHEST; 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /src/main/java/io/github/portlek/smartinventory/opener/package-info.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2021 Hasan Demirtaş 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | * 24 | */ 25 | /** 26 | * the package that contains {@link io.github.portlek.smartinventory.InventoryOpener} implementations. 27 | */ 28 | package io.github.portlek.smartinventory.opener; 29 | -------------------------------------------------------------------------------- /src/main/java/io/github/portlek/smartinventory/package-info.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2021 Hasan Demirtaş 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | * 24 | */ 25 | /** 26 | * the package that contains common interfaces. 27 | */ 28 | package io.github.portlek.smartinventory; 29 | -------------------------------------------------------------------------------- /src/main/java/io/github/portlek/smartinventory/page/BasicPage.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2021 Hasan Demirtaş 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | * 24 | */ 25 | 26 | package io.github.portlek.smartinventory.page; 27 | 28 | import io.github.portlek.observer.Source; 29 | import io.github.portlek.observer.source.BasicSource; 30 | import io.github.portlek.smartinventory.Handle; 31 | import io.github.portlek.smartinventory.InventoryContents; 32 | import io.github.portlek.smartinventory.InventoryProvider; 33 | import io.github.portlek.smartinventory.Page; 34 | import io.github.portlek.smartinventory.SmartInventory; 35 | import io.github.portlek.smartinventory.content.BasicInventoryContents; 36 | import io.github.portlek.smartinventory.event.PgCloseEvent; 37 | import io.github.portlek.smartinventory.event.PgInitEvent; 38 | import io.github.portlek.smartinventory.event.PgUpdateEvent; 39 | import io.github.portlek.smartinventory.event.abs.CloseEvent; 40 | import io.github.portlek.smartinventory.event.abs.PageEvent; 41 | import java.util.ArrayList; 42 | import java.util.Collection; 43 | import java.util.Map; 44 | import java.util.Optional; 45 | import java.util.function.Predicate; 46 | import lombok.RequiredArgsConstructor; 47 | import org.bukkit.entity.Player; 48 | import org.bukkit.event.inventory.InventoryCloseEvent; 49 | import org.bukkit.event.inventory.InventoryType; 50 | import org.bukkit.inventory.Inventory; 51 | import org.jetbrains.annotations.NotNull; 52 | import org.jetbrains.annotations.Nullable; 53 | 54 | /** 55 | * an implementation for {@link Page}. 56 | */ 57 | @RequiredArgsConstructor 58 | public final class BasicPage implements Page { 59 | 60 | /** 61 | * the handles. 62 | */ 63 | private final Collection> handles = new ArrayList<>(); 64 | 65 | /** 66 | * the inventory manager. 67 | */ 68 | @NotNull 69 | private final SmartInventory inventory; 70 | 71 | /** 72 | * the observer's source. 73 | */ 74 | private final Source source = new BasicSource<>(); 75 | 76 | /** 77 | * the inventory type. 78 | * 79 | * @todo #1:5m Add a method to change the type of the inventory. 80 | */ 81 | @NotNull 82 | private final InventoryType type = InventoryType.CHEST; 83 | 84 | /** 85 | * the async. 86 | */ 87 | private boolean async = false; 88 | 89 | /** 90 | * the can close. 91 | */ 92 | @NotNull 93 | private Predicate canClose = event -> true; 94 | 95 | /** 96 | * the column. 97 | */ 98 | private int column = 9; 99 | 100 | /** 101 | * the id. 102 | */ 103 | @NotNull 104 | private String id = "none"; 105 | 106 | /** 107 | * the parent. 108 | */ 109 | @Nullable 110 | private Page parent; 111 | 112 | /** 113 | * the provider. 114 | */ 115 | @NotNull 116 | private InventoryProvider provider; 117 | 118 | /** 119 | * the row. 120 | */ 121 | private int row = 1; 122 | 123 | /** 124 | * the start delay time. 125 | */ 126 | private long startDelay = 1L; 127 | 128 | /** 129 | * the tick time. 130 | */ 131 | private long tick = 1L; 132 | 133 | /** 134 | * the tick enable. 135 | */ 136 | private boolean tickEnable = true; 137 | 138 | /** 139 | * the title. 140 | */ 141 | @NotNull 142 | private String title = "Smart Inventory"; 143 | 144 | /** 145 | * ctor. 146 | * 147 | * @param inventory the inventory. 148 | */ 149 | public BasicPage(@NotNull final SmartInventory inventory) { 150 | this(inventory, InventoryProvider.EMPTY); 151 | } 152 | 153 | @Override 154 | public void accept(@NotNull final T event) { 155 | this.handles.stream() 156 | .filter(handle -> handle.type().isAssignableFrom(event.getClass())) 157 | .map(handle -> (Handle) handle) 158 | .forEach(handle -> handle.accept(event)); 159 | } 160 | 161 | @Override 162 | public boolean async() { 163 | return this.async; 164 | } 165 | 166 | @NotNull 167 | @Override 168 | public Page async(final boolean async) { 169 | this.async = async; 170 | return this; 171 | } 172 | 173 | @Override 174 | public boolean canClose(@NotNull final CloseEvent event) { 175 | return this.canClose.test(event); 176 | } 177 | 178 | @NotNull 179 | @Override 180 | public Page canClose(@NotNull final Predicate predicate) { 181 | this.canClose = predicate; 182 | return this; 183 | } 184 | 185 | @Override 186 | public void close(@NotNull final Player player) { 187 | SmartInventory.getHolder(player).ifPresent(holder -> { 188 | this.accept(new PgCloseEvent(holder.getContents(), new InventoryCloseEvent(player.getOpenInventory()))); 189 | this.inventory().stopTick(player.getUniqueId()); 190 | this.source.unsubscribe(this.provider()); 191 | holder.setActive(false); 192 | player.closeInventory(); 193 | }); 194 | } 195 | 196 | @Override 197 | public int column() { 198 | return this.column; 199 | } 200 | 201 | @NotNull 202 | @Override 203 | public Page column(final int column) { 204 | this.column = column; 205 | return this; 206 | } 207 | 208 | @NotNull 209 | @Override 210 | public Page handle(@NotNull final Handle handle) { 211 | this.handles.add(handle); 212 | return this; 213 | } 214 | 215 | @NotNull 216 | @Override 217 | public Page id(@NotNull final String id) { 218 | this.id = id; 219 | return this; 220 | } 221 | 222 | @NotNull 223 | @Override 224 | public String id() { 225 | return this.id; 226 | } 227 | 228 | @NotNull 229 | @Override 230 | public SmartInventory inventory() { 231 | return this.inventory; 232 | } 233 | 234 | @Override 235 | public void notifyUpdate(@NotNull final InventoryContents contents) { 236 | this.accept(new PgUpdateEvent(contents)); 237 | this.source.notifyTargets(contents); 238 | } 239 | 240 | @NotNull 241 | @Override 242 | public Inventory open(@NotNull final Player player, final int page, @NotNull final Map properties, 243 | final boolean close) { 244 | if (close) { 245 | this.close(player); 246 | } 247 | final var opener = this.inventory().findOpener(this.type).orElseThrow(() -> 248 | new IllegalStateException("No opener found for the inventory type " + this.type.name())); 249 | this.source.subscribe(this.provider()); 250 | final var contents = new BasicInventoryContents(this, player); 251 | contents.pagination().page(page); 252 | properties.forEach(contents::setProperty); 253 | this.accept(new PgInitEvent(contents)); 254 | this.provider().init(contents); 255 | final var opened = opener.open(contents); 256 | if (this.tickEnable()) { 257 | this.inventory().tick(player.getUniqueId(), this); 258 | } 259 | return opened; 260 | } 261 | 262 | @NotNull 263 | @Override 264 | public Optional parent() { 265 | return Optional.ofNullable(this.parent); 266 | } 267 | 268 | @NotNull 269 | @Override 270 | public Page parent(@NotNull final Page parent) { 271 | this.parent = parent; 272 | return this; 273 | } 274 | 275 | @NotNull 276 | @Override 277 | public InventoryProvider provider() { 278 | return this.provider; 279 | } 280 | 281 | @NotNull 282 | @Override 283 | public Page provider(@NotNull final InventoryProvider provider) { 284 | this.provider = provider; 285 | return this; 286 | } 287 | 288 | @Override 289 | public int row() { 290 | return this.row; 291 | } 292 | 293 | @NotNull 294 | @Override 295 | public Page row(final int row) { 296 | this.row = row; 297 | return this; 298 | } 299 | 300 | @Override 301 | public long startDelay() { 302 | return this.startDelay; 303 | } 304 | 305 | @NotNull 306 | @Override 307 | public Page startDelay(final long startDelay) { 308 | this.startDelay = startDelay; 309 | return this; 310 | } 311 | 312 | @Override 313 | public long tick() { 314 | return this.tick; 315 | } 316 | 317 | @NotNull 318 | @Override 319 | public Page tick(final long tick) { 320 | this.tick = tick; 321 | return this; 322 | } 323 | 324 | @Override 325 | public boolean tickEnable() { 326 | return this.tickEnable; 327 | } 328 | 329 | @NotNull 330 | @Override 331 | public Page tickEnable(final boolean tickEnable) { 332 | this.tickEnable = tickEnable; 333 | return this; 334 | } 335 | 336 | @NotNull 337 | @Override 338 | public String title() { 339 | return this.title; 340 | } 341 | 342 | @NotNull 343 | @Override 344 | public Page title(@NotNull final String title) { 345 | this.title = title; 346 | return this; 347 | } 348 | } 349 | -------------------------------------------------------------------------------- /src/main/java/io/github/portlek/smartinventory/page/package-info.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2021 Hasan Demirtaş 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | * 24 | */ 25 | /** 26 | * the package that contains {@link io.github.portlek.smartinventory.Page} implementations. 27 | */ 28 | package io.github.portlek.smartinventory.page; 29 | -------------------------------------------------------------------------------- /src/main/java/io/github/portlek/smartinventory/util/Pattern.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2021 Hasan Demirtaş 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | * 24 | */ 25 | 26 | package io.github.portlek.smartinventory.util; 27 | 28 | import com.google.common.base.Preconditions; 29 | import java.util.ArrayList; 30 | import java.util.HashMap; 31 | import java.util.List; 32 | import java.util.Map; 33 | import java.util.Optional; 34 | import java.util.stream.IntStream; 35 | import org.jetbrains.annotations.NotNull; 36 | import org.jetbrains.annotations.Nullable; 37 | 38 | /** 39 | * a class representing a pattern with arbitrary keys and values. 40 | * 41 | * @param the type of the values that will be associated with the character keys. 42 | */ 43 | public final class Pattern { 44 | 45 | /** 46 | * the lines. 47 | */ 48 | @NotNull 49 | private final String[] lines; 50 | 51 | /** 52 | * the mapping. 53 | */ 54 | private final Map mapping = new HashMap<>(); 55 | 56 | /** 57 | * the wrap around. 58 | */ 59 | private final boolean wrapAround; 60 | 61 | /** 62 | * the default value. 63 | */ 64 | @Nullable 65 | private T defaultValue; 66 | 67 | /** 68 | * ctor. 69 | * 70 | * @param lines the lines describing the pattern. 71 | * 72 | * @throws IllegalArgumentException if the length of {@code lines} is zero. 73 | * @throws IllegalArgumentException if the length of a line is not equal to the length of the first line. 74 | * @see #Pattern(boolean, String...) to get the possibility to create a repeating pattern. 75 | */ 76 | public Pattern(@NotNull final String... lines) { 77 | this(false, lines); 78 | } 79 | 80 | /** 81 | * ctor. 82 | * 83 | * @param wrapAround whether the pattern should be repeated if the. 84 | * @param lines the lines describing the pattern. 85 | * 86 | * @throws IllegalArgumentException if the length of {@code lines} is zero 87 | * @throws IllegalArgumentException if the length of a line is not equal to the length of the first line 88 | */ 89 | public Pattern(final boolean wrapAround, @NotNull final String... lines) { 90 | Preconditions.checkArgument(lines.length > 0, "The given pattern lines must not be empty."); 91 | final var count = lines[0].length(); 92 | this.lines = new String[lines.length]; 93 | IntStream.range(0, lines.length).forEach(i -> { 94 | final var line = lines[i]; 95 | Preconditions.checkNotNull(line, "The given pattern line %s cannot be null.", i); 96 | Preconditions.checkArgument(line.length() == count, 97 | "The given pattern line %s does not match the first line character count.", i); 98 | this.lines[i] = lines[i]; 99 | }); 100 | this.wrapAround = wrapAround; 101 | } 102 | 103 | /** 104 | * attaches an object to a character in this pattern instance. 105 | * 106 | * @param character The key character. 107 | * @param object The object to attach to that character. 108 | * 109 | * @return {@code this} for a builder-like usage. 110 | */ 111 | @NotNull 112 | public Pattern attach(final char character, @NotNull final T object) { 113 | this.mapping.put(character, object); 114 | return this; 115 | } 116 | 117 | /** 118 | * searches through this patterns lines to find all occurrences of this key. 119 | * the first position is the most top-left and the last position is the most bottom-right one. 120 | *

121 | * if the key isn't contained in this pattern, the returned list will be empty. 122 | * 123 | * @param character The character key to look for. 124 | * 125 | * @return A mutable list containing all positions where that key occurs. 126 | */ 127 | @NotNull 128 | public List findAllKeys(final char character) { 129 | final var positions = new ArrayList(); 130 | for (var row = 0; row < this.getRowCount(); row++) { 131 | for (var column = 0; column < this.getColumnCount(); column++) { 132 | if (this.lines[row].charAt(column) == character) { 133 | positions.add(SlotPos.of(row, column)); 134 | } 135 | } 136 | } 137 | return positions; 138 | } 139 | 140 | /** 141 | * searches through this patterns lines to find the first top-left occurrence of this key. 142 | * if it could not be found, the returned {@link Optional} is empty. 143 | * 144 | * @param character The character key to look for. 145 | * 146 | * @return an optional containing the slot position in this pattern, or empty if it could not be found. 147 | */ 148 | @NotNull 149 | public Optional findKey(final char character) { 150 | for (var row = 0; row < this.getRowCount(); row++) { 151 | for (var column = 0; column < this.getColumnCount(); column++) { 152 | if (this.lines[row].charAt(column) == character) { 153 | return Optional.of(SlotPos.of(row, column)); 154 | } 155 | } 156 | } 157 | return Optional.empty(); 158 | } 159 | 160 | /** 161 | * this method counts the amount of rows this pattern has based on the length of the lines. 162 | * 163 | * @return the amount of columns. 164 | */ 165 | public int getColumnCount() { 166 | return this.lines[0].length(); 167 | } 168 | 169 | /** 170 | * returns the default value set via {@link #setDefault(Object)}. 171 | * 172 | * @return The default value. 173 | */ 174 | @NotNull 175 | public Optional getDefaultValue() { 176 | return Optional.ofNullable(this.defaultValue); 177 | } 178 | 179 | /** 180 | * returns the object from the n-th key in this pattern. 181 | * if this pattern has wrapAround set to {@code true}, and the index is equals or greater than 182 | * the amount of individual positions in this pattern, it will continue downwards, and not wrap around sideways. 183 | * because of this, it could be unclear what this method does and usage is for code clarity discouraged. 184 | * 185 | * @param index The index in this pattern. 186 | * 187 | * @return The object associated with the key. 188 | * 189 | * @see #getObject(int, int) For more detailed information. 190 | */ 191 | @NotNull 192 | public Optional getObject(final int index) { 193 | final var count = this.getColumnCount(); 194 | return this.getObject(index / count, index % count); 195 | } 196 | 197 | /** 198 | * this method is simple a shorthand to the method call {@link #getObject(int, int) getObject(slot.getRow(), 199 | * slot.getColumn())}, 200 | * so all the special cases described in that method will apply to this one. 201 | * 202 | * @param slot The slot position to extract the row and column from. 203 | * 204 | * @return The object associated with the key, or the default object. 205 | * 206 | * @see #getObject(int, int) For the more detailed information. 207 | */ 208 | @NotNull 209 | public Optional getObject(@NotNull final SlotPos slot) { 210 | return this.getObject(slot.getRow(), slot.getColumn()); 211 | } 212 | 213 | /** 214 | * retrieves the object associated with the key found at the row and column in this pattern, if there is no object 215 | * attached to that character, 216 | * the default object set via {@link #setDefault(Object)} is used. 217 | *

218 | * if wrapAround is set to {@code true} and the row or column would be too big or small of the pattern, it will 219 | * wrap around and continue on from the other side, like it would be endless. 220 | * if not, {@link IndexOutOfBoundsException} will be thrown. 221 | * 222 | * @param row The row of the key. 223 | * @param column The column of the key. 224 | * 225 | * @return The object associated with the key, or the default object. 226 | * 227 | * @throws IndexOutOfBoundsException if wrapAround is {@code false} and row or column are negative or not less 228 | * that the patterns dimensions. 229 | */ 230 | @NotNull 231 | public Optional getObject(final int row, final int column) { 232 | var rowCache = row; 233 | var columnCache = column; 234 | if (this.wrapAround) { 235 | rowCache %= this.getRowCount(); 236 | if (rowCache < 0) { 237 | rowCache += this.getRowCount(); 238 | } 239 | columnCache %= this.getColumnCount(); 240 | if (columnCache < 0) { 241 | columnCache += this.getColumnCount(); 242 | } 243 | } else { 244 | Preconditions.checkElementIndex(rowCache, this.lines.length, "The row must be between 0 and the row count"); 245 | Preconditions.checkElementIndex(columnCache, this.lines[0].length(), "The column must be between 0 and the column size"); 246 | } 247 | return Optional.ofNullable( 248 | this.mapping.getOrDefault(this.lines[rowCache].charAt(columnCache), this.defaultValue)); 249 | } 250 | 251 | /** 252 | * this method counts the amount of rows this pattern has based on the amount of lines provided at creation. 253 | * 254 | * @return the amount of rows. 255 | */ 256 | public int getRowCount() { 257 | return this.lines.length; 258 | } 259 | 260 | /** 261 | * a simple getter for the value provided at the Patterns creation, if this pattern supports wrapAround. 262 | * 263 | * @return {@code true} if wrapAround is enabled for this instance. 264 | */ 265 | public boolean isWrapAround() { 266 | return this.wrapAround; 267 | } 268 | 269 | /** 270 | * sets a new default value, which can be null and will override the previous value if present. 271 | * 272 | * @param defaultValue The new default value. 273 | * 274 | * @return {@code this} for a builder-like usage. 275 | */ 276 | @NotNull 277 | public Pattern setDefault(@NotNull final T defaultValue) { 278 | this.defaultValue = defaultValue; 279 | return this; 280 | } 281 | } 282 | -------------------------------------------------------------------------------- /src/main/java/io/github/portlek/smartinventory/util/ReflectionUtils.java: -------------------------------------------------------------------------------- 1 | /* 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2021 Crypto Morin 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 17 | * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR 18 | * PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE 19 | * FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 20 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | */ 22 | 23 | package io.github.portlek.smartinventory.util; 24 | 25 | import java.lang.invoke.MethodHandle; 26 | import java.lang.invoke.MethodHandles; 27 | import java.lang.invoke.MethodType; 28 | import java.util.concurrent.CompletableFuture; 29 | import org.bukkit.Bukkit; 30 | import org.bukkit.entity.Player; 31 | import org.jetbrains.annotations.NotNull; 32 | import org.jetbrains.annotations.Nullable; 33 | 34 | /** 35 | * ReflectionUtils - Reflection handler for NMS and CraftBukkit.
36 | * Caches the packet related methods and is asynchronous. 37 | *

38 | * This class does not handle null checks as most of the requests are from the 39 | * other utility classes that already handle null checks. 40 | *

41 | * Clientbound Packets are considered fake 42 | * updates to the client without changing the actual data. Since all the data is handled 43 | * by the server. 44 | * 45 | * @author Crypto Morin 46 | * @version 2.0.0 47 | */ 48 | public final class ReflectionUtils { 49 | 50 | /** 51 | * We use reflection mainly to avoid writing a new class for version barrier. 52 | * The version barrier is for NMS that uses the Minecraft version as the main package name. 53 | *

54 | * E.g. EntityPlayer in 1.15 is in the class {@code net.minecraft.server.v1_15_R1} 55 | * but in 1.14 it's in {@code net.minecraft.server.v1_14_R1} 56 | * In order to maintain cross-version compatibility we cannot import these classes. 57 | */ 58 | public static final String VERSION = Bukkit.getServer().getClass().getPackage().getName().split("\\.")[3]; 59 | 60 | /** 61 | * the craft bukkit. 62 | */ 63 | public static final String CRAFT_BUKKIT = "org.bukkit.craftbukkit." + ReflectionUtils.VERSION + '.'; 64 | 65 | /** 66 | * the nms. 67 | */ 68 | public static final String NMS = "net.minecraft.server." + ReflectionUtils.VERSION + '.'; 69 | 70 | /** 71 | * the get handle. 72 | */ 73 | private static final MethodHandle GET_HANDLE; 74 | 75 | /** 76 | * the player connection. 77 | */ 78 | private static final MethodHandle PLAYER_CONNECTION; 79 | 80 | /** 81 | * the send packet. 82 | */ 83 | private static final MethodHandle SEND_PACKET; 84 | 85 | static { 86 | final var entityPlayer = ReflectionUtils.getNMSClass("EntityPlayer"); 87 | final var craftPlayer = ReflectionUtils.getCraftClass("entity.CraftPlayer"); 88 | final var playerConnection = ReflectionUtils.getNMSClass("PlayerConnection"); 89 | final var lookup = MethodHandles.lookup(); 90 | MethodHandle sendPacket = null; 91 | MethodHandle getHandle = null; 92 | MethodHandle connection = null; 93 | try { 94 | connection = lookup.findGetter(entityPlayer, "playerConnection", playerConnection); 95 | getHandle = lookup.findVirtual(craftPlayer, "getHandle", MethodType.methodType(entityPlayer)); 96 | sendPacket = lookup.findVirtual(playerConnection, "sendPacket", MethodType.methodType(void.class, ReflectionUtils.getNMSClass("Packet"))); 97 | } catch (final NoSuchMethodException | NoSuchFieldException | IllegalAccessException ex) { 98 | ex.printStackTrace(); 99 | } 100 | PLAYER_CONNECTION = connection; 101 | SEND_PACKET = sendPacket; 102 | GET_HANDLE = getHandle; 103 | } 104 | 105 | /** 106 | * ctor. 107 | */ 108 | private ReflectionUtils() { 109 | } 110 | 111 | /** 112 | * Get a CraftBukkit (org.bukkit.craftbukkit) class. 113 | * 114 | * @param name the name of the class to load. 115 | * 116 | * @return the CraftBukkit class or null if not found. 117 | * 118 | * @since 1.0.0 119 | */ 120 | @Nullable 121 | public static Class getCraftClass(@NotNull final String name) { 122 | try { 123 | return Class.forName(ReflectionUtils.CRAFT_BUKKIT + name); 124 | } catch (final ClassNotFoundException ex) { 125 | ex.printStackTrace(); 126 | return null; 127 | } 128 | } 129 | 130 | /** 131 | * Get a NMS (net.minecraft.server) class. 132 | * 133 | * @param name the name of the class. 134 | * 135 | * @return the NMS class or null if not found. 136 | * 137 | * @since 1.0.0 138 | */ 139 | @Nullable 140 | public static Class getNMSClass(@NotNull final String name) { 141 | try { 142 | return Class.forName(ReflectionUtils.NMS + name); 143 | } catch (final ClassNotFoundException ex) { 144 | ex.printStackTrace(); 145 | return null; 146 | } 147 | } 148 | 149 | /** 150 | * Sends a packet to the player asynchronously if they're online. 151 | * Packets are thread-safe. 152 | * 153 | * @param player the player to send the packet to. 154 | * @param packets the packets to send. 155 | * 156 | * @return the async thread handling the packet. 157 | * 158 | * @see #sendPacketSync(Player, Object...) 159 | * @since 1.0.0 160 | */ 161 | @NotNull 162 | public static CompletableFuture sendPacket(@NotNull final Player player, @NotNull final Object... packets) { 163 | return CompletableFuture.runAsync(() -> ReflectionUtils.sendPacketSync(player, packets)) 164 | .exceptionally(ex -> { 165 | ex.printStackTrace(); 166 | return null; 167 | }); 168 | } 169 | 170 | /** 171 | * Sends a packet to the player synchronously if they're online. 172 | * 173 | * @param player the player to send the packet to. 174 | * @param packets the packets to send. 175 | * 176 | * @see #sendPacket(Player, Object...) 177 | * @since 2.0.0 178 | */ 179 | public static void sendPacketSync(@NotNull final Player player, @NotNull final Object... packets) { 180 | try { 181 | final var handle = ReflectionUtils.GET_HANDLE.invoke(player); 182 | final var connection = ReflectionUtils.PLAYER_CONNECTION.invoke(handle); 183 | if (connection != null) { 184 | for (final var packet : packets) { 185 | ReflectionUtils.SEND_PACKET.invoke(connection, packet); 186 | } 187 | } 188 | } catch (final Throwable throwable) { 189 | throwable.printStackTrace(); 190 | } 191 | } 192 | } 193 | -------------------------------------------------------------------------------- /src/main/java/io/github/portlek/smartinventory/util/SlotPos.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2021 Hasan Demirtaş 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | * 24 | */ 25 | 26 | package io.github.portlek.smartinventory.util; 27 | 28 | import lombok.AccessLevel; 29 | import lombok.EqualsAndHashCode; 30 | import lombok.Getter; 31 | import lombok.RequiredArgsConstructor; 32 | import lombok.ToString; 33 | import org.jetbrains.annotations.NotNull; 34 | 35 | /** 36 | * represents the position (row + column) of a slot in an inventory. 37 | */ 38 | @Getter 39 | @ToString 40 | @EqualsAndHashCode 41 | @RequiredArgsConstructor(access = AccessLevel.PRIVATE) 42 | public final class SlotPos { 43 | 44 | /** 45 | * the column. 46 | */ 47 | private final int column; 48 | 49 | /** 50 | * the row. 51 | */ 52 | private final int row; 53 | 54 | /** 55 | * creates a simple slot position instance. 56 | * 57 | * @param row the row to create. 58 | * @param column the column to create. 59 | * 60 | * @return a simple slot position instance. 61 | */ 62 | @NotNull 63 | public static SlotPos of(final int row, final int column) { 64 | return new SlotPos(column, row); 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /src/main/java/io/github/portlek/smartinventory/util/package-info.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2021 Hasan Demirtaş 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | * 24 | */ 25 | /** 26 | * the package that contains utility classes. 27 | */ 28 | package io.github.portlek.smartinventory.util; 29 | --------------------------------------------------------------------------------