From 37c08183323b6937e3a882f7cc76e4f88fa307fc Mon Sep 17 00:00:00 2001 From: Phong940253 Date: Sun, 6 Aug 2023 22:52:09 +0700 Subject: [PATCH 1/7] implement feature character bonuses for combine & fix num result combine the wrong --- .../java/emu/grasscutter/data/GameData.java | 4 + .../data/excels/CombineBonusData.java | 23 ++++ .../game/combine/CombineManger.java | 129 +++++++++++++++++- .../game/combine/CombineResult.java | 43 +----- .../server/packet/recv/HandlerCombineReq.java | 8 +- 5 files changed, 163 insertions(+), 44 deletions(-) create mode 100644 src/main/java/emu/grasscutter/data/excels/CombineBonusData.java diff --git a/src/main/java/emu/grasscutter/data/GameData.java b/src/main/java/emu/grasscutter/data/GameData.java index 3b99f1f26..8c4cd3aaf 100644 --- a/src/main/java/emu/grasscutter/data/GameData.java +++ b/src/main/java/emu/grasscutter/data/GameData.java @@ -207,6 +207,10 @@ 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 new file mode 100644 index 000000000..743fd6791 --- /dev/null +++ b/src/main/java/emu/grasscutter/data/excels/CombineBonusData.java @@ -0,0 +1,23 @@ +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/CombineManger.java b/src/main/java/emu/grasscutter/game/combine/CombineManger.java index 090c82024..c42f93d16 100644 --- a/src/main/java/emu/grasscutter/game/combine/CombineManger.java +++ b/src/main/java/emu/grasscutter/game/combine/CombineManger.java @@ -4,7 +4,9 @@ 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; import emu.grasscutter.game.player.Player; import emu.grasscutter.game.props.ActionReason; @@ -16,9 +18,11 @@ import emu.grasscutter.server.packet.send.PacketCombineFormulaDataNotify; import emu.grasscutter.server.packet.send.PacketCombineRsp; import emu.grasscutter.server.packet.send.PacketReliquaryDecomposeRsp; 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.List; public class CombineManger extends BaseGameSystem { @@ -53,7 +57,7 @@ public class CombineManger extends BaseGameSystem { return true; } - public CombineResult combineItem(Player player, int cid, int count) { + public CombineResult combineItem(Player player, int cid, int count, long avatarGuid) { // check config exist if (!GameData.getCombineDataMap().containsKey(cid)) { player.getWorld().getHost().sendPacket(new PacketCombineRsp()); @@ -87,11 +91,124 @@ public class CombineManger extends BaseGameSystem { result.setMaterial(List.of()); result.setResult( List.of( - new ItemParamData( - combineData.getResultItemId(), combineData.getResultItemCount() * count))); - // TODO lucky characters - result.setExtra(List.of()); - result.setBack(List.of()); + new ItemParamData(combineData.getResultItemId(), combineData.getResultItemCount()))); + // 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); + for (int i = 0; i < count; i++) { + if (ThreadLocalRandom.current().nextDouble() <= luckyChange) { + luckyCount++; + } + } + } + + result.setExtra(new ArrayList()); + result.setBack(new ArrayList()); + result.setRandom(new ArrayList()); + + // add lucky items + if (luckyCount > 0) { + switch (combineBonusData.getBonusType()) { + case "COMBINE_BONUS_DOUBLE" -> { + var combineExtra = new ItemParamData(combineData.getResultItemId(), luckyCount * 2); + player.getInventory().addItem(combineExtra); + result.getExtra().add(combineExtra); + } + case "COMBINE_BONUS_REFUND" -> { + if (combineData.getMaterialItems().size() == 1) { + 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(); + mapIdCount.put(randomId, mapIdCount.getOrDefault(randomId, 0) + 1); + } + + for (var entry : mapIdCount.entrySet()) { + var combineBack = new ItemParamData(entry.getKey(), entry.getValue()); + player.getInventory().addItem(combineBack); + result.getBack().add(combineBack); + } + } + } + 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 + HashMap itemToRegion = + new HashMap<>() { + { + put(104301, 1); + 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); + } + }; + + // 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()); + } + + // check material id in itemToRegion + var itemId = combineData.getMaterialItems().get(0).getItemId(); + int rank = 0; // rank of material + if (itemToRegion.get(itemId) != null) rank = 1; + if (itemToRegion.get(itemId - 1) != null) { + rank = 2; + itemId -= 1; + } + + 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 + 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())); + 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()); + player.getInventory().addItem(combineRandom); + result.getRandom().add(combineRandom); + } + } + } + } + } return result; } diff --git a/src/main/java/emu/grasscutter/game/combine/CombineResult.java b/src/main/java/emu/grasscutter/game/combine/CombineResult.java index df8ace74b..8c59c37ce 100644 --- a/src/main/java/emu/grasscutter/game/combine/CombineResult.java +++ b/src/main/java/emu/grasscutter/game/combine/CombineResult.java @@ -2,42 +2,13 @@ package emu.grasscutter.game.combine; import emu.grasscutter.data.common.ItemParamData; import java.util.List; +import lombok.Getter; +import lombok.Setter; public class CombineResult { - private List material; - private List result; - private List extra; - private List back; - - public List getMaterial() { - return material; - } - - public void setMaterial(List material) { - this.material = material; - } - - public List getResult() { - return result; - } - - public void setResult(List result) { - this.result = result; - } - - public List getExtra() { - return extra; - } - - public void setExtra(List extra) { - this.extra = extra; - } - - public List getBack() { - return back; - } - - public void setBack(List back) { - this.back = back; - } + @Getter @Setter private List material; + @Getter @Setter private List result; + @Getter @Setter private List extra; + @Getter @Setter private List back; + @Getter @Setter private List random; } diff --git a/src/main/java/emu/grasscutter/server/packet/recv/HandlerCombineReq.java b/src/main/java/emu/grasscutter/server/packet/recv/HandlerCombineReq.java index fa8bd96d3..dc0b889ed 100644 --- a/src/main/java/emu/grasscutter/server/packet/recv/HandlerCombineReq.java +++ b/src/main/java/emu/grasscutter/server/packet/recv/HandlerCombineReq.java @@ -23,7 +23,11 @@ public class HandlerCombineReq extends PacketHandler { session .getServer() .getCombineSystem() - .combineItem(session.getPlayer(), req.getCombineId(), req.getCombineCount()); + .combineItem( + session.getPlayer(), + req.getCombineId(), + req.getCombineCount(), + req.getAvatarGuid()); if (result == null) { return; @@ -36,7 +40,7 @@ public class HandlerCombineReq extends PacketHandler { toItemParamList(result.getResult()), toItemParamList(result.getExtra()), toItemParamList(result.getBack()), - toItemParamList(result.getBack()))); + toItemParamList(result.getRandom()))); } private List toItemParamList(List list) { From cf8af7d7544e5258fbb79eb420ccf4408ec84999 Mon Sep 17 00:00:00 2001 From: Phong940253 Date: Thu, 10 Aug 2023 01:00:20 +0700 Subject: [PATCH 2/7] change regionToId to constant --- .../game/combine/CombineManger.java | 32 +++++++++---------- 1 file changed, 15 insertions(+), 17 deletions(-) diff --git a/src/main/java/emu/grasscutter/game/combine/CombineManger.java b/src/main/java/emu/grasscutter/game/combine/CombineManger.java index c42f93d16..a25f39491 100644 --- a/src/main/java/emu/grasscutter/game/combine/CombineManger.java +++ b/src/main/java/emu/grasscutter/game/combine/CombineManger.java @@ -23,6 +23,7 @@ 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; public class CombineManger extends BaseGameSystem { @@ -148,23 +149,20 @@ public class CombineManger extends BaseGameSystem { // 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 - HashMap itemToRegion = - new HashMap<>() { - { - put(104301, 1); - 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); - } - }; + 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 + ); // get list of material id with every region HashMap> regionToId = new HashMap<>(); From 7e87fd246b3dc2573420d6d7ca9a9cd22514d263 Mon Sep 17 00:00:00 2001 From: Phong940253 Date: Thu, 10 Aug 2023 01:06:16 +0700 Subject: [PATCH 3/7] fix double extra item bonus --- src/main/java/emu/grasscutter/game/combine/CombineManger.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/emu/grasscutter/game/combine/CombineManger.java b/src/main/java/emu/grasscutter/game/combine/CombineManger.java index a25f39491..1791410ec 100644 --- a/src/main/java/emu/grasscutter/game/combine/CombineManger.java +++ b/src/main/java/emu/grasscutter/game/combine/CombineManger.java @@ -115,7 +115,7 @@ public class CombineManger extends BaseGameSystem { if (luckyCount > 0) { switch (combineBonusData.getBonusType()) { case "COMBINE_BONUS_DOUBLE" -> { - var combineExtra = new ItemParamData(combineData.getResultItemId(), luckyCount * 2); + var combineExtra = new ItemParamData(combineData.getResultItemId(), luckyCount); player.getInventory().addItem(combineExtra); result.getExtra().add(combineExtra); } From 75a6d2d9189af5ee7d7b8d7016a915a4374d52fa Mon Sep 17 00:00:00 2001 From: Phong Date: Fri, 11 Aug 2023 11:49:08 +0700 Subject: [PATCH 4/7] Apply suggestions from code review (clean code CombineResult) Co-authored-by: Magix <27646710+KingRainbow44@users.noreply.github.com> --- .../grasscutter/data/excels/CombineBonusData.java | 2 +- .../emu/grasscutter/game/combine/CombineResult.java | 12 ++++++------ 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/main/java/emu/grasscutter/data/excels/CombineBonusData.java b/src/main/java/emu/grasscutter/data/excels/CombineBonusData.java index 743fd6791..cddab9a7f 100644 --- a/src/main/java/emu/grasscutter/data/excels/CombineBonusData.java +++ b/src/main/java/emu/grasscutter/data/excels/CombineBonusData.java @@ -8,7 +8,7 @@ import lombok.Getter; import lombok.Setter; @ResourceType( - name = {"CombineBonusExcelConfigData.json"}, + name = "CombineBonusExcelConfigData.json", loadPriority = LoadPriority.LOW) public class CombineBonusData extends GameResource { @Getter @Setter private int avatarId; diff --git a/src/main/java/emu/grasscutter/game/combine/CombineResult.java b/src/main/java/emu/grasscutter/game/combine/CombineResult.java index 8c59c37ce..40936b67d 100644 --- a/src/main/java/emu/grasscutter/game/combine/CombineResult.java +++ b/src/main/java/emu/grasscutter/game/combine/CombineResult.java @@ -4,11 +4,11 @@ import emu.grasscutter.data.common.ItemParamData; import java.util.List; import lombok.Getter; import lombok.Setter; - +@Setter @Getter public class CombineResult { - @Getter @Setter private List material; - @Getter @Setter private List result; - @Getter @Setter private List extra; - @Getter @Setter private List back; - @Getter @Setter private List random; + private List material; + private List result; + private List extra; + private List back; + private List random; } From af1da93d68a641c92461ef24690ca6ace3c69452 Mon Sep 17 00:00:00 2001 From: Magix <27646710+KingRainbow44@users.noreply.github.com> Date: Fri, 11 Aug 2023 23:52:30 -0400 Subject: [PATCH 5/7] Update src/main/java/emu/grasscutter/game/combine/CombineResult.java --- src/main/java/emu/grasscutter/game/combine/CombineResult.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/java/emu/grasscutter/game/combine/CombineResult.java b/src/main/java/emu/grasscutter/game/combine/CombineResult.java index 40936b67d..7aeb44b0b 100644 --- a/src/main/java/emu/grasscutter/game/combine/CombineResult.java +++ b/src/main/java/emu/grasscutter/game/combine/CombineResult.java @@ -4,6 +4,7 @@ import emu.grasscutter.data.common.ItemParamData; import java.util.List; import lombok.Getter; import lombok.Setter; + @Setter @Getter public class CombineResult { private List material; From d248dddc53bf676ff9fd7c1314698d27a8562a8d Mon Sep 17 00:00:00 2001 From: Phong Date: Thu, 25 Jul 2024 08:06:00 +0700 Subject: [PATCH 6/7] 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); } From 0b8f9ad8514db62f8ef7bdb30537b44dcc20b193 Mon Sep 17 00:00:00 2001 From: Phong Date: Thu, 25 Jul 2024 09:01:35 +0700 Subject: [PATCH 7/7] refactor: Replace HashMap with Map in GameConstants & CombineManager --- src/main/java/emu/grasscutter/GameConstants.java | 4 ++-- .../java/emu/grasscutter/game/combine/CombineManger.java | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/main/java/emu/grasscutter/GameConstants.java b/src/main/java/emu/grasscutter/GameConstants.java index 9189478cb..0411fae7c 100644 --- a/src/main/java/emu/grasscutter/GameConstants.java +++ b/src/main/java/emu/grasscutter/GameConstants.java @@ -59,7 +59,7 @@ 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<>() {{ + public static final Map YAE_MIKO_ITEM_TO_REGION_COMBINE_BONUS = new HashMap<>() {{ put(104304, 1); put(104307, 1); put(104310, 2); @@ -72,7 +72,7 @@ public final class GameConstants { put(104332, 4); put(104335, 4); }}; - public static final HashMap> YAE_MIKO_REGION_TO_ITEM_COMBINE_BONUS = new HashMap<>() {{ + public static final Map> 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)); diff --git a/src/main/java/emu/grasscutter/game/combine/CombineManger.java b/src/main/java/emu/grasscutter/game/combine/CombineManger.java index 8f7c27098..cd2ef1507 100644 --- a/src/main/java/emu/grasscutter/game/combine/CombineManger.java +++ b/src/main/java/emu/grasscutter/game/combine/CombineManger.java @@ -128,7 +128,7 @@ public class CombineManger extends BaseGameSystem { player.getInventory().addItem(combineBack); result.getBack().add(combineBack); } else { - HashMap mapIdCount = new HashMap<>(); + Map mapIdCount = new HashMap<>(); for (int i = 0; i < luckyCount; i++) { var randomId = combineData .getMaterialItems() @@ -152,10 +152,10 @@ public class CombineManger extends BaseGameSystem { // material excluded) when crafting. The rarity is that of the base material." // from wiki // map of material id to region id - HashMap itemToRegion = GameConstants.YAE_MIKO_ITEM_TO_REGION_COMBINE_BONUS; + Map itemToRegion = GameConstants.YAE_MIKO_ITEM_TO_REGION_COMBINE_BONUS; // get list of material id with every region - HashMap> regionToId = GameConstants.YAE_MIKO_REGION_TO_ITEM_COMBINE_BONUS; + Map> regionToId = GameConstants.YAE_MIKO_REGION_TO_ITEM_COMBINE_BONUS; // check material id in itemToRegion int itemId = combineData.getMaterialItems().get(0).getItemId();