|
1 | 1 | package hellfirepvp.observerlib.common.data; |
2 | 2 |
|
3 | 3 | import com.google.common.collect.Maps; |
4 | | -import com.google.common.io.Files; |
5 | 4 | import com.mojang.serialization.Codec; |
6 | 5 | import hellfirepvp.observerlib.ObserverLib; |
7 | 6 | import net.minecraft.nbt.CompoundTag; |
|
12 | 11 | import net.minecraft.SharedConstants; |
13 | 12 | import net.minecraft.util.Tuple; |
14 | 13 | import net.minecraft.world.level.Level; |
| 14 | +import org.apache.commons.io.FileUtils; |
15 | 15 |
|
16 | 16 | import javax.annotation.Nonnull; |
17 | 17 | import java.io.File; |
18 | 18 | import java.io.IOException; |
19 | 19 | import java.util.*; |
20 | | -import java.util.function.BiFunction; |
21 | 20 |
|
22 | 21 | /** |
23 | 22 | * This class is part of the ObserverLib Mod |
@@ -171,13 +170,13 @@ private static <T extends IWorldRelatedData<T>> T loadDataFromFile(WorldCacheDom |
171 | 170 | } |
172 | 171 |
|
173 | 172 | private static <F> IWorldRelatedData.FileLoader<F> createLoadingContext(DirectorySet dirSet, WorldCacheDomain.SaveKey<?> rootKey) { |
174 | | - String rootName = rootKey.saveFileResolver().resolveFile(new File("/")).getAbsolutePath(); |
| 173 | + String rootName = rootKey.saveFileResolver().resolveFile(new File("/")).getPath(); |
175 | 174 | return (fileResolver, codec) -> { |
176 | 175 | if (!dirSet.getActualDirectory().exists() && !dirSet.getBackupDirectory().exists()) { |
177 | 176 | return Optional.empty(); |
178 | 177 | } |
179 | 178 | // Resolve from root to get an idea of what's being loaded. |
180 | | - String attemptName = fileResolver.resolveFile(new File("/")).getAbsolutePath(); |
| 179 | + String attemptName = fileResolver.resolveFile(new File("/")).getPath(); |
181 | 180 | boolean isSaveRoot = attemptName.equals(rootName); |
182 | 181 |
|
183 | 182 | F data = null; |
@@ -211,23 +210,27 @@ private static <F> IWorldRelatedData.FileLoader<F> createLoadingContext(Director |
211 | 210 | (isSaveRoot ? rootKey.getIdentifier() : rootKey.getIdentifier() + attemptName)); |
212 | 211 | } |
213 | 212 | } |
| 213 | + // No existing data file found, nothing to load. |
| 214 | + if (dataFile == null || !dataFile.exists()) { |
| 215 | + return Optional.empty(); |
| 216 | + } |
| 217 | + |
214 | 218 | // Copy files to error directory and give up. |
215 | 219 | if (data == null) { |
216 | 220 | DirectorySet errorSet = dirSet.getErrorDirectories(); |
217 | 221 | try { |
218 | 222 | if (dirSet.getActualDirectory().exists()) { |
219 | | - Files.copy(dirSet.getActualDirectory(), errorSet.getActualDirectory()); |
| 223 | + FileUtils.copyDirectory(dirSet.getActualDirectory(), errorSet.getActualDirectory()); |
220 | 224 | } |
221 | 225 | if (dirSet.getBackupDirectory().exists()) { |
222 | | - Files.copy(dirSet.getBackupDirectory(), errorSet.getBackupDirectory()); |
| 226 | + FileUtils.copyDirectory(dirSet.getBackupDirectory(), errorSet.getBackupDirectory()); |
223 | 227 | } |
224 | 228 | } catch (Exception e) { |
225 | 229 | ObserverLib.log.warn("Copying erroneous level data {} to the error directory failed.", |
226 | 230 | (isSaveRoot ? rootKey.getIdentifier() : rootKey.getIdentifier() + attemptName)); |
227 | 231 | ObserverLib.log.error("Copying files failed.", e); |
228 | 232 | } |
229 | | - } |
230 | | - if (data == null) { |
| 233 | + |
231 | 234 | return Optional.empty(); |
232 | 235 | } |
233 | 236 | return Optional.of(new Tuple<>(data, dataFile)); |
|
0 commit comments