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 @@ -105,6 +105,7 @@
import org.bukkit.event.entity.EntityRegainHealthEvent.RegainReason;
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 @@ -624,6 +625,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
93 changes: 93 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,93 @@
/**
* 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.Literal;
import ch.njol.skript.lang.SkriptEvent;
import ch.njol.skript.lang.SkriptParser.ParseResult;
import ch.njol.skript.entity.EntityType;
import ch.njol.util.coll.CollectionUtils;
import org.bukkit.entity.Entity;
import org.bukkit.event.Event;
import org.bukkit.event.entity.EntityTeleportEvent;
import org.bukkit.event.player.PlayerTeleportEvent;
import org.jetbrains.annotations.Nullable;

public class EvtTeleport extends SkriptEvent {

static {
Skript.registerEvent("Teleport", EvtTeleport.class, CollectionUtils.array(EntityTeleportEvent.class, PlayerTeleportEvent.class), "[%entitytypes%] teleport[ing]")
.description(
"This event can be used to listen to teleports from non-players 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 wolves 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).")
.examples(
"on teleport:",
"on player teleport:",
"on creeper teleport:"
)
.since("1.0, INSERT VERSION (entity teleport)");
}

@Nullable
private Literal<EntityType> entitiesLiteral;
private EntityType @Nullable [] entities;

@Override
public boolean init(Literal<?>[] args, int matchedPattern, ParseResult parseResult) {
if (args[0] != null) {
entitiesLiteral = ((Literal<EntityType>) args[0]); // evaluate only once
entities = entitiesLiteral.getAll();
}
return true;
}


@Override
public boolean check(Event event) {
if (event instanceof EntityTeleportEvent) {
Entity entity = ((EntityTeleportEvent) event).getEntity();
return checkEntity(entity);
} else if (event instanceof PlayerTeleportEvent) {
Entity entity = ((PlayerTeleportEvent) event).getPlayer();
return checkEntity(entity);
} else {
return false;
}
}

private boolean checkEntity(Entity entity) {
if (entities != null) {
for (EntityType entType : entities) {
if (entType.isInstance(entity))
return true;
}
return false;
}
return true;
}

public String toString(@Nullable Event event, boolean debug) {
if (entitiesLiteral != null)
return "on " + entitiesLiteral.toString(event, debug) + " teleport";
return "on 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 @@ -320,10 +320,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