Jump to content

HashMap in TileEntity being cleared?


Big_Bad_E

Recommended Posts

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
Link to comment
Share on other sites

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?)

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

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.