From a957b8c5b8ca7270b9ce05663fc751524a240f89 Mon Sep 17 00:00:00 2001 From: GanyusLeftHorn <1244229+GanyusLeftHorn@users.noreply.github.com> Date: Fri, 1 Jul 2022 23:41:40 -0700 Subject: [PATCH] Notify client of unlocked recipies on login. --- .../game/managers/CookingManager.java | 78 +++++++++++++++++++ .../emu/grasscutter/game/player/Player.java | 16 ++++ .../grasscutter/server/game/GameServer.java | 2 + .../packet/send/PacketCookDataNotify.java | 21 +++++ 4 files changed, 117 insertions(+) create mode 100644 src/main/java/emu/grasscutter/game/managers/CookingManager.java create mode 100644 src/main/java/emu/grasscutter/server/packet/send/PacketCookDataNotify.java diff --git a/src/main/java/emu/grasscutter/game/managers/CookingManager.java b/src/main/java/emu/grasscutter/game/managers/CookingManager.java new file mode 100644 index 000000000..ad5b92b2a --- /dev/null +++ b/src/main/java/emu/grasscutter/game/managers/CookingManager.java @@ -0,0 +1,78 @@ +package emu.grasscutter.game.managers; + +import java.util.ArrayList; +import java.util.Dictionary; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +import emu.grasscutter.data.GameData; +import emu.grasscutter.game.player.Player; +import emu.grasscutter.net.proto.CookRecipeDataOuterClass; +import emu.grasscutter.server.packet.send.PacketCookDataNotify; + +public class CookingManager { + private static Set defaultUnlockedRecipies; + private final Player player; + + public CookingManager(Player player) { + this.player = player; + } + + public static void initialize() { + // Initialize the set of recipies that are unlocked by default. + defaultUnlockedRecipies = new HashSet<>(); + + for (var recipe : GameData.getCookRecipeDataMap().values()) { + if (recipe.isDefaultUnlocked()) { + defaultUnlockedRecipies.add(recipe.getId()); + } + } + } + + /******************** + * Perform cooking. + ********************/ + + + /******************** + * Notify unlocked recipies. + ********************/ + private void addDefaultUnlocked() { + // Get recipies that are already unlocked. + var unlockedRecipies = this.player.getUnlockedRecipies(); + + // Get recipies that should be unlocked by default but aren't. + var additionalRecipies = new HashSet<>(defaultUnlockedRecipies); + additionalRecipies.removeAll(unlockedRecipies.keySet()); + + // Add them to the player. + for (int id : additionalRecipies) { + unlockedRecipies.put(id, 0); + } + } + + public void sendCookDataNofity() { + // Default unlocked recipies to player if they don't have them yet. + this.addDefaultUnlocked(); + + // Get unlocked recipies. + var unlockedRecipies = this.player.getUnlockedRecipies(); + + // Construct CookRecipeData protos. + List data = new ArrayList<>(); + for (var recipe : unlockedRecipies.entrySet()) { + int recipeId = recipe.getKey(); + int proficiency = recipe.getValue(); + + CookRecipeDataOuterClass.CookRecipeData proto = CookRecipeDataOuterClass.CookRecipeData.newBuilder() + .setRecipeId(recipeId) + .setProficiency(proficiency) + .build(); + data.add(proto); + } + + // Send packet. + this.player.sendPacket(new PacketCookDataNotify(data)); + } +} diff --git a/src/main/java/emu/grasscutter/game/player/Player.java b/src/main/java/emu/grasscutter/game/player/Player.java index 59c001948..245742691 100644 --- a/src/main/java/emu/grasscutter/game/player/Player.java +++ b/src/main/java/emu/grasscutter/game/player/Player.java @@ -30,6 +30,7 @@ import emu.grasscutter.game.inventory.GameItem; import emu.grasscutter.game.inventory.Inventory; import emu.grasscutter.game.mail.Mail; import emu.grasscutter.game.mail.MailHandler; +import emu.grasscutter.game.managers.CookingManager; import emu.grasscutter.game.managers.FurnitureManager; import emu.grasscutter.game.managers.InsectCaptureManager; import emu.grasscutter.game.managers.ResinManager; @@ -113,6 +114,7 @@ public class Player { private Set unlockedFurniture; private Set unlockedFurnitureSuite; private List activeForges; + private Map unlockedRecipies; private Integer widgetId; @@ -184,6 +186,8 @@ public class Player { @Transient private GameHome home; @Transient private FurnitureManager furnitureManager; @Transient private BattlePassManager battlePassManager; + @Transient private CookingManager cookingManager; + // @Transient private @Getter @Transient private ActivityManager activityManager; @Transient private CollectionManager collectionManager; @@ -228,6 +232,7 @@ public class Player { this.unlockedFurniture = new HashSet<>(); this.unlockedFurnitureSuite = new HashSet<>(); this.activeForges = new ArrayList<>(); + this.unlockedRecipies = new HashMap<>(); this.setSceneId(3); this.setRegionId(1); @@ -254,6 +259,7 @@ public class Player { this.resinManager = new ResinManager(this); this.forgingManager = new ForgingManager(this); this.furnitureManager = new FurnitureManager(this); + this.cookingManager = new CookingManager(this); } // On player creation @@ -288,6 +294,7 @@ public class Player { this.deforestationManager = new DeforestationManager(this); this.forgingManager = new ForgingManager(this); this.furnitureManager = new FurnitureManager(this); + this.cookingManager = new CookingManager(this); } public int getUid() { @@ -666,6 +673,10 @@ public class Player { return this.activeForges; } + public Map getUnlockedRecipies() { + return this.unlockedRecipies; + } + public MpSettingType getMpSetting() { return MpSettingType.MP_SETTING_TYPE_ENTER_AFTER_APPLY; // TEMP } @@ -1298,6 +1309,10 @@ public class Player { return battlePassManager; } + public CookingManager getCookingManager() { + return cookingManager; + } + public void loadBattlePassManager() { if (this.battlePassManager != null) return; this.battlePassManager = DatabaseHelper.loadBattlePass(this); @@ -1506,6 +1521,7 @@ public class Player { session.send(new PacketCombineDataNotify(this.unlockedCombines)); this.forgingManager.sendForgeDataNotify(); this.resinManager.onPlayerLogin(); + this.cookingManager.sendCookDataNofity(); getTodayMoonCard(); // The timer works at 0:0, some users log in after that, use this method to check if they have received a reward today or not. If not, send the reward. // Battle Pass trigger diff --git a/src/main/java/emu/grasscutter/server/game/GameServer.java b/src/main/java/emu/grasscutter/server/game/GameServer.java index 21ad5907f..c7eae0a12 100644 --- a/src/main/java/emu/grasscutter/server/game/GameServer.java +++ b/src/main/java/emu/grasscutter/server/game/GameServer.java @@ -12,6 +12,7 @@ import emu.grasscutter.game.dungeons.DungeonManager; import emu.grasscutter.game.dungeons.challenge.DungeonChallenge; import emu.grasscutter.game.expedition.ExpeditionManager; import emu.grasscutter.game.gacha.GachaManager; +import emu.grasscutter.game.managers.CookingManager; import emu.grasscutter.game.managers.InventoryManager; import emu.grasscutter.game.managers.MultiplayerManager; import emu.grasscutter.game.managers.chat.ChatManager; @@ -88,6 +89,7 @@ public final class GameServer extends KcpServer { DungeonChallenge.initialize(); EnergyManager.initialize(); StaminaManager.initialize(); + CookingManager.initialize(); this.address = address; this.packetHandler = new GameServerPacketHandler(PacketHandler.class); diff --git a/src/main/java/emu/grasscutter/server/packet/send/PacketCookDataNotify.java b/src/main/java/emu/grasscutter/server/packet/send/PacketCookDataNotify.java new file mode 100644 index 000000000..569a63380 --- /dev/null +++ b/src/main/java/emu/grasscutter/server/packet/send/PacketCookDataNotify.java @@ -0,0 +1,21 @@ +package emu.grasscutter.server.packet.send; + +import java.util.List; + +import emu.grasscutter.net.packet.BasePacket; +import emu.grasscutter.net.packet.PacketOpcodes; +import emu.grasscutter.net.proto.CookDataNotifyOuterClass.CookDataNotify; +import emu.grasscutter.net.proto.CookRecipeDataOuterClass.CookRecipeData; + +public class PacketCookDataNotify extends BasePacket { + + public PacketCookDataNotify(List recipies) { + super(PacketOpcodes.CookDataNotify); + + CookDataNotify proto = CookDataNotify.newBuilder() + .addAllRecipeDataList(recipies) + .build(); + + this.setData(proto); + } +}