-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add an event for building waiting lobby user interface layouts
- Loading branch information
Showing
16 changed files
with
603 additions
and
48 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
31 changes: 31 additions & 0 deletions
31
src/main/java/xyz/nucleoid/plasmid/game/common/ui/WaitingLobbyUi.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,31 @@ | ||
package xyz.nucleoid.plasmid.game.common.ui; | ||
|
||
import eu.pb4.sgui.api.gui.HotbarGui; | ||
import net.minecraft.server.network.ServerPlayerEntity; | ||
import xyz.nucleoid.plasmid.game.GameSpace; | ||
import xyz.nucleoid.plasmid.game.event.GameWaitingLobbyEvents; | ||
import xyz.nucleoid.stimuli.Stimuli; | ||
|
||
public class WaitingLobbyUi extends HotbarGui { | ||
public WaitingLobbyUi(ServerPlayerEntity player, GameSpace gameSpace) { | ||
super(player); | ||
|
||
var layout = new WaitingLobbyUiLayout(); | ||
|
||
try (var invokers = Stimuli.select().forEntity(player)) { | ||
invokers.get(GameWaitingLobbyEvents.BUILD_UI_LAYOUT).onBuildUiLayout(layout, player); | ||
} | ||
|
||
int index = 0; | ||
|
||
for (var element : layout.build()) { | ||
this.setSlot(index, element); | ||
index += 1; | ||
} | ||
} | ||
|
||
@Override | ||
public boolean canPlayerClose() { | ||
return false; | ||
} | ||
} |
100 changes: 100 additions & 0 deletions
100
src/main/java/xyz/nucleoid/plasmid/game/common/ui/WaitingLobbyUiLayout.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,100 @@ | ||
package xyz.nucleoid.plasmid.game.common.ui; | ||
|
||
import eu.pb4.sgui.api.elements.GuiElement; | ||
import eu.pb4.sgui.api.elements.GuiElementInterface; | ||
import eu.pb4.sgui.api.gui.HotbarGui; | ||
import xyz.nucleoid.plasmid.game.common.ui.element.WaitingLobbyUiElement; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.Comparator; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Objects; | ||
|
||
public final class WaitingLobbyUiLayout { | ||
private static final int SIZE = 9; | ||
|
||
private final List<WaitingLobbyUiElement> leftElements = new ArrayList<>(); | ||
private final List<WaitingLobbyUiElement> rightElements = new ArrayList<>(); | ||
|
||
public void addLeft(WaitingLobbyUiElement element) { | ||
this.add(element, this.leftElements); | ||
} | ||
|
||
public void addRight(WaitingLobbyUiElement element) { | ||
this.add(element, this.rightElements); | ||
} | ||
|
||
private void add(WaitingLobbyUiElement element, List<WaitingLobbyUiElement> elements) { | ||
Objects.requireNonNull(element); | ||
|
||
if (this.leftElements.contains(element) || this.rightElements.contains(element)) { | ||
throw new IllegalArgumentException("Element " + element + " has already been added to the layout"); | ||
} else if (this.leftElements.size() + this.rightElements.size() >= SIZE) { | ||
throw new IllegalStateException("Cannot have more than " + SIZE + " elements in the layout"); | ||
} | ||
|
||
elements.add(element); | ||
} | ||
|
||
public GuiElementInterface[] build() { | ||
var elements = new GuiElementInterface[SIZE]; | ||
Arrays.fill(elements, GuiElement.EMPTY); | ||
|
||
if (this.leftElements.isEmpty() && this.rightElements.isEmpty()) { | ||
return elements; | ||
} | ||
|
||
var elementsToEntries = new HashMap<WaitingLobbyUiElement, WaitingLobbyUiLayoutEntry>(this.leftElements.size() + this.rightElements.size()); | ||
|
||
for (var element : this.leftElements) { | ||
elementsToEntries.put(element, new WaitingLobbyUiLayoutEntry(element)); | ||
} | ||
|
||
for (var element : this.rightElements) { | ||
elementsToEntries.put(element, new WaitingLobbyUiLayoutEntry(element)); | ||
} | ||
|
||
var entries = new ArrayList<>(elementsToEntries.values()); | ||
entries.sort(Comparator.comparingInt(WaitingLobbyUiLayoutEntry::size)); | ||
|
||
int shrinkIndex = 0; | ||
|
||
while (WaitingLobbyUiLayoutEntry.getTotalSize(entries) > SIZE) { | ||
var entry = entries.get(shrinkIndex); | ||
entry.shrink(); | ||
|
||
shrinkIndex += 1; | ||
} | ||
|
||
int index = 0; | ||
|
||
for (var element : this.leftElements) { | ||
var entry = elementsToEntries.get(element); | ||
|
||
for (var guiElement : entry.getGuiElements()) { | ||
elements[index] = guiElement; | ||
index += 1; | ||
} | ||
} | ||
|
||
index = SIZE - 1; | ||
|
||
for (var element : this.rightElements) { | ||
var entry = elementsToEntries.get(element); | ||
|
||
for (var guiElement : entry.getGuiElements()) { | ||
elements[index] = guiElement; | ||
index -= 1; | ||
} | ||
} | ||
|
||
return elements; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "WaitingLobbyUiLayout{leftElements=" + this.leftElements + ", rightElements=" + this.rightElements + "}"; | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
src/main/java/xyz/nucleoid/plasmid/game/common/ui/WaitingLobbyUiLayoutEntry.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,46 @@ | ||
package xyz.nucleoid.plasmid.game.common.ui; | ||
|
||
import eu.pb4.sgui.api.elements.GuiElementInterface; | ||
import xyz.nucleoid.plasmid.game.common.ui.element.WaitingLobbyUiElement; | ||
|
||
import java.util.List; | ||
import java.util.SequencedCollection; | ||
|
||
class WaitingLobbyUiLayoutEntry { | ||
private final WaitingLobbyUiElement element; | ||
|
||
private SequencedCollection<GuiElementInterface> guiElements; | ||
|
||
protected WaitingLobbyUiLayoutEntry(WaitingLobbyUiElement element) { | ||
this.element = element; | ||
|
||
this.guiElements = element.createExtendedElements(); | ||
} | ||
|
||
public SequencedCollection<GuiElementInterface> getGuiElements() { | ||
return this.guiElements; | ||
} | ||
|
||
public void shrink() { | ||
this.guiElements = List.of(this.element.createMainElement()); | ||
} | ||
|
||
public int size() { | ||
return this.guiElements.size(); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "WaitingLobbyUiLayoutEntry{element=" + element + ", guiElements=" + guiElements + "}"; | ||
} | ||
|
||
protected static int getTotalSize(Iterable<WaitingLobbyUiLayoutEntry> entries) { | ||
int size = 0; | ||
|
||
for (var entry : entries) { | ||
size += entry.size(); | ||
} | ||
|
||
return size; | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
.../java/xyz/nucleoid/plasmid/game/common/ui/element/TeamSelectionWaitingLobbyUiElement.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,57 @@ | ||
package xyz.nucleoid.plasmid.game.common.ui.element; | ||
|
||
import eu.pb4.sgui.api.elements.GuiElementBuilder; | ||
import eu.pb4.sgui.api.elements.GuiElementInterface; | ||
import net.minecraft.item.Items; | ||
import net.minecraft.text.Text; | ||
import net.minecraft.util.Formatting; | ||
import xyz.nucleoid.plasmid.game.common.team.GameTeamKey; | ||
import xyz.nucleoid.plasmid.game.common.team.GameTeamList; | ||
import xyz.nucleoid.plasmid.util.ColoredBlocks; | ||
|
||
import java.util.ArrayList; | ||
import java.util.SequencedCollection; | ||
import java.util.function.Consumer; | ||
|
||
public class TeamSelectionWaitingLobbyUiElement implements WaitingLobbyUiElement { | ||
private final GameTeamList teams; | ||
private final Consumer<GameTeamKey> selectCallback; | ||
|
||
public TeamSelectionWaitingLobbyUiElement(GameTeamList teams, Consumer<GameTeamKey> selectCallback) { | ||
this.teams = teams; | ||
this.selectCallback = selectCallback; | ||
} | ||
|
||
@Override | ||
public GuiElementInterface createMainElement() { | ||
return new GuiElementBuilder(Items.PAPER) | ||
.setItemName(Text.translatable("text.plasmid.team_selection.teams")) | ||
.build(); | ||
} | ||
|
||
@Override | ||
public SequencedCollection<GuiElementInterface> createExtendedElements() { | ||
var extendedElements = new ArrayList<GuiElementInterface>(this.teams.list().size()); | ||
|
||
for (var team : this.teams) { | ||
var key = team.key(); | ||
var config = team.config(); | ||
|
||
var name = Text.translatable("text.plasmid.team_selection.request_team", config.name()) | ||
.formatted(Formatting.BOLD, config.chatFormatting()); | ||
|
||
var element = new GuiElementBuilder(ColoredBlocks.wool(config.blockDyeColor()).asItem()) | ||
.setItemName(name) | ||
.setCallback((index, type, action) -> { | ||
if (type.isRight) { | ||
this.selectCallback.accept(key); | ||
} | ||
}) | ||
.build(); | ||
|
||
extendedElements.add(element); | ||
} | ||
|
||
return extendedElements; | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
src/main/java/xyz/nucleoid/plasmid/game/common/ui/element/WaitingLobbyUiElement.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,11 @@ | ||
package xyz.nucleoid.plasmid.game.common.ui.element; | ||
|
||
import java.util.SequencedCollection; | ||
|
||
import eu.pb4.sgui.api.elements.GuiElementInterface; | ||
|
||
public interface WaitingLobbyUiElement { | ||
GuiElementInterface createMainElement(); | ||
|
||
SequencedCollection<GuiElementInterface> createExtendedElements(); | ||
} |
Oops, something went wrong.