|
| 1 | +package com.github.litermc.vtil.api.storage; |
| 2 | + |
| 3 | +import com.github.litermc.vtil.Constants; |
| 4 | + |
| 5 | +import net.minecraft.nbt.CompoundTag; |
| 6 | +import net.minecraft.server.level.ServerLevel; |
| 7 | +import net.minecraft.world.level.saveddata.SavedData; |
| 8 | + |
| 9 | +import org.valkyrienskies.core.api.ships.PhysShip; |
| 10 | +import org.valkyrienskies.core.api.ships.ServerShip; |
| 11 | +import org.valkyrienskies.core.api.ships.Ship; |
| 12 | + |
| 13 | +import java.lang.reflect.Constructor; |
| 14 | +import java.lang.reflect.InvocationTargetException; |
| 15 | +import java.lang.reflect.Modifier; |
| 16 | +import java.util.Map; |
| 17 | +import java.util.concurrent.ConcurrentHashMap; |
| 18 | +import java.util.function.Supplier; |
| 19 | + |
| 20 | +public final class ShipDataStorage extends SavedData { |
| 21 | + private static final String DATA_NAME = Constants.MOD_ID + "_ShipAdditionalDatas"; |
| 22 | + private static volatile ShipDataStorage INSTANCE = null; |
| 23 | + |
| 24 | + private final Map<Long, Holder> holders = new ConcurrentHashMap<>(); |
| 25 | + |
| 26 | + private ShipDataStorage() {} |
| 27 | + |
| 28 | + private ShipDataStorage(final CompoundTag shipsData) { |
| 29 | + for (final String shipIdHex : shipsData.getAllKeys()) { |
| 30 | + final long shipId = Long.parseUnsignedLong(shipIdHex, 16); |
| 31 | + this.holders.put(shipId, this.new Holder(shipId, shipsData.getCompound(shipIdHex))); |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + public static Holder get(final Ship ship) { |
| 36 | + if (ship instanceof ServerShip || ship instanceof PhysShip) { |
| 37 | + return get(ship.getId()); |
| 38 | + } |
| 39 | + throw new IllegalArgumentException("Unexpected ship " + ship.getClass().getName() + " must be either ServerShip or PhysShip"); |
| 40 | + } |
| 41 | + |
| 42 | + public static Holder get(final long shipId) { |
| 43 | + final ShipDataStorage instance = INSTANCE; |
| 44 | + if (instance == null) { |
| 45 | + throw new IllegalStateException("ShipDataStorage is not initialized. Is overworld loaded?"); |
| 46 | + } |
| 47 | + return instance.holders.computeIfAbsent(shipId, (shipId0) -> instance.new Holder(shipId0, new CompoundTag())); |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * module-private |
| 52 | + */ |
| 53 | + public static void onShipRemoved(final long shipId) { |
| 54 | + final ShipDataStorage instance = INSTANCE; |
| 55 | + if (instance == null) { |
| 56 | + return; |
| 57 | + } |
| 58 | + instance.holders.remove(shipId); |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * module-private |
| 63 | + */ |
| 64 | + public static void onOverworldLoad(final ServerLevel overworld) { |
| 65 | + if (overworld == null) { |
| 66 | + INSTANCE = null; |
| 67 | + return; |
| 68 | + } |
| 69 | + INSTANCE = overworld.getDataStorage().computeIfAbsent( |
| 70 | + (data) -> new ShipDataStorage(data), |
| 71 | + () -> new ShipDataStorage(), |
| 72 | + DATA_NAME |
| 73 | + ); |
| 74 | + } |
| 75 | + |
| 76 | + @Override |
| 77 | + public boolean isDirty() { |
| 78 | + return true; |
| 79 | + } |
| 80 | + |
| 81 | + @Override |
| 82 | + public CompoundTag save(final CompoundTag data) { |
| 83 | + this.holders.forEach((shipId, holder) -> { |
| 84 | + final CompoundTag tag = new CompoundTag(); |
| 85 | + holder.save(tag); |
| 86 | + if (!tag.isEmpty()) { |
| 87 | + data.put(Long.toHexString(shipId), tag); |
| 88 | + } |
| 89 | + }); |
| 90 | + return data; |
| 91 | + } |
| 92 | + |
| 93 | + public final class Holder { |
| 94 | + private final long shipId; |
| 95 | + private final CompoundTag oldData; |
| 96 | + private final Map<Class<? extends IShipAdditionalData>, IShipAdditionalData> storages = new ConcurrentHashMap<>(); |
| 97 | + |
| 98 | + private Holder(final long shipId, final CompoundTag oldData) { |
| 99 | + this.shipId = shipId; |
| 100 | + this.oldData = oldData; |
| 101 | + } |
| 102 | + |
| 103 | + public <T extends IShipAdditionalData> T get(final Class<T> clazz) { |
| 104 | + final T storage = (T) this.storages.get(clazz); |
| 105 | + if (storage != null) { |
| 106 | + return storage; |
| 107 | + } |
| 108 | + if (!this.oldData.contains(clazz.getName())) { |
| 109 | + return null; |
| 110 | + } |
| 111 | + validateDataClass(clazz); |
| 112 | + return (T) this.storages.computeIfAbsent(clazz, (clazz0) -> { |
| 113 | + final T newStorage = construct((Class<T>) clazz0); |
| 114 | + newStorage.load(this.oldData.getCompound(clazz0.getName())); |
| 115 | + return newStorage; |
| 116 | + }); |
| 117 | + } |
| 118 | + |
| 119 | + public <T extends IShipAdditionalData> T getOrCreate(final Class<T> clazz) { |
| 120 | + final T storage = (T) this.storages.get(clazz); |
| 121 | + if (storage != null) { |
| 122 | + return storage; |
| 123 | + } |
| 124 | + validateDataClass(clazz); |
| 125 | + return (T) this.storages.computeIfAbsent(clazz, (clazz0) -> { |
| 126 | + final T newStorage = construct((Class<T>) clazz0); |
| 127 | + final String className = clazz0.getName(); |
| 128 | + if (this.oldData.contains(className)) { |
| 129 | + newStorage.load(this.oldData.getCompound(className)); |
| 130 | + } |
| 131 | + return newStorage; |
| 132 | + }); |
| 133 | + } |
| 134 | + |
| 135 | + public <T extends IShipAdditionalData> T getOrCreate(final Class<T> clazz, final Supplier<T> supplier) { |
| 136 | + final T storage = (T) this.storages.get(clazz); |
| 137 | + if (storage != null) { |
| 138 | + return storage; |
| 139 | + } |
| 140 | + validateDataClass(clazz); |
| 141 | + return (T) this.storages.computeIfAbsent(clazz, (clazz0) -> { |
| 142 | + final T newStorage; |
| 143 | + final String className = clazz0.getName(); |
| 144 | + if (this.oldData.contains(className)) { |
| 145 | + newStorage = construct((Class<T>) clazz0); |
| 146 | + newStorage.load(this.oldData.getCompound(className)); |
| 147 | + } else { |
| 148 | + newStorage = supplier.get(); |
| 149 | + } |
| 150 | + return newStorage; |
| 151 | + }); |
| 152 | + } |
| 153 | + |
| 154 | + public <T extends IShipAdditionalData> void put(final T storage) { |
| 155 | + validateDataClass(storage.getClass()); |
| 156 | + this.storages.put(storage.getClass(), storage); |
| 157 | + } |
| 158 | + |
| 159 | + public <T extends IShipAdditionalData> T remove(final Class<T> clazz) { |
| 160 | + return (T) this.storages.remove(clazz); |
| 161 | + } |
| 162 | + |
| 163 | + public void save(final CompoundTag data) { |
| 164 | + for (final String className : this.oldData.getAllKeys()) { |
| 165 | + data.put(className, this.oldData.get(className)); |
| 166 | + } |
| 167 | + this.storages.forEach((clazz, d) -> { |
| 168 | + final CompoundTag tag = new CompoundTag(); |
| 169 | + d.save(tag); |
| 170 | + data.put(clazz.getName(), tag); |
| 171 | + }); |
| 172 | + } |
| 173 | + } |
| 174 | + |
| 175 | + public static <T extends IShipAdditionalData> T construct(final Class<T> clazz) { |
| 176 | + try { |
| 177 | + return clazz.getConstructor().newInstance(); |
| 178 | + } catch (NoSuchMethodException | InstantiationException | IllegalAccessException | InvocationTargetException e) { |
| 179 | + throw new AssertionError(e); |
| 180 | + } |
| 181 | + } |
| 182 | + |
| 183 | + private static void validateDataClass(final Class<? extends IShipAdditionalData> clazz) throws IllegalArgumentException { |
| 184 | + if (clazz.getEnclosingClass() != null) { |
| 185 | + throw new IllegalArgumentException("Storage class " + clazz.getName() + " must be a top-level class"); |
| 186 | + } |
| 187 | + if (!Modifier.isFinal(clazz.getModifiers())) { |
| 188 | + throw new IllegalArgumentException("Storage class " + clazz.getName() + " must be a final class"); |
| 189 | + } |
| 190 | + try { |
| 191 | + clazz.getConstructor(); |
| 192 | + } catch (NoSuchMethodException e) { |
| 193 | + throw new IllegalArgumentException("Storage class " + clazz.getName() + " missing public no‑argument constructor!", e); |
| 194 | + } |
| 195 | + } |
| 196 | +} |
0 commit comments