Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Featured Replies

Posted

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

One output is server-side, the "0" is on client. You need packets to update client's value. Note that you don't always need all data on clients (mostly for rendering).

1.7.10 is no longer supported by forge, you are on your own.

  • Author

They're in the exact same class, being called at the same time, there is no client and server effect in this at all.

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.

  • Author

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

  • Author

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.

  • Author

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?

  • Author

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.

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/

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...

Important Information

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

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.