Jump to content

Broken Capability


eggpasta

Recommended Posts

Ok so i have a capability with the following code but when i try to use it i get an error

public class SizeCapability {
	@CapabilityInject(Size.class)
	public static Capability<Size> CAPABILITY_SIZE = null;
	public static void register() {
		CapabilityManager.INSTANCE.register(Size.class,new Size.SizeNBTStorage(),Size::createADefaultInstance);
	}
}

public class Size {
	public Size() {
		this(2);
	}
	public Size(int initialsizelevel) {
		sizeLevel = initialsizelevel;
	}
	public void setSizeLevel(int sizeLevelToSet) {sizeLevel = sizeLevelToSet;}
	private int sizeLevel;
	public int getSizeLevel() {return sizeLevel;}
	public static class SizeNBTStorage implements Capability.IStorage<Size> {

		@Override
		public INBT writeNBT(Capability<Size> capability, Size instance, Direction side) {
			IntNBT intNBT = IntNBT.valueOf(instance.sizeLevel);
			return intNBT;
		}

		@Override
		public void readNBT(Capability<Size> capability, Size instance, Direction side, INBT nbt) {
			int sizeLevel = 2;
			if (nbt.getType() == IntNBT.TYPE) {
				sizeLevel = ((IntNBT)nbt).getAsInt();
			}
			instance.setSizeLevel(sizeLevel);
			
		}
		
	}
	public static Size createADefaultInstance() {
		return new Size();
	}
}
public class CapabilityProviderEntities implements ICapabilitySerializable<INBT> {
	private final Direction NO_SPECIFIC_SIDE = null;
	@Override
	public <T> LazyOptional<T> getCapability(@Nonnull Capability<T> cap,@Nullable Direction side) {
		if (SizeCapability.CAPABILITY_SIZE == cap) {
			return (LazyOptional<T>)LazyOptional.of(() -> sizeCap);
		}
		return LazyOptional.empty();
	}
	private final static String SIZE_NBT = "size";
	@Override
	public INBT serializeNBT() {
		CompoundNBT nbt = new CompoundNBT();
		INBT sizeNBT = SizeCapability.CAPABILITY_SIZE.writeNBT(sizeCap, NO_SPECIFIC_SIDE);
		nbt.put(SIZE_NBT, sizeNBT);
		return nbt;
	}
	private Size sizeCap = new Size();
	@Override
	public void deserializeNBT(INBT nbt) {
		if(nbt.getId() != NBTtypesMBE.COMPOUND_NBT_ID) {
			return;
		}
		CompoundNBT compound = (CompoundNBT)nbt;
		INBT sizeNBT = compound.get(SIZE_NBT);
		SizeCapability.CAPABILITY_SIZE.readNBT(sizeCap, NO_SPECIFIC_SIDE, sizeNBT);
		// TODO Auto-generated method stub
		
	}

}
Size size = entityLiving.getCapability(SizeCapability.CAPABILITY_SIZE).orElse(null);
		size.setSizeLevel(5);
[08:15:06] [Server thread/ERROR] [minecraft/MinecraftServer]: Encountered an unexpected exception
net.minecraft.crash.ReportedException: Ticking player
	at net.minecraft.network.NetworkSystem.tick(NetworkSystem.java:137) ~[forge:?] {re:classloading}
	at net.minecraft.server.MinecraftServer.tickChildren(MinecraftServer.java:865) ~[forge:?] {re:classloading,pl:accesstransformer:B}
	at net.minecraft.server.MinecraftServer.tickServer(MinecraftServer.java:787) ~[forge:?] {re:classloading,pl:accesstransformer:B}
	at net.minecraft.server.integrated.IntegratedServer.tickServer(IntegratedServer.java:78) ~[forge:?] {re:classloading,pl:runtimedistcleaner:A}
	at net.minecraft.server.MinecraftServer.runServer(MinecraftServer.java:642) ~[forge:?] {re:classloading,pl:accesstransformer:B}
	at net.minecraft.server.MinecraftServer.lambda$spin$0(MinecraftServer.java:232) ~[forge:?] {re:classloading,pl:accesstransformer:B}
	at java.lang.Thread.run(Thread.java:832) [?:?] {}
