From 8e0a917ba5e84148d9afbdd35c49c8fe29c6a667 Mon Sep 17 00:00:00 2001 From: hannibal2 <24389977+hannibal00212@users.noreply.github.com> Date: Thu, 23 Mar 2023 21:40:47 +0100 Subject: Fixed crashes when first time joining garden with skyhanni --- .../skyhanni/features/garden/GardenAPI.kt | 2 +- .../features/garden/GardenCropMilestoneDisplay.kt | 50 +++++++++++++--------- 2 files changed, 31 insertions(+), 21 deletions(-) (limited to 'src') diff --git a/src/main/java/at/hannibal2/skyhanni/features/garden/GardenAPI.kt b/src/main/java/at/hannibal2/skyhanni/features/garden/GardenAPI.kt index fafdbe700..8c18659d0 100644 --- a/src/main/java/at/hannibal2/skyhanni/features/garden/GardenAPI.kt +++ b/src/main/java/at/hannibal2/skyhanni/features/garden/GardenAPI.kt @@ -66,7 +66,7 @@ class GardenAPI { fun onProfileJoin(event: ProfileJoinEvent) { if (cropsPerSecond.isEmpty()) { for (cropType in CropType.values()) { - cropsPerSecond[cropType] = -1 + cropType.setSpeed(-1) } } } diff --git a/src/main/java/at/hannibal2/skyhanni/features/garden/GardenCropMilestoneDisplay.kt b/src/main/java/at/hannibal2/skyhanni/features/garden/GardenCropMilestoneDisplay.kt index e33c29ffd..69b530bc0 100644 --- a/src/main/java/at/hannibal2/skyhanni/features/garden/GardenCropMilestoneDisplay.kt +++ b/src/main/java/at/hannibal2/skyhanni/features/garden/GardenCropMilestoneDisplay.kt @@ -94,29 +94,39 @@ class GardenCropMilestoneDisplay { @SubscribeEvent fun onOwnInventoryItemUpdate(event: OwnInventorItemUpdateEvent) { - val item = event.itemStack - val counter = GardenAPI.readCounter(item) - if (counter == -1) return - val crop = GardenAPI.getCropTypeFromItem(item) ?: return - if (cultivatingData.containsKey(crop)) { - val old = cultivatingData[crop]!! - val addedCounter = counter - old - // TODO remove try catch - try { + // TODO remove try catch + try { + val item = event.itemStack + val counter = GardenAPI.readCounter(item) + if (counter == -1) return + val crop = GardenAPI.getCropTypeFromItem(item) ?: return + if (cultivatingData.containsKey(crop)) { + val old = cultivatingData[crop]!! + val addedCounter = counter - old + + if (GardenCropMilestones.cropCounter.isEmpty()) { + for (innerCrop in CropType.values()) { + innerCrop.setCounter(0) + } + } + if (GardenAPI.isSpeedDataEmpty()) { + for (cropType in CropType.values()) { + cropType.setSpeed(-1) + } + } + crop.setCounter(crop.getCounter() + addedCounter) - } catch (e: NullPointerException) { - println("crop: '$crop'") - println("GardenCropMilestones.cropCounter: '${GardenCropMilestones.cropCounter.keys}'") - LorenzUtils.debug("NPE at OwnInventorItemUpdateEvent with GardenCropMilestones.cropCounter") - e.printStackTrace() - } - EliteFarmingWeight.addCrop(crop, addedCounter) - if (currentCrop == crop) { - calculateSpeed(addedCounter) - update() + EliteFarmingWeight.addCrop(crop, addedCounter) + if (currentCrop == crop) { + calculateSpeed(addedCounter) + update() + } } + cultivatingData[crop] = counter + } catch (e: Throwable) { + LorenzUtils.error("[SkyHanni] Error in OwnInventorItemUpdateEvent") + e.printStackTrace() } - cultivatingData[crop] = counter } @SubscribeEvent -- cgit