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

I am trying to keep a hashmap in my TileEntity of all the neighbors which have the power capability. I can get and store the neighbors, but for some reason every tick the hashmap clears.

Code:

Update method:

@Override
    public void update() {
        if (lastConsume == 0) {
            if (fuel > 0 && power < max-production) {
                lastConsume = type.isFuel(getStack());
                power += production;
                ITEMHANDLER.extractItem(0, 1, false);
                fuel -= 1;
                onConsume();
                markDirty();
            } else {
                getWorld().setBlockState(pos, getWorld().getBlockState(getPos()).cycleProperty(MachineBlock.LIT));
                lastConsume -= 1;
                markDirty();
            }
        } else if (lastConsume == -1) {
            if (fuel > 0 && power < max-production) {
                power += production;
                lastConsume = type.isFuel(getStack());
                getWorld().setBlockState(pos, getWorld().getBlockState(getPos()).cycleProperty(MachineBlock.LIT));
                ITEMHANDLER.extractItem(0, 1, false);
                fuel -= 1;
                onConsume();
                markDirty();
            }
        } else if(power < max-production){
            lastConsume -= 1;
            power += production;
        }
        if(power > exportSpeed) {
            if (power > exportSpeed*neighbors.size()) {
                System.out.println(neighbors.size());
                for(EnumFacing face : neighbors.keySet())
                    System.out.println("Power at " + face.getName());
                for (IEnergyStorage storage : neighbors.values()) {
                    power -= storage.receiveEnergy(exportSpeed, false);
                }
            } else {
                for (IEnergyStorage storage : neighbors.values()) {
                    power -= storage.receiveEnergy(exportSpeed, false);
                    if (power < exportSpeed) return;
                }
            }
        }
    }

Neighbor finding method (Called in the block neighborUpdated method):

public void onNeighborUpdated(BlockPos pos, World world, EnumFacing face, BlockPos fromPos) {
        if(world.getBlockState(fromPos).getBlock() == Blocks.AIR) {
            System.out.println("Removing neighbor!");
            neighbors.remove(face);
        }
        TileEntity neighbor = world.getTileEntity(fromPos);
        if(neighbor == null) return;
        if(neighbor.hasCapability(CapabilityEnergy.ENERGY, face)) {
            System.out.println("Added neighbor at " + face.getName());
            neighbors.put(face, neighbor.getCapability(CapabilityEnergy.ENERGY, face));
            System.out.println(neighbors.size());
        }
    }

Removing neighbor! is not printed unless the block is removed, but every tick the hashmap is cleared. Whenever I add a neighbor, the hashmap length is the correct length.

So basically: In update(), the hashmap is the empty, in onNeighborUpdated(), the hashmap is correct.

Edited by diesieben07
syntax highlighting

  • Author

It seems there are two instances of the TileEntity in the same position.

By printing "this", I see two different TE instances, one gotten through world.getTileEntity, and never has the update method called, and one that has update called, but is not returned by the getTileEntity method. I am guessing this is because one is being run on client and the other is server side.

I have my proxy setup, but I have two questions:

1. Which side should is the update method called on, and which side is neighborChanged() in the class Block called in?

2. How should I set the side? (Through proxy or through the annotation?)

1. The server. Because the server has authority over the world.

2. You don't have to do anything.

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.

  • Author
9 hours ago, Draco18s said:

1. The server. Because the server has authority over the world.

2. You don't have to do anything.

Then how do I get the correct instance that update is being called on? world.getTileEntity is giving me the wrong one.

You don't want to set anything here, what you have to do is query the side. Check if world#isRemote is false.

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.

  • Author
2 hours ago, Draco18s said:

You don't want to set anything here, what you have to do is query the side. Check if world#isRemote is false.

That was it, thanks!

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.