Skip to content

Commit

Permalink
feat: Portals
Browse files Browse the repository at this point in the history
Added PortalEnter/Leave events to the Portal spell. They are now cancellable, and their destination location is stored and can be modified. Added passive triggers: "portalenter" and "portalleave".
  • Loading branch information
JasperLorelai committed Mar 8, 2024
1 parent 2991961 commit 1d92baf
Show file tree
Hide file tree
Showing 8 changed files with 184 additions and 97 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 @@ -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);
}

}
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.PortalLeaveEvent;
import com.nisovin.magicspells.spells.passive.util.PassiveListener;

@Name("portalleave")
public class PortalLeaveListener extends PassiveListener {

private SpellFilter filter;

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

@EventHandler
public void onLeave(PortalLeaveEvent 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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,8 @@ private void initialize() {
addListener(PlayerAnimationListener.class);
addListener(PlayerMoveListener.class);
addListener(PlayerMoveToBlockListener.class);
addListener(PortalEnterListener.class);
addListener(PortalLeaveListener.class);
addListener(PotionEffectListener.class);
addListener(PrepareEnchantListener.class);
addListener(QuitListener.class);
Expand Down

0 comments on commit 1d92baf

Please sign in to comment.