3333
3434public class BlockListSetting extends Setting
3535{
36+ private final Object blockNamesLock = new Object ();
3637 private final ArrayList <String > blockNames = new ArrayList <>();
3738 private final String [] defaultNames ;
3839
@@ -50,38 +51,53 @@ public BlockListSetting(String name, String descriptionKey,
5051 this (name , WText .translated (descriptionKey ), blocks );
5152 }
5253
53- private void addFromString (String s )
54+ private boolean addFromString (String s )
5455 {
5556 if (s == null )
56- return ;
57+ return false ;
5758 String raw = s .trim ();
5859 if (raw .isEmpty ())
59- return ;
60+ return false ;
6061
6162 Identifier id = Identifier .tryParse (raw );
6263 String name = raw ;
6364
6465 if (id != null && BuiltInRegistries .BLOCK .containsKey (id ))
6566 name = id .toString ();
6667
67- if ( Collections . binarySearch ( blockNames , name ) < 0 )
68+ synchronized ( blockNamesLock )
6869 {
70+ if (Collections .binarySearch (blockNames , name ) >= 0 )
71+ return false ;
72+
6973 blockNames .add (name );
7074 Collections .sort (blockNames );
75+ return true ;
7176 }
7277 }
7378
7479 public List <String > getBlockNames ()
7580 {
76- return Collections .unmodifiableList (blockNames );
81+ return Collections .unmodifiableList (getBlockNamesSnapshot ());
82+ }
83+
84+ private ArrayList <String > getBlockNamesSnapshot ()
85+ {
86+ synchronized (blockNamesLock )
87+ {
88+ return new ArrayList <>(blockNames );
89+ }
7790 }
7891
7992 public int indexOf (String name )
8093 {
8194 if (name == null )
8295 return -1 ;
8396
84- return Collections .binarySearch (blockNames , name );
97+ synchronized (blockNamesLock )
98+ {
99+ return Collections .binarySearch (blockNames , name );
100+ }
85101 }
86102
87103 public int indexOf (Block block )
@@ -101,48 +117,69 @@ public boolean contains(Block block)
101117
102118 public int size ()
103119 {
104- return blockNames .size ();
120+ synchronized (blockNamesLock )
121+ {
122+ return blockNames .size ();
123+ }
105124 }
106125
107126 public void add (Block block )
108127 {
109128 String name = BlockUtils .getName (block );
110- if (Collections .binarySearch (blockNames , name ) >= 0 )
111- return ;
129+ boolean changed ;
130+ synchronized (blockNamesLock )
131+ {
132+ if (Collections .binarySearch (blockNames , name ) >= 0 )
133+ return ;
134+
135+ blockNames .add (name );
136+ Collections .sort (blockNames );
137+ changed = true ;
138+ }
112139
113- blockNames .add (name );
114- Collections .sort (blockNames );
115- WurstClient .INSTANCE .saveSettings ();
140+ if (changed )
141+ WurstClient .INSTANCE .saveSettings ();
116142 }
117143
118144 // New: allow adding raw keyword entries
119145 public void addRawName (String raw )
120146 {
121- int before = blockNames .size ();
122- addFromString (raw );
123- if (blockNames .size () != before )
147+ if (addFromString (raw ))
124148 WurstClient .INSTANCE .saveSettings ();
125149 }
126150
127151 public void remove (int index )
128152 {
129- if (index < 0 || index >= blockNames .size ())
130- return ;
153+ boolean changed ;
154+ synchronized (blockNamesLock )
155+ {
156+ if (index < 0 || index >= blockNames .size ())
157+ return ;
158+
159+ blockNames .remove (index );
160+ changed = true ;
161+ }
131162
132- blockNames . remove ( index );
133- WurstClient .INSTANCE .saveSettings ();
163+ if ( changed )
164+ WurstClient .INSTANCE .saveSettings ();
134165 }
135166
136167 public void resetToDefaults ()
137168 {
138- blockNames .clear ();
139- blockNames .addAll (Arrays .asList (defaultNames ));
169+ synchronized (blockNamesLock )
170+ {
171+ blockNames .clear ();
172+ blockNames .addAll (Arrays .asList (defaultNames ));
173+ }
140174 WurstClient .INSTANCE .saveSettings ();
141175 }
142176
143177 public void clear ()
144178 {
145- blockNames .clear ();
179+ synchronized (blockNamesLock )
180+ {
181+ blockNames .clear ();
182+ }
146183 WurstClient .INSTANCE .saveSettings ();
147184 }
148185
@@ -157,12 +194,17 @@ public void fromJson(JsonElement json)
157194 {
158195 try
159196 {
160- blockNames . clear ();
197+ ArrayList < String > parsedNames = new ArrayList <> ();
161198
162199 // if string "default", load default blocks
163200 if (JsonUtils .getAsString (json , "nope" ).equals ("default" ))
164201 {
165- blockNames .addAll (Arrays .asList (defaultNames ));
202+ parsedNames .addAll (Arrays .asList (defaultNames ));
203+ synchronized (blockNamesLock )
204+ {
205+ blockNames .clear ();
206+ blockNames .addAll (parsedNames );
207+ }
166208 return ;
167209 }
168210
@@ -178,16 +220,22 @@ public void fromJson(JsonElement json)
178220 }
179221
180222 String name = id .toString ();
181- if (blockNames .contains (name ))
223+ if (parsedNames .contains (name ))
182224 {
183225 System .out .println ("Discarding BlockList entry \" " + rawName
184226 + "\" as \" " + name + "\" is already in the list" );
185227 continue ;
186228 }
187229
188- blockNames .add (name );
230+ parsedNames .add (name );
231+ }
232+ parsedNames .sort (null );
233+
234+ synchronized (blockNamesLock )
235+ {
236+ blockNames .clear ();
237+ blockNames .addAll (parsedNames );
189238 }
190- blockNames .sort (null );
191239
192240 }catch (JsonException e )
193241 {
@@ -199,12 +247,14 @@ public void fromJson(JsonElement json)
199247 @ Override
200248 public JsonElement toJson ()
201249 {
250+ List <String > snapshot = getBlockNamesSnapshot ();
251+
202252 // if blockNames is the same as defaultNames, save string "default"
203- if (blockNames .equals (Arrays .asList (defaultNames )))
253+ if (snapshot .equals (Arrays .asList (defaultNames )))
204254 return new JsonPrimitive ("default" );
205255
206256 JsonArray json = new JsonArray ();
207- blockNames .forEach (s -> json .add (s ));
257+ snapshot .forEach (s -> json .add (s ));
208258 return json ;
209259 }
210260
@@ -257,12 +307,13 @@ public boolean matchesBlock(net.minecraft.world.level.block.Block block)
257307 String idFull = net .wurstclient .util .BlockUtils .getName (block );
258308 if (contains (idFull ))
259309 return true ;
310+ List <String > names = getBlockNamesSnapshot ();
260311 String localId = idFull .contains (":" )
261312 ? idFull .substring (idFull .indexOf (":" ) + 1 ) : idFull ;
262313 String localSpaced = localId .replace ('_' , ' ' );
263314 String transKey = block .getDescriptionId ();
264315 String display = block .getName ().getString ();
265- for (String s : blockNames )
316+ for (String s : names )
266317 {
267318 net .minecraft .resources .Identifier id =
268319 net .minecraft .resources .Identifier .tryParse (s );
0 commit comments