forked from GregTechCEu/GregTech
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Namespaced MTE Registries (GregTechCEu#2505)
- Loading branch information
1 parent
375af9e
commit e79ad8f
Showing
78 changed files
with
1,237 additions
and
184 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
src/main/java/gregtech/api/metatileentity/registry/MTEManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package gregtech.api.metatileentity.registry; | ||
|
||
import gregtech.api.GTValues; | ||
|
||
import net.minecraftforge.fml.common.eventhandler.Event; | ||
|
||
import it.unimi.dsi.fastutil.ints.Int2ObjectMap; | ||
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap; | ||
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap; | ||
import org.jetbrains.annotations.ApiStatus; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.annotations.UnmodifiableView; | ||
|
||
import java.util.Collection; | ||
import java.util.Map; | ||
|
||
public final class MTEManager { | ||
|
||
private static MTEManager instance; | ||
|
||
private static int networkId; | ||
private static MTERegistry internalRegistry; | ||
|
||
private final Map<String, MTERegistry> registryMap = new Object2ObjectOpenHashMap<>(); | ||
private final Int2ObjectMap<MTERegistry> networkMap = new Int2ObjectOpenHashMap<>(); | ||
|
||
/** | ||
* @return the global MTE Manager instance | ||
*/ | ||
@ApiStatus.Internal | ||
public static @NotNull MTEManager getInstance() { | ||
if (instance == null) { | ||
instance = new MTEManager(); | ||
internalRegistry = instance.createRegistry(GTValues.MODID); | ||
} | ||
return instance; | ||
} | ||
|
||
private MTEManager() {} | ||
|
||
/** | ||
* @param modid the modid of the registry | ||
* @return the registry associated with the modid, otherwise the default registry | ||
*/ | ||
public @NotNull MTERegistry getRegistry(@NotNull String modid) { | ||
MTERegistry registry = registryMap.get(modid); | ||
if (registry == null) { | ||
throw new IllegalArgumentException("No MTE registry exists for modid " + modid); | ||
} | ||
return registry; | ||
} | ||
|
||
/** | ||
* Create an MTE Registry | ||
* | ||
* @param modid the modid for the registry | ||
* @return the created registry | ||
*/ | ||
public @NotNull MTERegistry createRegistry(@NotNull String modid) { | ||
if (registryMap.containsKey(modid)) { | ||
throw new IllegalArgumentException("MTE Registry for modid " + modid + " is already registered"); | ||
} | ||
MTERegistry registry = new MTERegistry(modid, ++networkId); | ||
registryMap.put(modid, registry); | ||
networkMap.put(networkId, registry); | ||
return registry; | ||
} | ||
|
||
/** | ||
* @param networkId the network id of the registry | ||
* @return the registry associated with the network id, otherwise the default registry | ||
*/ | ||
public @NotNull MTERegistry getRegistry(int networkId) { | ||
MTERegistry registry = networkMap.get(networkId); | ||
return registry == null ? internalRegistry : registry; | ||
} | ||
|
||
/** | ||
* @return all the available MTE registries | ||
*/ | ||
public @NotNull @UnmodifiableView Collection<@NotNull MTERegistry> getRegistries() { | ||
return registryMap.values(); | ||
} | ||
|
||
/** | ||
* Event during which MTE Registries should be added by mods. | ||
* <p> | ||
* Use {@link #createRegistry(String)} to create a new MTE registry. | ||
*/ | ||
public static class MTERegistryEvent extends Event {} | ||
} |
Oops, something went wrong.