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'm trying to make my own custom recipes using lightning, but the output item gets deleted because of the lightning

how do i make that item invulnerable to the lightning?

Edited by magic_man

  • magic_man changed the title to make an itemEntity invulnerable

Aren't you already listening to the EntityStruckByLightining event?

You need some way to differentiate the item entities you spawned from the normal ones.

One simple way might be to look at the ItemEntity's age to see if it is zero, i.e. it was just created.

Boilerplate:

If you don't post your logs/debug.log we can't help you. For curseforge you need to enable the forge debug.log in its minecraft settings. You should also post your crash report if you have one.

If there is no error in the log file and you don't have a crash report then post the launcher_log.txt from the minecraft folder. Again for curseforge this will be in your curseforge/minecraft/Install

Large files should be posted to a file sharing site like https://gist.github.com  You should also read the support forum sticky post.

  • Author

is it also possible to make it invulnerable for only a certain time?

for example 1 second after it is spawned in?

  • Author

is that done the same way as just doing it permanently or is that a whole different story

 

also in case the problem is in my code here is the code:

lightning_recipes:

@Mod.EventBusSubscriber(Dist.CLIENT)
public class lightning_recipes {
    
    @SubscribeEvent
    public static void onLightningHitItem(EntityStruckByLightningEvent event) {
        Entity entity = event.getEntity();
                if (entity instanceof ItemEntity itemEntity) {
                    ItemStack itemStack = itemEntity.getItem();
                    if (itemStack.getItem() == Items.IRON_INGOT) {
                        System.out.println("# iron ingot is struck by lightning");
                        entity.remove(Entity.RemovalReason.KILLED);
                        entity.spawnAtLocation(itemInit.ELECTRIFIED_IRON_INGOT.get());
                    }
                }
        }
        
    }

serverevent:

@Mod.EventBusSubscriber(Dist.DEDICATED_SERVER)
public class serverEvent {

	
	@SubscribeEvent
	public static void onservertick(ServerTickEvent event) { } 
}


 

Edited by magic_man

Are you really trying to handle this on the client?

The event is only fired on the logical server from what I can see. See LightningBolt.tick()

You should remove the Dist.CLIENT.

Edited by warjort

Boilerplate:

If you don't post your logs/debug.log we can't help you. For curseforge you need to enable the forge debug.log in its minecraft settings. You should also post your crash report if you have one.

If there is no error in the log file and you don't have a crash report then post the launcher_log.txt from the minecraft folder. Again for curseforge this will be in your curseforge/minecraft/Install

Large files should be posted to a file sharing site like https://gist.github.com  You should also read the support forum sticky post.

  • Author

removing the dist.client changes nothing

the item that it has to transform into is there for a split second and then is gone

Quote

removing the dist.client changes nothing

Not for this issue, yes. But it does mean your mod won't stop working on a dedicated server. 🙂

Boilerplate:

If you don't post your logs/debug.log we can't help you. For curseforge you need to enable the forge debug.log in its minecraft settings. You should also post your crash report if you have one.

If there is no error in the log file and you don't have a crash report then post the launcher_log.txt from the minecraft folder. Again for curseforge this will be in your curseforge/minecraft/Install

Large files should be posted to a file sharing site like https://gist.github.com  You should also read the support forum sticky post.

  • Author

managed to do it like this:

    @SubscribeEvent
    public static void onEntityKilled(EntityLeaveWorldEvent event) {
        Entity entity = event.getEntity();
        if (entity instanceof ItemEntity itemEntity) {
            ItemStack itemStack = itemEntity.getItem();
            if (itemStack.getItem() == itemInit.ELECTRIFIED_IRON_INGOT.get()) {
                System.out.println("# not allowed");
                entity.spawnAtLocation(itemInit.ELECTRIFIED_IRON_INGOT.get());
    }
}
    }
}

(cancelling the event didn't work)
 

but i have a problem that if the iron stack on eachother instead of doing every single one individually it only gives one

example: you drop 16 iron ingots, a lightning strike hits it, output is still 1

  • Author

i tried it but didn't really get it to work and eventually thought like: what if i just say no to it deleting it?

  • Author

this worked, but this isn't the part that should cause the issue

    @SubscribeEvent
    public static void onEntityKilled(EntityLeaveWorldEvent event) {
        Entity entity = event.getEntity();
        if (entity instanceof ItemEntity itemEntity) {
            ItemStack itemStack = itemEntity.getItem();
            if (itemStack.getItem() == itemInit.ELECTRIFIED_IRON_INGOT.get()) {
                System.out.println("# not allowed");
                entity.spawnAtLocation(itemInit.ELECTRIFIED_IRON_INGOT.get());
    }
}
    }
}

it's this that should cause it:
 

   @SubscribeEvent
    public static void onLightningHitItem(EntityStruckByLightningEvent event) {
    	Entity entity = event.getEntity();
    			if (entity instanceof ItemEntity itemEntity) {
    			    ItemStack itemStack = itemEntity.getItem();
    			    if (itemStack.getItem() == Items.IRON_INGOT) {
    			    	System.out.println("# iron ingot is struck by lightning");
    			    	entity.remove(Entity.RemovalReason.KILLED);
    		            entity.spawnAtLocation(itemInit.ELECTRIFIED_IRON_INGOT.get());
    			    }
    			}
        }
		
	}

 

There is another way you can do this.

A lightning bolt does 5 points of damage and ItemEntitys are created with health = 5

If you use an Access Transformer to make the health field public, you can modify your spawned ItemEntity so it has enough health to survive a lightning strike.

The side effect might be the ItemEntity also survives other damage its not supposed to, e.g. an explosion.

Boilerplate:

If you don't post your logs/debug.log we can't help you. For curseforge you need to enable the forge debug.log in its minecraft settings. You should also post your crash report if you have one.

If there is no error in the log file and you don't have a crash report then post the launcher_log.txt from the minecraft folder. Again for curseforge this will be in your curseforge/minecraft/Install

Large files should be posted to a file sharing site like https://gist.github.com  You should also read the support forum sticky post.

Do you really think creating a new EntityType and writing all the code to replace spawnAtLocation() and maintain in when Mojang changes it future

is simpler than modifying one value? 🙂

Boilerplate:

If you don't post your logs/debug.log we can't help you. For curseforge you need to enable the forge debug.log in its minecraft settings. You should also post your crash report if you have one.

If there is no error in the log file and you don't have a crash report then post the launcher_log.txt from the minecraft folder. Again for curseforge this will be in your curseforge/minecraft/Install

Large files should be posted to a file sharing site like https://gist.github.com  You should also read the support forum sticky post.

By the way, nobody answered the other question.

Quote

but i have a problem that if the iron stack on eachother instead of doing every single one individually it only gives one

example: you drop 16 iron ingots, a lightning strike hits it, output is still 1

You should use something like (untested code):

Quote

entity.spawnAtLocation(new ItemStack(itemInit.ELECTRIFIED_IRON_INGOT.get(), itemStack.getCount()));

 

Boilerplate:

If you don't post your logs/debug.log we can't help you. For curseforge you need to enable the forge debug.log in its minecraft settings. You should also post your crash report if you have one.

If there is no error in the log file and you don't have a crash report then post the launcher_log.txt from the minecraft folder. Again for curseforge this will be in your curseforge/minecraft/Install

Large files should be posted to a file sharing site like https://gist.github.com  You should also read the support forum sticky post.

Quote

There is also another approach, if the spawned item is always added by the mod itself.

Item.canBeHurtBy()

Yes I saw that, but I got the impression the original poster was writing something more generic from the original question. 🙂

Boilerplate:

If you don't post your logs/debug.log we can't help you. For curseforge you need to enable the forge debug.log in its minecraft settings. You should also post your crash report if you have one.

If there is no error in the log file and you don't have a crash report then post the launcher_log.txt from the minecraft folder. Again for curseforge this will be in your curseforge/minecraft/Install

Large files should be posted to a file sharing site like https://gist.github.com  You should also read the support forum sticky post.

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.