From ee0a78a2fe6ea1a92f4752aa95b530680012d977 Mon Sep 17 00:00:00 2001 From: Jascha Starke Date: Sun, 24 Mar 2013 22:26:53 +0100 Subject: [PATCH] Experimental Feature: Per GameMode Permission (Vault, Perm-Groups) Readme update Removed dep. provided by plib --- README.md | 2 +- pom.xml | 11 -- .../limitedcreative/LimitedCreative.java | 1 + .../limitedcreative/ModGameModePerm.java | 66 +++++++++++ .../limitedcreative/ModInventories.java | 2 +- .../minecraft/limitedcreative/ModRegions.java | 2 +- .../limitedcreative/gmperm/GMPermConfig.java | 106 ++++++++++++++++++ .../gmperm/PlayerListener.java | 95 ++++++++++++++++ src/main/resources/lang/messages.properties | 2 + 9 files changed, 273 insertions(+), 14 deletions(-) create mode 100644 src/main/java/de/jaschastarke/minecraft/limitedcreative/ModGameModePerm.java create mode 100644 src/main/java/de/jaschastarke/minecraft/limitedcreative/gmperm/GMPermConfig.java create mode 100644 src/main/java/de/jaschastarke/minecraft/limitedcreative/gmperm/PlayerListener.java diff --git a/README.md b/README.md index 4af29a4..161283e 100644 --- a/README.md +++ b/README.md @@ -4,13 +4,13 @@ http://dev.bukkit.org/server-mods/limited-creative/ Required dependencies --------------------- +* [plib](https://github.com/possi/plib) * [Bukkit](https://github.com/Bukkit/Bukkit) Dependencies for optional integrations -------------------------------------- * [WorldGuard](https://github.com/sk89q/worldguard) -* [WorldEdit](https://github.com/sk89q/worldedit) * [xAuth](http://dev.bukkit.org/server-mods/xauth/) * [AuthMe](http://dev.bukkit.org/server-mods/authme-reloaded/) ([GitSource](https://github.com/Multiplayer-italia/AuthMe-Reloaded)) * [Multiverse-Core](http://dev.bukkit.org/server-mods/multiverse-core/) diff --git a/pom.xml b/pom.xml index 6a2ee8f..a4dfa71 100644 --- a/pom.xml +++ b/pom.xml @@ -55,11 +55,6 @@ onarandombox http://repo.onarandombox.com/content/groups/public - - - vault-repo - http://ci.herocraftonline.com/plugin/repository/everything - - org.bukkit - bukkit - 1.5.1-R0.1-SNAPSHOT - com.sk89q diff --git a/src/main/java/de/jaschastarke/minecraft/limitedcreative/LimitedCreative.java b/src/main/java/de/jaschastarke/minecraft/limitedcreative/LimitedCreative.java index 8b55ca6..bdcf525 100644 --- a/src/main/java/de/jaschastarke/minecraft/limitedcreative/LimitedCreative.java +++ b/src/main/java/de/jaschastarke/minecraft/limitedcreative/LimitedCreative.java @@ -29,6 +29,7 @@ public class LimitedCreative extends Core { addModule(new ModCreativeLimits(this)); addModule(new ModRegions(this)); addModule(new ModCmdBlocker(this)); + addModule(new ModGameModePerm(this)); addModule(new FeatureMetrics(this)); config.setModuleStates(); diff --git a/src/main/java/de/jaschastarke/minecraft/limitedcreative/ModGameModePerm.java b/src/main/java/de/jaschastarke/minecraft/limitedcreative/ModGameModePerm.java new file mode 100644 index 0000000..13d12ac --- /dev/null +++ b/src/main/java/de/jaschastarke/minecraft/limitedcreative/ModGameModePerm.java @@ -0,0 +1,66 @@ +package de.jaschastarke.minecraft.limitedcreative; + +import net.milkbowl.vault.permission.Permission; + +import org.bukkit.plugin.RegisteredServiceProvider; + +import de.jaschastarke.bukkit.lib.CoreModule; +import de.jaschastarke.minecraft.limitedcreative.gmperm.GMPermConfig; +import de.jaschastarke.minecraft.limitedcreative.gmperm.PlayerListener; +import de.jaschastarke.modularize.IModule; +import de.jaschastarke.modularize.ModuleEntry; +import de.jaschastarke.modularize.ModuleEntry.ModuleState; + +public class ModGameModePerm extends CoreModule { + private GMPermConfig config; + private Permission permission = null; + private RegisteredServiceProvider permissionProvider; + + public ModGameModePerm(LimitedCreative plugin) { + super(plugin); + } + @Override + public String getName() { + return "GameModePerm"; + } + + @Override + public void initialize(ModuleEntry entry) { + super.initialize(entry); + listeners.addListener(new PlayerListener(this)); + config = new GMPermConfig(this, entry); + plugin.getPluginConfig().registerSection(config); + + permissionProvider = plugin.getServer().getServicesManager().getRegistration(Permission.class); + + if (config.getEnabled()) { + if (!plugin.getServer().getPluginManager().isPluginEnabled("Vault")) { + getLog().warn(plugin.getLocale().trans("gmperm.warning.vault_not_found", getName())); + entry.initialState = ModuleState.NOT_INITIALIZED; + } /*else if (!getVaultPermission().hasGroupSupport()) { + getLog().warn(plugin.getLocale().trans("gmperm.warning.no_group_support", getName())); + entry.initialState = ModuleState.NOT_INITIALIZED; + }*/ + } + } + @Override + public void onEnable() { + super.onEnable(); + + getLog().info(plugin.getLocale().trans("basic.loaded.module")); + } + @Override + public void onDisable() { + super.onDisable(); + permission = null; + } + public GMPermConfig getConfig() { + return config; + } + public Permission getVaultPermission() { + if (permissionProvider != null && permission == null) { + permission = permissionProvider.getProvider(); + } + return permission; + } +} diff --git a/src/main/java/de/jaschastarke/minecraft/limitedcreative/ModInventories.java b/src/main/java/de/jaschastarke/minecraft/limitedcreative/ModInventories.java index 0f07063..ab7514b 100644 --- a/src/main/java/de/jaschastarke/minecraft/limitedcreative/ModInventories.java +++ b/src/main/java/de/jaschastarke/minecraft/limitedcreative/ModInventories.java @@ -47,7 +47,7 @@ public class ModInventories extends CoreModule { } String incomp = Hooks.InventoryIncompatible.test(); - if (incomp != null) { + if (config.getEnabled() && incomp != null) { getLog().warn(plugin.getLocale().trans("inventory.warning.conflict", incomp, this.getName())); entry.initialState = ModuleState.NOT_INITIALIZED; } diff --git a/src/main/java/de/jaschastarke/minecraft/limitedcreative/ModRegions.java b/src/main/java/de/jaschastarke/minecraft/limitedcreative/ModRegions.java index 21e3824..7dbcd36 100644 --- a/src/main/java/de/jaschastarke/minecraft/limitedcreative/ModRegions.java +++ b/src/main/java/de/jaschastarke/minecraft/limitedcreative/ModRegions.java @@ -53,7 +53,7 @@ public class ModRegions extends CoreModule { FlagList.addFlags(Flags.getList()); - if (!plugin.getServer().getPluginManager().isPluginEnabled("WorldGuard")) { + if (config.getEnabled() && !plugin.getServer().getPluginManager().isPluginEnabled("WorldGuard")) { getLog().warn(plugin.getLocale().trans("region.warning.worldguard_not_found", getName())); entry.initialState = ModuleState.NOT_INITIALIZED; } diff --git a/src/main/java/de/jaschastarke/minecraft/limitedcreative/gmperm/GMPermConfig.java b/src/main/java/de/jaschastarke/minecraft/limitedcreative/gmperm/GMPermConfig.java new file mode 100644 index 0000000..6231ef8 --- /dev/null +++ b/src/main/java/de/jaschastarke/minecraft/limitedcreative/gmperm/GMPermConfig.java @@ -0,0 +1,106 @@ +package de.jaschastarke.minecraft.limitedcreative.gmperm; + +import org.bukkit.configuration.ConfigurationSection; + +import de.jaschastarke.bukkit.lib.configuration.Configuration; +import de.jaschastarke.configuration.IConfigurationNode; +import de.jaschastarke.configuration.IConfigurationSubGroup; +import de.jaschastarke.configuration.InvalidValueException; +import de.jaschastarke.configuration.annotations.IsConfigurationNode; +import de.jaschastarke.maven.ArchiveDocComments; +import de.jaschastarke.minecraft.limitedcreative.ModGameModePerm; +import de.jaschastarke.modularize.IModule; +import de.jaschastarke.modularize.ModuleEntry; +import de.jaschastarke.modularize.ModuleEntry.ModuleState; + +/** + * GameMode-Permissions-Feature + * + * This Feature requires Vault-Plugin to be active. + * + * http://dev.bukkit.org/server-mods/limited-creative/pages/features/gmperm/ + */ +@ArchiveDocComments +public class GMPermConfig extends Configuration implements IConfigurationSubGroup { + protected ModGameModePerm mod; + protected ModuleEntry entry; + + public GMPermConfig(ModGameModePerm modGameModePerm, ModuleEntry modEntry) { + mod = modGameModePerm; + entry = modEntry; + } + + @Override + public void setValue(IConfigurationNode node, Object pValue) throws InvalidValueException { + super.setValue(node, pValue); + if (node.getName().equals("enabled")) { + if (getEnabled()) { + if (entry.initialState != ModuleState.NOT_INITIALIZED) + entry.enable(); + } else { + entry.disable(); + } + } + } + + @Override + public void setValues(ConfigurationSection sect) { + super.setValues(sect); + if (entry.initialState != ModuleState.NOT_INITIALIZED) + entry.initialState = getEnabled() ? ModuleState.ENABLED : ModuleState.DISABLED; + } + @Override + public String getName() { + return "gmperm"; + } + @Override + public int getOrder() { + return 600; + } + + /** + * GMPermEnabled + * + * Activates that players are put in a special permission group while in creative mode. + * + * default: false + */ + @IsConfigurationNode(order = 100) + public boolean getEnabled() { + return config.getBoolean("enabled", false); + } + + /** + * GMPermCreativeGroup + * + * Defines the Permission-Group which the player gets added to on entering creative-mode. If this value is changed + * the old group won't be automatically removed from players already in it. So be sure to delete the old group or + * remove all player of it, that they don't get stuck with that permissions. + */ + @IsConfigurationNode(order = 200) + public String getCreativeGroup() { + return config.getString("creativeGroup", ""); + } + + /** + * GMPermAdventureGroup + * + * Like GMPermCreativeGroup but for adventure users. This is optional, so you don't have to set any group. + * + * default: false + */ + @IsConfigurationNode(order = 300) + public String getAdventureGroup() { + return config.getString("adventureGroup"); + } + + @Override + public Object getValue(final IConfigurationNode node) { + Object val = super.getValue(node); + if (node.getName().equals("adventureGroup") && val == null) { + return new Boolean(false); + } else { + return val; + } + } +} diff --git a/src/main/java/de/jaschastarke/minecraft/limitedcreative/gmperm/PlayerListener.java b/src/main/java/de/jaschastarke/minecraft/limitedcreative/gmperm/PlayerListener.java new file mode 100644 index 0000000..d90c11e --- /dev/null +++ b/src/main/java/de/jaschastarke/minecraft/limitedcreative/gmperm/PlayerListener.java @@ -0,0 +1,95 @@ +/* + * Limited Creative - (Bukkit Plugin) + * Copyright (C) 2012 jascha@ja-s.de + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package de.jaschastarke.minecraft.limitedcreative.gmperm; + +import net.milkbowl.vault.permission.Permission; + +import org.bukkit.GameMode; +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.EventPriority; +import org.bukkit.event.Listener; +import org.bukkit.event.player.PlayerGameModeChangeEvent; +import org.bukkit.event.player.PlayerLoginEvent; +import org.bukkit.event.player.PlayerTeleportEvent; +import org.bukkit.event.player.PlayerLoginEvent.Result; + +import de.jaschastarke.minecraft.limitedcreative.ModGameModePerm; + +public class PlayerListener implements Listener { + private ModGameModePerm mod; + public PlayerListener(ModGameModePerm mod) { + this.mod = mod; + } + + protected Permission v() { + return mod.getVaultPermission(); + } + + @EventHandler(priority = EventPriority.MONITOR) + public void onGameModeChange(PlayerGameModeChangeEvent event) { + if (event.isCancelled()) + return; + + ensureGroup(event.getPlayer(), event.getNewGameMode()); + } + @EventHandler(priority = EventPriority.MONITOR) + public void onPlayerLogin(PlayerLoginEvent event) { + if (event.getResult() == Result.ALLOWED) + return; + + ensureGroup(event.getPlayer()); + } + @EventHandler(priority = EventPriority.MONITOR) + public void onPlayerTeleport(PlayerTeleportEvent event) { + if (!event.getFrom().getWorld().equals(event.getTo().getWorld())) { + ensureGroup(event.getPlayer()); + } + } + + protected void ensureGroup(Player player) { + ensureGroup(player, player.getGameMode()); + } + protected void ensureGroup(Player player, GameMode gm) { + String cgroup = mod.getConfig().getCreativeGroup(); + String agroup = mod.getConfig().getAdventureGroup(); + + if (gm == GameMode.CREATIVE) { + if (!v().playerInGroup(player, cgroup)) { + v().playerAddGroup(player, cgroup); + } + if (agroup != null && v().playerInGroup(player, agroup)) { + v().playerRemoveGroup(player, agroup); + } + } else if (gm == GameMode.ADVENTURE) { + if (v().playerInGroup(player, cgroup)) { + v().playerRemoveGroup(player, cgroup); + } + if (agroup != null && !v().playerInGroup(player, agroup)) { + v().playerAddGroup(player, agroup); + } + } else if (gm == GameMode.SURVIVAL) { + if (v().playerInGroup(player, cgroup)) { + v().playerRemoveGroup(player, cgroup); + } + if (agroup != null && v().playerInGroup(player, agroup)) { + v().playerRemoveGroup(player, agroup); + } + } + } +} diff --git a/src/main/resources/lang/messages.properties b/src/main/resources/lang/messages.properties index 8a67cb4..c7394db 100644 --- a/src/main/resources/lang/messages.properties +++ b/src/main/resources/lang/messages.properties @@ -2,6 +2,8 @@ basic.loaded.module: Module loaded. inventory.warning.conflict: Due to conflict with the plugin {0}, the feature {1} is disabled region.warning.worldguard_not_found: WorldGuard isn't found, the feature {0} is disabled +gmperm.warning.vault_not_found: Vault isn't found, the feature {0} is disabled +gmperm.warning.no_group_support: Your Permission-Plugin doesn't support groups, the feature {0} is disabled command.general: LimitedCreative: GameMode-Switch, Creative-Regions, Config and more command.regions: LimitedCreative-Region-Command: configure creative regions