Jump to content

[1.20.1] How to iterate over all entities when using an item


Recommended Posts

Posted

I was trying to create an item which when used summons lightning on all entities of given type (currently trying on cods).

It should do essentially the same thing, as if you used the command :

/execute at @e[type=cod] run summon lightning_bolt

My try on this problem was the following piece of code:

@Override
public InteractionResult useOn(UseOnContext pContext) {
    ServerLevel world = ((ServerLevel) pContext.getLevel());

    for(LivingEntity entity: pContext.getLevel().getEntities()) {        //problem with this line

        if(entity.getType() == EntityType.COD){
            EntityType.LIGHTNING_BOLT.spawn(world, entity.blockPosition(), MobSpawnType.EVENT);
        }
    }

    return InteractionResult.SUCCESS;
}

 

but I was stopped by the following error before running the game:

'getEntities()' has protected access in 'net.minecraft.world.level.Level'

 

So what should I do? Is there some other way to iterate over all entities?

Posted

_level.getServer().getCommands().performPrefixedCommand(new CommandSourceStack(CommandSource.NULL, new Vec3(x, y, z), Vec2.ZERO, _level, 4, "", Component.literal(""), _level.getServer(), null).withSuppressedOutput(), "execute at @e[type=cod] run summon lightning_bolt");

should do it, I don't think it'll send a chat message every time. Code thing wasn't working so I had to do plain text. :)

Posted

_level.getServer().getCommands().performPrefixedCommand(new CommandSourceStack(CommandSource.NULL, new Vec3(x, y, z), Vec2.ZERO, _level, 4, "", Component.literal(""), _level.getServer(), null).withSuppressedOutput(), "execute at @e[type=cod] run summon lightning_bolt");

should do it, I don't think it'll send a chat message every time. Code thing wasn't working so I had to do plain text. :)

Posted

So I tried implementing what you sent like this:

@Override
public InteractionResult useOn(UseOnContext pContext) {
    ServerLevel world = ((ServerLevel) pContext.getLevel());

    world.getServer().getCommands().performPrefixedCommand(new CommandSourceStack(
      CommandSource.NULL, Vec3.ZERO, Vec2.ZERO, world, 4, "", Component.literal(""), world.getServer(), null).withSuppressedOutput(), "execute at @e[type=cod] run summon lightning_bolt");

    return InteractionResult.SUCCESS;
}

but now when I used the item, the game crashed with the following error:
 

class net.minecraft.client.multiplayer.ClientLevel cannot be cast to class net.minecraft.server.level.ServerLevel 
(net.minecraft.client.multiplayer.ClientLevel and net.minecraft.server.level.ServerLevel are in module [email protected] of loader 'TRANSFORMER' @5377414a)

I seems to me that "world.getServer()" returns ClientLevel instead of ServerLevel, but since I'm new to modding and Java, I don't know how to fix it.

(I thought, that if I declared "world" to be "ServerLevel", than "world.getServer()" would return ServerLevel not ClientLevel, so I'm lost)

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.