From 20e8dcb9bed36708f2d717efaa9c32477a9fcd8a Mon Sep 17 00:00:00 2001 From: syeyoung Date: Sat, 8 May 2021 23:55:44 +0900 Subject: concurrency. Fix crashing or flashing related to cosmetics --- .../dungeonsguide/cosmetics/CosmeticsManager.java | 33 ++++++++++++---------- .../features/impl/cosmetics/PrefixSelectorGUI.java | 12 +++++--- 2 files changed, 26 insertions(+), 19 deletions(-) (limited to 'src/main/java/kr/syeyoung/dungeonsguide') diff --git a/src/main/java/kr/syeyoung/dungeonsguide/cosmetics/CosmeticsManager.java b/src/main/java/kr/syeyoung/dungeonsguide/cosmetics/CosmeticsManager.java index a7e91069..a5401022 100644 --- a/src/main/java/kr/syeyoung/dungeonsguide/cosmetics/CosmeticsManager.java +++ b/src/main/java/kr/syeyoung/dungeonsguide/cosmetics/CosmeticsManager.java @@ -41,22 +41,25 @@ import org.json.JSONArray; import org.json.JSONObject; import java.util.*; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.CopyOnWriteArrayList; +import java.util.concurrent.CopyOnWriteArraySet; import java.util.regex.Matcher; import java.util.regex.Pattern; public class CosmeticsManager implements StompMessageHandler { @Getter - private Map cosmeticDataMap = new HashMap<>(); + private Map cosmeticDataMap = new ConcurrentHashMap<>(); @Getter - private Map activeCosmeticMap = new HashMap<>(); + private Map activeCosmeticMap = new ConcurrentHashMap<>(); @Getter - private Map> activeCosmeticByType = new HashMap<>(); + private Map> activeCosmeticByType = new ConcurrentHashMap<>(); @Getter - private Map> activeCosmeticByPlayer = new HashMap<>(); + private Map> activeCosmeticByPlayer = new ConcurrentHashMap<>(); @Getter - private Map> activeCosmeticByPlayerNameLowerCase = new HashMap<>(); + private Map> activeCosmeticByPlayerNameLowerCase = new ConcurrentHashMap<>(); @Getter - private Set perms = new HashSet<>(); + private Set perms = new CopyOnWriteArraySet<>(); public void requestActiveCosmetics() { DungeonsGuide.getDungeonsGuide().getStompConnection().send(new StompPayload() @@ -103,15 +106,15 @@ public class CosmeticsManager implements StompMessageHandler { if (jsonObject.isNull("cosmeticUID")) { ActiveCosmetic activeCosmetic1 = activeCosmeticMap.remove(activeCosmetic.getActivityUID()); - List activeCosmetics = activeCosmeticByPlayer.computeIfAbsent(activeCosmetic.getPlayerUID(), a-> new ArrayList<>()); + List activeCosmetics = activeCosmeticByPlayer.computeIfAbsent(activeCosmetic.getPlayerUID(), a-> new CopyOnWriteArrayList<>()); activeCosmetics.remove(activeCosmetic1); - activeCosmetics = activeCosmeticByPlayerNameLowerCase.computeIfAbsent(activeCosmetic1.getUsername().toLowerCase(), a-> new ArrayList<>()); + activeCosmetics = activeCosmeticByPlayerNameLowerCase.computeIfAbsent(activeCosmetic1.getUsername().toLowerCase(), a-> new CopyOnWriteArrayList<>()); activeCosmetics.remove(activeCosmetic1); CosmeticData cosmeticData = cosmeticDataMap.get(activeCosmetic.getCosmeticData()); if (cosmeticData != null) { - List cosmeticsByTypeList = activeCosmeticByType.computeIfAbsent(cosmeticData.getCosmeticType(), a-> new ArrayList<>()); + List cosmeticsByTypeList = activeCosmeticByType.computeIfAbsent(cosmeticData.getCosmeticType(), a-> new CopyOnWriteArrayList<>()); cosmeticsByTypeList.remove(activeCosmetic1); } } else { @@ -123,15 +126,15 @@ public class CosmeticsManager implements StompMessageHandler { CosmeticData cosmeticData = cosmeticDataMap.get(activeCosmetic.getCosmeticData()); if (cosmeticData != null) { - List cosmeticsByTypeList = activeCosmeticByType.computeIfAbsent(cosmeticData.getCosmeticType(), a-> new ArrayList<>()); + List cosmeticsByTypeList = activeCosmeticByType.computeIfAbsent(cosmeticData.getCosmeticType(), a-> new CopyOnWriteArrayList<>()); cosmeticsByTypeList.add(activeCosmetic); cosmeticsByTypeList.remove(previousThing); } - List activeCosmetics = activeCosmeticByPlayer.computeIfAbsent(activeCosmetic.getPlayerUID(), a-> new ArrayList<>()); + List activeCosmetics = activeCosmeticByPlayer.computeIfAbsent(activeCosmetic.getPlayerUID(), a-> new CopyOnWriteArrayList<>()); activeCosmetics.add(activeCosmetic); activeCosmetics.remove(previousThing); - activeCosmetics = activeCosmeticByPlayerNameLowerCase.computeIfAbsent(activeCosmetic.getUsername().toLowerCase(), a-> new ArrayList<>()); + activeCosmetics = activeCosmeticByPlayerNameLowerCase.computeIfAbsent(activeCosmetic.getUsername().toLowerCase(), a-> new CopyOnWriteArrayList<>()); activeCosmetics.add(activeCosmetic); activeCosmetics.remove(previousThing); } @@ -185,12 +188,12 @@ public class CosmeticsManager implements StompMessageHandler { for (ActiveCosmetic value : activeCosmeticMap.values()) { CosmeticData cosmeticData = cosmeticDataMap.get(value.getCosmeticData()); if (cosmeticData != null) { - List cosmeticsByTypeList = activeCosmeticByType.computeIfAbsent(cosmeticData.getCosmeticType(), a-> new ArrayList<>()); + List cosmeticsByTypeList = activeCosmeticByType.computeIfAbsent(cosmeticData.getCosmeticType(), a-> new CopyOnWriteArrayList<>()); cosmeticsByTypeList.add(value); } - List activeCosmetics = activeCosmeticByPlayer.computeIfAbsent(value.getPlayerUID(), a-> new ArrayList<>()); + List activeCosmetics = activeCosmeticByPlayer.computeIfAbsent(value.getPlayerUID(), a-> new CopyOnWriteArrayList<>()); activeCosmetics.add(value); - activeCosmetics = activeCosmeticByPlayerName.computeIfAbsent(value.getUsername().toLowerCase(), a-> new ArrayList<>()); + activeCosmetics = activeCosmeticByPlayerName.computeIfAbsent(value.getUsername().toLowerCase(), a-> new CopyOnWriteArrayList<>()); activeCosmetics.add(value); } diff --git a/src/main/java/kr/syeyoung/dungeonsguide/features/impl/cosmetics/PrefixSelectorGUI.java b/src/main/java/kr/syeyoung/dungeonsguide/features/impl/cosmetics/PrefixSelectorGUI.java index eca6228a..aa9f2f9b 100644 --- a/src/main/java/kr/syeyoung/dungeonsguide/features/impl/cosmetics/PrefixSelectorGUI.java +++ b/src/main/java/kr/syeyoung/dungeonsguide/features/impl/cosmetics/PrefixSelectorGUI.java @@ -30,10 +30,8 @@ import net.minecraft.client.gui.Gui; import net.minecraft.client.renderer.GlStateManager; import java.awt.*; -import java.util.ArrayList; +import java.util.*; import java.util.List; -import java.util.Set; -import java.util.UUID; import java.util.function.Function; import java.util.stream.Collectors; @@ -79,7 +77,13 @@ public class PrefixSelectorGUI extends MPanel { FontRenderer fr = Minecraft.getMinecraft().fontRendererObj; CosmeticsManager cosmeticsManager = DungeonsGuide.getDungeonsGuide().getCosmeticsManager(); - Set activeCosmeticList = cosmeticsManager.getActiveCosmeticByPlayer().computeIfAbsent(Minecraft.getMinecraft().thePlayer.getGameProfile().getId(), (a) -> new ArrayList<>()).stream().map(ActiveCosmetic::getCosmeticData).collect(Collectors.toSet()); + List activeCosmeticList2 = cosmeticsManager.getActiveCosmeticByPlayer().get(Minecraft.getMinecraft().thePlayer.getGameProfile().getId()); + Set activeCosmeticList = new HashSet<>(); + if (activeCosmeticList2 !=null) { + for (ActiveCosmetic activeCosmetic : activeCosmeticList2) { + activeCosmeticList.add(activeCosmetic.getCosmeticData()); + } + } -- cgit