Skip to content

Commit 9e9f8c2

Browse files
committed
Add more docs for Tags, make TagTemplate builder a specific type on creation.
1 parent 119a9e9 commit 9e9f8c2

File tree

4 files changed

+174
-58
lines changed

4 files changed

+174
-58
lines changed

src/main/java/org/spongepowered/api/tag/Tag.java

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,35 +33,36 @@
3333
import java.util.Collection;
3434

3535
/**
36-
* Represents a Tag for a given type.
36+
* A {@link ResourceKey resource keyed} collection of {@link Taggable} values
37+
* (of type {@code T}).
3738
*
38-
* A {@link ResourceKey resource keyed} collection of {@link Taggable} values (of type {@code T}).
39-
*
40-
* <p>While any number of tags may exist and the values each tag contains is arbitary,
41-
* vanilla Minecraft generally uses pre-defined tags for one of the following reasons:</p>
39+
* <p>While any number of tags may exist and the values each tag contains is
40+
* arbitrary, vanilla Minecraft generally uses pre-defined tags for one of the
41+
* following reasons:</p>
4242
*
4343
* <ul>
44-
* <li>To define a common material that {@link BlockType blocks} are made of,
45-
* for example, {@link BlockTypeTags#ACACIA_LOGS}</li>
46-
* <li>To define a common behavior that the {@link Engine engine} should apply to a block,
47-
* such as specifying a block as a log that can burn via {@link BlockTypeTags#LOGS_THAT_BURN}</li>
44+
* <li>To define a common material that {@link BlockType blocks} are made
45+
* of, for example, {@link BlockTypeTags#ACACIA_LOGS}</li>
46+
* <li>To define a common behavior that the {@link Engine engine} should
47+
* apply to a block, such as specifying a block as a log that can burn via
48+
* {@link BlockTypeTags#LOGS_THAT_BURN}</li>
4849
* </ul>
49-
*
5050
*/
5151
public interface Tag<T> extends DefaultedRegistryValue, ResourceKeyed {
5252

5353
/**
54-
* Get every value in this Tag.
54+
* Gets all values that are associated with this tag.
5555
*
5656
* @return All tag values
5757
*/
5858
Collection<T> values();
5959

6060
/**
61-
* Whether this tag includes the given value.
61+
* Gets whether this tag contains the supplied value.
6262
*
6363
* @param value Value to check
6464
* @return Whether the value is contained in this tag.
6565
*/
6666
boolean contains(T value);
67+
6768
}

src/main/java/org/spongepowered/api/tag/TagTemplate.java

Lines changed: 136 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import org.spongepowered.api.Sponge;
3030
import org.spongepowered.api.datapack.DataPackSerializable;
3131
import org.spongepowered.api.event.lifecycle.RegisterDataPackValueEvent;
32+
import org.spongepowered.api.registry.DefaultedRegistryReference;
3233
import org.spongepowered.api.registry.RegistryKey;
3334

