Skip to content

Commit de25449

Browse files
committed
Make value tag setters return fluent interface
1 parent a1234e9 commit de25449

8 files changed

Lines changed: 56 additions & 38 deletions

File tree

‎src/main/java/org/glavo/nbt/tag/ByteTag.java‎

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -92,35 +92,39 @@ void setDirect(byte value) {
9292
}
9393

9494
/// Sets the value of the tag.
95-
@Contract(mutates = "this")
96-
public void set(byte value) {
95+
@Contract(value = "_ -> this", mutates = "this")
96+
public ByteTag set(byte value) {
9797
if (getParent() instanceof ByteArrayTag parent) {
9898
assert parent.getTagOrNull(getIndex()) == this;
9999

100100
parent.setDirect(getIndex(), value);
101101
}
102102

103103
setDirect(value);
104+
return this;
104105
}
105106

106107
/// Sets the value of the tag from an unsigned integer.
107-
@Contract(mutates = "this")
108-
public void setUnsigned(int value) {
108+
@Contract(value = "_ -> this", mutates = "this")
109+
public ByteTag setUnsigned(int value) {
109110
set((byte) value);
111+
return this;
110112
}
111113

112114
@Override
113-
@Contract(mutates = "this")
114-
public void setValue(Byte value) {
115+
@Contract(value = "_ -> this", mutates = "this")
116+
public ByteTag setValue(Byte value) {
115117
set(value);
118+
return this;
116119
}
117120

118121
/// Sets the boolean value of the tag.
119122
///
120123
/// If the `value` is `true`, the tag will be set to `1`; otherwise, it will be set to `0`.
121-
@Contract(mutates = "this")
122-
public void setBoolean(boolean value) {
124+
@Contract(value = "_ -> this", mutates = "this")
125+
public ByteTag setBoolean(boolean value) {
123126
set((byte) (value ? 1 : 0));
127+
return this;
124128
}
125129

126130
@Override

‎src/main/java/org/glavo/nbt/tag/DoubleTag.java‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,15 +70,17 @@ public String getAsString() {
7070
}
7171

7272
/// Sets the value of the tag.
73-
@Contract(mutates = "this")
74-
public void set(double value) {
73+
@Contract(value = "_ -> this", mutates = "this")
74+
public DoubleTag set(double value) {
7575
this.value = value;
76+
return this;
7677
}
7778

7879
@Override
79-
@Contract(mutates = "this")
80-
public void setValue(Double value) {
80+
@Contract(value = "_ -> this", mutates = "this")
81+
public DoubleTag setValue(Double value) {
8182
this.value = value;
83+
return this;
8284
}
8385

8486
@Override

‎src/main/java/org/glavo/nbt/tag/FloatTag.java‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,15 +70,17 @@ public String getAsString() {
7070
}
7171

7272
/// Sets the value of the tag.
73-
@Contract(mutates = "this")
74-
public void set(float value) {
73+
@Contract(value = "_ -> this", mutates = "this")
74+
public FloatTag set(float value) {
7575
this.value = value;
76+
return this;
7677
}
7778

7879
@Override
79-
@Contract(mutates = "this")
80-
public void setValue(Float value) {
80+
@Contract(value = "_ -> this", mutates = "this")
81+
public FloatTag setValue(Float value) {
8182
this.value = value;
83+
return this;
8284
}
8385

8486
@Override

‎src/main/java/org/glavo/nbt/tag/IntTag.java‎

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,27 +80,30 @@ void setDirect(int value) {
8080
}
8181

8282
/// Sets the value of the tag.
83-
@Contract(mutates = "this")
84-
public void set(int value) {
83+
@Contract(value = "_ -> this", mutates = "this")
84+
public IntTag set(int value) {
8585
if (getParent() instanceof IntArrayTag parent) {
8686
assert parent.getTagOrNull(getIndex()) == this;
8787

8888
parent.setDirect(getIndex(), value);
8989
}
9090

9191
setDirect(value);
92+
return this;
9293
}
9394

9495
/// Sets the value of the tag from an unsigned long.
95-
@Contract(mutates = "this")
96-
public void setUnsigned(long value) {
96+
@Contract(value = "_ -> this", mutates = "this")
97+
public IntTag setUnsigned(long value) {
9798
set((int) value);
99+
return this;
98100
}
99101

100102
@Override
101-
@Contract(mutates = "this")
102-
public void setValue(Integer value) {
103+
@Contract(value = "_ -> this", mutates = "this")
104+
public IntTag setValue(Integer value) {
103105
set(value);
106+
return this;
104107
}
105108

106109
@Override

‎src/main/java/org/glavo/nbt/tag/LongTag.java‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,21 +74,23 @@ void setDirect(long value) {
7474
}
7575

7676
/// Sets the value of the tag.
77-
@Contract(mutates = "this")
78-
public void set(long value) {
77+
@Contract(value = "_ -> this", mutates = "this")
78+
public LongTag set(long value) {
7979
if (getParent() instanceof LongArrayTag parent) {
8080
assert parent.getTagOrNull(getIndex()) == this;
8181

8282
parent.setDirect(getIndex(), value);
8383
}
8484

8585
setDirect(value);
86+
return this;
8687
}
8788

8889
@Override
89-
@Contract(mutates = "this")
90-
public void setValue(Long value) {
90+
@Contract(value = "_ -> this", mutates = "this")
91+
public LongTag setValue(Long value) {
9192
set(value);
93+
return this;
9294
}
9395

9496
@Override

‎src/main/java/org/glavo/nbt/tag/ShortTag.java‎

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -76,21 +76,24 @@ public String getAsString() {
7676
}
7777

7878
/// Sets the value of the tag.
79-
@Contract(mutates = "this")
80-
public void set(short value) {
79+
@Contract(value = "_ -> this", mutates = "this")
80+
public ShortTag set(short value) {
8181
this.value = value;
82+
return this;
8283
}
8384

8485
/// Sets the value of the tag from an unsigned integer.
85-
@Contract(mutates = "this")
86-
public void setUnsigned(int value) {
86+
@Contract(value = "_ -> this", mutates = "this")
87+
public ShortTag setUnsigned(int value) {
8788
this.value = (short) value;
89+
return this;
8890
}
8991

9092
@Override
91-
@Contract(mutates = "this")
92-
public void setValue(Short value) {
93+
@Contract(value = "_ -> this", mutates = "this")
94+
public ShortTag setValue(Short value) {
9395
this.value = value;
96+
return this;
9497
}
9598

9699
@Override

‎src/main/java/org/glavo/nbt/tag/StringTag.java‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,15 +71,17 @@ public String getAsString() {
7171
}
7272

7373
/// Sets the value of the tag.
74-
@Contract(mutates = "this")
75-
public void set(String value) {
74+
@Contract(value = "_ -> this", mutates = "this")
75+
public StringTag set(String value) {
7676
this.value = Objects.requireNonNull(value);
77+
return this;
7778
}
7879

7980
@Override
80-
@Contract(mutates = "this")
81-
public void setValue(String value) {
81+
@Contract(value = "_ -> this", mutates = "this")
82+
public StringTag setValue(String value) {
8283
this.value = Objects.requireNonNull(value);
84+
return this;
8385
}
8486

8587
@Override

‎src/main/java/org/glavo/nbt/tag/ValueTag.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@ public ValueTag<V> setName(String name) throws IllegalArgumentException {
6262
public abstract String getAsString();
6363

6464
/// Sets the value of the tag.
65-
@Contract(mutates = "this")
66-
public abstract void setValue(V value);
65+
@Contract(value = "_ -> this", mutates = "this")
66+
public abstract ValueTag<V> setValue(V value);
6767

6868
@Override
6969
@Contract(value = "-> new", pure = true)

0 commit comments

Comments
 (0)