From d7cc65d2f238467e9fc4539e8dbeb9ef448f3598 Mon Sep 17 00:00:00 2001 From: Jordan Date: Mon, 3 Jul 2023 16:37:42 +0100 Subject: [PATCH] refactor: some changes that may help #2289 (#2307) --- .../com/sk89q/worldedit/LocalSession.java | 4 +- .../worldedit/session/SessionManager.java | 4 +- .../session/storage/JsonFileSessionStore.java | 70 +++++++++++-------- .../sk89q/worldedit/world/item/ItemType.java | 12 ++-- 4 files changed, 50 insertions(+), 40 deletions(-) diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/LocalSession.java b/worldedit-core/src/main/java/com/sk89q/worldedit/LocalSession.java index 1ef2af6d6..dfa29a2e3 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/LocalSession.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/LocalSession.java @@ -105,8 +105,8 @@ import static com.google.common.base.Preconditions.checkNotNull; */ public class LocalSession implements TextureHolder { - private static final transient int CUI_VERSION_UNINITIALIZED = -1; - public static transient int MAX_HISTORY_SIZE = 15; + public static int MAX_HISTORY_SIZE = 15; + private static final int CUI_VERSION_UNINITIALIZED = -1; // Non-session related fields private transient LocalConfiguration config; diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/session/SessionManager.java b/worldedit-core/src/main/java/com/sk89q/worldedit/session/SessionManager.java index d93a6b298..646b18bc0 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/session/SessionManager.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/session/SessionManager.java @@ -45,8 +45,8 @@ import com.sk89q.worldedit.world.item.ItemTypes; import org.apache.logging.log4j.Logger; import javax.annotation.Nullable; -import java.io.File; import java.io.IOException; +import java.nio.file.Path; import java.util.HashMap; import java.util.Iterator; import java.util.Map; @@ -354,7 +354,7 @@ public class SessionManager { @Subscribe public void onConfigurationLoad(ConfigurationLoadEvent event) { LocalConfiguration config = event.getConfiguration(); - File dir = new File(config.getWorkingDirectoryPath().toFile(), "sessions"); + Path dir = config.getWorkingDirectoryPath().resolve("sessions"); store = new JsonFileSessionStore(dir); } diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/session/storage/JsonFileSessionStore.java b/worldedit-core/src/main/java/com/sk89q/worldedit/session/storage/JsonFileSessionStore.java index b383bda21..eb4b1ee1a 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/session/storage/JsonFileSessionStore.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/session/storage/JsonFileSessionStore.java @@ -36,6 +36,10 @@ import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.NoSuchFileException; +import java.nio.file.Path; +import java.nio.file.StandardCopyOption; import java.util.UUID; import static com.google.common.base.Preconditions.checkNotNull; @@ -49,20 +53,31 @@ public class JsonFileSessionStore implements SessionStore { private static final Logger LOGGER = LogManagerCompat.getLogger(); private final Gson gson; - private final File dir; + private final Path dir; + + /** + * Create a new session store. + * + * @param dir the directory + * @deprecated Use {@link #JsonFileSessionStore(Path)} + */ + @Deprecated + public JsonFileSessionStore(File dir) { + this(dir.toPath()); + } /** * Create a new session store. * * @param dir the directory */ - public JsonFileSessionStore(File dir) { + public JsonFileSessionStore(Path dir) { checkNotNull(dir); - if (!dir.isDirectory()) { - if (!dir.mkdirs()) { - LOGGER.warn("Failed to create directory '" + dir.getPath() + "' for sessions"); - } + try { + Files.createDirectories(dir); + } catch (IOException e) { + LOGGER.warn("Failed to create directory '" + dir + "' for sessions", e); } this.dir = dir; @@ -77,21 +92,19 @@ public class JsonFileSessionStore implements SessionStore { * @param id the ID * @return the file */ - private File getPath(UUID id) { + private Path getPath(UUID id) { checkNotNull(id); - return new File(dir, id + ".json"); + return dir.resolve(id + ".json"); } @Override public LocalSession load(UUID id) throws IOException { - File file = getPath(id); - try (Closer closer = Closer.create()) { - FileReader fr = closer.register(new FileReader(file)); - BufferedReader br = closer.register(new BufferedReader(fr)); - LocalSession session = gson.fromJson(br, LocalSession.class); + Path file = getPath(id); + try (var reader = Files.newBufferedReader(file)) { + LocalSession session = gson.fromJson(reader, LocalSession.class); if (session == null) { LOGGER.warn("Loaded a null session from {}, creating new session", file); - if (!file.delete()) { + if (!Files.deleteIfExists(file)) { LOGGER.warn("Failed to delete corrupted session {}", file); } session = new LocalSession(); @@ -99,7 +112,7 @@ public class JsonFileSessionStore implements SessionStore { return session; } catch (JsonParseException e) { throw new IOException(e); - } catch (FileNotFoundException e) { + } catch (NoSuchFileException e) { return new LocalSession(); } } @@ -107,29 +120,26 @@ public class JsonFileSessionStore implements SessionStore { @Override public void save(UUID id, LocalSession session) throws IOException { checkNotNull(session); - File finalFile = getPath(id); - File tempFile = new File(finalFile.getParentFile(), finalFile.getName() + ".tmp"); + Path finalFile = getPath(id); + Path tempFile = finalFile.getParent().resolve(finalFile.getFileName() + ".tmp"); - try (Closer closer = Closer.create()) { - FileWriter fr = closer.register(new FileWriter(tempFile)); - BufferedWriter bw = closer.register(new BufferedWriter(fr)); - gson.toJson(session, bw); + try (var writer = Files.newBufferedWriter(tempFile)) { + gson.toJson(session, writer); } catch (JsonIOException e) { throw new IOException(e); } - if (finalFile.exists()) { - if (!finalFile.delete()) { - LOGGER.warn("Failed to delete " + finalFile.getPath() + " so the .tmp file can replace it"); - } - } - - if (tempFile.length() == 0) { + if (Files.size(tempFile) == 0) { throw new IllegalStateException("Gson wrote zero bytes"); } - if (!tempFile.renameTo(finalFile)) { - LOGGER.warn("Failed to rename temporary session file to " + finalFile.getPath()); + try { + Files.move( + tempFile, finalFile, + StandardCopyOption.ATOMIC_MOVE, StandardCopyOption.REPLACE_EXISTING + ); + } catch (IOException e) { + LOGGER.warn("Failed to rename temporary session file to " + finalFile.toAbsolutePath(), e); } } diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/world/item/ItemType.java b/worldedit-core/src/main/java/com/sk89q/worldedit/world/item/ItemType.java index ecba92f8b..5a1e7653f 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/world/item/ItemType.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/world/item/ItemType.java @@ -43,7 +43,7 @@ public class ItemType implements RegistryItem, Keyed { private final String id; @SuppressWarnings("deprecation") - private final LazyReference name = LazyReference.from(() -> { + private transient final LazyReference name = LazyReference.from(() -> { String name = GuavaUtil.firstNonNull( WorldEdit.getInstance().getPlatformManager().queryCapability(Capability.GAME_HOOKS) .getRegistries().getItemRegistry().getName(this), @@ -51,18 +51,18 @@ public class ItemType implements RegistryItem, Keyed { ); return name.isEmpty() ? getId() : name; }); - private final LazyReference richName = LazyReference.from(() -> + private transient final LazyReference richName = LazyReference.from(() -> WorldEdit.getInstance().getPlatformManager().queryCapability(Capability.GAME_HOOKS) .getRegistries().getItemRegistry().getRichName(this) ); - private final LazyReference itemMaterial = LazyReference.from(() -> + private transient final LazyReference itemMaterial = LazyReference.from(() -> WorldEdit.getInstance().getPlatformManager().queryCapability(Capability.GAME_HOOKS) .getRegistries().getItemRegistry().getMaterial(this) ); //FAWE start - private BlockType blockType; - private boolean initBlockType; - private BaseItem defaultState; + private transient BlockType blockType; + private transient boolean initBlockType; + private transient BaseItem defaultState; //FAWE end public ItemType(String id) {