From 002854d8ba082bcd2b321c32a25f5a10e1a90d7f Mon Sep 17 00:00:00 2001 From: Max Gerbeth Date: Sun, 15 Jan 2023 19:02:54 +0100 Subject: [PATCH] Shop.java Give Player the Item --- src/main/java/org/bitbiome/shop/Shop.java | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/bitbiome/shop/Shop.java b/src/main/java/org/bitbiome/shop/Shop.java index 529caca..fceff83 100644 --- a/src/main/java/org/bitbiome/shop/Shop.java +++ b/src/main/java/org/bitbiome/shop/Shop.java @@ -21,7 +21,6 @@ public class Shop { } public boolean buy(String itemName, int amount){ - System.out.println("Hallo"); //Create File Objects File filePlayerConfig = new File("src/main/resources/playerconfig.json"); File fileGameConfig = new File("src/main/resources/gameconfig.json"); @@ -65,6 +64,25 @@ public class Shop { gold -= costs; playerConfig.put("gold", gold); + //Gameconfig amount reduese + JSONArray jsonArray2 = gameConfig.getJSONArray("shopitems"); + int intNewAmount; + + for(int i = 0; i < jsonArray2.length(); i++) { + JSONObject tempJSON = jsonArray2.getJSONObject(i); + if (tempJSON.getString("name").equals(itemName)) { + intNewAmount = (int) tempJSON.get("amount") - amount; + jsonArray2.remove(i); + tempJSON.put("amount", intNewAmount); + jsonArray2.put(tempJSON); + gameConfig.put("shopitems", jsonArray2); + FileWriter fileWriter = new FileWriter("src/main/resources/gameconfig.json"); + fileWriter.write(gameConfig.toString()); + fileWriter.close(); + currentShopItems = loadItems(); + } + } + //Give Player the Item JSONArray jsonArray = playerConfig.getJSONArray("inventory"); String newAmount;