-
Notifications
You must be signed in to change notification settings - Fork 8
Generify talisman code #351
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
Open
OhmV-IR
wants to merge
13
commits into
master
Choose a base branch
from
generify-talisman-code
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
e4d017e
migrate health talisman to inventory ticker
OhmV-IR 034df8e
try fix bug
OhmV-IR 4a8abdf
actually fix
OhmV-IR 27c763f
use weak to not leak mem
OhmV-IR 4aab043
generify talisman code
OhmV-IR 63ddf2c
fix name scheme of methods
OhmV-IR 1d9d964
pr comments
OhmV-IR f9e0881
adapt to changes in inv ticker
OhmV-IR f69cbb4
Merge branch 'master' into generify-talisman-code
OhmV-IR deb19b0
stop using weak hash map where it's not needed
OhmV-IR 792c09a
Merge branch 'master' into generify-talisman-code
OhmV-IR 1863a08
jdoc
OhmV-IR 63b8c14
Merge branch 'master' into generify-talisman-code
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
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
src/main/java/io/github/pylonmc/pylon/base/content/tools/base/Talisman.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| package io.github.pylonmc.pylon.base.content.tools.base; | ||
|
|
||
| import io.github.pylonmc.pylon.base.PylonBase; | ||
| import io.github.pylonmc.pylon.core.config.PylonConfig; | ||
| import io.github.pylonmc.pylon.core.item.PylonItem; | ||
| import io.github.pylonmc.pylon.core.item.base.PylonInventoryTicker; | ||
| import org.bukkit.Bukkit; | ||
| import org.bukkit.NamespacedKey; | ||
| import org.bukkit.entity.Player; | ||
| import org.bukkit.inventory.ItemStack; | ||
| import org.bukkit.persistence.PersistentDataType; | ||
| import org.bukkit.scheduler.BukkitTask; | ||
| import org.jetbrains.annotations.MustBeInvokedByOverriders; | ||
| import org.jetbrains.annotations.NotNull; | ||
|
|
||
| import java.util.HashMap; | ||
| import java.util.UUID; | ||
| import java.util.WeakHashMap; | ||
|
|
||
| public abstract class Talisman extends PylonItem implements PylonInventoryTicker { | ||
| private static final HashMap<NamespacedKey, HashMap<UUID, BukkitTask>> tasks = new HashMap<>(); | ||
|
|
||
| public Talisman(@NotNull ItemStack stack) { | ||
| super(stack); | ||
| } | ||
|
|
||
| @Override | ||
| public void onTick(@NotNull Player player) { | ||
| tasks.putIfAbsent(getTalismanKey(), new HashMap<>()); | ||
| boolean foundItem = false; | ||
| Integer currentTalismanLevel = player.getPersistentDataContainer().get(getTalismanKey(), PersistentDataType.INTEGER); | ||
| if (currentTalismanLevel == null) { | ||
| applyEffect(player); | ||
| foundItem = true; | ||
| } else if (currentTalismanLevel < getLevel()) { | ||
| removeEffect(player); | ||
| applyEffect(player); | ||
| foundItem = true; | ||
| } else if (currentTalismanLevel == getLevel()) { | ||
| foundItem = true; | ||
| } | ||
| BukkitTask toCancel = tasks.get(getTalismanKey()).get(player.getUniqueId()); | ||
| if (foundItem) { | ||
| if (toCancel != null) { | ||
| toCancel.cancel(); | ||
| } | ||
| tasks.get(getTalismanKey()).put(player.getUniqueId(), Bukkit.getScheduler().runTaskLater(PylonBase.getInstance(), () -> | ||
| removeEffect(player), | ||
| getTickInterval() * PylonConfig.getInventoryTickerBaseRate() + 1)); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * The implementation of this method MUST call super.applyEffect | ||
| * @param player The player who the effect is being applied to | ||
| */ | ||
| @MustBeInvokedByOverriders | ||
| public void applyEffect(@NotNull Player player) { | ||
| player.getPersistentDataContainer().set(getTalismanKey(), PersistentDataType.INTEGER, getLevel()); | ||
| } | ||
|
|
||
| /** | ||
| * The implementation of this method MUST call super.removeEffect | ||
| * @param player The player who the effect is being removed from | ||
| */ | ||
| @MustBeInvokedByOverriders | ||
| public void removeEffect(@NotNull Player player) { | ||
| player.getPersistentDataContainer().remove(getTalismanKey()); | ||
| } | ||
|
|
||
| /** | ||
| * Get the level of the talisman, this is used to determine which talismans should overwrite other ones | ||
| */ | ||
| public abstract int getLevel(); | ||
|
|
||
| /** | ||
| * Get the generic key of the talisman, should be the same between all talismans of the same type, ie all health talisman levels have the same return value for this. | ||
| */ | ||
| public abstract NamespacedKey getTalismanKey(); | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit