Skip to content

Commit

Permalink
feat: OrbitSpell "interactions"
Browse files Browse the repository at this point in the history
  • Loading branch information
JasperLorelai committed Apr 3, 2024
1 parent 2fdc2d4 commit 94526a0
Showing 1 changed file with 54 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package com.nisovin.magicspells.spells.targeted;

import java.util.Map;
import java.util.Set;
import java.util.HashSet;
import java.util.*;
import java.util.function.Predicate;

import org.bukkit.Location;
Expand All @@ -11,15 +9,16 @@
import org.bukkit.entity.ArmorStand;
import org.bukkit.entity.LivingEntity;

import com.nisovin.magicspells.util.*;
import io.papermc.paper.entity.TeleportFlag;

import com.nisovin.magicspells.util.*;
import com.nisovin.magicspells.Subspell;
import com.nisovin.magicspells.MagicSpells;
import com.nisovin.magicspells.spells.TargetedSpell;
import com.nisovin.magicspells.util.config.ConfigData;
import com.nisovin.magicspells.events.SpellTargetEvent;
import com.nisovin.magicspells.spelleffects.SpellEffect;
import com.nisovin.magicspells.util.trackers.Interaction;
import com.nisovin.magicspells.spells.TargetedEntitySpell;
import com.nisovin.magicspells.spelleffects.EffectPosition;
import com.nisovin.magicspells.spells.TargetedLocationSpell;
Expand Down Expand Up @@ -59,6 +58,9 @@ public class OrbitSpell extends TargetedSpell implements TargetedEntitySpell, Ta
private final String groundSpellName;
private final String entitySpellName;

private final List<?> interactionData;
private List<Interaction> interactions;

private Subspell orbitSpell;
private Subspell groundSpell;
private Subspell entitySpell;
Expand Down Expand Up @@ -94,6 +96,8 @@ public OrbitSpell(MagicConfig config, String spellName) {
orbitSpellName = getConfigString("spell", "");
groundSpellName = getConfigString("spell-on-hit-ground", "");
entitySpellName = getConfigString("spell-on-hit-entity", "");

interactionData = getConfigList("interactions", null);
}

@Override
Expand All @@ -110,6 +114,9 @@ public void initialize() {
entitySpell = initSubspell(entitySpellName,
error.formatted("spell-on-hit-entity"),
true);

if (interactionData == null || interactionData.isEmpty()) return;
interactions = Interaction.read(this, interactionData);
}

@Override
Expand Down Expand Up @@ -177,6 +184,7 @@ public void removeOrbits(LivingEntity target) {
private class OrbitTracker implements Runnable {

private SpellData data;
private boolean stopped = false;

private final BoundingBox box;
private final Vector currentDirection;
Expand Down Expand Up @@ -335,6 +343,47 @@ public void run() {
return;
}
}

if (interactions == null || interactions.isEmpty()) return;
Set<OrbitTracker> toRemove = new HashSet<>();
Set<OrbitTracker> trackers = new HashSet<>(trackerSet);
for (OrbitTracker collisionTracker : trackers) {
for (Interaction interaction : interactions) {
if (!canInteractWith(collisionTracker)) continue;
if (!interaction.interactsWith().check(collisionTracker.getOrbitSpell())) continue;

if (interaction.canInteractList() != null && !interaction.canInteractList().canTarget(data.caster(), collisionTracker.data.caster()))
continue;

if (interaction.collisionSpell() != null) {
Location middleLoc = currentLocation.clone().add(collisionTracker.currentLocation).multiply(0.5);
interaction.collisionSpell().subcast(data.retarget(null, middleLoc));
}

if (interaction.stopCausing()) {
toRemove.add(collisionTracker);
collisionTracker.stop(false);
}

if (interaction.stopWith()) {
toRemove.add(this);
stop(false);
}
}
}

trackerSet.removeAll(toRemove);
toRemove.clear();
trackers.clear();
}

private boolean canInteractWith(OrbitTracker collisionTracker) {
if (collisionTracker == null) return false;
if (stopped || collisionTracker.stopped) return false;
if (!data.hasCaster() || !collisionTracker.data.hasCaster()) return false;
if (collisionTracker.equals(this)) return false;
if (!collisionTracker.currentLocation.getWorld().equals(currentLocation.getWorld())) return false;
return collisionTracker.box.contains(currentLocation) || box.contains(collisionTracker.currentLocation);
}

private OrbitSpell getOrbitSpell() {
Expand Down Expand Up @@ -364,6 +413,7 @@ private void move() {
}

private void stop(boolean removeTracker) {
stopped = true;
playSpellEffects(EffectPosition.DELAYED, currentLocation, data);

MagicSpells.cancelTask(taskId);
Expand Down

0 comments on commit 94526a0

Please sign in to comment.