diff --git a/src/main/java/com/b1n_ry/yigd/config/YigdConfig.java b/src/main/java/com/b1n_ry/yigd/config/YigdConfig.java index b987572b..43f32b76 100644 --- a/src/main/java/com/b1n_ry/yigd/config/YigdConfig.java +++ b/src/main/java/com/b1n_ry/yigd/config/YigdConfig.java @@ -372,6 +372,8 @@ public static class ScrollConfig { @ConfigEntry.Gui.EnumHandler(option = ConfigEntry.Gui.EnumHandler.EnumDisplayOption.BUTTON) public ClickFunction clickFunction = ClickFunction.VIEW_CONTENTS; public boolean consumeOnUse = false; + public int useTime = 0; + public int useCooldown = 0; public enum ClickFunction { RESTORE_CONTENTS, VIEW_CONTENTS, TELEPORT_TO_LOCATION } diff --git a/src/main/java/com/b1n_ry/yigd/item/DeathScrollItem.java b/src/main/java/com/b1n_ry/yigd/item/DeathScrollItem.java index 69aa9cfa..5d5401ba 100644 --- a/src/main/java/com/b1n_ry/yigd/item/DeathScrollItem.java +++ b/src/main/java/com/b1n_ry/yigd/item/DeathScrollItem.java @@ -1,5 +1,6 @@ package com.b1n_ry.yigd.item; +import com.b1n_ry.yigd.Yigd; import com.b1n_ry.yigd.components.GraveComponent; import com.b1n_ry.yigd.config.YigdConfig; import com.b1n_ry.yigd.config.YigdConfig.ExtraFeatures.ScrollConfig; @@ -13,11 +14,13 @@ import net.minecraft.world.InteractionHand; import net.minecraft.world.InteractionResult; import net.minecraft.world.InteractionResultHolder; +import net.minecraft.world.entity.LivingEntity; import net.minecraft.world.entity.player.Player; import net.minecraft.world.flag.FeatureFlagSet; import net.minecraft.world.item.Item; import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.Items; +import net.minecraft.world.item.UseAnim; import net.minecraft.world.item.component.CustomData; import net.minecraft.world.item.component.ResolvableProfile; import net.minecraft.world.level.Level; @@ -34,6 +37,8 @@ public DeathScrollItem(Properties properties) { super(properties); } + private static final int USE_TIME_MARGIN = 3; + @Override public void onCraftedBy(@NotNull ItemStack stack, @NotNull Level level, @NotNull Player player) { if (!level.isClientSide) { @@ -47,22 +52,71 @@ public boolean isEnabled(@NotNull FeatureFlagSet enabledFeatures) { return YigdConfig.getConfig().extraFeatures.deathScroll.enabled; } + @Override + public int getUseDuration(@NotNull ItemStack ignoredStack, @NotNull LivingEntity ignoredEntity) { + return YigdConfig.getConfig().extraFeatures.deathScroll.useTime + USE_TIME_MARGIN; + } + + @Override + public @NotNull UseAnim getUseAnimation(@NotNull ItemStack stack) { + return UseAnim.BOW; + } + + @Override + public void onUseTick(@NotNull Level level, @NotNull LivingEntity livingEntity, @NotNull ItemStack stack, int remainingUseDuration) { + if (remainingUseDuration < USE_TIME_MARGIN) { + livingEntity.releaseUsingItem(); + } + } + + @Override + public void releaseUsing(@NotNull ItemStack stack, @NotNull Level level, @NotNull LivingEntity livingEntity, int timeCharged) { + float f = (float) (this.getUseDuration(stack, livingEntity) - timeCharged) / (float) (this.getUseDuration(stack, livingEntity) - USE_TIME_MARGIN); + Yigd.LOGGER.debug("{}", f); + if (f >= 1.0F && livingEntity instanceof Player player) { + InteractionHand hand = player.getItemInHand(InteractionHand.MAIN_HAND).equals(stack) ? InteractionHand.MAIN_HAND : InteractionHand.OFF_HAND; + this.useAction(level, player, hand); + return; + } + super.releaseUsing(stack, level, livingEntity, timeCharged); + } + @Override public @NotNull InteractionResultHolder use(Level world, @NotNull Player user, @NotNull InteractionHand hand) { if (world.isClientSide) return super.use(world, user, hand); - ScrollConfig scrollConfig = YigdConfig.getConfig().extraFeatures.deathScroll; - + YigdConfig.ExtraFeatures.ScrollConfig scrollConfig = YigdConfig.getConfig().extraFeatures.deathScroll; ServerPlayer player = (ServerPlayer) user; ItemStack scroll = player.getItemInHand(hand); CustomData scrollNbtComponent = scroll.get(DataComponents.CUSTOM_DATA); CompoundTag scrollNbt = scrollNbtComponent != null ? scrollNbtComponent.copyTag() : null; + // Rebind if the player is sneaking (and it can be rebound), or if the scroll is unbound - if ((scrollConfig.rebindable && player.isShiftKeyDown()) || scrollNbt == null || scrollNbt.getUUID("grave") == null) { + if ((scrollConfig.rebindable && player.isShiftKeyDown()) || scrollNbt == null || !scrollNbt.contains("grave")) { if (this.bindStackToLatestDeath(player, scroll)) return InteractionResultHolder.sidedSuccess(scroll, true); } + if (player.getCooldowns().isOnCooldown(this)) return InteractionResultHolder.fail(scroll); + + if (YigdConfig.getConfig().extraFeatures.deathScroll.useTime > 0) { + user.startUsingItem(hand); + } else { + return this.useAction(world, user, hand); + } + return InteractionResultHolder.consume(scroll); + } + + private InteractionResultHolder useAction(Level world, @NotNull Player user, @NotNull InteractionHand hand) { + if (world.isClientSide) return super.use(world, user, hand); + + ScrollConfig scrollConfig = YigdConfig.getConfig().extraFeatures.deathScroll; + + ServerPlayer player = (ServerPlayer) user; + ItemStack scroll = player.getItemInHand(hand); + CustomData scrollNbtComponent = scroll.get(DataComponents.CUSTOM_DATA); + CompoundTag scrollNbt = scrollNbtComponent != null ? scrollNbtComponent.copyTag() : null; + ScrollConfig.ClickFunction clickFunction = scrollConfig.clickFunction; if (scrollNbt != null && scrollNbt.contains("clickFunction") && !scrollNbt.getString("clickFunction").equals("default")) { clickFunction = ScrollConfig.ClickFunction.valueOf(scrollNbt.getString("clickFunction")); @@ -76,10 +130,9 @@ public boolean isEnabled(@NotNull FeatureFlagSet enabledFeatures) { if (res.getResult() != InteractionResult.PASS) { // If the action was successful/failed or something other than 'standard' if (YigdConfig.getConfig().extraFeatures.deathScroll.consumeOnUse && res.getResult() != InteractionResult.CONSUME) scroll.shrink(1); - return res; } - - return super.use(world, user, hand); + player.getCooldowns().addCooldown(this, scrollConfig.useCooldown); + return res; } public boolean bindStackToLatestDeath(ServerPlayer player, ItemStack scroll) { diff --git a/src/main/resources/assets/yigd/lang/en_us.json b/src/main/resources/assets/yigd/lang/en_us.json index 3c1610be..5d5724d9 100644 --- a/src/main/resources/assets/yigd/lang/en_us.json +++ b/src/main/resources/assets/yigd/lang/en_us.json @@ -250,6 +250,8 @@ "text.autoconfig.yigd.option.extraFeatures.deathScroll.receiveOnRespawn": "Receive on Respawn", "text.autoconfig.yigd.option.extraFeatures.deathScroll.clickFunction": "Executed When Used", "text.autoconfig.yigd.option.extraFeatures.deathScroll.consumeOnUse": "Consume When Use", + "text.autoconfig.yigd.option.extraFeatures.deathScroll.useTime": "Charge Time", + "text.autoconfig.yigd.option.extraFeatures.deathScroll.useCooldown": "Cooldown", "text.autoconfig.yigd.option.extraFeatures.graveCompass": "Grave Compass", "text.autoconfig.yigd.option.extraFeatures.graveCompass.receiveOnRespawn": "Receive on Respawn", "text.autoconfig.yigd.option.extraFeatures.graveCompass.consumeOnUse": "Consume on Use",