Search the Community
Showing results for tags 'capabilities'.
-
I made a few custom capabilities for my mod and I'm trying to use the player clone event to make the capabilities persistent between deaths. If I look at the debugger I can see that the oldPlayer variable has the capabilities and the correct values attached to it. But when I try to use getCapability, it just returns a LazyOptional with null as you would expect when it is not found. Also the ifPresent(...) method does not execute the code inside. I have used the getCapability multiple times already in other parts and never ran into any problem. For example, I've supplied a debug 3 variable from my LivingEntity death event (this part is only active on players) and this one works just fine and returns the capability as you would expect. Does anyone see anything I did wrong or a reason as to why it doesn't work and how to fix this issue? Edit: Solved: I needed to reviveCaps() before I could get them. @SubscribeEvent public void onPlayerClone(PlayerEvent.Clone event) { Player oldPlayer = event.getOriginal(); Player newPlayer = event.getEntity(); var debug = oldPlayer.getCapability(UpgradeDataProvider.UPGRADE_DATA); // This returns a LazyOptional with supplier null var debug2 = newPlayer.getCapability(UpgradeDataProvider.UPGRADE_DATA); // This returns a LazyOptional with a valid supplier and works fine oldPlayer.getCapability(PlayerDataProvider.PLAYER_DATA).ifPresent(oldStore -> { newPlayer.getCapability(PlayerDataProvider.PLAYER_DATA).ifPresent(newStore -> { newStore.copyFrom(oldStore); }); }); // this does absolutely nothing oldPlayer.getCapability(UpgradeDataProvider.UPGRADE_DATA).ifPresent(oldStore -> { newPlayer.getCapability(UpgradeDataProvider.UPGRADE_DATA).ifPresent(newStore -> { newStore.copyFrom(oldStore); }); }); // this doesn't either oldPlayer.getCapability(CurrencyDataProvider.CURRENCY_DATA).ifPresent(oldStore -> { newPlayer.getCapability(CurrencyDataProvider.CURRENCY_DATA).ifPresent(newStore -> { newStore.copyFrom(oldStore); }); }); } // neither does this In the LivingDeath event: var debug3 = player.getCapability(UpgradeDataProvider.UPGRADE_DATA); // This works just fine and returns the capability as expected.
-
I currently have a capability and everything I want to do with it setup correctly. I want to make it clear my capability works, persists through death updates correctly and all that. What I'm trying to do is force the NBT data to save at a time besides exiting the game/dying. I've tried to look at the super methods for saving but I can't grasp how exactly it's functioning. The reason I'm trying to do this in the first place is that I'm trying to make my mod-compatible with a modelling mod known as "Figura", and it's able to read the NBT data attached to a player for use in players models. Currently players can't actually read the new values because it doesn't save to NBT until death or exit.
-
Hello. I want to put every dimension on it's own server and establish player interaction between them. Is there any mod already or way to send data from one server to another?
-
Hello! So, I'm having a problem where I always lose the data when I exit and rejoin a world, I think my problem is with the deserialization and serialization of the NBT, but i'm not sure and i don't know how to fix it. I think my error is somewhere here: import net.minecraft.core.Direction; import net.minecraft.nbt.CompoundTag; import net.minecraftforge.common.capabilities.Capability; import net.minecraftforge.common.capabilities.CapabilityManager; import net.minecraftforge.common.capabilities.CapabilityToken; import net.minecraftforge.common.capabilities.ICapabilityProvider; import net.minecraftforge.common.util.INBTSerializable; import net.minecraftforge.common.util.LazyOptional; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; public class PlayerGodParentProvider implements ICapabilityProvider, INBTSerializable<CompoundTag> { public static Capability<PlayerGodParent> PLAYER_GOD_PARENT = CapabilityManager.get(new CapabilityToken<PlayerGodParent>() { }); private PlayerGodParent godParent = null; private final LazyOptional<PlayerGodParent> optional = LazyOptional.of(this::createPlayerGodParent); private PlayerGodParent createPlayerGodParent() { if(this.godParent == null) { this.godParent = new PlayerGodParent(); } return this.godParent; } @Override public @NotNull <T> LazyOptional<T> getCapability(@NotNull Capability<T> cap, @Nullable Direction side) { if(cap == PLAYER_GOD_PARENT) { return optional.cast(); } return LazyOptional.empty(); } @Override public CompoundTag serializeNBT() { CompoundTag nbt = new CompoundTag(); createPlayerGodParent().saveNBTData(nbt); return nbt; } @Override public void deserializeNBT(CompoundTag nbt) { createPlayerGodParent().loadNBTData(nbt); } } The whole repo is here: https://github.com/P3terD/myths-mod/tree/main
-
I am Attempting to register a custom capability to NBT running (mostly) off of a tutorial and failing miserably. I have spoken with someone familiar with Java, and both they and my IDE concur that there is no outward problem with my code, so I believe there is a problem with my implementation (likely caused by a mixture of ignorance and new versions. Github linked, if anyone is able to help, that would be greatly appreciated https://github.com/Zebralear/SoulCraft
- 2 replies
-
- 1.19.2
- capabilities
-
(and 1 more)
Tagged with:
-
Topic for making tags
-
- 1.10.2
- 1.11.2
-
(and 81 more)
Tagged with:
- 1.10.2
- 1.11.2
- 1.12.2
- 1.13.2
- 1.14.4
- 1.15.2
- 1.16.5
- 1.17.1
- 1.18.2
- 1.19.0
- 1.19.1
- 1.19.2
- 1.19.3
- 1.19.4
- 1.2.5
- 1.20.0
- 1.3.2
- 1.4.7
- 1.5.2
- 1.6.4
- 1.7.10
- 1.8.9
- 1.9.4
- armour
- bad jvm args
- biomes
- blocks
- broken mod
- bug
- capabilities
- client
- client-only mods on server
- codecs
- commands
- dimensions
- effects
- enchantments
- entities
- events
- feature request
- fluids
- food
- gradle
- groovy
- guis
- ide
- installer
- items
- java
- kotlin
- launcher
- libraries
- loot
- metadata
- missing jar association
- missing java
- missing mods
- mixin
- needs more info
- networking
- news
- optifine
- ores
- out of memory
- outdated drivers
- particles
- performance
- porting
- potions
- recipes
- registries
- rendering
- scala
- server
- solved
- sounds
- textures
- tools
- tutorial
- weapons
- worldgen
- wrong java version
- wrong mod version
-
I'm trying to use Capabilities to add a kind of player currency. But I can't get it to work. I've followed a couple of tutorials on it pretty much exactly, but when I try to render the amount on the corner of the screen it's always null. I apologise if this post is large, I guess the site appearance just changed and there's no preview option? ScreenTag (the gui with currency amount that's trying to be rendered) private Minecraft mc; private final int colour = new Color(255, 85, 255).getRGB(); public ScreenTag(Minecraft mc) { this.mc = mc; } @SubscribeEvent public void onRenderGui(RenderGameOverlayEvent.Post event) { if (event.getType() != ElementType.EXPERIENCE) { return; } IEchoesCapability echoes = mc.thePlayer.getCapability(EchoesManager.ECHOES, null); // if (echoes == null) return; // <- This always fires. Why is this always null? // drawCenteredString(mc.fontRendererObj, TextFormatting.RED + "Blood Echoes: " + TextFormatting.WHITE + echoes.getEchoes(), 20, 20, colour); drawCenteredString(mc.fontRendererObj, TextFormatting.RED + "Insight: " + TextFormatting.WHITE + echoes.getInsight(), 20, 50, colour); } IEchoesCapability (I don't think I'd need to post the Echoes class that implements this interface since it's just variable setters/getters) public boolean useEchoes(int amount); public void addEchoes(int amount); public void setEchoes(int amount); public int getEchoes(); // public boolean useInsight(int amount); public void addInsight(int amount); public void setInsight(int amount); public int getInsight(); EchoesStorage implements IStorage<IEchoesCapability> @Override public NBTBase writeNBT(Capability<IEchoesCapability> capability, IEchoesCapability instance, EnumFacing side) { NBTTagCompound nbt = new NBTTagCompound(); nbt.setInteger("echoes", instance.getEchoes()); nbt.setInteger("insight", instance.getInsight()); return nbt; } @Override public void readNBT(Capability<IEchoesCapability> capability, IEchoesCapability instance, EnumFacing side, NBTBase nbt) { instance.setEchoes(((NBTTagCompound) nbt).getInteger("echoes")); instance.setInsight(((NBTTagCompound) nbt).getInteger("insight")); } Capabilities (event handler) public static final ResourceLocation ECHOES = new ResourceLocation(Main.MODID, "echoes"); @SubscribeEvent public void attach(AttachCapabilitiesEvent.Entity event) { if (event.getEntity() instanceof EntityPlayer) { event.addCapability(ECHOES, new EchoesManager()); } } @SubscribeEvent public void clone(PlayerEvent.Clone event) { if (event.isWasDeath()) { IEchoesCapability original = event.getOriginal().getCapability(EchoesManager.ECHOES, null); IEchoesCapability cloned = event.getEntityPlayer().getCapability(EchoesManager.ECHOES, null); cloned.setEchoes(original.getEchoes()); cloned.setInsight(original.getInsight()); } } EchoesManager implements ICapabilitySerializable<NBTBase> @CapabilityInject(IEchoesCapability.class) public static final Capability<IEchoesCapability> ECHOES = null; private IEchoesCapability instance = ECHOES.getDefaultInstance(); @Override public boolean hasCapability(Capability<?> capability, EnumFacing facing) { return capability == ECHOES; } @Override public <T> T getCapability(Capability<T> capability, EnumFacing facing) { return capability == ECHOES ? ECHOES.<T>cast(instance) : null; } @Override public NBTBase serializeNBT() { return ECHOES.getStorage().writeNBT(ECHOES, instance, null); } @Override public void deserializeNBT(NBTBase nbt) { ECHOES.getStorage().readNBT(ECHOES, instance, null, nbt); } inside Main class @EventHandler public void init(FMLInitializationEvent event) { proxy.init(); MinecraftForge.EVENT_BUS.register(new ScreenTag(Minecraft.getMinecraft())); } and inside ServerProxy public void init() { CapabilityManager.INSTANCE.register(IEchoesCapability.class, new EchoesStorage(), Echoes.class); MinecraftForge.EVENT_BUS.register(new Capabilities()); } I think that should be everything relevant. The whole mod kinda depends on this working properly, so could someone tell me what exactly I'm doing wrong?