-
-
Notifications
You must be signed in to change notification settings - Fork 84
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #332 from Grayray75/item-cooldown
Item cooldown display
- Loading branch information
Showing
15 changed files
with
250 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
src/main/java/me/xmrvizzy/skyblocker/events/ClientPlayerBlockBreakEvent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package me.xmrvizzy.skyblocker.events; | ||
|
||
import net.fabricmc.fabric.api.event.Event; | ||
import net.fabricmc.fabric.api.event.EventFactory; | ||
import net.minecraft.block.BlockState; | ||
import net.minecraft.entity.player.PlayerEntity; | ||
import net.minecraft.util.math.BlockPos; | ||
import net.minecraft.world.World; | ||
|
||
// Fabric API currently doesn't have an event for this | ||
public class ClientPlayerBlockBreakEvent { | ||
public static final Event<AfterBlockBreak> AFTER = EventFactory.createArrayBacked(AfterBlockBreak.class, | ||
(listeners) -> (world, player, pos, state) -> { | ||
for (AfterBlockBreak listener : listeners) { | ||
listener.afterBlockBreak(world, player, pos, state); | ||
} | ||
}); | ||
|
||
@FunctionalInterface | ||
public interface AfterBlockBreak { | ||
void afterBlockBreak(World world, PlayerEntity player, BlockPos pos, BlockState state); | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
...izzy/skyblocker/utils/SkyblockEvents.java → ...zzy/skyblocker/events/SkyblockEvents.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
src/main/java/me/xmrvizzy/skyblocker/mixin/ClientPlayerInteractionManagerMixin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package me.xmrvizzy.skyblocker.mixin; | ||
|
||
import me.xmrvizzy.skyblocker.events.ClientPlayerBlockBreakEvent; | ||
import net.minecraft.block.BlockState; | ||
import net.minecraft.client.MinecraftClient; | ||
import net.minecraft.client.network.ClientPlayerInteractionManager; | ||
import net.minecraft.util.math.BlockPos; | ||
import net.minecraft.world.World; | ||
import org.spongepowered.asm.mixin.Final; | ||
import org.spongepowered.asm.mixin.Mixin; | ||
import org.spongepowered.asm.mixin.Shadow; | ||
import org.spongepowered.asm.mixin.injection.At; | ||
import org.spongepowered.asm.mixin.injection.Inject; | ||
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; | ||
import org.spongepowered.asm.mixin.injection.callback.LocalCapture; | ||
|
||
@Mixin(ClientPlayerInteractionManager.class) | ||
public class ClientPlayerInteractionManagerMixin { | ||
@Shadow | ||
@Final | ||
private MinecraftClient client; | ||
|
||
@Inject(method = "breakBlock", at = @At(value = "INVOKE", target = "Lnet/minecraft/block/Block;onBroken(Lnet/minecraft/world/WorldAccess;Lnet/minecraft/util/math/BlockPos;Lnet/minecraft/block/BlockState;)V"), locals = LocalCapture.CAPTURE_FAILHARD) | ||
private void skyblocker$onBlockBroken(BlockPos pos, CallbackInfoReturnable<Boolean> cir, World world, BlockState blockState) { | ||
ClientPlayerBlockBreakEvent.AFTER.invoker().afterBlockBreak(world, this.client.player, pos, blockState); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
115 changes: 115 additions & 0 deletions
115
src/main/java/me/xmrvizzy/skyblocker/skyblock/item/ItemCooldowns.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
package me.xmrvizzy.skyblocker.skyblock.item; | ||
|
||
import com.google.common.collect.ImmutableList; | ||
import me.xmrvizzy.skyblocker.config.SkyblockerConfigManager; | ||
import me.xmrvizzy.skyblocker.events.ClientPlayerBlockBreakEvent; | ||
import me.xmrvizzy.skyblocker.utils.ItemUtils; | ||
import net.fabricmc.fabric.api.event.player.UseItemCallback; | ||
import net.minecraft.block.BlockState; | ||
import net.minecraft.entity.player.PlayerEntity; | ||
import net.minecraft.item.ItemStack; | ||
import net.minecraft.registry.tag.BlockTags; | ||
import net.minecraft.util.Hand; | ||
import net.minecraft.util.TypedActionResult; | ||
import net.minecraft.util.math.BlockPos; | ||
import net.minecraft.world.World; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
public class ItemCooldowns { | ||
private static final String JUNGLE_AXE_ID = "JUNGLE_AXE"; | ||
private static final String TREECAPITATOR_ID = "TREECAPITATOR_AXE"; | ||
private static final String GRAPPLING_HOOK_ID = "GRAPPLING_HOOK"; | ||
private static final ImmutableList<String> BAT_ARMOR_IDS = ImmutableList.of("BAT_PERSON_HELMET", "BAT_PERSON_CHESTPLATE", "BAT_PERSON_LEGGINGS", "BAT_PERSON_BOOTS"); | ||
|
||
private static final Map<String, CooldownEntry> ITEM_COOLDOWNS = new HashMap<>(); | ||
|
||
public static void init() { | ||
ClientPlayerBlockBreakEvent.AFTER.register(ItemCooldowns::afterBlockBreak); | ||
UseItemCallback.EVENT.register(ItemCooldowns::onItemInteract); | ||
} | ||
|
||
public static void afterBlockBreak(World world, PlayerEntity player, BlockPos pos, BlockState state) { | ||
if (!SkyblockerConfigManager.get().general.itemCooldown.enableItemCooldowns) return; | ||
|
||
String usedItemId = ItemUtils.getItemId(player.getMainHandStack()); | ||
if (usedItemId == null) return; | ||
|
||
if (state.isIn(BlockTags.LOGS)) { | ||
if (usedItemId.equals(JUNGLE_AXE_ID)) { | ||
if (!isOnCooldown(JUNGLE_AXE_ID)) { | ||
ITEM_COOLDOWNS.put(JUNGLE_AXE_ID, new CooldownEntry(2000)); | ||
} | ||
} else if (usedItemId.equals(TREECAPITATOR_ID)) { | ||
if (!isOnCooldown(TREECAPITATOR_ID)) { | ||
ITEM_COOLDOWNS.put(TREECAPITATOR_ID, new CooldownEntry(2000)); | ||
} | ||
} | ||
} | ||
} | ||
|
||
private static TypedActionResult<ItemStack> onItemInteract(PlayerEntity player, World world, Hand hand) { | ||
if (!SkyblockerConfigManager.get().general.itemCooldown.enableItemCooldowns) return TypedActionResult.pass(ItemStack.EMPTY); | ||
|
||
String usedItemId = ItemUtils.getItemId(player.getMainHandStack()); | ||
if (usedItemId != null && usedItemId.equals(GRAPPLING_HOOK_ID) && player.fishHook != null) { | ||
if (!isOnCooldown(GRAPPLING_HOOK_ID) && !isWearingBatArmor(player)) { | ||
ITEM_COOLDOWNS.put(GRAPPLING_HOOK_ID, new CooldownEntry(2000)); | ||
} | ||
} | ||
|
||
return TypedActionResult.pass(ItemStack.EMPTY); | ||
} | ||
|
||
public static boolean isOnCooldown(ItemStack itemStack) { | ||
return isOnCooldown(ItemUtils.getItemId(itemStack)); | ||
} | ||
|
||
private static boolean isOnCooldown(String itemId) { | ||
if (ITEM_COOLDOWNS.containsKey(itemId)) { | ||
CooldownEntry cooldownEntry = ITEM_COOLDOWNS.get(itemId); | ||
if (cooldownEntry.isOnCooldown()) { | ||
return true; | ||
} else { | ||
ITEM_COOLDOWNS.remove(itemId); | ||
return false; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
public static CooldownEntry getItemCooldownEntry(ItemStack itemStack) { | ||
return ITEM_COOLDOWNS.get(ItemUtils.getItemId(itemStack)); | ||
} | ||
|
||
private static boolean isWearingBatArmor(PlayerEntity player) { | ||
for (ItemStack stack : player.getArmorItems()) { | ||
String itemId = ItemUtils.getItemId(stack); | ||
if (!BAT_ARMOR_IDS.contains(itemId)) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
|
||
public record CooldownEntry(int cooldown, long startTime) { | ||
public CooldownEntry(int cooldown) { | ||
this(cooldown, System.currentTimeMillis()); | ||
} | ||
|
||
public boolean isOnCooldown() { | ||
return (this.startTime + this.cooldown) > System.currentTimeMillis(); | ||
} | ||
|
||
public long getRemainingCooldown() { | ||
long time = (this.startTime + this.cooldown) - System.currentTimeMillis(); | ||
return Math.max(time, 0); | ||
} | ||
|
||
public float getRemainingCooldownPercent() { | ||
return this.isOnCooldown() ? (float) this.getRemainingCooldown() / cooldown : 0.0f; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.