Jump to content

Recommended Posts

Posted (edited)

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
Posted

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.

Posted (edited)

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
Posted (edited)

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.

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

Posted

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

Posted

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());
    			    }
    			}
        }
		
	}

 

Posted

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.

Posted

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.

Posted

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.

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

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