Eria8 Posted February 3, 2017 Posted February 3, 2017 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? Quote
Animefan8888 Posted February 3, 2017 Posted February 3, 2017 You will need to send a packet to sync the Capability. Quote VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect. Forge and vanilla BlockState generator.
Eria8 Posted February 3, 2017 Author Posted February 3, 2017 On 2/3/2017 at 3:05 AM, Animefan8888 said: You will need to send a packet to sync the Capability. Expand I realise that I need to use packets to sync the amounts. But without packets it should still render, just the amount would always be 0, right? Quote
Animefan8888 Posted February 3, 2017 Posted February 3, 2017 Is it an int, or an Integer? Quote VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect. Forge and vanilla BlockState generator.
Eria8 Posted February 3, 2017 Author Posted February 3, 2017 On 2/3/2017 at 3:10 AM, Animefan8888 said: Is it an int, or an Integer? Expand They're both ints. IEchoesCapability echoes = mc.thePlayer.getCapability(EchoesManager.ECHOES, null); if (echoes == null) return; This always returns when trying to render. So the capability itself is null, not any of the variables inside it. Quote
Choonster Posted February 3, 2017 Posted February 3, 2017 (edited) On 2/3/2017 at 3:02 AM, Eria8 said: inside Main class @EventHandler public void init(FMLInitializationEvent event) { proxy.init(); MinecraftForge.EVENT_BUS.register(new ScreenTag(Minecraft.getMinecraft())); } Expand You can't reference client-only classes like Minecraft from common code, otherwise you'll crash the dedicated server. Quote and inside ServerProxy public void init() { CapabilityManager.INSTANCE.register(IEchoesCapability.class, new EchoesStorage(), Echoes.class); MinecraftForge.EVENT_BUS.register(new Capabilities()); } Expand Capabilities and most event handlers need to be registered on both physical sides, so this should be done from your @Mod class. The class you specify in the serverSide property of the @SidedProxy annotation will only be loaded by the dedicated server. Edited February 3, 2017 by Choonster Quote Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.
Eria8 Posted February 3, 2017 Author Posted February 3, 2017 On 2/3/2017 at 3:15 AM, Choonster said: ... Expand Thank you so much, I totally overlooked that. Quote
Recommended Posts
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.