From 70ace999d0570224d8e35d11b4690be426f8d0a1 Mon Sep 17 00:00:00 2001 From: Max Gerbeth Date: Sun, 15 Jan 2023 17:59:42 +0100 Subject: [PATCH] Give Player the Item --- src/main/java/org/bitbiome/shop/Shop.java | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/main/java/org/bitbiome/shop/Shop.java b/src/main/java/org/bitbiome/shop/Shop.java index eb30b21..481b6f4 100644 --- a/src/main/java/org/bitbiome/shop/Shop.java +++ b/src/main/java/org/bitbiome/shop/Shop.java @@ -61,6 +61,28 @@ public class Shop { return false; } + //Player gold subtract + gold -= costs; + playerConfig.put("gold", gold); + + //Give Player the Item + JSONArray jsonArray = playerConfig.getJSONArray("inventory"); + String newAmount; + for(int i = 0; i < jsonArray.length(); i++) { + JSONObject tempJSON = jsonArray.getJSONObject(i); + if (tempJSON.getString("name").equals(itemName)) { + newAmount = String.valueOf((Integer.parseInt(tempJSON.getString("amount"))) + amount); + jsonArray.remove(i); + tempJSON.put("amount", newAmount); + jsonArray.put(tempJSON); + playerConfig.put("inventory", jsonArray); + FileWriter fileWriter = new FileWriter("src/main/resources/playerconfig.json"); + fileWriter.write(playerConfig.toString()); + fileWriter.close(); + return true; + } + } + }catch (Exception e){ e.printStackTrace(); }