From 3e28dc90639fb24cbaf3abebe3bf918e1c8ef695 Mon Sep 17 00:00:00 2001 From: j502647092 Date: Tue, 2 Jun 2015 22:11:20 +0800 Subject: [PATCH] fix if Server Land is null can't load save file... --- .../protection/ResidencePermissions.java | 894 +++++++++--------- 1 file changed, 444 insertions(+), 450 deletions(-) diff --git a/src/com/bekvon/bukkit/residence/protection/ResidencePermissions.java b/src/com/bekvon/bukkit/residence/protection/ResidencePermissions.java index b386ef8..e7578c6 100644 --- a/src/com/bekvon/bukkit/residence/protection/ResidencePermissions.java +++ b/src/com/bekvon/bukkit/residence/protection/ResidencePermissions.java @@ -1,18 +1,9 @@ /* - * To change this template, choose Tools | Templates - * and open the template in the editor. + * To change this template, choose Tools | Templates and open the template in the editor. */ package com.bekvon.bukkit.residence.protection; -import org.bukkit.ChatColor; -import com.bekvon.bukkit.residence.Residence; -import com.bekvon.bukkit.residence.event.ResidenceFlagChangeEvent; -import com.bekvon.bukkit.residence.event.ResidenceFlagCheckEvent; -import com.bekvon.bukkit.residence.event.ResidenceFlagEvent.FlagType; -import com.bekvon.bukkit.residence.event.ResidenceOwnerChangeEvent; -import com.bekvon.bukkit.residence.permissions.PermissionManager; -import com.bekvon.bukkit.residence.permissions.PermissionGroup; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; @@ -20,484 +11,487 @@ import java.util.Iterator; import java.util.Map; import java.util.Map.Entry; import java.util.Set; + +import org.bukkit.ChatColor; import org.bukkit.entity.Player; +import com.bekvon.bukkit.residence.Residence; +import com.bekvon.bukkit.residence.event.ResidenceFlagChangeEvent; +import com.bekvon.bukkit.residence.event.ResidenceFlagCheckEvent; +import com.bekvon.bukkit.residence.event.ResidenceFlagEvent.FlagType; +import com.bekvon.bukkit.residence.event.ResidenceOwnerChangeEvent; +import com.bekvon.bukkit.residence.permissions.PermissionGroup; +import com.bekvon.bukkit.residence.permissions.PermissionManager; + /** * * @author Administrator */ public class ResidencePermissions extends FlagPermissions { - protected String owner; - protected String world; - protected ClaimedResidence residence; + public static ResidencePermissions load(ClaimedResidence res, Map root) throws Exception { + ResidencePermissions newperms = new ResidencePermissions(res); + newperms.owner = (String) root.get("Owner"); + if (newperms.owner == null) { + newperms.owner = (String) root.get("OwnerLastKnownName"); + } + if (newperms.owner == null) { + Residence.getLog().warning("发现未知所有者的领地,转换为Server Land..."); + newperms.owner = "Server Land"; + } + newperms.world = (String) root.get("World"); + FlagPermissions.load(root, newperms); + if (newperms.owner == null) + throw new Exception("错误的Owner数据..."); + if (newperms.world == null) + throw new Exception("错误的World数据..."); + if (newperms.playerFlags == null) + throw new Exception("错误的PlayerFlags数据..."); + if (newperms.groupFlags == null) + throw new Exception("错误的GroupFlags数据..."); + if (newperms.cuboidFlags == null) + throw new Exception("错误的CuboidFlags数据..."); + newperms.fixNames(); + return newperms; + } - private ResidencePermissions(ClaimedResidence res) - { - residence = res; - } + protected String owner; + protected String world; - public ResidencePermissions(ClaimedResidence res, String creator, String inworld) - { - this(res); - owner = creator; - world = inworld; - } + protected ClaimedResidence residence; - public boolean playerHas(String player, String flag, boolean def) - { - return this.playerHas(player, world, flag, def); - } + private ResidencePermissions(ClaimedResidence res) { + residence = res; + } - @Override - public boolean playerHas(String player, String world, String flag, boolean def) { - ResidenceFlagCheckEvent fc = new ResidenceFlagCheckEvent(residence,flag,FlagType.PLAYER,player,def); - Residence.getServ().getPluginManager().callEvent(fc); - if(fc.isOverriden()) - return fc.getOverrideValue(); - return super.playerHas(player, world, flag, def); - } + public ResidencePermissions(ClaimedResidence res, String creator, String inworld) { + this(res); + owner = creator; + world = inworld; + } - @Override - public boolean groupHas(String group, String flag, boolean def) { - ResidenceFlagCheckEvent fc = new ResidenceFlagCheckEvent(residence,flag,FlagType.GROUP,group,def); - Residence.getServ().getPluginManager().callEvent(fc); - if(fc.isOverriden()) - return fc.getOverrideValue(); - return super.groupHas(group, flag, def); - } + public void applyDefaultFlags() { + PermissionManager gm = Residence.getPermissionManager(); + PermissionGroup group = gm.getGroup(owner, world); + Set> dflags = group.getDefaultResidenceFlags(); + Set> dcflags = group.getDefaultCreatorFlags(); + Set>> dgflags = group.getDefaultGroupFlags(); + this.applyGlobalDefaults(); + for (Entry next : dflags) { + if (this.checkValidFlag(next.getKey(), true)) { + if (next.getValue()) { + this.setFlag(next.getKey(), FlagState.TRUE); + } else { + this.setFlag(next.getKey(), FlagState.FALSE); + } + } + } + for (Entry next : dcflags) { + if (this.checkValidFlag(next.getKey(), false)) { + if (next.getValue()) { + this.setPlayerFlag(owner, next.getKey(), FlagState.TRUE); + } else { + this.setPlayerFlag(owner, next.getKey(), FlagState.FALSE); + } + } + } + for (Entry> entry : dgflags) { + Map value = entry.getValue(); + for (Entry flag : value.entrySet()) { + if (flag.getValue()) { + this.setGroupFlag(entry.getKey(), flag.getKey(), FlagState.TRUE); + } else { + this.setGroupFlag(entry.getKey(), flag.getKey(), FlagState.FALSE); + } + } + } + } - @Override - public boolean has(String flag, boolean def) { - ResidenceFlagCheckEvent fc = new ResidenceFlagCheckEvent(residence,flag,FlagType.RESIDENCE,null,def); - Residence.getServ().getPluginManager().callEvent(fc); - if(fc.isOverriden()) - return fc.getOverrideValue(); - return super.has(flag, def); - } + public void applyDefaultFlags(Player player, boolean resadmin) { + if (this.hasResidencePermission(player, true) || resadmin) { + this.applyDefaultFlags(); + player.sendMessage(ChatColor.YELLOW + Residence.getLanguage().getPhrase("FlagsDefault")); + } else { + player.sendMessage(ChatColor.GREEN + Residence.getLanguage().getPhrase("NoPermission")); + } + } + public void applyGlobalDefaults() { + this.clearFlags(); + FlagPermissions gRD = Residence.getConfigManager().getGlobalResidenceDefaultFlags(); + FlagPermissions gCD = Residence.getConfigManager().getGlobalCreatorDefaultFlags(); + Map gGD = Residence.getConfigManager() + .getGlobalGroupDefaultFlags(); + for (Entry entry : gRD.cuboidFlags.entrySet()) { + if (entry.getValue()) { + this.setFlag(entry.getKey(), FlagState.TRUE); + } else { + this.setFlag(entry.getKey(), FlagState.FALSE); + } + } + for (Entry entry : gCD.cuboidFlags.entrySet()) { + if (entry.getValue()) { + this.setPlayerFlag(owner, entry.getKey(), FlagState.TRUE); + } else { + this.setPlayerFlag(owner, entry.getKey(), FlagState.FALSE); + } + } + for (Entry entry : gGD.entrySet()) { + for (Entry flag : entry.getValue().cuboidFlags.entrySet()) { + if (flag.getValue()) { + this.setGroupFlag(entry.getKey(), flag.getKey(), FlagState.TRUE); + } else { + this.setGroupFlag(entry.getKey(), flag.getKey(), FlagState.FALSE); + } + } + } + } - public boolean hasApplicableFlag(String player, String flag) - { - return super.inheritanceIsPlayerSet(player,flag) || super.inheritanceIsGroupSet(Residence.getPermissionManager().getGroupNameByPlayer(player,world),flag) || super.inheritanceIsSet(flag); - } + public void applyTemplate(Player player, FlagPermissions list, boolean resadmin) { + if (player != null) { + if (!player.getName().equals(owner) && !resadmin) { + player.sendMessage(ChatColor.RED + + Residence.getLanguage().getPhrase("NoPermission")); + return; + } + } else { + resadmin = true; + } + PermissionGroup group = Residence.getPermissionManager().getGroup(owner, world); + for (Entry flag : list.cuboidFlags.entrySet()) { + if (group.hasFlagAccess(flag.getKey()) || resadmin) { + this.cuboidFlags.put(flag.getKey(), flag.getValue()); + } else { + player.sendMessage(ChatColor.RED + + Residence.getLanguage().getPhrase("FlagSetDeny", + ChatColor.YELLOW + flag.getKey() + ChatColor.RED)); + } + } + for (Entry> plists : list.playerFlags.entrySet()) { + for (Entry flag : plists.getValue().entrySet()) { + if (group.hasFlagAccess(flag.getKey()) || resadmin) { + if (!this.playerFlags.containsKey(plists.getKey())) { + this.playerFlags.put(plists.getKey(), + Collections.synchronizedMap(new HashMap())); + } + this.playerFlags.get(plists.getKey()).put(flag.getKey(), flag.getValue()); + } else { + player.sendMessage(ChatColor.RED + + Residence.getLanguage().getPhrase("FlagSetDeny", + ChatColor.YELLOW + flag.getKey() + ChatColor.RED)); + } + } + } + for (Entry> glists : list.groupFlags.entrySet()) { + for (Entry flag : glists.getValue().entrySet()) { + if (group.hasFlagAccess(flag.getKey()) || resadmin) { + if (!this.groupFlags.containsKey(glists.getKey())) { + this.groupFlags.put(glists.getKey(), + Collections.synchronizedMap(new HashMap())); + } + this.groupFlags.get(glists.getKey()).put(flag.getKey(), flag.getValue()); + } else { + player.sendMessage(ChatColor.RED + + Residence.getLanguage().getPhrase("FlagSetDeny", + ChatColor.YELLOW + flag.getKey() + ChatColor.RED)); + } + } + } + if (player != null) { + player.sendMessage(ChatColor.GREEN + + Residence.getLanguage().getPhrase("PermissionsApply")); + } + } - public void applyTemplate(Player player, FlagPermissions list, boolean resadmin) - { - if(player!=null) - { - if(!player.getName().equals(owner) && !resadmin) - { - player.sendMessage(ChatColor.RED+Residence.getLanguage().getPhrase("NoPermission")); - return; - } - } - else - { - resadmin = true; - } - PermissionGroup group = Residence.getPermissionManager().getGroup(owner,world); - for(Entry flag : list.cuboidFlags.entrySet()) - { - if(group.hasFlagAccess(flag.getKey()) || resadmin) - { - this.cuboidFlags.put(flag.getKey(), flag.getValue()); - } - else - { - player.sendMessage(ChatColor.RED+Residence.getLanguage().getPhrase("FlagSetDeny", ChatColor.YELLOW+flag.getKey() + ChatColor.RED)); - } - } - for(Entry> plists : list.playerFlags.entrySet()) - { - for(Entry flag : plists.getValue().entrySet()) - { - if(group.hasFlagAccess(flag.getKey()) || resadmin) - { - if(!this.playerFlags.containsKey(plists.getKey())) - this.playerFlags.put(plists.getKey(), Collections.synchronizedMap(new HashMap())); - this.playerFlags.get(plists.getKey()).put(flag.getKey(), flag.getValue()); - } - else - { - player.sendMessage(ChatColor.RED+Residence.getLanguage().getPhrase("FlagSetDeny", ChatColor.YELLOW+flag.getKey() + ChatColor.RED)); - } - } - } - for(Entry> glists : list.groupFlags.entrySet()) - { - for(Entry flag : glists.getValue().entrySet()) - { - if(group.hasFlagAccess(flag.getKey()) || resadmin) - { - if(!this.groupFlags.containsKey(glists.getKey())) - this.groupFlags.put(glists.getKey(), Collections.synchronizedMap(new HashMap())); - this.groupFlags.get(glists.getKey()).put(flag.getKey(), flag.getValue()); - } - else - { - player.sendMessage(ChatColor.RED+Residence.getLanguage().getPhrase("FlagSetDeny", ChatColor.YELLOW+flag.getKey() + ChatColor.RED)); - } - } - } - if(player!=null) - player.sendMessage(ChatColor.GREEN+Residence.getLanguage().getPhrase("PermissionsApply")); - } + private boolean checkCanSetFlag(Player player, String flag, FlagState state, boolean globalflag, boolean resadmin) { + if (!checkValidFlag(flag, globalflag)) { + player.sendMessage(ChatColor.RED + Residence.getLanguage().getPhrase("InvalidFlag")); + return false; + } + if (state == FlagState.INVALID) { + player.sendMessage(ChatColor.RED + + Residence.getLanguage().getPhrase("InvalidFlagState")); + return false; + } + if (!resadmin) { + if (!this.hasResidencePermission(player, false)) { + player.sendMessage(ChatColor.RED + + Residence.getLanguage().getPhrase("NoPermission")); + return false; + } + if (!hasFlagAccess(owner, flag)) { + player.sendMessage(ChatColor.RED + + Residence.getLanguage().getPhrase("OwnerNoPermission")); + return false; + } + } + return true; + } - public boolean hasResidencePermission(Player player, boolean requireOwner) { - if (Residence.getConfigManager().enabledRentSystem()) { - String resname = residence.getName(); - if (Residence.getRentManager().isRented(resname)) { - if (requireOwner) { - return false; - } - String renter = Residence.getRentManager().getRentingPlayer(resname); - if (player.getName().equalsIgnoreCase(renter)) { - return true; - } else { - return (playerHas(player.getName(), "admin", false)); - } - } - } - if (requireOwner) { - return (owner.equalsIgnoreCase(player.getName())); - } - return (playerHas(player.getName(), "admin", false) || owner.equalsIgnoreCase(player.getName())); - } + public void fixNames() { + ArrayList fixNames = new ArrayList(); + Iterator>> it = playerFlags.entrySet().iterator(); + while (it.hasNext()) { + String name = it.next().getKey(); + if (!name.equals(name.toLowerCase())) { + fixNames.add(name); + } + } + for (String name : fixNames) { + Map get = playerFlags.get(name); + playerFlags.remove(name); + playerFlags.put(name.toLowerCase(), get); + } + } - private boolean checkCanSetFlag(Player player, String flag, FlagState state, boolean globalflag, boolean resadmin) - { - if(!checkValidFlag(flag,globalflag)) - { - player.sendMessage(ChatColor.RED+Residence.getLanguage().getPhrase("InvalidFlag")); - return false; - } - if(state == FlagState.INVALID) - { - player.sendMessage(ChatColor.RED+Residence.getLanguage().getPhrase("InvalidFlagState")); - return false; - } - if(!resadmin) - { - if(!this.hasResidencePermission(player,false)) - { - player.sendMessage(ChatColor.RED+Residence.getLanguage().getPhrase("NoPermission")); - return false; - } - if(!hasFlagAccess(owner, flag)) - { - player.sendMessage(ChatColor.RED+Residence.getLanguage().getPhrase("OwnerNoPermission")); - return false; - } - } - return true; - } + public String getOwner() { + return owner; + } - private boolean hasFlagAccess(String player, String flag) - { - PermissionGroup group = Residence.getPermissionManager().getGroup(player,world); - return group.hasFlagAccess(flag); - } + public String getWorld() { + return world; + } - public boolean setPlayerFlag(Player player, String targetPlayer, String flag, String flagstate, boolean resadmin) { - if(validFlagGroups.containsKey(flag)) - return this.setFlagGroupOnPlayer(player, targetPlayer, flag, flagstate, resadmin); - FlagState state = FlagPermissions.stringToFlagState(flagstate); - if (checkCanSetFlag(player, flag, state, false, resadmin)) { - ResidenceFlagChangeEvent fc = new ResidenceFlagChangeEvent(residence, player, flag, ResidenceFlagChangeEvent.FlagType.PLAYER, state, targetPlayer); - Residence.getServ().getPluginManager().callEvent(fc); - if (fc.isCancelled()) - return false; - if(super.setPlayerFlag(targetPlayer, flag, state)) - { - player.sendMessage(ChatColor.GREEN+Residence.getLanguage().getPhrase("FlagSet")); - return true; - } - } - return false; - } + @Override + public boolean groupHas(String group, String flag, boolean def) { + ResidenceFlagCheckEvent fc = new ResidenceFlagCheckEvent(residence, flag, FlagType.GROUP, + group, def); + Residence.getServ().getPluginManager().callEvent(fc); + if (fc.isOverriden()) + return fc.getOverrideValue(); + return super.groupHas(group, flag, def); + } - public boolean setGroupFlag(Player player, String group, String flag, String flagstate, boolean resadmin) { - group = group.toLowerCase(); - if(validFlagGroups.containsKey(flag)) - return this.setFlagGroupOnGroup(player, flag, group, flagstate, resadmin); - FlagState state = FlagPermissions.stringToFlagState(flagstate); - if (checkCanSetFlag(player, flag, state, false, resadmin)) { - if (Residence.getPermissionManager().hasGroup(group)) { - ResidenceFlagChangeEvent fc = new ResidenceFlagChangeEvent(residence, player, flag, ResidenceFlagChangeEvent.FlagType.GROUP, state, group); - Residence.getServ().getPluginManager().callEvent(fc); - if (fc.isCancelled()) - return false; - if(super.setGroupFlag(group, flag, state)) - { - player.sendMessage(ChatColor.GREEN+Residence.getLanguage().getPhrase("FlagSet")); - return true; - } - } else { - player.sendMessage(ChatColor.RED+Residence.getLanguage().getPhrase("InvalidGroup")); - return false; - } - } - return false; - } + @Override + public boolean has(String flag, boolean def) { + ResidenceFlagCheckEvent fc = new ResidenceFlagCheckEvent(residence, flag, + FlagType.RESIDENCE, null, def); + Residence.getServ().getPluginManager().callEvent(fc); + if (fc.isOverriden()) + return fc.getOverrideValue(); + return super.has(flag, def); + } - public boolean setFlag(Player player, String flag, String flagstate, boolean resadmin) { - if(validFlagGroups.containsKey(flag)) - return this.setFlagGroup(player, flag, flagstate, resadmin); - FlagState state = FlagPermissions.stringToFlagState(flagstate); - if (checkCanSetFlag(player, flag, state, true, resadmin)) { - ResidenceFlagChangeEvent fc = new ResidenceFlagChangeEvent(residence,player,flag,ResidenceFlagChangeEvent.FlagType.RESIDENCE,state,null); - Residence.getServ().getPluginManager().callEvent(fc); - if(fc.isCancelled()) - return false; - if(super.setFlag(flag, state)) - { - player.sendMessage(ChatColor.GREEN+Residence.getLanguage().getPhrase("FlagSet")); - return true; - } - } - return false; - } - - public boolean removeAllPlayerFlags(Player player, String targetPlayer, boolean resadmin) { - if (this.hasResidencePermission(player, false) || resadmin) { - ResidenceFlagChangeEvent fc = new ResidenceFlagChangeEvent(residence, player, "ALL", ResidenceFlagChangeEvent.FlagType.RESIDENCE, FlagState.NEITHER, null); - Residence.getServ().getPluginManager().callEvent(fc); - if (fc.isCancelled()) { - return false; - } - super.removeAllPlayerFlags(targetPlayer); - player.sendMessage(ChatColor.GREEN+Residence.getLanguage().getPhrase("FlagSet")); - return true; - } - return false; - } + public boolean hasApplicableFlag(String player, String flag) { + return super.inheritanceIsPlayerSet(player, flag) + || super.inheritanceIsGroupSet(Residence.getPermissionManager() + .getGroupNameByPlayer(player, world), flag) || super.inheritanceIsSet(flag); + } - public boolean removeAllGroupFlags(Player player, String group, boolean resadmin) { - if (this.hasResidencePermission(player, false) || resadmin) { - ResidenceFlagChangeEvent fc = new ResidenceFlagChangeEvent(residence, player, "ALL", ResidenceFlagChangeEvent.FlagType.GROUP, FlagState.NEITHER, null); - Residence.getServ().getPluginManager().callEvent(fc); - if (fc.isCancelled()) { - return false; - } - super.removeAllGroupFlags(group); - player.sendMessage(ChatColor.GREEN+Residence.getLanguage().getPhrase("FlagSet")); - return true; - } - return false; - } + private boolean hasFlagAccess(String player, String flag) { + PermissionGroup group = Residence.getPermissionManager().getGroup(player, world); + return group.hasFlagAccess(flag); + } + public boolean hasResidencePermission(Player player, boolean requireOwner) { + if (Residence.getConfigManager().enabledRentSystem()) { + String resname = residence.getName(); + if (Residence.getRentManager().isRented(resname)) { + if (requireOwner) + return false; + String renter = Residence.getRentManager().getRentingPlayer(resname); + if (player.getName().equalsIgnoreCase(renter)) + return true; + else + return (playerHas(player.getName(), "admin", false)); + } + } + if (requireOwner) + return (owner.equalsIgnoreCase(player.getName())); + return (playerHas(player.getName(), "admin", false) || owner.equalsIgnoreCase(player + .getName())); + } - @Override - public boolean setFlag(String flag, FlagState state) { - ResidenceFlagChangeEvent fc = new ResidenceFlagChangeEvent(residence, null,flag,ResidenceFlagChangeEvent.FlagType.RESIDENCE,state,null); - Residence.getServ().getPluginManager().callEvent(fc); - if(fc.isCancelled()) - return false; - return super.setFlag(flag, state); - } + public boolean playerHas(String player, String flag, boolean def) { + return this.playerHas(player, world, flag, def); + } - @Override - public boolean setGroupFlag(String group, String flag, FlagState state) { - ResidenceFlagChangeEvent fc = new ResidenceFlagChangeEvent(residence, null,flag,ResidenceFlagChangeEvent.FlagType.GROUP,state,group); - Residence.getServ().getPluginManager().callEvent(fc); - if(fc.isCancelled()) - return false; - return super.setGroupFlag(group, flag, state); - } + @Override + public boolean playerHas(String player, String world, String flag, boolean def) { + ResidenceFlagCheckEvent fc = new ResidenceFlagCheckEvent(residence, flag, FlagType.PLAYER, + player, def); + Residence.getServ().getPluginManager().callEvent(fc); + if (fc.isOverriden()) + return fc.getOverrideValue(); + return super.playerHas(player, world, flag, def); + } - @Override - public boolean setPlayerFlag(String player, String flag, FlagState state) { - ResidenceFlagChangeEvent fc = new ResidenceFlagChangeEvent(residence, null,flag,ResidenceFlagChangeEvent.FlagType.PLAYER,state, player); - Residence.getServ().getPluginManager().callEvent(fc); - if(fc.isCancelled()) - return false; - return super.setPlayerFlag(player,flag,state); - } + public boolean removeAllGroupFlags(Player player, String group, boolean resadmin) { + if (this.hasResidencePermission(player, false) || resadmin) { + ResidenceFlagChangeEvent fc = new ResidenceFlagChangeEvent(residence, player, "ALL", + ResidenceFlagChangeEvent.FlagType.GROUP, FlagState.NEITHER, null); + Residence.getServ().getPluginManager().callEvent(fc); + if (fc.isCancelled()) + return false; + super.removeAllGroupFlags(group); + player.sendMessage(ChatColor.GREEN + Residence.getLanguage().getPhrase("FlagSet")); + return true; + } + return false; + } - public void applyDefaultFlags(Player player, boolean resadmin) - { - if(this.hasResidencePermission(player, true) || resadmin) - { - this.applyDefaultFlags(); - player.sendMessage(ChatColor.YELLOW+Residence.getLanguage().getPhrase("FlagsDefault")); - } - else - player.sendMessage(ChatColor.GREEN+Residence.getLanguage().getPhrase("NoPermission")); - } + public boolean removeAllPlayerFlags(Player player, String targetPlayer, boolean resadmin) { + if (this.hasResidencePermission(player, false) || resadmin) { + ResidenceFlagChangeEvent fc = new ResidenceFlagChangeEvent(residence, player, "ALL", + ResidenceFlagChangeEvent.FlagType.RESIDENCE, FlagState.NEITHER, null); + Residence.getServ().getPluginManager().callEvent(fc); + if (fc.isCancelled()) + return false; + super.removeAllPlayerFlags(targetPlayer); + player.sendMessage(ChatColor.GREEN + Residence.getLanguage().getPhrase("FlagSet")); + return true; + } + return false; + } - public void applyDefaultFlags() - { - PermissionManager gm = Residence.getPermissionManager(); - PermissionGroup group = gm.getGroup(owner, world); - Set> dflags = group.getDefaultResidenceFlags(); - Set> dcflags = group.getDefaultCreatorFlags(); - Set>> dgflags = group.getDefaultGroupFlags(); - this.applyGlobalDefaults(); - for (Entry next : dflags) { - if (this.checkValidFlag(next.getKey(), true)) { - if (next.getValue()) { - this.setFlag(next.getKey(), FlagState.TRUE); - } else { - this.setFlag(next.getKey(), FlagState.FALSE); - } - } - } - for (Entry next : dcflags) { - if (this.checkValidFlag(next.getKey(), false)) { - if (next.getValue()) { - this.setPlayerFlag(owner, next.getKey(), FlagState.TRUE); - } else { - this.setPlayerFlag(owner, next.getKey(), FlagState.FALSE); - } - } - } - for (Entry> entry : dgflags) - { - Map value = entry.getValue(); - for(Entry flag : value.entrySet()) - { - if(flag.getValue()) - { - this.setGroupFlag(entry.getKey(), flag.getKey(), FlagState.TRUE); - } - else - { - this.setGroupFlag(entry.getKey(), flag.getKey(), FlagState.FALSE); - } - } - } - } + @Override + public Map save() { + Map root = super.save(); + root.put("Owner", owner); + root.put("World", world); + return root; + } - public void setOwner(String newOwner, boolean resetFlags) - { - ResidenceOwnerChangeEvent ownerchange = new ResidenceOwnerChangeEvent(residence,newOwner); - Residence.getServ().getPluginManager().callEvent(ownerchange); - owner = newOwner; - if(resetFlags) - this.applyDefaultFlags(); - } + public boolean setFlag(Player player, String flag, String flagstate, boolean resadmin) { + if (validFlagGroups.containsKey(flag)) + return this.setFlagGroup(player, flag, flagstate, resadmin); + FlagState state = FlagPermissions.stringToFlagState(flagstate); + if (checkCanSetFlag(player, flag, state, true, resadmin)) { + ResidenceFlagChangeEvent fc = new ResidenceFlagChangeEvent(residence, player, flag, + ResidenceFlagChangeEvent.FlagType.RESIDENCE, state, null); + Residence.getServ().getPluginManager().callEvent(fc); + if (fc.isCancelled()) + return false; + if (super.setFlag(flag, state)) { + player.sendMessage(ChatColor.GREEN + Residence.getLanguage().getPhrase("FlagSet")); + return true; + } + } + return false; + } - public String getOwner() - { - return owner; - } + @Override + public boolean setFlag(String flag, FlagState state) { + ResidenceFlagChangeEvent fc = new ResidenceFlagChangeEvent(residence, null, flag, + ResidenceFlagChangeEvent.FlagType.RESIDENCE, state, null); + Residence.getServ().getPluginManager().callEvent(fc); + if (fc.isCancelled()) + return false; + return super.setFlag(flag, state); + } - public String getWorld() - { - return world; - } + public boolean setFlagGroup(Player player, String flaggroup, String state, boolean resadmin) { + if (ResidencePermissions.validFlagGroups.containsKey(flaggroup)) { + ArrayList flags = ResidencePermissions.validFlagGroups.get(flaggroup); + boolean changed = false; + for (String flag : flags) { + if (this.setFlag(player, flag, state, resadmin)) { + changed = true; + } + } + return changed; + } + return false; + } - @Override - public Map save() { - Map root = super.save(); - root.put("Owner", owner); - root.put("World", world); - return root; - } + public boolean setFlagGroupOnGroup(Player player, String flaggroup, String group, String state, boolean resadmin) { + if (ResidencePermissions.validFlagGroups.containsKey(flaggroup)) { + ArrayList flags = ResidencePermissions.validFlagGroups.get(flaggroup); + boolean changed = false; + for (String flag : flags) { + if (this.setGroupFlag(player, group, flag, state, resadmin)) { + changed = true; + } + } + return changed; + } + return false; + } - public static ResidencePermissions load(ClaimedResidence res, Map root) throws Exception { - ResidencePermissions newperms = new ResidencePermissions(res); - newperms.owner = (String) root.get("Owner"); - newperms.world = (String) root.get("World"); - FlagPermissions.load(root, newperms); - if(newperms.owner==null||newperms.world==null||newperms.playerFlags==null||newperms.groupFlags==null||newperms.cuboidFlags==null) - throw new Exception("Invalid Residence Permissions..."); - newperms.fixNames(); - return newperms; - } + public boolean setFlagGroupOnPlayer(Player player, String target, String flaggroup, String state, boolean resadmin) { + if (ResidencePermissions.validFlagGroups.containsKey(flaggroup)) { + ArrayList flags = ResidencePermissions.validFlagGroups.get(flaggroup); + boolean changed = false; + for (String flag : flags) { + if (this.setPlayerFlag(player, target, flag, state, resadmin)) { + changed = true; + } + } + return changed; + } + return false; + } - public void fixNames() - { - ArrayList fixNames = new ArrayList(); - Iterator>> it = playerFlags.entrySet().iterator(); - while(it.hasNext()) - { - String name = it.next().getKey(); - if(!name.equals(name.toLowerCase())) - { - fixNames.add(name); - } - } - for(String name : fixNames) - { - Map get = playerFlags.get(name); - playerFlags.remove(name); - playerFlags.put(name.toLowerCase(), get); - } - } + public boolean setGroupFlag(Player player, String group, String flag, String flagstate, boolean resadmin) { + group = group.toLowerCase(); + if (validFlagGroups.containsKey(flag)) + return this.setFlagGroupOnGroup(player, flag, group, flagstate, resadmin); + FlagState state = FlagPermissions.stringToFlagState(flagstate); + if (checkCanSetFlag(player, flag, state, false, resadmin)) { + if (Residence.getPermissionManager().hasGroup(group)) { + ResidenceFlagChangeEvent fc = new ResidenceFlagChangeEvent(residence, player, flag, + ResidenceFlagChangeEvent.FlagType.GROUP, state, group); + Residence.getServ().getPluginManager().callEvent(fc); + if (fc.isCancelled()) + return false; + if (super.setGroupFlag(group, flag, state)) { + player.sendMessage(ChatColor.GREEN + + Residence.getLanguage().getPhrase("FlagSet")); + return true; + } + } else { + player.sendMessage(ChatColor.RED + + Residence.getLanguage().getPhrase("InvalidGroup")); + return false; + } + } + return false; + } - public void applyGlobalDefaults() - { - this.clearFlags(); - FlagPermissions gRD = Residence.getConfigManager().getGlobalResidenceDefaultFlags(); - FlagPermissions gCD = Residence.getConfigManager().getGlobalCreatorDefaultFlags(); - Map gGD = Residence.getConfigManager().getGlobalGroupDefaultFlags(); - for(Entry entry : gRD.cuboidFlags.entrySet()) - { - if(entry.getValue()) - this.setFlag(entry.getKey(), FlagState.TRUE); - else - this.setFlag(entry.getKey(), FlagState.FALSE); - } - for(Entry entry : gCD.cuboidFlags.entrySet()) - { - if(entry.getValue()) - this.setPlayerFlag(owner, entry.getKey(), FlagState.TRUE); - else - this.setPlayerFlag(owner, entry.getKey(), FlagState.FALSE); - } - for(Entry entry : gGD.entrySet()) - { - for(Entry flag : entry.getValue().cuboidFlags.entrySet()) - { - if(flag.getValue()) - this.setGroupFlag(entry.getKey(), flag.getKey(), FlagState.TRUE); - else - this.setGroupFlag(entry.getKey(), flag.getKey(), FlagState.FALSE); - } - } - } + @Override + public boolean setGroupFlag(String group, String flag, FlagState state) { + ResidenceFlagChangeEvent fc = new ResidenceFlagChangeEvent(residence, null, flag, + ResidenceFlagChangeEvent.FlagType.GROUP, state, group); + Residence.getServ().getPluginManager().callEvent(fc); + if (fc.isCancelled()) + return false; + return super.setGroupFlag(group, flag, state); + } - public boolean setFlagGroup(Player player, String flaggroup, String state, boolean resadmin) { - if (ResidencePermissions.validFlagGroups.containsKey(flaggroup)) { - ArrayList flags = ResidencePermissions.validFlagGroups.get(flaggroup); - boolean changed = false; - for (String flag : flags) { - if (this.setFlag(player, flag, state, resadmin)) { - changed = true; - } - } - return changed; - } - return false; - } + public void setOwner(String newOwner, boolean resetFlags) { + ResidenceOwnerChangeEvent ownerchange = new ResidenceOwnerChangeEvent(residence, newOwner); + Residence.getServ().getPluginManager().callEvent(ownerchange); + owner = newOwner; + if (resetFlags) { + this.applyDefaultFlags(); + } + } - public boolean setFlagGroupOnGroup(Player player, String flaggroup, String group, String state, boolean resadmin) { - if (ResidencePermissions.validFlagGroups.containsKey(flaggroup)) { - ArrayList flags = ResidencePermissions.validFlagGroups.get(flaggroup); - boolean changed = false; - for (String flag : flags) { - if (this.setGroupFlag(player, group, flag, state, resadmin)) { - changed = true; - } - } - return changed; - } - return false; - } + public boolean setPlayerFlag(Player player, String targetPlayer, String flag, String flagstate, boolean resadmin) { + if (validFlagGroups.containsKey(flag)) + return this.setFlagGroupOnPlayer(player, targetPlayer, flag, flagstate, resadmin); + FlagState state = FlagPermissions.stringToFlagState(flagstate); + if (checkCanSetFlag(player, flag, state, false, resadmin)) { + ResidenceFlagChangeEvent fc = new ResidenceFlagChangeEvent(residence, player, flag, + ResidenceFlagChangeEvent.FlagType.PLAYER, state, targetPlayer); + Residence.getServ().getPluginManager().callEvent(fc); + if (fc.isCancelled()) + return false; + if (super.setPlayerFlag(targetPlayer, flag, state)) { + player.sendMessage(ChatColor.GREEN + Residence.getLanguage().getPhrase("FlagSet")); + return true; + } + } + return false; + } - public boolean setFlagGroupOnPlayer(Player player, String target, String flaggroup, String state, boolean resadmin) { - if (ResidencePermissions.validFlagGroups.containsKey(flaggroup)) { - ArrayList flags = ResidencePermissions.validFlagGroups.get(flaggroup); - boolean changed = false; - for (String flag : flags) { - if (this.setPlayerFlag(player, target, flag, state, resadmin)) { - changed = true; - } - } - return changed; - } - return false; - } + @Override + public boolean setPlayerFlag(String player, String flag, FlagState state) { + ResidenceFlagChangeEvent fc = new ResidenceFlagChangeEvent(residence, null, flag, + ResidenceFlagChangeEvent.FlagType.PLAYER, state, player); + Residence.getServ().getPluginManager().callEvent(fc); + if (fc.isCancelled()) + return false; + return super.setPlayerFlag(player, flag, state); + } }