Skip to content

Commit

Permalink
Added charge and cooldown times to death scroll
Browse files Browse the repository at this point in the history
  • Loading branch information
B1n-ry committed Nov 30, 2024
1 parent 73b49a8 commit 7e89148
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 6 deletions.
2 changes: 2 additions & 0 deletions src/main/java/com/b1n_ry/yigd/config/YigdConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
65 changes: 59 additions & 6 deletions src/main/java/com/b1n_ry/yigd/item/DeathScrollItem.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand All @@ -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) {
Expand All @@ -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<ItemStack> 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<ItemStack> 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"));
Expand All @@ -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) {
Expand Down
2 changes: 2 additions & 0 deletions src/main/resources/assets/yigd/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 7e89148

Please sign in to comment.