Caused by: java.lang.NullPointerException: Cannot invoke "mod.trianglesinpoo.mcores.sizechanging.Size.setSizeLevel(int)" because "size" is null
	at mod.trianglesinpoo.mcores.CRYOCOCKTAIL.finishUsingItem(CRYOCOCKTAIL.java:38) ~[?:?] {re:classloading}
	at net.minecraft.item.ItemStack.finishUsingItem(ItemStack.java:221) ~[forge:?] {re:classloading,xf:fml:forge:filled_map.4,xf:fml:forge:itemstack}
	at net.minecraft.entity.LivingEntity.completeUsingItem(LivingEntity.java:2843) ~[forge:?] {re:classloading}
	at net.minecraft.entity.player.ServerPlayerEntity.completeUsingItem(ServerPlayerEntity.java:1057) ~[forge:?] {re:classloading,pl:accesstransformer:B}
	at net.minecraft.entity.LivingEntity.updatingUsingItem(LivingEntity.java:2723) ~[forge:?] {re:classloading}
	at net.minecraft.entity.LivingEntity.tick(LivingEntity.java:2114) ~[forge:?] {re:classloading}
	at net.minecraft.entity.player.PlayerEntity.tick(PlayerEntity.java:223) ~[forge:?] {re:classloading,pl:accesstransformer:B}
	at net.minecraft.entity.player.ServerPlayerEntity.doTick(ServerPlayerEntity.java:404) ~[forge:?] {re:classloading,pl:accesstransformer:B}
	at net.minecraft.network.play.ServerPlayNetHandler.tick(ServerPlayNetHandler.java:207) ~[forge:?] {re:classloading}
	at net.minecraft.network.NetworkManager.tick(NetworkManager.java:226) ~[forge:?] {re:classloading}
	at net.minecraft.network.NetworkSystem.tick(NetworkSystem.java:134) ~[forge:?] {re:classloading}
	... 6 more
