From a376fffa47f110d855be8cb889738c4a58f3185a Mon Sep 17 00:00:00 2001 From: Olof Larsson Date: Thu, 13 Nov 2014 11:41:21 +0100 Subject: [PATCH] Use Massive collections to avoid null checks, convert to lower case and use quicker permission and flag checking. --- .../com/massivecraft/factions/Factions.java | 6 + .../adapter/FactionPreprocessAdapter.java | 6 +- .../factions/cmd/CmdFactionsPerm.java | 6 +- .../massivecraft/factions/entity/Faction.java | 394 +++++++++--------- .../factions/entity/FactionColl.java | 8 +- .../massivecraft/factions/entity/MPerm.java | 7 +- 6 files changed, 227 insertions(+), 200 deletions(-) diff --git a/src/main/java/com/massivecraft/factions/Factions.java b/src/main/java/com/massivecraft/factions/Factions.java index ec72f33d..b18fff90 100644 --- a/src/main/java/com/massivecraft/factions/Factions.java +++ b/src/main/java/com/massivecraft/factions/Factions.java @@ -1,5 +1,7 @@ package com.massivecraft.factions; +import org.bukkit.ChatColor; + import com.massivecraft.factions.adapter.BoardAdapter; import com.massivecraft.factions.adapter.BoardMapAdapter; import com.massivecraft.factions.adapter.FactionPreprocessAdapter; @@ -61,6 +63,10 @@ public class Factions extends MassivePlugin public final static String ID_SAFEZONE = "safezone"; public final static String ID_WARZONE = "warzone"; + public final static String NAME_NONE_DEFAULT = ChatColor.DARK_GREEN.toString() + "Wilderness"; + public final static String NAME_SAFEZONE_DEFAULT = "SafeZone"; + public final static String NAME_WARZONE_DEFAULT = "WarZone"; + // -------------------------------------------- // // INSTANCE & CONSTRUCT // -------------------------------------------- // diff --git a/src/main/java/com/massivecraft/factions/adapter/FactionPreprocessAdapter.java b/src/main/java/com/massivecraft/factions/adapter/FactionPreprocessAdapter.java index 2028df59..82d613f8 100644 --- a/src/main/java/com/massivecraft/factions/adapter/FactionPreprocessAdapter.java +++ b/src/main/java/com/massivecraft/factions/adapter/FactionPreprocessAdapter.java @@ -43,7 +43,11 @@ public class FactionPreprocessAdapter implements JsonDeserializer rename(jsonObject, "permOverrides", "perms"); } - public void rename(final JsonObject jsonObject, final String from, final String to) + // -------------------------------------------- // + // UTIL + // -------------------------------------------- // + + public static void rename(final JsonObject jsonObject, final String from, final String to) { JsonElement element = jsonObject.remove(from); if (element != null) jsonObject.add(to, element); diff --git a/src/main/java/com/massivecraft/factions/cmd/CmdFactionsPerm.java b/src/main/java/com/massivecraft/factions/cmd/CmdFactionsPerm.java index 6529d0c2..2d7d67c4 100644 --- a/src/main/java/com/massivecraft/factions/cmd/CmdFactionsPerm.java +++ b/src/main/java/com/massivecraft/factions/cmd/CmdFactionsPerm.java @@ -51,7 +51,7 @@ public class CmdFactionsPerm extends FactionsCommand msg(MPerm.getStateHeaders()); for (MPerm perm : MPerm.getAll()) { - msg(perm.getStateInfo(faction.getPermittedRelations(perm), true)); + msg(perm.getStateInfo(faction.getPermitted(perm), true)); } return; } @@ -65,7 +65,7 @@ public class CmdFactionsPerm extends FactionsCommand { msg(Txt.titleize("Perm for " + faction.describeTo(msender, true))); msg(MPerm.getStateHeaders()); - msg(mperm.getStateInfo(faction.getPermittedRelations(mperm), true)); + msg(mperm.getStateInfo(faction.getPermitted(mperm), true)); return; } @@ -105,7 +105,7 @@ public class CmdFactionsPerm extends FactionsCommand // Inform msg(Txt.titleize("Perm for " + faction.describeTo(msender, true))); msg(MPerm.getStateHeaders()); - msg(mperm.getStateInfo(faction.getPermittedRelations(mperm), true)); + msg(mperm.getStateInfo(faction.getPermitted(mperm), true)); } } diff --git a/src/main/java/com/massivecraft/factions/entity/Faction.java b/src/main/java/com/massivecraft/factions/entity/Faction.java index 8f9a4598..54a724a2 100644 --- a/src/main/java/com/massivecraft/factions/entity/Faction.java +++ b/src/main/java/com/massivecraft/factions/entity/Faction.java @@ -15,6 +15,9 @@ import com.massivecraft.factions.Lang; import com.massivecraft.factions.Rel; import com.massivecraft.factions.RelationParticipator; import com.massivecraft.factions.util.*; +import com.massivecraft.massivecore.CaseInsensitiveComparator; +import com.massivecraft.massivecore.collections.MassiveMapDef; +import com.massivecraft.massivecore.collections.MassiveTreeSetDef; import com.massivecraft.massivecore.mixin.Mixin; import com.massivecraft.massivecore.money.Money; import com.massivecraft.massivecore.ps.PS; @@ -116,19 +119,19 @@ public class Faction extends Entity implements EconomyParticipator // This is the ids of the invited players. // They are actually "senderIds" since you can invite "@console" to your faction. // Null means no one is invited - private Set invitedPlayerIds = null; + private MassiveTreeSetDef invitedPlayerIds = new MassiveTreeSetDef(CaseInsensitiveComparator.get()); // The keys in this map are factionIds. // Null means no special relation whishes. - private Map relationWishes = null; + private MassiveMapDef relationWishes = new MassiveMapDef(); // The flag overrides are modifications to the default values. // Null means default. - private Map flags = null; + private MassiveMapDef flags = new MassiveMapDef(); // The perm overrides are modifications to the default values. // Null means default. - private Map> perms = null; + private MassiveMapDef> perms = new MassiveMapDef>(); // -------------------------------------------- // // FIELD: id @@ -391,33 +394,27 @@ public class Faction extends Entity implements EconomyParticipator // FIELD: open // -------------------------------------------- // - /* + // Nowadays this is a flag! + + @Deprecated public boolean isDefaultOpen() { - return MConf.get().defaultFactionOpen; + return MFlag.getFlagOpen().isStandard(); } + @Deprecated public boolean isOpen() { - Boolean ret = this.open; - if (ret == null) ret = this.isDefaultOpen(); - return ret; + return this.getFlag(MFlag.getFlagOpen()); } + @Deprecated public void setOpen(Boolean open) { - // Clean input - Boolean target = open; - - // Detect Nochange - if (MUtil.equals(this.open, target)) return; - - // Apply - this.open = target; - - // Mark as changed - this.changed(); - }*/ + MFlag flag = MFlag.getFlagOpen(); + if (open == null) open = flag.isStandard(); + this.setFlag(flag, open); + } // -------------------------------------------- // // FIELD: invitedPlayerIds @@ -427,22 +424,15 @@ public class Faction extends Entity implements EconomyParticipator public TreeSet getInvitedPlayerIds() { - TreeSet ret = new TreeSet(String.CASE_INSENSITIVE_ORDER); - if (this.invitedPlayerIds != null) ret.addAll(this.invitedPlayerIds); - return ret; + return this.invitedPlayerIds; } public void setInvitedPlayerIds(Collection invitedPlayerIds) { // Clean input - TreeSet target; - if (invitedPlayerIds == null || invitedPlayerIds.isEmpty()) + MassiveTreeSetDef target = new MassiveTreeSetDef(CaseInsensitiveComparator.get()); + if (invitedPlayerIds != null) { - target = null; - } - else - { - target = new TreeSet(String.CASE_INSENSITIVE_ORDER); for (String invitedPlayerId : invitedPlayerIds) { target.add(invitedPlayerId.toLowerCase()); @@ -501,23 +491,13 @@ public class Faction extends Entity implements EconomyParticipator public Map getRelationWishes() { - Map ret = new LinkedHashMap(); - if (this.relationWishes != null) ret.putAll(this.relationWishes); - return ret; + return this.relationWishes; } public void setRelationWishes(Map relationWishes) { // Clean input - Map target; - if (relationWishes == null || relationWishes.isEmpty()) - { - target = null; - } - else - { - target = new LinkedHashMap(relationWishes); - } + MassiveMapDef target = new MassiveMapDef(relationWishes); // Detect Nochange if (MUtil.equals(this.relationWishes, target)) return; @@ -604,81 +584,31 @@ public class Faction extends Entity implements EconomyParticipator } // ... and if anything is explicitly set we use that info ... - if (this.flags != null) + Iterator> iter = this.flags.entrySet().iterator(); + while (iter.hasNext()) { - Iterator> iter = this.flags.entrySet().iterator(); - while (iter.hasNext()) + // ... for each entry ... + Entry entry = iter.next(); + + // ... extract id and remove null values ... + String id = entry.getKey(); + if (id == null) { - // ... for each entry ... - Entry entry = iter.next(); - - // ... extract id and remove null values ... - String id = entry.getKey(); - if (id == null) - { - iter.remove(); - continue; - } - - // ... resolve object and skip unknowns ... - MFlag mflag = MFlag.get(id); - if (mflag == null) continue; - - ret.put(mflag, entry.getValue()); + iter.remove(); + this.changed(); + continue; } + + // ... resolve object and skip unknowns ... + MFlag mflag = MFlag.get(id); + if (mflag == null) continue; + + ret.put(mflag, entry.getValue()); } return ret; } - public void setFlagIds(Map flags) - { - // Clean input - Map target = null; - if (flags != null) - { - // We start out with what was suggested - target = new LinkedHashMap(flags); - - // However if the context is fully live we try to throw some default values away. - if (this.attached() && Factions.get().isDatabaseInitialized()) - { - Iterator> iter = target.entrySet().iterator(); - while (iter.hasNext()) - { - // For each entry ... - Entry entry = iter.next(); - - // ... extract id and remove null values ... - String id = entry.getKey(); - if (id == null) - { - iter.remove(); - continue; - } - - // ... remove if known and standard ... - MFlag mflag = MFlag.get(id); - if (mflag != null && mflag.isStandard() == entry.getValue()) - { - iter.remove(); - } - } - - if (target.isEmpty()) target = null; - } - } - - // Detect Nochange - if (MUtil.equals(this.flags, target)) return; - - // Apply - this.flags = target; - - // Mark as changed - this.changed(); - } - public void setFlags(Map flags) { Map flagIds = new LinkedHashMap(); @@ -689,17 +619,79 @@ public class Faction extends Entity implements EconomyParticipator setFlagIds(flagIds); } + public void setFlagIds(Map flagIds) + { + // Clean input + MassiveMapDef target = new MassiveMapDef(); + for (Entry entry : flagIds.entrySet()) + { + String key = entry.getKey(); + if (key == null) continue; + key = key.toLowerCase(); // Lowercased Keys Version 2.6.0 --> 2.7.0 + + Boolean value = entry.getValue(); + if (value == null) continue; + + target.put(key, value); + } + + // Detect Nochange + if (MUtil.equals(this.flags, target)) return; + + // Apply + this.flags = new MassiveMapDef(target); + + // Mark as changed + this.changed(); + } + // FINER + public boolean getFlag(String flagId) + { + if (flagId == null) throw new NullPointerException("flagId"); + + Boolean ret = this.flags.get(flagId); + if (ret != null) return ret; + + MFlag flag = MFlag.get(flagId); + if (flag == null) throw new NullPointerException("flag"); + + return flag.isStandard(); + } + public boolean getFlag(MFlag flag) { - return this.getFlags().get(flag); + if (flag == null) throw new NullPointerException("flag"); + + String flagId = flag.getId(); + if (flagId == null) throw new NullPointerException("flagId"); + + Boolean ret = this.flags.get(flagId); + if (ret != null) return ret; + + return flag.isStandard(); } - public void setFlag(MFlag flag, boolean value) + + public Boolean setFlag(String flagId, boolean value) { - Map flags = this.getFlags(); - flags.put(flag, value); - this.setFlags(flags); + if (flagId == null) throw new NullPointerException("flagId"); + + Boolean ret = this.flags.put(flagId, value); + if (ret == null || ret != value) this.changed(); + return ret; + } + + public Boolean setFlag(MFlag flag, boolean value) + { + if (flag == null) throw new NullPointerException("flag"); + + String flagId = flag.getId(); + if (flagId == null) throw new NullPointerException("flagId"); + + Boolean ret = this.flags.put(flagId, value); + if (ret == null || ret != value) this.changed(); + return ret; } // -------------------------------------------- // @@ -718,85 +710,30 @@ public class Faction extends Entity implements EconomyParticipator } // ... and if anything is explicitly set we use that info ... - if (this.perms != null) + Iterator>> iter = this.perms.entrySet().iterator(); + while (iter.hasNext()) { - Iterator>> iter = this.perms.entrySet().iterator(); - while (iter.hasNext()) + // ... for each entry ... + Entry> entry = iter.next(); + + // ... extract id and remove null values ... + String id = entry.getKey(); + if (id == null) { - // ... for each entry ... - Entry> entry = iter.next(); - - // ... extract id and remove null values ... - String id = entry.getKey(); - if (id == null) - { - iter.remove(); - continue; - } - - // ... resolve object and skip unknowns ... - MPerm mperm = MPerm.get(id); - if (mperm == null) continue; - - ret.put(mperm, new LinkedHashSet(entry.getValue())); + iter.remove(); + continue; } + + // ... resolve object and skip unknowns ... + MPerm mperm = MPerm.get(id); + if (mperm == null) continue; + + ret.put(mperm, new LinkedHashSet(entry.getValue())); } return ret; } - public void setPermIds(Map> perms) - { - // Clean input - Map> target = null; - if (perms != null) - { - // We start out with what was suggested - target = new LinkedHashMap>(); - for (Entry> entry : perms.entrySet()) - { - target.put(entry.getKey(), new LinkedHashSet(entry.getValue())); - } - - // However if the context is fully live we try to throw some default values away. - if (this.attached() && Factions.get().isDatabaseInitialized()) - { - Iterator>> iter = target.entrySet().iterator(); - while (iter.hasNext()) - { - // For each entry ... - Entry> entry = iter.next(); - - // ... extract id and remove null values ... - String id = entry.getKey(); - if (id == null) - { - iter.remove(); - continue; - } - - // ... remove if known and standard ... - MPerm mperm = MPerm.get(id); - if (mperm != null && mperm.getStandard().equals(entry.getValue())) - { - iter.remove(); - } - } - - if (target.isEmpty()) target = null; - } - } - - // Detect Nochange - if (MUtil.equals(this.perms, target)) return; - - // Apply - this.perms = target; - - // Mark as changed - this.changed(); - } - public void setPerms(Map> perms) { Map> permIds = new LinkedHashMap>(); @@ -807,13 +744,98 @@ public class Faction extends Entity implements EconomyParticipator setPermIds(permIds); } + public void setPermIds(Map> perms) + { + // Clean input + MassiveMapDef> target = new MassiveMapDef>(); + for (Entry> entry : perms.entrySet()) + { + String key = entry.getKey(); + if (key == null) continue; + key = key.toLowerCase(); // Lowercased Keys Version 2.6.0 --> 2.7.0 + + Set value = entry.getValue(); + if (value == null) continue; + + target.put(key, value); + } + + // Detect Nochange + if (MUtil.equals(this.perms, target)) return; + + // Apply + this.perms = target; + + // Mark as changed + this.changed(); + } + // FINER + public boolean isPermitted(String permId, Rel rel) + { + if (permId == null) throw new NullPointerException("permId"); + + Set rels = this.perms.get(permId); + if (rels != null) return rels.contains(rel); + + MPerm perm = MPerm.get(permId); + if (perm == null) throw new NullPointerException("perm"); + + return perm.getStandard().contains(rel); + } + + public boolean isPermitted(MPerm perm, Rel rel) + { + if (perm == null) throw new NullPointerException("perm"); + + String permId = perm.getId(); + if (permId == null) throw new NullPointerException("permId"); + + Set rels = this.perms.get(permId); + if (rels != null) return rels.contains(rel); + + return perm.getStandard().contains(rel); + } + + // --- + + public Set getPermitted(MPerm perm) + { + if (perm == null) throw new NullPointerException("perm"); + + String permId = perm.getId(); + if (permId == null) throw new NullPointerException("permId"); + + Set rels = this.perms.get(permId); + if (rels != null) return rels; + + return perm.getStandard(); + } + + public Set getPermitted(String permId) + { + if (permId == null) throw new NullPointerException("permId"); + + Set rels = this.perms.get(permId); + if (rels != null) return rels; + + MPerm perm = MPerm.get(permId); + if (perm == null) throw new NullPointerException("perm"); + + return perm.getStandard(); + } + + @Deprecated + // Use getPermitted instead. It's much quicker although not immutable. public Set getPermittedRelations(MPerm perm) { return this.getPerms().get(perm); } + // --- + // TODO: Fix these below. They are reworking the whole map. + public void setPermittedRelations(MPerm perm, Set rels) { Map> perms = this.getPerms(); diff --git a/src/main/java/com/massivecraft/factions/entity/FactionColl.java b/src/main/java/com/massivecraft/factions/entity/FactionColl.java index 382cbea3..67bb14a5 100644 --- a/src/main/java/com/massivecraft/factions/entity/FactionColl.java +++ b/src/main/java/com/massivecraft/factions/entity/FactionColl.java @@ -2,8 +2,6 @@ package com.massivecraft.factions.entity; import java.util.*; -import org.bukkit.ChatColor; - import com.massivecraft.massivecore.store.Coll; import com.massivecraft.massivecore.store.MStore; import com.massivecraft.massivecore.util.Txt; @@ -89,7 +87,7 @@ public class FactionColl extends Coll faction = this.create(id); - faction.setName(ChatColor.DARK_GREEN+"Wilderness"); + faction.setName(Factions.NAME_NONE_DEFAULT); faction.setDescription(null); faction.setFlag(MFlag.getFlagOpen(), false); @@ -123,7 +121,7 @@ public class FactionColl extends Coll faction = this.create(id); - faction.setName("SafeZone"); + faction.setName(Factions.NAME_SAFEZONE_DEFAULT); faction.setDescription("Free from PVP and monsters"); faction.setFlag(MFlag.getFlagOpen(), false); @@ -156,7 +154,7 @@ public class FactionColl extends Coll faction = this.create(id); - faction.setName("WarZone"); + faction.setName(Factions.NAME_WARZONE_DEFAULT); faction.setDescription("Not the safest place to be"); faction.setFlag(MFlag.getFlagOpen(), false); diff --git a/src/main/java/com/massivecraft/factions/entity/MPerm.java b/src/main/java/com/massivecraft/factions/entity/MPerm.java index aca20d7c..6b339d26 100644 --- a/src/main/java/com/massivecraft/factions/entity/MPerm.java +++ b/src/main/java/com/massivecraft/factions/entity/MPerm.java @@ -277,9 +277,7 @@ public class MPerm extends Entity implements Prioritized, Registerable if (hostFaction == null) throw new NullPointerException("hostFaction"); Rel rel = faction.getRelationTo(hostFaction); - - Set permittedRelations = hostFaction.getPermittedRelations(this); - return permittedRelations.contains(rel); + return hostFaction.isPermitted(this, rel); } public boolean has(MPlayer mplayer, Faction hostFaction, boolean verboose) @@ -291,8 +289,7 @@ public class MPerm extends Entity implements Prioritized, Registerable if (mplayer.isUsingAdminMode()) return true; Rel rel = mplayer.getRelationTo(hostFaction); - Set permittedRelations = hostFaction.getPermittedRelations(this); - if (permittedRelations.contains(rel)) return true; + if (hostFaction.isPermitted(this, rel)) return true; if (verboose) mplayer.sendMessage(this.createDeniedMessage(mplayer, hostFaction));