Skip to content

Commit

Permalink
为蟹钳添加attribute tooltip
Browse files Browse the repository at this point in the history
  • Loading branch information
ZhuRuoLing committed Oct 4, 2024
1 parent a6af3f6 commit bc153f3
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 47 deletions.
84 changes: 44 additions & 40 deletions src/main/java/dev/dubhe/anvilcraft/item/AnvilHammerItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,18 +50,22 @@ public class AnvilHammerItem extends Item implements Equipable, IEngineerGoggles
public AnvilHammerItem(Item.Properties properties) {
super(properties);
modifiers = ItemAttributeModifiers.builder()
.add(
Attributes.ATTACK_DAMAGE,
new AttributeModifier(
BASE_ATTACK_DAMAGE_ID,
getAttackDamageModifierAmount(),
AttributeModifier.Operation.ADD_VALUE),
EquipmentSlotGroup.MAINHAND)
.add(
Attributes.ATTACK_SPEED,
new AttributeModifier(BASE_ATTACK_SPEED_ID, -3F, AttributeModifier.Operation.ADD_VALUE),
EquipmentSlotGroup.MAINHAND)
.build();
.add(
Attributes.ATTACK_DAMAGE,
new AttributeModifier(
BASE_ATTACK_DAMAGE_ID,
getAttackDamageModifierAmount(),
AttributeModifier.Operation.ADD_VALUE),
EquipmentSlotGroup.MAINHAND
).add(
Attributes.ATTACK_SPEED,
new AttributeModifier(
BASE_ATTACK_SPEED_ID,
-3F,
AttributeModifier.Operation.ADD_VALUE
),
EquipmentSlotGroup.MAINHAND
).build();
}

protected float getAttackDamageModifierAmount() {
Expand Down Expand Up @@ -92,18 +96,18 @@ private static void breakBlock(ServerPlayer player, BlockPos pos, @NotNull Serve
public static boolean ableToUseAnvilHammer(Level level, BlockPos blockPos, Player player) {
BlockState state = level.getBlockState(blockPos);
return state.getBlock() instanceof IHammerRemovable
|| state.getBlock() instanceof IHammerChangeable
|| state.is(ModBlockTags.HAMMER_REMOVABLE)
|| state.is(ModBlockTags.HAMMER_CHANGEABLE)
|| player.getOffhandItem().is(Items.FIREWORK_ROCKET);
|| state.getBlock() instanceof IHammerChangeable
|| state.is(ModBlockTags.HAMMER_REMOVABLE)
|| state.is(ModBlockTags.HAMMER_CHANGEABLE)
|| player.getOffhandItem().is(Items.FIREWORK_ROCKET);
}

private static void dropAnvil(Player player, Level level, BlockPos blockPos) {
if (player == null || level.isClientSide) return;
if (System.currentTimeMillis() - lastDropAnvilTime <= 150) return;
lastDropAnvilTime = System.currentTimeMillis();
AnvilFallOnLandEvent event = new AnvilFallOnLandEvent(
level, blockPos.above(), new FallingBlockEntity(EntityType.FALLING_BLOCK, level), player.fallDistance);
level, blockPos.above(), new FallingBlockEntity(EntityType.FALLING_BLOCK, level), player.fallDistance);
AnvilCraft.EVENT_BUS.post(event);
level.playSound(null, blockPos, SoundEvents.ANVIL_LAND, SoundSource.BLOCKS, 1f, 1f);
ItemStack itemStack = player.getItemInHand(player.getUsedItemHand());
Expand All @@ -116,7 +120,7 @@ private static void dropAnvil(Player player, Level level, BlockPos blockPos) {
* 右键方块
*/
public static void useBlock(
@NotNull ServerPlayer player, BlockPos blockPos, @NotNull ServerLevel level, ItemStack anvilHammer) {
@NotNull ServerPlayer player, BlockPos blockPos, @NotNull ServerLevel level, ItemStack anvilHammer) {
if (rocketJump(player, level, blockPos)) return;
if (player.isShiftKeyDown()) {
breakBlock(player, blockPos, level, anvilHammer);
Expand All @@ -138,15 +142,15 @@ private static boolean rocketJump(ServerPlayer serverPlayer, ServerLevel level,
serverPlayer.setDeltaMovement(0, power, 0);
PacketDistributor.sendToPlayer(serverPlayer, new RocketJumpPacket(power));
level.sendParticles(
ParticleTypes.FIREWORK,
serverPlayer.getX(),
serverPlayer.getY(),
serverPlayer.getZ(),
20,
0,
0.5,
0,
0.05);
ParticleTypes.FIREWORK,
serverPlayer.getX(),
serverPlayer.getY(),
serverPlayer.getZ(),
20,
0,
0.5,
0,
0.05);
level.playSound(null, blockPos, SoundEvents.FIREWORK_ROCKET_LAUNCH, SoundSource.PLAYERS);
return true;
}
Expand All @@ -167,7 +171,7 @@ public static void attackBlock(Player player, BlockPos blockPos, Level level) {

@Override
public boolean canAttackBlock(
@NotNull BlockState state, @NotNull Level level, @NotNull BlockPos pos, @NotNull Player player) {
@NotNull BlockState state, @NotNull Level level, @NotNull BlockPos pos, @NotNull Player player) {
return !player.isCreative();
}

Expand All @@ -178,11 +182,11 @@ public float getDestroySpeed(@NotNull ItemStack stack, @NotNull BlockState state

@Override
public boolean mineBlock(
@NotNull ItemStack stack,
@NotNull Level level,
@NotNull BlockState state,
@NotNull BlockPos pos,
@NotNull LivingEntity miningEntity) {
@NotNull ItemStack stack,
@NotNull Level level,
@NotNull BlockState state,
@NotNull BlockPos pos,
@NotNull LivingEntity miningEntity) {
if (state.getDestroySpeed(level, pos) != 0.0f) {
stack.hurtAndBreak(2, miningEntity, LivingEntity.getSlotForHand(miningEntity.getUsedItemHand()));
}
Expand All @@ -200,13 +204,13 @@ public boolean hurtEnemy(@NotNull ItemStack stack, @NotNull LivingEntity target,
target.hurt(target.level().damageSources().anvil(attacker), damageBonus);
if (attacker.fallDistance >= 3) {
attacker.level()
.playSound(
null,
BlockPos.containing(attacker.position()),
SoundEvents.ANVIL_LAND,
SoundSource.BLOCKS,
1f,
attacker.fallDistance > 17 ? (float) 0.5 : 1 - attacker.fallDistance / 35);
.playSound(
null,
BlockPos.containing(attacker.position()),
SoundEvents.ANVIL_LAND,
SoundSource.BLOCKS,
1f,
attacker.fallDistance > 17 ? (float) 0.5 : 1 - attacker.fallDistance / 35);
}
return true;
}
Expand Down
41 changes: 34 additions & 7 deletions src/main/java/dev/dubhe/anvilcraft/item/CrabClawItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,43 @@
import dev.dubhe.anvilcraft.api.entity.attribute.EntityReachAttribute;
import dev.dubhe.anvilcraft.init.ModItems;

import net.minecraft.ChatFormatting;
import net.minecraft.MethodsReturnNonnullByDefault;
import net.minecraft.core.Holder;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.chat.CommonComponents;
import net.minecraft.network.chat.Component;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.ai.attributes.Attribute;
import net.minecraft.world.entity.ai.attributes.AttributeModifier;
import net.minecraft.world.entity.ai.attributes.Attributes;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.Item;

import com.google.common.collect.Multimap;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.TooltipFlag;
import net.minecraft.world.item.component.ItemAttributeModifiers;

import javax.annotation.ParametersAreNonnullByDefault;
import java.util.List;
import java.util.function.Supplier;

@ParametersAreNonnullByDefault
@MethodsReturnNonnullByDefault
public class CrabClawItem extends Item {

public static final AttributeModifier rangeAttributeModifier =
new AttributeModifier(AnvilCraft.of("range_modifier"), 3, AttributeModifier.Operation.ADD_VALUE);

public static final AttributeModifier RANGE_ATTRIBUTE_MODIFIER = new AttributeModifier(
AnvilCraft.of("range_modifier"),
3,
AttributeModifier.Operation.ADD_VALUE
);
private static final Supplier<Multimap<Holder<Attribute>, AttributeModifier>> RANGE_MODIFIER_SUPPLIER =
EntityReachAttribute.getRangeModifierSupplier(RANGE_ATTRIBUTE_MODIFIER);
public static final String CRAB_CLAW_MARKER = "crabClaw";
public static final String DUAL_CRAB_CLAW_MARKER = "dualCrabClaw";

private static final Supplier<Multimap<Holder<Attribute>, AttributeModifier>> rangeModifier =
EntityReachAttribute.getRangeModifierSupplier(rangeAttributeModifier);


public CrabClawItem(Properties properties) {
super(properties);
Expand All @@ -46,12 +61,12 @@ public static void holdingCrabClawIncreasesRange(LivingEntity entity) {
boolean wasHoldingCrabClaw = customData.contains(CRAB_CLAW_MARKER);
boolean wasHoldingDualCrabClaw = customData.contains(DUAL_CRAB_CLAW_MARKER);
if (!holdingCrabClaw) {
player.getAttributes().removeAttributeModifiers(rangeModifier.get());
player.getAttributes().removeAttributeModifiers(RANGE_MODIFIER_SUPPLIER.get());
if (wasHoldingCrabClaw) {
customData.remove(CRAB_CLAW_MARKER);
}
} else {
player.getAttributes().addTransientAttributeModifiers(rangeModifier.get());
player.getAttributes().addTransientAttributeModifiers(RANGE_MODIFIER_SUPPLIER.get());
if (!wasHoldingDualCrabClaw) {
customData.putBoolean(CRAB_CLAW_MARKER, true);
}
Expand All @@ -67,4 +82,16 @@ public static void holdingCrabClawIncreasesRange(LivingEntity entity) {
}
}
}

@Override
public void appendHoverText(ItemStack stack, TooltipContext context, List<Component> tooltipComponents, TooltipFlag tooltipFlag) {
tooltipComponents.add(CommonComponents.EMPTY);
tooltipComponents.add(Component.translatable("item.modifiers.hand").withStyle(ChatFormatting.GRAY));
tooltipComponents.add(Component.translatable(
"attribute.modifier.plus." + RANGE_ATTRIBUTE_MODIFIER.operation().id(),
ItemAttributeModifiers.ATTRIBUTE_MODIFIER_FORMAT.format(RANGE_ATTRIBUTE_MODIFIER.amount()),
Component.translatable(Attributes.BLOCK_INTERACTION_RANGE.value().getDescriptionId())
).withStyle(Attributes.BLOCK_INTERACTION_RANGE.value().getStyle(true)));
super.appendHoverText(stack, context, tooltipComponents, tooltipFlag);
}
}

0 comments on commit bc153f3

Please sign in to comment.