[08:15:06] [Render thread/FATAL] [minecraft/ThreadTaskExecutor]: Error executing task on Client
java.lang.NullPointerException: Cannot invoke "mod.trianglesinpoo.mcores.sizechanging.Size.setSizeLevel(int)" because "size" is null
	at mod.trianglesinpoo.mcores.CRYOCOCKTAIL.finishUsingItem(CRYOCOCKTAIL.java:38) ~[main/:?] {re:classloading}
	at net.minecraft.item.ItemStack.finishUsingItem(ItemStack.java:221) ~[forge-1.16.5-36.1.4_mapped_official_1.16.5-recomp.jar:?] {re:classloading,xf:fml:forge:filled_map.4,xf:fml:forge:itemstack}
	at net.minecraft.entity.LivingEntity.completeUsingItem(LivingEntity.java:2843) ~[forge-1.16.5-36.1.4_mapped_official_1.16.5-recomp.jar:?] {re:classloading}
	at net.minecraft.entity.player.PlayerEntity.handleEntityEvent(PlayerEntity.java:415) ~[forge-1.16.5-36.1.4_mapped_official_1.16.5-recomp.jar:?] {re:classloading,pl:accesstransformer:B}
	at net.minecraft.client.entity.player.ClientPlayerEntity.handleEntityEvent(ClientPlayerEntity.java:447) ~[forge-1.16.5-36.1.4_mapped_official_1.16.5-recomp.jar:?] {re:classloading,pl:runtimedistcleaner:A}
	at net.minecraft.client.network.play.ClientPlayNetHandler.handleEntityEvent(ClientPlayNetHandler.java:958) ~[forge-1.16.5-36.1.4_mapped_official_1.16.5-recomp.jar:?] {re:classloading,pl:runtimedistcleaner:A}
	at net.minecraft.network.play.server.SEntityStatusPacket.handle(SEntityStatusPacket.java:35) ~[forge-1.16.5-36.1.4_mapped_official_1.16.5-recomp.jar:?] {re:classloading}
	at net.minecraft.network.play.server.SEntityStatusPacket.handle(SEntityStatusPacket.java:12) ~[forge-1.16.5-36.1.4_mapped_official_1.16.5-recomp.jar:?] {re:classloading}
	at net.minecraft.network.PacketThreadUtil.lambda$ensureRunningOnSameThread$0(PacketThreadUtil.java:19) ~[forge-1.16.5-36.1.4_mapped_official_1.16.5-recomp.jar:?] {re:classloading}
	at net.minecraft.util.concurrent.ThreadTaskExecutor.doRunTask(ThreadTaskExecutor.java:136) ~[forge-1.16.5-36.1.4_mapped_official_1.16.5-recomp.jar:?] {re:classloading,pl:accesstransformer:B}
	at net.minecraft.util.concurrent.RecursiveEventLoop.doRunTask(RecursiveEventLoop.java:22) ~[forge-1.16.5-36.1.4_mapped_official_1.16.5-recomp.jar:?] {re:classloading}
	at net.minecraft.util.concurrent.ThreadTaskExecutor.pollTask(ThreadTaskExecutor.java:109) ~[forge-1.16.5-36.1.4_mapped_official_1.16.5-recomp.jar:?] {re:classloading,pl:accesstransformer:B}
	at net.minecraft.util.concurrent.ThreadTaskExecutor.runAllTasks(ThreadTaskExecutor.java:97) ~[forge-1.16.5-36.1.4_mapped_official_1.16.5-recomp.jar:?] {re:classloading,pl:accesstransformer:B}
	at net.minecraft.client.Minecraft.runTick(Minecraft.java:947) ~[forge-1.16.5-36.1.4_mapped_official_1.16.5-recomp.jar:?] {re:classloading,pl:accesstransformer:B,pl:runtimedistcleaner:A}
	at net.minecraft.client.Minecraft.run(Minecraft.java:607) ~[forge-1.16.5-36.1.4_mapped_official_1.16.5-recomp.jar:?] {re:classloading,pl:accesstransformer:B,pl:runtimedistcleaner:A}
	at net.minecraft.client.main.Main.main(Main.java:184) ~[forge-1.16.5-36.1.4_mapped_official_1.16.5-recomp.jar:?] {re:classloading,pl:runtimedistcleaner:A}
	at jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:?] {}
	at jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:?] {}
	at jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:?] {}
	at java.lang.reflect.Method.invoke(Method.java:564) ~[?:?] {}
	at net.minecraftforge.userdev.FMLUserdevClientLaunchProvider.lambda$launchService$0(FMLUserdevClientLaunchProvider.java:52) ~[forge-1.16.5-36.1.4_mapped_official_1.16.5-recomp.jar:?] {}
	at cpw.mods.modlauncher.LaunchServiceHandlerDecorator.launch(LaunchServiceHandlerDecorator.java:37) [modlauncher-8.0.9.jar:?] {}
	at cpw.mods.modlauncher.LaunchServiceHandler.launch(LaunchServiceHandler.java:54) [modlauncher-8.0.9.jar:?] {}
	at cpw.mods.modlauncher.LaunchServiceHandler.launch(LaunchServiceHandler.java:72) [modlauncher-8.0.9.jar:?] {}
	at cpw.mods.modlauncher.Launcher.run(Launcher.java:82) [modlauncher-8.0.9.jar:?] {}
	at cpw.mods.modlauncher.Launcher.main(Launcher.java:66) [modlauncher-8.0.9.jar:?] {}
	at net.minecraftforge.userdev.LaunchTesting.main(LaunchTesting.java:105) [forge-1.16.5-36.1.4_mapped_official_1.16.5-recomp.jar:?] {}
AL lib: (EE) alc_cleanup: 1 device not closed

 

Link to comment
Share on other sites

33 minutes ago, eggpasta said:

Size size = entityLiving.getCapability(SizeCapability.CAPABILITY_SIZE).orElse(null);

size.setSizeLevel(5);

You should use ifPresent (if there's a likelyhood that the capability won't be present) or use orElseThrow (if you know the capability should always be present) rather than orElse(null) and not null-check.

Your problem is that you never attach the capability to any entities and your CapabilityProviderEntities class is both (a) unnecessary because entities are already capability providers and (b) unused, as your code never calls any of its methods.

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

1 minute ago, Draco18s said:

You should use ifPresent (if there's a likelyhood that the capability won't be present) or use orElseThrow (if you know the capability should always be present) rather than orElse(null) and not null-check.

Your problem is that you never attach the capability to any entities and your CapabilityProviderEntities class is both (a) unnecessary because entities are already capability providers and (b) unused, as your code never calls any of its methods.

How do i attach the capability?

Link to comment
Share on other sites

Just now, eggpasta said:

How do i attach the capability?

