Skip to content

Commit 9e57080

Browse files
committed
Add RegistryExpander
will be expanded to have a wider use late
1 parent 359fb12 commit 9e57080

6 files changed

Lines changed: 354 additions & 36 deletions

File tree

src/main/java/de/geolykt/starloader/api/NamespacedKey.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
public class NamespacedKey {
1111

12-
private final String keyString;
12+
private final @NotNull String keyString;
1313
private final @Nullable Extension nameSpaceNamesake;
1414
private String namespaceString; // initialised in a lazy manner
1515

@@ -61,7 +61,7 @@ public boolean equals(Object obj) {
6161
*
6262
* @return The key of this key
6363
*/
64-
public String getKey() {
64+
public @NotNull String getKey() {
6565
return keyString;
6666
}
6767

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
package de.geolykt.starloader.api.registry;
2+
3+
import org.jetbrains.annotations.NotNull;
4+
import org.jetbrains.annotations.Nullable;
5+
6+
import com.badlogic.gdx.graphics.Color;
7+
8+
import de.geolykt.starloader.api.NamespacedKey;
9+
import de.geolykt.starloader.api.gui.FlagSymbol;
10+
11+
/**
12+
* The registry expander takes care or adding new items to registries without requiring the presence of the galimulator jar.
13+
*
14+
* @since 1.5.0
15+
*/
16+
public final class RegistryExpander {
17+
18+
/**
19+
* Interface that is used to delegate the static methods of the Registry expander to.
20+
*/
21+
public static interface Implementation {
22+
23+
/**
24+
* Adds an empire special to the internal empire special registry.
25+
*
26+
* @param key The namespaced key to register the special under
27+
* @param enumName The name of the special as returned by {@link Enum#name()}. Convention is to have it in UPPERCASE_SNAKE_CASE
28+
* @param name The user-friendly name of the special.
29+
* @param abbreviation The user-friendly abbreviation of the special. Usually 3 letters long.
30+
* @param description The description of the special.
31+
* @param color The color of the special. Used for the special boxes.
32+
* @param techMod The technological impact of the special on the empire. Ranges from 0.0 to whatever. 1.0 for no impact
33+
* @param indMod The industrial impact of the special on the empire. Ranges from 0.0 to whatever. 1.0 for no impact. The industry modifier changes the ship cap, along other things.
34+
* @param stabilityMod The stability impact of the special on the empire. Ranges from 0.0 to whatever. 1.0 for no impact
35+
* @param peaceMod Exact use not fully explored.
36+
* @param bansAlliances Whether empires with this special are banned from joining alliances.
37+
*/
38+
public void addEmpireSpecial(@NotNull NamespacedKey key, @NotNull String enumName, @NotNull String name,
39+
@NotNull String abbreviation, @NotNull String description, @NotNull Color color,
40+
float techMod, float indMod, float stabilityMod, float peaceMod, boolean bansAlliances);
41+
42+
/**
43+
* Adds a flag symbol to the internal flag symbol registry.
44+
* The flag symbol will have the specified parameters.
45+
*
46+
* @param key The namespaced key to register the symbol under
47+
* @param enumName The name of the symbol as specified by {@link Enum#name()}. Convention is to have it in UPPERCASE_SNAKE_CASE
48+
* @param sprite The name of the sprite. For the file "data/sprites/sprite.png" the name is "sprite.png". Make sure to create that file at starloader startup / onInit() of your extension
49+
* @param mustBeSquare Whether any flag components with this symbol must be square
50+
* @param width The MAXIMUM width of the component, or 0 for no limitations
51+
* @param height The MAXIMUM height of the component, or 0 for no limitations
52+
* @return The created {@link FlagSymbol} instance.
53+
*/
54+
public @NotNull FlagSymbol addFlagSymbol(@NotNull NamespacedKey key, @NotNull String enumName, @NotNull String sprite,
55+
boolean mustBeSquare, int width, int height);
56+
}
57+
58+
/**
59+
* The currently active implementation used for static methods in this class.
60+
*/
61+
private static @Nullable Implementation impl;
62+
63+
/**
64+
* Adds an empire special to the internal empire special registry.
65+
*
66+
* @param key The namespaced key to register the special under
67+
* @param enumName The name of the special as returned by {@link Enum#name()}. Convention is to have it in UPPERCASE_SNAKE_CASE
68+
* @param name The user-friendly name of the special.
69+
* @param abbreviation The user-friendly abbreviation of the special. Usually 3 letters long.
70+
* @param description The description of the special.
71+
* @param color The color of the special. Used for the special boxes.
72+
* @param techMod The technological impact of the special on the empire. Ranges from 0.0 to whatever. 1.0 for no impact
73+
* @param indMod The industrial impact of the special on the empire. Ranges from 0.0 to whatever. 1.0 for no impact. The industry modifier changes the ship cap, along other things.
74+
* @param stabilityMod The stability impact of the special on the empire. Ranges from 0.0 to whatever. 1.0 for no impact
75+
* @param peaceMod Exact use not fully explored.
76+
* @param bansAlliances Whether empires with this special are banned from joining alliances.
77+
*/
78+
public static void addEmpireSpecial(@NotNull NamespacedKey key, @NotNull String enumName, @NotNull String name,
79+
@NotNull String abbreviation, @NotNull String description, @NotNull Color color,
80+
float techMod, float indMod, float stabilityMod, float peaceMod, boolean bansAlliances) {
81+
requireImplementation().addEmpireSpecial(key, enumName, name, abbreviation, description, color, techMod, indMod, stabilityMod, peaceMod, bansAlliances);
82+
}
83+
84+
/**
85+
* Adds a flag symbol to the internal flag symbol registry.
86+
* The flag symbol will have the specified parameters.
87+
*
88+
* @param key The namespaced key to register the symbol under
89+
* @param enumName The name of the symbol as specified by {@link Enum#name()}. Convention is to have it in UPPERCASE_SNAKE_CASE
90+
* @param sprite The name of the sprite. For the file "data/sprites/sprite.png" the name is "sprite.png". Make sure to create that file at starloader startup / onInit() of your extension
91+
* @param mustBeSquare Whether any flag components with this symbol must be square
92+
* @param width The MAXIMUM width of the component, or 0 for no limitations
93+
* @param height The MAXIMUM height of the component, or 0 for no limitations
94+
* @return The created {@link FlagSymbol} instance.
95+
*/
96+
public static @NotNull FlagSymbol addFlagSymbol(@NotNull NamespacedKey key, @NotNull String enumName, @NotNull String sprite,
97+
boolean mustBeSquare, int width, int height) {
98+
return requireImplementation().addFlagSymbol(key, enumName, sprite, mustBeSquare, width, height);
99+
}
100+
101+
/**
102+
* Obtains the implementation of the static methods of this class if possible.
103+
* If there is no such implementation it will return null.
104+
*
105+
* @return The currently active implementation
106+
*/
107+
public static @Nullable Implementation getImplementation() {
108+
return impl;
109+
}
110+
111+
/**
112+
* Obtains the implementation of the static methods of this class if possible.
113+
* If there is no such implementation it will throw an {@link IllegalStateException}.
114+
*
115+
* @return The currently active implementation
116+
*/
117+
public static @NotNull Implementation requireImplementation() {
118+
Implementation impl = RegistryExpander.impl;
119+
if (impl == null) {
120+
throw new IllegalStateException("Implementation not set.");
121+
}
122+
return impl;
123+
}
124+
125+
/**
126+
* The constructor of this class, should not be called as there is little need for taht.
127+
*/
128+
private RegistryExpander() {
129+
}
130+
}

src/main/java/de/geolykt/starloader/impl/registry/Registries.java

Lines changed: 57 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -63,22 +63,43 @@ public static void initAudio() {
6363
public static void initEmpireSpecials() {
6464
LOGGER.info("Registering empire specials");
6565
SimpleEnumRegistry<EmpireSpecial> specials = new SimpleEnumRegistry<>(EmpireSpecial.class);
66-
specials.register(RegistryKeys.GALIMULATOR_MILITANT, EmpireSpecial.MILITANT);
67-
specials.register(RegistryKeys.GALIMULATOR_AGGRESSIVE, EmpireSpecial.AGGRESSIVE);
68-
specials.register(RegistryKeys.GALIMULATOR_DEFENSIVE, EmpireSpecial.DEFENSIVE);
69-
specials.register(RegistryKeys.GALIMULATOR_SCIENTIFIC, EmpireSpecial.SCIENTIFIC);
70-
specials.register(RegistryKeys.GALIMULATOR_STABLE, EmpireSpecial.STABLE);
71-
specials.register(RegistryKeys.GALIMULATOR_UNSTABLE, EmpireSpecial.UNSTABLE);
72-
specials.register(RegistryKeys.GALIMULATOR_EXPLOSIVE, EmpireSpecial.EXPLOSIVE);
73-
specials.register(RegistryKeys.GALIMULATOR_SLOW_STARTER, EmpireSpecial.SLOW_STARTER);
74-
specials.register(RegistryKeys.GALIMULATOR_DIPLOMATIC, EmpireSpecial.DIPLOMATIC);
75-
specials.register(RegistryKeys.GALIMULATOR_XENOPHOBIC, EmpireSpecial.XENOPHOBIC);
76-
specials.register(RegistryKeys.GALIMULATOR_FANATICAL, EmpireSpecial.FANATICAL);
77-
specials.register(RegistryKeys.GALIMULATOR_RECLUSIVE, EmpireSpecial.RECLUSIVE);
78-
specials.register(RegistryKeys.GALIMULATOR_HORDE, EmpireSpecial.HORDE);
79-
specials.register(RegistryKeys.GALIMULATOR_CAPITALIST, EmpireSpecial.CAPITALIST);
80-
specials.register(RegistryKeys.GALIMULATOR_CULT, EmpireSpecial.CULT);
81-
specials.register(RegistryKeys.GALIMULATOR_INDUSTRIAL, EmpireSpecial.INDUSTRIAL);
66+
@SuppressWarnings("null") // Since we removed the enum access flag for the EmpireSpecial class, eclipse treats it as an ordinary class; thus we have to use hacks to make it comprehend that the members are not null.
67+
@NotNull EmpireSpecial[] specialsArray = new EmpireSpecial[] {
68+
EmpireSpecial.MILITANT,
69+
EmpireSpecial.AGGRESSIVE,
70+
EmpireSpecial.DEFENSIVE,
71+
EmpireSpecial.SCIENTIFIC,
72+
EmpireSpecial.STABLE,
73+
EmpireSpecial.UNSTABLE,
74+
EmpireSpecial.EXPLOSIVE,
75+
EmpireSpecial.SLOW_STARTER,
76+
EmpireSpecial.DIPLOMATIC,
77+
EmpireSpecial.XENOPHOBIC,
78+
EmpireSpecial.FANATICAL,
79+
EmpireSpecial.RECLUSIVE,
80+
EmpireSpecial.HORDE,
81+
EmpireSpecial.CAPITALIST,
82+
EmpireSpecial.CULT,
83+
EmpireSpecial.INDUSTRIAL
84+
};
85+
specials.registerBulk(new @NotNull NamespacedKey[] {
86+
RegistryKeys.GALIMULATOR_MILITANT,
87+
RegistryKeys.GALIMULATOR_AGGRESSIVE,
88+
RegistryKeys.GALIMULATOR_DEFENSIVE,
89+
RegistryKeys.GALIMULATOR_SCIENTIFIC,
90+
RegistryKeys.GALIMULATOR_STABLE,
91+
RegistryKeys.GALIMULATOR_UNSTABLE,
92+
RegistryKeys.GALIMULATOR_EXPLOSIVE,
93+
RegistryKeys.GALIMULATOR_SLOW_STARTER,
94+
RegistryKeys.GALIMULATOR_DIPLOMATIC,
95+
RegistryKeys.GALIMULATOR_XENOPHOBIC,
96+
RegistryKeys.GALIMULATOR_FANATICAL,
97+
RegistryKeys.GALIMULATOR_RECLUSIVE,
98+
RegistryKeys.GALIMULATOR_HORDE,
99+
RegistryKeys.GALIMULATOR_CAPITALIST,
100+
RegistryKeys.GALIMULATOR_CULT,
101+
RegistryKeys.GALIMULATOR_INDUSTRIAL
102+
}, specialsArray);
82103
Registry.EMPIRE_SPECIALS = specials;
83104
EventManager.handleEvent(new RegistryRegistrationEvent(specials, EmpireSpecial.class, RegistryRegistrationEvent.REGISTRY_EMPIRE_SPECIAL));
84105
}
@@ -114,6 +135,25 @@ public static void initEmpireStates() {
114135
public static void initFlagSymbols() {
115136
LOGGER.info("Registering flag symbols");
116137
SimpleEnumRegistry<BuiltinSymbols> symbolRegistry = new SimpleEnumRegistry<>(BuiltinSymbols.class);
138+
@SuppressWarnings("null")
139+
@NotNull BuiltinSymbols[] symbols = new @NotNull BuiltinSymbols[] {
140+
BuiltinSymbols.CIRCLE,
141+
BuiltinSymbols.SQUARE,
142+
BuiltinSymbols.TRIANGLE,
143+
BuiltinSymbols.STRIPES,
144+
BuiltinSymbols.HORIZONTAL_STRIPE,
145+
BuiltinSymbols.VERTICAL_STRIPE,
146+
BuiltinSymbols.GRADIENT_HORIZONTAL_STRIPE,
147+
BuiltinSymbols.GRADIENTLVERTICAL_STRIPE, // What should we do with this typo?
148+
BuiltinSymbols.FORCEFIELD,
149+
BuiltinSymbols.STAR,
150+
BuiltinSymbols.STAR2,
151+
BuiltinSymbols.CRESCENT,
152+
BuiltinSymbols.SEMICIRCLE,
153+
BuiltinSymbols.CROSS,
154+
BuiltinSymbols.FUNNEL,
155+
BuiltinSymbols.FRAME
156+
};
117157
symbolRegistry.registerBulk(new @NotNull NamespacedKey[] {
118158
RegistryKeys.GALIMULATOR_FLAG_CIRCLE,
119159
RegistryKeys.GALIMULATOR_FLAG_SQUARE,
@@ -131,24 +171,7 @@ public static void initFlagSymbols() {
131171
RegistryKeys.GALIMULATOR_FLAG_CROSS,
132172
RegistryKeys.GALIMULATOR_FLAG_FUNNEL,
133173
RegistryKeys.GALIMULATOR_FLAG_FRAME
134-
}, new @NotNull BuiltinSymbols[] {
135-
BuiltinSymbols.CIRCLE,
136-
BuiltinSymbols.SQUARE,
137-
BuiltinSymbols.TRIANGLE,
138-
BuiltinSymbols.STRIPES,
139-
BuiltinSymbols.HORIZONTAL_STRIPE,
140-
BuiltinSymbols.VERTICAL_STRIPE,
141-
BuiltinSymbols.GRADIENT_HORIZONTAL_STRIPE,
142-
BuiltinSymbols.GRADIENTLVERTICAL_STRIPE, // What should we do with this typo?
143-
BuiltinSymbols.FORCEFIELD,
144-
BuiltinSymbols.STAR,
145-
BuiltinSymbols.STAR2,
146-
BuiltinSymbols.CRESCENT,
147-
BuiltinSymbols.SEMICIRCLE,
148-
BuiltinSymbols.CROSS,
149-
BuiltinSymbols.FUNNEL,
150-
BuiltinSymbols.FRAME
151-
});
174+
}, symbols);
152175
Registry.FLAG_SYMBOLS = symbolRegistry;
153176
EventManager.handleEvent(new RegistryRegistrationEvent(symbolRegistry, BuiltinSymbols.class, RegistryRegistrationEvent.REGISTRY_FLAG_SYMBOL));
154177
}
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
package de.geolykt.starloader.impl.registry;
2+
3+
import com.badlogic.gdx.graphics.Color;
4+
5+
import de.geolykt.starloader.api.registry.RegistryExpander;
6+
7+
import snoddasmannen.galimulator.EmpireSpecial;
8+
import snoddasmannen.galimulator.GalColor;
9+
10+
/**
11+
* An implementation of {@link EmpireSpecial} that delegates method calls to field constants.
12+
* This class is used in combination to starloader's registry system.
13+
*
14+
* @see RegistryExpander#addEmpireSpecial(de.geolykt.starloader.api.NamespacedKey, String, String, String, String, Color, float, float, float, float, boolean)
15+
*/
16+
public class SLEmpireSpecial extends EmpireSpecial {
17+
18+
/**
19+
* serialVersionUID.
20+
*/
21+
private static final long serialVersionUID = 5285253728749916918L;
22+
23+
/**
24+
* Whether empires with this special are banned from joining alliances.
25+
*/
26+
protected final boolean alliancesBanned;
27+
28+
/**
29+
* The color of the special. Used for GUI-related tasks, more specifically
30+
* the color of the name of the special within the leaderboard as well as
31+
* the color of the box of the special within the empire screen.
32+
*/
33+
protected final GalColor color;
34+
35+
/**
36+
* The industrial impact of the special on the empire. Ranges from 0.0 to whatever. 1.0 for no impact. The industry modifier changes the ship cap, along other things.
37+
*/
38+
protected final float ind;
39+
40+
/**
41+
* Exact use of this variable not fully explored.
42+
*/
43+
protected final float peace;
44+
45+
/**
46+
* The stability impact of the special on the empire. Ranges from 0.0 to whatever. 1.0 for no impact.
47+
*/
48+
protected final float stability;
49+
50+
/**
51+
* The technological impact of the special on the empire. Ranges from 0.0 to whatever. 1.0 for no impact
52+
*/
53+
protected final float tech;
54+
55+
/**
56+
* The constructor. Instances of this class should be registered via the appropriate registry.
57+
*
58+
* @param enumName The name of the special as returned by {@link Enum#name()}. Convention is to have it in UPPERCASE_SNAKE_CASE
59+
* @param ordinal The integer ordinal of the enum instance as specified by {@link Enum#ordinal()}. Should be equal to the registration order.
60+
* @param name The user-friendly name of the special.
61+
* @param abbreviation The user-friendly abbreviation of the special. Usually 3 letters long.
62+
* @param description The description of the special.
63+
* @param color The color of the special. Used for the special boxes.
64+
* @param tech The technological impact of the special on the empire. Ranges from 0.0 to whatever. 1.0 for no impact
65+
* @param ind The industrial impact of the special on the empire. Ranges from 0.0 to whatever. 1.0 for no impact. The industry modifier changes the ship cap, along other things.
66+
* @param stability The stability impact of the special on the empire. Ranges from 0.0 to whatever. 1.0 for no impact
67+
* @param peace Exact use not fully explored.
68+
* @param alliancesBanned Whether empires with this special are banned from joining alliances.
69+
*/
70+
public SLEmpireSpecial(String enumName, int ordinal, String name, String abbreviation, String description,
71+
Color color, float tech, float ind, float stability, float peace, boolean alliancesBanned) {
72+
super(enumName, ordinal, name, abbreviation, description);
73+
this.color = new GalColor(color);
74+
this.tech = tech;
75+
this.ind = ind;
76+
this.stability = stability;
77+
this.peace = peace;
78+
this.alliancesBanned = alliancesBanned;
79+
}
80+
81+
@Override
82+
public float a() {
83+
return tech;
84+
}
85+
86+
@Override
87+
public float b() {
88+
return ind;
89+
}
90+
91+
@Override
92+
public float c() {
93+
return stability;
94+
}
95+
96+
@Override
97+
public float d() {
98+
return peace;
99+
}
100+
101+
@Override
102+
public boolean e() {
103+
return alliancesBanned;
104+
}
105+
106+
@Override
107+
public GalColor j() {
108+
return color;
109+
}
110+
}

0 commit comments

Comments
 (0)