Created
October 18, 2015 08:33
-
-
Save aikar/422e8a0b3dabf755bc4a to your computer and use it in GitHub Desktop.
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
/* | |
* Copyright (c) 2015. Starlis LLC / dba Empire Minecraft | |
* | |
* This source code is proprietary software and must not be redistributed without Starlis LLC's approval | |
* | |
*/ | |
package com.empireminecraft.util; | |
import lombok.NonNull; | |
import org.bukkit.Bukkit; | |
import org.bukkit.event.HandlerList; | |
import org.bukkit.scheduler.BukkitTask; | |
import java.util.HashMap; | |
import java.util.concurrent.Callable; | |
public class BukkitUtil { | |
private static final HashMap<String, org.bukkit.event.Listener> _listeners = new HashMap<>(); | |
public static void registerListener(@NonNull org.bukkit.event.Listener listener) { | |
Bukkit.getServer().getPluginManager().registerEvents(listener, Util.getEmpire()); | |
} | |
public static void registerListener(@NonNull String name, @NonNull org.bukkit.event.Listener listener) { | |
unregisterListener(name); | |
_listeners.put(name, listener); | |
registerListener(listener); | |
} | |
public static void unregisterListener(@NonNull String name) { | |
org.bukkit.event.Listener listener = _listeners.get(name); | |
if (listener != null) { | |
unregisterListener(listener); | |
} | |
} | |
public static void unregisterListener(@NonNull org.bukkit.event.Listener listener) { | |
HandlerList.unregisterAll(listener); | |
} | |
public static void unregisterListeners() { | |
HandlerList.unregisterAll(Util.getEmpire()); | |
_listeners.clear(); | |
} | |
public static BukkitTask runTaskTimer(@NonNull Runnable run, long start, long repeat) { | |
return Bukkit.getServer().getScheduler().runTaskTimer(Util.getEmpire(), run, start, repeat); | |
} | |
public static BukkitTask runTaskTimer(@NonNull Runnable run, long repeat) { | |
return Bukkit.getServer().getScheduler().runTaskTimer(Util.getEmpire(), run, 0, repeat); | |
} | |
public static BukkitTask runTaskTimerAsync(@NonNull Runnable run, long start, long repeat) { | |
return Bukkit.getServer().getScheduler().runTaskTimerAsynchronously(Util.getEmpire(), run, start, repeat); | |
} | |
public static BukkitTask runTaskTimerAsync(@NonNull Runnable run, long repeat) { | |
return Bukkit.getServer().getScheduler().runTaskTimerAsynchronously(Util.getEmpire(), run, 0, repeat); | |
} | |
public static int scheduleTask(@NonNull Runnable run, long delay) { | |
return Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(Util.getEmpire(), run, delay); | |
} | |
public static int scheduleAsyncTask(@NonNull Runnable run, long delay) { | |
return Bukkit.getServer().getScheduler().scheduleAsyncDelayedTask(Util.getEmpire(), run, delay); | |
} | |
public static BukkitTask runTask(@NonNull Runnable run) { | |
if (!Util.getEmpire().isEnabled()) { | |
return null; | |
} | |
return Bukkit.getServer().getScheduler().runTask(Util.getEmpire(), run); | |
} | |
public static <T> T runTaskSync(@NonNull Callable<T> run) throws Exception { | |
return Bukkit.getScheduler().callSyncMethod(Util.getEmpire(), run).get(); | |
} | |
public static int runTaskNextTick(@NonNull Runnable run) { | |
if (!Util.getEmpire().isEnabled()) { | |
run.run(); | |
return 0; | |
} | |
return scheduleTask(run, 1); | |
} | |
public static void runTaskAsync(@NonNull Runnable run) { | |
if (!Util.getEmpire().isEnabled()) { | |
run.run(); | |
return; | |
} | |
Bukkit.getScheduler().runTaskAsynchronously(Util.getEmpire(), run); | |
} | |
public static class Listener implements org.bukkit.event.Listener { | |
public Listener() { | |
BukkitUtil.registerListener(this); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment