Skip to content

Commit 6134afe

Browse files
authored
Make the Comp annotation easier to use (#236)
* place key directly in enum * convert comp * convert old format into new comp format * convert requirement into unique on comp * join via serial comma * rename method to parseComparisonRequirements * convert unique to lang keys, improve quality * remove default values from comp * ensure text isnt empty before adding * allow removing types and provide examples * remove types in valid situations * remove use of Integer.MAX_VALUE in comp * adjust magmatic smeltery text * move used types to enum * increase javadocs for Comp * remove unneeded types arrays * change default value to not be 0 * get first used type instead of just "first" * update new compat with new comp elements * convert deprecated elements of recent pyrotech * update examples file for prodigytech
1 parent a536e88 commit 6134afe

File tree

311 files changed

+1225
-1021
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

311 files changed

+1225
-1021
lines changed

examples/postInit/prodigytech.groovy

+2-2
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,10 @@ mods.prodigytech.explosion_furnace.recipeBuilder()
4949
// Explosion Furnace Additives:
5050
// Turn an item into an explosive or into a dampener when inserted into the Explosion Furnace.
5151

52-
// mods.prodigytech.explosion_furnace_additives.removeAllDampeners()
53-
// mods.prodigytech.explosion_furnace_additives.removeAllExplosives()
5452
mods.prodigytech.explosion_furnace_additives.removeDampener(ore('dustAsh'))
5553
mods.prodigytech.explosion_furnace_additives.removeExplosive(ore('gunpowder'))
54+
// mods.prodigytech.explosion_furnace_additives.removeAllDampeners()
55+
// mods.prodigytech.explosion_furnace_additives.removeAllExplosives()
5656

5757
mods.prodigytech.explosion_furnace_additives.addDampener(item('minecraft:stone'), 50)
5858
mods.prodigytech.explosion_furnace_additives.addExplosive(item('minecraft:cobblestone'), 50)

src/main/java/com/cleanroommc/groovyscript/api/documentation/annotations/Comp.java

+114-13
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,102 @@
33
import java.lang.annotation.Retention;
44
import java.lang.annotation.RetentionPolicy;
55
import java.lang.annotation.Target;
6+
import java.util.EnumSet;
67

78
/**
8-
* Used by {@link Property Properties} to determine what are valid values for the {@link Property}.
9-
* Frequently used in an array containing two entries, indicating minimum and maximum bounds.
9+
* Used for comparisons by {@link Property Properties} to determine what are valid values for the {@link Property}.
10+
* <p>
11+
* Fully written out, it uses the array {@link #types()} to store the types being tracked,
12+
* and the corresponding element to store what the value being compared against is.
13+
* <p>
14+
* However, to improve the ease of use, some information can be assumed by default and are thus optional.
15+
* <p>
16+
* If {@link #types()} is empty, elements that do not match the default value will be checked.
17+
* This means that in most cases {@link #types()} does not need to be declared.
18+
* <br>
19+
* The only exception is if any of {@link #gt()}, {@link #gte()}, {@link #eq()}, {@link #lte()}, {@link #lt()},
20+
* {@link #not()}, or {@link #unique()} elements match the default value,
21+
* which is {@link Integer#MIN_VALUE} for the {@link #gt()}, {@link #gte()}, {@link #eq()}, {@link #lte()}, and {@link #lt()} elements and
22+
* an empty string ({@code ""}) for both {@link #not()} and {@link #unique()} elements.
23+
* In this situation, {@link #types()} will need to contain all relevant {@link Type Types}.
1024
*
11-
* @see Property#valid()
25+
* @see Property#comp()
1226
*/
1327
@Retention(RetentionPolicy.RUNTIME)
1428
@Target({ /* No targets allowed */})
1529
public @interface Comp {
1630

1731
/**
1832
* @return the method of comparison
33+
* @deprecated use {@link #types()} instead
1934
*/
35+
@Deprecated
2036
Type type() default Type.EQ;
2137

2238
/**
2339
* @return the value that {@link #type()} compares with
40+
* @deprecated use {@link #lt()}, {@link #lte()}, {@link #gte()}, {@link #gt()}, {@link #eq()}, {@link #not()}, or {@link #unique()} instead
2441
*/
25-
String value();
42+
@Deprecated
43+
String value() default "";
44+
45+
/**
46+
* @return an array of types to compare with. In most cases, this can be assumed.
47+
*/
48+
Type[] types() default {};
49+
50+
/**
51+
* The value represent less than.
52+
* Enabled either via being set to a non-default value, or by adding {@link Comp.Type#LT} to the {@link #types()} element.
53+
*
54+
* @return if enabled, the value used to represent less than
55+
*/
56+
int lt() default Integer.MIN_VALUE;
57+
58+
/**
59+
* The value represent less than or equal to.
60+
* Enabled either via being set to a non-default value, or by adding {@link Comp.Type#LTE} to the {@link #types()} element.
61+
*
62+
* @return if enabled, the value used to represent less than or equal to
63+
*/
64+
int lte() default Integer.MIN_VALUE;
65+
66+
/**
67+
* The value represent greater than or equal to.
68+
* Enabled either via being set to a non-default value, or by adding {@link Comp.Type#GTE} to the {@link #types()} element.
69+
*
70+
* @return if enabled, the value used to represent greater than or equal to
71+
*/
72+
int gte() default Integer.MIN_VALUE;
73+
74+
/**
75+
* The value represent greater than.
76+
* Enabled either via being set to a non-default value, or by adding {@link Comp.Type#GT} to the {@link #types()} element.
77+
*
78+
* @return if enabled, the value used to represent greater than
79+
*/
80+
int gt() default Integer.MIN_VALUE;
81+
82+
/**
83+
* The value represent equal to.
84+
* Enabled either via being set to a non-default value, or by adding {@link Comp.Type#EQ} to the {@link #types()} element.
85+
*
86+
* @return if enabled, the value used to represent equal to
87+
*/
88+
int eq() default Integer.MIN_VALUE;
89+
90+
/**
91+
* The value represent not equal to.
92+
* Enabled either via being set to a non-default value, or by adding {@link Comp.Type#NOT} to the {@link #types()} element.
93+
*
94+
* @return if enabled, the value used to represent not equal to
95+
*/
96+
String not() default "";
97+
98+
/**
99+
* @return if {@link #types()} contains {@link Comp.Type#UNI}, the lang key used to represent the unique description
100+
*/
101+
String unique() default "";
26102

27103
/**
28104
* Used to determine the type of comparison. Contains a symbol representation and a localization key.
@@ -63,24 +139,49 @@
63139
* <th>!=</th>
64140
* <th>groovyscript.wiki.not</th>
65141
* </tr>
142+
* <tr>
143+
* <th>UNI</th>
144+
* <th>!?</th>
145+
* <th>groovyscript.wiki.unique</th>
146+
* </tr>
66147
* </table>
67148
*/
68149
enum Type {
69-
GT(">", "greater_than"),
70-
GTE(">=", "greater_than_or_equal_to"),
71-
EQ("==", "equal_to"),
72-
LTE("<=", "less_than_or_equal_to"),
73-
LT("<", "less_than"),
74-
NOT("!=", "not");
75-
76-
private static final String baseLocalizationPath = "groovyscript.wiki.";
150+
GT(">", "groovyscript.wiki.greater_than"),
151+
GTE(">=", "groovyscript.wiki.greater_than_or_equal_to"),
152+
EQ("==", "groovyscript.wiki.equal_to"),
153+
LTE("<=", "groovyscript.wiki.less_than_or_equal_to"),
154+
LT("<", "groovyscript.wiki.less_than"),
155+
NOT("!=", "groovyscript.wiki.not"),
156+
UNI("!?", "groovyscript.wiki.unique");
77157

78158
private final String symbol;
79159
private final String key;
80160

81161
Type(String symbol, String key) {
82162
this.symbol = symbol;
83-
this.key = baseLocalizationPath + key;
163+
this.key = key;
164+
}
165+
166+
/**
167+
* Creates an EnumSet based on the given {@link Comp}.
168+
* If {@link Comp#types()} has any elements, the types contained will be used.
169+
* Otherwise, any non-default values for the individual elements will be used.
170+
*
171+
* @param comp the {@link Comp} instance to be parsed
172+
* @return a set containing the types that are used in the Comp
173+
*/
174+
public static EnumSet<Type> getUsedTypes(Comp comp) {
175+
if (comp.types().length > 0) return EnumSet.of(comp.types()[0], comp.types());
176+
var usedTypes = EnumSet.noneOf(Comp.Type.class);
177+
if (comp.gt() != Integer.MIN_VALUE) usedTypes.add(Comp.Type.GT);
178+
if (comp.gte() != Integer.MIN_VALUE) usedTypes.add(Comp.Type.GTE);
179+
if (comp.eq() != Integer.MIN_VALUE) usedTypes.add(Comp.Type.EQ);
180+
if (comp.lte() != Integer.MIN_VALUE) usedTypes.add(Comp.Type.LTE);
181+
if (comp.lt() != Integer.MIN_VALUE) usedTypes.add(Comp.Type.LT);
182+
if (!comp.not().isEmpty()) usedTypes.add(Comp.Type.NOT);
183+
if (!comp.unique().isEmpty()) usedTypes.add(Comp.Type.UNI);
184+
return usedTypes;
84185
}
85186

86187
public String getSymbol() {

src/main/java/com/cleanroommc/groovyscript/api/documentation/annotations/Property.java

+73-16
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,7 @@
3030
* </li>
3131
* <li>{@link #property()} either contains nothing if {@link Property} was created attached to a field, or the relevant {@link Field#getName()} string.</li>
3232
* <li>{@link #defaultValue()} a string containing the default value of the property. If empty, defaults to {@code null}.</li>
33-
* <li>{@link #valid()} is an array of {@link Comp} that indicates the requirements of the {@link Property} to pass validation.</li>
34-
* <li>{@link #requirement()} is a localization key that states the requirements for the property to pass validation provided the requirements are too
35-
* complex to represent via {@link #valid()}.</li>
33+
* <li>{@link #comp()} is a {@link Comp} that indicates the requirements of the {@link Property} to pass validation.</li>
3634
* <li>{@link #ignoresInheritedMethods()} if this {@link Property} annotation requires any methods targeting the {@link Property} to not be inherited methods.</li>
3735
* <li>{@link #needsOverride()} if this {@link Property} annotation needs another {@link Property} annotation with this element set to {@code true} to function.
3836
* Used in wrapper classes, such as {@link com.cleanroommc.groovyscript.helper.recipe.AbstractRecipeBuilder AbstractRecipeBuilder}, where some or all of the fields may not be needed in subclasses.</li>
@@ -79,48 +77,107 @@
7977
String defaultValue() default "";
8078

8179
/**
82-
* The primary way to document properties, supplemented by {@link #requirement()}.
80+
* @deprecated {@link #comp()}
81+
*/
82+
@Deprecated
83+
Comp[] valid() default {};
84+
85+
/**
86+
* The primary way to document properties.
8387
* The three main ways this element is used is to refer to:
8488
* <br>- a number: Would indicate comparing directly against the number.
8589
* <br>- an array or list: Would indicate comparing against the length of the array/list.
8690
* <br>- another object: Would use to indicate {@code not null} is required.
91+
* <p>
92+
* In almost all cases, the {@link Comp#types()} element can be assumed due to the values of the desired
93+
* elements being set to a non-default value. For the int elements, the default value is {@link Integer#MIN_VALUE},
94+
* and for the String elements, the default value is an empty string.
95+
* <p>
96+
* Here is a quick shortcut table to better understand what the logic is:
8797
*
8898
* <table>
8999
* <tr>
90100
* <th>validation</th>
91101
* <th>code</th>
102+
* <th>logic</th>
103+
* </tr>
104+
* <tr>
105+
* <td><code>N/A</code></td>
106+
* <td><code>@Comp</code></td>
107+
* <td>no validation is applied</td>
92108
* </tr>
93109
* <tr>
94110
* <td><code>x == 1</code></td>
95-
* <td><code>valid = @Comp("1")</code></td>
111+
* <td><code>@Comp({@link Comp#types types} = {@link Comp.Type#EQ EQ}, {@link Comp#eq eq} = 1)</code></td>
112+
* <td>types checks eq, eq is set to 1</td>
96113
* </tr>
97114
* <tr>
98-
* <td><code>x != 1</code></td>
99-
* <td><code>valid = @Comp(value = "1", type = Comp.Type.NOT)</code></td>
115+
* <td><code>x == 1</code></td>
116+
* <td><code>@Comp({@link Comp#eq eq} = 1)</code></td>
117+
* <td>^1</td>
100118
* </tr>
101119
* <tr>
102-
* <td><code>x != null</code></td>
103-
* <td><code>valid = @Comp(value = "null", type = Comp.Type.NOT)</code></td>
120+
* <td><code>x > 0</code></td>
121+
* <td><code>@Comp({@link Comp#types types} = {@link Comp.Type#GT GT}, {@link Comp#gt gt} = 0)</code></td>
122+
* <td>types checks gt, gt is set to 0</td>
104123
* </tr>
105124
* <tr>
106125
* <td><code>x > 0</code></td>
107-
* <td><code>valid = @Comp(value = "0", type = Comp.Type.GT)</code></td>
126+
* <td><code>@Comp({@link Comp#gt gt} = 0)</code></td>
127+
* <td>^1</td>
128+
* </tr>
129+
* <tr>
130+
* <td><code>x >= 0</code></td>
131+
* <td><code>@Comp({@link Comp#types types} = {@link Comp.Type#GTE GTE}, {@link Comp#gte gte} = 0)</code></td>
132+
* <td>types checks gte, gte is set to 0</td>
133+
* </tr>
134+
* <tr>
135+
* <td><code>x != 1</code></td>
136+
* <td><code>@Comp({@link Comp#types types} = {@link Comp.Type#NOT NOT}, {@link Comp#not not} = "1")</code></td>
137+
* <td>types checks not, not is set to "1"</td>
138+
* </tr>
139+
* <tr>
140+
* <td><code>x != null</code></td>
141+
* <td><code>@Comp({@link Comp#not not} = "null")</code></td>
142+
* <td>^1</td>
108143
* </tr>
109144
* <tr>
110145
* <td><code>x >= 0 && x <= 5</code></td>
111-
* <td><code>valid = {{@literal @}Comp(value = "0", type = Comp.Type.GTE), @Comp(value = "5", type = Comp.Type.LTE)}</code></td>
146+
* <td><code>@Comp({@link Comp#types types} = {{@link Comp.Type#GTE GTE}, {@link Comp.Type#LTE LTE}}, {@link Comp#gte gte} = 0, {@link Comp#lte lte} = 5)</code></td>
147+
* <td>types checks gte and lte, gte is set to 0 and lte is set to 5</td>
148+
* </tr>
149+
* <tr>
150+
* <td><code>x > 0 && x <= 2</code></td>
151+
* <td><code>@Comp({@link Comp#gt gt} = 0, {@link Comp#lte lte} = 2)</code></td>
152+
* <td>^1</td>
153+
* </tr>
154+
* <tr>
155+
* <td><code>complex logic</code></td>
156+
* <td><code>@Comp({@link Comp#unique unique} = "complex logic")</code></td>
157+
* <td>^1</td>
158+
* </tr>
159+
* <tr>
160+
* <td><code>x >= 0 && complex logic</code></td>
161+
* <td><code>@Comp({@link Comp#gte gte} = 0, {@link Comp#unique unique} = "complex logic")</code></td>
162+
* <td>^1</td>
163+
* </tr>
164+
* <tr>
165+
* <td><code>x >= 1 && x <= 9 && complex logic</code></td>
166+
* <td><code>@Comp({@link Comp#gte gte} = 1, {@link Comp#lte lte} = 9, {@link Comp#unique unique} = "complex logic")</code></td>
167+
* <td>^1</td>
112168
* </tr>
113169
* </table>
114170
*
115-
* @return an array of {@link Comp} entries indicating valid values for the property to be.
171+
* ^1 = if types is empty, any non-default values are used
172+
*
173+
* @return a {@link Comp} indicating valid values for the property to be.
116174
*/
117-
Comp[] valid() default {};
175+
Comp comp() default @Comp;
118176

119177
/**
120-
* A localization key to declare validation requirements that are too complex to represent in {@link #valid()}.
121-
*
122-
* @return a string describing the valid value(s) for the field to be to pass validation
178+
* @deprecated use {@link #comp()} instead, via {@code @Comp(types = Comp.Type.UNI, unique = "lang-key-here")}
123179
*/
180+
@Deprecated
124181
String requirement() default "";
125182

126183
/**

src/main/java/com/cleanroommc/groovyscript/compat/mods/actuallyadditions/AtomicReconstructor.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,13 @@ public boolean removeByOutput(ItemStack output) {
6363
});
6464
}
6565

66-
@Property(property = "input", valid = @Comp("1"))
67-
@Property(property = "output", valid = @Comp("1"))
66+
@Property(property = "input", comp = @Comp(eq = 1))
67+
@Property(property = "output", comp = @Comp(eq = 1))
6868
public static class RecipeBuilder extends AbstractRecipeBuilder<LensConversionRecipe> {
6969

70-
@Property(defaultValue = "1", valid = @Comp(type = Comp.Type.GT, value = "0"))
70+
@Property(defaultValue = "1", comp = @Comp(gt = 0))
7171
private int energyUse = 1;
72-
@Property(defaultValue = "ActuallyAdditionsAPI.lensDefaultConversion", valid = @Comp(value = "null", type = Comp.Type.NOT))
72+
@Property(defaultValue = "ActuallyAdditionsAPI.lensDefaultConversion", comp = @Comp(not = "null"))
7373
private Lens type = ActuallyAdditionsAPI.lensDefaultConversion;
7474

7575
@RecipeBuilderMethodDescription

src/main/java/com/cleanroommc/groovyscript/compat/mods/actuallyadditions/BallOfFur.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,10 @@ public boolean removeByOutput(ItemStack output) {
4242
});
4343
}
4444

45-
@Property(property = "output", valid = @Comp("1"))
45+
@Property(property = "output", comp = @Comp(eq = 1))
4646
public static class RecipeBuilder extends AbstractRecipeBuilder<BallOfFurReturn> {
4747

48-
@Property(valid = @Comp(type = Comp.Type.GTE, value = "0"))
48+
@Property(comp = @Comp(gte = 0))
4949
private int weight;
5050

5151
@RecipeBuilderMethodDescription

src/main/java/com/cleanroommc/groovyscript/compat/mods/actuallyadditions/Compost.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,13 @@ public boolean removeByOutput(ItemStack output) {
6262
});
6363
}
6464

65-
@Property(property = "input", valid = @Comp("1"))
66-
@Property(property = "output", valid = @Comp("1"))
65+
@Property(property = "input", comp = @Comp(eq = 1))
66+
@Property(property = "output", comp = @Comp(eq = 1))
6767
public static class RecipeBuilder extends AbstractRecipeBuilder<CompostRecipe> {
6868

69-
@Property(property = "inputDisplay", valid = @Comp(type = Comp.Type.NOT, value = "null"))
69+
@Property(property = "inputDisplay", comp = @Comp(not = "null"))
7070
private IBlockState inputDisplay;
71-
@Property(property = "outputDisplay", valid = @Comp(type = Comp.Type.NOT, value = "null"))
71+
@Property(property = "outputDisplay", comp = @Comp(not = "null"))
7272
private IBlockState outputDisplay;
7373

7474
@RecipeBuilderMethodDescription

src/main/java/com/cleanroommc/groovyscript/compat/mods/actuallyadditions/Crusher.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,11 @@ public boolean removeByOutput(ItemStack output) {
6363
});
6464
}
6565

66-
@Property(property = "input", valid = @Comp("1"))
67-
@Property(property = "output", valid = {@Comp(type = Comp.Type.GTE, value = "1"), @Comp(type = Comp.Type.LTE, value = "2")})
66+
@Property(property = "input", comp = @Comp(eq = 1))
67+
@Property(property = "output", comp = @Comp(gte = 1, lte = 2))
6868
public static class RecipeBuilder extends AbstractRecipeBuilder<CrusherRecipe> {
6969

70-
@Property(valid = {@Comp(type = Comp.Type.GTE, value = "0"), @Comp(type = Comp.Type.LTE, value = "100")})
70+
@Property(comp = @Comp(gte = 0, lte = 100))
7171
private int chance;
7272

7373
@RecipeBuilderMethodDescription

0 commit comments

Comments
 (0)