I had a class i forgot to show

public class CustomSizeAddEvent {
	@SubscribeEvent
	public static void attachSizeToEntityHandler(AttachCapabilitiesEvent<Entity> event) {
		Entity entity = event.getObject();
		if(entity instanceof LivingEntity) {
			event.addCapability(new ResourceLocation(Main.MODID,"size_capability"), new CapabilityProviderEntities());
		}
	}
}

 

Link to comment
Share on other sites

7 minutes ago, eggpasta said:

event.addCapability(new ResourceLocation(Main.MODID,"size_capability"), new CapabilityProviderEntities());

45 minutes ago, eggpasta said:

@CapabilityInject(Size.class)

public static Capability<Size> CAPABILITY_SIZE = null;

Your CapabilityProviderEntities is not a Size capability.

 

Also I don't think your event handler is properly registered with the event bus.

Edited by Draco18s

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

12 minutes ago, Draco18s said:

Your CapabilityProviderEntities is not a Size capability.

 

Also I don't think your event handler is properly registered with the event bus.

MinecraftForge.EVENT_BUS.register(CapabilityProviderEntities.class);

 

13 minutes ago, Draco18s said:

Your CapabilityProviderEntities is not a Size capability.

Its supposed to provide the Size capability, should i just change it to SizeCapability?

Link to comment
Share on other sites

1 hour ago, eggpasta said:
1 hour ago, Draco18s said:

Also I don't think your event handler is properly registered with the event bus.


MinecraftForge.EVENT_BUS.register(CapabilityProviderEntities.class);

That does not register the CustomSizeAddEvent class....

Nor does your CapabilityProviderEntities class have any event handling methods....

1 hour ago, eggpasta said:

Its supposed to provide the Size capability, should i just change it to SizeCapability?

In order to reference your CAPABILITY_SIZE compatible capability you need to attach a CAPABILITY_SIZE compatible capability to the entity. Your SizeCapability class is not your capability, it is merely a static class that holds a reference to the CAPABILITY_SIZE registry entry, similiar to ModBlocks or ModItems. That is, your class names are awful.

Size -> your capability
SizeCapability -> your registry object reference
CapabilityProviderEntities -> a capability provider (that is completely useless)
CustomSizeAddEvent -> your event handler

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

2 hours ago, Draco18s said:

That does not register the CustomSizeAddEvent class....

Nor does your CapabilityProviderEntities class have any event handling methods....

In order to reference your CAPABILITY_SIZE compatible capability you need to attach a CAPABILITY_SIZE compatible capability to the entity. Your SizeCapability class is not your capability, it is merely a static class that holds a reference to the CAPABILITY_SIZE registry entry, similiar to ModBlocks or ModItems. That is, your class names are awful.

Size -> your capability
SizeCapability -> your registry object reference
CapabilityProviderEntities -> a capability provider (that is completely useless)
CustomSizeAddEvent -> your event handler

https://gist.github.com/williewillus/c8dc2a1e7963b57ef436c699f25a710d

Link to comment
Share on other sites

4 hours ago, eggpasta said:
Quote

An instance of IStorage (wha??? hang in there, I'll explain)

You don't have this. Not strictly needed, as you're registering the implementation class directly which is fine. You just can't share your Capability to other mods, IIRC. You will still need to implement the serialization interfaces, however.

Quote

Entities and TileEntities

You simply need to override hasCapability and getCapability, as the two classes mentioned all implement ICapabilityProvider. You do NOT need to supply your own ICapabilityProvider like in the above cases.

This is why your CapabilityProviderEntities is useless. You don't need it. Entities already are ICapabilityProviders.

Also most of that ICapabilityProvider information is out of date:

Quote

CapabilityManager.INSTANCE.register(capability interface class, storage, default implementation factory);

Size::new is sufficient.

Edited by Draco18s

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

3 minutes ago, Draco18s said:

You don't have this. Not strictly needed, as you're registering the implementation class directly which is fine. You just can't share your Capability to other mods, IIRC. You will still need to implement the serialization interfaces, however.

This is why your CapabilityProviderEntities is useless. You don't need it. Entities already are ICapabilityProviders.

Also most of that ICapabilityProvider information is out of date:

Size::new is sufficient.

Thanks, after changing some stuff, it now works

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



×
×
  • Create New...

Important Information

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