1111import org .bukkit .generator .ChunkGenerator ;
1212import org .bukkit .plugin .PluginBase ;
1313import org .bukkit .plugin .PluginDescriptionFile ;
14- import org .bukkit .plugin .PluginLoader ;
1514import org .jetbrains .annotations .NotNull ;
1615import org .jetbrains .annotations .Nullable ;
1716
1817import java .io .*;
19- import java .net . URL ;
20- import java .net . URLConnection ;
18+ import java .nio . file . Files ;
19+ import java .nio . file . Path ;
2120import java .util .List ;
2221import java .util .Objects ;
22+ import java .util .jar .JarEntry ;
23+ import java .util .jar .JarFile ;
2324import java .util .logging .Level ;
2425import java .util .logging .Logger ;
2526
@@ -42,7 +43,7 @@ public PaperShelledPlugin(@NotNull PaperShelledPluginLoader loader,
4243 this .paperShelledDescription = paperShelledDescription ;
4344 this .description = description ;
4445 this .file = file ;
45- dataFolder = file .getParentFile ();
46+ dataFolder = new File ( file .getParentFile (), description . getName () );
4647 configFile = new File (dataFolder , "config.yml" );
4748 logger = PaperShelledLogger .getLogger (description .getPrefix () == null ?
4849 description .getName () : description .getPrefix ());
@@ -71,7 +72,7 @@ public final File getDataFolder() {
7172 */
7273 @ NotNull
7374 @ Override
74- public final PluginLoader getPluginLoader () {
75+ public final PaperShelledPluginLoader getPluginLoader () {
7576 return loader ;
7677 }
7778
@@ -199,30 +200,17 @@ public void saveResource(@NotNull String resourcePath, boolean replace) {
199200 throw new IllegalArgumentException ("The embedded resource '" + resourcePath + "' cannot be found in " + file );
200201 }
201202
202- File outFile = new File (dataFolder , resourcePath );
203- int lastIndex = resourcePath .lastIndexOf ('/' );
204- File outDir = new File (dataFolder , resourcePath .substring (0 , Math .max (lastIndex , 0 )));
205-
206- if (!outDir .exists ()) {
207- // noinspection ResultOfMethodCallIgnored
208- outDir .mkdirs ();
209- }
203+ Path outFile = new File (dataFolder , resourcePath ).toPath ();
210204
211205 try {
212- if (!outFile .exists () || replace ) {
213- OutputStream out = new FileOutputStream (outFile );
214- byte [] buf = new byte [1024 ];
215- int len ;
216- while ((len = in .read (buf )) > 0 ) {
217- out .write (buf , 0 , len );
218- }
219- out .close ();
220- in .close ();
206+ if (outFile .getParent () != null ) Files .createDirectories (outFile .getParent ());
207+ if (!Files .exists (outFile ) || replace ) {
208+ Files .copy (in , outFile );
221209 } else {
222- logger .log (Level .WARNING , "Could not save " + outFile . getName () + " to " + outFile + " because " + outFile . getName () + " already exists." );
210+ logger .log (Level .WARNING , "Could not save " + outFile + " because it already exists." );
223211 }
224212 } catch (IOException ex ) {
225- logger .log (Level .SEVERE , "Could not save " + outFile . getName () + " to " + outFile , ex );
213+ logger .log (Level .SEVERE , "Could not save " + outFile , ex );
226214 }
227215 }
228216
@@ -235,15 +223,10 @@ public InputStream getResource(@NotNull String filename) {
235223 }
236224
237225 try {
238- URL url = getClass ().getResource (filename );
239-
240- if (url == null ) {
241- return null ;
242- }
243-
244- URLConnection connection = url .openConnection ();
245- connection .setUseCaches (false );
246- return connection .getInputStream ();
226+ JarFile jar = loader .getPluginJar (description .getName ());
227+ JarEntry entry = jar .getJarEntry (filename );
228+ if (entry == null ) return null ;
229+ return jar .getInputStream (entry );
247230 } catch (IOException ex ) {
248231 return null ;
249232 }
0 commit comments