forked from RypoFalem/ArmorStandEditor
-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[CORE] Readd Task Scheduler, Add Critical Menu Things.
Signed-off-by: Wolfieheart <[email protected]>
- Loading branch information
1 parent
da7eccf
commit f14feaa
Showing
5 changed files
with
762 additions
and
9 deletions.
There are no files selected for viewing
100 changes: 100 additions & 0 deletions
100
src/main/java/io/github/rypofalem/armorstandeditor/Scheduler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
/* | ||
* ArmorStandEditor: Bukkit plugin to allow editing armor stand attributes | ||
* Copyright (C) 2016-2023 RypoFalem | ||
* | ||
* 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 2 | ||
* 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, write to the Free Software | ||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
*/ | ||
package io.github.rypofalem.armorstandeditor; | ||
|
||
import org.bukkit.Bukkit; | ||
import org.bukkit.Location; | ||
import org.bukkit.entity.Entity; | ||
import org.bukkit.plugin.Plugin; | ||
|
||
import java.util.function.Consumer; | ||
|
||
import static io.github.rypofalem.armorstandeditor.ArmorStandEditorPlugin.isFolia; | ||
|
||
public class Scheduler { | ||
|
||
private static Object GLOBAL_REGION_SCHEDULER = null; | ||
|
||
public static <T> T callMethod(Class<?> clazz, Object object, String methodName, Class<?>[] parameterTypes, Object... args) { | ||
try { | ||
return (T) clazz.getDeclaredMethod(methodName, parameterTypes).invoke(object, args); | ||
} catch (Throwable t) { | ||
throw new IllegalStateException(t); | ||
} | ||
} | ||
|
||
public static <T> T callMethod(Object object, String methodName, Class<?>[] parameterTypes, Object... args) { | ||
return callMethod(object.getClass(), object, methodName, parameterTypes, args); | ||
} | ||
|
||
public static <T> T callMethod(Class<?> clazz, String methodName) { | ||
return callMethod(clazz, null, methodName, new Class[]{}); | ||
} | ||
|
||
private static boolean methodExist(Class<?> clazz, String methodName, Class<?>... parameterTypes) { | ||
try { | ||
clazz.getDeclaredMethod(methodName, parameterTypes); | ||
return true; | ||
} catch (Throwable ignored) { | ||
} | ||
return false; | ||
} | ||
|
||
public static Object getGlobalRegionScheduler() { | ||
if (GLOBAL_REGION_SCHEDULER == null) { | ||
GLOBAL_REGION_SCHEDULER = callMethod(Bukkit.class, "getGlobalRegionScheduler"); | ||
} | ||
return GLOBAL_REGION_SCHEDULER; | ||
} | ||
|
||
public static void runTask(Plugin plugin, Runnable runnable) { | ||
if (isFolia()) { | ||
Object globalRegionScheduler = getGlobalRegionScheduler(); | ||
callMethod(globalRegionScheduler, "run", new Class[]{Plugin.class, Consumer.class}, plugin, (Consumer<?>) (task) -> runnable.run()); | ||
return; | ||
} | ||
Bukkit.getScheduler().runTask(plugin, runnable); | ||
} | ||
|
||
public static void runTaskTimer(Plugin plugin, Runnable runnable, long initialDelayTicks, long periodTicks) { | ||
if (isFolia()) { | ||
Object globalRegionScheduler = getGlobalRegionScheduler(); | ||
callMethod(globalRegionScheduler, "runAtFixedRate", new Class[]{Plugin.class, Consumer.class, long.class, long.class}, | ||
plugin, (Consumer<?>) (task) -> runnable.run(), initialDelayTicks, periodTicks); | ||
return; | ||
} | ||
Bukkit.getScheduler().runTaskTimerAsynchronously(plugin, runnable, initialDelayTicks, periodTicks); | ||
} | ||
|
||
public static void runTaskLater(Plugin plugin, Runnable runnable, long delayedTicks) { | ||
if (isFolia()) { | ||
Object globalRegionScheduler = getGlobalRegionScheduler(); | ||
callMethod(globalRegionScheduler, "runDelayed", new Class[]{Plugin.class, Consumer.class, long.class}, | ||
plugin, (Consumer<?>) (task) -> runnable.run(), delayedTicks); | ||
return; | ||
} | ||
Bukkit.getScheduler().runTaskLater(plugin, runnable, delayedTicks); | ||
} | ||
|
||
public static void teleport(Entity entity, Location location) { | ||
if (isFolia()) callMethod(Entity.class, entity, "teleportAsync", new Class[]{Location.class}, location); | ||
else entity.teleport(location); | ||
} | ||
} | ||
|
2 changes: 1 addition & 1 deletion
2
...ditor/api/events/PlayerOpenMenuEvent.java → ...pi/events/editor/PlayerOpenMenuEvent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.