From 85f44ebdf363e74aec5df8e6f11104afc2329d89 Mon Sep 17 00:00:00 2001 From: AnimeGitB Date: Mon, 17 Oct 2022 20:40:07 +1030 Subject: [PATCH] Refactor out some EntrySets --- .../emu/grasscutter/game/avatar/Avatar.java | 44 ++++++++----------- .../grasscutter/game/entity/EntityAvatar.java | 8 +--- .../grasscutter/game/entity/EntityGadget.java | 2 +- .../game/entity/EntityMonster.java | 8 +--- .../game/entity/EntityVehicle.java | 9 +--- .../grasscutter/game/entity/GameEntity.java | 11 ++--- .../game/entity/platform/EntityPlatform.java | 9 +--- .../EntitySolarIsotomaElevatorPlatform.java | 11 +---- .../game/managers/cooking/CookingManager.java | 11 ++--- .../game/managers/stamina/StaminaManager.java | 5 +-- .../game/systems/InventorySystem.java | 21 ++++----- .../packet/send/PacketAvatarDataNotify.java | 21 +++------ .../PacketAvatarExpeditionAllDataRsp.java | 2 +- .../send/PacketAvatarTeamUpdateNotify.java | 10 +---- .../send/PacketCustomTeamListNotify.java | 10 +---- .../packet/send/PacketDestroyMaterialRsp.java | 13 +++--- 16 files changed, 59 insertions(+), 136 deletions(-) diff --git a/src/main/java/emu/grasscutter/game/avatar/Avatar.java b/src/main/java/emu/grasscutter/game/avatar/Avatar.java index d0d56b53d..06247a691 100644 --- a/src/main/java/emu/grasscutter/game/avatar/Avatar.java +++ b/src/main/java/emu/grasscutter/game/avatar/Avatar.java @@ -467,37 +467,31 @@ public class Avatar { } // Set stuff - for (Int2IntOpenHashMap.Entry e : setMap.int2IntEntrySet()) { - ReliquarySetData setData = GameData.getReliquarySetDataMap().get(e.getIntKey()); - if (setData == null) { - continue; - } + setMap.forEach((setId, amount) -> { + ReliquarySetData setData = GameData.getReliquarySetDataMap().get((int) setId); + if (setData == null) return; // Calculate how many items are from the set - int amount = e.getIntValue(); - // Add affix data from set bonus - for (int setIndex = 0; setIndex < setData.getSetNeedNum().length; setIndex++) { - if (amount >= setData.getSetNeedNum()[setIndex]) { - int affixId = (setData.getEquipAffixId() * 10) + setIndex; + val setNeedNum = setData.getSetNeedNum(); + for (int setIndex = 0; setIndex < setNeedNum.length; setIndex++) { + if (amount < setNeedNum[setIndex]) break; - EquipAffixData affix = GameData.getEquipAffixDataMap().get(affixId); - if (affix == null) { - continue; - } - - // Add properties from this affix to our avatar - for (FightPropData prop : affix.getAddProps()) { - this.addFightProperty(prop.getProp(), prop.getValue()); - } - - // Add any skill strings from this affix - this.addToExtraAbilityEmbryos(affix.getOpenConfig(), true); - } else { - break; + int affixId = (setData.getEquipAffixId() * 10) + setIndex; + EquipAffixData affix = GameData.getEquipAffixDataMap().get(affixId); + if (affix == null) { + continue; } + + // Add properties from this affix to our avatar + for (FightPropData prop : affix.getAddProps()) { + this.addFightProperty(prop.getProp(), prop.getValue()); + } + + // Add any skill strings from this affix + this.addToExtraAbilityEmbryos(affix.getOpenConfig(), true); } - } + }); // Weapon GameItem weapon = this.getWeapon(); diff --git a/src/main/java/emu/grasscutter/game/entity/EntityAvatar.java b/src/main/java/emu/grasscutter/game/entity/EntityAvatar.java index 7c04f4a4f..8bce738fa 100644 --- a/src/main/java/emu/grasscutter/game/entity/EntityAvatar.java +++ b/src/main/java/emu/grasscutter/game/entity/EntityAvatar.java @@ -247,13 +247,7 @@ public class EntityAvatar extends GameEntity { entityInfo.setMotionInfo(this.getMotionInfo()); } - for (Int2FloatMap.Entry entry : getFightProperties().int2FloatEntrySet()) { - if (entry.getIntKey() == 0) { - continue; - } - FightPropPair fightProp = FightPropPair.newBuilder().setPropType(entry.getIntKey()).setPropValue(entry.getFloatValue()).build(); - entityInfo.addFightPropList(fightProp); - } + this.addAllFightPropsToEntityInfo(entityInfo); PropPair pair = PropPair.newBuilder() .setType(PlayerProperty.PROP_LEVEL.getId()) diff --git a/src/main/java/emu/grasscutter/game/entity/EntityGadget.java b/src/main/java/emu/grasscutter/game/entity/EntityGadget.java index bd5292893..a7996b6d8 100644 --- a/src/main/java/emu/grasscutter/game/entity/EntityGadget.java +++ b/src/main/java/emu/grasscutter/game/entity/EntityGadget.java @@ -217,7 +217,7 @@ public class EntityGadget extends EntityBaseGadget { // We do not use the getter to null check because the getter will create a fight prop map if it is null if (this.fightProp != null) { - this.addAllFightPropsToEntityInfo(entityInfo); + addAllFightPropsToEntityInfo(entityInfo); } SceneGadgetInfo.Builder gadgetInfo = SceneGadgetInfo.newBuilder() diff --git a/src/main/java/emu/grasscutter/game/entity/EntityMonster.java b/src/main/java/emu/grasscutter/game/entity/EntityMonster.java index 6dbbced9d..31e4b8630 100644 --- a/src/main/java/emu/grasscutter/game/entity/EntityMonster.java +++ b/src/main/java/emu/grasscutter/game/entity/EntityMonster.java @@ -255,13 +255,7 @@ public class EntityMonster extends GameEntity { .setEntityAuthorityInfo(authority) .setLifeState(this.getLifeState().getValue()); - for (Int2FloatMap.Entry entry : getFightProperties().int2FloatEntrySet()) { - if (entry.getIntKey() == 0) { - continue; - } - FightPropPair fightProp = FightPropPair.newBuilder().setPropType(entry.getIntKey()).setPropValue(entry.getFloatValue()).build(); - entityInfo.addFightPropList(fightProp); - } + this.addAllFightPropsToEntityInfo(entityInfo); PropPair pair = PropPair.newBuilder() .setType(PlayerProperty.PROP_LEVEL.getId()) diff --git a/src/main/java/emu/grasscutter/game/entity/EntityVehicle.java b/src/main/java/emu/grasscutter/game/entity/EntityVehicle.java index 1cf252614..462a0d402 100644 --- a/src/main/java/emu/grasscutter/game/entity/EntityVehicle.java +++ b/src/main/java/emu/grasscutter/game/entity/EntityVehicle.java @@ -122,14 +122,7 @@ public class EntityVehicle extends EntityBaseGadget { .setPropValue(ProtoHelper.newPropValue(PlayerProperty.PROP_LEVEL, 47)) .build(); - for (Int2FloatMap.Entry entry : getFightProperties().int2FloatEntrySet()) { - if (entry.getIntKey() == 0) { - continue; - } - FightPropPair fightProp = FightPropPair.newBuilder().setPropType(entry.getIntKey()).setPropValue(entry.getFloatValue()).build(); - entityInfo.addFightPropList(fightProp); - } - + this.addAllFightPropsToEntityInfo(entityInfo); entityInfo.addPropList(pair); return entityInfo.build(); diff --git a/src/main/java/emu/grasscutter/game/entity/GameEntity.java b/src/main/java/emu/grasscutter/game/entity/GameEntity.java index 91bb3230e..220edcb47 100644 --- a/src/main/java/emu/grasscutter/game/entity/GameEntity.java +++ b/src/main/java/emu/grasscutter/game/entity/GameEntity.java @@ -113,13 +113,10 @@ public abstract class GameEntity { } public void addAllFightPropsToEntityInfo(SceneEntityInfo.Builder entityInfo) { - for (Int2FloatMap.Entry entry : this.getFightProperties().int2FloatEntrySet()) { - if (entry.getIntKey() == 0) { - continue; - } - FightPropPair fightProp = FightPropPair.newBuilder().setPropType(entry.getIntKey()).setPropValue(entry.getFloatValue()).build(); - entityInfo.addFightPropList(fightProp); - } + this.getFightProperties().forEach((key, value) -> { + if (key == 0) return; + entityInfo.addFightPropList(FightPropPair.newBuilder().setPropType(key).setPropValue(value).build()); + }); } protected MotionInfo getMotionInfo() { diff --git a/src/main/java/emu/grasscutter/game/entity/platform/EntityPlatform.java b/src/main/java/emu/grasscutter/game/entity/platform/EntityPlatform.java index afee26cd6..3ec85c35e 100644 --- a/src/main/java/emu/grasscutter/game/entity/platform/EntityPlatform.java +++ b/src/main/java/emu/grasscutter/game/entity/platform/EntityPlatform.java @@ -93,14 +93,7 @@ public class EntityPlatform extends EntityBaseGadget { .setGadget(gadgetInfo) .setLifeState(1); - for (Int2FloatMap.Entry entry : getFightProperties().int2FloatEntrySet()) { - if (entry.getIntKey() == 0) { - continue; - } - FightPropPairOuterClass.FightPropPair fightProp = FightPropPairOuterClass.FightPropPair.newBuilder().setPropType(entry.getIntKey()).setPropValue(entry.getFloatValue()).build(); - entityInfo.addFightPropList(fightProp); - } - + this.addAllFightPropsToEntityInfo(entityInfo); return entityInfo.build(); } diff --git a/src/main/java/emu/grasscutter/game/entity/platform/EntitySolarIsotomaElevatorPlatform.java b/src/main/java/emu/grasscutter/game/entity/platform/EntitySolarIsotomaElevatorPlatform.java index 9801f4ca5..9bc159490 100644 --- a/src/main/java/emu/grasscutter/game/entity/platform/EntitySolarIsotomaElevatorPlatform.java +++ b/src/main/java/emu/grasscutter/game/entity/platform/EntitySolarIsotomaElevatorPlatform.java @@ -89,16 +89,7 @@ public class EntitySolarIsotomaElevatorPlatform extends EntityPlatform { Grasscutter.getLogger().warn("Why gadget owner doesn't exist?"); } - for (var entry : getFightProperties().int2FloatEntrySet()) { - if (entry.getIntKey() == 0) { - continue; - } - var fightProp = FightPropPairOuterClass.FightPropPair.newBuilder() - .setPropType(entry.getIntKey()) - .setPropValue(entry.getFloatValue()) - .build(); - info.addFightPropList(fightProp); - } + this.addAllFightPropsToEntityInfo(info); info.setLifeState(1) .setGadget(gadget) diff --git a/src/main/java/emu/grasscutter/game/managers/cooking/CookingManager.java b/src/main/java/emu/grasscutter/game/managers/cooking/CookingManager.java index 09d4874c9..f103cc338 100644 --- a/src/main/java/emu/grasscutter/game/managers/cooking/CookingManager.java +++ b/src/main/java/emu/grasscutter/game/managers/cooking/CookingManager.java @@ -172,16 +172,11 @@ public class CookingManager extends BasePlayerManager { // Construct CookRecipeData protos. List data = new ArrayList<>(); - for (var recipe : unlockedRecipes.entrySet()) { - int recipeId = recipe.getKey(); - int proficiency = recipe.getValue(); - - CookRecipeDataOuterClass.CookRecipeData proto = CookRecipeDataOuterClass.CookRecipeData.newBuilder() + unlockedRecipes.forEach((recipeId, proficiency) -> + data.add(CookRecipeDataOuterClass.CookRecipeData.newBuilder() .setRecipeId(recipeId) .setProficiency(proficiency) - .build(); - data.add(proto); - } + .build())); // Send packet. this.player.sendPacket(new PacketCookDataNotify(data)); diff --git a/src/main/java/emu/grasscutter/game/managers/stamina/StaminaManager.java b/src/main/java/emu/grasscutter/game/managers/stamina/StaminaManager.java index 7450c571c..975acb3c1 100644 --- a/src/main/java/emu/grasscutter/game/managers/stamina/StaminaManager.java +++ b/src/main/java/emu/grasscutter/game/managers/stamina/StaminaManager.java @@ -304,9 +304,8 @@ public class StaminaManager extends BasePlayerManager { session.send(new PacketVehicleStaminaNotify(vehicleId, ((float) newStamina) / 100)); } // notify updated - for (Map.Entry listener : afterUpdateStaminaListeners.entrySet()) { - listener.getValue().onAfterUpdateStamina(reason, newStamina, isCharacterStamina); - } + int s = newStamina; + afterUpdateStaminaListeners.forEach((k, v) -> v.onAfterUpdateStamina(reason, s, isCharacterStamina)); return newStamina; } diff --git a/src/main/java/emu/grasscutter/game/systems/InventorySystem.java b/src/main/java/emu/grasscutter/game/systems/InventorySystem.java index db7a4ce9c..baa8ba150 100644 --- a/src/main/java/emu/grasscutter/game/systems/InventorySystem.java +++ b/src/main/java/emu/grasscutter/game/systems/InventorySystem.java @@ -36,6 +36,7 @@ import emu.grasscutter.server.game.BaseGameSystem; import emu.grasscutter.server.game.GameServer; import emu.grasscutter.server.packet.send.*; import emu.grasscutter.utils.Utils; +import it.unimi.dsi.fastutil.ints.Int2FloatArrayMap; import it.unimi.dsi.fastutil.ints.Int2IntArrayMap; import it.unimi.dsi.fastutil.ints.Int2IntMap; import it.unimi.dsi.fastutil.ints.Int2IntOpenHashMap; @@ -634,7 +635,7 @@ public class InventorySystem extends BaseGameSystem { Map oldPropMap = avatar.getFightProperties(); if (oldLevel != level) { // Deep copy if level has changed - oldPropMap = avatar.getFightProperties().int2FloatEntrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)); + oldPropMap = new Int2FloatArrayMap(avatar.getFightProperties()); } // Done @@ -696,7 +697,8 @@ public class InventorySystem extends BaseGameSystem { public void destroyMaterial(Player player, List list) { // Return materials - Int2IntOpenHashMap returnMaterialMap = new Int2IntOpenHashMap(); + val returnMaterialMap = new Int2IntOpenHashMap(); + val inventory = player.getInventory(); for (MaterialInfo info : list) { // Sanity check @@ -704,28 +706,27 @@ public class InventorySystem extends BaseGameSystem { continue; } - GameItem item = player.getInventory().getItemByGuid(info.getGuid()); + GameItem item = inventory.getItemByGuid(info.getGuid()); if (item == null || !item.isDestroyable()) { continue; } // Remove int removeAmount = Math.min(info.getCount(), item.getCount()); - player.getInventory().removeItem(item, removeAmount); + inventory.removeItem(item, removeAmount); // Delete material return items - if (item.getItemData().getDestroyReturnMaterial().length > 0) { - for (int i = 0; i < item.getItemData().getDestroyReturnMaterial().length; i++) { - returnMaterialMap.addTo(item.getItemData().getDestroyReturnMaterial()[i], item.getItemData().getDestroyReturnMaterialCount()[i]); + val data = item.getItemData(); + if (data.getDestroyReturnMaterial().length > 0) { + for (int i = 0; i < data.getDestroyReturnMaterial().length; i++) { + returnMaterialMap.addTo(data.getDestroyReturnMaterial()[i], data.getDestroyReturnMaterialCount()[i]); } } } // Give back items if (returnMaterialMap.size() > 0) { - for (Int2IntMap.Entry e : returnMaterialMap.int2IntEntrySet()) { - player.getInventory().addItem(new GameItem(e.getIntKey(), e.getIntValue())); - } + returnMaterialMap.forEach((id, count) -> inventory.addItem(new GameItem(id, count))); } // Packets diff --git a/src/main/java/emu/grasscutter/server/packet/send/PacketAvatarDataNotify.java b/src/main/java/emu/grasscutter/server/packet/send/PacketAvatarDataNotify.java index 2c91e743c..c71dc4c71 100644 --- a/src/main/java/emu/grasscutter/server/packet/send/PacketAvatarDataNotify.java +++ b/src/main/java/emu/grasscutter/server/packet/send/PacketAvatarDataNotify.java @@ -1,14 +1,10 @@ package emu.grasscutter.server.packet.send; -import java.util.Map.Entry; - import emu.grasscutter.game.avatar.Avatar; import emu.grasscutter.game.player.Player; -import emu.grasscutter.game.player.TeamInfo; import emu.grasscutter.net.packet.BasePacket; import emu.grasscutter.net.packet.PacketOpcodes; import emu.grasscutter.net.proto.AvatarDataNotifyOuterClass.AvatarDataNotify; -import emu.grasscutter.net.proto.AvatarTeamOuterClass.AvatarTeam; public class PacketAvatarDataNotify extends BasePacket { @@ -21,21 +17,14 @@ public class PacketAvatarDataNotify extends BasePacket { .addAllOwnedFlycloakList(player.getFlyCloakList()) .addAllOwnedCostumeList(player.getCostumeList()); - for (Avatar avatar : player.getAvatars()) { - proto.addAvatarList(avatar.toProto()); - } + player.getAvatars().forEach(avatar -> proto.addAvatarList(avatar.toProto())); - // Add the id list for custom teams. - for (int id : player.getTeamManager().getTeams().keySet()) { - if (id > 4) { + player.getTeamManager().getTeams().forEach((id, teamInfo) -> { + proto.putAvatarTeamMap(id, teamInfo.toProto(player)); + if (id > 4) { // Add the id list for custom teams. proto.addCustomTeamIds(id); } - } - - for (Entry entry : player.getTeamManager().getTeams().entrySet()) { - TeamInfo teamInfo = entry.getValue(); - proto.putAvatarTeamMap(entry.getKey(), teamInfo.toProto(player)); - } + }); // Set main character Avatar mainCharacter = player.getAvatars().getAvatarById(player.getMainCharacterId()); diff --git a/src/main/java/emu/grasscutter/server/packet/send/PacketAvatarExpeditionAllDataRsp.java b/src/main/java/emu/grasscutter/server/packet/send/PacketAvatarExpeditionAllDataRsp.java index a3ad052eb..6ce17ee5e 100644 --- a/src/main/java/emu/grasscutter/server/packet/send/PacketAvatarExpeditionAllDataRsp.java +++ b/src/main/java/emu/grasscutter/server/packet/send/PacketAvatarExpeditionAllDataRsp.java @@ -12,7 +12,7 @@ public class PacketAvatarExpeditionAllDataRsp extends BasePacket { public PacketAvatarExpeditionAllDataRsp(Map expeditionInfo, int expeditionCountLimit) { super(PacketOpcodes.AvatarExpeditionAllDataRsp); - List openExpeditionList = new ArrayList<>(List.of(306,305,304,303,302,301,206,105,204,104,203,103,202,101,102,201,106,205,401,402,403,404,405,406)); + var openExpeditionList = List.of(306,305,304,303,302,301,206,105,204,104,203,103,202,101,102,201,106,205,401,402,403,404,405,406); this.setData(AvatarExpeditionAllDataRsp.newBuilder() .addAllOpenExpeditionList(openExpeditionList) diff --git a/src/main/java/emu/grasscutter/server/packet/send/PacketAvatarTeamUpdateNotify.java b/src/main/java/emu/grasscutter/server/packet/send/PacketAvatarTeamUpdateNotify.java index 79be0b104..63f43dd0d 100644 --- a/src/main/java/emu/grasscutter/server/packet/send/PacketAvatarTeamUpdateNotify.java +++ b/src/main/java/emu/grasscutter/server/packet/send/PacketAvatarTeamUpdateNotify.java @@ -1,13 +1,8 @@ package emu.grasscutter.server.packet.send; -import java.util.Map.Entry; - -import emu.grasscutter.game.avatar.Avatar; import emu.grasscutter.game.player.Player; -import emu.grasscutter.game.player.TeamInfo; import emu.grasscutter.net.packet.BasePacket; import emu.grasscutter.net.packet.PacketOpcodes; -import emu.grasscutter.net.proto.AvatarTeamOuterClass.AvatarTeam; import emu.grasscutter.net.proto.AvatarTeamUpdateNotifyOuterClass.AvatarTeamUpdateNotify; public class PacketAvatarTeamUpdateNotify extends BasePacket { @@ -17,10 +12,7 @@ public class PacketAvatarTeamUpdateNotify extends BasePacket { AvatarTeamUpdateNotify.Builder proto = AvatarTeamUpdateNotify.newBuilder(); - for (Entry entry : player.getTeamManager().getTeams().entrySet()) { - TeamInfo teamInfo = entry.getValue(); - proto.putAvatarTeamMap(entry.getKey(), teamInfo.toProto(player)); - } + player.getTeamManager().getTeams().forEach((id, teamInfo) -> proto.putAvatarTeamMap(id, teamInfo.toProto(player))); this.setData(proto); } diff --git a/src/main/java/emu/grasscutter/server/packet/send/PacketCustomTeamListNotify.java b/src/main/java/emu/grasscutter/server/packet/send/PacketCustomTeamListNotify.java index e011b2d91..182adb0fb 100644 --- a/src/main/java/emu/grasscutter/server/packet/send/PacketCustomTeamListNotify.java +++ b/src/main/java/emu/grasscutter/server/packet/send/PacketCustomTeamListNotify.java @@ -1,13 +1,8 @@ package emu.grasscutter.server.packet.send; -import java.util.Map.Entry; - -import emu.grasscutter.game.avatar.Avatar; import emu.grasscutter.game.player.Player; -import emu.grasscutter.game.player.TeamInfo; import emu.grasscutter.net.packet.BasePacket; import emu.grasscutter.net.packet.PacketOpcodes; -import emu.grasscutter.net.proto.AvatarTeamOuterClass.AvatarTeam; import emu.grasscutter.net.proto.CustomTeamListNotifyOuterClass.CustomTeamListNotify; public class PacketCustomTeamListNotify extends BasePacket { @@ -24,10 +19,7 @@ public class PacketCustomTeamListNotify extends BasePacket { } // Add the avatar lists for all the teams the player has. - for (Entry entry : player.getTeamManager().getTeams().entrySet()) { - TeamInfo teamInfo = entry.getValue(); - proto.putAvatarTeamMap(entry.getKey(), teamInfo.toProto(player)); - } + player.getTeamManager().getTeams().forEach((id, teamInfo) -> proto.putAvatarTeamMap(id, teamInfo.toProto(player))); this.setData(proto); } diff --git a/src/main/java/emu/grasscutter/server/packet/send/PacketDestroyMaterialRsp.java b/src/main/java/emu/grasscutter/server/packet/send/PacketDestroyMaterialRsp.java index 95fb95b6d..3fcc2d627 100644 --- a/src/main/java/emu/grasscutter/server/packet/send/PacketDestroyMaterialRsp.java +++ b/src/main/java/emu/grasscutter/server/packet/send/PacketDestroyMaterialRsp.java @@ -4,19 +4,18 @@ import emu.grasscutter.net.packet.BasePacket; import emu.grasscutter.net.packet.PacketOpcodes; import emu.grasscutter.net.proto.DestroyMaterialRspOuterClass.DestroyMaterialRsp; import it.unimi.dsi.fastutil.ints.Int2IntMap; -import it.unimi.dsi.fastutil.ints.Int2IntOpenHashMap; public class PacketDestroyMaterialRsp extends BasePacket { - public PacketDestroyMaterialRsp(Int2IntOpenHashMap returnMaterialMap) { + public PacketDestroyMaterialRsp(Int2IntMap returnMaterialMap) { super(PacketOpcodes.DestroyMaterialRsp); - DestroyMaterialRsp.Builder proto = DestroyMaterialRsp.newBuilder(); + var proto = DestroyMaterialRsp.newBuilder(); - for (Int2IntMap.Entry e : returnMaterialMap.int2IntEntrySet()) { - proto.addItemIdList(e.getIntKey()); - proto.addItemCountList(e.getIntValue()); - } + returnMaterialMap.forEach((id, count) -> { + proto.addItemIdList(id); + proto.addItemCountList(count); + }); this.setData(proto); }