Skip to content

Commit

Permalink
feat: SlimeHUD integration
Browse files Browse the repository at this point in the history
  • Loading branch information
ybw0014 committed Oct 28, 2024
1 parent e295464 commit 6b4b0fa
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class InfinityExpansion2 : AbstractAddon(

logger.info("Loading libraries, please wait...")
logger.info("If you stuck here for a long time, try to specify a mirror repository.")
logger.info("More info at: https://docs.ybw0014.dev/infinity-expansion-2/installing")
logger.info("Add -DcentralRepository=<url> to the JVM arguments.")

// download libs
val manager = BukkitLibraryManager(this, "libraries")
Expand Down Expand Up @@ -117,7 +117,11 @@ class InfinityExpansion2 : AbstractAddon(
} else if (pluginVersion.startsWith("Build")) {
try {
// use updater in lib plugin
val clazz = Class.forName("net.guizhanss.guizhanlibplugin.updater.GuizhanUpdater")
val pluginPackage = charArrayOf(
'n', 'e', 't', '.', 'g', 'u', 'i', 'z', 'h', 'a', 'n', 's', 's', '.',
'g', 'u', 'i', 'z', 'h', 'a', 'n', 'l', 'i', 'b', 'p', 'l', 'u', 'g', 'i', 'n'
)
val clazz = Class.forName(String(pluginPackage) + ".updater.GuizhanUpdater")
val updaterStart = clazz.getDeclaredMethod(
"start",
Plugin::class.java,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import io.github.thebusybiscuit.slimefun4.api.items.SlimefunItem
import io.github.thebusybiscuit.slimefun4.libraries.dough.items.ItemUtils
import net.guizhanss.infinityexpansion2.InfinityExpansion2
import net.guizhanss.infinityexpansion2.implementation.listeners.TranslationsLoadListener
import net.guizhanss.infinityexpansion2.integration.SlimeHUDIntegration
import net.guizhanss.infinityexpansion2.utils.items.MaterialType
import net.guizhanss.infinityexpansion2.utils.items.removePrefix
import net.guizhanss.slimefuntranslation.api.SlimefunTranslationAPI
Expand All @@ -23,6 +24,14 @@ class IntegrationService(private val plugin: InfinityExpansion2) {

var slimefunTranslationEnabled = isPluginEnabled("SlimefunTranslation")
private set
var slimeHudEnabled = isPluginEnabled("SlimeHUD")
private set

init {
if (slimeHudEnabled) {
SlimeHUDIntegration
}
}

private fun isPluginEnabled(pluginName: String) = plugin.server.pluginManager.isPluginEnabled(pluginName)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
package net.guizhanss.infinityexpansion2.implementation.items.mobsim

import io.github.schntgaispock.slimehud.util.HudBuilder
import io.github.schntgaispock.slimehud.waila.HudRequest
import io.github.thebusybiscuit.slimefun4.api.items.ItemGroup
import io.github.thebusybiscuit.slimefun4.api.items.SlimefunItemStack
import io.github.thebusybiscuit.slimefun4.api.recipes.RecipeType
import io.github.thebusybiscuit.slimefun4.libraries.dough.inventory.InvUtils
import io.github.thebusybiscuit.slimefun4.utils.ChestMenuUtils
import me.mrCookieSlime.Slimefun.api.inventory.BlockMenu
import net.guizhanss.infinityexpansion2.InfinityExpansion2
import net.guizhanss.infinityexpansion2.api.mobsim.MobDataCardProps
import net.guizhanss.infinityexpansion2.core.IERegistry
import net.guizhanss.infinityexpansion2.core.items.attributes.EnergyTickingConsumer
import net.guizhanss.infinityexpansion2.core.items.attributes.InformationalRecipeDisplayItem
import net.guizhanss.infinityexpansion2.core.menu.MenuLayout
import net.guizhanss.infinityexpansion2.implementation.items.machines.abstracts.AbstractTickingMachine
import net.guizhanss.infinityexpansion2.utils.bukkitext.isAir
import net.guizhanss.infinityexpansion2.utils.getBlockMenu
import net.guizhanss.infinityexpansion2.utils.getInt
import net.guizhanss.infinityexpansion2.utils.items.GuiItems
import net.guizhanss.infinityexpansion2.utils.items.isSlimefunItem
Expand Down Expand Up @@ -70,27 +74,14 @@ class MobSimulationChamber(

override fun process(b: Block, menu: BlockMenu): Boolean {
val l = b.location
val input = menu.getItemInSlot(layout.inputSlots[0])
if (input.isAir || !input.isSlimefunItem<MobDataCard>()) {
menu.setStatus { GuiItems.INVALID_INPUT }
menu.setEnergyConsumption(0)
return false
}

// check if input is a registered card
val id = MobDataCard.getMobDataId(input) ?: run {
menu.setStatus { GuiItems.INVALID_INPUT }
menu.setEnergyConsumption(0)
return false
}
val props = IERegistry.mobDataCards[id] ?: run {
val (props, cardAmount) = menu.getDataCard(layout) ?: run {
menu.setStatus { GuiItems.INVALID_INPUT }
menu.setEnergyConsumption(0)
return false
}

// handle stackable
val amount = if (InfinityExpansion2.configService.mobSimAllowStackedCard) input.amount else 1
val amount = if (InfinityExpansion2.configService.mobSimAllowStackedCard) cardAmount else 1
val energy = getEnergyConsumptionPerTick() + props.energy

if (getCharge(menu.location) < energy) {
Expand Down Expand Up @@ -165,5 +156,31 @@ class MobSimulationChamber(
private const val ENERGY_CONSUMPTION_SLOT = 5
private const val XP_SLOT = 8
private const val XP_KEY = "xp"

/**
* Get the data card from the menu input slot (the layout must be [MenuLayout.SINGLE_INPUT]).
* If the input is invalid, return null.
* Returns the [MobDataCardProps] and the amount of the card.
*/
private fun BlockMenu.getDataCard(layout: MenuLayout): Pair<MobDataCardProps, Int>? {
val input = getItemInSlot(layout.inputSlots[0])
if (input.isAir || !input.isSlimefunItem<MobDataCard>()) {
return null
}

// check if input is a registered card
val id = MobDataCard.getMobDataId(input) ?: return null
val props = IERegistry.mobDataCards[id] ?: return null

return props to input.amount
}

fun getHudResponse(request: HudRequest): String {
val menu = request.location.getBlockMenu()
val chamber = request.slimefunItem as MobSimulationChamber
val energyHud = " | " + HudBuilder.formatEnergyStored(chamber.getCharge(request.location), chamber.capacity)
val (props, _) = menu.getDataCard(chamber.layout) ?: return "Empty$energyHud"
return props.name + energyHud
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package net.guizhanss.infinityexpansion2.integration

import io.github.schntgaispock.slimehud.SlimeHUD
import net.guizhanss.infinityexpansion2.implementation.items.mobsim.MobSimulationChamber

object SlimeHUDIntegration {
init {
SlimeHUD.getHudController()
.registerCustomHandler(MobSimulationChamber::class.java, MobSimulationChamber::getHudResponse)
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package net.guizhanss.infinityexpansion2.utils

import me.mrCookieSlime.Slimefun.api.BlockStorage
import me.mrCookieSlime.Slimefun.api.inventory.BlockMenu
import org.bukkit.Location
import org.bukkit.block.Block

Expand All @@ -27,3 +28,7 @@ fun Location.getInt(key: String, defaultValue: Int = 0) = getString(key)?.toIntO
fun Block.getInt(key: String, defaultValue: Int = 0) = location.getInt(key, defaultValue)
fun Location.setInt(key: String, value: Int) = setString(key, value.toString())
fun Block.setInt(key: String, value: Int) = location.setInt(key, value)

// inventory
fun Location.getBlockMenu(): BlockMenu = BlockStorage.getInventory(this)
fun Block.getBlockMenu() = location.getBlockMenu()

0 comments on commit 6b4b0fa

Please sign in to comment.