-
Notifications
You must be signed in to change notification settings - Fork 8
Add InventoryEffectItem class #396
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
dd10c95
add inventory effect item
OhmV-IR 0805f12
pr comments
OhmV-IR 850f8ea
oop
OhmV-IR 6a09ea1
Merge branch 'add-inventory-ticker' into add-inv-effect
OhmV-IR 2bc0db0
Merge branch 'add-inventory-ticker' into add-inv-effect
OhmV-IR 947e958
adapter for ticker changes
OhmV-IR 7d2e54c
Add missing mustbeinvoked by overriders
OhmV-IR 25eab3e
best effort avoidance of bugs
OhmV-IR File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
52 changes: 52 additions & 0 deletions
52
...n-core/src/main/kotlin/io/github/pylonmc/pylon/core/item/base/PylonInventoryEffectItem.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| package io.github.pylonmc.pylon.core.item.base | ||
|
|
||
| import io.github.pylonmc.pylon.core.PylonCore | ||
| import io.github.pylonmc.pylon.core.config.PylonConfig | ||
| import io.github.pylonmc.pylon.core.item.PylonItem | ||
| import org.bukkit.Bukkit | ||
| import org.bukkit.NamespacedKey | ||
| import org.bukkit.entity.Player | ||
| import org.bukkit.persistence.PersistentDataType | ||
| import org.bukkit.scheduler.BukkitTask | ||
| import org.jetbrains.annotations.MustBeInvokedByOverriders | ||
| import java.util.* | ||
|
|
||
| interface PylonInventoryEffectItem : PylonInventoryTicker { | ||
| @MustBeInvokedByOverriders | ||
| override fun onTick(player: Player) { | ||
| tasks.putIfAbsent(itemKey, HashMap()) | ||
| tasks[itemKey]!![player.uniqueId]?.cancel() | ||
| if (!player.persistentDataContainer.has(itemKey)) { | ||
| player.persistentDataContainer.set(itemKey, PersistentDataType.BOOLEAN, true) | ||
| onAddedToInventory(player) | ||
| } | ||
| tasks[itemKey]!![player.uniqueId] = Bukkit.getScheduler().runTaskLater(PylonCore.javaPlugin, Runnable { | ||
| player.persistentDataContainer.remove(itemKey) | ||
| onRemovedFromInventory(player) | ||
| }, tickInterval * PylonConfig.inventoryTickerBaseRate + 1) | ||
| } | ||
|
|
||
| /** | ||
| * Remove the effect from the player. Best-effort removal therefore is no guarantee that the player is still connected or that the | ||
| * [PylonItem.stack] is up to date with the actual ItemStack when it runs. | ||
| */ | ||
| @MustBeInvokedByOverriders | ||
| fun onRemovedFromInventory(player: Player) { | ||
| player.persistentDataContainer.remove(itemKey) | ||
OhmV-IR marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| /** | ||
| * Apply the effect of this item onto the player | ||
| */ | ||
| @MustBeInvokedByOverriders | ||
| fun onAddedToInventory(player: Player) { | ||
| player.persistentDataContainer.set(itemKey, PersistentDataType.BOOLEAN, true) | ||
OhmV-IR marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| val itemKey: NamespacedKey | ||
| get() = NamespacedKey((this as PylonItem).key.namespace, key.key + "_effect") | ||
|
|
||
| companion object { | ||
| private val tasks = HashMap<NamespacedKey, HashMap<UUID, BukkitTask>>() | ||
Seggan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.