-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow custom GameSpaceList registration
- Loading branch information
Showing
6 changed files
with
101 additions
and
18 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
68 changes: 68 additions & 0 deletions
68
src/main/java/xyz/nucleoid/plasmid/game/GameSpaceLists.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,68 @@ | ||
package xyz.nucleoid.plasmid.game; | ||
|
||
import net.minecraft.util.Identifier; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import java.util.List; | ||
import java.util.UUID; | ||
|
||
public final class GameSpaceLists { | ||
private static final List<GameSpaceList> REGISTRY = new ArrayList<>(); | ||
private static GameSpaceList composite = GameSpaceList.EMPTY; | ||
|
||
private GameSpaceLists() { | ||
} | ||
|
||
public static GameSpaceList composite() { | ||
return composite; | ||
} | ||
|
||
public static void register(GameSpaceList list) { | ||
REGISTRY.add(list); | ||
composite = buildCompositeList(List.copyOf(REGISTRY)); | ||
} | ||
|
||
public static void unregister(GameSpaceList list) { | ||
REGISTRY.remove(list); | ||
composite = buildCompositeList(List.copyOf(REGISTRY)); | ||
} | ||
|
||
private static GameSpaceList buildCompositeList(List<GameSpaceList> registry) { | ||
return new GameSpaceList() { | ||
@Override | ||
public Collection<? extends ListedGameSpace> getOpenGameSpaces() { | ||
var result = new ArrayList<ListedGameSpace>(); | ||
for (var list : registry) { | ||
result.addAll(list.getOpenGameSpaces()); | ||
} | ||
return result; | ||
} | ||
|
||
@Override | ||
@Nullable | ||
public ListedGameSpace byId(UUID id) { | ||
for (var list : registry) { | ||
var gameSpace = list.byId(id); | ||
if (gameSpace != null) { | ||
return gameSpace; | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
@Override | ||
@Nullable | ||
public ListedGameSpace byUserId(Identifier userId) { | ||
for (var list : registry) { | ||
var gameSpace = list.byUserId(userId); | ||
if (gameSpace != null) { | ||
return gameSpace; | ||
} | ||
} | ||
return null; | ||
} | ||
}; | ||
} | ||
} |
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