Skip to content

Commit

Permalink
feat
Browse files Browse the repository at this point in the history
  • Loading branch information
shulng committed Jul 24, 2024
1 parent 51f56d2 commit 1a04f80
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 138 deletions.
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"java.compile.nullAnalysis.mode": "automatic"
"java.compile.nullAnalysis.mode": "automatic",
"java.configuration.updateBuildConfiguration": "automatic"
}
161 changes: 30 additions & 131 deletions src/main/java/cc/baka9/catseedlogin/bukkit/CatScheduler.java
Original file line number Diff line number Diff line change
@@ -1,168 +1,67 @@
package cc.baka9.catseedlogin.bukkit;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.function.Consumer;

import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;
import org.bukkit.scheduler.BukkitTask;
import space.arim.morepaperlib.scheduling.ScheduledTask;

public class CatScheduler {
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class CatScheduler {
// folia check
public static boolean folia = isFolia();
public static boolean isFolia(){
try {
Class.forName("io.papermc.paper.threadedregions.RegionizedServer");
Class.forName("io.papermc.paper.threadedregions.RegionizedServerInitEvent");
return true;
} catch (ClassNotFoundException e) {
return false;
}
}


private static Object asyncScheduler;
private static Object globalRegionScheduler;
private static Method runNow;
private static Method runAtFixedRate;
private static Method runDelayed;
private static Method run;
private static Method cancel;
private static Method teleportAsync;
private static Class<?> scheduledTask;


static {
// init reflect for folia
if (folia){
if (folia) {
try {

// folia scheduler
String asyncSchedulerName = "io.papermc.paper.threadedregions.scheduler.AsyncScheduler";
String globalRegionSchedulerName = "io.papermc.paper.threadedregions.scheduler.GlobalRegionScheduler";
String scheduledTaskName = "io.papermc.paper.threadedregions.scheduler.ScheduledTask";

Method getAsyncScheduler = Bukkit.class.getMethod("getAsyncScheduler");
Method getGlobalRegionScheduler = Bukkit.class.getMethod("getGlobalRegionScheduler");

scheduledTask = Class.forName(scheduledTaskName);

asyncScheduler = getAsyncScheduler.invoke(Bukkit.class);
globalRegionScheduler = getGlobalRegionScheduler.invoke(Bukkit.class);

runNow = Class.forName(asyncSchedulerName).getMethod("runNow", Plugin.class, Consumer.class);
runAtFixedRate = Class.forName(globalRegionSchedulerName).getMethod("runAtFixedRate", Plugin.class, Consumer.class, long.class, long.class);
runDelayed = Class.forName(globalRegionSchedulerName).getMethod("runDelayed", Plugin.class, Consumer.class, long.class);
run = Class.forName(globalRegionSchedulerName).getMethod("run", Plugin.class, Consumer.class);
cancel = scheduledTask.getMethod("cancel");

// folia async teleport
teleportAsync = Player.class.getMethod("teleportAsync", Location.class);

} catch (NoSuchMethodException | ClassNotFoundException | IllegalAccessException | InvocationTargetException e) {
} catch (NoSuchMethodException e) {
e.printStackTrace();
folia = false;
}
Bukkit.getLogger().info("[CatSeedLogin] folia support loaded");
}
}

// just teleport (for folia support)
public static void teleport(Player player, Location location){
if (folia) {
try {
teleportAsync.invoke(player,location);
} catch (IllegalAccessException | InvocationTargetException e) {
throw new RuntimeException(e);
}
} else player.teleport(location);
public static boolean isFolia() {
return CatSeedLogin.morePaperLib.scheduling().isUsingFolia();
}

public static void teleport(Player player, Location location) {
if (!folia) {
player.teleport(location);
return;
}

// for cc.baka9.catseedlogin.bukkit.CatSeedLogin#runTaskAsync
public static void runTaskAsync(Runnable runnable){
if (folia){
CatSeedLogin.morePaperLib.scheduling().entitySpecificScheduler(player).run(() -> {
try {
runNow.invoke(asyncScheduler,CatSeedLogin.instance, (Consumer<?>) task -> runnable.run());
teleportAsync.invoke(player, location);
} catch (IllegalAccessException | InvocationTargetException e) {
throw new RuntimeException(e);
}
} else {
Bukkit.getScheduler().runTaskAsynchronously(CatSeedLogin.instance, runnable);
}
}
}, null);

// from cc.baka9.catseedlogin.bukkit.task.Task
private static final List<BukkitTask> bukkitTaskList = new ArrayList<>();
}

// not type-safe
private static final List<Object> foliaTaskList = new ArrayList<>();
public static void updateInventory(Player player) {
CatSeedLogin.morePaperLib.scheduling().entitySpecificScheduler(player).run(player::updateInventory, null);
}

// for cc.baka9.catseedlogin.bukkit.task.Task#runTaskTimer
public static void runTaskTimer(Runnable runnable, long l){
if (folia){
try {
foliaTaskList.add(runAtFixedRate.invoke(globalRegionScheduler,CatSeedLogin.instance, (Consumer<?>) task -> runnable.run(), 1, l));
} catch (IllegalAccessException | InvocationTargetException e) {
throw new RuntimeException(e);
}
} else {
bukkitTaskList.add(Bukkit.getScheduler().runTaskTimer(CatSeedLogin.instance, runnable, 0, l));
}
public static ScheduledTask runTaskAsync(Runnable runnable) {
return CatSeedLogin.morePaperLib.scheduling().asyncScheduler().run(runnable);
}

// for cc.baka9.catseedlogin.bukkit.task.Task#cancelAll
public static void cancelAll(){
if(folia){
Iterator<Object> iterator = foliaTaskList.iterator();
while (iterator.hasNext()) {
Object task = iterator.next();
try {
cancel.invoke(scheduledTask.cast(task));
} catch (IllegalAccessException | InvocationTargetException e) {
throw new RuntimeException(e);
}
iterator.remove();
}
} else {
Iterator<BukkitTask> iterator = bukkitTaskList.iterator();
while (iterator.hasNext()) {
iterator.next().cancel();
iterator.remove();
}
}
public static ScheduledTask runTaskTimer(Runnable runnable, long delay, long period) {
return CatSeedLogin.morePaperLib.scheduling().globalRegionalScheduler().runAtFixedRate(runnable,delay == 0 ? 1 : delay,period);
}

// for all codes that use org.bukkit.scheduler.BukkitScheduler#runTask
public static void runTask(Runnable runnable){
if (folia){
try {
run.invoke(globalRegionScheduler,CatSeedLogin.instance, (Consumer<?>) task -> runnable.run());
} catch (IllegalAccessException | InvocationTargetException e) {
throw new RuntimeException(e);
}
} else {
Bukkit.getScheduler().runTask(CatSeedLogin.instance, runnable);
}
public static ScheduledTask runTask(Runnable runnable) {
return CatSeedLogin.morePaperLib.scheduling().globalRegionalScheduler().run(runnable);
}

// for all codes that use org.bukkit.scheduler.BukkitScheduler#runTaskLater
public static void runTaskLater(Runnable runnable, long l){
if (folia) {
try {
runDelayed.invoke(globalRegionScheduler,CatSeedLogin.instance, (Consumer<?>) task -> runnable.run(), l);
} catch (IllegalAccessException | InvocationTargetException e) {
throw new RuntimeException(e);
}
} else {
Bukkit.getScheduler().runTaskLater(CatSeedLogin.instance, runnable, l);
}
public static ScheduledTask runTaskLater(Runnable runnable, long delay) {
return CatSeedLogin.morePaperLib.scheduling().globalRegionalScheduler().runDelayed(runnable,delay);
}


}
5 changes: 3 additions & 2 deletions src/main/java/cc/baka9/catseedlogin/bukkit/CatSeedLogin.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,19 @@
import cc.baka9.catseedlogin.bukkit.database.SQLite;
import cc.baka9.catseedlogin.bukkit.object.LoginPlayerHelper;
import cc.baka9.catseedlogin.bukkit.task.Task;
import space.arim.morepaperlib.MorePaperLib;

public class CatSeedLogin extends JavaPlugin {

public static CatSeedLogin instance;
public static SQL sql;
public static boolean loadProtocolLib = false;
public static boolean folia = CatScheduler.folia;
public static MorePaperLib morePaperLib;

@Override
public void onEnable(){
instance = this;
if (folia) getLogger().warning("检测到Folia,注意目前版本对Folia的支持还不稳定!");
morePaperLib = new MorePaperLib(this);
//Config
try {
Config.load();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public boolean onCommand(CommandSender sender, Command command, String lable, St
CatSeedPlayerLoginEvent loginEvent = new CatSeedPlayerLoginEvent(player, lp.getEmail(), CatSeedPlayerLoginEvent.Result.SUCCESS);
Bukkit.getServer().getPluginManager().callEvent(loginEvent);
sender.sendMessage(Config.Language.LOGIN_SUCCESS);
player.updateInventory();
CatScheduler.updateInventory(player);
LoginPlayerHelper.recordCurrentIP(player, lp);
if (Config.Settings.AfterLoginBack && Config.Settings.CanTpSpawnLocation) {
Config.getOfflineLocation(player).ifPresent(location -> CatScheduler.teleport(player,location));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public boolean onCommand(CommandSender sender, Command command, String lable, St
Bukkit.getServer().getPluginManager().callEvent(event);
});
sender.sendMessage(Config.Language.REGISTER_SUCCESS);
player.updateInventory();
CatScheduler.updateInventory(player);
LoginPlayerHelper.recordCurrentIP(player, lp);
}

Expand Down
16 changes: 14 additions & 2 deletions src/main/java/cc/baka9/catseedlogin/bukkit/task/Task.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
package cc.baka9.catseedlogin.bukkit.task;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import cc.baka9.catseedlogin.bukkit.CatScheduler;
import cc.baka9.catseedlogin.bukkit.CatSeedLogin;
import space.arim.morepaperlib.scheduling.ScheduledTask;

public abstract class Task implements Runnable {
private static List<ScheduledTask> scheduledTasks = new ArrayList<>();
private static CatSeedLogin plugin = CatSeedLogin.instance;
protected Task(){
}

Expand Down Expand Up @@ -33,12 +41,16 @@ public static void runAll(){
}

public static void cancelAll(){
CatScheduler.cancelAll();
Iterator<ScheduledTask> iterator = scheduledTasks.iterator();
while (iterator.hasNext()) {
iterator.next().cancel();
iterator.remove();
}

}

public static void runTaskTimer(Runnable runnable, long l){
CatScheduler.runTaskTimer(runnable,l);
scheduledTasks.add(CatScheduler.runTaskTimer(runnable, 0, l));

}
}

0 comments on commit 1a04f80

Please sign in to comment.