Skip to content

Commit

Permalink
Added cooldown and use time support for death scrolls
Browse files Browse the repository at this point in the history
  • Loading branch information
B1n-ry committed Dec 28, 2024
1 parent f0e5982 commit bff2396
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/main/java/com/b1n_ry/yigd/block/GraveBlock.java
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ public void afterBreak(World world, PlayerEntity player, BlockPos pos, BlockStat

@Override
public float calcBlockBreakingDelta(BlockState state, PlayerEntity player, BlockView world, BlockPos pos) {
if (!(world.getBlockEntity(pos) instanceof GraveBlockEntity grave) || grave.isUnclaimed()
if (!(world.getBlockEntity(pos) instanceof GraveBlockEntity grave) || !grave.isUnclaimed()
|| YigdConfig.getConfig().graveConfig.retrieveMethods.onBreak) {
// Same calculations as done for "normal" blocks, except with the overwritten destroy speed of 0.8
float f = 0.8f;
Expand Down
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 @@ -377,6 +377,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
51 changes: 49 additions & 2 deletions src/main/java/com/b1n_ry/yigd/item/DeathScrollItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import net.minecraft.component.DataComponentTypes;
import net.minecraft.component.type.NbtComponent;
import net.minecraft.component.type.ProfileComponent;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
Expand All @@ -18,15 +19,19 @@
import net.minecraft.util.ActionResult;
import net.minecraft.util.Hand;
import net.minecraft.util.TypedActionResult;
import net.minecraft.util.UseAction;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.UUID;

public class DeathScrollItem extends Item {
private static final int USE_TIME_MARGIN = 3;

public DeathScrollItem(Settings settings) {
super(settings);
}
Expand All @@ -44,6 +49,31 @@ public boolean isEnabled(FeatureSet enabledFeatures) {
return YigdConfig.getConfig().extraFeatures.deathScroll.enabled;
}

@Override
public int getMaxUseTime(ItemStack stack, LivingEntity user) {
return YigdConfig.getConfig().extraFeatures.deathScroll.useTime + USE_TIME_MARGIN;
}
@Override
public UseAction getUseAction(ItemStack stack) {
return UseAction.BOW;
}
@Override
public void usageTick(World world, LivingEntity user, ItemStack stack, int remainingUseTicks) {
if (remainingUseTicks < USE_TIME_MARGIN) {
user.stopUsingItem();
}
}
@Override
public void onStoppedUsing(ItemStack stack, World world, LivingEntity user, int remainingUseTicks) {
float f = (float) (this.getMaxUseTime(stack, user) - remainingUseTicks) / (float) (this.getMaxUseTime(stack, user) - USE_TIME_MARGIN);
if (f >= 1.0F && user instanceof PlayerEntity player) {
Hand hand = player.getStackInHand(Hand.MAIN_HAND).equals(stack) ? Hand.MAIN_HAND : Hand.OFF_HAND;
this.useAction(world, player, hand);
return;
}
super.onStoppedUsing(stack, world, user, remainingUseTicks);
}

@Override
public TypedActionResult<ItemStack> use(World world, PlayerEntity user, Hand hand) {
if (world.isClient) return super.use(world, user, hand);
Expand All @@ -55,11 +85,27 @@ public TypedActionResult<ItemStack> use(World world, PlayerEntity user, Hand han
NbtComponent scrollNbtComponent = scroll.get(DataComponentTypes.CUSTOM_DATA);
NbtCompound scrollNbt = scrollNbtComponent != null ? scrollNbtComponent.copyNbt() : null;
// Rebind if the player is sneaking (and it can be rebound), or if the scroll is unbound
if ((scrollConfig.rebindable && player.isSneaking()) || scrollNbt == null || scrollNbt.getUuid("grave") == null) {
if ((scrollConfig.rebindable && player.isSneaking()) || scrollNbt == null || !scrollNbt.contains("grave")) {
if (this.bindStackToLatestDeath(player, scroll))
return TypedActionResult.success(scroll, true);
}

if (player.getItemCooldownManager().isCoolingDown(this)) return TypedActionResult.fail(scroll);
if (YigdConfig.getConfig().extraFeatures.deathScroll.useTime > 0) {
user.setCurrentHand(hand);
} else {
return this.useAction(world, user, hand);
}
return TypedActionResult.consume(scroll);
}
private TypedActionResult<ItemStack> useAction(World world, @NotNull PlayerEntity user, @NotNull Hand hand) {
if (world.isClient) return super.use(world, user, hand);
ScrollConfig scrollConfig = YigdConfig.getConfig().extraFeatures.deathScroll;
ServerPlayerEntity player = (ServerPlayerEntity) user;
ItemStack scroll = player.getStackInHand(hand);
NbtComponent scrollNbtComponent = scroll.get(DataComponentTypes.CUSTOM_DATA);
NbtCompound scrollNbt = scrollNbtComponent != null ? scrollNbtComponent.copyNbt() : 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,7 +122,8 @@ public TypedActionResult<ItemStack> use(World world, PlayerEntity user, Hand han
return res;
}

return super.use(world, user, hand);
player.getItemCooldownManager().set(this, scrollConfig.useCooldown);
return res;
}

public boolean bindStackToLatestDeath(ServerPlayerEntity 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 @@ -258,6 +258,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 bff2396

Please sign in to comment.