mirror of
https://github.com/GeyserMC/Geyser.git
synced 2024-08-14 23:57:35 +00:00
address review
This commit is contained in:
parent
2e161f05bf
commit
9187c78790
4 changed files with 65 additions and 84 deletions
|
|
@ -151,32 +151,18 @@ public interface CameraData {
|
|||
*
|
||||
* @param element the {@link GuiElement} to hide
|
||||
*/
|
||||
void hideElement(@NonNull GuiElement element);
|
||||
|
||||
/**
|
||||
* Hides a set of {@link GuiElement}'s on the client's side.
|
||||
*
|
||||
* @param elements the {@link Set<GuiElement>} to hide
|
||||
*/
|
||||
void hideElements(@NonNull Set<GuiElement> elements);
|
||||
void hideElement(@NonNull GuiElement... element);
|
||||
|
||||
/**
|
||||
* Resets a {@link GuiElement} on the client's side.
|
||||
* This makes the client decide on its own - e.g. based on client settings -
|
||||
* whether to show or hide the gui element.
|
||||
* <p>
|
||||
* If no elements are specified, this will reset all currently hidden elements
|
||||
*
|
||||
* @param element the {@link GuiElement} to reset
|
||||
*/
|
||||
void resetElement(@NonNull GuiElement element);
|
||||
|
||||
/**
|
||||
* Resets a set of {@link GuiElement} on the client's side.
|
||||
* This makes the client decide on its own - e.g. based on client settings -
|
||||
* whether to show or hide the gui elements.
|
||||
*
|
||||
* @param elements the {@link Set<GuiElement>} to reset
|
||||
*/
|
||||
void resetElements(@NonNull Set<GuiElement> elements);
|
||||
void resetElement(@NonNull GuiElement... element);
|
||||
|
||||
/**
|
||||
* Determines whether a {@link GuiElement} is currently hidden.
|
||||
|
|
|
|||
|
|
@ -25,6 +25,11 @@
|
|||
|
||||
package org.geysermc.geyser.api.bedrock.camera;
|
||||
|
||||
/**
|
||||
* Represent GUI elements on the players HUD display.
|
||||
* These can be hidden using {@link CameraData#hideElement(GuiElement...)},
|
||||
* and one can reset their visibility using {@link CameraData#resetElement(GuiElement...)}.
|
||||
*/
|
||||
public enum GuiElement {
|
||||
PAPER_DOLL,
|
||||
ARMOR,
|
||||
|
|
|
|||
|
|
@ -49,6 +49,7 @@ import org.geysermc.geyser.api.bedrock.camera.CameraPosition;
|
|||
import org.geysermc.geyser.api.bedrock.camera.CameraShake;
|
||||
import org.geysermc.geyser.api.bedrock.camera.GuiElement;
|
||||
import org.geysermc.geyser.session.GeyserSession;
|
||||
import org.geysermc.mcprotocollib.protocol.data.game.entity.player.GameMode;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.HashSet;
|
||||
|
|
@ -75,6 +76,19 @@ public class GeyserCameraData implements CameraData {
|
|||
*/
|
||||
private final Set<GuiElement> hiddenHudElements = new HashSet<>();
|
||||
|
||||
/**
|
||||
* An array of elements to hide when the player is in spectator mode.
|
||||
* Helps with tidying up the GUI; Java-style.
|
||||
*/
|
||||
private static final GuiElement[] SPECTATOR_HIDDEN_ELEMENTS = {
|
||||
GuiElement.AIR_BUBBLES_BAR,
|
||||
GuiElement.ARMOR,
|
||||
GuiElement.HEALTH,
|
||||
GuiElement.FOOD_BAR,
|
||||
GuiElement.PROGRESS_BAR,
|
||||
GuiElement.TOOL_TIPS
|
||||
};
|
||||
|
||||
public GeyserCameraData(GeyserSession session) {
|
||||
this.session = session;
|
||||
}
|
||||
|
|
@ -243,53 +257,35 @@ public class GeyserCameraData implements CameraData {
|
|||
}
|
||||
|
||||
@Override
|
||||
public void hideElement(@NonNull GuiElement element) {
|
||||
Objects.requireNonNull(element);
|
||||
this.hiddenHudElements.add(element);
|
||||
|
||||
SetHudPacket packet = new SetHudPacket();
|
||||
packet.setVisibility(HudVisibility.HIDE);
|
||||
packet.getElements().add(HudElement.values()[element.ordinal()]);
|
||||
session.sendUpstreamPacket(packet);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void hideElements(@NonNull Set<GuiElement> elements) {
|
||||
Objects.requireNonNull(elements);
|
||||
this.hiddenHudElements.removeAll(elements);
|
||||
|
||||
public void hideElement(GuiElement... elements) {
|
||||
SetHudPacket packet = new SetHudPacket();
|
||||
packet.setVisibility(HudVisibility.HIDE);
|
||||
Set<HudElement> elementSet = packet.getElements();
|
||||
elements.forEach((element) -> elementSet.add(HudElement.values()[element.ordinal()]));
|
||||
session.sendUpstreamPacket(packet);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void resetElement(@NonNull GuiElement element) {
|
||||
Objects.requireNonNull(element);
|
||||
this.hiddenHudElements.remove(element);
|
||||
|
||||
SetHudPacket packet = new SetHudPacket();
|
||||
packet.setVisibility(HudVisibility.RESET);
|
||||
packet.getElements().add(HudElement.values()[element.ordinal()]);
|
||||
session.sendUpstreamPacket(packet);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void resetElements(@NonNull Set<GuiElement> elements) {
|
||||
Objects.requireNonNull(elements);
|
||||
this.hiddenHudElements.removeAll(elements);
|
||||
|
||||
// This is unfortunate, but resetting multiple elements doesn't work otherwise
|
||||
if (!hiddenHudElements.isEmpty()) {
|
||||
elements.forEach(this::resetElement);
|
||||
return;
|
||||
for (GuiElement element : elements) {
|
||||
this.hiddenHudElements.add(element);
|
||||
elementSet.add(HudElement.values()[element.ordinal()]);
|
||||
}
|
||||
|
||||
session.sendUpstreamPacket(packet);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void resetElement(GuiElement... elements) {
|
||||
SetHudPacket packet = new SetHudPacket();
|
||||
packet.setVisibility(HudVisibility.RESET);
|
||||
packet.getElements().addAll(Set.of(HudElement.values()));
|
||||
Set<HudElement> elementSet = packet.getElements();
|
||||
|
||||
if (elements.length != 0) {
|
||||
for (GuiElement element : elements) {
|
||||
this.hiddenHudElements.remove(element);
|
||||
elementSet.add(HudElement.values()[element.ordinal()]);
|
||||
}
|
||||
} else {
|
||||
this.hiddenHudElements.clear();
|
||||
elementSet.addAll(Set.of(HudElement.values()));
|
||||
}
|
||||
|
||||
session.sendUpstreamPacket(packet);
|
||||
}
|
||||
|
||||
|
|
@ -301,6 +297,24 @@ public class GeyserCameraData implements CameraData {
|
|||
|
||||
@Override
|
||||
public @NonNull Set<GuiElement> hiddenElements() {
|
||||
return Set.copyOf(hiddenHudElements);
|
||||
return Set.copyOf(this.hiddenHudElements);
|
||||
}
|
||||
|
||||
/**
|
||||
* Deals with hiding hud elements while in spectator.
|
||||
*
|
||||
* @param currentlySpectator whether the player is currently in spectator mode
|
||||
* @param newGameMode the new GameMode to switch to
|
||||
*/
|
||||
public void handleGameModeChange(boolean currentlySpectator, GameMode newGameMode) {
|
||||
if (newGameMode == GameMode.SPECTATOR) {
|
||||
if (!currentlySpectator) {
|
||||
hideElement(SPECTATOR_HIDDEN_ELEMENTS);
|
||||
}
|
||||
} else {
|
||||
if (currentlySpectator) {
|
||||
resetElement(SPECTATOR_HIDDEN_ELEMENTS);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -28,7 +28,6 @@ package org.geysermc.geyser.session;
|
|||
import com.github.steveice10.mc.auth.data.GameProfile;
|
||||
import com.github.steveice10.mc.auth.exception.request.RequestException;
|
||||
import com.github.steveice10.mc.auth.service.MsaAuthenticationService;
|
||||
import org.geysermc.geyser.api.bedrock.camera.GuiElement;
|
||||
import org.geysermc.mcprotocollib.protocol.MinecraftConstants;
|
||||
import org.geysermc.mcprotocollib.protocol.MinecraftProtocol;
|
||||
import org.geysermc.mcprotocollib.protocol.data.ProtocolState;
|
||||
|
|
@ -581,19 +580,6 @@ public class GeyserSession implements GeyserConnection, GeyserCommandSource {
|
|||
|
||||
private MinecraftProtocol protocol;
|
||||
|
||||
/**
|
||||
* A set of elements to hide when the player is in spectator mode.
|
||||
* Helps with tidying up the GUI; Java-style.
|
||||
*/
|
||||
private static final Set<GuiElement> SPECTATOR_HUD = Set.of(
|
||||
GuiElement.AIR_BUBBLES_BAR,
|
||||
GuiElement.ARMOR,
|
||||
GuiElement.HEALTH,
|
||||
GuiElement.FOOD_BAR,
|
||||
GuiElement.PROGRESS_BAR,
|
||||
GuiElement.TOOL_TIPS
|
||||
);
|
||||
|
||||
public GeyserSession(GeyserImpl geyser, BedrockServerSession bedrockServerSession, EventLoop eventLoop) {
|
||||
this.geyser = geyser;
|
||||
this.upstream = new UpstreamSession(bedrockServerSession);
|
||||
|
|
@ -1335,19 +1321,9 @@ public class GeyserSession implements GeyserConnection, GeyserCommandSource {
|
|||
}
|
||||
|
||||
public void setGameMode(GameMode newGamemode) {
|
||||
boolean currentlySpectator = gameMode == GameMode.SPECTATOR;
|
||||
boolean currentlySpectator = this.gameMode == GameMode.SPECTATOR;
|
||||
this.gameMode = newGamemode;
|
||||
|
||||
// Hide/Unhide GUI elements as needed
|
||||
if (newGamemode == GameMode.SPECTATOR) {
|
||||
if (!currentlySpectator) {
|
||||
this.cameraData.hideElements(SPECTATOR_HUD);
|
||||
}
|
||||
} else {
|
||||
if (currentlySpectator) {
|
||||
this.cameraData.resetElements(SPECTATOR_HUD);
|
||||
}
|
||||
}
|
||||
this.cameraData.handleGameModeChange(currentlySpectator, newGamemode);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue