Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

hurtBreak method not working


Recommended Posts

I'm trying to make an jetpack, but I can't make the item receive damage. I think it has something to do with the client side.

Here's the code:

Spoiler
public class JetPack extends ArmorItem implements Wearable, IForgeItem {

    final int chargeTick = 15;

    public JetPack(Properties p_40388_) {
        super(new JetPackMaterial().MATERIAL, EquipmentSlot.CHEST, p_40388_);
        DispenserBlock.registerBehavior(this, ArmorItem.DISPENSE_ITEM_BEHAVIOR);
    }

    @Override
    public int getMaxDamage(ItemStack stack) {
        return 64;
    }

    public boolean isValidRepairItem(ItemStack p_41134_, ItemStack p_41135_) {
        return p_41135_.is(Items.GUNPOWDER);

    }

    public static boolean isEnabled(ItemStack p_41141_) {
        return p_41141_.getDamageValue() < p_41141_.getMaxDamage() - 1;
    }

    @Override
    public InteractionResultHolder<ItemStack> use(Level p_41137_, Player p_41138_, InteractionHand p_41139_) {
        ItemStack itemstack = p_41138_.getItemInHand(p_41139_);
        EquipmentSlot equipmentslot = Mob.getEquipmentSlotForItem(itemstack);
        ItemStack itemstack1 = p_41138_.getItemBySlot(equipmentslot);
        if (itemstack1.isEmpty()) {
            p_41138_.setItemSlot(equipmentslot, itemstack.copy());
            if (!p_41137_.isClientSide()) {
                p_41138_.awardStat(Stats.ITEM_USED.get(this));
            }

            itemstack.setCount(0);
            return InteractionResultHolder.sidedSuccess(itemstack, p_41137_.isClientSide());
        } else {
            return InteractionResultHolder.fail(itemstack);
        }

    }

    @Nullable
    @Override
    public String getArmorTexture(ItemStack stack, Entity entity, EquipmentSlot slot, String type) {
        if(stack.getItem() instanceof JetPack){
            if(slot.getIndex() == 1){
                return new ResourceLocation(ExampleMod.MOD_ID, "textures/models/armor/jetpack_layer_2.png").toString();
            }
            return new ResourceLocation(ExampleMod.MOD_ID, "textures/models/armor/jetpack_layer_1.png").toString();
        }
        return null;
    }

    @Override
    @OnlyIn(Dist.CLIENT)
    public void initializeClient(Consumer<IItemRenderProperties> consumer) {
        consumer.accept(new IItemRenderProperties() {
            @Override
            public <A extends HumanoidModel<?>> A getArmorModel(LivingEntity entity, ItemStack stack, EquipmentSlot slot, A properties) {
                HumanoidModel<?> model = slot.equals(EquipmentSlot.LEGS) ? new HumanoidModel<>(ModModelLayers.PLAYER_FIT_INNER_ARMOR.bakeRoot())
                        : new HumanoidModel<>(ModModelLayers.PLAYER_FIT_OUTER_ARMOR.bakeRoot());

                model.crouching = properties.crouching;
                model.riding = properties.riding;
                model.young = properties.young;

                return (A) model;

            }
        });
    }

    @Override
    public void onArmorTick(ItemStack stack, Level world, Player player) {
        int tick = 0;
        if(stack.getOrCreateTag().contains("ChargeTick")) {
            tick = stack.getOrCreateTag().getInt("ChargeTick");
            if(tick > 0 && player.isOnGround()){
                tick = 0;
            }
        }
        if(tick > 0) {
            tick--;
        }
        if(tick == 0){
            tick--;
            player.level.addParticle(ModParticles.BLOCK_OFF_SIDE_PARTICLE.get(), player.getRandomX(0.5D), player.getRandomY() - 0.25D, player.getRandomZ(0.5D), (player.getRandom().nextDouble() - 0.5D) * 2.0D, -player.getRandom().nextDouble(), (player.getRandom().nextDouble() - 0.5D) * 2.0D);

        }

        stack.getOrCreateTag().putInt("ChargeTick", tick);
    }

    public boolean hasCharge(ItemStack stack){
        if(stack.getOrCreateTag().contains("ChargeTick")){
            return stack.getOrCreateTag().getInt("ChargeTick") < 0;
        }
        return false;
    }

    public void uncharge(ItemStack stack){
        stack.getOrCreateTag().putInt("ChargeTick", chargeTick);
    }

    @Override
    public boolean canElytraFly(ItemStack stack, net.minecraft.world.entity.LivingEntity entity) {
        if(JetPack.isEnabled(stack) && !entity.isInWater() && !entity.isOnGround() && hasCharge(stack)){
            jump(stack, entity);
            stack.hurtAndBreak(1, entity, e -> e.broadcastBreakEvent(EquipmentSlot.CHEST));
        }
        return false;
    }

    public void jump(ItemStack stack, LivingEntity entity) {
        uncharge(stack);
        Vec3 vec = entity.getDeltaMovement();
        double vetY = Math.max(entity.getDeltaMovement().add(0, 0.5, 0).y(), 0.25F);
        Vec3 vet = new Vec3(vec.x, vetY, vec.z);
        entity.setDeltaMovement(vet);
        int max = 20;
        for (int i = 1; i <= max; i++) {
            double angle = (2 * Math.PI * i) / max;
            double x = entity.getX() + Math.cos(angle);
            double y = entity.getY();
            double z = entity.getZ() + Math.sin(angle);
            entity.level.addParticle(ParticleTypes.FIREWORK, x, y, z, 0, 0, 0);

        }
    }

    @Nullable
    @Override
    public EquipmentSlot getEquipmentSlot(ItemStack stack) {
        return EquipmentSlot.CHEST;
    }


}

 

 

Link to comment
Share on other sites

Posted (edited)

I want to make the item decrease its durability when it is used. I mean, "hurt" the item when the player tries to use it seems like the better option. I've tried to call it when inside the elytraFlightTick method and cancel the flight later, the item durability reduces, but the player keep starting the boring flight animation. 

Sorry if i wrote a little weird, i'm not very good at english.

Edited by LuccaPossamai
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.