Jump to content

[1.11] Sharing Energy Issue


Lambda

Recommended Posts

Hey there!

 

So I have a block, 'tethers' that share energy to nearby TEs that have the Energy Capability.. However I'm having issues actually getting this working, I've changed much code, but with no avail. So I came to post this code here, some methods you should look at is the updateEntity(), receiveEnergy(), extractEnergy().. Currently the forloop of 'energyUsers' is not complete, because I knew that wasn't going to work, I need some insight...

 

public class TileEntityNetworkTethers extends TileEntityBase implements ISharingEnergyHandler, IEnergyDisplay {

    public CustomEnergyStorage storage = new CustomEnergyStorage(50000, 50, 50);

    public static final int MAX_SEND = 50;

    public List<TileEntity> energyUsers = new ArrayList<>();

    public TileEntityNetworkTethers() {
        super("networkTethers");
    }

    @Override
    public void updateEntity() {
        super.updateEntity();

        if(!world.isRemote) {
            Iterable<BlockPos> blockPos = BlockPos.getAllInBox(pos.add(-5, -5, -5),
                                                               pos.add(+5, +5, +5));
            for (BlockPos position : blockPos) {
                TileEntity te = world.getTileEntity(position);
                if (te != null) {
                    if (!energyUsers.contains(te)) {
                        energyUsers.add(te);
                    }
                }
            }

            if(energyUsers.size() != 0) {
                for(TileEntity te : energyUsers) {
                    if(te != null) {
                        boolean isProducer = false;
                        int energy = 0;
                        for(EnumFacing facing : EnumFacing.VALUES) {
                            if(isProducer(te, facing)) {
                                energy = extractEnergy(te, facing, MAX_SEND);
                            }else {
                                energy = receiveEnergy(te, facing, MAX_SEND);
                            }
                        }
                    }
                }
            }

            validateList();

            if(sendUpdateWithInterval()) {

            }
        }
    }

    public boolean isProducer(TileEntity te, EnumFacing facing) {
        if (te.hasCapability(CapabilityEnergy.ENERGY, facing)) {
            IEnergyStorage cap = te.getCapability(CapabilityEnergy.ENERGY, facing);
            if (cap != null) {
                if(cap.canReceive()) {
                    return false;
                }
            }
        }
        return true;
    }


    public int receiveEnergy(TileEntity te, EnumFacing facing, int amount) {
        if (te.hasCapability(CapabilityEnergy.ENERGY, facing)) {
            IEnergyStorage cap = te.getCapability(CapabilityEnergy.ENERGY, facing);
            if (cap != null) {
                return cap.receiveEnergy(amount, false);
            }
        }
        return 0;
    }

    public int extractEnergy(TileEntity te, EnumFacing facing, int amount) {
        if (te.hasCapability(CapabilityEnergy.ENERGY, facing)) {
            IEnergyStorage cap = te.getCapability(CapabilityEnergy.ENERGY, facing);
            if (cap != null) {
                return cap.extractEnergy(amount, false);
            }
        }
        return 0;
    }

    public void validateList() {
        Iterator<TileEntity> iterator = energyUsers.iterator();
        while (iterator.hasNext()) {
            TileEntity te = iterator.next();
            if (te.isInvalid()) {
                iterator.remove();
            }
        }
    }

    @Override
    public IEnergyStorage getEnergyStorage(EnumFacing facing) {
        return storage;
    }

    @Override
    public int getEnergyToSplitShare() {
        return storage.getEnergyStored();
    }

    @Override
    public boolean doesShareEnergy() {
        return true;
    }

    @Override
    public EnumFacing[] getEnergyShareSides() {
        return EnumFacing.VALUES;
    }

    @Override
    public boolean canShareTo(TileEntity tile) {
        return true;
    }

    @Override
    public CustomEnergyStorage getEnergyStorage() {
        return storage;
    }

    @Override
    public boolean needsHoldShift() {
        return false;
    }
}

 

The effect I want to have happen:

>Pulls energy from anywhere in the 5x5 space.

>With that pulled energy it is buffered within the tether,

- Then it goes to either two places:

>>Another tether, in which they balance out their energy

>>A machine that is requesting power (in need of power)

 

Thanks for your time! :)

Relatively new to modding.

Currently developing:

https://github.com/LambdaXV/DynamicGenerators

Link to comment
Share on other sites

Check a 5x5 area and extract power from the energy providers. Then check the 5x5 space again and transmit the energy to 'machines'. Finally balance the energy in the tethers within the 5x5 area. You should do it in steps not all at once. Also something I feel I need to say. Ender IO works on a push based system, meaning you cannot extract from Ender IO generators or capacitors. They will have to push to you.

VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING

I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect.

Forge and vanilla BlockState generator.

Link to comment
Share on other sites

Yeah I was afraid of something like this.. Maybe there is a way to prioritize..? but the EnderIO github isn't update, so I don't know how they handle that..

 

Make sure you're looking at the right branch. The 1.10 branch was committed to 3 hours ago.

Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.

Link to comment
Share on other sites

Okay, an update:

 

So now I've got a lot of this working, however it seems sometimes it gives energy to tiles that have 'MaxReceive' to 0 and sometimes no power is taken / received into the tether.. Is there a better way of coding this?

public class TileEntityNetworkTethers extends TileEntityBase implements ISharingEnergyHandler, IEnergyDisplay {

    public CustomEnergyStorage storage = new CustomEnergyStorage(50000, 50, 50);

    public static final int MAX_SEND = 50;

