mirror of
https://github.com/IntellectualSites/PlotSquared.git
synced 2025-12-28 12:48:47 +01:00
Begin work on the task system
This commit is contained in:
committed by
Alexander Söderberg
parent
8eb903ad72
commit
7f412f5472
@@ -0,0 +1,46 @@
|
||||
/*
|
||||
* _____ _ _ _____ _
|
||||
* | __ \| | | | / ____| | |
|
||||
* | |__) | | ___ | |_| (___ __ _ _ _ __ _ _ __ ___ __| |
|
||||
* | ___/| |/ _ \| __|\___ \ / _` | | | |/ _` | '__/ _ \/ _` |
|
||||
* | | | | (_) | |_ ____) | (_| | |_| | (_| | | | __/ (_| |
|
||||
* |_| |_|\___/ \__|_____/ \__, |\__,_|\__,_|_| \___|\__,_|
|
||||
* | |
|
||||
* |_|
|
||||
* PlotSquared plot management system for Minecraft
|
||||
* Copyright (C) 2020 IntellectualSites
|
||||
*
|
||||
* This program 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.
|
||||
*
|
||||
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package com.plotsquared.bukkit.util.task;
|
||||
|
||||
import com.plotsquared.core.util.task.PlotSquaredTask;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.bukkit.scheduler.BukkitRunnable;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
|
||||
/**
|
||||
* Bukkit implementation of {@link PlotSquaredTask}
|
||||
*/
|
||||
@RequiredArgsConstructor
|
||||
public final class BukkitPlotSquaredTask extends BukkitRunnable implements PlotSquaredTask {
|
||||
|
||||
@Nonnull private final Runnable runnable;
|
||||
|
||||
@Override public void runTask() {
|
||||
this.runnable.run();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,94 @@
|
||||
/*
|
||||
* _____ _ _ _____ _
|
||||
* | __ \| | | | / ____| | |
|
||||
* | |__) | | ___ | |_| (___ __ _ _ _ __ _ _ __ ___ __| |
|
||||
* | ___/| |/ _ \| __|\___ \ / _` | | | |/ _` | '__/ _ \/ _` |
|
||||
* | | | | (_) | |_ ____) | (_| | |_| | (_| | | | __/ (_| |
|
||||
* |_| |_|\___/ \__|_____/ \__, |\__,_|\__,_|_| \___|\__,_|
|
||||
* | |
|
||||
* |_|
|
||||
* PlotSquared plot management system for Minecraft
|
||||
* Copyright (C) 2020 IntellectualSites
|
||||
*
|
||||
* This program 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.
|
||||
*
|
||||
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package com.plotsquared.bukkit.util.task;
|
||||
|
||||
import com.google.inject.Inject;
|
||||
import com.google.inject.Singleton;
|
||||
import com.plotsquared.bukkit.BukkitPlatform;
|
||||
import com.plotsquared.core.util.task.PlotSquaredTask;
|
||||
import com.plotsquared.core.util.task.TaskManager;
|
||||
import com.plotsquared.core.util.task.TaskTime;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
|
||||
/**
|
||||
* Bukkit implementation of {@link TaskManager} using
|
||||
* by {@link org.bukkit.scheduler.BukkitScheduler} and {@link BukkitPlotSquaredTask}
|
||||
*/
|
||||
@Singleton public class BukkitTaskManager extends TaskManager {
|
||||
|
||||
private final BukkitPlatform bukkitMain;
|
||||
private final TaskTime.TimeConverter timeConverter;
|
||||
|
||||
@Inject public BukkitTaskManager(@Nonnull final BukkitPlatform bukkitMain,
|
||||
@Nonnull final TaskTime.TimeConverter timeConverter) {
|
||||
this.bukkitMain = bukkitMain;
|
||||
this.timeConverter = timeConverter;
|
||||
}
|
||||
|
||||
@Override
|
||||
public PlotSquaredTask taskRepeat(@Nonnull final Runnable runnable,
|
||||
@Nonnull final TaskTime taskTime) {
|
||||
final long ticks = this.timeConverter.toTicks(taskTime);
|
||||
final BukkitPlotSquaredTask bukkitPlotSquaredTask = new BukkitPlotSquaredTask(runnable);
|
||||
bukkitPlotSquaredTask.runTaskTimer(this.bukkitMain, ticks, ticks);
|
||||
return bukkitPlotSquaredTask;
|
||||
}
|
||||
|
||||
@Override
|
||||
public PlotSquaredTask taskRepeatAsync(@Nonnull final Runnable runnable,
|
||||
@Nonnull final TaskTime taskTime) {
|
||||
final long ticks = this.timeConverter.toTicks(taskTime);
|
||||
final BukkitPlotSquaredTask bukkitPlotSquaredTask = new BukkitPlotSquaredTask(runnable);
|
||||
bukkitPlotSquaredTask.runTaskTimerAsynchronously(this.bukkitMain, ticks, ticks);
|
||||
return bukkitPlotSquaredTask;
|
||||
}
|
||||
|
||||
@Override public void taskAsync(@Nonnull final Runnable runnable) {
|
||||
if (this.bukkitMain.isEnabled()) {
|
||||
new BukkitPlotSquaredTask(runnable).runTaskAsynchronously(this.bukkitMain);
|
||||
} else {
|
||||
runnable.run();
|
||||
}
|
||||
}
|
||||
|
||||
@Override public void task(@Nonnull final Runnable runnable) {
|
||||
new BukkitPlotSquaredTask(runnable).runTaskAsynchronously(this.bukkitMain);
|
||||
}
|
||||
|
||||
@Override public void taskLater(@Nonnull final Runnable runnable,
|
||||
@Nonnull final TaskTime taskTime) {
|
||||
final long delay = this.timeConverter.toTicks(taskTime);
|
||||
new BukkitPlotSquaredTask(runnable).runTaskLater(this.bukkitMain, delay);
|
||||
}
|
||||
|
||||
@Override public void taskLaterAsync(@Nonnull final Runnable runnable,
|
||||
@Nonnull final TaskTime taskTime) {
|
||||
final long delay = this.timeConverter.toTicks(taskTime);
|
||||
new BukkitPlotSquaredTask(runnable).runTaskLaterAsynchronously(this.bukkitMain, delay);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
/*
|
||||
* _____ _ _ _____ _
|
||||
* | __ \| | | | / ____| | |
|
||||
* | |__) | | ___ | |_| (___ __ _ _ _ __ _ _ __ ___ __| |
|
||||
* | ___/| |/ _ \| __|\___ \ / _` | | | |/ _` | '__/ _ \/ _` |
|
||||
* | | | | (_) | |_ ____) | (_| | |_| | (_| | | | __/ (_| |
|
||||
* |_| |_|\___/ \__|_____/ \__, |\__,_|\__,_|_| \___|\__,_|
|
||||
* | |
|
||||
* |_|
|
||||
* PlotSquared plot management system for Minecraft
|
||||
* Copyright (C) 2020 IntellectualSites
|
||||
*
|
||||
* This program 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.
|
||||
*
|
||||
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package com.plotsquared.bukkit.util.task;
|
||||
|
||||
import com.plotsquared.core.util.task.TaskTime;
|
||||
import org.bukkit.Bukkit;
|
||||
|
||||
import javax.annotation.Nonnegative;
|
||||
|
||||
/**
|
||||
* Time converter that uses the server MSPT count to convert between
|
||||
* different time units
|
||||
*/
|
||||
public final class PaperTimeConverter implements TaskTime.TimeConverter {
|
||||
|
||||
@Override public long msToTicks(@Nonnegative final long ms) {
|
||||
return (long) (ms / Bukkit.getAverageTickTime());
|
||||
}
|
||||
|
||||
@Override public long ticksToMs(@Nonnegative final long ticks) {
|
||||
return (long) (ticks * Bukkit.getAverageTickTime());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
/*
|
||||
* _____ _ _ _____ _
|
||||
* | __ \| | | | / ____| | |
|
||||
* | |__) | | ___ | |_| (___ __ _ _ _ __ _ _ __ ___ __| |
|
||||
* | ___/| |/ _ \| __|\___ \ / _` | | | |/ _` | '__/ _ \/ _` |
|
||||
* | | | | (_) | |_ ____) | (_| | |_| | (_| | | | __/ (_| |
|
||||
* |_| |_|\___/ \__|_____/ \__, |\__,_|\__,_|_| \___|\__,_|
|
||||
* | |
|
||||
* |_|
|
||||
* PlotSquared plot management system for Minecraft
|
||||
* Copyright (C) 2020 IntellectualSites
|
||||
*
|
||||
* This program 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.
|
||||
*
|
||||
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package com.plotsquared.bukkit.util.task;
|
||||
|
||||
import com.plotsquared.core.util.task.TaskTime;
|
||||
|
||||
import javax.annotation.Nonnegative;
|
||||
|
||||
/**
|
||||
* Naive time converter that assumes that all ticks are 50 milliseconds
|
||||
*/
|
||||
public final class SpigotTimeConverter implements TaskTime.TimeConverter {
|
||||
|
||||
private static final long MS_PER_TICKS = 50L;
|
||||
|
||||
@Override public long msToTicks(@Nonnegative final long ms) {
|
||||
return ms / MS_PER_TICKS;
|
||||
}
|
||||
|
||||
@Override public long ticksToMs(@Nonnegative final long ticks) {
|
||||
return ticks * MS_PER_TICKS;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user