1
mirror of https://github.com/CarmJos/MoeTeleport.git synced 2024-09-19 13:25: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>
<artifactId>moeteleport</artifactId>
<version>3.1.0</version>
<version>3.1.1</version>
<name>MoeTeleport</name>
<description>喵喵传送,简单的传送、设置家的插件。</description>
@ -179,7 +179,7 @@
<dependency>
<groupId>org.jetbrains</groupId>
<artifactId>annotations</artifactId>
<version>22.0.0</version>
<version>23.0.0</version>
<scope>provided</scope>
</dependency>

View File

@ -34,10 +34,11 @@ import java.util.Arrays;
public class Main extends EasyPlugin {
private static Main instance;
private static DataStorage storage;
private WarpManager warpManager;
private UserManager userManager;
private RequestManager requestManager;
protected DataStorage storage;
protected WarpManager warpManager;
protected UserManager userManager;
protected RequestManager requestManager;
public Main() {
super(new EasyPluginMessageProvider.zh_CN());
@ -81,16 +82,18 @@ public class Main extends EasyPlugin {
info("初始化存储方式...");
StorageMethod storageMethod = StorageMethod.read(PluginConfig.STORAGE_METHOD.get());
info(" 正在使用 " + storageMethod.name() + " 进行数据存储");
storage = storageMethod.createStorage();
if (!storage.initialize()) {
try {
info(" 正在使用 " + storageMethod.name() + " 进行数据存储");
storage = storageMethod.createStorage();
storage.initialize();
} catch (Exception ex) {
severe("初始化存储失败,请检查配置文件。");
storage.shutdown();
setEnabled(false);
return false; // 初始化失败不再继续加载
}
info("加载地标管理器...");
warpManager = new WarpManager();
@ -98,7 +101,7 @@ public class Main extends EasyPlugin {
this.userManager = new UserManager();
if (Bukkit.getOnlinePlayers().size() > 0) {
info(" 加载现有用户数据...");
getUserManager().loadAll();
this.userManager.loadAll();
}
info("加载请求管理器...");
@ -147,16 +150,16 @@ public class Main extends EasyPlugin {
@Override
protected void shutdown() {
info("关闭所有请求...");
getRequestManager().shutdown();
this.requestManager.shutdown();
info("保存用户数据...");
getUserManager().unloadAll(true);
this.userManager.unloadAll(true);
info("保存地标数据...");
getWarpManager().saveWarps();
this.warpManager.saveWarps();
info("终止存储源...");
getStorage().shutdown();
this.storage.shutdown();
info("卸载监听器...");
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.StorageMethod;
import java.util.function.Supplier;
public class MoeTeleport {
public static void outputInfo() {
@ -15,27 +13,27 @@ public class MoeTeleport {
}
public static DataStorage getStorage() {
return Main.getInstance().getStorage();
return Main.getInstance().storage;
}
public static WarpManager getWarpManager() {
return Main.getInstance().getWarpManager();
return Main.getInstance().warpManager;
}
public static UserManager getUserManager() {
return Main.getInstance().getUserManager();
return Main.getInstance().userManager;
}
public static RequestManager getRequestManager() {
return Main.getInstance().getRequestManager();
return Main.getInstance().requestManager;
}
public void registerCustomStorage(DataStorage storage) {
registerCustomStorage(() -> storage);
public void setStorage(DataStorage storage) {
Main.getInstance().storage = storage;
}
public void registerCustomStorage(Supplier<DataStorage> storageSupplier) {
StorageMethod.CUSTOM.setStorageSupplier(storageSupplier);
public void registerCustomStorage(Class<? extends DataStorage> storageClazz) {
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.Optional;
import java.util.function.Supplier;
public enum StorageMethod {
CUSTOM(0, new String[]{}, CustomStorage::new),
YAML(1, new String[]{"yml"}, YAMLStorage::new),
JSON(2, new String[]{}, JSONStorage::new),
MYSQL(3, new String[]{"my-sql", "mariadb", "sql", "database"}, MySQLStorage::new),
CUSTOM(0, new String[]{}, CustomStorage.class),
YAML(1, new String[]{"yml"}, YAMLStorage.class),
JSON(2, new String[]{}, JSONStorage.class),
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 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.alias = alias;
this.storageSupplier = storageSupplier;
this.storageClazz = storageClazz;
}
public static @NotNull StorageMethod read(String s) {
@ -65,15 +64,15 @@ public enum StorageMethod {
return alias;
}
public @NotNull Supplier<@NotNull DataStorage> getStorageSupplier() {
return storageSupplier;
public @NotNull Class<? extends DataStorage> getStorageClazz() {
return storageClazz;
}
public void setStorageSupplier(@NotNull Supplier<@NotNull DataStorage> storageSupplier) {
this.storageSupplier = storageSupplier;
public void setStorageClazz(@NotNull Class<? extends DataStorage> storageClazz) {
this.storageClazz = storageClazz;
}
public @NotNull DataStorage createStorage() {
return getStorageSupplier().get();
public @NotNull DataStorage createStorage() throws Exception {
return getStorageClazz().newInstance();
}
}

View File

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

View File

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

View File

@ -16,17 +16,10 @@ import java.util.UUID;
public class EssentialStorage extends PluginBasedStorage {
private Essentials essentials;
public EssentialStorage() {
super("Essentials");
}
@Override
public boolean initialize() {
return super.initialize() && (this.essentials = (Essentials) getDependPlugin()) != null;
}
@Override
public @Nullable UserData loadData(@NotNull UUID uuid) {
return new EssentialUserData(uuid, getEssentials());
@ -40,7 +33,7 @@ public class EssentialStorage extends PluginBasedStorage {
try {
Location warpLocation = getEssentials().getWarps().getWarp(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) {
}
}
@ -74,7 +67,7 @@ public class EssentialStorage extends PluginBasedStorage {
}
public Essentials getEssentials() {
return essentials;
return (Essentials) getDependPlugin();
}
public static class EssentialUserData extends UserData {

View File

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

View File

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

View File

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

View File

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