@@ -71,10 +71,10 @@ public final class SkullManager implements Manager {
71
71
private boolean loading = false ;
72
72
73
73
@ Getter
74
- private final List < Skull > skulls = Collections . synchronizedList ( new ArrayList <>() );
74
+ private final ConcurrentHashMap < Integer , Skull > skulls = new ConcurrentHashMap <>();
75
75
76
76
@ Getter
77
- private final List <Integer > idList = Collections .synchronizedList (new ArrayList <>());
77
+ private final Set <Integer > idList = Collections .synchronizedSet (new HashSet <>());
78
78
79
79
@ Getter
80
80
private final Map <Location , PlacedSkull > placedSkulls = new ConcurrentHashMap <>();
@@ -90,58 +90,54 @@ public List<OfflinePlayer> getOnlineOfflinePlayers() {
90
90
}
91
91
92
92
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 );
96
94
}
97
95
98
96
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 ());
102
98
}
103
99
104
100
public List <Skull > getSkulls (String category ) {
105
- synchronized (this .skulls ) {
106
- if (BaseCategory .getById (category ) != null )
101
+ if (BaseCategory .getById (category ) != null )
107
102
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 ());
109
104
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 ());
112
106
}
113
107
114
108
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 ();
116
110
return enabledSkulls .get (random .nextInt (enabledSkulls .size ()));
117
111
}
118
112
119
113
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 ]);
126
118
}
119
+ }
127
120
128
- if (id != -1 )
129
- return Collections .singletonList (getSkull (id ));
121
+ if (id != -1 )
122
+ return Collections .singletonList (getSkull (id ));
130
123
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 ());
133
125
}
134
126
135
127
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 );
138
134
}
135
+
136
+ return results ;
139
137
}
140
138
141
139
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 ();
145
141
}
146
142
147
143
public ItemStack getSkullItem (final int id ) {
@@ -171,22 +167,25 @@ public void removePlacedSkull(@NonNull final PlacedSkull placedSkull) {
171
167
172
168
private void checkAndFixDatabase () {
173
169
Bukkit .getServer ().getScheduler ().runTaskAsynchronously (Skulls .getInstance (), () -> {
174
- final Set < Skull > heads = new HashSet <>();
170
+ final Map < Integer , Skull > heads = new HashMap <>();
175
171
176
172
Common .log ("&r&aRunning database check :)" );
177
173
178
174
final List <Skull > downloaded = performHeadDownload (true );
179
175
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 );
182
178
});
183
179
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 ();
186
183
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!" );
190
189
}
191
190
});
192
191
}
@@ -198,8 +197,8 @@ public void downloadHeads() {
198
197
199
198
final List <Skull > heads = new ArrayList <>(performHeadDownload (false ));
200
199
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());
203
202
Skulls .getDataManager ().insertSkulls (heads );
204
203
});
205
204
}
@@ -277,8 +276,8 @@ public void load() {
277
276
return ;
278
277
}
279
278
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());
282
281
283
282
if (this .skulls .isEmpty ()) {
284
283
Common .log ("&cCould not find any skulls, attempting to redownload them!" );
0 commit comments