aboutsummaryrefslogtreecommitdiff
path: root/src/compat
diff options
context:
space:
mode:
Diffstat (limited to 'src/compat')
-rw-r--r--src/compat/jade/java/moe/nea/firmament/compat/jade/CustomFakeBlockProvider.kt41
-rw-r--r--src/compat/jade/java/moe/nea/firmament/compat/jade/CustomMiningHardnessProvider.kt97
-rw-r--r--src/compat/jade/java/moe/nea/firmament/compat/jade/DrillToolProvider.kt78
-rw-r--r--src/compat/jade/java/moe/nea/firmament/compat/jade/FirmamentJadePlugin.kt21
-rw-r--r--src/compat/jade/java/moe/nea/firmament/compat/jade/JadeIntegration.kt50
-rw-r--r--src/compat/jade/java/moe/nea/firmament/compat/jade/utils.kt6
-rw-r--r--src/compat/jade/java/moe/nea/firmament/mixins/compat/jade/ElementAccessor.java12
-rw-r--r--src/compat/jade/java/moe/nea/firmament/mixins/compat/jade/EnforceToolDisplayForCustomBlocksInHarvestToolProvider.java33
-rw-r--r--src/compat/jade/java/moe/nea/firmament/mixins/compat/jade/OnUpdateBreakProgress.java22
-rw-r--r--src/compat/jade/java/moe/nea/firmament/mixins/compat/jade/PatchBreakingBarSpeedJade.java25
10 files changed, 385 insertions, 0 deletions
diff --git a/src/compat/jade/java/moe/nea/firmament/compat/jade/CustomFakeBlockProvider.kt b/src/compat/jade/java/moe/nea/firmament/compat/jade/CustomFakeBlockProvider.kt
new file mode 100644
index 0000000..53e3255
--- /dev/null
+++ b/src/compat/jade/java/moe/nea/firmament/compat/jade/CustomFakeBlockProvider.kt
@@ -0,0 +1,41 @@
+package moe.nea.firmament.compat.jade
+
+import snownee.jade.api.Accessor
+import snownee.jade.api.BlockAccessor
+import snownee.jade.api.IWailaClientRegistration
+import snownee.jade.api.callback.JadeRayTraceCallback
+import net.minecraft.util.hit.HitResult
+import moe.nea.firmament.repo.MiningRepoData
+import moe.nea.firmament.util.mc.FirmamentDataComponentTypes
+
+class CustomFakeBlockProvider(val registration: IWailaClientRegistration) : JadeRayTraceCallback {
+
+ override fun onRayTrace(
+ hitResult: HitResult,
+ accessor: Accessor<*>?,
+ originalAccessor: Accessor<*>?
+ ): Accessor<*>? {
+ if (!JadeIntegration.TConfig.blockDetection) return accessor
+ if (accessor !is BlockAccessor) return accessor
+ val customBlock = JadeIntegration.customBlocks[accessor.block]
+ if (customBlock == null) return accessor
+ return registration.blockAccessor()
+ .from(accessor)
+ .fakeBlock(customBlock.getDisplayItem(accessor.block))
+ .build()
+ }
+
+ companion object {
+ @JvmStatic
+ fun hasCustomBlock(accessor: BlockAccessor): Boolean {
+ return getCustomBlock(accessor) != null
+ }
+
+ @JvmStatic
+ fun getCustomBlock(accessor: BlockAccessor): MiningRepoData.CustomMiningBlock? {
+ if (!accessor.isFakeBlock) return null
+ val item = accessor.fakeBlock
+ return item.get(FirmamentDataComponentTypes.CUSTOM_MINING_BLOCK_DATA)
+ }
+ }
+}
diff --git a/src/compat/jade/java/moe/nea/firmament/compat/jade/CustomMiningHardnessProvider.kt b/src/compat/jade/java/moe/nea/firmament/compat/jade/CustomMiningHardnessProvider.kt
new file mode 100644
index 0000000..29fecd2
--- /dev/null
+++ b/src/compat/jade/java/moe/nea/firmament/compat/jade/CustomMiningHardnessProvider.kt
@@ -0,0 +1,97 @@
+package moe.nea.firmament.compat.jade
+
+import snownee.jade.api.BlockAccessor
+import snownee.jade.api.IBlockComponentProvider
+import snownee.jade.api.ITooltip
+import snownee.jade.api.config.IPluginConfig
+import kotlin.time.DurationUnit
+import net.minecraft.block.BlockState
+import net.minecraft.util.Identifier
+import net.minecraft.util.math.BlockPos
+import moe.nea.firmament.Firmament
+import moe.nea.firmament.annotations.Subscribe
+import moe.nea.firmament.events.TickEvent
+import moe.nea.firmament.util.MC
+import moe.nea.firmament.util.TimeMark
+import moe.nea.firmament.util.tr
+
+object CustomMiningHardnessProvider : IBlockComponentProvider {
+
+ override fun appendTooltip(
+ tooltip: ITooltip,
+ block: BlockAccessor,
+ config: IPluginConfig?
+ ) {
+ val customBlock = CustomFakeBlockProvider.getCustomBlock(block) ?: return
+ if (customBlock.breakingPower > 0)
+ tooltip.add(tr("firmament.jade.breaking_power", "Required Breaking Power: ${customBlock.breakingPower}"))
+ }
+
+ override fun getUid(): Identifier =
+ Firmament.identifier("custom_mining_hardness")
+
+ data class BreakingInfo(
+ val blockPos: BlockPos, val stage: Int,
+ val state: BlockState?,
+ val ts: TimeMark = TimeMark.now()
+ )
+
+ var previousBreakingInfo: BreakingInfo? = null
+ var currentBreakingInfo: BreakingInfo? = null
+
+ @Subscribe
+ fun clearInfoOnStopBreaking(event: TickEvent) {
+ val isBreakingBlock = MC.interactionManager?.isBreakingBlock ?: false
+ if (!isBreakingBlock) {
+ previousBreakingInfo = null
+ currentBreakingInfo = null
+ }
+ }
+
+ @JvmStatic
+ fun setBreakingInfo(blockPos: BlockPos, stage: Int) {
+ previousBreakingInfo = currentBreakingInfo
+ val state = MC.world?.getBlockState(blockPos)
+ if (previousBreakingInfo?.let { it.state != state || it.blockPos != blockPos } ?: false)
+ previousBreakingInfo == null
+ currentBreakingInfo = BreakingInfo(blockPos.toImmutable(), stage, state)
+ // For some reason hypixel initially sends a stage 10 packet, and then fixes it up with a stage 0 packet.
+ // Ignore the stage 10 packet if we dont have any previous packets for this block.
+ // This could in theory still have issues if someone perfectly stops breaking a block the tick it finishes and then does not break another block until it respawns, but i deem that to be too much of an edge case.
+ if (stage == 10 && previousBreakingInfo == null) {
+ previousBreakingInfo = null
+ currentBreakingInfo = null
+ }
+ }
+
+ @JvmStatic
+ fun replaceBreakProgress(original: Float): Float {
+ if (!JadeIntegration.TConfig.miningProgress) return original
+ if (!isOnMiningIsland()) return original
+ val pos = MC.interactionManager?.currentBreakingPos ?: return original
+ val info = currentBreakingInfo
+ if (info?.blockPos != pos || info.state != MC.world?.getBlockState(pos)) {
+ currentBreakingInfo = null
+ previousBreakingInfo = null
+ return original
+ }
+ // TODO: improve this interpolation to work across all stages, to alleviate some of the jittery bar.
+ // Maybe introduce a proper mining API that tracks the actual progress with some sort of FSM
+ val interpolatedStage = previousBreakingInfo?.let { prev ->
+ val timeBetweenTicks = (info.ts - prev.ts).toDouble(DurationUnit.SECONDS)
+ val stagesPerUpdate = (info.stage - prev.stage).toDouble()
+ if (stagesPerUpdate < 1) return@let null
+ val stagesPerSecond = stagesPerUpdate / timeBetweenTicks
+ info.stage + (info.ts.passedTime().toDouble(DurationUnit.SECONDS) * stagesPerSecond)
+ .coerceAtMost(stagesPerUpdate)
+ }?.toFloat()
+ val stage = interpolatedStage ?: info.stage.toFloat()
+ return stage / 10F
+ }
+
+ @JvmStatic
+ fun replaceBlockBreakSpeed(original: Float): Float {
+ if (isOnMiningIsland()) return 0F
+ return original
+ }
+}
diff --git a/src/compat/jade/java/moe/nea/firmament/compat/jade/DrillToolProvider.kt b/src/compat/jade/java/moe/nea/firmament/compat/jade/DrillToolProvider.kt
new file mode 100644
index 0000000..ab45e7c
--- /dev/null
+++ b/src/compat/jade/java/moe/nea/firmament/compat/jade/DrillToolProvider.kt
@@ -0,0 +1,78 @@
+package moe.nea.firmament.compat.jade
+
+import java.util.Optional
+import java.util.function.UnaryOperator
+import snownee.jade.api.BlockAccessor
+import snownee.jade.api.IBlockComponentProvider
+import snownee.jade.api.ITooltip
+import snownee.jade.api.JadeIds
+import snownee.jade.api.config.IPluginConfig
+import snownee.jade.api.theme.IThemeHelper
+import snownee.jade.api.ui.IElement
+import snownee.jade.api.ui.IElementHelper
+import snownee.jade.impl.ui.ItemStackElement
+import snownee.jade.impl.ui.TextElement
+import kotlin.jvm.optionals.getOrDefault
+import net.minecraft.item.ItemStack
+import net.minecraft.item.Items
+import net.minecraft.text.Text
+import net.minecraft.util.Identifier
+import net.minecraft.util.math.Vec2f
+import moe.nea.firmament.Firmament
+import moe.nea.firmament.repo.ItemCache.asItemStack
+import moe.nea.firmament.repo.RepoManager
+import moe.nea.firmament.repo.SBItemStack
+import moe.nea.firmament.util.MC
+
+class DrillToolProvider : IBlockComponentProvider {
+ override fun appendTooltip(
+ tooltip: ITooltip,
+ accessor: BlockAccessor,
+ p2: IPluginConfig?
+ ) {
+ val customBlock = CustomFakeBlockProvider.getCustomBlock(accessor) ?: return
+ val tool = RepoManager.miningData.getToolsThatCanBreak(customBlock.breakingPower).firstOrNull()
+ ?.asImmutableItemStack() ?: return
+ tooltip.replace(JadeIds.MC_HARVEST_TOOL, UnaryOperator { elements ->
+ elements.map { inner ->
+ val lastItemIndex = inner.indexOfLast { it is ItemStackElement }
+ if (lastItemIndex < 0) return@map inner
+ val innerMut = inner.toMutableList()
+ val harvestIndicator = innerMut.indexOfLast {
+ it is TextElement && it.cachedSize == Vec2f.ZERO && it.text.visit {
+ if (it.isEmpty()) Optional.empty() else Optional.of(true)
+ }.getOrDefault(false)
+ }
+ val canHarvest = SBItemStack(MC.stackInHand).breakingPower >= customBlock.breakingPower
+ val lastItem = innerMut[lastItemIndex] as ItemStackElement
+ if (harvestIndicator < 0) {
+ innerMut.add(lastItemIndex + 1, canHarvestIndicator(canHarvest, lastItem.alignment))
+ } else {
+ innerMut.set(harvestIndicator, canHarvestIndicator(canHarvest, lastItem.alignment))
+ }
+ innerMut.set(lastItemIndex, IElementHelper.get()
+ .item(tool, 0.75f)
+ .translate(lastItem.translation)
+ .size(lastItem.size)
+ .message(null)
+ .align(lastItem.alignment))
+ innerMut.subList(0, lastItemIndex - 1).removeIf { it is ItemStackElement }
+ innerMut
+ }
+ })
+ }
+
+ fun canHarvestIndicator(canHarvest: Boolean, align: IElement.Align): IElement {
+ val t = IThemeHelper.get()
+ val text = if (canHarvest) t.success(CHECK) else t.danger(X)
+ return IElementHelper.get().text(text)
+ .scale(0.75F).zOffset(800).size(Vec2f.ZERO).translate(Vec2f(-3F, 3.25F)).align(align)
+ }
+
+ private val CHECK: Text = Text.literal("✔")
+ private val X: Text = Text.literal("✕")
+
+ override fun getUid(): Identifier {
+ return Firmament.identifier("toolprovider")
+ }
+}
diff --git a/src/compat/jade/java/moe/nea/firmament/compat/jade/FirmamentJadePlugin.kt b/src/compat/jade/java/moe/nea/firmament/compat/jade/FirmamentJadePlugin.kt
new file mode 100644
index 0000000..51e2453
--- /dev/null
+++ b/src/compat/jade/java/moe/nea/firmament/compat/jade/FirmamentJadePlugin.kt
@@ -0,0 +1,21 @@
+package moe.nea.firmament.compat.jade
+
+import snownee.jade.api.IWailaClientRegistration
+import snownee.jade.api.IWailaCommonRegistration
+import snownee.jade.api.IWailaPlugin
+import snownee.jade.api.WailaPlugin
+import net.minecraft.block.Block
+import moe.nea.firmament.Firmament
+
+@WailaPlugin
+class FirmamentJadePlugin : IWailaPlugin {
+ override fun register(registration: IWailaCommonRegistration) {
+ Firmament.logger.debug("Registering Jade integration...")
+ }
+
+ override fun registerClient(registration: IWailaClientRegistration) {
+ registration.registerBlockComponent(CustomMiningHardnessProvider, Block::class.java)
+ registration.registerBlockComponent(DrillToolProvider(), Block::class.java)
+ registration.addRayTraceCallback(CustomFakeBlockProvider(registration))
+ }
+}
diff --git a/src/compat/jade/java/moe/nea/firmament/compat/jade/JadeIntegration.kt b/src/compat/jade/java/moe/nea/firmament/compat/jade/JadeIntegration.kt
new file mode 100644
index 0000000..d411c26
--- /dev/null
+++ b/src/compat/jade/java/moe/nea/firmament/compat/jade/JadeIntegration.kt
@@ -0,0 +1,50 @@
+package moe.nea.firmament.compat.jade
+
+import moe.nea.firmament.annotations.Subscribe
+import moe.nea.firmament.events.SkyblockServerUpdateEvent
+import moe.nea.firmament.repo.MiningRepoData
+import moe.nea.firmament.repo.RepoManager
+import moe.nea.firmament.util.ErrorUtil
+import net.minecraft.block.Block
+import moe.nea.firmament.events.ReloadRegistrationEvent
+import moe.nea.firmament.gui.config.ManagedConfig
+
+object JadeIntegration {
+ object TConfig : ManagedConfig("jade-integration", Category.INTEGRATIONS) {
+ val miningProgress by toggle("progress") { true }
+ val blockDetection by toggle("blocks") { true }
+ }
+
+ var customBlocks: Map<Block, MiningRepoData.CustomMiningBlock> = mapOf()
+
+ fun refreshBlockInfo() {
+ if (!isOnMiningIsland()) {
+ customBlocks = mapOf()
+ return
+ }
+ val blocks = RepoManager.miningData.customMiningBlocks
+ .flatMap { customBlock ->
+ // TODO: add a lifted helper method for this
+ customBlock.blocks189.filter { it.isCurrentlyActive }
+ .mapNotNull { it.block }
+ .map { customBlock to it }
+ }
+ .groupBy { it.second }
+ customBlocks = blocks.mapNotNull { (block, customBlocks) ->
+ val singleMatch =
+ ErrorUtil.notNullOr(customBlocks.singleOrNull()?.first,
+ "Two custom blocks both want to supply custom mining behaviour for $block.") { return@mapNotNull null }
+ block to singleMatch
+ }.toMap()
+ }
+
+ @Subscribe
+ fun onRepoReload(event: ReloadRegistrationEvent) {
+ event.repo.registerReloadListener { refreshBlockInfo() }
+ }
+
+ @Subscribe
+ fun onWorldSwap(event: SkyblockServerUpdateEvent) {
+ refreshBlockInfo()
+ }
+}
diff --git a/src/compat/jade/java/moe/nea/firmament/compat/jade/utils.kt b/src/compat/jade/java/moe/nea/firmament/compat/jade/utils.kt
new file mode 100644
index 0000000..364dc02
--- /dev/null
+++ b/src/compat/jade/java/moe/nea/firmament/compat/jade/utils.kt
@@ -0,0 +1,6 @@
+package moe.nea.firmament.compat.jade
+
+import moe.nea.firmament.util.SBData
+
+fun isOnMiningIsland(): Boolean =
+ SBData.skyblockLocation?.hasCustomMining ?: false
diff --git a/src/compat/jade/java/moe/nea/firmament/mixins/compat/jade/ElementAccessor.java b/src/compat/jade/java/moe/nea/firmament/mixins/compat/jade/ElementAccessor.java
new file mode 100644
index 0000000..1b58e3c
--- /dev/null
+++ b/src/compat/jade/java/moe/nea/firmament/mixins/compat/jade/ElementAccessor.java
@@ -0,0 +1,12 @@
+package moe.nea.firmament.mixins.compat.jade;
+
+import org.spongepowered.asm.mixin.Mixin;
+import org.spongepowered.asm.mixin.gen.Accessor;
+import snownee.jade.api.ui.Element;
+import snownee.jade.api.ui.IElement;
+
+@Mixin(Element.class)
+public interface ElementAccessor {
+ @Accessor("align")
+ IElement.Align getAlign_firmament();
+}
diff --git a/src/compat/jade/java/moe/nea/firmament/mixins/compat/jade/EnforceToolDisplayForCustomBlocksInHarvestToolProvider.java b/src/compat/jade/java/moe/nea/firmament/mixins/compat/jade/EnforceToolDisplayForCustomBlocksInHarvestToolProvider.java
new file mode 100644
index 0000000..3677d01
--- /dev/null
+++ b/src/compat/jade/java/moe/nea/firmament/mixins/compat/jade/EnforceToolDisplayForCustomBlocksInHarvestToolProvider.java
@@ -0,0 +1,33 @@
+package moe.nea.firmament.mixins.compat.jade;
+
+import com.llamalad7.mixinextras.injector.ModifyExpressionValue;
+import com.llamalad7.mixinextras.sugar.Local;
+import moe.nea.firmament.compat.jade.CustomFakeBlockProvider;
+import net.minecraft.block.Blocks;
+import net.minecraft.item.ItemStack;
+import org.spongepowered.asm.mixin.Mixin;
+import org.spongepowered.asm.mixin.injection.At;
+import snownee.jade.addon.harvest.HarvestToolProvider;
+import snownee.jade.api.BlockAccessor;
+
+import java.util.List;
+
+@Mixin(HarvestToolProvider.class)
+public class EnforceToolDisplayForCustomBlocksInHarvestToolProvider {
+ @ModifyExpressionValue(method = "getText", at = @At(value = "INVOKE", target = "Lnet/minecraft/block/BlockState;isToolRequired()Z"))
+ private boolean overwriteRequiresTool(boolean original, @Local(argsOnly = true) BlockAccessor accessor) {
+ if (CustomFakeBlockProvider.hasCustomBlock(accessor))
+ return true;
+ return original;
+ }
+
+ private static final List<ItemStack> REPLACEABLE_TOOL = List.of(new ItemStack(Blocks.ENCHANTING_TABLE));
+
+ @ModifyExpressionValue(method = "getText", at = @At(value = "INVOKE", target = "Lcom/google/common/cache/Cache;get(Ljava/lang/Object;Ljava/util/concurrent/Callable;)Ljava/lang/Object;"))
+ private Object overwriteAvailableTools(Object original, @Local(argsOnly = true) BlockAccessor accessor) {
+ var orig = (List<ItemStack>) original;
+ if (orig.isEmpty() && CustomFakeBlockProvider.hasCustomBlock(accessor))
+ return REPLACEABLE_TOOL;
+ return orig;
+ }
+}
diff --git a/src/compat/jade/java/moe/nea/firmament/mixins/compat/jade/OnUpdateBreakProgress.java b/src/compat/jade/java/moe/nea/firmament/mixins/compat/jade/OnUpdateBreakProgress.java
new file mode 100644
index 0000000..7d71ae8
--- /dev/null
+++ b/src/compat/jade/java/moe/nea/firmament/mixins/compat/jade/OnUpdateBreakProgress.java
@@ -0,0 +1,22 @@
+package moe.nea.firmament.mixins.compat.jade;
+
+import moe.nea.firmament.compat.jade.CustomMiningHardnessProvider;
+import moe.nea.firmament.util.MC;
+import net.minecraft.client.render.WorldRenderer;
+import net.minecraft.util.math.BlockPos;
+import org.spongepowered.asm.mixin.Mixin;
+import org.spongepowered.asm.mixin.injection.At;
+import org.spongepowered.asm.mixin.injection.Inject;
+import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
+
+import java.util.Objects;
+
+@Mixin(WorldRenderer.class)
+public class OnUpdateBreakProgress {
+ @Inject(method = "setBlockBreakingInfo", at = @At("HEAD"))
+ private void replaceBreakProgress(int entityId, BlockPos pos, int stage, CallbackInfo ci) {
+ if (entityId == 0 && null != MC.INSTANCE.getInteractionManager() && Objects.equals(MC.INSTANCE.getInteractionManager().currentBreakingPos, pos)) {
+ CustomMiningHardnessProvider.setBreakingInfo(pos, stage);
+ }
+ }
+}
diff --git a/src/compat/jade/java/moe/nea/firmament/mixins/compat/jade/PatchBreakingBarSpeedJade.java b/src/compat/jade/java/moe/nea/firmament/mixins/compat/jade/PatchBreakingBarSpeedJade.java
new file mode 100644
index 0000000..203f7e4
--- /dev/null
+++ b/src/compat/jade/java/moe/nea/firmament/mixins/compat/jade/PatchBreakingBarSpeedJade.java
@@ -0,0 +1,25 @@
+package moe.nea.firmament.mixins.compat.jade;
+
+import com.llamalad7.mixinextras.injector.ModifyExpressionValue;
+import moe.nea.firmament.compat.jade.CustomMiningHardnessProvider;
+import org.objectweb.asm.Opcodes;
+import org.spongepowered.asm.mixin.Mixin;
+import org.spongepowered.asm.mixin.injection.At;
+import snownee.jade.JadeClient;
+
+@Mixin(JadeClient.class)
+public class PatchBreakingBarSpeedJade {
+ @ModifyExpressionValue(
+ method = "drawBreakingProgress",
+ at = @At(value = "FIELD", target = "Lnet/minecraft/client/network/ClientPlayerInteractionManager;currentBreakingProgress:F", opcode = Opcodes.GETFIELD)
+ )
+ private static float replaceBlockBreakingProgress(float original) {
+ return CustomMiningHardnessProvider.replaceBreakProgress(original);
+ }
+
+ @ModifyExpressionValue(method = "drawBreakingProgress",
+ at = @At(value = "INVOKE", target = "Lnet/minecraft/block/BlockState;calcBlockBreakingDelta(Lnet/minecraft/entity/player/PlayerEntity;Lnet/minecraft/world/BlockView;Lnet/minecraft/util/math/BlockPos;)F"))
+ private static float replacePlayerSpecificBreakingProgress(float original) {
+ return CustomMiningHardnessProvider.replaceBlockBreakSpeed(original);
+ }
+}