|
| 1 | +package yancey.openparticle.api.common.data; |
| 2 | + |
| 3 | +import yancey.openparticle.api.common.controller.SimpleParticleController; |
| 4 | +import yancey.openparticle.api.common.data.particle.DataParticle; |
| 5 | + |
| 6 | +import java.io.DataInputStream; |
| 7 | +import java.io.DataOutputStream; |
| 8 | +import java.io.IOException; |
| 9 | +import java.util.ArrayList; |
| 10 | +import java.util.Comparator; |
| 11 | +import java.util.List; |
| 12 | + |
| 13 | +public class DataParticleManager { |
| 14 | + |
| 15 | + public static List<DataParticle> dataParticleList = new ArrayList<>(); |
| 16 | + public final List<DataParticle> parents; |
| 17 | + |
| 18 | + public DataParticleManager() { |
| 19 | + parents = new ArrayList<>(); |
| 20 | + } |
| 21 | + |
| 22 | + public DataParticleManager(DataInputStream dataInputStream) throws IOException { |
| 23 | + int size1 = dataInputStream.readInt(); |
| 24 | + dataParticleList = new ArrayList<>(size1); |
| 25 | + for (int i = 0; i < size1; i++) { |
| 26 | + dataParticleList.add(DataParticle.readFromFile(this, dataInputStream)); |
| 27 | + } |
| 28 | + int size2 = dataInputStream.readInt(); |
| 29 | + parents = new ArrayList<>(size2); |
| 30 | + for (int i = 0; i < size2; i++) { |
| 31 | + parents.add(getDataParticle(dataInputStream.readInt())); |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + public void add(DataParticle dataParticle) { |
| 36 | + parents.add(dataParticle); |
| 37 | + } |
| 38 | + |
| 39 | + public void writeToFile(DataOutputStream dataOutputStream) throws IOException { |
| 40 | + dataOutputStream.writeInt(dataParticleList.size()); |
| 41 | + for (DataParticle dataParticle : dataParticleList) { |
| 42 | + dataParticle.writeToFile(dataOutputStream); |
| 43 | + } |
| 44 | + dataOutputStream.writeInt(parents.size()); |
| 45 | + for (DataParticle parent : parents) { |
| 46 | + dataOutputStream.writeInt(parent.id); |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + public DataParticle getDataParticle(int id) { |
| 51 | + return dataParticleList.get(id); |
| 52 | + } |
| 53 | + |
| 54 | + public long getParticleCount() { |
| 55 | + return parents.stream() |
| 56 | + .flatMap(DataParticle::streamParticleSingle) |
| 57 | + .count(); |
| 58 | + } |
| 59 | + |
| 60 | + public DataRunningPerTick[] getDataRunningList() { |
| 61 | + List<DataRunningPerTick> dataRunningList = new ArrayList<>(); |
| 62 | + parents.stream() |
| 63 | + .flatMap(parent -> parent.getNode().second) |
| 64 | + .toList() |
| 65 | + .stream() |
| 66 | + .map(SimpleParticleController::new) |
| 67 | + .toList() |
| 68 | + .stream() |
| 69 | + .forEach(controller -> { |
| 70 | + DataRunningPerTick.getFromList(controller.getTickStart(), dataRunningList).controllerList.add(controller); |
| 71 | + }); |
| 72 | + dataRunningList.sort(Comparator.comparingInt(dataRunningPerTick -> dataRunningPerTick.tick)); |
| 73 | + return dataRunningList.toArray(new DataRunningPerTick[0]); |
| 74 | + } |
| 75 | +} |
0 commit comments