Skip to content

Commit

Permalink
Add new config option for game mode
Browse files Browse the repository at this point in the history
  • Loading branch information
mattysweeps committed Dec 7, 2023
1 parent 0c87c0a commit 5e5f914
Show file tree
Hide file tree
Showing 9 changed files with 147 additions and 7 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
pluginGroup=io.github.m0pt0pmatt.survivalgames
pluginId=survivalgames
pluginVersion=1.2.1
pluginVersion=1.3.0
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
@Plugin(
id = "survival-games",
name = "Survival Games",
version = "1.2.1",
version = "1.3.0",
description = "Survival Games for Sponge.",
url = "https://github.com/mattysweeps/SurvivalGames"
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,6 @@ public class CommandKeys {
public static final Text INTERVAL_NAME = Text.of("interval-name");
public static final Text SECONDS_PER_INTERVAL = Text.of("seconds-per-interval");
public static final Text CENTER_VECTOR = Text.of("center-vector");

public static final Text GAME_MODE = Text.of("game-mode");
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public class PrintCommand extends ParentCommand {
.add(PrintSurvivalGameStateCommand.getInstance())
.add(PrintWorldNameCommand.getInstance())
.add(PrintEventIntervalsCommand.getInstance())
.add(PrintGameModeCommand.getInstance())
.build()
);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* This file is part of SurvivalGames, licensed under the MIT License (MIT).
*
* Copyright (c) Matthew Broomfield <[email protected]>
* Copyright (c) contributors
*
* 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.
*/

package io.github.m0pt0pmatt.survivalgames.command.executor.print;

import io.github.m0pt0pmatt.survivalgames.command.element.SurvivalGameCommandElement;
import io.github.m0pt0pmatt.survivalgames.command.executor.SurvivalGamesCommand;
import org.spongepowered.api.text.Text;

import java.util.Optional;

class PrintGameModeCommand extends AbstractPrintCommand {

private static final SurvivalGamesCommand INSTANCE = new PrintGameModeCommand();

private PrintGameModeCommand() {
super(
"game-mode",
SurvivalGameCommandElement.getInstance(),
survivalGame -> Optional.of(Text.of(survivalGame.getConfig().getGameMode().getName())));
}

static SurvivalGamesCommand getInstance() {
return INSTANCE;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public class SetCommand extends ParentCommand {
.add(SetExitWorldNameCommand.getInstance())
.add(SetPlayerLimitCommand.getInstance())
.add(SetWorldNameCommand.getInstance())
.add(SetGameModeCommand.getInstance())
.build());
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* This file is part of SurvivalGames, licensed under the MIT License (MIT).
*
* Copyright (c) Matthew Broomfield <[email protected]>
* Copyright (c) contributors
*
* 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.
*/

package io.github.m0pt0pmatt.survivalgames.command.executor.set;

import io.github.m0pt0pmatt.survivalgames.command.CommandKeys;
import io.github.m0pt0pmatt.survivalgames.command.element.SurvivalGameCommandElement;
import io.github.m0pt0pmatt.survivalgames.command.executor.LeafCommand;
import io.github.m0pt0pmatt.survivalgames.command.executor.SurvivalGamesCommand;
import io.github.m0pt0pmatt.survivalgames.game.SurvivalGame;
import org.spongepowered.api.command.CommandException;
import org.spongepowered.api.command.CommandResult;
import org.spongepowered.api.command.CommandSource;
import org.spongepowered.api.command.args.CommandContext;
import org.spongepowered.api.command.args.GenericArguments;
import org.spongepowered.api.entity.living.player.gamemode.GameMode;

import javax.annotation.Nonnull;

import static io.github.m0pt0pmatt.survivalgames.Util.getOrThrow;
import static io.github.m0pt0pmatt.survivalgames.Util.sendSuccess;

class SetGameModeCommand extends LeafCommand {

private static final SurvivalGamesCommand INSTANCE = new SetGameModeCommand();

private SetGameModeCommand() {
super(
SetCommand.getInstance(),
"game-mode",
GenericArguments.seq(
SurvivalGameCommandElement.getInstance(),
GenericArguments.catalogedElement(CommandKeys.GAME_MODE, GameMode.class)));
}

@Nonnull
@Override
public CommandResult executeCommand(@Nonnull CommandSource src, @Nonnull CommandContext args)
throws CommandException {
SurvivalGame survivalGame = (SurvivalGame) getOrThrow(args, CommandKeys.SURVIVAL_GAME);
GameMode gameMode = (GameMode) getOrThrow(args, CommandKeys.GAME_MODE);

survivalGame.getConfig().setGameMode(gameMode);

sendSuccess(src, "Set game mode", gameMode);
return CommandResult.success();
}

static SurvivalGamesCommand getInstance() {
return INSTANCE;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
import ninja.leaping.configurate.objectmapping.Setting;
import ninja.leaping.configurate.objectmapping.serialize.ConfigSerializable;
import org.spongepowered.api.block.BlockSnapshot;
import org.spongepowered.api.entity.living.player.gamemode.GameMode;
import org.spongepowered.api.entity.living.player.gamemode.GameModes;

import java.util.ArrayList;
import java.util.List;
Expand All @@ -50,6 +52,8 @@ public class GameConfig {
private static final int DEFAULT_PLAYER_LIMIT = 4;
private static final int DEFAULT_COUNTDOWN_SECONDS = 10;

private static final GameMode DEFAULT_GAME_MODE = GameModes.ADVENTURE;

public static final ObjectMapper<GameConfig> OBJECT_MAPPER;

static {
Expand Down Expand Up @@ -111,13 +115,17 @@ public class GameConfig {
@Setting(value = "event-intervals")
private final Map<String, Integer> eventIntervals = Maps.newHashMap();

@Setting(value = "game-mode")
private GameMode gameMode;

// @Setting(value = "blocks")
// Do not save blocks yet: this needs to be replaced with a more scalable solution.
private List<BlockSnapshot> blocks = Lists.newArrayList();

public GameConfig() {
setPlayerLimit(DEFAULT_PLAYER_LIMIT);
setCountdownSeconds(DEFAULT_COUNTDOWN_SECONDS);
setGameMode(DEFAULT_GAME_MODE);
spawnPoints = new ArrayList<>();
}

Expand Down Expand Up @@ -222,4 +230,12 @@ public List<BlockSnapshot> getBlocks() {
public void setBlocks(List<BlockSnapshot> blocks) {
this.blocks = blocks;
}

public GameMode getGameMode() {
return gameMode;
}

public void setGameMode(GameMode gameMode) {
this.gameMode = gameMode;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@
import org.spongepowered.api.command.CommandException;
import org.spongepowered.api.data.key.Keys;
import org.spongepowered.api.entity.living.player.Player;
import org.spongepowered.api.entity.living.player.gamemode.GameModes;
import org.spongepowered.api.util.TextMessageException;
import org.spongepowered.api.world.World;

import java.util.ArrayList;
Expand All @@ -59,16 +57,16 @@ public void execute(SurvivalGame survivalGame, Player player) throws CommandExce
if (!spawnPoints.isEmpty()) {
Vector3i spawnPoint = spawnPoints.remove(0).toInt();

spawnPlayer(player, world,
spawnPlayer(survivalGame, player, world,
new Vector3d(spawnPoint.getX(), spawnPoint.getY(), spawnPoint.getZ()),
new Vector3d(centerVector.getX(), centerVector.getY(), centerVector.getZ()));
}
}

private static void spawnPlayer(Player player, World world, Vector3d spawnPoint, Vector3d centerVector) {
private static void spawnPlayer(SurvivalGame survivalGame, Player player, World world, Vector3d spawnPoint, Vector3d centerVector) {
player.setLocation(world.getLocation(spawnPoint).add(new Vector3d(0.5, 0, 0.5)));
player.lookAt(centerVector);
player.offer(Keys.GAME_MODE, GameModes.ADVENTURE);
player.offer(Keys.GAME_MODE, survivalGame.getConfig().getGameMode());
}

@Override
Expand Down

0 comments on commit 5e5f914

Please sign in to comment.