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

1 hour ago, Draco18s said:

Capabilities aren't sync'd to the client automatically.

 

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
	@Nullable
	public CompoundNBT getShareTag(ItemStack stack) {
		CompoundNBT tag = stack.getOrCreateTag();
		stack.getCapability(CapabilityEnergy.ENERGY, null).ifPresent(handler -> {tag.putInt("Energy", stack.getCapability(CapabilityEnergy.ENERGY).orElseGet(null).getEnergyStored());});
		return tag;
	}
	
	@Override
	public void readShareTag(ItemStack stack, @Nullable CompoundNBT nbt)
    {
        stack.setTag(nbt);
        int energy = stack.getCapability(CapabilityEnergy.ENERGY, null).orElse(null).getEnergyStored();
        energy=nbt.getInt("Energy");
    }

I feel like I have done the readShareTag part wrong

Link to comment
Share on other sites

2 hours ago, TallYate said:

ifPresent(handler

handler is your capability instance, you don't need to call getCapability inside here.

2 hours ago, TallYate said:

stack.setTag(nbt);

I don't know why you're doing this.

 

2 hours ago, TallYate said:

stack.getCapability

Use ifPresent

  

2 hours ago, TallYate said:

int energy = ...

energy=nbt.getInt("Energy");

Yes, that's it, just throw the value into a local variable, I'm sure nothing could go wrong.

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

@Override
	@Nullable
	public CompoundNBT getShareTag(ItemStack stack) {
		CompoundNBT tag = stack.getOrCreateTag();
		stack.getCapability(CapabilityEnergy.ENERGY, null).ifPresent(handler -> {tag.putInt("Energy", handler.getEnergyStored());});
		return tag;
	}
	
	@Override
	public void readShareTag(ItemStack stack, @Nullable CompoundNBT nbt)
    {
        stack.getCapability(CapabilityEnergy.ENERGY, null).ifPresent(handler -> {nbt.putInt("Energy", handler.getEnergyStored());});
    }

ok I've done this.
I did the local variable thing because I thought it was a reference that I could change.
What am I actually supposed to do in readShareTag?

Link to comment
Share on other sites

I'm sure that when you're READING its a great idea to WRITE.

Try READING instead.

Hint, the methods are supposed to be mirrors. In write you get the value FROM the capability, therefor in read you should...

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

I think I have to do something like
handler.getEnergyStored()=nbt.getInt("Energy");

but getEnergyStored() returns a value and not a variable/reference, and I don't see any other methods to write to the capability.

Edited by TallYate
changed last sentence
Link to comment
Share on other sites

Gosh if there was only some other method that accepted a value instead of returning it.

Hint: You can add one to your implementation, even if it is not specified by the interface

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

You implemented the interface somewhere, yes?

Use polymorphism to your advantage.

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

23 minutes ago, TallYate said:

My Armor Item doesn’t implement IEnergyStorage, I think that came from CapabilityEnergy.ENERGY

Those are two different things. IEnergyStorage is the interface that defines the storage of the data, CapabilityEnergy.ENERGY is the type of capability.

Anyway, the class I was referring to is one that Forge created (EnergyStorage note the lack of an I on the front), I thought they left it for modders to implement, I was wrong.

 

What I'd do, then, is, extract all the energy that the client side capability thinks it has (just dump it on the ground, all we care about is getting the internal value to zero) then inserting the value received from the nbt data.

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

Considering that I don't know what you actually did, I have no idea.

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

Ok so I saw that <T> is based on the capability so I made a class that is basically the same as CapabilityEnergy
 

public class ModEnergy
{
    @CapabilityInject(ModEnergyStorage.class)
    public static Capability<ModEnergyStorage> ENERGY = null;

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

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

but it uses ModEnergyStorage
 

public class ModEnergyStorage extends EnergyStorage{

	public ModEnergyStorage(int capacity, int maxReceive, int maxExtract) {
		super(capacity, maxReceive, maxExtract);
	}
	
    public void setEnergyStored(int x)
    {
        this.energy=x;
    }
	
}

for the extra method
and this is the Item Class

public class PowerArmor extends ArmorItem {
	public PowerArmor(IArmorMaterial materialIn, EquipmentSlotType slot, Properties builder) {
		super(materialIn, slot, builder);
	}

	private ModEnergyStorage newStorage() {
		return new ModEnergyStorage(10000, 1000, 1000);
	}

	@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 == ModEnergy.ENERGY) {
					return storageHolder.cast();
				}
				return LazyOptional.empty();
			}
		};
	}

	@Override
	public boolean shouldSyncTag() {
		return true;
	}

	

	@Override
	@Nullable
	public CompoundNBT getShareTag(ItemStack stack) {
		CompoundNBT tag = stack.getOrCreateTag();
		stack.getCapability(ModEnergy.ENERGY, null).ifPresent(handler -> {
			tag.putInt("Energy", handler.getEnergyStored());
			CrudeTechMod.log("getEnergy: " + handler.getEnergyStored());
		});
		return tag;
	}

	@Override
	public void readShareTag(ItemStack stack, @Nullable CompoundNBT nbt) {
		stack.getCapability(ModEnergy.ENERGY, null).ifPresent(handler -> {
			handler.setEnergyStored(nbt.getInt("Energy"));
			CrudeTechMod.log("readEnergy: " + handler.getEnergyStored());
		});

	}

	@Override
	public void addInformation(ItemStack stack, @Nullable World worldIn, List<ITextComponent> tooltip,
			ITooltipFlag flagIn) {
		IEnergyStorage cap = stack.getCapability(ModEnergy.ENERGY, null).orElseGet(null);
		if (cap != null) {
			String charge = Integer.toString(cap.getEnergyStored());
			String capacity = Integer.toString(cap.getMaxEnergyStored());
			tooltip.add(new TranslationTextComponent("Charge: " + charge + "/" + capacity));
		} else {
			tooltip.add(new TranslationTextComponent("missing capability"));
		}
	}
}


 

Link to comment
Share on other sites

10 minutes ago, TallYate said:

if (!(instance instanceof EnergyStorage))

This basically can't happen. ModEnergyStorage extends EnergyStorage, so getting a class that is the former and not the latter is impossible.

13 minutes ago, TallYate said:

CrudeTechMod.log("getEnergy: " + handler.getEnergyStored());

Is this getting called? No?
Have you tried figuring out if the parent method is getting called, but skipping over the lambda?

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
	@Nullable
	public CompoundNBT getShareTag(ItemStack stack) {
		CompoundNBT tag = stack.getOrCreateTag();
		stack.getCapability(ModCapabilityEnergy.ENERGY, null).ifPresent(handler -> {
			tag.putInt("Energy", handler.getEnergyStored());
			CrudeTechMod.log("getEnergy: " + handler.getEnergyStored());
		});
		CrudeTechMod.log("getShareTag end");
		return tag;
	}

Ok I made the capability more like forges and made a ModIEnergyStorage
now getShareTag is being run, but not readShareTag

Also, getShareTag is getting 0 energy
 

Edited by TallYate
Link to comment
Share on other sites

Your repository is not complete (missing build.gradle), so it cannot be tested by others.

 

getShareTag and readShareTag must be extensions of ItemStack#getTag. Currently your readShareTag does not do that.

Again: read the javadocs, they tell you the use cases that must be supported by your implementation.

Link to comment
Share on other sites

18 minutes ago, TallYate said:

Also, could you link me to the javadocs you're reading?

I can't link to something that is in your IDE.

 

As for your mod... you never put any energy into the thing. So yes it will be 0 always.

But you also never write your capability to the ItemStack, hence it will not persist and cannot ever get to the client (or end up in the share tag). The ICapabilityProvider you attach does not declare that it needs to write NBT data. To do that it must implement INBTSerializable. Forge provides a combination-interface you can use called ICapabilitySerializable.

 

