2929import org .spongepowered .api .Sponge ;
3030import org .spongepowered .api .datapack .DataPackSerializable ;
3131import org .spongepowered .api .event .lifecycle .RegisterDataPackValueEvent ;
32+ import org .spongepowered .api .registry .DefaultedRegistryReference ;
3233import org .spongepowered .api .registry .RegistryKey ;
3334
3435import java .util .Collection ;
4041public 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}
0 commit comments