1
mirror of https://github.com/CarmJos/MoeTeleport.git synced 2024-09-19 21:35:56 +00:00

[3.1.1] 版本修复

- `[R]` 重写存储源初始化方法。
- `[F]` 修复服务器在未安装 Essentials 时出现无法加载插件的问题。
This commit is contained in:
Carm Jos 2022-04-03 20:54:38 +08:00
parent 61d97c2df4
commit 7cf0b9ed01
12 changed files with 75 additions and 121 deletions

View File

@ -15,7 +15,7 @@
<groupId>cc.carm.plugin</groupId> <groupId>cc.carm.plugin</groupId>
<artifactId>moeteleport</artifactId> <artifactId>moeteleport</artifactId>
<version>3.1.0</version> <version>3.1.1</version>
<name>MoeTeleport</name> <name>MoeTeleport</name>
<description>喵喵传送,简单的传送、设置家的插件。</description> <description>喵喵传送,简单的传送、设置家的插件。</description>
@ -179,7 +179,7 @@
<dependency> <dependency>
<groupId>org.jetbrains</groupId> <groupId>org.jetbrains</groupId>
<artifactId>annotations</artifactId> <artifactId>annotations</artifactId>
<version>22.0.0</version> <version>23.0.0</version>
<scope>provided</scope> <scope>provided</scope>
</dependency> </dependency>

View File

