Jump to content

Can't call method from another class


Hive Mind
 Share

Recommended Posts

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!

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



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • The version you are using is no longer supported on this forum. Please update to a modern version of Minecraft to receive support. Currently supported versions are 1.19.3 (Latest), 1.19.2, and 1.18.2 (LTS).
    • Technically, you can reuse the crafting recipe classes as long as you change the RecipeType since that's what determines whether the recipes can be used by a given system or not.
    • I mean...it's neither. You are trying to play a 1.19.2 mod on 1.19.3. It won't work. 1.19.2 does not equal 1.19.3.
    • Hi all, lately I've started developing mods that are server-side only, so I've already started working with packets, I thought it was a pretty simple thing (having already worked on it when developing plugins) (I know it's completely different at code level, but logical I think it's the same) but I ran into multiple problems, I'm developing a mod in 1.19.2 server side which for now has to send a simple message to individual clients by executing a command, like a sort of "hello world" debugging. I developed the command part and it works, but I really have no idea how to send a chatmessagepacket to the client, could someone help me? Test.java -- Command Class (Working) public class Test { public Test (CommandDispatcher<CommandSourceStack> dispatcher){ dispatcher.register(Commands.literal("test").executes((command) -> { return 0; })); } private int prova(CommandSourceStack source) throws CommandSyntaxException{ ServerPlayer player = source.getPlayer(); BlockPos pos = player.blockPosition(); //here must send a packet to the client return 1; } } PacketHandler.java public class PacketHandler { private static SimpleChannel INSTANCE; private static int packetId = 0; public static void register(){ SimpleChannel net = NetworkRegistry.ChannelBuilder .named(new ResourceLocation(Mceconomy.MODID, "main")) .networkProtocolVersion(() -> "1.0") .clientAcceptedVersions(s -> true) .serverAcceptedVersions(s -> true) .simpleChannel(); INSTANCE = net; net.messageBuilder(MessagePacket.class, id(), NetworkDirection.PLAY_TO_SERVER) .encoder(MessagePacket::toBytes) .decoder(MessagePacket::new) .consumerMainThread(MessagePacket::handle) .add(); } private static int id() { return packetId++; } public static <MSG> void sendToServer(MSG message) { INSTANCE.sendToServer(message); } public static <MSG> void sendToClient(MSG message, ServerPlayer player) { INSTANCE.send(PacketDistributor.PLAYER.with(() -> player), message); } } MessagePacket.java -- (StringTextComponent can't find it and I don't know why) public class MessagePacket { public MessagePacket() { } public MessagePacket(FriendlyByteBuf buf) { } public void toBytes(FriendlyByteBuf buf) { } public boolean handle(Supplier<NetworkEvent.Context> supplier) { NetworkEvent.Context context = supplier.get(); context.enqueueWork(() -> { //On the server ServerPlayer player = context.getSender(); ServerLevel world = player.getLevel(); player.sendSystemMessage(new StringTextComponent("Hello World!")); }); return true; } } Any ideas?
    • Alright, so for a second I thought I had a solution when I remembered the old position Entities use for lerping is public public final Vec3 getPosition(float p_20319_) { double d0 = Mth.lerp((double)p_20319_, this.xo, this.getX()); double d1 = Mth.lerp((double)p_20319_, this.yo, this.getY()); double d2 = Mth.lerp((double)p_20319_, this.zo, this.getZ()); return new Vec3(d0, d1, d2); } and while I was able to get the position on server, I found out the old position is identical to the current position (both on client and server), which means if I subtract it from the current position, I still get 0.0, what's up with that? I how can the game lerp motion if the two positions are the same?
  • Topics

×
×
  • Create New...

Important Information

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