Jump to content

[1.11] Creating an Overlay


Lambda

Recommended Posts

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.

Relatively new to modding.

Currently developing:

https://github.com/LambdaXV/DynamicGenerators

Link to comment
Share on other sites

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.

Relatively new to modding.

Currently developing:

https://github.com/LambdaXV/DynamicGenerators

Link to comment
Share on other sites

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

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.

Link to comment
Share on other sites

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;
    }
}

Relatively new to modding.

Currently developing:

https://github.com/LambdaXV/DynamicGenerators

Link to comment
Share on other sites

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());
    }

Relatively new to modding.

Currently developing:

https://github.com/LambdaXV/DynamicGenerators

Link to comment
Share on other sites

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:

863410f691d5407ebc8d5582b8184947.png

 

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?

Relatively new to modding.

Currently developing:

https://github.com/LambdaXV/DynamicGenerators

Link to comment
Share on other sites

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;
    }

Relatively new to modding.

Currently developing:

https://github.com/LambdaXV/DynamicGenerators

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 :P

 

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.

 

 

Relatively new to modding.

Currently developing:

https://github.com/LambdaXV/DynamicGenerators

Link to comment
Share on other sites

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.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • where opportunities abound and promises of financial prosperity beckon from every corner of the internet, the line between opportunity and deception becomes increasingly blurred. As a 38-year-old single mom, I embarked on a journey into the world of cryptocurrency investing, hoping to secure a brighter future for myself and my family. Little did I know, this journey would lead me down a treacherous path fraught with deception and heartbreak. My foray into cryptocurrency investing began with the promise of lucrative returns from a platform claiming to operate within Europe and South Asia. Blinded by optimism and the allure of financial gain, I entrusted my hard-earned savings to this fraudulent company, believing wholeheartedly in its legitimacy. However, my hopes were dashed when I began encountering difficulties with withdrawals and found myself entangled in a web of exorbitant fees and dubious practices. In a desperate bid to salvage what remained of my investment, I turned to a recovery company recommended to me by the very platform that had deceived me. Yet, even in my darkest hour, the deceit persisted, as I soon discovered that the company tasked with recovering my funds was complicit in the deception. Faced with the crushing realization that I had been betrayed once again, I felt a sense of hopelessness engulf me. It was in this moment of despair that I stumbled upon Lee Ultimate Hacker – a shining beacon of hope amidst the darkness of deception. Through a stroke of luck, I came across a blog post singing the praises of this remarkable team, and I knew I had found my savior. With nothing left to lose and everything to gain, I reached out to them, hoping against hope for a chance at redemption. From the outset, Lee Ultimate Hacker proved to be a guiding light in my journey toward financial recovery. Their professionalism, expertise, and unwavering commitment to client satisfaction set them apart from the myriad of recovery services in the digital sphere. With empathy and understanding, they listened to my story and embarked on a mission to reclaim what was rightfully mine. Through their diligent efforts and meticulous attention to detail, Lee Ultimate Hacker succeeded where others had failed, restoring a sense of hope and security in the wake of betrayal. Their dedication to justice and unwavering determination to deliver results ensured that I emerged from the ordeal stronger and more resilient than ever before. Throughout the recovery process, their team remained accessible, transparent, and supportive, offering guidance and reassurance every step of the way. To anyone grappling with devastating financial fraud, I offer a lifeline of hope – trust in Lee Ultimate Hacker to guide you through the storm with expertise and compassion. In a world rife with deception and uncertainty, they stand as a beacon of reliability and trustworthiness, ready to lead you toward financial restitution and a brighter future. If you find yourself in a similar predicament, do not hesitate to reach out to Lee Ultimate Hacker.AT LEEULTIMATEHACKER@ AOL. COM   Support @ leeultimatehacker . com.  telegram:LEEULTIMATE   wh@tsapp +1  (715) 314  -  9248  https://leeultimatehacker.com
    • Sorry, this is the report https://paste.ee/p/OeDj1
    • Please share a link to your crash report on https://paste.ee, as explained in the FAQ
    • This is the crash report [User dumped their crash report directly in their thread, triggering the anti-spam]  
    • Add the crash-report or latest.log (logs-folder) with sites like https://paste.ee/ and paste the link to it here  
  • Topics

×
×
  • Create New...

Important Information

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