Jump to content

thebadscientist

Members
  • Posts

    8
  • Joined

  • Last visited

Recent Profile Visitors

The recent visitors block is disabled and is not being shown to other users.

thebadscientist's Achievements

Tree Puncher

Tree Puncher (2/8)

1

Reputation

  1. You mean regarding the value resetting to 0 or regarding the whole capability implementation? Either way, check my GitHub repository to see how I implemented it. https://github.com/deneth-weerasinghe/PracticeMod Note the capability's stored value is increased by 10 when right-clicking water with a custom item I made.
  2. Alright, I think I got everything sorted except that the value I want to store gets reset to 0 everytime I log into the world: [23:41:41.634] [Server thread/INFO] [co.de.pr.se.PracticeMod/]: reading NBT! [23:41:41.634] [Server thread/INFO] [co.de.pr.se.PracticeMod/]: NBT Data: 120 [23:41:41.634] [Server thread/INFO] [co.de.pr.se.PracticeMod/]: Instance Data: 120 [23:41:41.636] [Server thread/INFO] [minecraft/PlayerList]: Dev[local:E:2d267046] logged in with entity id 372 at (193.58322637936269, 72.0, -162.9556295622775) [23:41:41.642] [Server thread/INFO] [minecraft/MinecraftServer]: Dev joined the game [23:41:41.719] [Server thread/INFO] [co.de.pr.se.PracticeMod/]: PLAYER LOGIN EVENT [23:41:41.719] [Server thread/INFO] [co.de.pr.se.PracticeMod/]: reading NBT! [23:41:41.719] [Server thread/INFO] [co.de.pr.se.PracticeMod/]: NBT Data: 0 [23:41:41.719] [Server thread/INFO] [co.de.pr.se.PracticeMod/]: Instance Data: 0 Do you know what would cause this?
  3. I copied from some mod, oh well. So what should the getCapability look like then? I still don't quite understand LazyOptional tbh My mistake, copied some old code, what I actually put was this, is this ok or is it bad as well? this.getCapability(PLAYER_COUNTER, null).orElseThrow(() -> new IllegalArgumentException("LazyOptional must not be empty!")) Ye probably shouldn't adapt code from two different mods (it's from ToughAsNails, which I thought could be useful)
  4. It's an attempt to adapt from 1.12 not 1.13 CustomClass: PlayerDispatcher: CounterStorage: PlayerPropertiesEvent: EventHandler: (this increases the value I want to store)
  5. that's for 1.13, which is outdated. for instance, 1.14 does not have the hasCapability method from ICapabilityProvider and makes no mention of LazyOptional. what I did is tried looking at source code from 1.12 and 1.13 mods and see if I could adapt the code to 1.14 but it didn't end too well.
  6. Can't seem to find any 1.14 mod that does something like this and there's very little documentation about the 1.14 capability system. Any help?
  7. In an attempt to learn the capability system, I made a custom capability that tracks a simple variable (called counter) and allows me to increment it by 10 everytime I right-click with gunpowder and right-clicking in the air will print out the current value to the console. The incrementing and printing out works fine. However, upon rejoining the world or respawning, the value in counter resets back to the default 20. From my limited understanding of capabilities, this must mean the data is not being saved as NBT so there's no data to load for respawning or rejoining. That or a new instance of the CustomClass class is made overwriting the previous one? Btw I do have a player clone event method. Here's the code: CustomClass public class CustomClass implements ICustomClass { private int counter = 20; @Override public void setCounter(int value) { this.counter = value; } @Override public int getCounter() { return counter; } @Override public void copyForRespawn(CustomClass deadPlayer) { counter = deadPlayer.counter; } } Dispatcher public class PlayerDispatcher implements ICapabilitySerializable<CompoundNBT> { @CapabilityInject(ICustomClass.class) public static Capability<ICustomClass> PLAYER_COUNTER; private ICustomClass instance = PLAYER_COUNTER.getDefaultInstance(); @Nonnull @Override public <T> LazyOptional<T> getCapability(@Nonnull Capability<T> cap, Direction side) { return getCapability(cap); } @Nonnull @Override public <T> LazyOptional<T> getCapability(@Nonnull Capability<T> cap) { if (cap == PLAYER_COUNTER){ return LazyOptional.of(() -> (T) instance); } return LazyOptional.empty(); } @Override public CompoundNBT serializeNBT() { return (CompoundNBT) PLAYER_COUNTER.getStorage().writeNBT(PLAYER_COUNTER, instance, null); } @Override public void deserializeNBT(CompoundNBT nbt) { PLAYER_COUNTER.getStorage().readNBT(PLAYER_COUNTER, instance, null, nbt); } } Storage class public class CounterStorage implements Capability.IStorage<ICustomClass> { @Override public INBT writeNBT(Capability<ICustomClass> capability, ICustomClass instance, Direction side) { CompoundNBT tag = new CompoundNBT(); tag.putInt("counter", instance.getCounter()); return tag; } @Override public void readNBT(Capability<ICustomClass> capability, ICustomClass instance, Direction side, INBT nbt) { CompoundNBT tag = (CompoundNBT) nbt; instance.setCounter(tag.getInt("counter")); } } The event handler @Mod.EventBusSubscriber public class CapEventHandler { @SubscribeEvent public static void onTest(PlayerInteractEvent.RightClickItem event){ PlayerEntity player = event.getEntityPlayer(); if (event.getItemStack().getItem() == Items.GUNPOWDER){ player.getCapability(PlayerDispatcher.PLAYER_COUNTER).ifPresent(customClass -> { customClass.setCounter(customClass.getCounter() + 10); System.out.println("Current value is: " + customClass.getCounter()); }); } } @SubscribeEvent public static void onAirRightClick(PlayerInteractEvent.RightClickEmpty event) { PlayerEntity player = event.getEntityPlayer(); ItemStack item = player.getHeldItemMainhand(); if (item.isEmpty()) { player.getCapability(PlayerDispatcher.PLAYER_COUNTER).ifPresent(customClass ->{ System.out.println("Current value is " + customClass.getCounter()); }); } } } Another event handler public class PlayerPropertiesEvent { public static PlayerPropertiesEvent instance = new PlayerPropertiesEvent(); @SubscribeEvent public void onEntityConstruction(AttachCapabilitiesEvent<Entity> event) { if (event.getObject() instanceof PlayerEntity){ event.addCapability(new ResourceLocation(PracticeMod.MODID, "counter"), new PlayerDispatcher()); } } @SubscribeEvent public void onPlayerCloned(PlayerEvent.Clone event){ if (event.isWasDeath()){ LazyOptional<ICustomClass> capability = event.getOriginal().getCapability(PlayerDispatcher.PLAYER_COUNTER); capability.ifPresent(oldStore -> { event.getEntityPlayer().getCapability(PlayerDispatcher.PLAYER_COUNTER).ifPresent(newStore -> { newStore.copyForRespawn((CustomClass) oldStore); }); }); } } }
×
×
  • Create New...

Important Information

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