Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Featured Replies

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?

_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. :)

_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. :)

  • Author

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...

Important Information

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

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.