Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Entity Teleport event. #6530

Merged
15 changes: 15 additions & 0 deletions src/main/java/ch/njol/skript/classes/data/BukkitEventValues.java
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@
import org.bukkit.event.entity.EntityPickupItemEvent;
import org.bukkit.event.entity.EntityResurrectEvent;
import org.bukkit.event.entity.EntityTameEvent;
import org.bukkit.event.entity.EntityTeleportEvent;
import org.bukkit.event.entity.EntityTransformEvent;
import org.bukkit.event.entity.EntityTransformEvent.TransformReason;
import org.bukkit.event.entity.FireworkExplodeEvent;
Expand Down Expand Up @@ -616,6 +617,20 @@ public Entity get(final EntityTameEvent e) {
}
}, 0);

// EntityTeleportEvent
EventValues.registerEventValue(EntityTeleportEvent.class, Location.class, new Getter<Location, EntityTeleportEvent>() {
@Override
public @Nullable Location get(EntityTeleportEvent event) {
return event.getFrom();
}
}, EventValues.TIME_PAST);
EventValues.registerEventValue(EntityTeleportEvent.class, Location.class, new Getter<Location, EntityTeleportEvent>() {
@Override
public @Nullable Location get(EntityTeleportEvent event) {
return event.getTo();
}
}, EventValues.TIME_NOW);

// EntityChangeBlockEvent
EventValues.registerEventValue(EntityChangeBlockEvent.class, Block.class, new Getter<Block, EntityChangeBlockEvent>() {
@Override
Expand Down
104 changes: 104 additions & 0 deletions src/main/java/ch/njol/skript/events/EvtTeleport.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/**
* This file is part of Skript.
*
* Skript is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Skript is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Skript. If not, see <http://www.gnu.org/licenses/>.
*
* Copyright Peter Güttinger, SkriptLang team and contributors
*/
package ch.njol.skript.events;

import ch.njol.skript.Skript;
import ch.njol.skript.lang.Expression;
import ch.njol.skript.lang.Literal;
import ch.njol.skript.lang.SkriptEvent;
import ch.njol.skript.lang.SkriptParser;
import ch.njol.skript.entity.EntityType;
import ch.njol.util.coll.CollectionUtils;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.event.Event;
import org.bukkit.event.entity.EntityTeleportEvent;
import org.bukkit.event.player.PlayerTeleportEvent;

import javax.annotation.Nullable;

public class EvtTeleport extends SkriptEvent {

static {
Skript.registerEvent("Teleport", EvtTeleport.class, CollectionUtils.array(EntityTeleportEvent.class, PlayerTeleportEvent.class), "entity teleport[ing] [of %entitytypes%]", "[player] teleport[ing]")
.description("This event can be used to teleport non-player or player entities respectively", "When teleporting entities, the event may also be called due to a result of natural causes, such as an Enderman or Shulker teleporting, or Wolfs teleporting to players.", "When teleporting players, the event can be called by teleporting through a nether/end portal, or by other means (e.g. plugins).")
sovdeeth marked this conversation as resolved.
Show resolved Hide resolved
.examples(
"on player teleport:",
"on entity teleport:",
"on entity teleport of creeper:"
sovdeeth marked this conversation as resolved.
Show resolved Hide resolved
)
.since("1.0, INSERT VERSION (entity teleport)");
}

@SuppressWarnings("unchecked")
private Expression<EntityType> entities;

private int matchedPattern;
@Override
public boolean init(Literal<?>[] args, int matchedPattern, SkriptParser.ParseResult parseResult) {
sovdeeth marked this conversation as resolved.
Show resolved Hide resolved
this.matchedPattern = matchedPattern;
if (matchedPattern == 0 && args[0] != null) {
entities = (Expression<EntityType>) args[0];
}
sovdeeth marked this conversation as resolved.
Show resolved Hide resolved
return true;
}


@Override
public boolean check(Event event) {
Entity entity;
if (event instanceof EntityTeleportEvent) {
EntityTeleportEvent entityEvent = (EntityTeleportEvent) event;
entity = entityEvent.getEntity();
if (entity instanceof Player) {
return false;
}
} else if (event instanceof PlayerTeleportEvent) {
PlayerTeleportEvent playerEvent = (PlayerTeleportEvent) event;
entity = playerEvent.getPlayer();
if (matchedPattern == 0) {
return false; // Exclude players from "entity teleport event"
}
} else {
return false;
}
if (entities != null) {
for (EntityType entType : entities.getArray(event)) {
sovdeeth marked this conversation as resolved.
Show resolved Hide resolved
if (entType.isInstance(entity)) {
return true;
}
sovdeeth marked this conversation as resolved.
Show resolved Hide resolved
Asleeepp marked this conversation as resolved.
Show resolved Hide resolved
}
} else {
return true;
}
return false;
Asleeepp marked this conversation as resolved.
Show resolved Hide resolved
Asleeepp marked this conversation as resolved.
Show resolved Hide resolved
}



public String toString(@Nullable Event e, boolean debug) {
if (entities != null) {
return "on entity teleport of " + entities.toString(e, debug);
} else if (e instanceof PlayerTeleportEvent) {
return "on player teleport";
sovdeeth marked this conversation as resolved.
Show resolved Hide resolved
} else {
return "on entity teleport";
}
}
Asleeepp marked this conversation as resolved.
Show resolved Hide resolved
}
4 changes: 0 additions & 4 deletions src/main/java/ch/njol/skript/events/SimpleEvents.java
Original file line number Diff line number Diff line change
Expand Up @@ -325,10 +325,6 @@ public class SimpleEvents {
.description("Called when a player respawns. You should prefer this event over the <a href='#death'>death event</a> as the player is technically alive when this event is called.")
.examples("on respawn:")
.since("1.0");
Skript.registerEvent("Teleport", SimpleEvent.class, PlayerTeleportEvent.class, "[player] teleport[ing]")
.description("Called whenever a player is teleported, either by a nether/end portal or other means (e.g. by plugins).")
.examples("on teleport:")
.since("1.0");
Skript.registerEvent("Sneak Toggle", SimpleEvent.class, PlayerToggleSneakEvent.class, "[player] toggl(e|ing) sneak", "[player] sneak toggl(e|ing)")
.description("Called when a player starts or stops sneaking. Use <a href='conditions.html#CondIsSneaking'>is sneaking</a> to get whether the player was sneaking before the event was called.")
.examples("# make players that stop sneaking jump",
Expand Down