Skip to content

Commit

Permalink
[3.1.1] 版本修复
Browse files Browse the repository at this point in the history
- `[R]` 重写存储源初始化方法。
- `[F]` 修复服务器在未安装 Essentials 时出现无法加载插件的问题。
  • Loading branch information
CarmJos committed Apr 3, 2022
1 parent 61d97c2 commit 7cf0b9e
Show file tree
Hide file tree
Showing 12 changed files with 75 additions and 121 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down Expand Up @@ -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>

Expand Down
45 changes: 16 additions & 29 deletions src/main/java/cc/carm/plugin/moeteleport/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,11 @@

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());
Expand Down Expand Up @@ -81,24 +82,26 @@ protected boolean initialize() {

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

info("加载用户管理器...");
this.userManager = new UserManager();
if (Bukkit.getOnlinePlayers().size() > 0) {
info(" 加载现有用户数据...");
getUserManager().loadAll();
this.userManager.loadAll();
}

info("加载请求管理器...");
Expand Down Expand Up @@ -147,16 +150,16 @@ protected boolean initialize() {
@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);
Expand All @@ -174,21 +177,5 @@ public void outputInfo() {
}
}

protected DataStorage getStorage() {
return storage;
}

protected WarpManager getWarpManager() {
return getInstance().warpManager;
}

protected UserManager getUserManager() {
return getInstance().userManager;
}

protected RequestManager getRequestManager() {
return getInstance().requestManager;
}


}
18 changes: 8 additions & 10 deletions src/main/java/cc/carm/plugin/moeteleport/MoeTeleport.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,36 +6,34 @@
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() {
Main.getInstance().outputInfo();
}

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

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ public interface DataStorage {
/**
* 在插件加载存储源时执行。
*
* @return 是否初始化成功
* @throws Exception 当出现任何错误时抛出
*/
boolean initialize();
void initialize() throws Exception;

/**
* 在插件被卸载时执行。
Expand Down
29 changes: 14 additions & 15 deletions src/main/java/cc/carm/plugin/moeteleport/storage/StorageMethod.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,24 @@

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) {
Expand Down Expand Up @@ -65,15 +64,15 @@ public String[] getAlias() {
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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -31,14 +31,14 @@ public void shutdown() {

@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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -38,9 +38,7 @@ public boolean initialize() {
);
this.sqlManager.setDebugMode(() -> Main.getInstance().isDebugging());
} catch (Exception exception) {
Main.severe("无法连接到数据库,请检查配置文件。");
exception.printStackTrace();
return false;
throw new Exception("无法连接到数据库,请检查配置文件", exception);
}

try {
Expand All @@ -58,20 +56,15 @@ public boolean initialize() {
.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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,10 @@

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());
Expand All @@ -40,7 +33,7 @@ public Map<String, WarpInfo> getWarps() {
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) {
}
}
Expand Down Expand Up @@ -74,7 +67,7 @@ public boolean hasWarp(@NotNull String name) {
}

public Essentials getEssentials() {
return essentials;
return (Essentials) getDependPlugin();
}

public static class EssentialUserData extends UserData {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading

0 comments on commit 7cf0b9e

Please sign in to comment.