From dacdad0a6681355a9642e704edc88a52bcb77ddd Mon Sep 17 00:00:00 2001 From: creatorfromhell Date: Mon, 11 Dec 2023 22:00:35 -0500 Subject: [PATCH] Add access/deny permissions for worlds. --- .../lokka30/phantomworlds/PhantomWorlds.java | 2 + .../commands/phantomworlds/PWCommand.java | 2 +- .../resolvers/AliasWorldResolver.java | 5 +- .../resolvers/GameRulesResolver.java | 49 --------------- .../suggestion/AliasWorldSuggestion.java | 1 - .../phantomworlds/utils/WorldRule.java | 55 ---------------- .../player/PlayerChangeWorldListener.java | 2 +- .../player/PlayerTeleportListener.java | 62 +++++++++++++++++++ .../phantomworlds/managers/WorldManager.java | 10 ++- .../phantomworlds/managers/WorldRule.java | 56 ----------------- 10 files changed, 74 insertions(+), 170 deletions(-) delete mode 100644 src/main/java/me/lokka30/phantomworlds/commands/phantomworlds/parameters/resolvers/GameRulesResolver.java delete mode 100644 src/main/java/me/lokka30/phantomworlds/commands/phantomworlds/utils/WorldRule.java create mode 100644 src/main/java/me/lokka30/phantomworlds/listeners/player/PlayerTeleportListener.java delete mode 100644 src/main/java/me/lokka30/phantomworlds/managers/WorldRule.java diff --git a/src/main/java/me/lokka30/phantomworlds/PhantomWorlds.java b/src/main/java/me/lokka30/phantomworlds/PhantomWorlds.java index e59846a..9c4dde4 100644 --- a/src/main/java/me/lokka30/phantomworlds/PhantomWorlds.java +++ b/src/main/java/me/lokka30/phantomworlds/PhantomWorlds.java @@ -13,6 +13,7 @@ import me.lokka30.phantomworlds.listeners.player.PlayerDeathListener; import me.lokka30.phantomworlds.listeners.player.PlayerJoinListener; import me.lokka30.phantomworlds.listeners.player.PlayerPortalListener; +import me.lokka30.phantomworlds.listeners.player.PlayerTeleportListener; import me.lokka30.phantomworlds.listeners.world.WorldInitListener; import me.lokka30.phantomworlds.managers.FileManager; import me.lokka30.phantomworlds.managers.WorldManager; @@ -249,6 +250,7 @@ void registerListeners() { getServer().getPluginManager().registerEvents(new PlayerDeathListener(this), this); getServer().getPluginManager().registerEvents(new PlayerJoinListener(this), this); getServer().getPluginManager().registerEvents(new PlayerPortalListener(this), this); + getServer().getPluginManager().registerEvents(new PlayerTeleportListener(this), this); getServer().getPluginManager().registerEvents(new WorldInitListener(this), this); } diff --git a/src/main/java/me/lokka30/phantomworlds/commands/phantomworlds/PWCommand.java b/src/main/java/me/lokka30/phantomworlds/commands/phantomworlds/PWCommand.java index 93cfe30..b8e8929 100644 --- a/src/main/java/me/lokka30/phantomworlds/commands/phantomworlds/PWCommand.java +++ b/src/main/java/me/lokka30/phantomworlds/commands/phantomworlds/PWCommand.java @@ -86,7 +86,7 @@ public void backup(BukkitCommandActor actor, @Optional final World world) { @Subcommand({"create", "+", "new"}) @CommandPermission("phantomworlds.command.phantomworlds.create") @Description("command.phantomworlds.help.create") - public void create(BukkitCommandActor actor) { + public void create(BukkitCommandActor actor, final String name) { //todo: port create command } diff --git a/src/main/java/me/lokka30/phantomworlds/commands/phantomworlds/parameters/resolvers/AliasWorldResolver.java b/src/main/java/me/lokka30/phantomworlds/commands/phantomworlds/parameters/resolvers/AliasWorldResolver.java index 36565ac..8d02ca3 100644 --- a/src/main/java/me/lokka30/phantomworlds/commands/phantomworlds/parameters/resolvers/AliasWorldResolver.java +++ b/src/main/java/me/lokka30/phantomworlds/commands/phantomworlds/parameters/resolvers/AliasWorldResolver.java @@ -18,7 +18,6 @@ */ import me.lokka30.phantomworlds.PhantomWorlds; -import org.bukkit.Bukkit; import org.bukkit.World; import org.jetbrains.annotations.NotNull; import revxrsal.commands.process.ValueResolver; @@ -35,8 +34,6 @@ public class AliasWorldResolver implements ValueResolver { public World resolve(@NotNull ValueResolverContext context) throws Throwable { final String value = context.arguments().pop(); - final String name = PhantomWorlds.worldManager().aliases.getOrDefault(value, value); - - return Bukkit.getWorld(name); + return PhantomWorlds.worldManager().findWorld(value); } } \ No newline at end of file diff --git a/src/main/java/me/lokka30/phantomworlds/commands/phantomworlds/parameters/resolvers/GameRulesResolver.java b/src/main/java/me/lokka30/phantomworlds/commands/phantomworlds/parameters/resolvers/GameRulesResolver.java deleted file mode 100644 index 85a0e3f..0000000 --- a/src/main/java/me/lokka30/phantomworlds/commands/phantomworlds/parameters/resolvers/GameRulesResolver.java +++ /dev/null @@ -1,49 +0,0 @@ -package me.lokka30.phantomworlds.commands.phantomworlds.parameters.resolvers; -/* - * Phantom Worlds - * Copyright (C) 2023 Daniel "creatorfromhell" Vidmar - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ - -import me.lokka30.phantomworlds.commands.phantomworlds.utils.WorldFolder; -import me.lokka30.phantomworlds.commands.phantomworlds.utils.WorldRule; -import org.bukkit.GameRule; -import org.jetbrains.annotations.NotNull; -import revxrsal.commands.process.ValueResolver; - -import java.util.ArrayList; -import java.util.ListIterator; - -/** - * GameRulesResolver - * - * @author creatorfromhell - * @since 2.0.5.0 - */ -public class GameRulesResolver implements ValueResolver { - - @Override - public WorldRule[] resolve(@NotNull ValueResolverContext context) throws Throwable { - final WorldRule[] rules = new WorldRule[context.arguments().size()]; - final ListIterator it = context.arguments().listIterator(); - - while(it.hasNext()) { - final String value = it.next(); - - - } - return rules; - } -} \ No newline at end of file diff --git a/src/main/java/me/lokka30/phantomworlds/commands/phantomworlds/parameters/suggestion/AliasWorldSuggestion.java b/src/main/java/me/lokka30/phantomworlds/commands/phantomworlds/parameters/suggestion/AliasWorldSuggestion.java index 134ea0a..4e3a09f 100644 --- a/src/main/java/me/lokka30/phantomworlds/commands/phantomworlds/parameters/suggestion/AliasWorldSuggestion.java +++ b/src/main/java/me/lokka30/phantomworlds/commands/phantomworlds/parameters/suggestion/AliasWorldSuggestion.java @@ -25,7 +25,6 @@ import revxrsal.commands.command.CommandActor; import revxrsal.commands.command.ExecutableCommand; -import java.io.File; import java.util.ArrayList; import java.util.Collection; import java.util.List; diff --git a/src/main/java/me/lokka30/phantomworlds/commands/phantomworlds/utils/WorldRule.java b/src/main/java/me/lokka30/phantomworlds/commands/phantomworlds/utils/WorldRule.java deleted file mode 100644 index 6981168..0000000 --- a/src/main/java/me/lokka30/phantomworlds/commands/phantomworlds/utils/WorldRule.java +++ /dev/null @@ -1,55 +0,0 @@ -package me.lokka30.phantomworlds.commands.phantomworlds.utils; -/* - * Phantom Worlds - * Copyright (C) 2023 Daniel "creatorfromhell" Vidmar - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ - -/** - * WorldRule - * - * @author creatorfromhell - * @since 2.0.5.0 - */ -public class WorldRule { - - private final String full; - private String rule; - private String value; - - public WorldRule(String full) { - this.full = full; - } - - public String getFull() { - return full; - } - - public String getRule() { - return rule; - } - - public void setRule(String rule) { - this.rule = rule; - } - - public String getValue() { - return value; - } - - public void setValue(String value) { - this.value = value; - } -} \ No newline at end of file diff --git a/src/main/java/me/lokka30/phantomworlds/listeners/player/PlayerChangeWorldListener.java b/src/main/java/me/lokka30/phantomworlds/listeners/player/PlayerChangeWorldListener.java index 97bbdb1..0f6b915 100644 --- a/src/main/java/me/lokka30/phantomworlds/listeners/player/PlayerChangeWorldListener.java +++ b/src/main/java/me/lokka30/phantomworlds/listeners/player/PlayerChangeWorldListener.java @@ -56,7 +56,7 @@ public void onChangeWorld(PlayerChangedWorldEvent event) { } final String cfgPath = "worlds-to-load." + event.getPlayer().getWorld().getName(); - if(PhantomWorlds.instance().data.getConfig().contains(cfgPath + ".gameMode")) { + if(PhantomWorlds.instance().data.getConfig().contains(cfgPath + ".gameMode") && !event.getPlayer().hasPermission("phantomworlds.world.bypass.gamemode")) { final GameMode mode = GameMode.valueOf(PhantomWorlds.instance().data.getConfig().getString(cfgPath + ".gameMode")); event.getPlayer().setGameMode(mode); } diff --git a/src/main/java/me/lokka30/phantomworlds/listeners/player/PlayerTeleportListener.java b/src/main/java/me/lokka30/phantomworlds/listeners/player/PlayerTeleportListener.java new file mode 100644 index 0000000..b7b7d48 --- /dev/null +++ b/src/main/java/me/lokka30/phantomworlds/listeners/player/PlayerTeleportListener.java @@ -0,0 +1,62 @@ +package me.lokka30.phantomworlds.listeners.player; +/* + * Phantom Worlds + * Copyright (C) 2023 Daniel "creatorfromhell" Vidmar + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +import me.lokka30.phantomworlds.PhantomWorlds; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.player.PlayerPortalEvent; +import org.bukkit.event.player.PlayerTeleportEvent; + +/** + * PlayerTeleportEvent + * + * @author creatorfromhell + * @since 2.0.5.0 + */ +public class PlayerTeleportListener implements Listener { + + final PhantomWorlds plugin; + + public PlayerTeleportListener(PhantomWorlds plugin) { + this.plugin = plugin; + } + + @EventHandler + public void onPortal(PlayerTeleportEvent event) { + if(event.getTo() == null || event.getTo().getWorld() == null || event.getFrom().getWorld() == null) { + return; + } + + if(event.getFrom().getWorld().getUID().equals(event.getTo().getWorld().getUID())) { + return; + } + + final String cfgPath = "worlds-to-load." + event.getTo().getWorld().getName(); + + if(PhantomWorlds.instance().data.getConfig().getBoolean(cfgPath + ".whitelist", false) + && !event.getPlayer().hasPermission("phantomworlds.world.access." + event.getPlayer().getWorld())) { + event.setCancelled(true); + return; + } + + if(event.getPlayer().hasPermission("phantomworlds.world.deny." + event.getPlayer().getWorld())) { + event.setCancelled(true); + } + } +} \ No newline at end of file diff --git a/src/main/java/me/lokka30/phantomworlds/managers/WorldManager.java b/src/main/java/me/lokka30/phantomworlds/managers/WorldManager.java index c8a28ec..d4982ea 100644 --- a/src/main/java/me/lokka30/phantomworlds/managers/WorldManager.java +++ b/src/main/java/me/lokka30/phantomworlds/managers/WorldManager.java @@ -9,14 +9,12 @@ import org.bukkit.GameMode; import org.bukkit.World; import org.bukkit.WorldType; +import org.jetbrains.annotations.Nullable; import java.io.File; import java.io.IOException; -import java.util.ArrayList; -import java.util.HashMap; import java.util.HashSet; import java.util.LinkedHashMap; -import java.util.List; import java.util.Map; import static me.lokka30.phantomworlds.misc.Utils.zipFolder; @@ -161,6 +159,12 @@ public PhantomWorld getPhantomWorldFromData(final String name) { return world; } + @Nullable + public World findWorld(final String name) { + + return Bukkit.getWorld(aliases.getOrDefault(name, name)); + } + public boolean backupWorld(final String world) { return backupWorld(world, new File(PhantomWorlds.instance().getDataFolder(), PhantomWorlds.BACKUP_FOLDER)); } diff --git a/src/main/java/me/lokka30/phantomworlds/managers/WorldRule.java b/src/main/java/me/lokka30/phantomworlds/managers/WorldRule.java deleted file mode 100644 index d48e5e7..0000000 --- a/src/main/java/me/lokka30/phantomworlds/managers/WorldRule.java +++ /dev/null @@ -1,56 +0,0 @@ -package me.lokka30.phantomworlds.managers; -/* - * Phantom Worlds - * Copyright (C) 2023 Daniel "creatorfromhell" Vidmar - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ - -/** - * WorldRule - * - * @author creatorfromhell - * @since 2.0.5.0 - */ -public abstract class WorldRule { - - private final String full; - - private String id; - private String value; - - public WorldRule(String full) { - this.full = full; - } - - public String getFull() { - return full; - } - - public String getId() { - return id; - } - - public void setId(String id) { - this.id = id; - } - - public String getValue() { - return value; - } - - public void setValue(String value) { - this.value = value; - } -} \ No newline at end of file