From a8ffe80e502472fccc777567ff86708f8e873fb7 Mon Sep 17 00:00:00 2001 From: Indyuce Date: Tue, 21 Dec 2021 20:13:24 +0100 Subject: [PATCH] class selection now redirects you to the subclass you took last time. this does not work well with subclass nets yet but does delete data randomly at least --- .../api/player/profess/PlayerClass.java | 14 ++++++++++ .../net/Indyuce/mmocore/gui/ClassSelect.java | 27 ++++++++++++++++++- 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/src/main/java/net/Indyuce/mmocore/api/player/profess/PlayerClass.java b/src/main/java/net/Indyuce/mmocore/api/player/profess/PlayerClass.java index b088dba3..83380933 100644 --- a/src/main/java/net/Indyuce/mmocore/api/player/profess/PlayerClass.java +++ b/src/main/java/net/Indyuce/mmocore/api/player/profess/PlayerClass.java @@ -267,6 +267,20 @@ public class PlayerClass extends PostLoadObject { return subclasses; } + /** + * Recursive method which checks if the given class + * is a child of the current class in the 'subclass tree'. + * + * @param profess Some player class + * @return If given class is a subclass of the current class + */ + public boolean hasSubclass(PlayerClass profess) { + for (Subclass sub : subclasses) + if (sub.getProfess().equals(profess) || sub.getProfess().hasSubclass(profess)) + return true; + return false; + } + public boolean hasSkill(Skill skill) { return hasSkill(skill.getId()); } diff --git a/src/main/java/net/Indyuce/mmocore/gui/ClassSelect.java b/src/main/java/net/Indyuce/mmocore/gui/ClassSelect.java index 3ca7e40d..a8dedce5 100644 --- a/src/main/java/net/Indyuce/mmocore/gui/ClassSelect.java +++ b/src/main/java/net/Indyuce/mmocore/gui/ClassSelect.java @@ -121,8 +121,33 @@ public class ClassSelect extends EditableInventory { return; } - InventoryManager.CLASS_CONFIRM.newInventory(playerData, profess, this).open(); + InventoryManager.CLASS_CONFIRM.newInventory(playerData, findDeepestSubclass(playerData, profess), this).open(); } } } + + /** + * When switching from a class where you had progress before, + * you should be instantly redirected to the highest subclass + * in the subclass tree that you chose, because your progress + * is saved there. + *

+ * It's also more RPG style to take the player back to the subclass + * he chose because that way he can't turn back and chose another path. + * + * This does NOT function properly with subclass nets yet. + * + * @param root The root class, it's called the root because since the + * player was able to choose it in the class GUI, it should + * be at the bottom of the class tree. + */ + private PlayerClass findDeepestSubclass(PlayerData player, PlayerClass root) { + for (String checkedName : player.getSavedClasses()) { + PlayerClass checked = MMOCore.plugin.classManager.getOrThrow(checkedName); + if (root.hasSubclass(checked)) + return checked; + } + + return root; + } }