Jump to content

Recommended Posts

Posted

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

Posted

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.

Posted

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.

Posted

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

Posted

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/

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



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • When I first heard about Bitcoin back in 2018, I was skeptical. The idea of a decentralized, digital currency seemed too good to be true. But I was intrigued as I learned more about the technology behind it and its potential. I started small, investing just a few hundred dollars, dipping my toes into the cryptocurrency waters. At first, it was exhilarating to watch the value of my investment grow exponentially. I felt like I was part of the future, an early adopter of this revolutionary new asset. But that euphoria was short-lived. One day, I logged into my digital wallet only to find it empty - my Bitcoin had vanished without a trace. It turned out that the online exchange I had trusted had been hacked, and my funds were stolen. I was devastated, both financially and emotionally. All the potential I had seen in Bitcoin was tainted by the harsh reality that with decentralization came a lack of regulation and oversight. My hard-earned money was gone, lost to the ether of the digital world. This experience taught me a painful lesson about the price of trust in the uncharted territory of cryptocurrency. While the technology holds incredible promise, the risks can be catastrophic if you don't approach it with extreme caution. My Bitcoin investment gamble had failed, and I was left to pick up the pieces, wiser but poorer for having placed my faith in the wrong hands. My sincere appreciation goes to MUYERN TRUST HACKER. You are my hero in recovering my lost funds. Send a direct m a i l ( muyerntrusted ( @ ) mail-me ( . )c o m ) or message on whats app : + 1 ( 4-4-0 ) ( 3 -3 -5 ) ( 0-2-0-5 )
    • You could try posting a log (if there is no log at all, it may be the launcher you are using, the FAQ may have info on how to enable the log) as described in the FAQ, however this will probably need to be reported to/remedied by the mod author.
    • So me and a couple of friends are playing with a shitpost mod pack and one of the mods in the pack is corail tombstone and for some reason there is a problem with it, where on death to fire the player will get kicked out of the server and the tombstone will not spawn basically deleting an entire inventory, it doesn't matter what type of fire it is, whether it's from vanilla fire/lava, or from modded fire like ice&fire/lycanites and it's common enough to where everyone on the server has experienced at least once or twice and it doesn't give any crash log. a solution to this would be much appreciated thank you!
    • It is 1.12.2 - I have no idea if there is a 1.12 pack
  • Topics

×
×
  • Create New...

Important Information

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