1
1
package fi .dy .masa .malilib .util ;
2
2
3
- import javax .annotation .Nullable ;
4
3
import java .io .BufferedWriter ;
5
4
import java .io .File ;
6
- import java .io .FileInputStream ;
7
5
import java .io .IOException ;
8
6
import java .nio .charset .StandardCharsets ;
9
7
import java .nio .file .*;
10
8
import java .util .*;
11
9
import java .util .function .Consumer ;
12
10
import java .util .function .Predicate ;
13
- import com .google .common .collect .ImmutableSet ;
14
- import net .minecraft .nbt .NbtCompound ;
15
- import net .minecraft .nbt .NbtIo ;
16
- import net .minecraft .nbt .NbtSizeTracker ;
11
+ import javax .annotation .Nullable ;
12
+
17
13
import fi .dy .masa .malilib .MaLiLib ;
18
14
import fi .dy .masa .malilib .config .value .FileWriteType ;
19
15
import fi .dy .masa .malilib .util .game .wrap .GameWrap ;
@@ -27,29 +23,34 @@ public class FileUtils
27
23
public static final Predicate <Path > ALWAYS_FALSE_FILEFILTER = p -> false ;
28
24
public static final Predicate <Path > ANY_FILE_FILEFILTER = Files ::isRegularFile ;
29
25
public static final Predicate <Path > JSON_FILEFILTER = (f ) -> Files .isRegularFile (f ) && f .getFileName ().toString ().endsWith (".json" );
30
- private static final Set <Character > ILLEGAL_CHARACTERS = ImmutableSet .of ( '/' , '\n' , '\r' , '\t' , '\0' , '\f' , '`' , '?' , '*' , '\\' , '<' , '>' , '|' , '\"' , ':' );
31
26
27
+ /**
28
+ * Please stop using the File object
29
+ * @return ()
30
+ */
31
+ @ Deprecated (forRemoval = true )
32
32
public static File getConfigDirectory ()
33
33
{
34
- //return new File(MinecraftClient.getInstance().runDirectory, "config");
35
34
return new File (GameWrap .getClient ().runDirectory , "config" );
36
35
}
37
36
38
- public static Path getConfigDirectoryAsPath ()
37
+ /**
38
+ * Please stop using the File object
39
+ * @return ()
40
+ */
41
+ @ Deprecated (forRemoval = true )
42
+ public static File getMinecraftDirectory ()
39
43
{
40
- //return new File(MinecraftClient.getInstance().runDirectory, "config");
41
- return GameWrap .getClient ().runDirectory .toPath ().resolve ("config" );
44
+ return GameWrap .getClient ().runDirectory ;
42
45
}
43
46
44
- public static File getMinecraftDirectory ()
47
+ public static Path getConfigDirectoryAsPath ()
45
48
{
46
- //return MinecraftClient.getInstance().runDirectory;
47
- return GameWrap .getClient ().runDirectory ;
49
+ return GameWrap .getClient ().runDirectory .toPath ().resolve ("config" );
48
50
}
49
51
50
- public static Path getMinecraftDirectoryPath ()
52
+ public static Path getMinecraftDirectoryAsPath ()
51
53
{
52
- //return MinecraftClient.getInstance().runDirectory;
53
54
return GameWrap .getClient ().runDirectory .toPath ();
54
55
}
55
56
@@ -365,54 +366,51 @@ public static String getJoinedTrailingPathElements(File file, File rootPath, int
365
366
return path ;
366
367
}
367
368
368
- public static String getNameWithoutExtension ( String name )
369
+ public static String getJoinedTrailingPathElements ( Path file , Path rootPath , int maxStringLength , String separator )
369
370
{
370
- int i = name .lastIndexOf ("." );
371
- return i != -1 ? name .substring (0 , i ) : name ;
372
- }
371
+ StringBuilder path = new StringBuilder ();
373
372
374
- public static String generateSimpleSafeFileName (String name )
375
- {
376
- return name .toLowerCase (Locale .US ).replaceAll ("\\ W" , "_" );
377
- }
378
-
379
- public static String generateSafeFileName (String name )
380
- {
381
- StringBuilder sb = new StringBuilder (name .length ());
373
+ if (maxStringLength <= 0 )
374
+ {
375
+ return "..." ;
376
+ }
382
377
383
- for ( int i = 0 ; i < name . length (); ++ i )
378
+ while ( file != null )
384
379
{
385
- char c = name . charAt ( i );
380
+ String name = file . getFileName (). toString ( );
386
381
387
- if (ILLEGAL_CHARACTERS . contains ( c ) == false )
382
+ if (( path . length () == 0 ) == false )
388
383
{
389
- sb .append (c );
384
+ path .insert (0 , name + separator );
385
+ }
386
+ else
387
+ {
388
+ path = new StringBuilder (name );
390
389
}
391
- }
392
390
393
- // Some weird reserved windows keywords apparently... FFS >_>
394
- return sb .toString ().replaceAll ("COM" , "" ).replaceAll ("PRN" , "" );
395
- }
391
+ int len = path .length ();
396
392
397
- @ Nullable
398
- public static NbtCompound readNBTFile (File file )
399
- {
400
- if (file .exists () && file .isFile () && file .canRead ())
401
- {
402
- try
393
+ if (len > maxStringLength )
403
394
{
404
- FileInputStream is = new FileInputStream (file );
405
- NbtCompound nbt = NbtIo .readCompressed (is , NbtSizeTracker .ofUnlimitedBytes ());
406
- is .close ();
407
- return nbt ;
395
+ path = new StringBuilder ("... " + path .substring (len - maxStringLength , len ));
396
+ break ;
408
397
}
409
- catch (Exception e )
398
+
399
+ if (file .equals (rootPath ))
410
400
{
411
- MaLiLib . LOGGER . warn ( "Failed to read NBT data from file '{}'" , file . getAbsolutePath ()) ;
401
+ break ;
412
402
}
403
+
404
+ file = file .getParent ();
413
405
}
414
406
415
- return null ;
407
+ return path .toString ();
408
+ }
409
+
410
+ public static String getNameWithoutExtension (String name )
411
+ {
412
+ int i = name .lastIndexOf ("." );
413
+ return i != -1 ? name .substring (0 , i ) : name ;
416
414
}
417
415
418
416
public static boolean writeDataToFile (final Path file , Consumer <BufferedWriter > dataWriter , FileWriteType writeType )
0 commit comments