Skip to content

Commit 3d905e6

Browse files
committed
bug fixes, 3.20
Took 34 minutes
1 parent dddda3f commit 3d905e6

File tree

3 files changed

+47
-43
lines changed

3 files changed

+47
-43
lines changed

src/main/java/ca/tweetzy/skulls/guis/SettingsGUI.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ protected void draw() {
4646
setButton(1, 2, QuickItem.of(CompMaterial.GOLD_NUGGET).name("&e&lForce Sync Prices").lore("&7Clicking this will force update all the prices", "&7for all skulls to the default category price", "&7set within the configuration file.").make(), click -> {
4747
AtomicInteger total = new AtomicInteger(0);
4848

49-
Skulls.getSkullManager().getSkulls().forEach(skull -> {
49+
Skulls.getSkullManager().getSkulls().values().forEach(skull -> {
5050
final BaseCategory category = BaseCategory.getById(skull.getCategory());
5151
if (skull.getPrice() != category.getDefaultPrice()) {
5252
skull.setPrice(category.getDefaultPrice());

src/main/java/ca/tweetzy/skulls/guis/SkullsViewGUI.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public SkullsViewGUI(final Gui parent, final SkullUser skullPlayer, final String
5656
Bukkit.getPlayer(skullPlayer.getUUID()),
5757
viewMode == ViewMode.SEARCH ? TranslationManager.string(Translations.GUI_SKULLS_LIST_TITLE_SEARCH, "search_phrase", category) : viewMode == ViewMode.FAVOURITE ? TranslationManager.string(Translations.GUI_SKULLS_LIST_TITLE_FAVOURITES) : TranslationManager.string(Translations.GUI_SKULLS_LIST_TITLE_CATEGORY, "category_name", category),
5858
6,
59-
viewMode == ViewMode.SEARCH ? new ArrayList<>() : viewMode == ViewMode.FAVOURITE ? Skulls.getSkullManager().getSkulls(skullPlayer.getFavourites()) : Skulls.getSkullManager().getSkulls(category)
59+
new ArrayList<>()
6060
);
6161

6262
this.category = category;
@@ -70,6 +70,11 @@ public SkullsViewGUI(final Gui parent, final SkullUser skullPlayer, final String
7070
protected void prePopulate() {
7171
if (this.viewMode == ViewMode.SEARCH)
7272
this.items = Skulls.getSkullManager().getSkullsBySearch(this.player, category);
73+
74+
else if (viewMode == ViewMode.FAVOURITE)
75+
this.items = Skulls.getSkullManager().getSkulls(skullPlayer.getFavourites());
76+
else
77+
this.items= Skulls.getSkullManager().getSkulls(category);
7378
}
7479

7580
@Override

src/main/java/ca/tweetzy/skulls/manager/SkullManager.java

Lines changed: 40 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,10 @@ public final class SkullManager implements Manager {
7171
private boolean loading = false;
7272

7373
@Getter
74-
private final List<Skull> skulls = Collections.synchronizedList(new ArrayList<>());
74+
private final ConcurrentHashMap<Integer, Skull> skulls = new ConcurrentHashMap<>();
7575

7676
@Getter
77-
private final List<Integer> idList = Collections.synchronizedList(new ArrayList<>());
77+
private final Set<Integer> idList = Collections.synchronizedSet(new HashSet<>());
7878

7979
@Getter
8080
private final Map<Location, PlacedSkull> placedSkulls = new ConcurrentHashMap<>();
@@ -90,58 +90,54 @@ public List<OfflinePlayer> getOnlineOfflinePlayers() {
9090
}
9191

9292
public Skull getSkull(final int id) {
93-
synchronized (this.skulls) {
94-
return this.skulls.stream().filter(skull -> skull.getId() == id).findFirst().orElse(null);
95-
}
93+
return this.skulls.getOrDefault(id, null);
9694
}
9795

9896
public List<Skull> getSkulls(BaseCategory category) {
99-
synchronized (this.skulls) {
100-
return this.skulls.stream().filter(skull -> skull.getCategory().equalsIgnoreCase(category.getId())).collect(Collectors.toList());
101-
}
97+
return this.skulls.values().stream().filter(skull -> skull.getCategory().equalsIgnoreCase(category.getId())).collect(Collectors.toList());
10298
}
10399

104100
public List<Skull> getSkulls(String category) {
105-
synchronized (this.skulls) {
106-
if (BaseCategory.getById(category) != null)
101+
if (BaseCategory.getById(category) != null)
107102

108-
return this.skulls.stream().filter(skull -> skull.getCategory().equalsIgnoreCase(category)).collect(Collectors.toList());
103+
return this.skulls.values().stream().filter(skull -> skull.getCategory().equalsIgnoreCase(category)).collect(Collectors.toList());
109104

110-
return this.skulls.stream().filter(skull -> Skulls.getCategoryManager().findCategory(category).getSkulls().contains(skull.getId())).collect(Collectors.toList());
111-
}
105+
return this.skulls.values().stream().filter(skull -> Skulls.getCategoryManager().findCategory(category).getSkulls().contains(skull.getId())).collect(Collectors.toList());
112106
}
113107

114108
public Skull getRandomSkull() {
115-
final List<Skull> enabledSkulls = getSkulls().stream().filter(skull -> BaseCategory.getById(skull.getCategory()).isEnabled()).collect(Collectors.toList());
109+
final List<Skull> enabledSkulls = getSkulls().values().stream().filter(skull -> BaseCategory.getById(skull.getCategory()).isEnabled()).toList();
116110
return enabledSkulls.get(random.nextInt(enabledSkulls.size()));
117111
}
118112

119113
public List<Skull> getSkullsBySearch(Player player, String phrase) {
120-
synchronized (this.skulls) {
121-
int id = -1;
122-
if (phrase.startsWith("id:")) {
123-
if (NumberUtils.isNumber(phrase.split(":")[1])) {
124-
id = Integer.parseInt(phrase.split(":")[1]);
125-
}
114+
int id = -1;
115+
if (phrase.startsWith("id:")) {
116+
if (NumberUtils.isNumber(phrase.split(":")[1])) {
117+
id = Integer.parseInt(phrase.split(":")[1]);
126118
}
119+
}
127120

128-
if (id != -1)
129-
return Collections.singletonList(getSkull(id));
121+
if (id != -1)
122+
return Collections.singletonList(getSkull(id));
130123

131-
return this.skulls.stream().filter(skull -> player.hasPermission("skulls.category." + BaseCategory.getById(skull.getCategory()).getId().toLowerCase().replace(" ", "").replace("&", "")) && BaseCategory.getById(skull.getCategory()).isEnabled() && (Common.match(phrase, skull.getName()) || Common.match(phrase, skull.getCategory()) || skull.getTags().stream().anyMatch(tag -> Common.match(phrase, tag)))).collect(Collectors.toList());
132-
}
124+
return this.skulls.values().stream().filter(skull -> player.hasPermission("skulls.category." + BaseCategory.getById(skull.getCategory()).getId().toLowerCase().replace(" ", "").replace("&", "")) && BaseCategory.getById(skull.getCategory()).isEnabled() && (Common.match(phrase, skull.getName()) || Common.match(phrase, skull.getCategory()) || skull.getTags().stream().anyMatch(tag -> Common.match(phrase, tag)))).collect(Collectors.toList());
133125
}
134126

135127
public List<Skull> getSkulls(List<Integer> ids) {
136-
synchronized (this.skulls) {
137-
return this.skulls.stream().filter(skull -> ids.contains(skull.getId())).collect(Collectors.toList());
128+
final List<Skull> results = new ArrayList<>();
129+
130+
for (Integer id : ids) {
131+
final Skull found = this.skulls.getOrDefault(id, null);
132+
if (found != null)
133+
results.add(found);
138134
}
135+
136+
return results;
139137
}
140138

141139
public long getSkullCount(String category) {
142-
synchronized (this.skulls) {
143-
return this.skulls.stream().filter(skull -> skull.getCategory().equalsIgnoreCase(category)).count();
144-
}
140+
return this.skulls.values().stream().filter(skull -> skull.getCategory().equalsIgnoreCase(category)).count();
145141
}
146142

147143
public ItemStack getSkullItem(final int id) {
@@ -171,22 +167,25 @@ public void removePlacedSkull(@NonNull final PlacedSkull placedSkull) {
171167

172168
private void checkAndFixDatabase() {
173169
Bukkit.getServer().getScheduler().runTaskAsynchronously(Skulls.getInstance(), () -> {
174-
final Set<Skull> heads = new HashSet<>();
170+
final Map<Integer, Skull> heads = new HashMap<>();
175171

176172
Common.log("&r&aRunning database check :)");
177173

178174
final List<Skull> downloaded = performHeadDownload(true);
179175
downloaded.forEach(skull -> {
180-
if (this.skulls.contains(skull)) return;
181-
heads.add(skull);
176+
if (!this.skulls.containsKey(skull.getId()))
177+
heads.put(skull.getId(),skull);
182178
});
183179

184-
if (this.skulls.size() < heads.size()) {
185-
Common.log("&r&eFound some missing heads, downloading/inserting them now!");
180+
int oldSize = this.skulls.size();
181+
this.skulls.putAll(heads);
182+
int newSize = this.skulls.size();
186183

187-
this.skulls.addAll(heads);
188-
this.idList.addAll(heads.stream().map(Skull::getId).toList());
189-
Skulls.getDataManager().insertSkulls(new ArrayList<>(heads));
184+
if (newSize > oldSize) {
185+
Skulls.getDataManager().insertSkulls(new ArrayList<>(heads.values()));
186+
Common.log("&r&eFound some new skulls, saving them now :D");
187+
} else {
188+
Common.log("&r&aEverything looks up-to-date, no issues found. Enjoy!");
190189
}
191190
});
192191
}
@@ -198,8 +197,8 @@ public void downloadHeads() {
198197

199198
final List<Skull> heads = new ArrayList<>(performHeadDownload(false));
200199

201-
this.skulls.addAll(heads);
202-
this.idList.addAll(heads.stream().map(Skull::getId).toList());
200+
heads.forEach(skull -> this.skulls.putIfAbsent(skull.getId(), skull));
201+
// this.idList.addAll(heads.stream().map(Skull::getId).toList());
203202
Skulls.getDataManager().insertSkulls(heads);
204203
});
205204
}
@@ -277,8 +276,8 @@ public void load() {
277276
return;
278277
}
279278

280-
this.skulls.addAll(all);
281-
this.idList.addAll(all.stream().map(Skull::getId).toList());
279+
all.forEach(skull -> this.skulls.put(skull.getId(), skull));
280+
// this.idList.addAll(all.stream().map(Skull::getId).toList());
282281

283282
if (this.skulls.isEmpty()) {
284283
Common.log("&cCould not find any skulls, attempting to redownload them!");

0 commit comments

Comments
 (0)