From 26e1341ce9ba53f23ef837fda6f019ab8a16942d Mon Sep 17 00:00:00 2001 From: Benjamin Elsdon Date: Wed, 20 Apr 2022 17:14:07 +0800 Subject: [PATCH] Out of my madness and suffering I have achieved the impossible. I almost lost all hope and was about to end it all. However, it worked out in the end and now we have gacha reloading. --- .../grasscutter/game/gacha/GachaManager.java | 26 ++++++++++++++----- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/src/main/java/emu/grasscutter/game/gacha/GachaManager.java b/src/main/java/emu/grasscutter/game/gacha/GachaManager.java index d52dff8bf..eb1cba379 100644 --- a/src/main/java/emu/grasscutter/game/gacha/GachaManager.java +++ b/src/main/java/emu/grasscutter/game/gacha/GachaManager.java @@ -10,6 +10,7 @@ import java.util.concurrent.ThreadLocalRandom; import com.google.gson.reflect.TypeToken; +import com.sun.nio.file.SensitivityWatchEventModifier; import emu.grasscutter.Grasscutter; import emu.grasscutter.data.GenshinData; import emu.grasscutter.data.def.ItemData; @@ -31,14 +32,11 @@ import it.unimi.dsi.fastutil.ints.IntArrayList; import it.unimi.dsi.fastutil.ints.IntList; import org.greenrobot.eventbus.Subscribe; -import static java.nio.file.StandardWatchEventKinds.ENTRY_MODIFY; - public class GachaManager { private final GameServer server; private final Int2ObjectMap gachaBanners; private GetGachaInfoRsp cachedProto; WatchService watchService; - WatchKey watchKey; private int[] yellowAvatars = new int[] {1003, 1016, 1042, 1035, 1041}; private int[] yellowWeapons = new int[] {11501, 11502, 12501, 12502, 13502, 13505, 14501, 14502, 15501, 15502}; @@ -74,9 +72,16 @@ public class GachaManager { public synchronized void load() { try (FileReader fileReader = new FileReader(Grasscutter.getConfig().DATA_FOLDER + "Banners.json")) { + getGachaBanners().clear(); List banners = Grasscutter.getGsonFactory().fromJson(fileReader, TypeToken.getParameterized(Collection.class, GachaBanner.class).getType()); - for (GachaBanner banner : banners) { - getGachaBanners().put(banner.getGachaType(), banner); + if(banners.size() > 0) { + for (GachaBanner banner : banners) { + getGachaBanners().put(banner.getGachaType(), banner); + } + Grasscutter.getLogger().info("Banners successfully loaded."); + this.cachedProto = createProto(); + } else { + Grasscutter.getLogger().error("Unable to load banners. Banners size is 0."); } } catch (Exception e) { // TODO Auto-generated catch block @@ -281,8 +286,7 @@ public class GachaManager { try { this.watchService = FileSystems.getDefault().newWatchService(); Path path = new File(Grasscutter.getConfig().DATA_FOLDER).toPath(); - path.register(watchService, ENTRY_MODIFY); - watchKey = watchService.take(); + path.register(watchService, new WatchEvent.Kind[]{StandardWatchEventKinds.ENTRY_MODIFY}, SensitivityWatchEventModifier.HIGH); server.OnGameServerTick.register(this); } catch (Exception e) { @@ -298,6 +302,8 @@ public class GachaManager { public synchronized void watchBannerJson(GameServerTickEvent tickEvent) { if(Grasscutter.getConfig().getServerOptions().WatchGacha) { try { + WatchKey watchKey = watchService.take(); + for (WatchEvent event : watchKey.pollEvents()) { final Path changed = (Path) event.context(); if (changed.endsWith("Banners.json")) { @@ -305,6 +311,12 @@ public class GachaManager { this.load(); } } + + boolean valid = watchKey.reset(); + if (!valid) { + Grasscutter.getLogger().error("Unable to reset Gacha Manager Watch Key. Auto-reload of banners.json will no longer work."); + return; + } } catch (Exception e) { e.printStackTrace(); }