Jump to content

Recommended Posts

Posted

I've been trying to create a sword with a special Riptide-like ability but I've ran into the problem of not being able to call the method that performs said ability, which is called "startObliterationAttack". The code where I'm trying to call the method from is in one "ObliteratorBlade" class and the abilities actual code is in another "SHPlayer" class, both shown below:

 

ObliteratorBlade

package net.hivethemodder.superheated.common.item;

import net.minecraft.util.Mth;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResultHolder;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.MoverType;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.*;
import net.minecraft.world.level.Level;
import net.minecraft.world.phys.Vec3;

public class ObliteratorBlade extends SwordItem implements Vanishable {

    public ObliteratorBlade(Tier tier, int damage, float attackSpeed, Properties props) {
        super(tier, damage, attackSpeed, props);
    }

    @Override
    public UseAnim getUseAnimation(ItemStack itemStack) {
        return UseAnim.BOW;
    }

    @Override
    public int getUseDuration(ItemStack itemStack) {
        return 72000;
    }

    @Override
    public void releaseUsing(ItemStack itemStack, Level level, LivingEntity livingEntity, int p_41415_) {
        if (livingEntity instanceof Player player) {
            int i = this.getUseDuration(itemStack) - p_41415_;
            if (i >= 10) {
                float playerRotX = player.getXRot();
                float playerRotY = player.getYRot();
                float VecX = -Mth.sin(playerRotY * ((float)Math.PI / 180f)) * Mth.cos(playerRotX * ((float)Math.PI / 180f));
                float VecY = -Mth.sin(playerRotX * ((float)Math.PI / 180f));
                float VecZ = Mth.cos(playerRotY * ((float)Math.PI / 180f)) * Mth.cos(playerRotX * ((float)Math.PI / 180f));
                float modifier = Mth.sqrt(VecX * VecX + VecY * VecY + VecZ * VecZ);
                float force = 1.5f;
                VecX *= force / modifier;
                VecY *= force / modifier;
                VecZ *= force / modifier;
                player.push(VecX, VecY, VecZ);
                //Where I want to call the method startObliterationAttack()
                if (player.isOnGround()) {
                    player.move(MoverType.SELF, new Vec3(0.0d, 1.1999999d, 0.0d));
                }
            }
        }
    }

    @Override
    public InteractionResultHolder<ItemStack> use(Level level, Player player, InteractionHand hand) {
        ItemStack itemStack = player.getItemInHand(hand);
        player.startUsingItem(hand);
        return InteractionResultHolder.consume(itemStack);
    }
}

SHPlayer

package net.hivethemodder.superheated.client;

import com.mojang.authlib.GameProfile;
import net.minecraft.core.BlockPos;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.entity.player.ProfilePublicKey;
import net.minecraft.world.level.Explosion;
import net.minecraft.world.level.Level;
import net.minecraft.world.phys.AABB;
import org.jetbrains.annotations.Nullable;

import java.util.List;

public abstract class SHPlayer extends Player {
    public int obliterationAttackTicks = 0;

    public SHPlayer(Level level, BlockPos blockPos, float p_219729_,
                    GameProfile profile,
                    @Nullable ProfilePublicKey publicKey) {
        super(level, blockPos, p_219729_, profile, publicKey);
    }

    //The method I want to call
    public void startObliterationAttack(int time) {
        this.autoSpinAttackTicks = time;
        if (!this.level.isClientSide) {
            this.removeEntitiesOnShoulder();
        }
    }

    @Override
    public void aiStep() {

        AABB aabb = this.getBoundingBox();

        if (obliterationAttackTicks > 0) {
            obliterationAttackTicks--;
            this.checkObliterationAttack(aabb, this.getBoundingBox());
        }
    }

    protected void checkObliterationAttack(AABB aabb1, AABB aabb2) {
        AABB aabb = aabb1.minmax(aabb2);
        List<Entity> list = this.level.getEntities(this, aabb);
        if (!list.isEmpty()) {
            for (Entity entity : list) {
                if (entity instanceof LivingEntity) {
                    this.doAutoAttackOnTouch((LivingEntity) entity);
                    Explosion.BlockInteraction interaction = Explosion.BlockInteraction.NONE;
                    level.explode(entity, this.getX(), this.getY(), this.getZ(), 3f, interaction);
                    this.obliterationAttackTicks = 0;
                    this.setDeltaMovement(this.getDeltaMovement().scale(-0.2d));
                    break;
                }
            }
        } else if (this.horizontalCollision) {
            this.obliterationAttackTicks = 0;
        }
    }
}

 

What I've already tried:

1. Import SHPlayer as a variable and then call the method:

SHPlayer shPlayer;
shPlayer.startObliterationAttack(time);

However this produces a NullPointerException when I try to use the ability in-game.

2. Cast Player into SHPlayer and then call the method:

SHPlayer shPlayer = (SHPlayer)livingEntity;
shPlayer.startObliterationAttack(time);

Doing this generates a "java.lang.ClassCastException" error when I try to use the ability.

 

Any help would be greatly appreciated!

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
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
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.

Announcements



×
×
  • Create New...

Important Information

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