Note also that you won't see readShareTag being called usually when playing single player. This is because vanilla packets aren't actually serialized to bytes when playing single player, they are simply passed through a shared queue between integrated server. Thus the ItemStacks being sent over are not written and then read from NBT, they are simply cloned.

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

    • [17:29:37] [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:748) [?:1.8.0_291] {} Caused by: java.lang.NullPointerException at net.minecraft.world.server.TicketManager.removePlayer(TicketManager.java:227) ~[forge:?] {re:classloading} at net.minecraft.world.server.ChunkManager.updatePlayerStatus(ChunkManager.java:843) ~[forge:?] {re:classloading} at net.minecraft.world.server.ChunkManager.removeEntity(ChunkManager.java:982) ~[forge:?] {re:classloading} at net.minecraft.world.server.ServerChunkProvider.removeEntity(ServerChunkProvider.java:454) ~[forge:?] {re:classloading,pl:accesstransformer:B} at net.minecraft.world.server.ServerWorld.removeEntityComplete(ServerWorld.java:905) ~[forge:?] {re:classloading} at net.minecraft.world.server.ServerWorld.removeEntity(ServerWorld.java:951) ~[forge:?] {re:classloading} at net.minecraft.world.server.ServerWorld.removePlayer(ServerWorld.java:968) ~[forge:?] {re:classloading} at net.minecraft.server.management.PlayerList.respawn(PlayerList.java:411) ~[forge:?] {re:classloading} at com.mclich.engmod.item.TotemOfAbyssItem.activateTotem(TotemOfAbyssItem.java:72) ~[?:?] {re:classloading} at com.mclich.engmod.item.TotemOfAbyssItem.inventoryTick(TotemOfAbyssItem.java:58) ~[?:?] {re:classloading} at net.minecraft.item.ItemStack.inventoryTick(ItemStack.java:434) ~[forge:?] {re:classloading,xf:fml:forge:filled_map.4,xf:fml:forge:itemstack} at net.minecraft.entity.player.PlayerInventory.tick(PlayerInventory.java:238) ~[forge:?] {re:classloading} at net.minecraft.entity.player.PlayerEntity.aiStep(PlayerEntity.java:487) ~[forge:?] {re:classloading,pl:accesstransformer:B} at net.minecraft.entity.LivingEntity.tick(LivingEntity.java:2158) ~[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  
    • you can search for it in the forum, as I have already answered this question in a few threads you can start at this thread:  
    • This code will hard-crash on servers, the Minecraft class does not exist there. Show the stacktrace of the exception.
    • asasasasasasasasasasasasasasasa
    • I have an item (basically totem), that will teleport player to its spawnpoint after fell in void. I use PlayerList#respawn method to teleport player back, but it throws NullPointerException. The code is below: private void activateTotem(World world, ServerPlayerEntity player, ItemStack itemStack) { player.fallDistance=0F; ItemStack totem=itemStack.copy(); Minecraft minecraft=Minecraft.getInstance(); if(world.dimension()==World.END) player.moveTo(100.5F, 49F, 0.5F); else player=world.getServer().getPlayerList().respawn(player, false); if(minecraft.gameMode.getPlayerMode().isSurvival()) itemStack.shrink(1); DistExecutor.unsafeRunWhenOn(Dist.CLIENT, ()->()->minecraft.gameRenderer.displayItemActivation(totem)); minecraft.particleEngine.createTrackingEmitter(player, ParticleTypes.PORTAL, 40); world.playSound((PlayerEntity)null, player.blockPosition(), SoundEvents.TOTEM_USE, SoundCategory.PLAYERS, 2F, 1F); player.playSound(SoundEvents.TOTEM_USE, 2F, 1F); } Is there any errors or am i just do something wrong?
  • Topics

  • Who's Online (See full list)

×
×
  • Create New...

Important Information

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