Jump to content

[1.8][SOLVED] Custom Attribute/Attribute Modifier Help


NovaViper

Recommended Posts

Hey, I'm trying to make a new attribute that makes an entity that has it more resistant to explosions from creepers and TNT. This is my first time attempting this and I have no idea where to start off with it. I already created the attribute itself but I don't know how to create the events to activate it/utilize it. My code is on Github and here is attribute:

 

    public static final IAttribute blastResist = (new RangedAttribute((IAttribute)null, "generic.blastResist", 30.0D, 0.0D, Double.MAX_VALUE)).setDescription("Explosion Resistance").setShouldWatch(true);

Main Developer and Owner of Zero Quest

Visit the Wiki for more information

If I helped anyone, please give me a applaud and a thank you!

Link to comment
Share on other sites

Anything realted to dealing damage requires you to subscribe to LivingHurtEvent. There you will need to check DamageSource and if it's an explosion, do check for value of this attribute (example on how to get attribute are in vanilla) and compare values (do math) and then apply new damage (cancel old one).

 

Registering attribute should be done in EntityConstructing (instanceof EntityLivingBase). I mean - I THINK so.

To register it you use something like entity.registerBaseAttribute (I don't remember exact name)

1.7.10 is no longer supported by forge, you are on your own.

Link to comment
Share on other sites

Ok, here's what I started to do but I'm not sure how get the damage the entity has taken

 

	@SubscribeEvent
public void blastResist(LivingHurtEvent event){
	if(event.entity instanceof EntityMetalZertum){
		if(event.source.isExplosion()){

		}
	}

}

Main Developer and Owner of Zero Quest

Visit the Wiki for more information

If I helped anyone, please give me a applaud and a thank you!

Link to comment
Share on other sites

How do I get the explosion damage type? I'm not exactly sure if I'm doing this correctly, here is what I have

 

	@SubscribeEvent
public void blastResist(LivingHurtEvent event){
	if(event.entity instanceof EntityMetalZertum){

		EntityMetalZertum entity;

		if(event.source.isExplosion()){
			event.ammount = event.source / entity.blastResist;
		}
	}

}

Main Developer and Owner of Zero Quest

Visit the Wiki for more information

If I helped anyone, please give me a applaud and a thank you!

Link to comment
Share on other sites

I can't use the same thing again, it'll glitch it up. What I'm trying to do is take the damage and divide it.

 

Like this

 

	@SubscribeEvent
public void blastResist(LivingHurtEvent event){
	if(event.entity instanceof EntityMetalZertum){

		EntityMetalZertum entity;

		if(event.source.isExplosion()){
			event.ammount = event.ammount / entity.getEntityAttribute(entity.blastResist).getAttributeValue();
		}
	}

}

 

Also the ammount is a float, not a double

Main Developer and Owner of Zero Quest

Visit the Wiki for more information

If I helped anyone, please give me a applaud and a thank you!

Link to comment
Share on other sites

I'm getting a crash saying

 

 

[19:40:59] [Client thread/ERROR] [FML]: Caught exception from zero_quest

java.lang.IllegalArgumentException: Method public void common.zeroquest.events.LivingEvents.blastResist(net.minecraftforge.event.entity.living.LivingHurtEvent,common.zeroquest.entity.EntityMetalZertum) has @SubscribeEvent annotation, but requires 2 arguments.  Event handler methods must require a single argument.

at net.minecraftforge.fml.common.eventhandler.EventBus.register(EventBus.java:72) ~[forgeSrc-1.8-11.14.1.1325.jar:?]

at common.zeroquest.ZeroQuest.load(ZeroQuest.java:98) ~[bin/:?]

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_25]

at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_25]

at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_25]

at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_25]

at net.minecraftforge.fml.common.FMLModContainer.handleModStateEvent(FMLModContainer.java:518) ~[forgeSrc-1.8-11.14.1.1325.jar:?]

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_25]

at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_25]

at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_25]

at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_25]

at com.google.common.eventbus.EventSubscriber.handleEvent(EventSubscriber.java:74) ~[guava-17.0.jar:?]

at com.google.common.eventbus.SynchronizedEventSubscriber.handleEvent(SynchronizedEventSubscriber.java:47) ~[guava-17.0.jar:?]

at com.google.common.eventbus.EventBus.dispatch(EventBus.java:322) ~[guava-17.0.jar:?]

at com.google.common.eventbus.EventBus.dispatchQueuedEvents(EventBus.java:304) ~[guava-17.0.jar:?]

at com.google.common.eventbus.EventBus.post(EventBus.java:275) ~[guava-17.0.jar:?]

at net.minecraftforge.fml.common.LoadController.sendEventToModContainer(LoadController.java:208) ~[forgeSrc-1.8-11.14.1.1325.jar:?]

at net.minecraftforge.fml.common.LoadController.propogateStateMessage(LoadController.java:187) ~[forgeSrc-1.8-11.14.1.1325.jar:?]

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_25]

at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_25]

at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_25]

at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_25]

at com.google.common.eventbus.EventSubscriber.handleEvent(EventSubscriber.java:74) ~[guava-17.0.jar:?]

at com.google.common.eventbus.SynchronizedEventSubscriber.handleEvent(SynchronizedEventSubscriber.java:47) ~[guava-17.0.jar:?]

at com.google.common.eventbus.EventBus.dispatch(EventBus.java:322) ~[guava-17.0.jar:?]

at com.google.common.eventbus.EventBus.dispatchQueuedEvents(EventBus.java:304) ~[guava-17.0.jar:?]

at com.google.common.eventbus.EventBus.post(EventBus.java:275) ~[guava-17.0.jar:?]

at net.minecraftforge.fml.common.LoadController.distributeStateMessage(LoadController.java:118) [LoadController.class:?]

at net.minecraftforge.fml.common.Loader.initializeMods(Loader.java:692) [Loader.class:?]

at net.minecraftforge.fml.client.FMLClientHandler.finishMinecraftLoading(FMLClientHandler.java:291) [FMLClientHandler.class:?]

at net.minecraft.client.Minecraft.startGame(Minecraft.java:521) [Minecraft.class:?]

at net.minecraft.client.Minecraft.run(Minecraft.java:356) [Minecraft.class:?]

at net.minecraft.client.main.Main.main(Main.java:117) [Main.class:?]

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_25]

at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_25]

at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_25]

at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_25]

at net.minecraft.launchwrapper.Launch.launch(Launch.java:135) [launchwrapper-1.11.jar:?]

at net.minecraft.launchwrapper.Launch.main(Launch.java:28) [launchwrapper-1.11.jar:?]

at net.minecraftforge.gradle.GradleStartCommon.launch(GradleStartCommon.java:85) [start/:?]

at GradleStart.main(GradleStart.java:45) [start/:?]

[19:40:59] [Client thread/INFO] [sTDOUT]: [net.minecraft.init.Bootstrap:printToSYSOUT:660]: ---- Minecraft Crash Report ----

 

 

Here is my code now

	@SubscribeEvent
public void blastResist(LivingHurtEvent event, EntityMetalZertum entity){
	if(event.entity instanceof EntityMetalZertum){
		this.entity = entity;

		if(event.source.isExplosion()){
			event.ammount = (float)(event.ammount /entity.getEntityAttribute(entity.blastResist).getAttributeValue());
		}
	}

	System.out.println(event.ammount);
}

Main Developer and Owner of Zero Quest

Visit the Wiki for more information

If I helped anyone, please give me a applaud and a thank you!

Link to comment
Share on other sites

You cant just add another variable to the method.

I would highly recommend you to learn more about java and how it works.

For now.

 

I guess you want to reduce the damage taken from an explosion if the affected entity is an EntityMetalZertum.

 

The event got the event.entity .

You can check if the entity is an instance of EntityMetalZertum and if it is an EntityMetalZertum you can cast it to one and then get the blastResist from it.

 

Link to comment
Share on other sites

Ok, I can't tell if it's working exactly but I do notice that it takes alot more grenades to kill it than with normal Zertums, also here is some debug numbers I got

 

[20:12:54] [server thread/INFO] [sTDOUT]: [common.zeroquest.command.CommandZeroQuest$TameModifier:modify:231]: Tamed! //Taming Metal Zertum
2.0
2.0
0.0
4.6

With a normal Zertum, it takes 2 grenades to kill it, but with this on the Metal Zertums, it takes like, 8 or 9 grenades.

Main Developer and Owner of Zero Quest

Visit the Wiki for more information

If I helped anyone, please give me a applaud and a thank you!

Link to comment
Share on other sites

It seems to work, because if the attribute is set to 0, it instantly kills the entity (also in the debug, it says "Infinity" instead of a number). When I hit the grenade with a regular Zertum, it says like 5.0 - 19.0, for Metal Zertums though (when the attribute is set to 5.0), it says 0.0 - 3.0.

 

