Spawn Recipe Commands - UQcsse3200/2024-studio-3 GitHub Wiki
SpawnRecipeCommands
is a class package responsible for spawning recipes into the players inventory in the game. This command is useful for developers or players during debugging or testing to quickly generate specific meals using predefined ingredients. The class allows users to specify a recipe and spawn it directly into the players inventory, replacing any exisiting item if necessary.
The SpawnRecipeCommands
class is designed to handle the spawn
command, which accecpts a single argument representing the name of a recipe in the format as follows;
- "bananasplit"
- "steakmeal"
- "acaibowl"
- "salad"
- "fruitsalad" When executed, the command spawns the corresponding meal, populating it with the necessary ingredients and adding it to the players inventory.
Upon the user pressing f1
, the ingame terminal will appear in the bottom left of the screen, as signified by the >
character. The user then types in the spawn
keyword, follwed by an accepted recipe string as stated above. This will be in the form;
spawn <recipeName>
If the provided argument is invalid or unrecognised, the command will return false
and log a debug message indicating the issue.
This method processes the 'spawn' command by validating the input determining which recipe to create based on the argument. It handles interactions with the players inventory to ensure that items are added or replaced correctly.
Before creating any recipes, the player entity and their inventory component must be retrieved;
Entity player = ServiceLocator.getPlayerService().getPlayer();
InventoryComponent playerInventory = player.getComponent(InventoryComponent.class);
In order to populate a recipe, the required ingredients must be created. Using banana split as an example;
Entity strawberry = ItemFactory.createBaseItem("strawberry");
Entity chocolate = ItemFactory.createBaseItem("chocolate");
Entity banana = ItemFactory.createBaseItem("banana");
As the creation of a recipe requires an array list, the ingredients of the recipe must be added to this;
ArrayList<IngredientComponent> bananaSplitIngredients = new ArrayList<>();
bananaSplitIngredients.add(strawberry.getComponent(IngredientComponent.class));
bananaSplitIngredients.add(chocolate.getComponent(IngredientComponent.class));
bananaSplitIngredients.add(banana.getComponent(IngredientComponent.class));
Then the recipe can be created using this array, and added to the players inventory as follows;
Entity bananaSplitEntity = ItemFactory.createMeal("bananaSplit", bananaSplitIngredients);
ItemComponent bananaSplitItem = bananaSplitEntity.getComponent(MealComponent.class);
playerInventory.addItem(bananaSplitItem);