Skip to content

Commit ecf3bae

Browse files
committed
Added Empire State API
There's still a few things that need to be done till I can declare v1.1.0 as "done"
1 parent 866023f commit ecf3bae

19 files changed

Lines changed: 634 additions & 68 deletions

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,12 @@
1616
* This should be used to reduce the amount of calls to obfuscated methods, which will improve the
1717
* sanity of anyone that is working on updating an extension.
1818
*/
19-
public class Galimulator {
19+
public final class Galimulator {
20+
21+
/**
22+
* Constructor that should not be called.
23+
*/
24+
private Galimulator() {}
2025

2126
/**
2227
* Connect two stars with each other. The preferred way of connecting two stars.

src/main/java/de/geolykt/starloader/api/empire/ActiveEmpire.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,14 @@ public interface ActiveEmpire extends Empire, Metadatable {
116116
*/
117117
public @NotNull Religion getReligion();
118118

119+
/**
120+
* Obtains the registry key of the current state of the empire.
121+
* To obtain the actual empire state object, it has to be passed into it's respective registry first.
122+
*
123+
* @return A {@link NamespacedKey} representing the current state of the empire.
124+
*/
125+
public @NotNull NamespacedKey getState();
126+
119127
/**
120128
* Obtains the technology level of the empire. It shouldn't be below 1 as math have some issues there and this
121129
* event does not occur naturally.
@@ -186,4 +194,14 @@ public interface ActiveEmpire extends Empire, Metadatable {
186194
* @param religion The new religion to preach
187195
*/
188196
public void setReligion(@NotNull Religion religion);
197+
198+
/**
199+
* Sets the state of the empire via a registry key that corresponds to the future state of the empire.
200+
* Note that the key should be valid and for invalid keys an exception will quickly be thrown.
201+
*
202+
* @param state A {@link NamespacedKey} representing the future state of the empire.
203+
* @param force If true no events will be called and the action is more likely to happen
204+
* @return Whether the state was changed
205+
*/
206+
public boolean setState(@NotNull NamespacedKey state, boolean force);
189207
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package de.geolykt.starloader.api.event.empire;
2+
3+
import org.jetbrains.annotations.NotNull;
4+
5+
import de.geolykt.starloader.api.empire.ActiveEmpire;
6+
import de.geolykt.starloader.api.registry.RegistryKeys;
7+
8+
/**
9+
* Event that is fired whenever an empire enters a rioting stage.
10+
*/
11+
public class EmpireRiotingEvent extends EmpireStateChangeEvent {
12+
13+
/**
14+
* Constructor.
15+
*
16+
* @param empire The affected empire
17+
*/
18+
public EmpireRiotingEvent(@NotNull ActiveEmpire empire) {
19+
super(empire, RegistryKeys.GALIMULATOR_RIOTING);
20+
}
21+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package de.geolykt.starloader.api.event.empire;
2+
3+
import org.jetbrains.annotations.NotNull;
4+
5+
import de.geolykt.starloader.api.NamespacedKey;
6+
import de.geolykt.starloader.api.empire.ActiveEmpire;
7+
8+
/**
9+
* Event that is fired whenever an Empire changes into a stanle state from a less stable state, like
10+
* if the empire changed from the RIOTING or DEGENERATING state.
11+
*
12+
* @see EmpireUnfortifyEvent
13+
*/
14+
public class EmpireStabiliseEvent extends EmpireStateChangeEvent {
15+
16+
/**
17+
* Constructor.
18+
* For certain states a subclass might be better used.
19+
* The validity of the registry key is not directly checked by this constructor,
20+
* however it would be nice of the caller to make sure that it is valid as otherwise bad things can happen.
21+
*
22+
* @param empire The target empire
23+
* @param state The registry key of the new state of the empire
24+
*/
25+
public EmpireStabiliseEvent(@NotNull ActiveEmpire empire, @NotNull NamespacedKey state) {
26+
super(empire, state);
27+
}
28+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package de.geolykt.starloader.api.event.empire;
2+
3+
import org.jetbrains.annotations.NotNull;
4+
5+
import de.geolykt.starloader.api.NamespacedKey;
6+
import de.geolykt.starloader.api.empire.ActiveEmpire;
7+
import de.geolykt.starloader.api.event.Cancellable;
8+
9+
/**
10+
* Event fired when the state of an empire is altered.
11+
*/
12+
public class EmpireStateChangeEvent extends EmpireEvent implements Cancellable {
13+
14+
/**
15+
* The registry key of the proposed new state of the empire.
16+
*/
17+
protected final NamespacedKey state;
18+
19+
/**
20+
* The cancellation status of the event.
21+
* It should not be modified directly and instead be modified via {@link Cancellable#setCancelled(boolean)}.
22+
*/
23+
private boolean cancelled = false;
24+
25+
/**
26+
* Constructor.
27+
* For certain states a subclass might be better used.
28+
* The validity of the registry key is not directly checked by this constructor,
29+
* however it would be nice of the caller to make sure that it is valid as otherwise bad things can happen.
30+
*
31+
* @param empire The target empire
32+
* @param newState The registry key of the new state of the empire
33+
*/
34+
public EmpireStateChangeEvent(@NotNull ActiveEmpire empire, @NotNull NamespacedKey newState) {
35+
super(empire);
36+
state = newState;
37+
}
38+
39+
@Override
40+
public boolean isCancelled() {
41+
return cancelled;
42+
}
43+
44+
@Override
45+
public void setCancelled(boolean cancelled) {
46+
this.cancelled = cancelled;
47+
}
48+
49+
/**
50+
* Obtains a {@link NamespacedKey} that represents the new empire state that will be applied once
51+
* the event is passed without getting cancelled (this might not be the case if the event is fired by an extension
52+
* for dummy checks). To obtain the empire state it has to be passed through a registry beforehand.
53+
* Note that the validity of the key is not guaranteed, however it is very likely that it is a valid registry key.
54+
* Unless the caller of the constructor did some errors.
55+
*
56+
* @return The {@link NamespacedKey} of the new state of the empire.
57+
*/
58+
public @NotNull NamespacedKey getNewState() {
59+
return state;
60+
}
61+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package de.geolykt.starloader.api.event.empire;
2+
3+
import org.jetbrains.annotations.NotNull;
4+
5+
import de.geolykt.starloader.api.empire.ActiveEmpire;
6+
import de.geolykt.starloader.api.registry.RegistryKeys;
7+
8+
/**
9+
* Event that is fired whenever an Empire transcends into hyperbliss.
10+
* It is most often fired almost directly after an {@link TechnologyLevelSetEvent}, however extensions
11+
* might fire this event directly.
12+
*/
13+
public class EmpireTranscendEvent extends EmpireStateChangeEvent {
14+
15+
/**
16+
* Constructor.
17+
*
18+
* @param empire The affected empire
19+
*/
20+
public EmpireTranscendEvent(@NotNull ActiveEmpire empire) {
21+
super(empire, RegistryKeys.GALIMULATOR_TRANSCENDING);
22+
// No further data
23+
}
24+
}

src/main/java/de/geolykt/starloader/api/gui/Drawing.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ public static float drawText(@NotNull String message, float x, float y, @NotNull
112112
*
113113
* @param message The message to send
114114
*/
115-
public static void sendBulletin(@NotNull String message) {
115+
public static void sendBulletin(@NotNull String message) { // FIXME typo
116116
implementation.sendBulletin(message);
117117
}
118118

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package de.geolykt.starloader.api.registry;
2+
3+
import de.geolykt.starloader.api.registry.MetadatableRegistry.MetadataEntry;
4+
5+
/**
6+
* The metadata entry for empire states.
7+
* This for example can be used for dynamic responses to the empire state as well
8+
* as further extension harmony.
9+
*/
10+
public class EmpireStateMetadataEntry extends MetadataEntry {
11+
12+
/**
13+
* Used for {@link #isStable()}
14+
*/
15+
protected final boolean stable;
16+
17+
/**
18+
* Whether diplomatic relations are hindered due to the warmongering behaviour
19+
* Used for {@link #isWarmongering()}
20+
*/
21+
protected final boolean warmongering;
22+
23+
/**
24+
* Constructor.
25+
*
26+
* @param stable Whether to consider the state "stable"
27+
* @param warmongering Whether diplomatic relations are hindered due to the warmongering behaviour
28+
*/
29+
public EmpireStateMetadataEntry(boolean stable, boolean warmongering) {
30+
this.stable = stable;
31+
this.warmongering = warmongering;
32+
}
33+
34+
/**
35+
* Obtains whether this empire state can be considered "stable", for example
36+
* Fortifying as well as expanding fall under this category.
37+
*
38+
* @return Whether the state is considered stable
39+
*/
40+
public boolean isStable() {
41+
return stable;
42+
}
43+
44+
/**
45+
* Whether diplomatic relations are handicapped due to the warmongering behaviour.
46+
* In the base game, blood crusade, crusading and ALL WILL BE ASHES fall in this category.
47+
* If the implementation returns true, then the treaties will be voided when the new state is added
48+
*
49+
* @return The warmongering flag
50+
*/
51+
public boolean isWarmongering() {
52+
return warmongering;
53+
}
54+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package de.geolykt.starloader.api.registry;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
import org.jetbrains.annotations.NotNull;
7+
import org.jetbrains.annotations.Nullable;
8+
9+
import de.geolykt.starloader.api.NamespacedKey;
10+
11+
/**
12+
* Registry of enum and/or enum-like objects with additionally capability to add metadata to
13+
* the key-value pairs. This is added for extension harmony as multiple extensions cannot do these
14+
* themselves without breaking aspects of the functionality or creating agreements themselves.
15+
* Since the StarloaderAPI is already one of the first extensions to experiment with such aspects,
16+
* the StarloaderAPI is taking the authority in this.
17+
* The metadata is a concept that is introduced since especially non-abstract enums are very state based
18+
* and a metadata API would make it more data based and increase the modifiability of the behaviour of the game.
19+
*
20+
* @param <T> The type the registry is holding
21+
* @param <U> The metadata container type of the registry entries
22+
*/
23+
public abstract class MetadatableRegistry<T, U extends MetadatableRegistry.MetadataEntry> extends Registry<T> {
24+
25+
/**
26+
* The metadata entry structure. Does nothing on it's own other than looking good; it is up to the implementation
27+
* to make it more meaningful.
28+
*/
29+
public static abstract class MetadataEntry {}
30+
31+
/**
32+
* Internal map containing the key-metadata entry pairs of the registry for lookup.
33+
*/
34+
protected final Map<NamespacedKey, U> metadataEntries = new HashMap<>();
35+
36+
/**
37+
* Obtains the metadata entry bound to the given registry key. If no metadata is bound at that key,
38+
* then null will be returned.
39+
*
40+
* @param key The {@link NamespacedKey} that is used for the lookup operation
41+
* @return The {@link MetadataEntry}
42+
*/
43+
public @Nullable U getMetadataEntry(@NotNull NamespacedKey key) {
44+
return metadataEntries.get(key);
45+
}
46+
47+
/**
48+
* @deprecated This method has no use as it does not specify the metadata.
49+
*
50+
* This operation instantly throws an exception
51+
*
52+
* @param key irrelevant
53+
* @param value irrelevant
54+
*/
55+
@Override
56+
@Deprecated(forRemoval = false, since = "1.1.0")
57+
public final void register(@NotNull NamespacedKey key, @NotNull T value) {
58+
throw new IllegalArgumentException("The metadatable registry requires to know the metadata entry."
59+
+ "Use the other register method instead.");
60+
}
61+
62+
/**
63+
* Registers the value to the given key; the implementation might be thread-safe, however extensions should always
64+
* believe that multithreading can be dangerous and as such this method should never be called concurrently as otherwise
65+
* some other things (such as the values array) might break.
66+
*
67+
* @param key The key of the entry to register
68+
* @param value The value of the entry
69+
* @param metadata The metadata entry.
70+
*/
71+
public abstract void register(@NotNull NamespacedKey key, @NotNull T value, @NotNull U metadata);
72+
}

src/main/java/de/geolykt/starloader/api/registry/Registry.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@
99
import de.geolykt.starloader.DebugNagException;
1010
import de.geolykt.starloader.api.NamespacedKey;
1111
import snoddasmannen.galimulator.EmpireSpecial;
12+
import snoddasmannen.galimulator.EmpireState;
1213

1314
/**
14-
* Registry of enum and/or enum-like objects.s
15+
* Registry of enum and/or enum-like objects.
1516
* This is added for extension harmony as multiple extensions cannot do these themselves without
1617
* breaking aspects of the functionality or creating agreements themselves. Since the StarloaderAPI
1718
* is already one of the first extensions to experiment with such aspects, the StarloaderAPI is taking
@@ -26,6 +27,11 @@ public abstract class Registry<T> {
2627
*/
2728
public static Registry<EmpireSpecial> EMPIRE_SPECIALS;
2829

30+
/**
31+
* The empire state registry.
32+
*/
33+
public static MetadatableRegistry<EmpireState, EmpireStateMetadataEntry> EMPIRE_STATES;
34+
2935
/**
3036
* Internal map containing the key-value pairs of the registry for lookup.
3137
*/
@@ -87,6 +93,8 @@ public abstract class Registry<T> {
8793
* Registers the value to the given key; the implementation might be thread-safe, however extensions should always
8894
* believe that multithreading can be dangerous and as such this method should never be called concurrently as otherwise
8995
* some other things (such as the values array) might break.
96+
* Note that {@link MetadatableRegistry} does not support this method, if the registry is one of these,
97+
* {@link MetadatableRegistry#register(NamespacedKey, Object, de.geolykt.starloader.api.registry.MetadatableRegistry.MetadataEntry)} should be used instead.
9098
*
9199
* @param key The key of the entry to register
92100
* @param value The value of the entry

0 commit comments

Comments
 (0)