Lambda Posted December 10, 2016 Posted December 10, 2016 Hey there, So I'm in need of overlaying something over the screen when an item is held or equipped, I need it to display a % based on an int, so my questions are: - How would I make an int that stores information that can be accessed and is held to only one player (capability?) - How would I overlay that information Thanks for your time. Quote Relatively new to modding. Currently developing: https://github.com/LambdaXV/DynamicGenerators
hugo_the_dwarf Posted December 10, 2016 Posted December 10, 2016 Can store the information in the item being held, or the player via Capability, or as an NBT on the item but Capabilities are more recommended. I normally use the "RenderGameOverlayEvent" for handling "overlays" Quote Currently updating my Mod to 1.10.2 https://bitbucket.org/hugo_the_dwarf/riseoftristram2016/src?at=master
Lambda Posted December 10, 2016 Author Posted December 10, 2016 Can store the information in the item being held, or the player via Capability, or as an NBT on the item but Capabilities are more recommended. I normally use the "RenderGameOverlayEvent" for handling "overlays" Okay, so how would I get a capability and display it / set it to a temp int? Aswell, how would I display it? what do I have to call, etc. Quote Relatively new to modding. Currently developing: https://github.com/LambdaXV/DynamicGenerators
Draco18s Posted December 10, 2016 Posted December 10, 2016 I am not sure what you mean by the int, but you can render the overlay in your client proxy, on renderOverlayEvent. I think. client proxy: no renderOverlayEvent: yes Quote 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.
Lambda Posted December 10, 2016 Author Posted December 10, 2016 Okay, this seemed NOT to work: public class ManaEvent { protected FontRenderer fontRendererObj; @SubscribeEvent public void onRenderGameOverlay(RenderGameOverlayEvent event) { Minecraft mc = Minecraft.getMinecraft(); if(!event.isCancelable()) { int posX = event.getResolution().getScaledWidth() / 2 + 10; int posY = event.getResolution().getScaledHeight() - 48; mc.renderEngine.bindTexture(new ResourceLocation(ModUtil.MOD_ID + ":" + "textures/gui/mana.png")); String string = CapabilityMagic.MANA.getStorage().toString() + " TEST"; AssetUtil.displayNameString(this.fontRendererObj, posX, posY, string); } } } registering via: MinecraftForge.EVENT_BUS.register(ManaEvent.class); in pre-init; However, will my capability display properly? And I should be able to add to it / remove / access it correct? I have it register and everything, here is the capa: public class CapabilityMagic { @CapabilityInject(IManaStorage.class) public static Capability<IManaStorage> MANA = null; public static void register() { CapabilityManager.INSTANCE.register(IManaStorage.class, new Capability.IStorage<IManaStorage>() { @Override public NBTBase writeNBT(Capability<IManaStorage> capability, IManaStorage instance, EnumFacing side) { return new NBTTagInt(instance.getManaStored()); } @Override public void readNBT(Capability<IManaStorage> capability, IManaStorage instance, EnumFacing side, NBTBase nbt) { if (!(instance instanceof ManaStorage)) throw new IllegalArgumentException("Can not deserialize to an instance that isn't the default implementation"); ((ManaStorage)instance).mana = ((NBTTagInt)nbt).getInt(); } }, new Callable<IManaStorage>() { @Override public IManaStorage call() throws Exception { return new ManaStorage(1000); } }); } } ManaStorage public class ManaStorage implements IManaStorage { protected int mana; protected int capacity; protected int maxReceive; protected int maxExtract; public ManaStorage(int capacity) { this(capacity, capacity, capacity); } public ManaStorage(int capacity, int maxTransfer) { this(capacity, maxTransfer, maxTransfer); } public ManaStorage(int capacity, int maxReceive, int maxExtract) { this.capacity = capacity; this.maxReceive = maxReceive; this.maxExtract = maxExtract; } @Override public int receiveMana(int maxReceive, boolean simulate) { if (!canReceive()) return 0; int energyReceived = Math.min(capacity - mana, Math.min(this.maxReceive, maxReceive)); if (!simulate) mana += energyReceived; return energyReceived; } @Override public int extractMana(int maxExtract, boolean simulate) { if (!canExtract()) return 0; int energyExtracted = Math.min(mana, Math.min(this.maxExtract, maxExtract)); if (!simulate) mana -= energyExtracted; return energyExtracted; } @Override public int getManaStored() { return mana; } @Override public int getMaxManaStored() { return capacity; } @Override public boolean canExtract() { return this.maxExtract > 0; } @Override public boolean canReceive() { return this.maxReceive > 0; } } Quote Relatively new to modding. Currently developing: https://github.com/LambdaXV/DynamicGenerators
hugo_the_dwarf Posted December 10, 2016 Posted December 10, 2016 where are you actaully "drawing" anything in that event? You can look at how I do it, however it's a bit hacky as I had thrown it together years ago. EventPlayerOverlayGui.java Quote Currently updating my Mod to 1.10.2 https://bitbucket.org/hugo_the_dwarf/riseoftristram2016/src?at=master
Lambda Posted December 10, 2016 Author Posted December 10, 2016 I'm drawing it in my assetUtil: @SideOnly(Side.CLIENT) public static void displayNameString(FontRenderer font, int xSize, int yPositionOfMachineText, String text){ font.drawString(text, xSize/2-font.getStringWidth(text)/2, yPositionOfMachineText, StringUtil.DECIMAL_COLOR_WHITE); } @SideOnly(Side.CLIENT) public static void displayNameString(FontRenderer font, int xSize, int yPositionOfMachineText, TileEntityBase tile){ displayNameString(font, xSize, yPositionOfMachineText, tile.getDisplayedName()); } Quote Relatively new to modding. Currently developing: https://github.com/LambdaXV/DynamicGenerators
hugo_the_dwarf Posted December 10, 2016 Posted December 10, 2016 Try using mc.fontRendererObj instead of your own defined one. Quote Currently updating my Mod to 1.10.2 https://bitbucket.org/hugo_the_dwarf/riseoftristram2016/src?at=master
Lambda Posted December 10, 2016 Author Posted December 10, 2016 Okay still have a bit of an issue, it seems like the hunger bar is buggy and I cannot get the mana that my capability stores: Here is what the hunger bar looks like now: Fixed, via ElementType.EXPERIENCE. Also, doing this to get my capability results in a NullExecption: IManaStorage storage = mc.thePlayer.getCapability(CapabilityMagic.MANA, null); String string = "MANA: " + Integer.toString(storage.getManaStored()); mc.fontRendererObj.drawString(string, 50 + 1, 50, 50); Any pointers? I think it has to do with the capability isnt on the player in the first place. How would I do this? edit: Confirmed that the played does not have the capability via System.out.print(mc.thePlayer.hasCapability(CapabilityMagic.MANA, null)); how would I define one to the player? Quote Relatively new to modding. Currently developing: https://github.com/LambdaXV/DynamicGenerators
hugo_the_dwarf Posted December 10, 2016 Posted December 10, 2016 Did you attach the capability correctly? Quote Currently updating my Mod to 1.10.2 https://bitbucket.org/hugo_the_dwarf/riseoftristram2016/src?at=master
Lambda Posted December 10, 2016 Author Posted December 10, 2016 I dont think so, I dont know how to attach it to the player. Quote Relatively new to modding. Currently developing: https://github.com/LambdaXV/DynamicGenerators
hugo_the_dwarf Posted December 10, 2016 Posted December 10, 2016 https://mcforge.readthedocs.io/en/latest/datastorage/capabilities/ or you can view my code in my repo in my sig. My Attach Capabilities Event Quote Currently updating my Mod to 1.10.2 https://bitbucket.org/hugo_the_dwarf/riseoftristram2016/src?at=master
Lambda Posted December 10, 2016 Author Posted December 10, 2016 This doesnt seem to work: CapaProvider public class CapabilityMagicProvider implements ICapabilityProvider, ICapabilitySerializable<NBTTagCompound> { public static final ResourceLocation KEY = new ResourceLocation(ModUtil.MOD_ID, "mana"); private CapManaData INSTANCE = new CapManaData(); public CapabilityMagicProvider(EntityLivingBase entity) { INSTANCE.setEntity(entity); } @Override public boolean hasCapability(Capability<?> capability, EnumFacing facing) { return capability == CapabilityMagic.MANA; } @Override public <T> T getCapability(Capability<T> capability, EnumFacing facing) { if (capability == CapabilityMagic.MANA) return (T) INSTANCE; return null; } @Override public NBTTagCompound serializeNBT() { return (NBTTagCompound) CapabilityMagic.MANA.writeNBT(INSTANCE, null); } @Override public void deserializeNBT(NBTTagCompound nbt) { CapabilityMagic.MANA.readNBT(INSTANCE, null, nbt); } } my CapaData: public class CapManaData implements IManaStorage { private EntityLivingBase entity; protected int mana; protected int capacity; protected int maxReceive; protected int maxExtract; public EntityLivingBase getEntity() { return entity; } public void setEntity(EntityLivingBase entity) { this.entity = entity; mana = this.getManaStored(); } @Override public int receiveMana(int maxReceive, boolean simulate) { if (!canReceive()) return 0; int energyReceived = Math.min(capacity - mana, Math.min(this.maxReceive, maxReceive)); if (!simulate) mana += energyReceived; return energyReceived; } @Override public int extractMana(int maxExtract, boolean simulate) { if (!canExtract()) return 0; int energyExtracted = Math.min(mana, Math.min(this.maxExtract, maxExtract)); if (!simulate) mana -= energyExtracted; return energyExtracted; } @Override public int getManaStored() { return mana; } @Override public int getMaxManaStored() { return capacity; } @Override public boolean canExtract() { return this.maxExtract > 0; } @Override public boolean canReceive() { return this.maxReceive > 0; } } my new events: @SubscribeEvent public static void onRenderGameOverlay(RenderGameOverlayEvent event) { Minecraft mc = Minecraft.getMinecraft(); if(!event.isCancelable() && event.getType() == RenderGameOverlayEvent.ElementType.EXPERIENCE) { int posX = event.getResolution().getScaledWidth() / 2 + 10; int posY = event.getResolution().getScaledHeight() - 48; // mc.renderEngine.bindTexture(new ResourceLocation(ModUtil.MOD_ID + ":" + "textures/gui/mana.png")); System.out.print(mc.thePlayer.hasCapability(CapabilityMagic.MANA, null)); IManaStorage storage = mc.thePlayer.getCapability(CapabilityMagic.MANA, null); // String string = "MANA: " + Integer.toString(storage.getManaStored() + 5 ); mc.fontRendererObj.drawString("yum", 50 + 1, 50, 50); } } @SubscribeEvent public void onAddCapabilitiesEntity(AttachCapabilitiesEvent<Entity> e) { if (canHaveAttributes(e.getObject())) { EntityLivingBase ent = (EntityLivingBase) e.getObject(); if (ent instanceof EntityPlayer) e.addCapability(CapabilityMagicProvider.KEY, new CapabilityMagicProvider(ent)); e.addCapability(CapabilityMagicProvider.KEY, new CapabilityMagicProvider(ent)); } } public static boolean canHaveAttributes(Entity entity) { if (entity instanceof EntityLivingBase) return true; return false; } Quote Relatively new to modding. Currently developing: https://github.com/LambdaXV/DynamicGenerators
hugo_the_dwarf Posted December 10, 2016 Posted December 10, 2016 in your event you are attaching it twice Also which do you mean it doesn't work? Your player.hasCapability() returning false? or stuck with default values? If it's the default values you will need to create packets (for at least save and reload world) to update the client with the saved server values. if (ent instanceof EntityPlayer) e.addCapability(CapabilityMagicProvider.KEY, new CapabilityMagicProvider(ent)); e.addCapability(CapabilityMagicProvider.KEY, new CapabilityMagicProvider(ent)); You can remove the second one, as it looks like you copied my event, and mine was so if it was a living entity add attributes, if it's a player add the player extra capability. Also what does your CapabilityMagic look like. Quote Currently updating my Mod to 1.10.2 https://bitbucket.org/hugo_the_dwarf/riseoftristram2016/src?at=master
Lambda Posted December 10, 2016 Author Posted December 10, 2016 Thanks for your response, Ah didn't catch that.. And yes, the capability WAS returning false on the player, sorry for the vague explanation, it was 3 in the morning Ended up fixed it by changing a few more things, I didnt have the CapabilityMagic set to the data, so it was not registering properly. It is working now So I thank you for that , will test out NBT write/read things shortly, so I'll reply back if there is another problem Thanks. Quote Relatively new to modding. Currently developing: https://github.com/LambdaXV/DynamicGenerators
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.