Improves comments for Stargate events, and adds a new event for teleporting entities

Adds information about what events can be used for
Tries to clarify event comments where possible
Renames The StargatePortalEvent to StargatePlayerPortalEvent
Adds StargateEntityPortalEvent
Makes the StargateEntityPortalEvent trigger whenever a vehicle is teleported
Removes the unused event name for all events
This commit is contained in:
Kristian Knarvik 2021-10-13 15:45:15 +02:00
parent 0ab6cb52c0
commit bf7a10636e
14 changed files with 223 additions and 104 deletions

View File

@ -7,6 +7,8 @@ import org.jetbrains.annotations.NotNull;
/**
* This event should be called whenever a player attempts to access a stargate
*
* <p>This event can be used to override whether the player should be allowed to access the stargate.</p>
*/
@SuppressWarnings("unused")
public class StargateAccessEvent extends StargatePlayerEvent {
@ -17,25 +19,16 @@ public class StargateAccessEvent extends StargatePlayerEvent {
/**
* Instantiates a new stargate access event
*
* @param player <p>The player involved in the vent</p>
* @param player <p>The player involved in the event</p>
* @param portal <p>The portal involved in the event</p>
* @param deny <p>Whether the event should be denied</p>
* @param deny <p>Whether the stargate access should be denied</p>
*/
public StargateAccessEvent(Player player, Portal portal, boolean deny) {
super("StargateAccessEvent", portal, player);
super(portal, player);
this.deny = deny;
}
/**
* Gets a handler-list containing all event handlers
*
* @return <p>A handler-list with all event handlers</p>
*/
public static HandlerList getHandlerList() {
return handlers;
}
/**
* Gets whether the player should be denied access
*
@ -46,14 +39,23 @@ public class StargateAccessEvent extends StargatePlayerEvent {
}
/**
* Sets whether to deny the player
* Sets whether to deny access to the player
*
* @param deny <p>Whether to deny the player</p>
* @param deny <p>Whether to deny access to the player</p>
*/
public void setDeny(boolean deny) {
this.deny = deny;
}
/**
* Gets a handler-list containing all event handlers
*
* @return <p>A handler-list with all event handlers</p>
*/
public static HandlerList getHandlerList() {
return handlers;
}
@Override
@NotNull
public HandlerList getHandlers() {

View File

@ -9,7 +9,9 @@ import java.util.List;
/**
* This event should be called whenever a player activates a stargate
* <p>Activation of a stargate happens when a player right-clicks the sign of a stargate.</p>
*
* <p>Activation of a stargate happens when a player right-clicks the sign of a stargate.
* This event can be used to overwrite the selected destination, and all destinations the player can see.</p>
*/
@SuppressWarnings("unused")
public class StargateActivateEvent extends StargatePlayerEvent {
@ -24,24 +26,15 @@ public class StargateActivateEvent extends StargatePlayerEvent {
* @param portal <p>The activated portal</p>
* @param player <p>The player activating the portal</p>
* @param destinations <p>The destinations available to the player using the portal</p>
* @param destination <p>The chosen destination to activate</p>
* @param destination <p>The currently selected destination</p>
*/
public StargateActivateEvent(Portal portal, Player player, List<String> destinations, String destination) {
super("StargateActivateEvent", portal, player);
super(portal, player);
this.destinations = destinations;
this.destination = destination;
}
/**
* Gets a handler-list containing all event handlers
*
* @return <p>A handler-list with all event handlers</p>
*/
public static HandlerList getHandlerList() {
return handlers;
}
/**
* Gets the destinations available for the portal
*
@ -61,23 +54,32 @@ public class StargateActivateEvent extends StargatePlayerEvent {
}
/**
* Gets the chosen destination to activate
* Gets the selected destination
*
* @return <p>The chosen destination to activate</p>
* @return <p>The selected destination</p>
*/
public String getDestination() {
return destination;
}
/**
* Sets (changes) the chosen destination to activate
* Sets (changes) the selected destination
*
* @param destination <p>The new destination to activate</p>
* @param destination <p>The new selected destination</p>
*/
public void setDestination(String destination) {
this.destination = destination;
}
/**
* Gets a handler-list containing all event handlers
*
* @return <p>A handler-list with all event handlers</p>
*/
public static HandlerList getHandlerList() {
return handlers;
}
@Override
@NotNull
public HandlerList getHandlers() {

View File

@ -6,6 +6,9 @@ import org.jetbrains.annotations.NotNull;
/**
* This event should be called whenever a stargate is closed
*
* <p>This event can be used to overwrite whether the stargate should be forced to close, even if it's set as
* always-on.</p>
*/
@SuppressWarnings("unused")
public class StargateCloseEvent extends StargateEvent {
@ -20,20 +23,11 @@ public class StargateCloseEvent extends StargateEvent {
* @param force <p>Whether to force the gate to close, even if set as always-on</p>
*/
public StargateCloseEvent(Portal portal, boolean force) {
super("StargateCloseEvent", portal);
super(portal);
this.force = force;
}
/**
* Gets a handler-list containing all event handlers
*
* @return <p>A handler-list with all event handlers</p>
*/
public static HandlerList getHandlerList() {
return handlers;
}
/**
* Gets whether to force the stargate to close
*
@ -52,6 +46,15 @@ public class StargateCloseEvent extends StargateEvent {
this.force = force;
}
/**
* Gets a handler-list containing all event handlers
*
* @return <p>A handler-list with all event handlers</p>
*/
public static HandlerList getHandlerList() {
return handlers;
}
@NotNull
@Override
public HandlerList getHandlers() {

View File

@ -7,6 +7,8 @@ import org.jetbrains.annotations.NotNull;
/**
* This event should be called whenever a stargate is created
*
* <p>This event can be used to deny or change the cost of a stargate creation.</p>
*/
@SuppressWarnings("unused")
public class StargateCreateEvent extends StargatePlayerEvent {
@ -28,22 +30,13 @@ public class StargateCreateEvent extends StargatePlayerEvent {
* @param cost <p>The cost of creating the new star gate</p>
*/
public StargateCreateEvent(Player player, Portal portal, String[] lines, boolean deny, String denyReason, int cost) {
super("StargateCreateEvent", portal, player);
super(portal, player);
this.lines = lines;
this.deny = deny;
this.denyReason = denyReason;
this.cost = cost;
}
/**
* Gets a handler-list containing all event handlers
*
* @return <p>A handler-list with all event handlers</p>
*/
public static HandlerList getHandlerList() {
return handlers;
}
/**
* Gets a given line from the sign creating the star gate
*
@ -109,6 +102,15 @@ public class StargateCreateEvent extends StargatePlayerEvent {
this.cost = cost;
}
/**
* Gets a handler-list containing all event handlers
*
* @return <p>A handler-list with all event handlers</p>
*/
public static HandlerList getHandlerList() {
return handlers;
}
@NotNull
@Override
public HandlerList getHandlers() {

View File

@ -6,7 +6,9 @@ import org.jetbrains.annotations.NotNull;
/**
* This event should be called whenever a stargate is deactivated
* <p>A deactivation is usually caused by no activity for a set amount of time.</p>
*
* <p>A deactivation is usually caused by no activity for a set amount of time.
* This event can only be used to listen for de-activation events.</p>
*/
@SuppressWarnings("unused")
public class StargateDeactivateEvent extends StargateEvent {
@ -19,7 +21,7 @@ public class StargateDeactivateEvent extends StargateEvent {
* @param portal <p>The portal which was deactivated</p>
*/
public StargateDeactivateEvent(Portal portal) {
super("StargateDeactivateEvent", portal);
super(portal);
}
/**

View File

@ -7,6 +7,8 @@ import org.jetbrains.annotations.NotNull;
/**
* This event represents an event where a star gate is destroyed or attempted to be destroyed
*
* <p>This event can be used to deny or change the cost of a stargate destruction.</p>
*/
@SuppressWarnings("unused")
public class StargateDestroyEvent extends StargatePlayerEvent {
@ -19,28 +21,19 @@ public class StargateDestroyEvent extends StargatePlayerEvent {
/**
* Instantiates a new Stargate Destroy Event
*
* @param portal <p>The portal destroyed</p>
* @param portal <p>The destroyed portal</p>
* @param player <p>The player destroying the portal</p>
* @param deny <p>Whether the event should be denied (cancelled)</p>
* @param denyMsg <p>The message to display if the event is denied</p>
* @param cost <p>The cost of destroying the portal</p>
*/
public StargateDestroyEvent(Portal portal, Player player, boolean deny, String denyMsg, int cost) {
super("StargateDestroyEvent", portal, player);
super(portal, player);
this.deny = deny;
this.denyReason = denyMsg;
this.cost = cost;
}
/**
* Gets a handler-list containing all event handlers
*
* @return <p>A handler-list with all event handlers</p>
*/
public static HandlerList getHandlerList() {
return handlers;
}
/**
* Gets whether this event should be denied
*
@ -95,6 +88,15 @@ public class StargateDestroyEvent extends StargatePlayerEvent {
this.cost = cost;
}
/**
* Gets a handler-list containing all event handlers
*
* @return <p>A handler-list with all event handlers</p>
*/
public static HandlerList getHandlerList() {
return handlers;
}
@NotNull
@Override
public HandlerList getHandlers() {

View File

@ -0,0 +1,87 @@
package net.knarcraft.stargate.event;
import net.knarcraft.stargate.portal.Portal;
import org.bukkit.Location;
import org.bukkit.entity.Entity;
import org.bukkit.event.HandlerList;
import org.jetbrains.annotations.NotNull;
/**
* This event should be called whenever a non-player teleports through a stargate
*
* <p>This event can be used to overwrite the location the entity is teleported to.</p>
*/
@SuppressWarnings("unused")
public class StargateEntityPortalEvent extends StargateEvent {
private static final HandlerList handlers = new HandlerList();
Entity travellingEntity;
private final Portal destination;
private Location exit;
/**
* Instantiates a new stargate portal event
*
* @param travellingEntity <p>The entity travelling through this portal</p>
* @param portal <p>The portal the entity entered from</p>
* @param destination <p>The destination the entity should exit from</p>
* @param exit <p>The exit location of the destination portal the entity will be teleported to</p>
*/
public StargateEntityPortalEvent(Entity travellingEntity, Portal portal, Portal destination, Location exit) {
super(portal);
this.travellingEntity = travellingEntity;
this.destination = destination;
this.exit = exit;
}
/**
* Return the non-player entity teleporting
*
* @return <p>The non-player teleporting</p>
*/
public Entity getEntity() {
return travellingEntity;
}
/**
* Return the destination portal
*
* @return <p>The destination portal</p>
*/
public Portal getDestination() {
return destination;
}
/**
* Return the location of the players exit point
*
* @return <p>Location of the exit point</p>
*/
public Location getExit() {
return exit;
}
/**
* Set the location of the entity's exit point
*/
public void setExit(Location location) {
this.exit = location;
}
/**
* Gets a handler-list containing all event handlers
*
* @return <p>A handler-list with all event handlers</p>
*/
public static HandlerList getHandlerList() {
return handlers;
}
@Override
@NotNull
public HandlerList getHandlers() {
return handlers;
}
}

View File

@ -16,10 +16,9 @@ public abstract class StargateEvent extends Event implements Cancellable {
/**
* Instantiates a new stargate event
*
* @param event <p>UNUSED</p>
* @param portal <p>The portal involved in this stargate event</p>
*/
StargateEvent(String event, Portal portal) {
StargateEvent(Portal portal) {
this.portal = portal;
this.cancelled = false;
}

View File

@ -7,6 +7,8 @@ import org.jetbrains.annotations.NotNull;
/**
* This event should be called whenever a player opens a stargate
*
* <p>This event can be used to overwrite whether the stargate should be forced to open, even if it's already open.</p>
*/
@SuppressWarnings({"unused"})
public class StargateOpenEvent extends StargatePlayerEvent {
@ -18,24 +20,15 @@ public class StargateOpenEvent extends StargatePlayerEvent {
* Instantiates a new stargate open event
*
* @param player <p>The player opening the stargate</p>
* @param portal <p>The portal opened</p>
* @param portal <p>The opened portal</p>
* @param force <p>Whether to force the portal open</p>
*/
public StargateOpenEvent(Player player, Portal portal, boolean force) {
super("StargateOpenEvent", portal, player);
super(portal, player);
this.force = force;
}
/**
* Gets a handler-list containing all event handlers
*
* @return <p>A handler-list with all event handlers</p>
*/
public static HandlerList getHandlerList() {
return handlers;
}
/**
* Gets whether the portal should be forced open
*
@ -54,6 +47,15 @@ public class StargateOpenEvent extends StargatePlayerEvent {
this.force = force;
}
/**
* Gets a handler-list containing all event handlers
*
* @return <p>A handler-list with all event handlers</p>
*/
public static HandlerList getHandlerList() {
return handlers;
}
@Override
@NotNull
public HandlerList getHandlers() {

View File

@ -14,11 +14,10 @@ public abstract class StargatePlayerEvent extends StargateEvent {
/**
* Instantiates a new stargate player event
*
* @param event <p>UNUSED</p>
* @param portal <p>The portal involved in this stargate event</p>
*/
StargatePlayerEvent(String event, Portal portal, Player player) {
super(event, portal);
StargatePlayerEvent(Portal portal, Player player) {
super(portal);
this.player = player;
}

View File

@ -8,38 +8,31 @@ import org.jetbrains.annotations.NotNull;
/**
* This event should be called whenever a player teleports through a stargate
*
* <p>This event can be used to overwrite the location the player is teleported to.</p>
*/
@SuppressWarnings("unused")
public class StargatePortalEvent extends StargatePlayerEvent {
public class StargatePlayerPortalEvent extends StargatePlayerEvent {
private static final HandlerList handlers = new HandlerList();
private final Portal destination;
private Location exit;
/**
* Instantiates a new stargate portal event
* Instantiates a new stargate player portal event
*
* @param player <p>The player teleporting</p>
* @param portal <p>The portal the player entered from</p>
* @param destination <p>The destination the player should exit from</p>
* @param exit <p>The exit location of the destination portal the user will be teleported to</p>
*/
public StargatePortalEvent(Player player, Portal portal, Portal destination, Location exit) {
super("StargatePortalEvent", portal, player);
public StargatePlayerPortalEvent(Player player, Portal portal, Portal destination, Location exit) {
super(portal, player);
this.destination = destination;
this.exit = exit;
}
/**
* Gets a handler-list containing all event handlers
*
* @return <p>A handler-list with all event handlers</p>
*/
public static HandlerList getHandlerList() {
return handlers;
}
/**
* Return the destination portal
*
@ -61,8 +54,17 @@ public class StargatePortalEvent extends StargatePlayerEvent {
/**
* Set the location of the player's exit point
*/
public void setExit(Location loc) {
this.exit = loc;
public void setExit(Location location) {
this.exit = location;
}
/**
* Gets a handler-list containing all event handlers
*
* @return <p>A handler-list with all event handlers</p>
*/
public static HandlerList getHandlerList() {
return handlers;
}
@Override

View File

@ -93,7 +93,7 @@ public class PlayerEventListener implements Listener {
horse.setOwner(player);
}
}
destination.teleport((Vehicle) playerVehicle);
destination.teleport((Vehicle) playerVehicle, entrancePortal);
} else {
destination.teleport(player, entrancePortal, event);
}

View File

@ -70,7 +70,7 @@ public class VehicleEventListener implements Listener {
return;
}
Stargate.debug("vehicleTeleport", destinationPortal.getWorld() + " " + destinationPortal.getSignLocation());
destinationPortal.teleport(vehicle);
destinationPortal.teleport(vehicle, entrancePortal);
}
}
@ -109,7 +109,7 @@ public class VehicleEventListener implements Listener {
}
Stargate.sendSuccessMessage(player, Stargate.getString("teleportMsg"));
destinationPortal.teleport(vehicle);
destinationPortal.teleport(vehicle, entrancePortal);
entrancePortal.close(false);
}

View File

@ -8,8 +8,9 @@ import net.knarcraft.stargate.container.RelativeBlockVector;
import net.knarcraft.stargate.event.StargateActivateEvent;
import net.knarcraft.stargate.event.StargateCloseEvent;
import net.knarcraft.stargate.event.StargateDeactivateEvent;
import net.knarcraft.stargate.event.StargateEntityPortalEvent;
import net.knarcraft.stargate.event.StargateOpenEvent;
import net.knarcraft.stargate.event.StargatePortalEvent;
import net.knarcraft.stargate.event.StargatePlayerPortalEvent;
import net.knarcraft.stargate.utility.DirectionHelper;
import net.knarcraft.stargate.utility.EntityHelper;
import net.knarcraft.stargate.utility.SignHelper;
@ -486,17 +487,18 @@ public class Portal {
//Rotate the player to face out from the portal
adjustRotation(exit);
//Call the StargatePortalEvent to allow plugins to change destination
//Call the StargatePlayerPortalEvent to allow plugins to change destination
if (!origin.equals(this)) {
StargatePortalEvent stargatePortalEvent = new StargatePortalEvent(player, origin, this, exit);
Stargate.server.getPluginManager().callEvent(stargatePortalEvent);
StargatePlayerPortalEvent stargatePlayerPortalEvent = new StargatePlayerPortalEvent(player, origin,
this, exit);
Stargate.server.getPluginManager().callEvent(stargatePlayerPortalEvent);
//Teleport is cancelled. Teleport the player back to where it came from
if (stargatePortalEvent.isCancelled()) {
if (stargatePlayerPortalEvent.isCancelled()) {
origin.teleport(player, origin, event);
return;
}
//Update exit if needed
exit = stargatePortalEvent.getExit();
exit = stargatePlayerPortalEvent.getExit();
}
//Load chunks to make sure not to teleport to the void
@ -530,8 +532,9 @@ public class Portal {
* Teleports a vehicle to this portal
*
* @param vehicle <p>The vehicle to teleport</p>
* @param origin <p>The portal the vehicle teleports from</p>
*/
public void teleport(final Vehicle vehicle) {
public void teleport(final Vehicle vehicle, Portal origin) {
Location traveller = vehicle.getLocation();
Location exit = getExit(vehicle, traveller);
@ -547,6 +550,20 @@ public class Portal {
List<Entity> passengers = vehicle.getPassengers();
//Call the StargateEntityPortalEvent to allow plugins to change destination
if (!origin.equals(this)) {
StargateEntityPortalEvent stargateEntityPortalEvent = new StargateEntityPortalEvent(vehicle, origin,
this, exit);
Stargate.server.getPluginManager().callEvent(stargateEntityPortalEvent);
//Teleport is cancelled. Teleport the entity back to where it came from
if (stargateEntityPortalEvent.isCancelled()) {
origin.teleport(vehicle, origin);
return;
}
//Update exit if needed
exit = stargateEntityPortalEvent.getExit();
}
//Load chunks to make sure not to teleport to the void
loadChunks();