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

Make EffTeleport do teleports asynchronously on Paper #3478

Merged
merged 7 commits into from
Nov 28, 2020
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 35 additions & 10 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,20 @@ import org.apache.tools.ant.filters.ReplaceTokens

plugins {
id "com.github.hierynomus.license" version "0.14.0"
id "com.github.johnrengelman.shadow" version "5.2.0"
}

apply plugin: 'java'
apply plugin: 'maven-publish'

ext {
paperVersion = '1.16.3-R0.1-SNAPSHOT' // NOTICE: Edit this when updating to a new MC Version
}

// Substitute dependencies' versions of Bukkit with latest Paper API
configurations.all {
resolutionStrategy.dependencySubstitution {
substitute module('org.bukkit:bukkit') with module('com.destroystokyo.paper:paper-api:1.14-R0.1-SNAPSHOT')
substitute module('org.bukkit:bukkit') with module("com.destroystokyo.paper:paper-api:${paperVersion}")
}
}

Expand All @@ -28,7 +33,7 @@ allprojects {
}

maven {
url 'https://repo.destroystokyo.com/repository/maven-public//'
url 'https://papermc.io/repo/repository/maven-public/'
}

maven {
Expand All @@ -50,8 +55,10 @@ allprojects {

configurations {
ecj
shade
implementation.extendsFrom(shade)
}

dependencies {
ecj 'org.eclipse.jdt:ecj:3.15.0'
}
Expand All @@ -70,14 +77,15 @@ allprojects {
}

dependencies {
implementation 'com.destroystokyo.paper:paper-api:1.16.2-R0.1-SNAPSHOT'
implementation "com.destroystokyo.paper:paper-api:${paperVersion}"
implementation 'org.eclipse.jdt:org.eclipse.jdt.annotation:1.1.0'
implementation 'com.google.code.findbugs:findbugs:3.0.1'
implementation 'com.sk89q.worldguard:worldguard-legacy:7.0.0-SNAPSHOT'
implementation 'com.github.marcelo-mason:PreciousStones:24e3d4bf67b7240ae36b32be10e99d4091938c5c'
implementation('net.milkbowl.vault:Vault:1.7.1') {
exclude group: 'org.bstats', module: 'bstats-bukkit'
}
shade 'io.papermc:paperlib:1.0.5'

testImplementation 'junit:junit:4.12'
testImplementation 'org.easymock:easymock:3.6'
Expand All @@ -97,23 +105,40 @@ processResources {
]
}

jar {
task jar(overwrite: true, type: ShadowJar) {
archiveName System.getenv('SKRIPT_JAR_NAME') == null ? 'Skript.jar' : System.getenv("SKRIPT_JAR_NAME")
from sourceSets.main.output
}

//jar.dependsOn shadowJar
tasks.withType(ShadowJar) {
configurations = [project.configurations.shade]
dependencies {
include (dependency('io.papermc:paperlib'))
}
relocate 'io.papermc.lib', 'ch.njol.skript.paperlib'

manifest {
attributes("Name": "ch/njol/skript",
"Sealed": "true")
"Sealed": "true")
}

from('skript-aliases') {
into('aliases-english') // Change this if we get aliases in other languages
}

// Include all modules of Skript
from 'skript-worldguard6/build/classes/java/main'
from 'skript-worldguard7fawe/build/classes/java/main'
}

import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar
import com.github.jengelman.gradle.plugins.shadow.tasks.ConfigureShadowRelocation
task relocateShadowJar(type: ConfigureShadowRelocation) {
target = tasks.shadowJar
}
tasks.shadowJar.dependsOn tasks.relocateShadowJar

license {
header file('licenseheader.txt')
exclude('**/Metrics.java') // Not under GPLv3
Expand Down Expand Up @@ -236,7 +261,7 @@ task githubResources(type: ProcessResources) {
into 'build/resources/main'
}

task githubRelease(type: Jar) {
task githubRelease(type: ShadowJar) {
from sourceSets.main.output
dependsOn githubResources
archiveName = 'Skript-github.jar'
Expand Down Expand Up @@ -278,7 +303,7 @@ task spigotResources(type: ProcessResources) {
into 'build/resources/main'
}

task spigotRelease(type: Jar) {
task spigotRelease(type: ShadowJar) {
from sourceSets.main.output
dependsOn spigotResources
archiveName = 'Skript-spigot.jar'
Expand Down Expand Up @@ -315,7 +340,7 @@ task nightlyResources(type: ProcessResources) {
into 'build/resources/main'
}

task nightlyRelease(type: Jar) {
task nightlyRelease(type: ShadowJar) {
from sourceSets.main.output
dependsOn nightlyResources
dependsOn licenseMain
Expand Down
82 changes: 66 additions & 16 deletions src/main/java/ch/njol/skript/effects/EffTeleport.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,13 @@
import ch.njol.skript.lang.Effect;
import ch.njol.skript.lang.Expression;
import ch.njol.skript.lang.SkriptParser.ParseResult;
import ch.njol.skript.lang.Trigger;
import ch.njol.skript.lang.TriggerItem;
import ch.njol.skript.timings.SkriptTimings;
import ch.njol.skript.util.Direction;
import ch.njol.skript.variables.Variables;
import ch.njol.util.Kleenean;
import io.papermc.lib.PaperLib;

@Name("Teleport")
@Description("Teleport an entity to a specific location.")
Expand All @@ -61,28 +66,73 @@ public boolean init(final Expression<?>[] exprs, final int matchedPattern, final
location = Direction.combine((Expression<? extends Direction>) exprs[1], (Expression<? extends Location>) exprs[2]);
return true;
}


@Nullable
@Override
protected void execute(final Event e) {
Location to = location.getSingle(e);
if (to == null)
return;
if (Math.abs(to.getX() - to.getBlockX() - 0.5) < Skript.EPSILON && Math.abs(to.getZ() - to.getBlockZ() - 0.5) < Skript.EPSILON) {
final Block on = to.getBlock().getRelative(BlockFace.DOWN);
protected TriggerItem walk(Event e) {
debug(e, true);
TriggerItem next = getNext();
boolean delayed = Delay.isDelayed(e);
Delay.addDelayedEvent(e);

final Location loc = location.getSingle(e);
final Entity[] entityArray = entities.getArray(e); // We have to fetch this before possible async execution to avoid async local variable access.
final boolean respawnEvent = !delayed && e instanceof PlayerRespawnEvent && entityArray.length == 1 && entityArray[0].equals(((PlayerRespawnEvent) e).getPlayer());

if (respawnEvent && loc != null) {
((PlayerRespawnEvent) e).setRespawnLocation(getSafeLocation(loc));
}

if (respawnEvent || loc == null) {
continueWalk(next, e);
return null;
}

//This will either fetch the chunk instantly if on spigot or already loaded or fetch it async if on paper.
PaperLib.getChunkAtAsync(loc).thenAccept(chunk -> {
// The following is now on the main thread
for (final Entity entity : entityArray) {
entity.teleport(getSafeLocation(loc));
}

// Continue the rest of the trigger if there is one
continueWalk(next, e);
});
return null;
}

private void continueWalk(@Nullable TriggerItem next, Event e) {
Object timing = null;
if (next != null) {
if (SkriptTimings.enabled()) {
Trigger trigger = getTrigger();
if (trigger != null) {
timing = SkriptTimings.start(trigger.getDebugLabel());
}
}

TriggerItem.walk(next, e);
}
Variables.removeLocals(e); // Clean up local vars, we may be exiting now
SkriptTimings.stop(timing);
}

private Location getSafeLocation(Location loc) {
Location toLoc = loc;
if (Math.abs(toLoc.getX() - toLoc.getBlockX() - 0.5) < Skript.EPSILON && Math.abs(toLoc.getZ() - toLoc.getBlockZ() - 0.5) < Skript.EPSILON) {
final Block on = toLoc.getBlock().getRelative(BlockFace.DOWN);
if (on.getType() != Material.AIR) {
to = to.clone();
toLoc = toLoc.clone();
// TODO 1.13 block height stuff
//to.setY(on.getY() + Utils.getBlockHeight(on.getTypeId(), on.getData()));
}
}
for (final Entity entity : entities.getArray(e)) {
to.getChunk().load();
if (e instanceof PlayerRespawnEvent && entity.equals(((PlayerRespawnEvent) e).getPlayer()) && !Delay.isDelayed(e)) {
((PlayerRespawnEvent) e).setRespawnLocation(to);
} else {
entity.teleport(to);
}
}
return toLoc;
}

@Override
protected void execute(final Event e) {
// Nothing needs to happen here, we're executing in walk
}

@Override
Expand Down