Convert godmode over to using entity metadata

Remove old CommandBook-has-GodComponent checks (mostly)
Cleaned up some trailing whitespace
This commit is contained in:
zml2008 2012-03-23 20:19:54 -07:00
parent 16ce56c1ad
commit 3d50486dac
4 changed files with 84 additions and 141 deletions

View File

@ -22,11 +22,10 @@ import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import com.sk89q.commandbook.CommandBook;
import com.sk89q.commandbook.GodComponent;
import com.sk89q.util.yaml.YAMLFormat;
import com.sk89q.util.yaml.YAMLProcessor;
import org.bukkit.World;
@ -34,6 +33,8 @@ import org.bukkit.entity.Player;
import com.sk89q.worldguard.LocalPlayer;
import com.sk89q.worldguard.blacklist.Blacklist;
import org.bukkit.metadata.FixedMetadataValue;
import org.bukkit.metadata.MetadataValue;
/**
* Represents the global configuration and also delegates configuration
@ -44,6 +45,8 @@ import com.sk89q.worldguard.blacklist.Blacklist;
*/
public class ConfigurationManager {
public static final String GOD_METADATA_KEY = "god";
private static final String CONFIG_HEADER = "#\r\n" +
"# WorldGuard's main configuration file\r\n" +
"#\r\n" +
@ -80,22 +83,14 @@ public class ConfigurationManager {
*/
private YAMLProcessor config;
/**
* List of people with god mode.
*/
@Deprecated
private Set<String> hasGodMode = new HashSet<String>();
/**
* List of people who can breathe underwater.
*/
private Set<String> hasAmphibious = new HashSet<String>();
private boolean hasCommandBookGodMode = false;
public boolean useRegionsScheduler;
public boolean activityHaltToggle = false;
public boolean autoGodMode;
@Deprecated public boolean autoGodMode = false;
public boolean usePlayerMove;
/**
@ -133,12 +128,8 @@ public class ConfigurationManager {
}
config.removeProperty("suppress-tick-sync-warnings");
useRegionsScheduler = config.getBoolean(
"regions.use-scheduler", true);
autoGodMode = config.getBoolean(
"auto-invincible", config.getBoolean("auto-invincible-permission", false));
usePlayerMove = config.getBoolean(
"use-player-move-event", true);
useRegionsScheduler = config.getBoolean("regions.use-scheduler", true);
usePlayerMove = config.getBoolean("use-player-move-event", true);
useSqlDatabase = config.getBoolean(
"regions.sql.use", false);
@ -199,8 +190,6 @@ public class ConfigurationManager {
bl.forgetPlayer(player);
}
}
hasGodMode.remove(player.getName());
hasAmphibious.remove(player.getName());
}
@ -211,7 +200,9 @@ public class ConfigurationManager {
*/
@Deprecated
public void enableGodMode(Player player) {
hasGodMode.add(player.getName());
if (!hasGodMode(player)) {
player.setMetadata(GOD_METADATA_KEY, new FixedMetadataValue(plugin, true));
}
}
/**
@ -221,7 +212,7 @@ public class ConfigurationManager {
*/
@Deprecated
public void disableGodMode(Player player) {
hasGodMode.remove(player.getName());
player.removeMetadata(GOD_METADATA_KEY, plugin);
}
/**
@ -231,13 +222,20 @@ public class ConfigurationManager {
* @return Whether the player has godmode through WorldGuard or CommandBook
*/
public boolean hasGodMode(Player player) {
if (hasCommandBookGodMode) {
GodComponent god = CommandBook.inst().getComponentManager().getComponent(GodComponent.class);
if (god != null) {
return god.hasGodMode(player);
}
List<MetadataValue> values = player.getMetadata(GOD_METADATA_KEY);
switch (values.size()) {
case 0:
return false;
case 1:
return values.get(0).asBoolean();
default:
for (MetadataValue val : values) {
if (val.asBoolean()) {
return true;
}
}
return false;
}
return hasGodMode.contains(player.getName());
}
/**
@ -268,18 +266,12 @@ public class ConfigurationManager {
return hasAmphibious.contains(player.getName());
}
public void updateCommandBookGodMode() {
try {
if (plugin.getServer().getPluginManager().isPluginEnabled("CommandBook")) {
Class.forName("com.sk89q.commandbook.GodComponent");
hasCommandBookGodMode = true;
return;
}
} catch (ClassNotFoundException ignore) {}
hasCommandBookGodMode = false;
}
public boolean hasCommandBookGodMode() {
return hasCommandBookGodMode;
try {
Class.forName("com.sk89q.commandbook.GodComponent");
return true;
} catch (ClassNotFoundException e) {
return false;
}
}
}

View File

@ -123,7 +123,7 @@ public class WorldGuardPlugin extends JavaPlugin {
reg.register(GeneralCommands.class);
}
}
}, 0L);
}, 1L);
// Need to create the plugins/WorldGuard folder
getDataFolder().mkdirs();
@ -153,7 +153,6 @@ public class WorldGuardPlugin extends JavaPlugin {
(new WorldGuardEntityListener(this)).registerEvents();
(new WorldGuardWeatherListener(this)).registerEvents();
(new WorldGuardVehicleListener(this)).registerEvents();
(new WorldGuardServerListener(this)).registerEvents();
if (getServer().getPluginManager().isPluginEnabled("CommandBook")) {
getServer().getPluginManager().registerEvents(new WorldGuardCommandBookListener(this), this);
@ -165,16 +164,6 @@ public class WorldGuardPlugin extends JavaPlugin {
worldListener.initWorld(world);
}
worldListener.registerEvents();
if (!configuration.hasCommandBookGodMode()) {
// Check god mode for existing players, if any
for (Player player : getServer().getOnlinePlayers()) {
if (inGroup(player, "wg-invincible") ||
(configuration.autoGodMode && hasPermission(player, "worldguard.auto-invincible"))) {
configuration.enableGodMode(player);
}
}
}
}
/**

View File

@ -1,38 +0,0 @@
package com.sk89q.worldguard.bukkit;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.server.PluginDisableEvent;
import org.bukkit.event.server.PluginEnableEvent;
import org.bukkit.plugin.PluginManager;
/**
* @author zml2008
*/
public class WorldGuardServerListener implements Listener {
private final WorldGuardPlugin plugin;
public WorldGuardServerListener(WorldGuardPlugin plugin) {
this.plugin = plugin;
}
public void registerEvents() {
PluginManager pm = plugin.getServer().getPluginManager();
pm.registerEvents(this, plugin);
}
@EventHandler
public void onPluginEnable(PluginEnableEvent event) {
if (event.getPlugin().getDescription().getName().equalsIgnoreCase("CommandBook")) {
plugin.getGlobalStateManager().updateCommandBookGodMode();
}
}
@EventHandler
public void onPluginDisable(PluginDisableEvent event) {
if (event.getPlugin().getDescription().getName().equalsIgnoreCase("CommandBook")) {
plugin.getGlobalStateManager().updateCommandBookGodMode();
}
}
}