===EDIT===

Yeah, it's working. Thanks!

Main Developer and Owner of Zero Quest

Visit the Wiki for more information

If I helped anyone, please give me a applaud and a thank you!

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



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • They were already updated, and just to double check I even did a cleanup and fresh update from that same page. I'm quite sure drivers are not the problem here. 
    • i tried downloading the drivers but it says no AMD graphics hardware has been detected    
    • Update your AMD/ATI drivers - get the drivers from their website - do not update via system  
    • As the title says i keep on crashing on forge 1.20.1 even without any mods downloaded, i have the latest drivers (nvidia) and vanilla minecraft works perfectly fine for me logs: https://pastebin.com/5UR01yG9
    • Hello everyone, I'm making this post to seek help for my modded block, It's a special block called FrozenBlock supposed to take the place of an old block, then after a set amount of ticks, it's supposed to revert its Block State, Entity, data... to the old block like this :  The problem I have is that the system breaks when handling multi blocks (I tried some fix but none of them worked) :  The bug I have identified is that the function "setOldBlockFields" in the item's "setFrozenBlock" function gets called once for the 1st block of multiblock getting frozen (as it should), but gets called a second time BEFORE creating the first FrozenBlock with the data of the 1st block, hence giving the same data to the two FrozenBlock :   Old Block Fields set BlockState : Block{minecraft:black_bed}[facing=east,occupied=false,part=head] BlockEntity : net.minecraft.world.level.block.entity.BedBlockEntity@73681674 BlockEntityData : id:"minecraft:bed",x:3,y:-60,z:-6} Old Block Fields set BlockState : Block{minecraft:black_bed}[facing=east,occupied=false,part=foot] BlockEntity : net.minecraft.world.level.block.entity.BedBlockEntity@6d1aa3da BlockEntityData : {id:"minecraft:bed",x:2,y:-60,z:-6} Frozen Block Entity set BlockState : Block{minecraft:black_bed}[facing=east,occupied=false,part=foot] BlockPos{x=3, y=-60, z=-6} BlockEntity : net.minecraft.world.level.block.entity.BedBlockEntity@6d1aa3da BlockEntityData : {id:"minecraft:bed",x:2,y:-60,z:-6} Frozen Block Entity set BlockState : Block{minecraft:black_bed}[facing=east,occupied=false,part=foot] BlockPos{x=2, y=-60, z=-6} BlockEntity : net.minecraft.world.level.block.entity.BedBlockEntity@6d1aa3da BlockEntityData : {id:"minecraft:bed",x:2,y:-60,z:-6} here is the code inside my custom "freeze" item :    @Override     public @NotNull InteractionResult useOn(@NotNull UseOnContext pContext) {         if (!pContext.getLevel().isClientSide() && pContext.getHand() == InteractionHand.MAIN_HAND) {             BlockPos blockPos = pContext.getClickedPos();             BlockPos secondBlockPos = getMultiblockPos(blockPos, pContext.getLevel().getBlockState(blockPos));             if (secondBlockPos != null) {                 createFrozenBlock(pContext, secondBlockPos);             }             createFrozenBlock(pContext, blockPos);             return InteractionResult.SUCCESS;         }         return super.useOn(pContext);     }     public static void createFrozenBlock(UseOnContext pContext, BlockPos blockPos) {         BlockState oldState = pContext.getLevel().getBlockState(blockPos);         BlockEntity oldBlockEntity = oldState.hasBlockEntity() ? pContext.getLevel().getBlockEntity(blockPos) : null;         CompoundTag oldBlockEntityData = oldState.hasBlockEntity() ? oldBlockEntity.serializeNBT() : null;         if (oldBlockEntity != null) {             pContext.getLevel().removeBlockEntity(blockPos);         }         BlockState FrozenBlock = setFrozenBlock(oldState, oldBlockEntity, oldBlockEntityData);         pContext.getLevel().setBlockAndUpdate(blockPos, FrozenBlock);     }     public static BlockState setFrozenBlock(BlockState blockState, @Nullable BlockEntity blockEntity, @Nullable CompoundTag blockEntityData) {         BlockState FrozenBlock = BlockRegister.FROZEN_BLOCK.get().defaultBlockState();         ((FrozenBlock) FrozenBlock.getBlock()).setOldBlockFields(blockState, blockEntity, blockEntityData);         return FrozenBlock;     }  
  • Topics

×
×
  • Create New...

Important Information

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