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 6b3624b commit 51f56d2
Show file tree
Hide file tree
Showing 11 changed files with 194 additions and 31 deletions.
168 changes: 168 additions & 0 deletions src/main/java/cc/baka9/catseedlogin/bukkit/CatScheduler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
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;

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){
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) {
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);
}


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

// 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<>();

// 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));
}
}

// 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();
}
}
}

// 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);
}
}

// 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);
}
}
}
6 changes: 3 additions & 3 deletions src/main/java/cc/baka9/catseedlogin/bukkit/CatSeedLogin.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import org.bukkit.Bukkit;
import org.bukkit.command.PluginCommand;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.scheduler.BukkitScheduler;

import cc.baka9.catseedlogin.bukkit.command.CommandBindEmail;
import cc.baka9.catseedlogin.bukkit.command.CommandCatSeedLogin;
Expand All @@ -25,13 +24,14 @@
public class CatSeedLogin extends JavaPlugin {

public static CatSeedLogin instance;
public static BukkitScheduler scheduler = Bukkit.getScheduler();
public static SQL sql;
public static boolean loadProtocolLib = false;
public static boolean folia = CatScheduler.folia;

@Override
public void onEnable(){
instance = this;
if (folia) getLogger().warning("检测到Folia,注意目前版本对Folia的支持还不稳定!");
//Config
try {
Config.load();
Expand Down Expand Up @@ -149,7 +149,7 @@ public void onDisable(){
}

public void runTaskAsync(Runnable runnable){
scheduler.runTaskAsynchronously(this, runnable);
CatScheduler.runTaskAsync(runnable);
}


Expand Down
4 changes: 2 additions & 2 deletions src/main/java/cc/baka9/catseedlogin/bukkit/Communication.java
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ private static void handleKeepLoggedInRequest(String playerName, String time, St
// 对比玩家名,时间戳,和authKey加密的结果(加密是因为如果登录服不在内网环境下,则可能会被人使用这个功能给发包来直接绕过登录)
if (CommunicationAuth.encryption(playerName, time, Config.BungeeCord.AuthKey).equals(sign)) {
// 切换主线程给予登录状态
Bukkit.getScheduler().runTask(CatSeedLogin.instance, () -> {
CatScheduler.runTask( () -> {
LoginPlayer lp = Cache.getIgnoreCase(playerName);
if (lp != null) {
LoginPlayerHelper.add(lp);
Expand All @@ -124,7 +124,7 @@ private static void handleKeepLoggedInRequest(String playerName, String time, St

private static void handleConnectRequest(Socket socket, String playerName) {
// 切换主线程获取是否已登录
Bukkit.getScheduler().runTask(CatSeedLogin.instance, () -> {
CatScheduler.runTask( () -> {
boolean result = LoginPlayerHelper.isLogin(playerName);

// 切换异步线程返回结果
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/cc/baka9/catseedlogin/bukkit/Listeners.java
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ public void onPlayerMove(PlayerMoveEvent event) {
}

if (Config.Settings.CanTpSpawnLocation) {
player.teleport(Config.Settings.SpawnLocation);
CatScheduler.teleport(player,Config.Settings.SpawnLocation);
} else {
event.setCancelled(true);
}
Expand All @@ -182,7 +182,7 @@ public void onPlayerQuit(PlayerQuitEvent event) {
if (!player.isDead() || Config.Settings.DeathStateQuitRecordLocation) {
Config.setOfflineLocation(player);
}
Bukkit.getScheduler().runTaskLater(CatSeedLogin.instance, () -> LoginPlayerHelper.remove(player.getName()), Config.Settings.ReenterInterval);
CatScheduler.runTaskLater(() -> LoginPlayerHelper.remove(player.getName()), Config.Settings.ReenterInterval);
}
Task.getTaskAutoKick().playerJoinTime.remove(player.getName());

Expand All @@ -201,7 +201,7 @@ public void onPlayerJoin(PlayerJoinEvent event) {
}
Cache.refresh(p.getName());
if (Config.Settings.CanTpSpawnLocation) {
p.teleport(Config.Settings.SpawnLocation);
CatScheduler.teleport(p,Config.Settings.SpawnLocation);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

import cc.baka9.catseedlogin.bukkit.CatScheduler;
import cc.baka9.catseedlogin.bukkit.CatSeedLogin;
import cc.baka9.catseedlogin.bukkit.Communication;
import cc.baka9.catseedlogin.bukkit.Config;
Expand Down Expand Up @@ -264,7 +265,7 @@ private boolean delPlayer(CommandSender sender, String[] args){
CatSeedLogin.sql.del(lp.getName());
LoginPlayerHelper.remove(lp);
sender.sendMessage("§e已删除账户 §a" + lp.getName());
Bukkit.getScheduler().runTask(CatSeedLogin.instance, () -> {
CatScheduler.runTask(() -> {
Player p = Bukkit.getPlayerExact(lp.getName());
if (p != null && p.isOnline()) {
p.kickPlayer("§c你的账户已被删除!");
Expand Down Expand Up @@ -316,7 +317,7 @@ private boolean setPwd(CommandSender sender, String[] args){
LoginPlayerHelper.remove(lp);
sender.sendMessage(String.join(" ", "§a玩家", lp.getName(), "密码已设置"));
LoginPlayer finalLp = lp;
Bukkit.getScheduler().runTask(CatSeedLogin.instance, () -> {
CatScheduler.runTask(() -> {
Player p = Bukkit.getPlayer(finalLp.getName());
if (p != null && p.isOnline()) {
p.sendMessage("§c密码已被管理员重新设置,请重新登录");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

import cc.baka9.catseedlogin.bukkit.CatScheduler;
import cc.baka9.catseedlogin.bukkit.CatSeedLogin;
import cc.baka9.catseedlogin.bukkit.Config;
import cc.baka9.catseedlogin.bukkit.database.Cache;
Expand Down Expand Up @@ -62,7 +63,7 @@ public boolean onCommand(CommandSender sender, Command command, String lable, St
CatSeedLogin.sql.edit(lp);
LoginPlayerHelper.remove(lp);

Bukkit.getScheduler().runTask(CatSeedLogin.instance, () -> {
CatScheduler.runTask(() -> {
Player player = Bukkit.getPlayer(((Player) sender).getUniqueId());
if (player != null && player.isOnline()) {
player.sendMessage(Config.Language.CHANGEPASSWORD_SUCCESS);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

import cc.baka9.catseedlogin.bukkit.CatScheduler;
import cc.baka9.catseedlogin.bukkit.Config;
import cc.baka9.catseedlogin.bukkit.database.Cache;
import cc.baka9.catseedlogin.bukkit.event.CatSeedPlayerLoginEvent;
Expand Down Expand Up @@ -44,7 +45,7 @@ public boolean onCommand(CommandSender sender, Command command, String lable, St
player.updateInventory();
LoginPlayerHelper.recordCurrentIP(player, lp);
if (Config.Settings.AfterLoginBack && Config.Settings.CanTpSpawnLocation) {
Config.getOfflineLocation(player).ifPresent(player::teleport);
Config.getOfflineLocation(player).ifPresent(location -> CatScheduler.teleport(player,location));
}
} else {
sender.sendMessage(Config.Language.LOGIN_FAIL);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

import cc.baka9.catseedlogin.bukkit.CatScheduler;
import cc.baka9.catseedlogin.bukkit.CatSeedLogin;
import cc.baka9.catseedlogin.bukkit.Config;
import cc.baka9.catseedlogin.bukkit.database.Cache;
Expand Down Expand Up @@ -62,7 +63,7 @@ public boolean onCommand(CommandSender sender, Command command, String lable, St
lp.crypt();
CatSeedLogin.sql.add(lp);
LoginPlayerHelper.add(lp);
Bukkit.getScheduler().runTask(CatSeedLogin.instance, () -> {
CatScheduler.runTask(() -> {
CatSeedPlayerRegisterEvent event = new CatSeedPlayerRegisterEvent(Bukkit.getPlayer(sender.getName()));
Bukkit.getServer().getPluginManager().callEvent(event);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

import cc.baka9.catseedlogin.bukkit.CatScheduler;
import cc.baka9.catseedlogin.bukkit.CatSeedLogin;
import cc.baka9.catseedlogin.bukkit.Config;
import cc.baka9.catseedlogin.bukkit.database.Cache;
Expand Down Expand Up @@ -91,12 +92,12 @@ public boolean onCommand(CommandSender sender, Command command, String s, String
CatSeedLogin.sql.edit(lp);
LoginPlayerHelper.remove(lp);
EmailCode.removeByName(name, EmailCode.Type.ResetPassword);
Bukkit.getScheduler().runTask(CatSeedLogin.instance, () -> {
CatScheduler.runTask(() -> {
Player p = Bukkit.getPlayer(lp.getName());
if (p != null && p.isOnline()) {
if (Config.Settings.CanTpSpawnLocation) {
// PlayerTeleport.teleport(p, Config.Settings.SpawnLocation);
p.teleport(Config.Settings.SpawnLocation);
// PlayerTeleport.teleport(p, Config.Settings.SpawnLocation);
CatScheduler.teleport(p,Config.Settings.SpawnLocation);
}
p.sendMessage(Config.Language.RESETPASSWORD_SUCCESS);
if (CatSeedLogin.loadProtocolLib) {
Expand All @@ -106,7 +107,7 @@ public boolean onCommand(CommandSender sender, Command command, String s, String

});
} catch (Exception e) {
Bukkit.getScheduler().runTask(CatSeedLogin.instance, () -> sender.sendMessage("§c数据库异常!"));
CatScheduler.runTask( () -> sender.sendMessage("§c数据库异常!"));
e.printStackTrace();
}

Expand Down
Loading

0 comments on commit 51f56d2

Please sign in to comment.