Jump to content

I think I'm just blind, or plain stupid.


TomTheBeast

Recommended Posts

Beacuse I'm lazy there's only a TLDR.

TL;DR: I have two methods in the same class, called at the same time in this case.  One of them outputs the correct 'energy' value, the other always returns 0, why?

 

Works:

@Override
    protected boolean addEnergy(int energy)
    {
        if (this.energy >= (this.energyCap * this.energyCapMultiplier))
        {
            return false;
        }
        this.energy += energy;
        if (this.energy > this.energyCap * this.energyCapMultiplier)
            this.energy = this.energyCap * this.energyCapMultiplier;
        System.out.println(this.getName() + " at " + this.getPos().toString() + ", added " + energy + " " + Reference.ENERGY_UNIT + "; it now has " + this.energy + " " + Reference.ENERGY_UNIT + ".");
        return true;
    }

 

DOESN'T WORK:

protected void sendEnergy()
    {
        if (checkLink()) 
        {
            int minEnergy = (getEnergyDistributeCap() * getEnergyDistributeCapMultiplier()) / 4;
            if (energy >= minEnergy)
            {
                if (link.sendEnergy(minEnergy, this.getPos()))
                {
                    //removeEnergy(minEnergy);
                }
            } 
            else
            {
                System.out.println("Not enough energy, MinEnergy:" + minEnergy + ", EnergyDistributeCap:" + getEnergyDistributeCap() + ", EDCM:" + getEnergyDistributeCapMultiplier() + ", Energy:" + energy);
            }
        }
    }

Link to comment
Share on other sites

How can the 2nd method even return zero? It's return "type" is void, so assigning it to an L-value should earn you a warning from Eclipse.

The debugger is a powerful and necessary tool in any IDE, so learn how to use it. You'll be able to tell us more and get better help here if you investigate your runtime problems in the debugger before posting.

Link to comment
Share on other sites

Post more code, where are these methods called from?

 

It's all in a tile entity class, and called on update,

 

update

@Override
    public void update()
    {
        generateEnergy();
        sendEnergy();

    }

 

sendEnergy()

protected void generateEnergy()
    {
        if (fuel != null)
        {
            int value = Fuel.getFuelValue(fuel.getUnlocalizedName().substring(5));
            if (sendEnergy(value, null))
            {
                decrStackSize(0, 1);
            }
        }
    }

 

sendEnergy(int,BlockPos)

@Override
    public boolean sendEnergy(int energy, BlockPos pos)
    {
        energyRecievedBlockPos = pos;
        return addEnergy(energy);
    }

 

addEnergy(above in first post)

@Override
    protected boolean addEnergy(int energy)
    {
        if (this.energy >= (this.energyCap * this.energyCapMultiplier))
        {
            return false;
        }
        this.energy += energy;
        if (this.energy > this.energyCap * this.energyCapMultiplier)
        {
            this.energy = this.energyCap * this.energyCapMultiplier;
        }
        System.out.println(this.getName() + " at " + this.getPos().toString() + ", added " + energy + " " + Reference.ENERGY_UNIT + "; it now has " + this.energy + " " + Reference.ENERGY_UNIT + ".");
        return true;
    }

 

sendEnergy() (posted above in firstpost)

protected void sendEnergy()
    {
        if (checkLink())
        {
            int minEnergy = (getEnergyDistributeCap() * getEnergyDistributeCapMultiplier()) / 4;
            if (energy >= minEnergy)
            {
                if (link.sendEnergy(minEnergy, this.getPos()))
                {
                    //removeEnergy(minEnergy);
                }
            } else
            {
                System.out.println("Not enough energy, MinEnergy:" + minEnergy + ", EnergyDistributeCap:" + getEnergyDistributeCap() + ", EDCM:" + getEnergyDistributeCapMultiplier() + ", Energy:" + energy);
            }
        }
    }

Link to comment
Share on other sites

How can the 2nd method even return zero? It's return "type" is void, so assigning it to an L-value should earn you a warning from Eclipse.

 

I just used the term return to be lazy, I mean when the method reaches the sout, it prints 0, every time, where as the other method works perfectly fine and prints the correct value.

Link to comment
Share on other sites

Already spotted a problem: update() is called on client and server, but energy transfers should be server only.

 

I'm new to MC modding, could you explain why? I kinda understand the concept of what you mean, but I don't follow fully; and how could I prevent that? using the World.isRemote boolean?

Link to comment
Share on other sites

Well, Minecraft runs in "two parts". Server and Client. The server simulates the world, the client just displays it (basically). You can check whether or you are on server or client by using world.isRemote, yes.

 

Right, that's obvious, but how does the client/server work together in SMP and SP?  Like in SP, is there a instance of the server and client running?, and if there is a instance of a server and client running, would there be any real difference than running a SMP server, to where the client/server aren't integrated into eachother?

I'm just trying to think ahead incase I have to do something special for something to work on SMP if it works fine on SP.

Link to comment
Share on other sites

Well, Minecraft runs in "two parts". Server and Client. The server simulates the world, the client just displays it (basically). You can check whether or you are on server or client by using world.isRemote, yes.

 

Right, that's obvious, but how does the client/server work together in SMP and SP?  Like in SP, is there a instance of the server and client running?, and if there is a instance of a server and client running, would there be any real difference than running a SMP server, to where the client/server aren't integrated into eachother?

I'm just trying to think ahead incase I have to do something special for something to work on SMP if it works fine on SP.

 

Yes, for the most part in SP you can think of it as a "separate" server and client both running in same JVM. That is the one gotcha though -- that it is in same JVM -- because when you get to the topic of sided proxies there are some classes that are only guaranteed to be loaded on one side or the other. In SMP there is different JVM running the server and client so if you don't handle the sided proxy properly it will fail, however such problems can be masked in JVM because each side might still be able to "see" the class loaded on the other side. Anyway, my point is you should test pretty regularly in SMP because SP can mask some issues. Hopefully you see what I mean.

 

By the way, using Eclipse you can launch the server and client (or even multiple clients) simultaneously provided you have enough system memory in your computer. So you can test SMP on single computer.

 

Some people get confused about the idea of sided proxy versus the World#isRemote check. World#isRemote is used in cases where the code may run on both sides and you need to distinguish (this is your case with the onUpdate() method in your question above). Side proxies are used when you need to access classes, methods or fields that are marked @SideOnly and therefore are only guaranteed to load on that side.

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.