Skip to content

Commit

Permalink
[CORE] Readd Task Scheduler, Add Critical Menu Things.
Browse files Browse the repository at this point in the history
Signed-off-by: Wolfieheart <[email protected]>
  • Loading branch information
Wolfieheart committed Jan 3, 2025
1 parent da7eccf commit f14feaa
Show file tree
Hide file tree
Showing 5 changed files with 762 additions and 9 deletions.
100 changes: 100 additions & 0 deletions src/main/java/io/github/rypofalem/armorstandeditor/Scheduler.java
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);
}
}

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.github.rypofalem.armorstandeditor.api.events;
package io.github.rypofalem.armorstandeditor.api.events.editor;

import lombok.Getter;
import lombok.Setter;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,15 @@

import static java.util.Objects.requireNonNull;


//https://github.com/Tonierbobcat/ArmorStandEditor/blob/65d4a847118e6eb942e0f04a08699b03b9b79eff/src/main/java/io/github/rypofalem/armorstandeditor/menu/EquipmentMenu.java
//https://github.com/StarTux/ArmorStandEditor/blob/master/src/main/java/io/github/rypofalem/armorstandeditor/menu/EquipmentMenu.java#L51
public class EquipmentMenu implements EditorMenu{

Inventory menuInv;
private ArmorStand armorStand;
private PlayerEditor pe;
static String menuName = "ArmorStand Eqiupment";
ItemStack helm, chest, pants, boots, rHand, lHand;

public EquipmentMenu(PlayerEditor pe, ArmorStand as){
this.pe = pe;
Expand Down
Loading

0 comments on commit f14feaa

Please sign in to comment.