3435
import java.util.Collection;
@@ -40,60 +41,102 @@
4041
public interface TagTemplate extends DataPackSerializable {
4142

4243
/**
43-
* Creates a builder to create a new {@link Tag}
44-
* or modify an existing one.
45-
* @return Builder to build a new Tag.
44+
* Returns a {@link Builder} that creates {@link TagTemplate}s.
45+
*
46+
* @param reference The {@link TagType} of the builder
47+
* @return The builder.
4648
*/
4749
@SuppressWarnings("unchecked")
48-
static Builder<?> builder() {
49-
return Sponge.game().builderProvider().provide(Builder.class);
50+
static <T extends Taggable<T>> Builder<T> builder(final DefaultedRegistryReference<TagType<T>> reference) {
51+
return Sponge.game().factoryProvider().provide(Factory.class).builder(reference.get());
52+
}
53+
54+
/**
55+
* Returns a {@link Builder} that creates {@link TagTemplate}s.
56+
*
57+
* @param tagType The {@link TagType} of the builder
58+
* @return The builder.
59+
*/
60+
@SuppressWarnings("unchecked")
61+
static <T extends Taggable<T>> Builder<T> builder(final TagType<T> tagType) {
62+
return Sponge.game().factoryProvider().provide(Factory.class).builder(tagType);
5063
}
5164

5265
interface Builder<T extends Taggable<T>> extends org.spongepowered.api.util.ResourceKeyedBuilder<TagTemplate, Builder<T>> {
5366

5467
/**
55-
* Sets the tag type and generifies the builder.
68+
* Sets whether the values contained by a tag template will replace
69+
* existing values if a tag with the same {@link #key()} and
70+
* {@link TagType} already exists when datapacks are (re)loaded.
5671
*
57-
* @param tagType Type to build a tag for, e.g {@link TagTypes#BLOCK_TYPE}
58-
* @return This builder, for chaining
59-
*/
60-
<NT extends Taggable<NT>> Builder<NT> type(TagType<NT> tagType);
61-
62-
/**
63-
* <p>Whether to replace instead of append if the tag already
64-
* exists. This replaces any minecraft tag data and any data packs
65-
* that are lower priority than this one (the plugin data pack)</p>
72+
* <p>If this is set to {@code true}, any previous entries for this
73+
* given key-tag type combination will be discarded and the data
74+
* in the built template will <strong>replace</strong> the data. If
75+
* this is set to {@code false}, the entries in this template will
76+
* be <strong>merged</strong> with any existing data. The default
77+
* behavior is to merge data.</p>
6678
*
67-
* <p>By default, if the {@link ResourceKey#value()} is the same as
68-
* another data pack / plugin, the two will be combined, and their
69-
* tags appended to each other.</p>
79+
* <p>As no guarantees are made to the order in which templates are
80+
* applied when datapacks are (re)loaded, note that replacement will
81+
* only act upon templates that have previously been applied.</p>
7082
*
7183
* @param replace Whether to replace instead of merge.
7284
* @return This builder, for chaining.
7385
*/
7486
Builder<T> replace(boolean replace);
7587

88+
/**
89+
* Indicates that any existing values of a tag with the same
90+
* {@link #key()} and {@link TagType} during datapack (re)loading will
91+
* have their values discarded and replaced by this template.
92+
*
93+
* @see #replace(boolean)
94+
* @return This builder, for chaining
95+
*/
7696
default Builder<T> replace() {
77-
return replace(true);
97+
return this.replace(true);
7898
}
7999

80100
/**
81101
* Adds the {@link RegistryKey} for a value to the builder.
82102
*
103+
* <p>As the objects that this tag will contain may be available at this
104+
* stage, their {@link ResourceKey resource key ID} should be provided
105+
* instead. However, this gives rise to the possibility that a value
106+
* may not be available when tags are generated. The behavior during tag
107+
* generation if the a value for the supplied key is not present can be
108+
* controlled with the argument passed to the {@code required}
109+
* parameter:</p>
110+
*
111+
* <ul>
112+
* <li>if {@code true}, the entire tag will not be applied.</li>
113+
* <li>if {@code false}, the tag will be applied without the value.
114+
* </li>
115+
* </ul>
116+
*
83117
* @param value Value to add
84118
* @param required Whether this tag should fail to load if
85119
* the value is not found while loading.
86120
* @return This builder, for chaining
87121
*/
88122
Builder<T> addValue(RegistryKey<T> value, boolean required);
89123

90-
default Builder<T> addValue(RegistryKey<T> value) {
91-
return addValue(value, true);
124+
/**
125+
* Adds the {@link RegistryKey} for a value to the builder, marking it
126+
* as required.
127+
*
128+
* @see #addValue(RegistryKey, boolean)
129+
* @param value Value to add
130+
* @return This builder, for chaining
131+
*/
132+
default Builder<T> addValue(final RegistryKey<T> value) {
133+
return this.addValue(value, true);
92134
}
93135

94136
/**
95137
* Adds a collection of {@link RegistryKey}s for values to this builder.
96138
*
139+
* @see #addValue(RegistryKey, boolean)
97140
* @param values Values to add
98141
* @param required Whether the values are required. If required,
99142
* the tag will fail to load if they are not found
@@ -102,66 +145,117 @@ default Builder<T> addValue(RegistryKey<T> value) {
102145
*/
103146
Builder<T> addValues(Collection<RegistryKey<T>> values, boolean required);
104147

105-
default Builder<T> addValues(Collection<RegistryKey<T>> values) {
106-
return addValues(values, true);
148+
/**
149+
* Adds a collection of {@link RegistryKey}s for values to this builder,
150+
* marking all as required.
151+
*
152+
* @see #addValue(RegistryKey, boolean)
153+
* @param values Values to add
154+
* @return This builder, for chaining.
155+
*/
156+
default Builder<T> addValues(final Collection<RegistryKey<T>> values) {
157+
return this.addValues(values, true);
107158
}
108159

109160
/**
110-
* Adds a {@link RegistryKey} for a Tag to this builder.
111-
* This means anything contained in that tag, is always
112-
* also contained in this one.
161+
* Marks a tag with a given {@link RegistryKey} as a child of the tag
162+
* generated by this template.
163+
*
164+
* <p>A child {@link Tag} is considered a subset of its parent, that is,
165+
* the tag generated from this template will include all values from all
166+
* its children, as well as those directly provided via the addValue(s)
167+
* methods.</p>
168+
*
169+
* <p>As {@link Tag}s may not be available at this stage, their
170+
* {@link #key()} should be provided instead. If the key does not point
171+
* to a valid tag (or tag template) when datapacks are reloaded, the
172+
* argument supplied to {@code required} determines whether this tag
173+
* is ultimately generated, that is, if the supplied child key does
174+
* not represent a tag:</p>
175+
*
176+
* <ul>
177+
* <li>if {@code true}, no tag will be generated.</li>
178+
* <li>if {@code false}, a tag will be generated without the given
179+
* child.</li>
180+
* </ul>
113181
*
114182
* @param childTag {@link RegistryKey} for tag to be added.
115183
* @param required Whether loading this tag should fail if the child tag is not found.
116184
* @return This builder, for chaining
117185
*/
118186
Builder<T> addChild(RegistryKey<Tag<T>> childTag, boolean required);
119187

120-
default Builder<T> addChild(RegistryKey<Tag<T>> childTag) {
121-
return addChild(childTag, true);
188+
/**
189+
* Marks a tag with a given {@link RegistryKey} as a required child of
190+
* the tag generated by this template.
191+
*
192+
* @see #addChild(RegistryKey)
193+
* @param childTag {@link RegistryKey} for tag to be added.
194+
* @return This builder, for chaining
195+
*/
196+
default Builder<T> addChild(final RegistryKey<Tag<T>> childTag) {
197+
return this.addChild(childTag, true);
122198
}
123199

124200
/**
125201
* Adds a child tag to this builder, using a {@link TagTemplate}.
126202
*
203+
* @see #addChild(RegistryKey, boolean)
127204
* @param childTag The child to be added.
128205
* @return This builder, for chaining
129206
* @throws IllegalArgumentException If this TagTemplate is not the same {@link TagType}
130-
* as this builder, so is incompatible.
207+
* as this builder.
131208
*/
132209
Builder<T> addChild(TagTemplate childTag) throws IllegalArgumentException;
133210

134211
/**
135-
* Adds a collection of children, that are required.
136-
* If these are not found they will cause the tag to fail to load.
212+
* Adds multiple children to this template.
213+
*
214+
* @see #addChild(RegistryKey, boolean)
137215
* @param children Children to add.
138-
* @param required Whether to fail loading the tag
139-
* if these children are not found.
216+
* @param required Whether to fail loading the tag if these children are
217+
* not found.
140218
* @return This builder, for chaining
141219
*/
142220
Builder<T> addChildren(Collection<RegistryKey<Tag<T>>> children, boolean required);
143221

144-
default Builder<T> addChildren(Collection<RegistryKey<Tag<T>>> children) {
145-
return addChildren(children, true);
222+
/**
223+
* Adds multiple required children to this template.
224+
*
225+
* @see #addChild(RegistryKey, boolean)
226+
* @param children Children to add.
227+
* @return This builder, for chaining
228+
*/
229+
default Builder<T> addChildren(final Collection<RegistryKey<Tag<T>>> children) {
230+
return this.addChildren(children, true);
146231
}
147232

148233
/**
149-
* Adds a map of children to this builder, of
150-
* {@link RegistryKey} to whether they are required.
151-
* This tag will fail to load if the child tag is not
152-
* present and it is required.
234+
* Adds multiple required children to this template, where each
235+
* key-value pair in this map represents a child {@link ResourceKey}
236+
* and whether the child is required.
153237
*
154-
* @param childrenMap Map of children, RegistryKey-Required
238+
* @see #addChild(RegistryKey, boolean)
239+
* @param childrenMap Map of children, RegistryKey
155240
* @return This builder, for chaining.
156241
*/
157242
Builder<T> addChildren(Map<RegistryKey<Tag<T>>, Boolean> childrenMap);
158243

159244
/**
160-
* Creates a {@link TagTemplate} that should be registered during the {@link RegisterDataPackValueEvent}.
245+
* Creates a {@link TagTemplate} that should be registered during the
246+
* {@link RegisterDataPackValueEvent}.
161247
*
162248
* @return The built {@link TagTemplate}.
163249
*/
164250
@Override
165-
@NonNull TagTemplate build();
251+
@NonNull
252+
TagTemplate build();
166253
}
254+
255+
interface Factory {
256+
257+
<T extends Taggable<T>> Builder<T> builder(TagType<T> tagType);
258+
259+
}
260+
167261
}

src/main/java/org/spongepowered/api/tag/TagType.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,27 @@
2828
import org.spongepowered.api.registry.RegistryType;
2929
import org.spongepowered.api.util.annotation.CatalogedBy;
3030

31+
/**
32+
* Represents a type that can be associated with a {@link Tag}.
33+
*
34+
* @param <T> The type of {@link Taggable}.
35+
*/
3136
@CatalogedBy(TagTypes.class)
3237
public interface TagType<T extends Taggable<T>> extends DefaultedRegistryValue {
3338

39+
/**
40+
* The {@link RegistryType} that represents the known collection of objects
41+
* of type {@code T}.
42+
*
43+
* @return The {@link RegistryType}
44+
*/
3445
RegistryType<T> taggableRegistry();
3546

47+
/**
48+
* The {@link RegistryType} of tags that can be associated with objects.
49+
*
50+
* @return The {@link RegistryType}
51+
*/
3652
RegistryType<Tag<T>> tagRegistry();
53+
3754
}

src/main/java/org/spongepowered/api/tag/Taggable.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,18 @@
3434
public interface Taggable<T extends Taggable<T>> extends DefaultedRegistryValue {
3535

3636
/**
37-
* Gets the {@link TagType} for this Taggable
38-
* @return This Taggable's TagType
37+
* Gets the {@link TagType} that represents the types of {@link Tag tags}
38+
* that can be associated with this object.
39+
*
40+
* @return The {@link TagType}
3941
*/
4042
TagType<T> tagType();
4143

4244
/**
43-
* Gets all tags that this taggable is in.
44-
* @return All tags that this taggable is in.
45+
* Gets all {@link Tag tags} that have been associated with this object.
46+
*
47+
* @return The {@link Collection} of {@link Tag}s.
4548
*/
4649
Collection<Tag<T>> tags();
50+
4751
}

0 commit comments

Comments
 (0)