Jump to content

1.8 - How to update a count based on the amount of an item in inv


HighRez

Recommended Posts

Hello!

I've recently started modding, and am running into a problem. 

I'm trying to make a mod that basically sets a goal for an item, and displays the amount currently in the inventory of the player.

 

This is what my code looks like right now: 

	String text;

    @SubscribeEvent
    public void LivingUpdateEvent(LivingEvent.LivingUpdateEvent event) {
        if (event.entity instanceof EntityPlayerMP) {
            EntityPlayerMP player = (EntityPlayerMP) event.entity;
            InventoryPlayer inventory = player.inventory;

            if (inventory.hasItem(diamond)) {
                for (int i = 0; i <= Constants.LISTLENGTH-1; i++) {
                    text = "(1/" + Constants.AMMOUNT.get(i) + ") " + Constants.ITEMS.get(i);
                }
            }
        }
    }



    public GuiNotif(Minecraft mc) {
        //Creates needed stuff
        ScaledResolution scaled = new ScaledResolution(mc);
        int width = scaled.getScaledWidth();
        int height = scaled.getScaledHeight();

        //Writes at the top left to start a list of sorts
        drawCenteredString(mc.fontRendererObj, "Needed:", width - 40, 10, Integer.parseInt("c40600", 16));



        //For loop to print the diffrent list elements
        for (int i = 0; i <= Constants.LISTLENGTH-1; i++) {
            text = "(0/" + Constants.AMMOUNT.get(i) + ") " + Constants.ITEMS.get(i);
            drawCenteredString(mc.fontRendererObj, text, width - 40, 10*i+20, Integer.parseInt("FFFFFF", 16));
        }
    }

This isn't working/is incorrect.  I'm assuming that the problem lays in the if inventory has diamond line, but I don't fully understand what isn't working here. On top of this, this was just a start - I also need to make it so that if there is more than one diamond in the inventory (in a stack/or not) that is added to the count, and would appreciate help on that aswell if anyone is willing.

Thanks!

-Rez

Link to comment
Share on other sites

1 hour ago, diesieben07 said:
  • Stop using 1.8, update.
  • Instead of LivingUpdateEvent and checking for EntityPlayer, use PlayerTickEvent.
  • What on earth is this monstrosity? Java has hexadecimal integer literals.

  • You are reaching across logical sides. You must compute the text on the client, not the server.

  • Why on earth are you drawing things in the constructor? You must draw onto the HUD every frame using RenderGameOverlayEvent.

Thank you for your time and help. 

1. Only piece of your advice I'm rejecting :P Not happening. I want this for 1.8.9, it's not otherwise useful to me.''

2. Thanks! Trying to get this to work, will update if it doesn't.

3. Oof, yes I started writing something that changed the color of the text earlier, and realized this but didn't update it here. Thanks for the reminder!

4. Oops, thank you. To be honest I don't totally understand this idea/proxy's in mods, but I'm sure I can figure it out. Thanks again.

5. Thanks one last time! I haven't used constructors much, and don't know there proper use really, I'll make sure to do this.

Link to comment
Share on other sites

2 hours ago, diesieben07 said:
  • Stop using 1.8, update.
  • Instead of LivingUpdateEvent and checking for EntityPlayer, use PlayerTickEvent.
  • What on earth is this monstrosity? Java has hexadecimal integer literals.

  • You are reaching across logical sides. You must compute the text on the client, not the server.

  • Why on earth are you drawing things in the constructor? You must draw onto the HUD every frame using RenderGameOverlayEvent.

Alright, I've to some degree started from scratch. I realized that I was off to a bad start, and hope I've started to get back on track... I didn't have Client/Common proxy classes before :l

 

I made a new class, which basically does the same thing as the last, and I THINK should work (or with a small amount of bug fixing), however for some reason event.entity isn't being recognized, and I don't know why. Here is my new class (with imports just to clarify it dosen't have anything to do with that):

import highrez.rezhigh.lib.Constants;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.Gui;
import net.minecraft.client.gui.ScaledResolution;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.fml.common.gameevent.TickEvent;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;

import static net.minecraft.init.Items.diamond;


@SideOnly(Side.CLIENT)
public class RenderTickHandler extends Gui{
    /** Stores an instance of Minecraft for easy access */
    private Minecraft mc;
    String text;

    // create a constructor that takes a Minecraft argument; now we have it whenever we need it
    public RenderTickHandler(Minecraft mc) {
        this.mc = mc;
    }

    @SubscribeEvent
    public void onRenderTick(TickEvent.RenderTickEvent event) {
        //Creates needed stuff
        ScaledResolution scaled = new ScaledResolution(mc);
        int width = scaled.getScaledWidth();
        int height = scaled.getScaledHeight();

        //Writes at the top left to start a list of sorts
        drawCenteredString(mc.fontRendererObj, "Needed:", width - 40, 10, Integer.parseInt("c40600", 16));



        //For loop to print the diffrent list elements
        for (int i = 0; i <= Constants.LISTLENGTH-1; i++) {
            text = "(0/" + Constants.AMMOUNT.get(i) + ") " + Constants.ITEMS.get(i);
            drawCenteredString(mc.fontRendererObj, text, width - 40, 10*i+20, Integer.parseInt("FFFFFF", 16));
        }


    }


    @SubscribeEvent
    public void PlayerTickeEvent(TickEvent.PlayerTickEvent event) {
        if (event.entity instanceof EntityPlayerMP) {
            EntityPlayerMP player = (EntityPlayerMP) event.entity;
            InventoryPlayer inventory = player.inventory;

            if (inventory.hasItem(diamond)) {
                for (int i = 0; i <= Constants.LISTLENGTH-1; i++) {
                    text = "(1/" + Constants.AMMOUNT.get(i) + ") " + Constants.ITEMS.get(i);
                }
            }
        }
    }
}

Thanks for your help.

Link to comment
Share on other sites

If you're trying to draw a HUD (overlay information) you usually do that in the RenderGameOverlayEvent. As mentioned by diesieben07 you should check events to make sure you handle the phase and also check for any sub-class events that are more specific for your needs (that simplifies the coding and improves performance).

 

For EntityPlayer, as diesieben07 also mentions, it may not be obvious but the MP sub-class is for the server. So ideally you'd use the client side sub-class but also in this case it would be okay to simply use the parent class, EntityPlayer itself, since all the methods you're calling on the player instance are common.

Check out my tutorials here: http://jabelarminecraft.blogspot.com/

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



×
×
  • Create New...

Important Information

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