Repository navigation
feat: ProxySpell #1100
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
DragonsAscent
wants to merge
15
commits into
TheComputerGeek2:main
Choose a base branch
from
DragonsAscent:proxy-spell
base: main
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
feat: ProxySpell #1100
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
c3dd9ac
feat: ProxySpell
DragonsAscent 07902cc
Update core/src/main/java/com/nisovin/magicspells/spells/buff/ProxySp…
DragonsAscent 80c59ce
Update core/src/main/java/com/nisovin/magicspells/spells/buff/ProxySp…
DragonsAscent eaee89b
Update core/src/main/java/com/nisovin/magicspells/spells/buff/ProxySp…
DragonsAscent 808204c
Update core/src/main/java/com/nisovin/magicspells/spells/buff/ProxySp…
DragonsAscent cdb4acb
Update core/src/main/java/com/nisovin/magicspells/spells/buff/ProxySp…
DragonsAscent b639531
Update core/src/main/java/com/nisovin/magicspells/spells/buff/ProxySp…
JasperLorelai 1d970fb
refactor: Sort imports
JasperLorelai 8970aba
fix: Increase priority
JasperLorelai 0e01c96
fix: Check if buff is active for the event-entity
JasperLorelai 7c96a57
refactor: Store Entity instead of SpellData
JasperLorelai 350167f
fix: Remove duplicate
JasperLorelai d0c638e
fix: Prevent same entity
JasperLorelai e49078b
Update core/src/main/java/com/nisovin/magicspells/spells/buff/ProxySp…
DragonsAscent 8bff645
feat: Configure redirection
JasperLorelai 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
171 changes: 171 additions & 0 deletions
171
core/src/main/java/com/nisovin/magicspells/spells/buff/ProxySpell.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,171 @@ | ||
| package com.nisovin.magicspells.spells.buff; | ||
|
|
||
| import java.util.Map; | ||
| import java.util.Set; | ||
| import java.util.UUID; | ||
| import java.util.HashMap; | ||
| import java.util.HashSet; | ||
| import java.util.Collection; | ||
|
|
||
| import org.jetbrains.annotations.NotNull; | ||
|
|
||
| import org.bukkit.event.Listener; | ||
| import org.bukkit.event.EventHandler; | ||
| import org.bukkit.entity.LivingEntity; | ||
| import org.bukkit.event.EventPriority; | ||
| import org.bukkit.event.entity.EntityDamageEvent; | ||
| import org.bukkit.event.entity.EntityDamageByEntityEvent; | ||
|
|
||
| import com.nisovin.magicspells.util.Util; | ||
| import com.nisovin.magicspells.MagicSpells; | ||
| import com.nisovin.magicspells.util.SpellData; | ||
| import com.nisovin.magicspells.spells.BuffSpell; | ||
| import com.nisovin.magicspells.util.MagicConfig; | ||
| import com.nisovin.magicspells.events.SpellTargetEvent; | ||
| import com.nisovin.magicspells.spelleffects.EffectPosition; | ||
|
|
||
| public class ProxySpell extends BuffSpell { | ||
|
|
||
| private final Set<UUID> redirecting = new HashSet<>(); | ||
| private final Map<UUID, LivingEntity> proxies = new HashMap<>(); | ||
|
|
||
| private SpellListener spellListener; | ||
| private DamageListener damageListener; | ||
|
|
||
| private RedirectDamage redirectDamage = RedirectDamage.ALL; | ||
|
|
||
| public ProxySpell(MagicConfig config, String spellName) { | ||
| super(config, spellName); | ||
|
|
||
| boolean redirectSpells = getConfigBoolean("redirect-spells", true); | ||
|
|
||
| String redirectDamageString = getConfigString("redirect-damage", null); | ||
| if (redirectDamageString != null) { | ||
| RedirectDamage redirect = Util.enumValueSafe(RedirectDamage.class, redirectDamageString); | ||
| if (redirect == null) MagicSpells.error("ProxySpell '" + internalName + "' has an invalid 'redirect-damage' value '" + redirectDamageString + "'."); | ||
| else redirectDamage = redirect; | ||
| } | ||
|
|
||
| if (redirectSpells) { | ||
| spellListener = new SpellListener(); | ||
| registerEvents(spellListener); | ||
| } | ||
| if (redirectDamage != RedirectDamage.NONE) { | ||
| damageListener = new DamageListener(); | ||
| registerEvents(damageListener); | ||
| } | ||
|
|
||
| if (redirectSpells || redirectDamage != RedirectDamage.NONE) return; | ||
| MagicSpells.error("ProxySpell '" + internalName + "' has no redirection enabled."); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean castBuff(SpellData data) { | ||
| if (data.target().equals(data.caster())) return false; | ||
| proxies.put(data.target().getUniqueId(), data.caster()); | ||
| return true; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean recastBuff(SpellData data) { | ||
| stopEffects(data.target()); | ||
| turnOffBuff(data.target()); | ||
| return castBuff(data); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean isActive(LivingEntity entity) { | ||
| return proxies.containsKey(entity.getUniqueId()); | ||
| } | ||
|
|
||
| @Override | ||
| public void turnOffBuff(LivingEntity entity) { | ||
| proxies.remove(entity.getUniqueId()); | ||
| } | ||
|
|
||
| @Override | ||
| protected @NotNull Collection<UUID> getActiveEntities() { | ||
| return proxies.keySet(); | ||
| } | ||
|
|
||
| @Override | ||
| protected void turnOff() { | ||
| super.turnOff(); | ||
|
|
||
| if (spellListener != null) { | ||
| unregisterEvents(spellListener); | ||
| spellListener = null; | ||
| } | ||
| if (damageListener != null) { | ||
| unregisterEvents(damageListener); | ||
| damageListener = null; | ||
| } | ||
| } | ||
|
|
||
| private class SpellListener implements Listener { | ||
|
|
||
| @EventHandler(ignoreCancelled = true) | ||
| public void onSpellTarget(SpellTargetEvent event) { | ||
| LivingEntity target = event.getTarget(); | ||
| if (target == null || !isActive(target)) return; | ||
|
|
||
| LivingEntity proxyTarget = getProxyTarget(target); | ||
| if (proxyTarget == null) return; | ||
|
|
||
| event.setTarget(proxyTarget); | ||
| playRedirectEffects(target, proxyTarget, event.getSpellData()); | ||
|
|
||
| addUseAndChargeCost(target); | ||
| } | ||
|
|
||
| } | ||
|
|
||
| private class DamageListener implements Listener { | ||
|
|
||
| @EventHandler(ignoreCancelled = true, priority = EventPriority.HIGHEST) | ||
| public void onEntityDamage(EntityDamageEvent event) { | ||
| if (!(event.getEntity() instanceof LivingEntity target) || !isActive(target)) return; | ||
|
|
||
| LivingEntity proxyTarget = getProxyTarget(target); | ||
| if (proxyTarget == null) return; | ||
| if (!redirecting.add(proxyTarget.getUniqueId())) return; | ||
|
|
||
| LivingEntity damager = null; | ||
| if (event instanceof EntityDamageByEntityEvent byEvent) | ||
| damager = byEvent.getDamager() instanceof LivingEntity e ? e : null; | ||
| else if (redirectDamage == RedirectDamage.ENTITIES) return; | ||
|
|
||
| SpellData subData = new SpellData(damager, proxyTarget); | ||
| playRedirectEffects(target, proxyTarget, subData); | ||
|
|
||
| event.setCancelled(true); | ||
| try { | ||
| proxyTarget.damage(event.getFinalDamage(), event.getDamageSource()); | ||
| addUseAndChargeCost(target); | ||
| } finally { | ||
| redirecting.remove(proxyTarget.getUniqueId()); | ||
| } | ||
| } | ||
|
|
||
| } | ||
|
|
||
| private enum RedirectDamage { | ||
| ALL, | ||
| ENTITIES, | ||
| NONE | ||
| } | ||
|
|
||
| private LivingEntity getProxyTarget(LivingEntity target) { | ||
| LivingEntity proxyTarget = proxies.get(target.getUniqueId()); | ||
| if (proxyTarget != null && proxyTarget.isValid()) return proxyTarget; | ||
|
|
||
| turnOff(target); | ||
| return null; | ||
| } | ||
|
|
||
| private void playRedirectEffects(LivingEntity target, LivingEntity proxyTarget, SpellData data) { | ||
| playSpellEffects(EffectPosition.START_POSITION, target, data); | ||
| playSpellEffects(EffectPosition.END_POSITION, proxyTarget, data); | ||
| } | ||
|
|
||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.