From d248dddc53bf676ff9fd7c1314698d27a8562a8d Mon Sep 17 00:00:00 2001 From: Phong Date: Thu, 25 Jul 2024 08:06:00 +0700 Subject: [PATCH] Chore: Remove CombineBonusData class and update GameData (CombineBonusData not found in 4.0 resource), instead, load from CombineBonus.json in the data folder --- .../java/emu/grasscutter/GameConstants.java | 21 ++++- .../java/emu/grasscutter/data/GameData.java | 4 - .../data/excels/CombineBonusData.java | 23 ----- .../game/combine/CombineBonusData.java | 19 ++++ .../game/combine/CombineManger.java | 92 +++++++++---------- 5 files changed, 80 insertions(+), 79 deletions(-) delete mode 100644 src/main/java/emu/grasscutter/data/excels/CombineBonusData.java create mode 100644 src/main/java/emu/grasscutter/game/combine/CombineBonusData.java diff --git a/src/main/java/emu/grasscutter/GameConstants.java b/src/main/java/emu/grasscutter/GameConstants.java index 8640dfaa3..cb94377c2 100644 --- a/src/main/java/emu/grasscutter/GameConstants.java +++ b/src/main/java/emu/grasscutter/GameConstants.java @@ -3,7 +3,7 @@ package emu.grasscutter; import emu.grasscutter.game.world.Position; import emu.grasscutter.utils.objects.SparseSet; import emu.grasscutter.utils.Utils; -import java.util.Arrays; +import java.util.*; public final class GameConstants { public static String VERSION = "3.7.0"; @@ -47,4 +47,23 @@ public final class GameConstants { public static final int[] DEFAULT_ABILITY_HASHES = Arrays.stream(DEFAULT_ABILITY_STRINGS).mapToInt(Utils::abilityHash).toArray(); public static final int DEFAULT_ABILITY_NAME = Utils.abilityHash("Default"); + public static final HashMap YAE_MIKO_ITEM_TO_REGION_COMBINE_BONUS = new HashMap<>() {{ + put(104304, 1); + put(104307, 1); + put(104310, 2); + put(104313, 2); + put(104316, 2); + put(104320, 3); + put(104323, 3); + put(104326, 3); + put(104329, 4); + put(104332, 4); + put(104335, 4); + }}; + public static final HashMap> YAE_MIKO_REGION_TO_ITEM_COMBINE_BONUS = new HashMap<>() {{ + put(1, List.of(104304, 104307)); + put(2, List.of(104310, 104313, 104316)); + put(3, List.of(104320, 104323, 104326)); + put(4, List.of(104329, 104332, 104335)); + }}; } diff --git a/src/main/java/emu/grasscutter/data/GameData.java b/src/main/java/emu/grasscutter/data/GameData.java index 8c4cd3aaf..3b99f1f26 100644 --- a/src/main/java/emu/grasscutter/data/GameData.java +++ b/src/main/java/emu/grasscutter/data/GameData.java @@ -207,10 +207,6 @@ public final class GameData { @Getter private static final Int2ObjectMap combineDataMap = new Int2ObjectOpenHashMap<>(); - @Getter - private static final Int2ObjectMap combineBonusDataMap = - new Int2ObjectOpenHashMap<>(); - @Getter private static final Int2ObjectMap cookBonusDataMap = new Int2ObjectOpenHashMap<>(); diff --git a/src/main/java/emu/grasscutter/data/excels/CombineBonusData.java b/src/main/java/emu/grasscutter/data/excels/CombineBonusData.java deleted file mode 100644 index cddab9a7f..000000000 --- a/src/main/java/emu/grasscutter/data/excels/CombineBonusData.java +++ /dev/null @@ -1,23 +0,0 @@ -package emu.grasscutter.data.excels; - -import emu.grasscutter.data.GameResource; -import emu.grasscutter.data.ResourceType; -import emu.grasscutter.data.ResourceType.LoadPriority; -import java.util.List; -import lombok.Getter; -import lombok.Setter; - -@ResourceType( - name = "CombineBonusExcelConfigData.json", - loadPriority = LoadPriority.LOW) -public class CombineBonusData extends GameResource { - @Getter @Setter private int avatarId; - @Getter @Setter private int combineType; - @Getter @Setter private String bonusType; - @Getter @Setter private List paramVec; - - @Override - public int getId() { - return this.avatarId; - } -} diff --git a/src/main/java/emu/grasscutter/game/combine/CombineBonusData.java b/src/main/java/emu/grasscutter/game/combine/CombineBonusData.java new file mode 100644 index 000000000..65f32078c --- /dev/null +++ b/src/main/java/emu/grasscutter/game/combine/CombineBonusData.java @@ -0,0 +1,19 @@ +package emu.grasscutter.game.combine; + +import java.util.List; +import lombok.Getter; +import lombok.Setter; + +@Getter @Setter +public class CombineBonusData { + private int avatarId; + private int combineType; + private BonusType bonusType; + private List paramVec; + + public enum BonusType { + COMBINE_BONUS_DOUBLE, + COMBINE_BONUS_REFUND, + COMBINE_BONUS_REFUND_RANDOM, + } +} diff --git a/src/main/java/emu/grasscutter/game/combine/CombineManger.java b/src/main/java/emu/grasscutter/game/combine/CombineManger.java index 1791410ec..7bc4c2430 100644 --- a/src/main/java/emu/grasscutter/game/combine/CombineManger.java +++ b/src/main/java/emu/grasscutter/game/combine/CombineManger.java @@ -1,10 +1,10 @@ package emu.grasscutter.game.combine; +import emu.grasscutter.GameConstants; import emu.grasscutter.Grasscutter; import emu.grasscutter.data.DataLoader; import emu.grasscutter.data.GameData; import emu.grasscutter.data.common.ItemParamData; -import emu.grasscutter.data.excels.CombineBonusData; import emu.grasscutter.data.excels.CombineData; import emu.grasscutter.game.avatar.Avatar; import emu.grasscutter.game.inventory.GameItem; @@ -21,17 +21,24 @@ import emu.grasscutter.utils.Utils; import io.netty.util.internal.ThreadLocalRandom; import it.unimi.dsi.fastutil.ints.Int2ObjectMap; import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.Map; -import java.util.List; +import java.util.*; public class CombineManger extends BaseGameSystem { private static final Int2ObjectMap> reliquaryDecomposeData = new Int2ObjectOpenHashMap<>(); + private final Int2ObjectMap combineBonusData = new Int2ObjectOpenHashMap<>(); public CombineManger(GameServer server) { super(server); + + // load combine bonus data + try { + DataLoader.loadList("CombineBonus.json", CombineBonusData.class) + .forEach(entry -> combineBonusData.put(entry.getAvatarId(), entry)); + } catch (Exception ignored) { + Grasscutter.getLogger() + .error("Unable to load combine bonus data. Please place CombineBonus.json in the data folder."); + } } public static void initialize() { @@ -96,10 +103,10 @@ public class CombineManger extends BaseGameSystem { // lucky characters int luckyCount = 0; Avatar avatar = player.getAvatars().getAvatarByGuid(avatarGuid); - CombineBonusData combineBonusData = GameData.getCombineBonusDataMap().get(avatar.getAvatarId()); - if (combineBonusData != null - && combineData.getCombineType() == combineBonusData.getCombineType()) { - double luckyChange = combineBonusData.getParamVec().get(0); + CombineBonusData combineBonusAvatar = combineBonusData.get(avatar.getAvatarId()); + if (combineBonusAvatar != null + && combineData.getCombineType() == combineBonusAvatar.getCombineType()) { + double luckyChange = combineBonusAvatar.getParamVec().get(0); for (int i = 0; i < count; i++) { if (ThreadLocalRandom.current().nextDouble() <= luckyChange) { luckyCount++; @@ -113,28 +120,27 @@ public class CombineManger extends BaseGameSystem { // add lucky items if (luckyCount > 0) { - switch (combineBonusData.getBonusType()) { - case "COMBINE_BONUS_DOUBLE" -> { + switch (combineBonusAvatar.getBonusType()) { + case COMBINE_BONUS_DOUBLE -> { var combineExtra = new ItemParamData(combineData.getResultItemId(), luckyCount); player.getInventory().addItem(combineExtra); result.getExtra().add(combineExtra); } - case "COMBINE_BONUS_REFUND" -> { + case COMBINE_BONUS_REFUND -> { if (combineData.getMaterialItems().size() == 1) { - var combineBack = - new ItemParamData(combineData.getMaterialItems().get(0).getItemId(), luckyCount); + var combineBack = new ItemParamData(combineData.getMaterialItems().get(0).getItemId(), + luckyCount); player.getInventory().addItem(combineBack); result.getBack().add(combineBack); } else { HashMap mapIdCount = new HashMap<>(); for (int i = 0; i < luckyCount; i++) { - var randomId = - combineData - .getMaterialItems() - .get( - ThreadLocalRandom.current() - .nextInt(combineData.getMaterialItems().size())) - .getItemId(); + var randomId = combineData + .getMaterialItems() + .get( + ThreadLocalRandom.current() + .nextInt(combineData.getMaterialItems().size())) + .getItemId(); mapIdCount.put(randomId, mapIdCount.getOrDefault(randomId, 0) + 1); } @@ -145,36 +151,22 @@ public class CombineManger extends BaseGameSystem { } } } - case "COMBINE_BONUS_REFUND_RANDOM" -> { - // for yae miko, "Has a 25% chance to get 1 regional Character Talent Material (base - // material excluded) when crafting. The rarity is that of the base material." from wiki + case COMBINE_BONUS_REFUND_RANDOM -> { + // for yae miko, "Has a 25% chance to get 1 regional Character Talent Material + // (base + // material excluded) when crafting. The rarity is that of the base material." + // from wiki // map of material id to region id - Map itemToRegion = Map.of( - 104301, 1, - 104304, 1, - 104307, 1, - 104310, 2, - 104313, 2, - 104316, 2, - 104320, 3, - 104323, 3, - 104326, 3, - 104329, 4, - 104332, 4, - 104335, 4 - ); + HashMap itemToRegion = GameConstants.YAE_MIKO_ITEM_TO_REGION_COMBINE_BONUS; // get list of material id with every region - HashMap> regionToId = new HashMap<>(); - for (var entry : itemToRegion.entrySet()) { - regionToId.putIfAbsent(entry.getValue(), new ArrayList<>()); - regionToId.get(entry.getValue()).add(entry.getKey()); - } + HashMap> regionToId = GameConstants.YAE_MIKO_REGION_TO_ITEM_COMBINE_BONUS; // check material id in itemToRegion - var itemId = combineData.getMaterialItems().get(0).getItemId(); + int itemId = combineData.getMaterialItems().get(0).getItemId(); int rank = 0; // rank of material - if (itemToRegion.get(itemId) != null) rank = 1; + if (itemToRegion.get(itemId) != null) + rank = 1; if (itemToRegion.get(itemId - 1) != null) { rank = 2; itemId -= 1; @@ -183,23 +175,21 @@ public class CombineManger extends BaseGameSystem { if (rank >= 1) { // if material is regional // get list of material id with same region List listIdRandom = regionToId.get(itemToRegion.get(itemId)); - // remove material id from list + // remove material id from array listIdRandom.remove(Integer.valueOf(itemId)); HashMap mapIdCount = new HashMap<>(); // pick random material from list with luckyCount for (int i = 0; i < luckyCount; i++) { - var randomId = - listIdRandom.get(ThreadLocalRandom.current().nextInt(listIdRandom.size())); + int randomId = listIdRandom.get(ThreadLocalRandom.current().nextInt(listIdRandom.size())); mapIdCount.put(randomId, mapIdCount.getOrDefault(randomId, 0) + 1); } // add to random list for (var entry : mapIdCount.entrySet()) { // if rank 2, add 1 to material id - var combineRandom = - new ItemParamData( - (rank == 2) ? entry.getKey() + 1 : entry.getKey(), entry.getValue()); + var combineRandom = new ItemParamData( + (rank == 2) ? entry.getKey() + 1 : entry.getKey(), entry.getValue()); player.getInventory().addItem(combineRandom); result.getRandom().add(combineRandom); }