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



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • Hello everyone, As the title suggests I'm new the the entire modding thing I used to make little game projects here and there and since I've started University I've taking Java and I fell in love with it somehow. So I have a couple of questions: Can I learn Java from modding the game (Yes I have the basics) can it expand my knowledge in this language? Does modding require massive teams (I've seen too many projects on curseforge that have like 3-6 contributors) or can I do it solo? Where to start? I know you might be thinking "Whats wrong with this guy just simply google what to do..." and for that I say "I did" and found too many results not saying its a bad thing just you know we have a common term in development in general and that's "Development hell" you guys are more experienced than I'm what tutorial series do you recommend? Why didn't I use Reddit for this question why the minecraftforge forums where we fix issues to modding not help beginners start?  Because it gave me mental issues not saying this for attention but I really dislike the communities there are helpful somewhat but everything I said there regardless of what it is even a simple question in development will be bombarded with hateful comments cruelty and bullying and even worse grammar police , I truly hope that you guys aren't like this...   Please forgive me for my English as I'm not an English speaker. I'm alright with you people criticizing me for the question or the stuff I said just don't be cruel , thanks in advance! 
    • Hi! As the title suggests, I am encountering an issue with the Prominence II mod pack in which my server keeps crashing at random intervals. I am hosting it through Aternos, and below is a copy of my crash log. I have tried googling solutions and all forums are years old. I am new to hosting a server and don't know my way around files. I have also contacted Aternos support and they said it was a problem with the trinket mod? However I don't know if removing it will solve or corrupt my world. Any help would be greatly appreciated!   https://mclo.gs/RWmkEtK
    • I've been trying to render a line on the screen for a few days now, and everywhere I've looked has outdated answers. They say to use RenderGameOverlayEvent, which has been removed by my understanding. Is there a specific event I should be using or anything else I'm doing wrong? Tesselator tesselator = Tesselator.getInstance(); BufferBuilder renderer = tesselator.getBuilder(); renderer.begin(VertexFormat.Mode.DEBUG_LINES, DefaultVertexFormat.POSITION_COLOR); Color c = new Color(255, 0, 0, 255); renderer.vertex(0, 100, 0).color(c.getRed(), c.getGreen(), c.getBlue(), c.getAlpha()).endVertex(); renderer.vertex(0, 200, 0).color(c.getRed(), c.getGreen(), c.getBlue(), c.getAlpha()).endVertex(); tesselator.end(); Based on what I've seen, this is the code that should be working, though I feel that I need to override an event and add it in. What event should I be using for this? Is there another way of doing it that I don't know of?
    • yes i did it, we installed the drivers from nvidia website
    • Wait, how did you fix it?      
  • Topics

×
×
  • Create New...

Important Information

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