@@ -297,76 +297,143 @@ public void put(String name, Tag tag) {
297297 addTag (tag );
298298 }
299299
300- /// Adds a byte tag with the given name and value to this compound tag.
300+ @ SuppressWarnings ("unchecked" )
301+ private <T extends Tag > T getOrPutTag (String name , TagType <T > tagType ) throws IllegalStateException {
302+ Tag existingTag = get (name );
303+ if (existingTag != null ) {
304+ assert existingTag .getName ().equalsIgnoreCase (name );
305+
306+ if (existingTag .getType () == tagType ) {
307+ return (T ) existingTag ;
308+ } else {
309+ throw new IllegalStateException ("Cannot set a " + tagType + " with name " + name + " because there is already a " + existingTag .getType () + " with the same name" );
310+ }
311+ } else {
312+ T tag = tagType .createTag ();
313+ tag .setName (name );
314+ addTag (tag );
315+ return tag ;
316+ }
317+ }
318+
319+ /// Sets the byte value of the [ByteTag] with the given name.
320+ ///
321+ /// If no byte tag with the given name exists, a new byte tag will be created.
322+ ///
323+ /// @throws IllegalStateException if the tag with the given name exists but is not a byte tag.
301324 @ Contract (mutates = "this" )
302- public void putByte (String name , byte value ) {
303- addTag ( new ByteTag ( name , value ) );
325+ public void setByte (String name , byte value ) throws IllegalStateException {
326+ getOrPutTag ( name , TagType . BYTE ). set ( value );
304327 }
305328
306- /// Adds a byte tag with the given name and value to this compound tag.
329+ /// Sets the boolean value of the [ByteTag] with the given name.
330+ ///
331+ /// If no byte tag with the given name exists, a new byte tag will be created.
332+ ///
333+ /// @throws IllegalStateException if the tag with the given name exists but is not a byte tag.
307334 @ Contract (mutates = "this" )
308- public void putBoolean (String name , boolean value ) {
309- addTag ( new ByteTag ( name , value ) );
335+ public void setBoolean (String name , boolean value ) throws IllegalStateException {
336+ getOrPutTag ( name , TagType . BYTE ). setBoolean ( value );
310337 }
311338
312- /// Adds a short tag with the given name and value to this compound tag.
339+ /// Sets the short value of the [ShortTag] with the given name.
340+ ///
341+ /// If no short tag with the given name exists, a new short tag will be created.
342+ ///
343+ /// @throws IllegalStateException if the tag with the given name exists but is not a short tag.
313344 @ Contract (mutates = "this" )
314- public void putShort (String name , short value ) {
315- addTag ( new ShortTag ( name , value ) );
345+ public void setShort (String name , short value ) throws IllegalStateException {
346+ getOrPutTag ( name , TagType . SHORT ). set ( value );
316347 }
317348
318- /// Adds an int tag with the given name and value to this compound tag.
349+ /// Sets the int value of the [IntTag] with the given name.
350+ ///
351+ /// If no int tag with the given name exists, a new int tag will be created.
352+ ///
353+ /// @throws IllegalStateException if the tag with the given name exists but is not an int tag.
319354 @ Contract (mutates = "this" )
320- public void putInt (String name , int value ) {
321- addTag ( new IntTag ( name , value ) );
355+ public void setInt (String name , int value ) throws IllegalStateException {
356+ getOrPutTag ( name , TagType . INT ). set ( value );
322357 }
323358
324- /// Adds a long tag with the given name and value to this compound tag.
359+ /// Sets the long value of the [LongTag] with the given name.
360+ ///
361+ /// If no long tag with the given name exists, a new long tag will be created.
362+ ///
363+ /// @throws IllegalStateException if the tag with the given name exists but is not a long tag.
325364 @ Contract (mutates = "this" )
326- public void putLong (String name , long value ) {
327- addTag ( new LongTag ( name , value ) );
365+ public void setLong (String name , long value ) throws IllegalStateException {
366+ getOrPutTag ( name , TagType . LONG ). set ( value );
328367 }
329368
330- /// Adds a float tag with the given name and value to this compound tag.
369+ /// Sets the float value of the [FloatTag] with the given name.
370+ ///
371+ /// If no float tag with the given name exists, a new float tag will be created.
372+ ///
373+ /// @throws IllegalStateException if the tag with the given name exists but is not a float tag.
331374 @ Contract (mutates = "this" )
332- public void putFloat (String name , float value ) {
333- addTag ( new FloatTag ( name , value ) );
375+ public void setFloat (String name , float value ) throws IllegalStateException {
376+ getOrPutTag ( name , TagType . FLOAT ). set ( value );
334377 }
335378
336- /// Adds a double tag with the given name and value to this compound tag.
379+ /// Sets the double value of the [DoubleTag] with the given name.
380+ ///
381+ /// If no double tag with the given name exists, a new double tag will be created.
382+ ///
383+ /// @throws IllegalStateException if the tag with the given name exists but is not a double tag.
337384 @ Contract (mutates = "this" )
338- public void putDouble (String name , double value ) {
339- addTag ( new DoubleTag ( name , value ) );
385+ public void setDouble (String name , double value ) throws IllegalStateException {
386+ getOrPutTag ( name , TagType . DOUBLE ). set ( value );
340387 }
341388
342- /// Adds a string tag with the given name and value to this compound tag.
389+ /// Sets the string value of the [StringTag] with the given name.
390+ ///
391+ /// If no string tag with the given name exists, a new string tag will be created.
392+ ///
393+ /// @throws IllegalStateException if the tag with the given name exists but is not a string tag.
343394 @ Contract (mutates = "this" )
344- public void putString (String name , String value ) {
345- addTag ( new StringTag ( name , value ) );
395+ public void setString (String name , String value ) throws IllegalStateException {
396+ getOrPutTag ( name , TagType . STRING ). set ( value );
346397 }
347398
348- /// Adds a byte array tag with the given name and value to this compound tag.
399+ /// Sets the byte array value of the [ByteArrayTag] with the given name.
400+ ///
401+ /// If no byte array tag with the given name exists, a new byte array tag will be created.
402+ ///
403+ /// @throws IllegalStateException if the tag with the given name exists but is not a byte array tag.
349404 @ Contract (mutates = "this" )
350- public void putByteArray (String name , byte [] value ) {
351- addTag ( new ByteArrayTag ( name , value ) );
405+ public void setByteArray (String name , byte [] value ) throws IllegalStateException {
406+ getOrPutTag ( name , TagType . BYTE_ARRAY ). setAll ( value );
352407 }
353408
354- /// Adds an int array tag with the given name and value to this compound tag.
409+ /// Sets the int array value of the [IntArrayTag] with the given name.
410+ ///
411+ /// If no int array tag with the given name exists, a new int array tag will be created.
412+ ///
413+ /// @throws IllegalStateException if the tag with the given name exists but is not an int array tag.
355414 @ Contract (mutates = "this" )
356- public void putIntArray (String name , int [] value ) {
357- addTag ( new IntArrayTag ( name , value ) );
415+ public void setIntArray (String name , int [] value ) throws IllegalStateException {
416+ getOrPutTag ( name , TagType . INT_ARRAY ). setAll ( value );
358417 }
359418
360- /// Adds a long array tag with the given name and value to this compound tag.
419+ /// Sets the long array value of the [LongArrayTag] with the given name.
420+ ///
421+ /// If no long array tag with the given name exists, a new long array tag will be created.
422+ ///
423+ /// @throws IllegalStateException if the tag with the given name exists but is not a long array tag.
361424 @ Contract (mutates = "this" )
362- public void putLongArray (String name , long [] value ) {
363- addTag ( new LongArrayTag ( name , value ) );
425+ public void setLongArray (String name , long [] value ) throws IllegalStateException {
426+ getOrPutTag ( name , TagType . LONG_ARRAY ). setAll ( value );
364427 }
365428
366- /// Adds an int array tag with the given name and UUID value to this compound tag.
429+ /// Set the UUID value of the [IntArrayTag] with the given name.
430+ ///
431+ /// If no int array tag with the given name exists, a new int array tag will be created.
432+ ///
433+ /// @throws IllegalStateException if the tag with the given name exists but is not an int array tag.
367434 @ Contract (mutates = "this" )
368- public void putUUID (String name , UUID value ) {
369- addTag ( new IntArrayTag ( name , value ) );
435+ public void setUUID (String name , UUID value ) {
436+ getOrPutTag ( name , TagType . INT_ARRAY ). setUUID ( value );
370437 }
371438
372439 @ Override
0 commit comments