Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Allow client-side "game settings" menu gamemode/difficulty changes #4062

Merged
merged 12 commits into from
Sep 8, 2023
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

package org.geysermc.geyser.platform.spigot.world.manager;

import com.github.steveice10.mc.protocol.data.game.entity.player.GameMode;
import com.github.steveice10.mc.protocol.data.game.level.block.BlockEntityInfo;
import com.github.steveice10.opennbt.tag.builtin.CompoundTag;
import org.bukkit.Bukkit;
Expand Down Expand Up @@ -171,6 +172,11 @@ public int getGameRuleInt(GeyserSession session, GameRule gameRule) {
return gameRule.getDefaultIntValue();
}

@Override
public GameMode getDefaultGameMode(GeyserSession session) {
return GameMode.byId(Bukkit.getDefaultGameMode().ordinal());
}

@Override
public boolean hasPermission(GeyserSession session, String permission) {
return Bukkit.getPlayer(session.getPlayerEntity().getUsername()).hasPermission(permission);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

package org.geysermc.geyser.level;

import com.github.steveice10.mc.protocol.data.game.entity.player.GameMode;
import com.github.steveice10.mc.protocol.data.game.level.block.BlockEntityInfo;
import com.github.steveice10.opennbt.tag.builtin.CompoundTag;
import it.unimi.dsi.fastutil.objects.Object2ObjectMap;
Expand Down Expand Up @@ -160,6 +161,10 @@ public int getGameRuleInt(GeyserSession session, GameRule gameRule) {
return gameRule.getDefaultIntValue();
}

public GameMode getDefaultGameMode(GeyserSession session) {
return GameMode.SURVIVAL;
}
onebeastchris marked this conversation as resolved.
Show resolved Hide resolved

@Override
public boolean hasPermission(GeyserSession session, String permission) {
return false;
Expand Down
18 changes: 18 additions & 0 deletions core/src/main/java/org/geysermc/geyser/level/WorldManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,24 @@ public void setPlayerGameMode(GeyserSession session, GameMode gameMode) {
session.sendCommand("gamemode " + gameMode.name().toLowerCase(Locale.ROOT));
}

/**
* Get the default game mode of the server
*
* @param session The session of the player requesting the default game mode
* @return The default game mode of the server, or Survival if unknown.
*/
public abstract GameMode getDefaultGameMode(GeyserSession session);

/**
* Change the default game mode of the server's session
*
* @param session The session of the player making the change
* @param gameMode The new default game mode
*/
public void setDefaultGameMode(GeyserSession session, GameMode gameMode) {
session.sendCommand("defaultgamemode " + gameMode.name().toLowerCase(Locale.ROOT));
}
onebeastchris marked this conversation as resolved.
Show resolved Hide resolved

/**
* Change the difficulty of the Java server
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Copyright (c) 2019-2023 GeyserMC. http://geysermc.org
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* @author GeyserMC
* @link https://github.com/GeyserMC/Geyser
*/

package org.geysermc.geyser.translator.protocol.bedrock.entity.player;

import com.github.steveice10.mc.protocol.data.game.entity.player.GameMode;
import org.cloudburstmc.protocol.bedrock.packet.SetDefaultGameTypePacket;
import org.geysermc.geyser.session.GeyserSession;
import org.geysermc.geyser.translator.protocol.PacketTranslator;
import org.geysermc.geyser.translator.protocol.Translator;

@Translator(packet = SetDefaultGameTypePacket.class)
public class BedrockSetDefaultGameTypeTranslator extends PacketTranslator<SetDefaultGameTypePacket> {

/**
* Sets the default game mode for the server via the Bedrock client's "world" menu (given sufficient permissions).
*/
@Override
public void translate(GeyserSession session, SetDefaultGameTypePacket packet) {
if (session.getOpPermissionLevel() >= 2 || session.hasPermission("geyser.settings.server")) {
session.getGeyser().getWorldManager().setDefaultGameMode(session, GameMode.byId(packet.getGamemode()));
}
// update the client
SetDefaultGameTypePacket defaultGameTypePacket = new SetDefaultGameTypePacket();
defaultGameTypePacket.setGamemode(packet.getGamemode());
onebeastchris marked this conversation as resolved.
Show resolved Hide resolved
session.sendUpstreamPacket(defaultGameTypePacket);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright (c) 2019-2023 GeyserMC. http://geysermc.org
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* @author GeyserMC
* @link https://github.com/GeyserMC/Geyser
*/

package org.geysermc.geyser.translator.protocol.bedrock.entity.player;

import com.github.steveice10.mc.protocol.data.game.setting.Difficulty;
import org.cloudburstmc.protocol.bedrock.packet.SetDifficultyPacket;
import org.geysermc.geyser.session.GeyserSession;
import org.geysermc.geyser.translator.protocol.PacketTranslator;
import org.geysermc.geyser.translator.protocol.Translator;

@Translator(packet = SetDifficultyPacket.class)
public class BedrockSetDifficultyTranslator extends PacketTranslator<SetDifficultyPacket> {

/**
* Sets the Java server's difficulty via the Bedrock client's "world" menu (given sufficient permissions).
*/
@Override
public void translate(GeyserSession session, SetDifficultyPacket packet) {
if (session.getOpPermissionLevel() >= 2 || session.hasPermission("geyser.settings.server")) {
if (packet.getDifficulty() != session.getWorldCache().getDifficulty().ordinal()) {
session.getGeyser().getWorldManager().setDifficulty(session, Difficulty.from(packet.getDifficulty()));
}
}
// inform the client
SetDifficultyPacket difficultyPacket = new SetDifficultyPacket();
onebeastchris marked this conversation as resolved.
Show resolved Hide resolved
difficultyPacket.setDifficulty(session.getWorldCache().getDifficulty().ordinal());
session.sendUpstreamPacket(difficultyPacket);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,23 +25,41 @@

package org.geysermc.geyser.translator.protocol.bedrock.entity.player;

import com.github.steveice10.mc.protocol.data.game.entity.player.GameMode;
import org.cloudburstmc.protocol.bedrock.packet.SetPlayerGameTypePacket;
import org.geysermc.geyser.session.GeyserSession;
import org.geysermc.geyser.translator.protocol.PacketTranslator;
import org.geysermc.geyser.translator.protocol.Translator;

/**
* In vanilla Bedrock, if you have operator status, this sets the player's gamemode without confirmation from the server.
* Since we have a custom server option to request the gamemode, we just reset the gamemode and ignore this.
* With operator status, the Gamemode change is sent to the Java server, if it is not present, the gamemode is not changed.
*/
@Translator(packet = SetPlayerGameTypePacket.class)
public class BedrockSetPlayerGameTypeTranslator extends PacketTranslator<SetPlayerGameTypePacket> {

/**
* Sets client game mode for the server via the Bedrock client's "world" menu (given sufficient permissions).
*/
@Override
public void translate(GeyserSession session, SetPlayerGameTypePacket packet) {
// no
SetPlayerGameTypePacket playerGameTypePacket = new SetPlayerGameTypePacket();
playerGameTypePacket.setGamemode(session.getGameMode().ordinal());
session.sendUpstreamPacket(playerGameTypePacket);
// yes, if you are OP
if (session.getOpPermissionLevel() >= 2 || session.hasPermission("geyser.settings.server")) {
if (packet.getGamemode() != session.getGameMode().ordinal()) {
// For some reason, spectator mode is... 6?
GameMode gameMode = switch (packet.getGamemode()) {
case 1 -> GameMode.CREATIVE;
case 2 -> GameMode.ADVENTURE;
case 5 -> session.getGeyser().getWorldManager().getDefaultGameMode(session);
onebeastchris marked this conversation as resolved.
Show resolved Hide resolved
case 6 -> GameMode.SPECTATOR;
default -> GameMode.SURVIVAL;
};
session.getGeyser().getWorldManager().setPlayerGameMode(session, gameMode);
}
} else {
SetPlayerGameTypePacket playerGameTypePacket = new SetPlayerGameTypePacket();
playerGameTypePacket.setGamemode(session.getGameMode().ordinal());
session.sendUpstreamPacket(playerGameTypePacket);
}
}
}