Skip to content

Commit

Permalink
Merge pull request #865 from JasperLorelai/main
Browse files Browse the repository at this point in the history
Features and fixes
  • Loading branch information
Chronoken authored Mar 9, 2024
2 parents 5d044fd + 1d92baf commit e2769b7
Show file tree
Hide file tree
Showing 16 changed files with 223 additions and 145 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,53 +1,17 @@
package com.nisovin.magicspells.events;

import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;
import org.bukkit.Location;
import org.bukkit.entity.LivingEntity;

import org.jetbrains.annotations.NotNull;

import com.nisovin.magicspells.spells.instant.PortalSpell;

/**
* This event is fired whenever an entity enters a portal from PortalSpell.
* This event is fired whenever an entity enters a portal from {@link PortalSpell}.
*/
public class PortalEnterEvent extends Event {

private static final HandlerList handlers = new HandlerList();

private final LivingEntity entity;

private final PortalSpell portalSpell;

public PortalEnterEvent(LivingEntity entity, PortalSpell portalSpell) {
this.entity = entity;
this.portalSpell = portalSpell;
}

/**
* Gets the entity who entered the portal
* @return the entity
*/
public LivingEntity getEntity() {
return entity;
}

/**
* Gets the portal spell
* @return the spell
*/
public PortalSpell getPortalSpell() {
return portalSpell;
}

@NotNull
@Override
public HandlerList getHandlers() {
return handlers;
}
public class PortalEnterEvent extends PortalEvent {

public static HandlerList getHandlerList() {
return handlers;
public PortalEnterEvent(LivingEntity entity, Location destination, PortalSpell portalSpell) {
super(entity, destination, portalSpell);
}

}
84 changes: 84 additions & 0 deletions core/src/main/java/com/nisovin/magicspells/events/PortalEvent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package com.nisovin.magicspells.events;

import org.bukkit.Location;
import org.bukkit.event.Cancellable;
import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;
import org.bukkit.entity.LivingEntity;

import org.jetbrains.annotations.NotNull;

import com.nisovin.magicspells.spells.instant.PortalSpell;

/**
* {@link PortalSpell}
*/
public class PortalEvent extends Event implements Cancellable {

private static final HandlerList handlers = new HandlerList();

private Location destination;
private final LivingEntity entity;
private final PortalSpell portalSpell;

private boolean cancelled = false;

public PortalEvent(LivingEntity entity, Location destination, PortalSpell portalSpell) {
this.entity = entity;
this.destination = destination;
this.portalSpell = portalSpell;
}

/**
* Gets the entity who entered the portal
* @return the entity
*/
public LivingEntity getEntity() {
return entity;
}

/**
* Gets a clone of the portal destination
* @return location
*/
public Location getDestination() {
return destination.clone();
}

/**
* Set the portal destination for this event
* @param destination new destination
*/
public void setDestination(Location destination) {
this.destination = destination;
}

/**
* Gets the portal spell
* @return the spell
*/
public PortalSpell getPortalSpell() {
return portalSpell;
}

@NotNull
@Override
public HandlerList getHandlers() {
return handlers;
}

public static HandlerList getHandlerList() {
return handlers;
}

@Override
public boolean isCancelled() {
return cancelled;
}

@Override
public void setCancelled(boolean cancel) {
cancelled = cancel;
}

}
Original file line number Diff line number Diff line change
@@ -1,53 +1,17 @@
package com.nisovin.magicspells.events;

import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;
import org.bukkit.Location;
import org.bukkit.entity.LivingEntity;

import org.jetbrains.annotations.NotNull;

import com.nisovin.magicspells.spells.instant.PortalSpell;

/**
* This event is fired whenever an entity leaves a portal from PortalSpell.
* This event is fired whenever an entity leaves a portal from {@link PortalSpell}.
*/
public class PortalLeaveEvent extends Event {

private static final HandlerList handlers = new HandlerList();

private final LivingEntity entity;

private final PortalSpell portalSpell;

public PortalLeaveEvent(LivingEntity entity, PortalSpell portalSpell) {
this.entity = entity;
this.portalSpell = portalSpell;
}

/**
* Gets the entity who left the portal
* @return the entity
*/
public LivingEntity getEntity() {
return entity;
}

/**
* Gets the portal spell
* @return the spell
*/
public PortalSpell getPortalSpell() {
return portalSpell;
}

@NotNull
@Override
public HandlerList getHandlers() {
return handlers;
}
public class PortalLeaveEvent extends PortalEvent {

public static HandlerList getHandlerList() {
return handlers;
public PortalLeaveEvent(LivingEntity entity, Location destination, PortalSpell portalSpell) {
super(entity, destination, portalSpell);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public SpellTargetEvent(Spell spell, SpellData spellData) {

this.spellData = spellData;
}

public SpellTargetEvent(Spell spell, SpellData spellData, LivingEntity target) {
this(spell, spellData.target(target));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package com.nisovin.magicspells.listeners;

import org.bukkit.GameMode;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.event.Listener;
import org.bukkit.entity.ArmorStand;
import org.bukkit.event.EventHandler;
Expand All @@ -26,13 +24,12 @@ public class MagicSpellListener implements Listener {

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

if (Perm.NO_TARGET.has(target)) event.setCancelled(true);
if (target instanceof Player && ((Player) target).getGameMode() == GameMode.SPECTATOR) event.setCancelled(true);
if (spell != null && noMagicZoneManager != null && noMagicZoneManager.willFizzle(target, spell)) event.setCancelled(true);
if (isMSEntity(target)) event.setCancelled(true);
if (target instanceof ArmorStand && target.isInvisible() && ((ArmorStand) target).isMarker()) event.setCancelled(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ private boolean activateSpells(SpellData data) {

if (data.hasTarget()) {
SpellTargetEvent targetEvent = new SpellTargetEvent(this, data);
if (!targetEvent.callEvent()) {
if (!validTargetList.canTarget(data.caster(), data.target()) || !targetEvent.callEvent()) {
MagicSpells.debug(3, " Target cancelled (TE)");

disabled = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ public CastResult castAtEntityFromLocation(SpellData data) {
//check entities in the beam range
for (LivingEntity e : loc.getNearbyLivingEntities(hitRadius, verticalHitRadius)) {
if (!e.isValid() || immune.contains(e)) continue;
if (validTargetList != null && !validTargetList.canTarget(data.caster(), e)) continue;
if (!validTargetList.canTarget(data.caster(), e)) continue;

SpellTargetEvent event = new SpellTargetEvent(this, locData, e);
if (!event.callEvent()) continue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ public CastResult castAtEntityFromLocation(SpellData data) {
//check entities in the beam range
for (LivingEntity e : loc.getNearbyLivingEntities(hitRadius, verticalHitRadius)) {
if (!e.isValid() || immune.contains(e)) continue;
if (validTargetList != null && !validTargetList.canTarget(data.caster(), e)) continue;
if (!validTargetList.canTarget(data.caster(), e)) continue;

SpellTargetEvent event = new SpellTargetEvent(this, locData, e);
if (!event.callEvent()) continue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,18 +145,18 @@ else if (radius > 0 && caster.getLocation().distanceSquared(foundEntity.getLocat
}
if (!ordered.isEmpty()) {
for (NearbyEntity ne : ordered) {
if (ne.entity instanceof LivingEntity le) {
SpellTargetEvent event = new SpellTargetEvent(this, data, le);
EventUtil.call(event);
if (!event.isCancelled()) {
foundEntity = event.getTarget();
data = event.getSpellData();
break;
}
} else {
if (!(ne.entity instanceof LivingEntity le)) {
foundEntity = ne.entity;
break;
}

if (!validTargetList.canTarget(caster, le)) continue;
SpellTargetEvent event = new SpellTargetEvent(this, data, le);
EventUtil.call(event);
if (event.isCancelled()) continue;
foundEntity = event.getTarget();
data = event.getSpellData();
break;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
import com.nisovin.magicspells.MagicSpells;
import com.nisovin.magicspells.spells.InstantSpell;
import com.nisovin.magicspells.util.config.ConfigData;
import com.nisovin.magicspells.events.PortalEnterEvent;
import com.nisovin.magicspells.events.PortalLeaveEvent;
import com.nisovin.magicspells.events.SpellTargetEvent;
import com.nisovin.magicspells.spelleffects.EffectPosition;

Expand Down Expand Up @@ -318,14 +320,22 @@ private void onMove(PlayerMoveEvent event) {
// Enters start portal
if (checkHitbox(event.getTo(), startPortal)) {
if (!checkTeleport(pl, startPortal)) return;
teleport(endPortal.portalLocation().clone(), pl, event);

PortalEnterEvent portalEvent = new PortalEnterEvent(pl, endPortal.portalLocation(), PortalSpell.this);
if (!portalEvent.callEvent()) return;

teleport(portalEvent.getDestination(), pl, event);
return;
}

// Enters end portal
if (allowReturn && checkHitbox(event.getTo(), endPortal)) {
if (!checkTeleport(pl, endPortal)) return;
teleport(startPortal.portalLocation().clone(), pl, event);

PortalLeaveEvent portalEvent = new PortalLeaveEvent(pl, startPortal.portalLocation(), PortalSpell.this);
if (!portalEvent.callEvent()) return;

teleport(portalEvent.getDestination(), pl, event);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.nisovin.magicspells.spells.passive;

import org.bukkit.event.EventHandler;
import org.bukkit.entity.LivingEntity;

import org.jetbrains.annotations.NotNull;

import com.nisovin.magicspells.util.Name;

import com.nisovin.magicspells.util.SpellFilter;
import com.nisovin.magicspells.events.PortalEnterEvent;
import com.nisovin.magicspells.spells.passive.util.PassiveListener;

@Name("portalenter")
public class PortalEnterListener extends PassiveListener {

private SpellFilter filter;

@Override
public void initialize(@NotNull String var) {
if (var.isEmpty()) return;
filter = SpellFilter.fromString(var);
}

@EventHandler
public void onEnter(PortalEnterEvent event) {
if (!isCancelStateOk(event.isCancelled())) return;

LivingEntity entity = event.getEntity();
if (!canTrigger(entity)) return;

if (filter != null && !filter.check(event.getPortalSpell())) return;

boolean casted = passiveSpell.activate(entity);
if (cancelDefaultAction(casted)) event.setCancelled(true);
}

}
Loading

0 comments on commit e2769b7

Please sign in to comment.