Posted January 17, 20214 yr When I asked console: System.out.println(player.getCapability(MagicCapability.MAGIC).orElse(null) == null); "true" What did I do or not do, that made this capability not loaded? Please point it out. I have no clue. public static void setup (final FMLCommonSetupEvent event){ MinecraftForge.EVENT_BUS.register(new Fool()); MagicCapability.register(); } public class MagicCapability { @CapabilityInject(IMaxMP.class) public static Capability<IMaxMP> MAGIC = null; public static void register() { CapabilityManager.INSTANCE.register(IMaxMP.class, new IStorage<IMaxMP>() { @Override public INBT writeNBT(Capability<IMaxMP> capability, IMaxMP instance, Direction side) { return IntNBT.valueOf(instance.getManaStored()); } @Override public void readNBT(Capability<IMaxMP> capability, IMaxMP instance, Direction side, INBT nbt) { ((MaxMP)instance).addMana(((IntNBT)nbt).getInt()); } }, () -> new MaxMP(null)); } } public interface IMaxMP { public int getManaStored(); public void addMana(int amount); public void setMana(int amount); public boolean useMana(int amount); public int getMaxMana(); public void addMaxMana(int amount); public void setMaxMana(int amount); } public class Fool { @SubscribeEvent public void attatchCapability(AttachCapabilitiesEvent <Entity> event) { ServerPlayerEntity player; if (event.getObject() instanceof ServerPlayerEntity) { player = (ServerPlayerEntity) event.getObject(); } else { return; } LazyOptional<IMaxMP> optPlayer = player.getCapability(MagicCape.MAGIC); if (!optPlayer.isPresent()) { event.addCapability(new ResourceLocation(Chants.MODID, "magic_capability"), new Maou(player)); } } } public class Maou implements ICapabilityProvider, ICapabilitySerializable<CompoundNBT> { IMaxMP storage; public Maou (ServerPlayerEntity serverPlayerEntity) { storage = new MaxMP(serverPlayerEntity); } @Override public <T> LazyOptional<T> getCapability(Capability<T> cap, Direction side) { if (cap == MagicCape.MAGIC) return (LazyOptional<T>) LazyOptional.of(() -> storage); return LazyOptional.empty(); } @Override public CompoundNBT serializeNBT() { CompoundNBT ret = new CompoundNBT(); ret.putInt("magicStored", storage.getManaStored()); return ret; } @Override public void deserializeNBT(CompoundNBT nbt) { int magic = nbt.getInt("magicStored"); storage.addMana(magic); } } Edited January 17, 20214 yr by e2rifia
January 17, 20214 yr Author 17 minutes ago, poopoodice said: Is the event called? Have you attach the capability to the players? I think so.
January 17, 20214 yr Author 31 minutes ago, diesieben07 said: More context is needed. Where is this code located? @Mod(Main.MODID) @Mod.EventBusSubscriber(modid = Main.MODID) public class Main { public static final String MODID = "main"; @SubscribeEvent public static void onServerStarting(FMLServerStartingEvent event) { CommandDispatcher<CommandSource> d = event.getServer().getCommandManager().getDispatcher(); d.register(Commands.literal("tellme").requires(source -> { try { return source.asPlayer() != null; } catch(CommandSyntaxException e) { return false; } }).executes(command -> { ServerPlayerEntity player = command.getSource().asPlayer(); System.out.println(player.getCapability(MagicCape.MAGIC).orElse(null) == null); return 1; })); 42 minutes ago, diesieben07 said: Thinking is not helpful here. Verify using the debugger. I see that @SubscribeEvent setup() is not called at all.
January 17, 20214 yr Author @Mod(Main.MODID) @Mod.EventBusSubscriber(modid = Main.MODID) public class Main{ public static final String MODID = "main"; @SubscribeEvent public static void setup(final FMLCommonSetupEvent event) { System.out.println("I'm setting up!"); MinecraftForge.EVENT_BUS.register(new CapabilityAttatcher()); MagicCapability.register(); } } Edited January 17, 20214 yr by e2rifia
January 17, 20214 yr Author 5 minutes ago, diesieben07 said: @EventBusSubscriber subscribes to the forge bus by default, but FMLCommonSetupEvent is fired on the mod bus. I should separate @Mod and @EventBusSubscriber?
January 17, 20214 yr Author 9 minutes ago, diesieben07 said: You don't need to, no. Why? Is @EventBusSubscriber what's getting in the way of setup? It didn't work... Edited January 17, 20214 yr by e2rifia
January 17, 20214 yr 24 minutes ago, e2rifia said: It didn't work... what dint work, show your new code
January 17, 20214 yr Author 40 minutes ago, diesieben07 said: @EventBusSubscriber subscribes to the forge bus by default, but FMLCommonSetupEvent is fired on the mod bus. @Mod(Main.MODID) //@Mod.EventBusSubscriber(modid = Main.MODID) public class Main{ public static final String MODID = "main"; @SubscribeEvent public static void setup(final FMLCommonSetupEvent event) { System.out.println("I'm setting up!"); MinecraftForge.EVENT_BUS.register(new CapabilityAttatcher()); MagicCapability.register(); } }
January 17, 20214 yr I could be wrong, but I think you might need to add this code in the constructor: FMLJavaModLoadingContext.get().getModEventBus().addListener(this::setup); And you may need to change the setup method to non-static. Edited January 17, 20214 yr by Linky132
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.