diff --git a/src/main/java/net/raphimc/viaproxy/plugins/events/UITabInitializeEvent.java b/src/main/java/net/raphimc/viaproxy/plugins/events/UITabInitializeEvent.java new file mode 100644 index 00000000..43f95e90 --- /dev/null +++ b/src/main/java/net/raphimc/viaproxy/plugins/events/UITabInitializeEvent.java @@ -0,0 +1,45 @@ +/* + * This file is part of ViaProxy - https://github.com/RaphiMC/ViaProxy + * Copyright (C) 2021-2026 RK_01/RaphiMC and contributors + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package net.raphimc.viaproxy.plugins.events; + +import net.raphimc.viaproxy.plugins.events.types.EventCancellable; +import net.raphimc.viaproxy.ui.UITab; +import net.raphimc.viaproxy.ui.ViaProxyWindow; + +public class UITabInitializeEvent extends EventCancellable { + + private final ViaProxyWindow window; + private UITab tab; + + public UITabInitializeEvent(ViaProxyWindow window, UITab tab) { + this.window = window; + this.tab = tab; + } + + public ViaProxyWindow getWindow() { + return window; + } + + public UITab getTab() { + return tab; + } + + public void setTab(UITab tab) { + this.tab = tab; + } +} diff --git a/src/main/java/net/raphimc/viaproxy/saves/impl/AccountsSave.java b/src/main/java/net/raphimc/viaproxy/saves/impl/AccountsSave.java index 259e1746..708134a0 100644 --- a/src/main/java/net/raphimc/viaproxy/saves/impl/AccountsSave.java +++ b/src/main/java/net/raphimc/viaproxy/saves/impl/AccountsSave.java @@ -35,6 +35,7 @@ public class AccountsSave extends AbstractSave { private final List accounts = new ArrayList<>(); + private final List unknownAccounts = new ArrayList<>(); public AccountsSave() { super("accountsV4"); @@ -48,17 +49,30 @@ public void load(JsonElement jsonElement) throws Exception { this.accounts.clear(); for (JsonElement element : jsonElement.getAsJsonArray()) { + JsonObject jsonObject = null; try { - final JsonObject jsonObject = element.getAsJsonObject(); - final String type = jsonObject.get("accountType").getAsString(); + jsonObject = element.getAsJsonObject(); + if (!jsonObject.has("accountType")) { + Logger.LOGGER.error("Failed to load an account, missing accountType"); + continue; + } + + String type = jsonObject.get("accountType").getAsString(); final Class clazz = Classes.find(type, true, classLoaders); final Account account = (Account) clazz.getConstructor(JsonObject.class).newInstance(jsonObject); this.accounts.add(account); - } catch (Throwable e) { - Logger.LOGGER.error("Failed to load an account", e); + } catch (Throwable t) { + if (t instanceof ClassNotFoundException classNotFoundException) { + Logger.LOGGER.error("Failed to load an account, unknown accountType: " + classNotFoundException.getMessage()); + this.unknownAccounts.add(jsonObject); + continue; + } + + Logger.LOGGER.error("Failed to load an account", t); } } + Logger.LOGGER.info("Loaded " + this.accounts.size() + " accounts, " + this.unknownAccounts.size() + " unknown accounts"); } @Override @@ -69,6 +83,7 @@ public JsonElement save() { jsonObject.addProperty("accountType", account.getClass().getName()); array.add(jsonObject); } + this.unknownAccounts.forEach(array::add); return array; } diff --git a/src/main/java/net/raphimc/viaproxy/ui/ViaProxyWindow.java b/src/main/java/net/raphimc/viaproxy/ui/ViaProxyWindow.java index c4bd1807..9c81cad7 100644 --- a/src/main/java/net/raphimc/viaproxy/ui/ViaProxyWindow.java +++ b/src/main/java/net/raphimc/viaproxy/ui/ViaProxyWindow.java @@ -25,6 +25,7 @@ import net.lenni0451.reflect.JavaBypass; import net.lenni0451.reflect.stream.RStream; import net.raphimc.viaproxy.ViaProxy; +import net.raphimc.viaproxy.plugins.events.UITabInitializeEvent; import net.raphimc.viaproxy.ui.events.UICloseEvent; import net.raphimc.viaproxy.ui.impl.*; import net.raphimc.viaproxy.util.logging.Logger; @@ -111,8 +112,17 @@ private void initTabs() { .fields() .filter(field -> UITab.class.isAssignableFrom(field.type())) .forEach(field -> { - final UITab tab = field.get(); - this.tabs.add(field.get()); + UITab tab = field.get(); + + UITabInitializeEvent event = ViaProxy.EVENT_MANAGER.call(new UITabInitializeEvent(ViaProxyWindow.this, tab)); + if (event.isCancelled()) + return; + + tab = event.getTab(); + if (tab == null) + throw new IllegalStateException("UITabInitializeEvent returned null tab"); + + this.tabs.add(tab); tab.add(this.contentPane); this.eventManager.register(tab); });