Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions core/src/main/java/com/nisovin/magicspells/MagicSpells.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
import com.nisovin.magicspells.mana.ManaHandler;
import com.nisovin.magicspells.variables.Variable;
import com.nisovin.magicspells.spells.PassiveSpell;
import com.nisovin.magicspells.util.glow.GlowManager;
import com.nisovin.magicspells.util.compat.EventUtil;
import com.nisovin.magicspells.commands.MagicCommands;
import com.nisovin.magicspells.storage.StorageHandler;
Expand All @@ -93,6 +94,7 @@
import com.nisovin.magicspells.events.SpellLearnEvent.LearnSource;
import com.nisovin.magicspells.spelleffects.trackers.EffectTracker;
import com.nisovin.magicspells.spells.passive.util.PassiveListener;
import com.nisovin.magicspells.util.glow.impl.PacketEventsGlowManager;
import com.nisovin.magicspells.variables.variabletypes.GlobalVariable;
import com.nisovin.magicspells.spelleffects.trackers.AsyncEffectTracker;
import com.nisovin.magicspells.spelleffects.effecttypes.EffectLibEffect;
Expand Down Expand Up @@ -139,6 +141,7 @@ public class MagicSpells extends JavaPlugin {
private DeprecationHandler deprecationHandler;
private VolatileCodeHandle volatileCodeHandle;

private GlowManager glowManager;
private BuffManager buffManager;
private EffectManager effectManager;
private BossBarManager bossBarManager;
Expand Down Expand Up @@ -408,6 +411,10 @@ public void load() {
lifeLengthTracker = new LifeLengthTracker();
expressionDictionary = new ExpressionDictionary();

if (Bukkit.getPluginManager().isPluginEnabled("packetevents")) glowManager = new PacketEventsGlowManager();
else glowManager = MagicSpells.getVolatileCodeHandler().getGlowManager();
glowManager.load();

// Call loading event
Bukkit.getPluginManager().callEvent(new MagicSpellsLoadingEvent(this));

Expand Down Expand Up @@ -1442,6 +1449,10 @@ public static CustomGoalsManager getCustomGoalsManager() {
return plugin.customGoalsManager;
}

public static GlowManager getGlowManager() {
return plugin.glowManager;
}

public static BuffManager getBuffManager() {
return plugin.buffManager;
}
Expand Down Expand Up @@ -2269,6 +2280,12 @@ public void unload() {
buffManager = null;
}

// Turn off glow manager
if (glowManager != null) {
glowManager.unload();
glowManager = null;
}

// Clear memory
spells.clear();
spells = null;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package com.nisovin.magicspells.listeners;

import java.util.UUID;

import org.bukkit.entity.Entity;
import org.bukkit.event.Listener;
import org.bukkit.event.EventHandler;
import org.bukkit.entity.LivingEntity;
import org.bukkit.event.world.ChunkLoadEvent;
import org.bukkit.persistence.PersistentDataType;
import org.bukkit.event.entity.EntityDamageEvent;
import org.bukkit.event.entity.EntityRemoveEvent;
import org.bukkit.event.entity.EntityDismountEvent;
Expand All @@ -16,39 +19,61 @@
import com.nisovin.magicspells.Spell;
import com.nisovin.magicspells.util.Util;
import com.nisovin.magicspells.MagicSpells;
import com.nisovin.magicspells.util.SpellData;
import com.nisovin.magicspells.util.EntityData;
import com.nisovin.magicspells.util.pdc.UUIDTagType;
import com.nisovin.magicspells.events.SpellTargetEvent;
import com.nisovin.magicspells.zones.NoMagicZoneManager;
import com.nisovin.magicspells.spelleffects.effecttypes.*;
import com.nisovin.magicspells.util.pdc.PersistentDataEntry;
import com.nisovin.magicspells.events.ParticleProjectileHitEvent;

public class MagicSpellListener implements Listener {

private final NoMagicZoneManager noMagicZoneManager = MagicSpells.getNoMagicZoneManager();
public static final PersistentDataEntry<byte[], UUID> PDC_CASTER = new PersistentDataEntry<>(UUIDTagType.INSTANCE, "caster");
public static final PersistentDataEntry<Byte, Boolean> PDC_TARGETABLE = new PersistentDataEntry<>(PersistentDataType.BOOLEAN, "targetable");
public static final PersistentDataEntry<Byte, Boolean> PDC_TARGETABLE_BY_CASTER = new PersistentDataEntry<>(PersistentDataType.BOOLEAN, "targetable_by_caster");

@EventHandler
public void onSpellTarget(SpellTargetEvent event) {
// Check if target has noTarget permission / is in noMagicZone / is an invisible marker armorstand
LivingEntity target = event.getTarget();
Spell spell = event.getSpell();
SpellData data = event.getSpellData();
if (!data.hasTarget()) return;

if (target == null)
return;

if (isMSEntity(target)) {
if (!isTargetable(data.target(), data.caster())) {
event.setCancelled(true);
return;
}

if (Perm.NO_TARGET.has(target)) {
if (Perm.NO_TARGET.has(data.target())) {
event.setCancelled(true);
return;
}

if (spell != null && noMagicZoneManager != null && noMagicZoneManager.willFizzle(target, spell))
NoMagicZoneManager zoneManager = MagicSpells.getNoMagicZoneManager();
if (spell != null && zoneManager != null && zoneManager.willFizzle(data.target(), spell))
event.setCancelled(true);
}

private boolean isTargetable(Entity target, Entity caster) {
PersistentDataContainer pdc = target.getPersistentDataContainer();

if (caster != null && caster.getUniqueId().equals(PDC_CASTER.get(pdc))) {
Boolean targetableByCaster = PDC_TARGETABLE_BY_CASTER.get(pdc);
if (targetableByCaster != null) return targetableByCaster;
}

Boolean targetable = PDC_TARGETABLE.get(pdc);
if (targetable != null) return targetable;

return !isMSEntity(target);
}

private boolean isMSEntity(Entity entity) {
return entity.getScoreboardTags().contains(ArmorStandEffect.ENTITY_TAG)
|| entity.getScoreboardTags().contains(EntityEffect.ENTITY_TAG);
}

@EventHandler
public void onProjectileHit(ParticleProjectileHitEvent event) {
LivingEntity target = event.getTarget();
Expand Down Expand Up @@ -78,11 +103,11 @@ public void onEntityDamage(EntityDamageEvent event) {
@EventHandler
public void onEntityRemove(EntityRemoveEvent event) {
Util.forEachPassenger(event.getEntity(), passenger -> {
PersistentDataContainer container = passenger.getPersistentDataContainer();
if (!container.has(EntityData.MS_PASSENGER)) return;
PersistentDataContainer pdc = passenger.getPersistentDataContainer();
if (!EntityData.MS_PASSENGER.has(pdc)) return;

if (passenger.isPersistent()) {
container.remove(EntityData.MS_PASSENGER);
EntityData.MS_PASSENGER.remove(pdc);
return;
}

Expand All @@ -92,22 +117,18 @@ public void onEntityRemove(EntityRemoveEvent event) {

@EventHandler
public void onEntityDismount(EntityDismountEvent event) {
event.getEntity().getPersistentDataContainer().remove(EntityData.MS_PASSENGER);
}

private boolean isMSEntity(Entity entity) {
return entity.getScoreboardTags().contains(ArmorStandEffect.ENTITY_TAG) || entity.getScoreboardTags().contains(EntityEffect.ENTITY_TAG);
EntityData.MS_PASSENGER.remove(event.getEntity().getPersistentDataContainer());
}

@EventHandler
public void onFireworkDamage(EntityDamageByEntityEvent event) {
if (!event.getDamager().getPersistentDataContainer().has(FireworksEffect.MS_FIREWORK)) return;
if (!FireworksEffect.MS_FIREWORK.has(event.getDamager().getPersistentDataContainer())) return;
event.setCancelled(true);
}

@EventHandler
public void onInvPickup(InventoryPickupItemEvent event) {
if (!event.getItem().getPersistentDataContainer().has(ItemSprayEffect.MS_ITEM_SPRAY)) return;
if (!ItemSprayEffect.MS_ITEM_SPRAY.has(event.getItem().getPersistentDataContainer())) return;
event.setCancelled(true);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.EquipmentSlot;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.persistence.PersistentDataContainer;

import com.nisovin.magicspells.util.Name;
import com.nisovin.magicspells.util.Util;
Expand All @@ -16,6 +17,7 @@
import com.nisovin.magicspells.util.magicitems.MagicItem;
import com.nisovin.magicspells.util.magicitems.MagicItems;
import com.nisovin.magicspells.util.config.ConfigDataUtil;
import com.nisovin.magicspells.listeners.MagicSpellListener;

@Name("armorstand")
public class ArmorStandEffect extends SpellEffect {
Expand Down Expand Up @@ -62,12 +64,21 @@ protected ArmorStand playArmorStandEffectLocation(Location location, SpellData d
stand.setItem(EquipmentSlot.HEAD, headItem);
stand.setItem(EquipmentSlot.HAND, mainhandItem);
stand.setItem(EquipmentSlot.OFF_HAND, offhandItem);

preSpawn(stand);
Util.forEachPassenger(stand, this::preSpawn);
}, stand -> {
postSpawn(stand);
Util.forEachPassenger(stand, this::postSpawn);
});
}

private void preSpawn(Entity entity) {
PersistentDataContainer pdc = entity.getPersistentDataContainer();
MagicSpellListener.PDC_TARGETABLE.set(pdc, false);
MagicSpellListener.PDC_TARGETABLE_BY_CASTER.set(pdc, false);
}

private void postSpawn(Entity entity) {
entity.setPersistent(false);
entity.addScoreboardTag(ENTITY_TAG);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.bukkit.Location;
import org.bukkit.entity.Entity;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.persistence.PersistentDataContainer;

import com.nisovin.magicspells.util.Name;
import com.nisovin.magicspells.util.Util;
Expand All @@ -15,11 +16,12 @@
import com.nisovin.magicspells.util.config.ConfigData;
import com.nisovin.magicspells.spelleffects.SpellEffect;
import com.nisovin.magicspells.util.config.ConfigDataUtil;
import com.nisovin.magicspells.listeners.MagicSpellListener;

@Name("entity")
public class EntityEffect extends SpellEffect {

public static final Set<Entity> entities = new HashSet<>();
private static final Set<Entity> entities = new HashSet<>();

public static final String ENTITY_TAG = "MS_ENTITY";

Expand All @@ -45,12 +47,21 @@ protected void loadFromConfig(ConfigurationSection config) {
protected Entity playEntityEffectLocation(Location location, SpellData data) {
return entityData.spawn(location, data, entity -> {
entity.setGravity(gravity.get(data));

preSpawn(entity);
Util.forEachPassenger(entity, this::preSpawn);
}, entity -> {
postSpawn(entity);
Util.forEachPassenger(entity, this::postSpawn);
});
}

private void preSpawn(Entity entity) {
PersistentDataContainer pdc = entity.getPersistentDataContainer();
MagicSpellListener.PDC_TARGETABLE.set(pdc, false);
MagicSpellListener.PDC_TARGETABLE_BY_CASTER.set(pdc, false);
}

private void postSpawn(Entity entity) {
entity.setPersistent(false);
entity.addScoreboardTag(ENTITY_TAG);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@
import org.bukkit.configuration.ConfigurationSection;

import com.nisovin.magicspells.util.Name;
import com.nisovin.magicspells.MagicSpells;
import com.nisovin.magicspells.util.SpellData;
import com.nisovin.magicspells.util.config.ConfigData;
import com.nisovin.magicspells.spelleffects.SpellEffect;
import com.nisovin.magicspells.util.config.ConfigDataUtil;
import com.nisovin.magicspells.util.pdc.PersistentDataEntry;

@Name("fireworks")
public class FireworksEffect extends SpellEffect {

public static final NamespacedKey MS_FIREWORK = new NamespacedKey(MagicSpells.getInstance(), "fireworks_effect");
public static final PersistentDataEntry<Byte, Boolean> MS_FIREWORK = new PersistentDataEntry<>(PersistentDataType.BOOLEAN, "fireworks_effect");

private ConfigData<Integer> type;
private ConfigData<Integer> flightDuration;
Expand Down Expand Up @@ -95,7 +95,7 @@ public Runnable playEffectLocation(Location location, SpellData data) {
firework.setSilent(true);
firework.setTicksToDetonate(flightDuration.get(data));

firework.getPersistentDataContainer().set(MS_FIREWORK, PersistentDataType.BOOLEAN, true);
MS_FIREWORK.set(firework.getPersistentDataContainer(), true);
});

return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import org.bukkit.Material;
import org.bukkit.entity.Item;
import org.bukkit.util.Vector;
import org.bukkit.NamespacedKey;
import org.bukkit.entity.Entity;
import org.bukkit.inventory.ItemStack;
import org.bukkit.persistence.PersistentDataType;
Expand All @@ -21,11 +20,12 @@
import com.nisovin.magicspells.util.config.ConfigData;
import com.nisovin.magicspells.spelleffects.SpellEffect;
import com.nisovin.magicspells.util.config.ConfigDataUtil;
import com.nisovin.magicspells.util.pdc.PersistentDataEntry;

@Name("itemspray")
public class ItemSprayEffect extends SpellEffect {

public static final NamespacedKey MS_ITEM_SPRAY = new NamespacedKey(MagicSpells.getInstance(), "ms_item_spray");
public static final PersistentDataEntry<Byte, Boolean> MS_ITEM_SPRAY = new PersistentDataEntry<>(PersistentDataType.BOOLEAN, "ms_item_spray");

private static final List<Item> items = new ArrayList<>();

Expand Down Expand Up @@ -80,7 +80,7 @@ public Runnable playEffectLocation(Location location, SpellData data) {
int amount = this.amount.get(data);
for (int i = 0; i < amount; i++) {
Item dropped = loc.getWorld().dropItem(loc, itemStack, item -> {
item.getPersistentDataContainer().set(MS_ITEM_SPRAY, PersistentDataType.BOOLEAN, true);
MS_ITEM_SPRAY.set(item.getPersistentDataContainer(), true);

Vector velocity = this.velocity.get(data);
if (velocity == null) velocity = new Vector(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public CastResult castAtLocation(SpellData data) {

Collection<Item> items = location.getNearbyEntitiesByType(Item.class, radius, item -> {
if (!item.isValid() || item.getItemStack().isEmpty()) return false;
if (item.getPersistentDataContainer().has(ItemSprayEffect.MS_ITEM_SPRAY)) return false;
if (ItemSprayEffect.MS_ITEM_SPRAY.has(item.getPersistentDataContainer())) return false;

if (data.hasCaster()) {
LivingEntity caster = data.caster();
Expand Down
Loading
Loading