110 lines
3.9 KiB
Java
110 lines
3.9 KiB
Java
import arzumify.polyenergy.api.EnergyProvider;
|
|
import arzumify.polyenergy.api.EnergyReceiver;
|
|
import arzumify.polyenergy.impl.CoordinateMatchMaker;
|
|
import arzumify.presence.presences.IntegerPresence;
|
|
import arzumify.presence.presences.Presence;
|
|
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerBlockEntityEvents;
|
|
import net.minecraft.block.entity.BlockEntity;
|
|
import net.minecraft.server.world.ServerWorld;
|
|
import net.minecraft.util.math.Vec3i;
|
|
|
|
import java.util.Set;
|
|
import java.util.concurrent.ConcurrentHashMap;
|
|
|
|
/**
|
|
* SimpleBattery, but implemented using only the API and not the actual block entity for testing purposes.
|
|
* All fields are public for testing purposes.
|
|
*/
|
|
public class SimpleCodeOnlyBattery implements EnergyProvider, EnergyReceiver, ServerBlockEntityEvents.Unload, ServerBlockEntityEvents.Load {
|
|
public final long capacity;
|
|
public final long inputRate;
|
|
public final long outputRate;
|
|
public final IntegerPresence presence;
|
|
public final String name;
|
|
public final Set<EnergyProvider> providers = ConcurrentHashMap.newKeySet();
|
|
public long energy = 0;
|
|
public boolean providing = false;
|
|
|
|
public SimpleCodeOnlyBattery(Vec3i pos, long capacity, long inputRate, long outputRate, String name) {
|
|
this.capacity = capacity;
|
|
this.inputRate = inputRate;
|
|
this.outputRate = outputRate;
|
|
this.name = name;
|
|
this.presence = new IntegerPresence((short) 1, new arzumify.presence.maths.Vec3i(pos.getX(), pos.getY(), pos.getZ()));
|
|
CoordinateMatchMaker.addProvider(this);
|
|
CoordinateMatchMaker.addReceiver(this);
|
|
}
|
|
|
|
public static void tick(SimpleCodeOnlyBattery battery) {
|
|
System.out.println(battery.name + " ticking: list is " + battery.providers);
|
|
var leftToFill = Math.min(battery.inputRate, battery.capacity - battery.energy);
|
|
for (EnergyProvider provider : battery.providers) {
|
|
// Less than zero shouldn't be possible, but just in case...
|
|
if (leftToFill <= 0) {
|
|
break;
|
|
}
|
|
long extracted = provider.extract(leftToFill, battery);
|
|
battery.energy += extracted;
|
|
leftToFill -= extracted;
|
|
}
|
|
if (!battery.providing && battery.energy > 0) {
|
|
CoordinateMatchMaker.addProvider(battery);
|
|
battery.providing = true;
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public long extract(long amount, EnergyReceiver receiver) {
|
|
if (providing) {
|
|
System.out.println(name + " extracting " + amount + " energy");
|
|
long extracted = Math.min(Math.min(outputRate, amount), energy);
|
|
energy -= extracted;
|
|
System.out.println(name + " extracted " + extracted + " energy");
|
|
if (energy == 0) {
|
|
CoordinateMatchMaker.removeProvider(this);
|
|
}
|
|
return extracted;
|
|
} else {
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void exists(EnergyReceiver receiver) {
|
|
System.out.println(name + " found other receiver");
|
|
receiver.ready(this);
|
|
}
|
|
|
|
@Override
|
|
public void ready(EnergyProvider provider) {
|
|
System.out.println(name + " found other provider");
|
|
providers.add(provider);
|
|
}
|
|
|
|
@Override
|
|
public void unready(EnergyProvider provider) {
|
|
System.out.println(name + " lost other provider");
|
|
providers.remove(provider);
|
|
}
|
|
|
|
@Override
|
|
public void onLoad(BlockEntity blockEntity, ServerWorld serverWorld) {
|
|
if (energy > 0) {
|
|
CoordinateMatchMaker.addProvider(this);
|
|
providing = true;
|
|
}
|
|
CoordinateMatchMaker.addReceiver(this);
|
|
}
|
|
|
|
@Override
|
|
public void onUnload(BlockEntity blockEntity, ServerWorld serverWorld) {
|
|
CoordinateMatchMaker.removeProvider(this);
|
|
CoordinateMatchMaker.removeReceiver(this);
|
|
providers.clear();
|
|
}
|
|
|
|
@Override
|
|
public Presence<IntegerPresence> presence() {
|
|
return presence;
|
|
}
|
|
}
|