Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

[1.15.2] How do I create an item with power?


TallYate
 Share

Recommended Posts

Oh I've been trying to test the energy using /give ...{Energy:500}
 

public class ModCapabilityEnergy implements INBTSerializable
{
    @CapabilityInject(ModIEnergyStorage.class)
    public static Capability<ModIEnergyStorage> ENERGY = null;

    public static void register()
    {
        CapabilityManager.INSTANCE.register(ModIEnergyStorage.class, new IStorage<ModIEnergyStorage>()
        {
            @Override
            public INBT writeNBT(Capability<ModIEnergyStorage> capability, ModIEnergyStorage instance, Direction side)
            {
            	
            	CrudeTechMod.log("writeNBT " + IntNBT.valueOf(instance.getEnergyStored()));
                return IntNBT.valueOf(instance.getEnergyStored());
            }

            @Override
            public void readNBT(Capability<ModIEnergyStorage> capability, ModIEnergyStorage instance, Direction side, INBT nbt)
            {
                if (!(instance instanceof ModEnergyStorage))
                    throw new IllegalArgumentException("Can not deserialize to an instance that isn't the default implementation");
                ((ModEnergyStorage)instance).energy = ((IntNBT)nbt).getInt();
                CrudeTechMod.log("readNBT " + ((ModEnergyStorage)instance).energy);
            }
        },
        () -> new ModEnergyStorage(1000));
    }

	@Override
	public INBT serializeNBT() {
		
		return null;
	}

	@Override
	public void deserializeNBT(INBT nbt) {
		
	}

}

how would I do serializeNBT since there is no input

Also I see that I'm supposed to do something like this

CapabilityManager.INSTANCE.register(ModIEnergyStorage.class, ModCapabilityEnergy.ENERGY.getStorage(), ModEnergyStorage.class);

but:
The method register(Class<T>, Capability.IStorage<T>, Callable<? extends T>) in the type CapabilityManager is not applicable for the arguments (Class<ModIEnergyStorage>, Capability.IStorage<ModIEnergyStorage>, Class<ModEnergyStorage>)

How do I write the capability to the ItemStack?

Link to comment
Share on other sites

23 minutes ago, TallYate said:

public class ModCapabilityEnergy implements INBTSerializable

Why did you implement this here... This does not make any sense... And it is not what I told you. This is not your capability provider.

 

23 minutes ago, TallYate said:

Oh I've been trying to test the energy using /give ...{Energy:500}

That does not work. That is not how capability data is stored in ItemStacks.

Link to comment
Share on other sites

@Override
	public ICapabilityProvider initCapabilities(ItemStack stack, @Nullable CompoundNBT nbt) {
		return new ICapabilityProvider()  {
			protected ModEnergyStorage storage = newStorage();
			protected LazyOptional<ModEnergyStorage> storageHolder = LazyOptional.of(() -> storage);

			public <T> LazyOptional<T> getCapability(Capability<T> cap, Direction side) {
				if (cap == ModCapabilityEnergy.ENERGY) {
					return storageHolder.cast();
				}
				return LazyOptional.empty();
			}
		};
	}

How would I make this method in PowerArmor.java implement INBTSerializable, and attach it to the ItemStack?
Or should PowerArmor.java implement INBTSerializable
 

Also, do you know what I am supposed to put in here?

CapabilityManager.INSTANCE.register(ModIEnergyStorage.class, ModCapabilityEnergy.ENERGY.getStorage(), ModEnergyStorage.class);

The method register(Class<T>, Capability.IStorage<T>, Callable<? extends T>) in the type CapabilityManager is not applicable for the arguments (Class<ModIEnergyStorage>, Capability.IStorage<ModIEnergyStorage>, Class<ModEnergyStorage>)

Link to comment
Share on other sites

12 minutes ago, TallYate said:

How would I make this method

The method doesn't implement anything. Methods return things. You want the thing the method returns to implement that interface.

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

@SuppressWarnings("null")
	@Override
	public INBT serializeNBT() {
		
		
		DataOutput d = null;
		try {
			d.writeInt(this.energy);
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		IntNBT a = null;
		try {
			a.write(d);
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		return a;
	}

	@Override
	public void deserializeNBT(INBT nbt) {
		this.energy = ((IntNBT)nbt).getInt();
	}

I did serializeNBT and deserializeNBT, and added a thing so when I jump, I get 100 power.
It works, but when I open a chest, the power resets

https://github.com/TallYate/CrudeTechMod/tree/master/src/main/java/me/joshua/crudetechmod

Edited by TallYate
added github
Link to comment
Share on other sites

1 hour ago, loordgek said:

WHAT ??? Suppressing this will not work

ok I made it this:

@Override
	public INBT serializeNBT() {
		return new ModIntNBT(this.energy);
	}

(ModIntNBT is IntNBT but with a public constructor)

 

 

1 hour ago, loordgek said:

no retrun in initCapabilities ICapabilitySerializable

 

What do you mean. It returns storageHolder, which holds a ModEnergyStorage
 

public class ModEnergyStorage implements ModIEnergyStorage, INBTSerializable

 

Link to comment
Share on other sites

8 minutes ago, TallYate said:

ModIntNBT

...did you implement your own NBT class?

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, TallYate said:

so I'm not supposed to use a modified version?

Why would you have to? It literally makes no sense that the game would have to ask you to serialize something to a given format and not have a working implementation of that format to serialize to.

I'm not even sure the game would know how to serialize and deserialize your custom NBT classes properly.

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

Override
			public IntNBT serializeNBT() {
				IntNBT nbt = null;
				getCapability(ModCapabilityEnergy.ENERGY, null).ifPresent(handler -> {
					nbt = IntNBT.valueOf(handler.getEnergyStored());
				});
				return nbt;
			}

That needs a serializeNBT and deserializeNBT. Do you know how I can make the handler return the nbt? Because "nbt cannot be assigned, since it is defined in an enclosing type"

Link to comment
Share on other sites

@Override
public ICapabilitySerializable initCapabilities(ItemStack stack, @Nullable CompoundNBT nbt) {
	return new ICapabilitySerializable() {
		protected ModEnergyStorage storage = newStorage();
		protected LazyOptional<ModEnergyStorage> storageHolder = LazyOptional.of(() -> storage);

		public <T> LazyOptional<T> getCapability(Capability<T> cap, Direction side) {
			if (cap == ModCapabilityEnergy.ENERGY) {
				return storageHolder.cast();
			}
			return LazyOptional.empty();
		}

		
		public Integer a = null;
		
		public void setA(int x) {
			a = x;
		}

		@Override
		public IntNBT serializeNBT() {
			getCapability(ModCapabilityEnergy.ENERGY, null).ifPresent(handler -> {
				setA(handler.getEnergyStored());
			});
			return IntNBT.valueOf(a);
		}

		@Override
		public void deserializeNBT(INBT nbt) {
			getCapability(ModCapabilityEnergy.ENERGY, null).ifPresent(handler -> {
				handler.setEnergy(((IntNBT) nbt).getInt());
			});
		}
	};
}

is this ok?

Edited by TallYate
more context
Link to comment
Share on other sites

@Override
  public IntNBT serializeNBT() {
  int a = 0;
  getCapability(ModCapabilityEnergy.ENERGY, null).ifPresent(handler -> {
    a = handler.getEnergyStored();
  });
  return IntNBT.valueOf(a);
}

This doesn't work so I did that bad thing. Is there a better way that you know of?

Link to comment
Share on other sites

Dude, seriously.

You have access to the underlying storage directly, there's ZERO reason to use getCap and the lazy optional.

public IntNBT serializeNBT() {
    return IntNBT.valueOf(storage.getEnergy());
}

and

public void deserializeNBT(IntNBT nbt) {
    storage.setEnergy(nbt.getInt());
}

 

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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

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

 Share




  • Recently Browsing

    No registered users viewing this page.

  • Posts

    • Hi i'm currently working on an entity. I just i finished it but it won't spawn and i don't know why. I got this error : java.lang.NullPointerException: Cannot invoke "net.minecraft.world.entity.ai.attributes.AttributeSupplier.getValue(net.minecraft.world.entity.ai.attributes.Attribute)" because "this.supplier" is null at net.minecraft.world.entity.ai.attributes.AttributeMap.getValue(AttributeMap.java:65) ~[forge-1.17.1-37.0.103_mapped_official_1.17.1-recomp.jar%2375!:?] at net.minecraft.world.entity.LivingEntity.getAttributeValue(LivingEntity.java:1806) ~[forge-1.17.1-37.0.103_mapped_official_1.17.1-recomp.jar%2375!:?] at net.minecraft.world.entity.LivingEntity.getMaxHealth(LivingEntity.java:1571) ~[forge-1.17.1-37.0.103_mapped_official_1.17.1-recomp.jar%2375!:?] at net.minecraft.world.entity.LivingEntity.<init>(LivingEntity.java:230) ~[forge-1.17.1-37.0.103_mapped_official_1.17.1-recomp.jar%2375!:?] at net.minecraft.world.entity.Mob.<init>(Mob.java:112) ~[forge-1.17.1-37.0.103_mapped_official_1.17.1-recomp.jar%2375!:?] at com.armanse.dreamy.entity.SleepingPlayerEntity.<init>(SleepingPlayerEntity.java:16) ~[%2379!:?] at net.minecraft.world.entity.EntityType.create(EntityType.java:460) ~[forge-1.17.1-37.0.103_mapped_official_1.17.1-recomp.jar%2375!:?] at net.minecraft.world.entity.EntityType.lambda$create$1(EntityType.java:470) ~[forge-1.17.1-37.0.103_mapped_official_1.17.1-recomp.jar%2375!:?] at java.util.Optional.map(Optional.java:260) ~[?:?] at net.minecraft.world.entity.EntityType.create(EntityType.java:469) ~[forge-1.17.1-37.0.103_mapped_official_1.17.1-recomp.jar%2375!:?] at net.minecraft.world.entity.EntityType.loadStaticEntity(EntityType.java:552) ~[forge-1.17.1-37.0.103_mapped_official_1.17.1-recomp.jar%2375!:?] at net.minecraft.world.entity.EntityType.loadEntityRecursive(EntityType.java:508) ~[forge-1.17.1-37.0.103_mapped_official_1.17.1-recomp.jar%2375!:?] at net.minecraft.server.commands.SummonCommand.spawnEntity(SummonCommand.java:50) ~[forge-1.17.1-37.0.103_mapped_official_1.17.1-recomp.jar%2375!:?] at net.minecraft.server.commands.SummonCommand.lambda$register$1(SummonCommand.java:34) ~[forge-1.17.1-37.0.103_mapped_official_1.17.1-recomp.jar%2375!:?] at com.mojang.brigadier.CommandDispatcher.execute(CommandDispatcher.java:263) ~[brigadier-1.0.18.jar%2341!:?] at net.minecraft.commands.Commands.performCommand(Commands.java:224) ~[forge-1.17.1-37.0.103_mapped_official_1.17.1-recomp.jar%2375!:?] at net.minecraft.server.network.ServerGamePacketListenerImpl.handleCommand(ServerGamePacketListenerImpl.java:1130) ~[forge-1.17.1-37.0.103_mapped_official_1.17.1-recomp.jar%2375!:?] at net.minecraft.server.network.ServerGamePacketListenerImpl.handleChat(ServerGamePacketListenerImpl.java:1109) ~[forge-1.17.1-37.0.103_mapped_official_1.17.1-recomp.jar%2375!:?] at net.minecraft.server.network.ServerGamePacketListenerImpl.handleChat(ServerGamePacketListenerImpl.java:1095) ~[forge-1.17.1-37.0.103_mapped_official_1.17.1-recomp.jar%2375!:?] at net.minecraft.network.protocol.game.ServerboundChatPacket.handle(ServerboundChatPacket.java:27) ~[forge-1.17.1-37.0.103_mapped_official_1.17.1-recomp.jar%2375!:?] at net.minecraft.network.protocol.game.ServerboundChatPacket.handle(ServerboundChatPacket.java:6) ~[forge-1.17.1-37.0.103_mapped_official_1.17.1-recomp.jar%2375!:?] at net.minecraft.network.protocol.PacketUtils.lambda$ensureRunningOnSameThread$0(PacketUtils.java:21) ~[forge-1.17.1-37.0.103_mapped_official_1.17.1-recomp.jar%2375!:?] at net.minecraft.server.TickTask.run(TickTask.java:17) ~[forge-1.17.1-37.0.103_mapped_official_1.17.1-recomp.jar%2375!:?] at net.minecraft.util.thread.BlockableEventLoop.doRunTask(BlockableEventLoop.java:139) ~[forge-1.17.1-37.0.103_mapped_official_1.17.1-recomp.jar%2375!:?] at net.minecraft.util.thread.ReentrantBlockableEventLoop.doRunTask(ReentrantBlockableEventLoop.java:22) ~[forge-1.17.1-37.0.103_mapped_official_1.17.1-recomp.jar%2375!:?] at net.minecraft.server.MinecraftServer.doRunTask(MinecraftServer.java:775) ~[forge-1.17.1-37.0.103_mapped_official_1.17.1-recomp.jar%2375!:?] at net.minecraft.server.MinecraftServer.doRunTask(MinecraftServer.java:162) ~[forge-1.17.1-37.0.103_mapped_official_1.17.1-recomp.jar%2375!:?] at net.minecraft.util.thread.BlockableEventLoop.pollTask(BlockableEventLoop.java:112) ~[forge-1.17.1-37.0.103_mapped_official_1.17.1-recomp.jar%2375!:?] at net.minecraft.server.MinecraftServer.pollTaskInternal(MinecraftServer.java:758) ~[forge-1.17.1-37.0.103_mapped_official_1.17.1-recomp.jar%2375!:?] at net.minecraft.server.MinecraftServer.pollTask(MinecraftServer.java:752) ~[forge-1.17.1-37.0.103_mapped_official_1.17.1-recomp.jar%2375!:?] at net.minecraft.util.thread.BlockableEventLoop.managedBlock(BlockableEventLoop.java:122) ~[forge-1.17.1-37.0.103_mapped_official_1.17.1-recomp.jar%2375!:?] at net.minecraft.server.MinecraftServer.waitUntilNextTick(MinecraftServer.java:738) ~[forge-1.17.1-37.0.103_mapped_official_1.17.1-recomp.jar%2375!:?] at net.minecraft.server.MinecraftServer.runServer(MinecraftServer.java:687) ~[forge-1.17.1-37.0.103_mapped_official_1.17.1-recomp.jar%2375!:?] at net.minecraft.server.MinecraftServer.lambda$spin$2(MinecraftServer.java:258) ~[forge-1.17.1-37.0.103_mapped_official_1.17.1-recomp.jar%2375!:?] at java.lang.Thread.run(Thread.java:831) [?:?] I was wondering if the problem was with the attributes of the entity since it is mention at the top but i couldn't find the error (i'm a noob so that's kinda normal). Here is the code related to the attributes: @EventBusSubscriber(modid = DreamyMain.MOD_ID, bus = Bus.FORGE) public class ClientInitEvent { @SubscribeEvent public static final void entityAttributCreation(EntityAttributeCreationEvent event) { event.put(EntityInit.Entity.get(), Entity.createAttributes().build()); } @SubscribeEvent public static final void entityClientRederer(EntityRenderersEvent.RegisterRenderers event) { event.registerEntityRenderer(EntityInit.Entity.get(), Entity::new); } }   //IN The entity class public static AttributeSupplier.Builder createAttributes() { return Mob.createMobAttributes().add(Attributes.MAX_HEALTH, 20D).add(Attributes.MOVEMENT_SPEED, 0D); }  
    • I removed a bunch of mods that said other version than 1.16.5 seems to be working. Still its kinda weird because it clearly said that the "game version" was 1.16.5. Still not happy bout it  
    • From the looks of it LoomContainer still doesn't call onCraftMatrixChanged... What am i missing seriously?
  • Topics

  • Who's Online (See full list)

×
×
  • Create New...

Important Information

By using this site, you agree to our Privacy Policy.