Jump to content

Recommended Posts

Posted

i just hava a custom item which is called damage star

This item has a tag ("damage") if the player has the item in their inventory and the player attacks an entity.

Couter damage is increased by 1. If the player attacks, the damage the player deal plus the value of the tag.

this is the star item:

public class DamageStar extends Star {

	public DamageStar() {
		
		super(new Item.Properties().group(Cave.MISC), StarType.DAMAGE);
		
	}
	
	@Override
	public void starTick(ItemStack stack, World world, PlayerEntity player) {
		
		// custom method in Star
		
	}
	
	@Override
	public void onCreated(ItemStack stack, World worldIn, PlayerEntity playerIn) {
		
		Cave.LOGGER.debug("creat tag");
		stack.getOrCreateChildTag(this.getTag()).putDouble(this.getTag(), 0);
		
	}

}

 

and this the event i use to calc the tag:

	@SubscribeEvent
	public static void LivingDamage(LivingDamageEvent event) {
		
		Entity target = event.getEntity();
		Entity entity = event.getSource().getTrueSource();
		float amount = event.getAmount();
		float newAmount = 0.0f;
		
		if (entity instanceof PlayerEntity) {
			
			PlayerEntity player = (PlayerEntity) entity;

			if (PlayerManager.getItem(player, ModItems.DAMAGE_STAR.get()).getItem() != ItemStack.EMPTY.getItem()) {
				
				ItemStack stack = PlayerManager.getItem(player, ModItems.DAMAGE_STAR.get());
				
				if (stack.getItem() instanceof DamageStar) {
					
					DamageStar star = (DamageStar) stack.getItem();
					//gets the TagName from the Enum StarType
					final String tag = star.getTag();
					
					if (!stack.hasTag()) {
						
						stack.getOrCreateChildTag(tag);
						Cave.LOGGER.debug("creat tag");
						
					}
					
					CompoundNBT nbt = stack.getChildTag(tag);
					Cave.LOGGER.debug("getDouble: " + nbt.getDouble(tag));
					newAmount += nbt.getDouble(tag);
					nbt.putDouble(tag, nbt.getDouble(tag) + 1);
					stack.setTag(nbt);
					
					Cave.LOGGER.debug("getDouble: " + nbt.getDouble(tag));

				}
				
			}
			
			event.setAmount(newAmount == 0.0f ? amount : newAmount);
			
		}
		
	}

 

but now my problem i got an error but i dont knwo why:

java.lang.NullPointerException: Cannot invoke "net.minecraft.nbt.CompoundNBT.getDouble(String)" because "nbt" is null
	at net.luis.cave.events.entity.living.OnLivingDamageEvent.LivingDamage(OnLivingDamageEvent.java:72) ~[main/:?] {re:classloading}
	at net.minecraftforge.eventbus.ASMEventHandler_20_OnLivingDamageEvent_LivingDamage_LivingDamageEvent.invoke(.dynamic) ~[?:?] {}
	at net.minecraftforge.eventbus.ASMEventHandler.invoke(ASMEventHandler.java:85) ~[eventbus-4.0.0.jar:?] {}
	at net.minecraftforge.eventbus.EventBus.post(EventBus.java:302) ~[eventbus-4.0.0.jar:?] {}
	at net.minecraftforge.eventbus.EventBus.post(EventBus.java:283) ~[eventbus-4.0.0.jar:?] {}
	at net.minecraftforge.common.ForgeHooks.onLivingDamage(ForgeHooks.java:349) ~[forge:?] {re:classloading}
	at net.minecraft.entity.LivingEntity.damageEntity(LivingEntity.java:1578) ~[forge:?] {re:classloading}
	at net.minecraft.entity.LivingEntity.attackEntityFrom(LivingEntity.java:1043) ~[forge:?] {re:classloading}
	at net.minecraft.entity.passive.IronGolemEntity.attackEntityFrom(IronGolemEntity.java:210) ~[forge:?] {re:classloading}
	at net.minecraft.entity.player.PlayerEntity.attackTargetEntityWithCurrentItem(PlayerEntity.java:1167) ~[forge:?] {re:classloading,pl:accesstransformer:B}
	at net.minecraft.entity.player.ServerPlayerEntity.attackTargetEntityWithCurrentItem(ServerPlayerEntity.java:1399) ~[forge:?] {re:classloading,pl:accesstransformer:B}
	at net.minecraft.network.play.ServerPlayNetHandler.processUseEntity(ServerPlayNetHandler.java:1255) ~[forge:?] {re:classloading}
	at net.minecraft.network.play.client.CUseEntityPacket.processPacket(CUseEntityPacket.java:89) ~[forge:?] {re:classloading}
	at net.minecraft.network.play.client.CUseEntityPacket.processPacket(CUseEntityPacket.java:15) ~[forge:?] {re:classloading}
	at net.minecraft.network.PacketThreadUtil.lambda$checkThreadAndEnqueue$0(PacketThreadUtil.java:19) ~[forge:?] {re:classloading}
	at net.minecraft.util.concurrent.TickDelayedTask.run(TickDelayedTask.java:20) ~[forge:?] {re:classloading}
	at net.minecraft.util.concurrent.ThreadTaskExecutor.run(ThreadTaskExecutor.java:139) ~[forge:?] {re:classloading,pl:accesstransformer:B}
	at net.minecraft.util.concurrent.RecursiveEventLoop.run(RecursiveEventLoop.java:22) ~[forge:?] {re:classloading}
	at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:758) ~[forge:?] {re:classloading,pl:accesstransformer:B}
	at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:159) ~[forge:?] {re:classloading,pl:accesstransformer:B}
	at net.minecraft.util.concurrent.ThreadTaskExecutor.driveOne(ThreadTaskExecutor.java:109) ~[forge:?] {re:classloading,pl:accesstransformer:B}
	at net.minecraft.server.MinecraftServer.driveOneInternal(MinecraftServer.java:741) ~[forge:?] {re:classloading,pl:accesstransformer:B}
	at net.minecraft.server.MinecraftServer.driveOne(MinecraftServer.java:735) ~[forge:?] {re:classloading,pl:accesstransformer:B}
	at net.minecraft.util.concurrent.ThreadTaskExecutor.drainTasks(ThreadTaskExecutor.java:97) ~[forge:?] {re:classloading,pl:accesstransformer:B}
	at net.minecraft.server.MinecraftServer.runScheduledTasks(MinecraftServer.java:720) ~[forge:?] {re:classloading,pl:accesstransformer:B}
	at net.minecraft.server.MinecraftServer.func_240802_v_(MinecraftServer.java:667) ~[forge:?] {re:classloading,pl:accesstransformer:B}
	at net.minecraft.server.MinecraftServer.lambda$startServer$0(MinecraftServer.java:233) ~[forge:?] {re:classloading,pl:accesstransformer:B}
	at java.lang.Thread.run(Thread.java:832) [?:?] {}

 

Posted
10 minutes ago, diesieben07 said:

NullPointerException

i know but why is the tag null it can't be null because

it will set before attacking the last entity so i don't understand why the tag is Empty

 

do I have to save the tag

Posted
1 hour ago, diesieben07 said:

Your code for NBT handling is needlessly complicated and faulty. If the stack has a tag, but the child tag does not yet exist, you will not attempt to create the child tag, but then try to use it regardless.

However you don't need to do all this logic. Simply call getOrCreateChildTag every time and have it be the only method you call. It will handle all this logic of lazy creation of tags for you, that's its job.

okay thanks that work

  • 1 year later...
Posted
On 2/12/2021 at 3:28 AM, diesieben07 said:

You are even running a modern Java version... it tells you in plain English why it threw a NullPointerException:

 

"Cannot invoke "net.minecraft.nbt.CompoundNBT.getDouble(String)" because "nbt" is null"

Ah yes, plain English.

Think I'm running low on "net.minecraft.nbt.CompoundNBT.getDouble(String)", better run to the store and get some.

  • Haha 1
Posted
34 minutes ago, X66Herobrine66X said:

"Cannot invoke "net.minecraft.nbt.CompoundNBT.getDouble(String)" because "nbt" is null"

Ah yes, plain English.

Think I'm running low on "net.minecraft.nbt.CompoundNBT.getDouble(String)", better run to the store and get some.

You joke, but it's actually a pretty reasonable error

That's pretty good as far as errors go, it tells you what exactly is null, and what function caused it, from there you just create a breakpoint before it's called and watch to see what's missing. Could have something like this lmao:
wo8aZ.jpg

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.