    public ConcurrentHashMap<TileEntity, EnumFacing> energyUsers = new ConcurrentHashMap<>();

    private int oldEnergy;

    public TileEntityNetworkTethers() {
        super("networkTethers");
    }

    @Override
    public void updateEntity() {
        super.updateEntity();

        if(!world.isRemote) {
            Iterable<BlockPos> blockPos = BlockPos.getAllInBox(pos.add(-5, -5, -5),
                    pos.add(+5, +5, +5));
            for(BlockPos bPos : blockPos) {
                TileEntity te = world.getTileEntity(bPos);
                for(EnumFacing facing : EnumFacing.VALUES) {
                    if(te != null) {
                        energyUsers.put(te, facing);
                    }
                }
            }

            for(TileEntity tileEntity : energyUsers.keySet()) {
                for(EnumFacing facing : energyUsers.values()) {
                    if(tileEntity.hasCapability(CapabilityEnergy.ENERGY, facing)) {
                        IEnergyStorage cap = tileEntity.getCapability(CapabilityEnergy.ENERGY, facing);
                        if(cap != null) {
                            handleEnergy(tileEntity, cap);
                        }
                    }
                }
            }

            validateMap();

            if (this.oldEnergy != this.storage.getEnergyStored() && this.sendUpdateWithInterval()) {
                this.oldEnergy = this.storage.getEnergyStored();
            }
        }
    }

    public void handleEnergy(TileEntity te, IEnergyStorage cap) {
        if (te instanceof TileEntityNetworkTethers) {
            IEnergyStorage theirStorage = ((TileEntityNetworkTethers) te).getEnergyStorage();
            if(theirStorage.getEnergyStored() + MAX_SEND <= theirStorage.getMaxEnergyStored() || storage.getEnergyStored() + MAX_SEND <= storage.getMaxEnergyStored()) {
                if (theirStorage.getEnergyStored() != storage.getEnergyStored()) {
                    solveDifference(theirStorage, storage);
                }
            }
        }

        if (cap.canExtract()) {
            int theirEnergyStored = cap.getEnergyStored();
            if (theirEnergyStored - MAX_SEND >= 0) {
                cap.extractEnergy(MAX_SEND, false);
                this.storage.receiveEnergy(MAX_SEND, false);
            }
        }

        if (cap.canReceive()) {
            int theirEnergyStored = cap.getEnergyStored();
            int theirMaxEnergyStored = cap.getMaxEnergyStored();

            if (theirEnergyStored + MAX_SEND <= theirMaxEnergyStored) {
                if(storage.getEnergyStored() - MAX_SEND >= 0) {
                    storage.extractEnergyInternal(MAX_SEND, false);
                    cap.receiveEnergy(MAX_SEND, false);
                }
            }
        }
    }

    public void validateMap() {
        for(TileEntity te : energyUsers.keySet()) {
            if(te != null) {
                if(te.isInvalid()) {
                    energyUsers.remove(te);
                }
            }
        }
    }

    public void solveDifference(IEnergyStorage theirStorage, IEnergyStorage thisStorage) {
        int difference = Math.abs(theirStorage.getEnergyStored()-thisStorage.getEnergyStored());
        System.out.println(difference);
        if(theirStorage.receiveEnergy(difference/2, false) <= theirStorage.getMaxEnergyStored() && thisStorage.extractEnergy(difference / 2, false) >= 0) {
            theirStorage.receiveEnergy(difference / 2, false);
            thisStorage.extractEnergy(difference / 2, false);
        }
    }

    @Override
    public IEnergyStorage getEnergyStorage(EnumFacing facing) {
        return storage;
    }

    @Override
    public int getEnergyToSplitShare() {
        return storage.getEnergyStored();
    }

    @Override
    public boolean doesShareEnergy() {
        return true;
    }

    @Override
    public EnumFacing[] getEnergyShareSides() {
        return EnumFacing.VALUES;
    }

    @Override
    public boolean canShareTo(TileEntity tile) {
        return true;
    }

    @Override
    public CustomEnergyStorage getEnergyStorage() {
        return storage;
    }

    @Override
    public boolean needsHoldShift() {
        return false;
    }
}

 

Also, my github is updated..

 

Thanks.

Relatively new to modding.

Currently developing:

https://github.com/LambdaXV/DynamicGenerators

Link to comment
Share on other sites

The way you are using a

Map<TileEntity, EnumFacing>

is not good. Everytime you call

HashMap#put

, it overrides the previous value for that key. So if a

TileEntity

has multiple sides, only the last will retain in the

Map

.

for(TileEntity tileEntity : energyUsers.keySet()) {
               for(EnumFacing facing : energyUsers.values()) {
                    if(tileEntity.hasCapability(CapabilityEnergy.ENERGY, facing)) {
                        IEnergyStorage cap = tileEntity.getCapability(CapabilityEnergy.ENERGY, facing);
                        if(cap != null) {
                            handleEnergy(tileEntity, cap);
                        }
                    }
                }
            }

This is wrong, especially the for loops. You go through each

TileEntity

, then go through every side in the

Map

(hint: only the last from each

TileEntity

). This doesn't make sense. Just have a

List<TileEntity>

to save them, then go through each side when in the part of the code when you need the energy from the

TileEntity

.

Don't PM me with questions. They will be ignored! Make a thread on the appropriate board for support.

 

1.12 -> 1.13 primer by williewillus.

 

1.7.10 and older versions of Minecraft are no longer supported due to it's age! Update to the latest version for support.

 

http://www.howoldisminecraft1710.today/

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.