1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
|
package at.hannibal2.skyhanni.features.garden
import at.hannibal2.skyhanni.SkyHanniMod
import at.hannibal2.skyhanni.data.IslandType
import at.hannibal2.skyhanni.data.PetAPI
import at.hannibal2.skyhanni.data.ProfileStorageData
import at.hannibal2.skyhanni.data.jsonobjects.repo.GardenJson
import at.hannibal2.skyhanni.events.BlockClickEvent
import at.hannibal2.skyhanni.events.ConfigLoadEvent
import at.hannibal2.skyhanni.events.CropClickEvent
import at.hannibal2.skyhanni.events.GardenToolChangeEvent
import at.hannibal2.skyhanni.events.GuiContainerEvent
import at.hannibal2.skyhanni.events.LorenzTickEvent
import at.hannibal2.skyhanni.events.LorenzWorldChangeEvent
import at.hannibal2.skyhanni.events.PacketEvent
import at.hannibal2.skyhanni.events.RepositoryReloadEvent
import at.hannibal2.skyhanni.features.garden.CropType.Companion.getCropType
import at.hannibal2.skyhanni.features.garden.composter.ComposterOverlay
import at.hannibal2.skyhanni.features.garden.contest.FarmingContestAPI
import at.hannibal2.skyhanni.features.garden.farming.GardenBestCropTime
import at.hannibal2.skyhanni.features.garden.farming.GardenCropSpeed
import at.hannibal2.skyhanni.features.garden.fortuneguide.FFGuideGUI
import at.hannibal2.skyhanni.features.garden.fortuneguide.FarmingItems
import at.hannibal2.skyhanni.features.garden.inventory.SkyMartCopperPrice
import at.hannibal2.skyhanni.features.garden.visitor.VisitorAPI
import at.hannibal2.skyhanni.utils.BlockUtils.isBabyCrop
import at.hannibal2.skyhanni.utils.ChatUtils
import at.hannibal2.skyhanni.utils.DelayedRun
import at.hannibal2.skyhanni.utils.InventoryUtils
import at.hannibal2.skyhanni.utils.ItemUtils.getInternalName
import at.hannibal2.skyhanni.utils.ItemUtils.getItemRarityOrNull
import at.hannibal2.skyhanni.utils.LocationUtils.isPlayerInside
import at.hannibal2.skyhanni.utils.LorenzRarity
import at.hannibal2.skyhanni.utils.LorenzUtils.isInIsland
import at.hannibal2.skyhanni.utils.LorenzVec
import at.hannibal2.skyhanni.utils.NEUInternalName
import at.hannibal2.skyhanni.utils.RenderUtils.addItemIcon
import at.hannibal2.skyhanni.utils.SkyBlockItemModifierUtils.getCultivatingCounter
import at.hannibal2.skyhanni.utils.SkyBlockItemModifierUtils.getHoeCounter
import net.minecraft.client.Minecraft
import net.minecraft.item.ItemStack
import net.minecraft.network.play.client.C09PacketHeldItemChange
import net.minecraft.util.AxisAlignedBB
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent
import kotlin.time.Duration.Companion.seconds
object GardenAPI {
var toolInHand: String? = null
var itemInHand: ItemStack? = null
var cropInHand: CropType? = null
val mushroomCowPet
get() = PetAPI.isCurrentPet("Mooshroom Cow") &&
storage?.fortune?.farmingItems?.get(FarmingItems.MOOSHROOM_COW)
?.let { it.getItemRarityOrNull()?.isAtLeast(LorenzRarity.RARE) } ?: false
private var inBarn = false
val onBarnPlot get() = inBarn && inGarden()
val storage get() = ProfileStorageData.profileSpecific?.garden
val config get() = SkyHanniMod.feature.garden
var totalAmountVisitorsExisting = 0
var gardenExp: Long?
get() = storage?.experience
set(value) {
value?.let {
storage?.experience = it
}
}
private val barnArea = AxisAlignedBB(35.5, 70.0, -4.5, -32.5, 100.0, -46.5)
// TODO USE SH-REPO
private val otherToolsList = listOf(
"DAEDALUS_AXE",
"BASIC_GARDENING_HOE",
"ADVANCED_GARDENING_AXE",
"BASIC_GARDENING_AXE",
"ADVANCED_GARDENING_HOE",
"ROOKIE_HOE",
"BINGHOE"
)
@SubscribeEvent
fun onSendPacket(event: PacketEvent.SendEvent) {
if (!inGarden()) return
if (event.packet !is C09PacketHeldItemChange) return
checkItemInHand()
}
@SubscribeEvent
fun onCloseWindow(event: GuiContainerEvent.CloseWindowEvent) {
if (!inGarden()) return
checkItemInHand()
}
@SubscribeEvent
fun onTick(event: LorenzTickEvent) {
if (!inGarden()) return
if (event.isMod(10)) {
inBarn = barnArea.isPlayerInside()
// We ignore random hypixel moments
Minecraft.getMinecraft().currentScreen ?: return
checkItemInHand()
}
}
// TODO use IslandChangeEvent
@SubscribeEvent
fun onWorldChange(event: LorenzWorldChangeEvent) {
DelayedRun.runDelayed(2.seconds) {
if (inGarden()) {
checkItemInHand()
}
}
}
private fun updateGardenTool() {
GardenToolChangeEvent(cropInHand, itemInHand).postAndCatch()
}
private fun checkItemInHand() {
val toolItem = InventoryUtils.getItemInHand()
val crop = toolItem?.getCropType()
val newTool = getToolInHand(toolItem, crop)
if (toolInHand != newTool) {
toolInHand = newTool
cropInHand = crop
itemInHand = toolItem
updateGardenTool()
}
}
private fun getToolInHand(toolItem: ItemStack?, crop: CropType?): String? {
if (crop != null) return crop.cropName
val internalName = toolItem?.getInternalName() ?: return null
return if (isOtherTool(internalName)) internalName.asString() else null
}
private fun isOtherTool(internalName: NEUInternalName): Boolean {
return internalName.asString() in otherToolsList
}
fun inGarden() = IslandType.GARDEN.isInIsland()
fun ItemStack.getCropType(): CropType? {
val internalName = getInternalName()
return CropType.entries.firstOrNull { internalName.startsWith(it.toolName) }
}
fun readCounter(itemStack: ItemStack): Long = itemStack.getHoeCounter() ?: itemStack.getCultivatingCounter() ?: -1L
fun MutableList<Any>.addCropIcon(crop: CropType, highlight: Boolean = false) =
addItemIcon(crop.icon.copy(), highlight)
fun hideExtraGuis() = ComposterOverlay.inInventory || AnitaMedalProfit.inInventory ||
SkyMartCopperPrice.inInventory || FarmingContestAPI.inInventory || VisitorAPI.inInventory || FFGuideGUI.isInGui()
fun clearCropSpeed() {
storage?.cropsPerSecond?.clear()
GardenBestCropTime.reset()
updateGardenTool()
ChatUtils.chat("Manually reset all crop speed data!")
}
@SubscribeEvent
fun onConfigLoad(event: ConfigLoadEvent) {
GardenBestCropTime.reset()
}
fun getCurrentlyFarmedCrop(): CropType? {
val brokenCrop = if (toolInHand != null) GardenCropSpeed.lastBrokenCrop else null
return cropInHand ?: brokenCrop
}
private var lastLocation: LorenzVec? = null
@SubscribeEvent
fun onBlockBreak(event: BlockClickEvent) {
if (!inGarden()) return
val blockState = event.getBlockState
val cropBroken = blockState.getCropType() ?: return
if (cropBroken.multiplier == 1 && blockState.isBabyCrop()) return
val position = event.position
if (lastLocation == position) {
return
}
lastLocation = position
CropClickEvent(cropBroken, blockState, event.clickType, event.itemInHand).postAndCatch()
}
fun getExpForLevel(requestedLevel: Int): Long {
var totalExp = 0L
var tier = 0
for (tierExp in gardenExperience) {
totalExp += tierExp
tier++
if (tier == requestedLevel) {
return totalExp
}
}
while (tier < requestedLevel) {
totalExp += gardenOverflowExp
tier++
if (tier == requestedLevel) {
return totalExp
}
}
return 0
}
fun getGardenLevel(): Int {
val gardenExp = this.gardenExp ?: return 0
var tier = 0
var totalExp = 0L
for (tierExp in gardenExperience) {
totalExp += tierExp
if (totalExp > gardenExp) {
return tier
}
tier++
}
totalExp += gardenOverflowExp
while (totalExp < gardenExp) {
tier++
totalExp += gardenOverflowExp
}
return tier
}
@SubscribeEvent
fun onRepoReload(event: RepositoryReloadEvent) {
val data = event.getConstant<GardenJson>("Garden")
gardenExperience = data.garden_exp
totalAmountVisitorsExisting = data.visitors.size
}
private var gardenExperience = listOf<Int>()
private const val gardenOverflowExp = 10000
}
|