Skip to content

Commit

Permalink
Add individual permissions for each gamemode (#329)
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasmny authored Jan 22, 2025
1 parent 3895c3d commit 423e09e
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command cmd, @N
}

case 1: {
if (!player.hasPermission("buildsystem.build.others")) {
if (!player.hasPermission("buildsystem.build.other")) {
plugin.sendPermissionMessage(player);
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,37 +46,35 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command cmd, @N
}

Player player = (Player) sender;
if (!player.hasPermission("buildsystem.gamemode")) {
plugin.sendPermissionMessage(player);
if (args.length == 0) {
sendUsageMessage(player);
return true;
}

if (args.length != 0) {
switch (args[0].toLowerCase(Locale.ROOT)) {
case "survival":
case "s":
case "0":
setGamemode(player, args, GameMode.SURVIVAL, Messages.getString("gamemode_survival", player));
break;
case "creative":
case "c":
case "1":
setGamemode(player, args, GameMode.CREATIVE, Messages.getString("gamemode_creative", player));
break;
case "adventure":
case "a":
case "2":
setGamemode(player, args, GameMode.ADVENTURE, Messages.getString("gamemode_adventure", player));
break;
case "spectator":
case "sp":
case "3":
setGamemode(player, args, GameMode.SPECTATOR, Messages.getString("gamemode_spectator", player));
break;
default:
sendUsageMessage(player);
break;
}
switch (args[0].toLowerCase(Locale.ROOT)) {
case "survival":
case "s":
case "0":
setGamemode(player, args, GameMode.SURVIVAL, Messages.getString("gamemode_survival", player));
break;
case "creative":
case "c":
case "1":
setGamemode(player, args, GameMode.CREATIVE, Messages.getString("gamemode_creative", player));
break;
case "adventure":
case "a":
case "2":
setGamemode(player, args, GameMode.ADVENTURE, Messages.getString("gamemode_adventure", player));
break;
case "spectator":
case "sp":
case "3":
setGamemode(player, args, GameMode.SPECTATOR, Messages.getString("gamemode_spectator", player));
break;
default:
sendUsageMessage(player);
break;
}

return true;
Expand All @@ -92,6 +90,7 @@ private void setGamemode(Player player, String[] args, GameMode gameMode, String
break;
default:
this.sendUsageMessage(player);
break;
}
}

Expand All @@ -100,7 +99,7 @@ private void sendUsageMessage(Player player) {
}

private void setPlayerGamemode(Player player, GameMode gameMode, String gameModeName) {
if (!player.hasPermission("buildsystem.gamemode")) {
if (!player.hasPermission(String.format("buildsystem.gamemode.%s", gameMode.name().toLowerCase(Locale.ROOT)))) {
plugin.sendPermissionMessage(player);
return;
}
Expand All @@ -110,7 +109,7 @@ private void setPlayerGamemode(Player player, GameMode gameMode, String gameMode
}

private void setTargetGamemode(Player player, String[] args, GameMode gameMode, String gameModeName) {
if (!player.hasPermission("buildsystem.gamemode.others")) {
if (!player.hasPermission(String.format("buildsystem.gamemode.%s.other", gameMode.name().toLowerCase(Locale.ROOT)))) {
plugin.sendPermissionMessage(player);
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public List<String> onTabComplete(@NotNull CommandSender sender, @NotNull Comman
}

if (args.length == 1) {
if (!player.hasPermission("buildsystem.build.others")) {
if (!player.hasPermission("buildsystem.build.other")) {
Bukkit.getOnlinePlayers().forEach(pl -> addArgument(args[0], pl.getName(), arrayList));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import de.eintosti.buildsystem.BuildSystem;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;
import org.bukkit.Bukkit;
Expand All @@ -38,40 +39,45 @@ public GamemodeTabComplete(BuildSystem plugin) {
@Override
public List<String> onTabComplete(@NotNull CommandSender sender, @NotNull Command cmd, @NotNull String label, String[] args) {
ArrayList<String> arrayList = new ArrayList<>();

if (!(sender instanceof Player)) {
return arrayList;
}
Player player = (Player) sender;

if (!player.hasPermission("buildsystem.gamemode")) {
return arrayList;
}

Player player = (Player) sender;
if (args.length == 1) {
for (GameMode gameMode : GameMode.values()) {
addArgument(args[0], gameMode.name().toLowerCase(Locale.ROOT), arrayList);
}
Arrays.stream(GameMode.values())
.map(gameMode -> gameMode.name().toLowerCase(Locale.ROOT))
.filter(gameModeName -> player.hasPermission(String.format("buildsystem.gamemode.%s", gameModeName)))
.forEach(gameModeName -> addArgument(args[0], gameModeName, arrayList));
} else if (args.length == 2) {
if (!player.hasPermission("buildsystem.gamemode.others")) {
return arrayList;
}

String gameModeName;
switch (args[0].toLowerCase(Locale.ROOT)) {
case "survival":
case "s":
case "0":
gameModeName = GameMode.SURVIVAL.name().toLowerCase(Locale.ROOT);
break;
case "creative":
case "c":
case "1":
gameModeName = GameMode.CREATIVE.name().toLowerCase(Locale.ROOT);
break;
case "adventure":
case "a":
case "2":
gameModeName = GameMode.ADVENTURE.name().toLowerCase(Locale.ROOT);
break;
case "spectator":
case "sp":
case "3":
Bukkit.getOnlinePlayers().forEach(pl -> addArgument(args[1], pl.getName(), arrayList));
gameModeName = GameMode.SPECTATOR.name().toLowerCase(Locale.ROOT);
break;
default:
return arrayList;
}

if (player.hasPermission(String.format("buildsystem.gamemode.%s.other", gameModeName))) {
Bukkit.getOnlinePlayers().forEach(pl -> addArgument(args[1], pl.getName(), arrayList));
}
}

Expand Down
16 changes: 16 additions & 0 deletions buildsystem-core/src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,22 @@ permissions:
- buildsystem.setstatus.notstarted
default: op
description: Permission for specific status states
buildsystem.gamemode:
children:
- buildsystem.gamemode.survival
- buildsystem.gamemode.creative
- buildsystem.gamemode.adventure
- buildsystem.gamemode.spectator
default: op
description: Permission for changing own gamemode
buildsystem.gamemode.other:
children:
- buildsystem.gamemode.survival.other
- buildsystem.gamemode.creative.other
- buildsystem.gamemode.adventure.other
- buildsystem.gamemode.spectator.other
default: op
description: Permission for changing other player's gamemode
buildsystem.physics.message:
description: Receive the message that physics are disabled in a world.
default: true
Expand Down

0 comments on commit 423e09e

Please sign in to comment.