Jump to content

Recommended Posts

Posted

Hey there,

 

So I have a prototype cable here, to transfer energy across a line, which detecting TEs along the way,

if one is found it sends energy to them.

 

However, it does not work, as in, the energy IS not transfer if one is found.

 

Here is the class:

public class TileEntityPipeEnergy extends TileEntityBase implements ITickable, ICustomEnergyReceiver, ISharingEnergyProvider {

    public EnergyStorage energyStorage = new EnergyStorage(6000, 6000);

    public TileEntityPipeEnergy() { super("energypipe"); }

    @Override
    public void updateEntity() {
        super.updateEntity();
        List<EnumFacing> possibleSides = getPossibleSides();
        if (!possibleSides.isEmpty()) {
            int forEach = energyStorage.getEnergyStored() / possibleSides.size();
            for (EnumFacing side : possibleSides) {
                BlockPos neighbor = pos.offset(side);
                TileEntity tile = worldObj.getTileEntity(neighbor);
                if (tile != null) {
                    if (tile instanceof ICustomEnergyReceiver) {
                        transferEnergyToTile((ICustomEnergyReceiver) tile, side, forEach);
                    } else if (tile instanceof ICustomEnergyReceiver) {
                        transferEnergyToTile((ICustomEnergyReceiver) tile, side, forEach);
                    }
                }
            }
        }
    }

    private void transferEnergyToTile(ICustomEnergyReceiver receiver, EnumFacing side, int energyAmount) {
        if (receiver.canConnectEnergy(side.getOpposite())) {
            receiver.receiveEnergy(side.getOpposite(), extractEnergy(side, energyAmount, false), false);
        }
    }

    public List<EnumFacing> getPossibleSides() {
        List<EnumFacing> sides = new ArrayList<EnumFacing>();
        for (EnumFacing side : EnumFacing.VALUES) {
            BlockPos neighbor = pos.offset(side);
            TileEntity tile = worldObj.getTileEntity(neighbor);
            if (tile != null) {
                if (tile instanceof IEnergyReceiver) {
                    sides.add(side);
                }
            }
        }

        if (sides.size() != 0) {
            int forEach = energyStorage.getEnergyStored() / sides.size();

            for (EnumFacing side : EnumFacing.VALUES) {
                if (sides.contains(side)) {
                    BlockPos neighbor = pos.offset(side);
                    TileEntity tile = worldObj.getTileEntity(neighbor);
                    if (tile != null && tile instanceof ICustomEnergyReceiver) {
                        ICustomEnergyReceiver receiver = (ICustomEnergyReceiver) tile;
                        if (receiver.getEnergyStored(side.getOpposite()) + forEach > receiver.getMaxEnergyStored(side.getOpposite())) {
                            sides.remove(side);
                        }
                        if (!receiver.canConnectEnergy(side.getOpposite())) {
                            sides.remove(side);
                        }
                    }
                }
            }
        }
        return sides;
    }

    public int getEnergyToSplitShare() {
        return this.getEnergyStored();
    }

    public boolean doesShareEnergy() {
        return true;
    }

    public EnumFacing[] getEnergyShareSides() {
        return EnumFacing.values();
    }

    @Override
    public void writeSyncableNBT(NBTTagCompound compound, NBTType type){
        super.writeSyncableNBT(compound, type);
        this.energyStorage.writeToNBT(compound);
    }

    @Override
    public void readSyncableNBT(NBTTagCompound compound, NBTType type){
        super.readSyncableNBT(compound, type);
        this.energyStorage.readFromNBT(compound);
    }

    @Override
    public int getEnergyStored(EnumFacing from) {
        return energyStorage.getEnergyStored();
    }

    @Override
    public int getMaxEnergyStored(EnumFacing from) {
        return energyStorage.getMaxEnergyStored();
    }

    public int getEnergyStored() {
        return energyStorage.getEnergyStored();
    }

    public int getMaxEnergyStored() {
        return energyStorage.getMaxEnergyStored();
    }

    @Override
    public boolean canConnectEnergy(EnumFacing from) {
        return worldObj.isBlockIndirectlyGettingPowered(pos) >= 1;
    }

    @Override
    public int extractEnergy(EnumFacing from, int maxExtract, boolean simulate) {
        return energyStorage.extractEnergy(maxExtract, simulate);
    }

    @Override
    public int receiveEnergy(EnumFacing from, int maxReceive, boolean simulate) {
        return energyStorage.receiveEnergy(maxReceive, simulate);
    }

}

 

Thanks for your time.

Relatively new to modding.

Currently developing:

https://github.com/LambdaXV/DynamicGenerators

Posted

Why are you not using Capabilities?

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.

Posted

I'm not particularly good with them :/ Where should I read up on them? And how would I apply them to this?

Forge provides a Energy Capability you just need to override getCapability and hasCapability in your TE. This is also what you should be doing for inventories using the IItemHandler capability.

Look at the forge docs

http://mcforge.readthedocs.io/en/latest/datastorage/capabilities/

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

... So something like this?

 

    @Override
    public boolean hasCapability(Capability<?> capability, EnumFacing facing) {
        if(capability == CapabilityEnergy.ENERGY) {
            return true;
        }
        return super.hasCapability(capability, facing);
    }

    @Override
    public <T> T getCapability(Capability<T> capability, EnumFacing facing) {
        if (capability == CapabilityEnergy.ENERGY) {
            return (T) energyStorage;
        }
        return super.getCapability(capability, facing);
    }

 

at the bottom of the class? I'm handling the energy with CoFH API.

Relatively new to modding.

Currently developing:

https://github.com/LambdaXV/DynamicGenerators

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.