@ -34,10 +34,11 @@ import java.util.Arrays;
public class Main extends EasyPlugin { public class Main extends EasyPlugin {
private static Main instance; private static Main instance;
private static DataStorage storage;
private WarpManager warpManager; protected DataStorage storage;
private UserManager userManager; protected WarpManager warpManager;
private RequestManager requestManager; protected UserManager userManager;
protected RequestManager requestManager;
public Main() { public Main() {
super(new EasyPluginMessageProvider.zh_CN()); super(new EasyPluginMessageProvider.zh_CN());
@ -81,16 +82,18 @@ public class Main extends EasyPlugin {
info("初始化存储方式..."); info("初始化存储方式...");
StorageMethod storageMethod = StorageMethod.read(PluginConfig.STORAGE_METHOD.get()); StorageMethod storageMethod = StorageMethod.read(PluginConfig.STORAGE_METHOD.get());
info(" 正在使用 " + storageMethod.name() + " 进行数据存储");
storage = storageMethod.createStorage(); try {
if (!storage.initialize()) { info(" 正在使用 " + storageMethod.name() + " 进行数据存储");
storage = storageMethod.createStorage();
storage.initialize();
} catch (Exception ex) {
severe("初始化存储失败,请检查配置文件。"); severe("初始化存储失败,请检查配置文件。");
storage.shutdown();
setEnabled(false); setEnabled(false);
return false; // 初始化失败不再继续加载 return false; // 初始化失败不再继续加载
} }
info("加载地标管理器..."); info("加载地标管理器...");
warpManager = new WarpManager(); warpManager = new WarpManager();
@ -98,7 +101,7 @@ public class Main extends EasyPlugin {
this.userManager = new UserManager(); this.userManager = new UserManager();
if (Bukkit.getOnlinePlayers().size() > 0) { if (Bukkit.getOnlinePlayers().size() > 0) {
info(" 加载现有用户数据..."); info(" 加载现有用户数据...");
getUserManager().loadAll(); this.userManager.loadAll();
} }
info("加载请求管理器..."); info("加载请求管理器...");
@ -147,16 +150,16 @@ public class Main extends EasyPlugin {
@Override @Override
protected void shutdown() { protected void shutdown() {
info("关闭所有请求..."); info("关闭所有请求...");
getRequestManager().shutdown(); this.requestManager.shutdown();
info("保存用户数据..."); info("保存用户数据...");
getUserManager().unloadAll(true); this.userManager.unloadAll(true);
info("保存地标数据..."); info("保存地标数据...");
getWarpManager().saveWarps(); this.warpManager.saveWarps();
info("终止存储源..."); info("终止存储源...");
getStorage().shutdown(); this.storage.shutdown();
info("卸载监听器..."); info("卸载监听器...");
Bukkit.getServicesManager().unregisterAll(this); Bukkit.getServicesManager().unregisterAll(this);
@ -174,21 +177,5 @@ public class Main extends EasyPlugin {
} }
} }
protected DataStorage getStorage() {
return storage;
}
protected WarpManager getWarpManager() {
return getInstance().warpManager;
}
protected UserManager getUserManager() {
return getInstance().userManager;
}
protected RequestManager getRequestManager() {
return getInstance().requestManager;
}
} }

View File

@ -6,8 +6,6 @@ import cc.carm.plugin.moeteleport.manager.WarpManager;
import cc.carm.plugin.moeteleport.storage.DataStorage; import cc.carm.plugin.moeteleport.storage.DataStorage;
import cc.carm.plugin.moeteleport.storage.StorageMethod; import cc.carm.plugin.moeteleport.storage.StorageMethod;
import java.util.function.Supplier;
public class MoeTeleport { public class MoeTeleport {
public static void outputInfo() { public static void outputInfo() {
@ -15,27 +13,27 @@ public class MoeTeleport {
} }
public static DataStorage getStorage() { public static DataStorage getStorage() {
return Main.getInstance().getStorage(); return Main.getInstance().storage;
} }
public static WarpManager getWarpManager() { public static WarpManager getWarpManager() {
return Main.getInstance().getWarpManager(); return Main.getInstance().warpManager;
} }
public static UserManager getUserManager() { public static UserManager getUserManager() {
return Main.getInstance().getUserManager(); return Main.getInstance().userManager;
} }
public static RequestManager getRequestManager() { public static RequestManager getRequestManager() {
return Main.getInstance().getRequestManager(); return Main.getInstance().requestManager;
} }
public void registerCustomStorage(DataStorage storage) { public void setStorage(DataStorage storage) {
registerCustomStorage(() -> storage); Main.getInstance().storage = storage;
} }
public void registerCustomStorage(Supplier<DataStorage> storageSupplier) { public void registerCustomStorage(Class<? extends DataStorage> storageClazz) {
StorageMethod.CUSTOM.setStorageSupplier(storageSupplier); StorageMethod.CUSTOM.setStorageClazz(storageClazz);
} }
} }

View File

@ -15,9 +15,9 @@ public interface DataStorage {
/** /**
* 在插件加载存储源时执行 * 在插件加载存储源时执行
* *
* @return 是否初始化成功 * @throws Exception 当出现任何错误时抛出
*/ */
boolean initialize(); void initialize() throws Exception;
/** /**
* 在插件被卸载时执行 * 在插件被卸载时执行

View File

@ -10,25 +10,24 @@ import org.jetbrains.annotations.Nullable;
import java.util.Arrays; import java.util.Arrays;
import java.util.Optional; import java.util.Optional;
import java.util.function.Supplier;
public enum StorageMethod { public enum StorageMethod {
CUSTOM(0, new String[]{}, CustomStorage::new), CUSTOM(0, new String[]{}, CustomStorage.class),
YAML(1, new String[]{"yml"}, YAMLStorage::new), YAML(1, new String[]{"yml"}, YAMLStorage.class),
JSON(2, new String[]{}, JSONStorage::new), JSON(2, new String[]{}, JSONStorage.class),
MYSQL(3, new String[]{"my-sql", "mariadb", "sql", "database"}, MySQLStorage::new), MYSQL(3, new String[]{"my-sql", "mariadb", "sql", "database"}, MySQLStorage.class),
ESSENTIALS(11, new String[]{"essential", "ess", "EssentialsX", "essX"}, EssentialStorage::new); ESSENTIALS(11, new String[]{"essential", "ess", "EssentialsX", "essX"}, EssentialStorage.class);
private final int id; private final int id;
private final String[] alias; private final String[] alias;
private @NotNull Supplier<@NotNull DataStorage> storageSupplier; private @NotNull Class<? extends DataStorage> storageClazz;
StorageMethod(int id, String[] alias, @NotNull Supplier<@NotNull DataStorage> storageSupplier) { StorageMethod(int id, String[] alias, @NotNull Class<? extends DataStorage> storageClazz) {
this.id = id; this.id = id;
this.alias = alias; this.alias = alias;
this.storageSupplier = storageSupplier; this.storageClazz = storageClazz;
} }
public static @NotNull StorageMethod read(String s) { public static @NotNull StorageMethod read(String s) {
@ -65,15 +64,15 @@ public enum StorageMethod {
return alias; return alias;
} }
public @NotNull Supplier<@NotNull DataStorage> getStorageSupplier() { public @NotNull Class<? extends DataStorage> getStorageClazz() {
return storageSupplier; return storageClazz;
} }
public void setStorageSupplier(@NotNull Supplier<@NotNull DataStorage> storageSupplier) { public void setStorageClazz(@NotNull Class<? extends DataStorage> storageClazz) {
this.storageSupplier = storageSupplier; this.storageClazz = storageClazz;
} }
public @NotNull DataStorage createStorage() { public @NotNull DataStorage createStorage() throws Exception {
return getStorageSupplier().get(); return getStorageClazz().newInstance();
} }
} }

View File

@ -17,10 +17,10 @@ public class CustomStorage implements DataStorage {
@Override @Override
@TestOnly @TestOnly
public boolean initialize() { public void initialize() throws UnsupportedOperationException {
Main.severe("您选择使用自定义存储,但并没有应用成功。"); Main.severe("您选择使用自定义存储,但并没有应用成功。");
Main.severe("You are using CustomStorage, but not overwrite the methods."); Main.severe("You are using CustomStorage, but not overwrite the methods.");
return false; throw new UnsupportedOperationException("您选择使用自定义存储,但并没有应用成功。");
} }
@Override @Override
@ -31,14 +31,14 @@ public class CustomStorage implements DataStorage {
@Override @Override
@TestOnly @TestOnly
public @Nullable UserData loadData(@NotNull UUID uuid) { public @Nullable UserData loadData(@NotNull UUID uuid) throws UnsupportedOperationException {
return null; throw new UnsupportedOperationException("您选择使用自定义存储,但并没有应用成功。");
} }
@Override @Override
@TestOnly @TestOnly
public void saveUserData(@NotNull UserData data) { public void saveUserData(@NotNull UserData data) throws UnsupportedOperationException {
throw new UnsupportedOperationException("您选择使用自定义存储,但并没有应用成功。");
} }
@Override @Override

View File

@ -26,7 +26,7 @@ public class MySQLStorage implements DataStorage {
Map<String, WarpInfo> warpsMap = new HashMap<>(); Map<String, WarpInfo> warpsMap = new HashMap<>();
@Override @Override
public boolean initialize() { public void initialize() throws Exception {
try { try {
Main.info(" 尝试连接到数据库..."); Main.info(" 尝试连接到数据库...");
String url = String.format("jdbc:mysql://%s:%s/%s?useSSL=false", String url = String.format("jdbc:mysql://%s:%s/%s?useSSL=false",
@ -38,9 +38,7 @@ public class MySQLStorage implements DataStorage {
); );
this.sqlManager.setDebugMode(() -> Main.getInstance().isDebugging()); this.sqlManager.setDebugMode(() -> Main.getInstance().isDebugging());
} catch (Exception exception) { } catch (Exception exception) {
Main.severe("无法连接到数据库,请检查配置文件。"); throw new Exception("无法连接到数据库,请检查配置文件", exception);
exception.printStackTrace();
return false;
} }
try { try {
@ -58,20 +56,15 @@ public class MySQLStorage implements DataStorage {
.build().execute(); .build().execute();
} catch (SQLException exception) { } catch (SQLException exception) {
Main.severe("无法创建插件所需的表,请检查数据库权限。"); throw new Exception("无法创建插件所需的表,请检查数据库权限。", exception);
exception.printStackTrace();
return false;
} }
Main.info(" 加载地标数据..."); Main.info(" 加载地标数据...");
try { try {
this.warpsMap = loadWarps(); this.warpsMap = loadWarps();
} catch (Exception e) { } catch (Exception e) {
Main.severe("无法加载地标数据,请检查数据库权限和相关表。"); throw new Exception("无法加载地标数据,请检查数据库权限和相关表。", e);
e.printStackTrace();
} }
return true;
} }
@Override @Override

View File

@ -16,17 +16,10 @@ import java.util.UUID;
public class EssentialStorage extends PluginBasedStorage { public class EssentialStorage extends PluginBasedStorage {
private Essentials essentials;
public EssentialStorage() { public EssentialStorage() {
super("Essentials"); super("Essentials");
} }
@Override
public boolean initialize() {
return super.initialize() && (this.essentials = (Essentials) getDependPlugin()) != null;
}
@Override @Override
public @Nullable UserData loadData(@NotNull UUID uuid) { public @Nullable UserData loadData(@NotNull UUID uuid) {
return new EssentialUserData(uuid, getEssentials()); return new EssentialUserData(uuid, getEssentials());
@ -40,7 +33,7 @@ public class EssentialStorage extends PluginBasedStorage {
try { try {
Location warpLocation = getEssentials().getWarps().getWarp(warpName); Location warpLocation = getEssentials().getWarps().getWarp(warpName);
UUID owner = getEssentials().getWarps().getLastOwner(warpName); UUID owner = getEssentials().getWarps().getLastOwner(warpName);
warps.put(warpName, new WarpInfo(warpName,owner, new DataLocation(warpLocation))); warps.put(warpName, new WarpInfo(warpName, owner, new DataLocation(warpLocation)));
} catch (Exception ignore) { } catch (Exception ignore) {
} }
} }
@ -74,7 +67,7 @@ public class EssentialStorage extends PluginBasedStorage {
} }
public Essentials getEssentials() { public Essentials getEssentials() {
return essentials; return (Essentials) getDependPlugin();
} }
public static class EssentialUserData extends UserData { public static class EssentialUserData extends UserData {

View File

@ -30,17 +30,9 @@ public class JSONStorage extends FileBasedStorage {
Map<String, WarpInfo> warpsMap = new HashMap<>(); Map<String, WarpInfo> warpsMap = new HashMap<>();
@Override @Override
public boolean initialize() { public void initialize() throws Exception {
if (super.initialize()) { super.initialize();
try { this.warpsMap = loadWarps();
this.warpsMap = loadWarps();
return true;
} catch (Exception e) {
Main.severe("无法加载地标数据,请检查文件权限和相关配置。");
e.printStackTrace();
}
}
return false;
} }
@Override @Override

View File

@ -26,22 +26,14 @@ public class YAMLStorage extends FileBasedStorage {
FileConfiguration warpsConfiguration; FileConfiguration warpsConfiguration;
@Override @Override
public boolean initialize() { public void initialize() throws Exception {
if (super.initialize()) { super.initialize();
try { this.warpsDataFile = new File(getDataFolder(), "warps.yml");
this.warpsDataFile = new File(getDataFolder(), "warps.yml"); if (!this.warpsDataFile.exists() && !warpsDataFile.createNewFile()) {
if (!this.warpsDataFile.exists()) { throw new Exception("无法创建 warps.yml 文件。");
boolean success = warpsDataFile.createNewFile();
}
this.warpsConfiguration = YamlConfiguration.loadConfiguration(warpsDataFile);
this.warpsMap = loadWarps();
return true;
} catch (Exception e) {
Main.severe("无法加载地标数据,请检查文件权限和相关配置。");
e.printStackTrace();
}
} }
return false; this.warpsConfiguration = YamlConfiguration.loadConfiguration(warpsDataFile);
this.warpsMap = loadWarps();
} }
@Override @Override

View File

@ -20,16 +20,14 @@ public abstract class FileBasedStorage implements DataStorage {
protected @Nullable File dataFolder; protected @Nullable File dataFolder;
@Override @Override
public boolean initialize() { public void initialize() throws Exception {
try { this.dataFolder = new File(Main.getInstance().getDataFolder(), FILE_PATH.get());
this.dataFolder = new File(Main.getInstance().getDataFolder(), FILE_PATH.get()); if (!dataFolder.exists()) {
if (!dataFolder.exists()) { if (!dataFolder.mkdir()) {
return dataFolder.mkdir(); throw new Exception("无法创建数据文件夹!");
} else {
return dataFolder.isDirectory();
} }
} catch (Exception ex) { } else if (!dataFolder.isDirectory()) {
return false; throw new Exception("数据文件夹路径对应的不是一个文件夹!");
} }
} }

View File

@ -20,8 +20,10 @@ public abstract class PluginBasedStorage implements DataStorage {
} }
@Override @Override
public boolean initialize() { public void initialize() throws NullPointerException {
return dependPlugin != null; if (dependPlugin == null) {
throw new NullPointerException("该存储类型依赖的插件不存在,请检查配置文件");
}
} }
public Plugin getDependPlugin() { public Plugin getDependPlugin() {