From 7171d43a2ac97f60c30d2bcfc37407388c7465d4 Mon Sep 17 00:00:00 2001 From: Magnus Ulf Date: Sat, 19 Jan 2019 19:27:54 +0100 Subject: [PATCH] Add faction votes --- plugin.yml | 18 +++ .../factions/cmd/CmdFactions.java | 3 +- .../factions/cmd/CmdFactionsVote.java | 15 +++ .../factions/cmd/CmdFactionsVoteCreate.java | 71 ++++++++++ .../factions/cmd/CmdFactionsVoteDo.java | 46 +++++++ .../factions/cmd/CmdFactionsVoteList.java | 42 ++++++ .../factions/cmd/CmdFactionsVoteRemove.java | 47 +++++++ .../factions/cmd/CmdFactionsVoteShow.java | 49 +++++++ .../factions/cmd/CmdFactionsWarpRemove.java | 1 + .../cmd/type/TypeEntityInternalFaction.java | 1 + .../factions/cmd/type/TypeRank.java | 2 + .../factions/cmd/type/TypeVote.java | 37 +++++ .../massivecraft/factions/entity/Faction.java | 25 +++- .../massivecraft/factions/entity/MPerm.java | 9 ++ .../massivecraft/factions/entity/Vote.java | 127 ++++++++++++++++++ .../migrator/MigratorMPlayer001Ranks.java | 2 +- .../factions/event/EventFactionsVoteAdd.java | 40 ++++++ .../event/EventFactionsVoteRemove.java | 40 ++++++ .../event/EventFactionsWarpRemove.java | 4 +- 19 files changed, 573 insertions(+), 6 deletions(-) create mode 100644 src/com/massivecraft/factions/cmd/CmdFactionsVote.java create mode 100644 src/com/massivecraft/factions/cmd/CmdFactionsVoteCreate.java create mode 100644 src/com/massivecraft/factions/cmd/CmdFactionsVoteDo.java create mode 100644 src/com/massivecraft/factions/cmd/CmdFactionsVoteList.java create mode 100644 src/com/massivecraft/factions/cmd/CmdFactionsVoteRemove.java create mode 100644 src/com/massivecraft/factions/cmd/CmdFactionsVoteShow.java create mode 100644 src/com/massivecraft/factions/cmd/type/TypeVote.java create mode 100644 src/com/massivecraft/factions/entity/Vote.java create mode 100644 src/com/massivecraft/factions/event/EventFactionsVoteAdd.java create mode 100644 src/com/massivecraft/factions/event/EventFactionsVoteRemove.java diff --git a/plugin.yml b/plugin.yml index 68e764a0..1e365be4 100644 --- a/plugin.yml +++ b/plugin.yml @@ -113,6 +113,12 @@ permissions: factions.unclaim.square: {description: unclaim by square and radius, default: false} factions.unclaim.circle: {description: unclaim by circle and radius, default: false} factions.unclaim.all: {description: unclaim all faction land, default: false} + factions.vote: {description: vote in faction votes, default: false} + factions.vote.do: {description: do vote, default: false} + factions.vote.list: {description: list votes, default: false} + factions.vote.show: {description: show vote result, default: false} + factions.vote.create: {description: create a vote, default: false} + factions.vote.remove: {description: remove a vote, default: false} factions.warp: {description: use warps, default: false} factions.warp.go: {description: go to a warp, default: false} factions.warp.list: {description: list warps, default: false} @@ -239,6 +245,12 @@ permissions: factions.unclaim.all: true factions.unsethome: true factions.unstuck: true + factions.vote: true + factions.vote.do: true + factions.vote.list: true + factions.vote.show: true + factions.vote.create: true + factions.vote.remove: true factions.warp: true factions.warp.go: true factions.warp.list: true @@ -384,6 +396,12 @@ permissions: factions.unclaim.all: true factions.unsethome: true factions.unstuck: true + factions.vote: true + factions.vote.do: true + factions.vote.list: true + factions.vote.show: true + factions.vote.create: true + factions.vote.remove: true factions.warp: true factions.warp.go: true factions.warp.list: true diff --git a/src/com/massivecraft/factions/cmd/CmdFactions.java b/src/com/massivecraft/factions/cmd/CmdFactions.java index 10594fcd..eff73a8d 100644 --- a/src/com/massivecraft/factions/cmd/CmdFactions.java +++ b/src/com/massivecraft/factions/cmd/CmdFactions.java @@ -1,11 +1,9 @@ package com.massivecraft.factions.cmd; import com.massivecraft.factions.Factions; -import com.massivecraft.factions.Perm; import com.massivecraft.factions.entity.MConf; import com.massivecraft.massivecore.command.MassiveCommandDeprecated; import com.massivecraft.massivecore.command.MassiveCommandVersion; -import com.massivecraft.massivecore.command.requirement.RequirementHasPerm; import java.util.List; @@ -30,6 +28,7 @@ public class CmdFactions extends FactionsCommand public CmdFactionsJoin cmdFactionsJoin = new CmdFactionsJoin(); public CmdFactionsLeave cmdFactionsLeave = new CmdFactionsLeave(); public CmdFactionsWarp cmdFactionsWarp = new CmdFactionsWarp(); + public CmdFactionsVote cmdFactionsVote = new CmdFactionsVote(); public CmdFactionsMap cmdFactionsMap = new CmdFactionsMap(); public CmdFactionsCreate cmdFactionsCreate = new CmdFactionsCreate(); public CmdFactionsName cmdFactionsName = new CmdFactionsName(); diff --git a/src/com/massivecraft/factions/cmd/CmdFactionsVote.java b/src/com/massivecraft/factions/cmd/CmdFactionsVote.java new file mode 100644 index 00000000..48384a08 --- /dev/null +++ b/src/com/massivecraft/factions/cmd/CmdFactionsVote.java @@ -0,0 +1,15 @@ +package com.massivecraft.factions.cmd; + +public class CmdFactionsVote extends FactionsCommand +{ + // -------------------------------------------- // + // FIELDS + // -------------------------------------------- // + + public CmdFactionsVoteShow cmdFactionsVoteShow = new CmdFactionsVoteShow(); + public CmdFactionsVoteList cmdFactionsVoteList = new CmdFactionsVoteList(); + public CmdFactionsVoteDo cmdFactionsVoteDo = new CmdFactionsVoteDo(); + public CmdFactionsVoteCreate cmdFactionsVoteCreate = new CmdFactionsVoteCreate(); + public CmdFactionsVoteRemove cmdFactionsVoteRemove = new CmdFactionsVoteRemove(); + +} diff --git a/src/com/massivecraft/factions/cmd/CmdFactionsVoteCreate.java b/src/com/massivecraft/factions/cmd/CmdFactionsVoteCreate.java new file mode 100644 index 00000000..fbfe87ce --- /dev/null +++ b/src/com/massivecraft/factions/cmd/CmdFactionsVoteCreate.java @@ -0,0 +1,71 @@ +package com.massivecraft.factions.cmd; + +import com.massivecraft.factions.entity.MPerm; +import com.massivecraft.factions.entity.Vote; +import com.massivecraft.factions.event.EventFactionsVoteAdd; +import com.massivecraft.massivecore.MassiveException; +import com.massivecraft.massivecore.collections.MassiveList; +import com.massivecraft.massivecore.command.type.container.TypeList; +import com.massivecraft.massivecore.command.type.primitive.TypeString; +import com.massivecraft.massivecore.util.IdUtil; + +import java.util.List; + +public class CmdFactionsVoteCreate extends FactionsCommandWarp +{ + // -------------------------------------------- // + // CONSTRUCT + // -------------------------------------------- // + + public CmdFactionsVoteCreate() + { + // Parameters + this.addParameter(TypeString.get(), "name"); + this.addParameter(TypeList.get(TypeString.get()), "options", true); + } + + // -------------------------------------------- // + // OVERRIDE + // -------------------------------------------- // + + @Override + public void perform() throws MassiveException + { + // Args + String name = this.readArg(); + List options = this.readArg(); + + // MPerm + if ( ! MPerm.getPermCreateVote().has(msender, msenderFaction, true)) return; + + if (msenderFaction.getVoteByName(name).isPresent()) + { + throw new MassiveException().addMsg("There is already a vote called %s.", name); + } + + // Make sure there are no duplicated + List options2 = new MassiveList<>(); + for (String str : options) + { + if (options2.stream().anyMatch(str::equalsIgnoreCase)) continue; + options2.add(str); + } + + + Vote vote = new Vote(IdUtil.getId(sender), name, options2); + + // Event + EventFactionsVoteAdd event = new EventFactionsVoteAdd(sender, msenderFaction, vote); + event.run(); + if (event.isCancelled()) return; + vote = event.getVote(); + + // Apply + msenderFaction.addVote(vote); + + // Inform + msenderFaction.msg("%s added the vote %s to your faction. You can now use:", msender.describeTo(msenderFaction, true), vote.getName()); + msenderFaction.sendMessage(CmdFactions.get().cmdFactionsVote.cmdFactionsVoteDo.getTemplateWithArgs(null, vote.getName())); + } + +} diff --git a/src/com/massivecraft/factions/cmd/CmdFactionsVoteDo.java b/src/com/massivecraft/factions/cmd/CmdFactionsVoteDo.java new file mode 100644 index 00000000..61b5405d --- /dev/null +++ b/src/com/massivecraft/factions/cmd/CmdFactionsVoteDo.java @@ -0,0 +1,46 @@ +package com.massivecraft.factions.cmd; + +import com.massivecraft.factions.cmd.type.TypeVote; +import com.massivecraft.factions.entity.MPerm; +import com.massivecraft.factions.entity.Vote; +import com.massivecraft.massivecore.MassiveException; +import com.massivecraft.massivecore.command.type.primitive.TypeString; + +public class CmdFactionsVoteDo extends FactionsCommandWarp +{ + // -------------------------------------------- // + // CONSTRUCT + // -------------------------------------------- // + + public CmdFactionsVoteDo() + { + // Parameters + this.addParameter(TypeVote.get()); + this.addParameter(TypeString.get(), "option"); + } + + // -------------------------------------------- // + // OVERRIDE + // -------------------------------------------- // + + @Override + public void perform() throws MassiveException + { + // Args + Vote vote = TypeVote.get(msenderFaction).read(this.arg(), sender); + String option = this.readArg(); + + // Any and MPerm + if ( ! MPerm.getPermVote().has(msender, msenderFaction, true)) return; + + if (vote.getOptions().stream().noneMatch(option::equalsIgnoreCase)) + { + throw new MassiveException().addMsg("No option in %s matches %s.", vote.getName(), option); + } + + vote.setVote(msender, option); + + msg("Succesfully voted for %s in %s.", option, vote.getName()); + } + +} diff --git a/src/com/massivecraft/factions/cmd/CmdFactionsVoteList.java b/src/com/massivecraft/factions/cmd/CmdFactionsVoteList.java new file mode 100644 index 00000000..319b98d8 --- /dev/null +++ b/src/com/massivecraft/factions/cmd/CmdFactionsVoteList.java @@ -0,0 +1,42 @@ +package com.massivecraft.factions.cmd; + +import com.massivecraft.factions.entity.Vote; +import com.massivecraft.massivecore.MassiveException; +import com.massivecraft.massivecore.command.Parameter; +import com.massivecraft.massivecore.pager.Pager; +import com.massivecraft.massivecore.pager.Stringifier; +import com.massivecraft.massivecore.util.Txt; + +public class CmdFactionsVoteList extends FactionsCommandWarp +{ + // -------------------------------------------- // + // CONSTRUCT + // -------------------------------------------- // + + public CmdFactionsVoteList() + { + // Parameters + this.addParameter(Parameter.getPage()); + } + + // -------------------------------------------- // + // OVERRIDE + // -------------------------------------------- // + + @Override + public void perform() throws MassiveException + { + // Args + int idx = this.readArg(); + + Pager pager = new Pager<>(this, "Faction Votes", idx, msenderFaction.getVotes().getAll()); + pager.setMsonifier((Stringifier) (vote, i) -> + { + String options = Txt.implodeCommaAndDot(vote.getOptions(), Txt.parse("%s"), Txt.parse(", "), Txt.parse(" and "), ""); + return Txt.parse("%s: %s", vote.getName(), options); + }); + + pager.message(); + } + +} diff --git a/src/com/massivecraft/factions/cmd/CmdFactionsVoteRemove.java b/src/com/massivecraft/factions/cmd/CmdFactionsVoteRemove.java new file mode 100644 index 00000000..040a5496 --- /dev/null +++ b/src/com/massivecraft/factions/cmd/CmdFactionsVoteRemove.java @@ -0,0 +1,47 @@ +package com.massivecraft.factions.cmd; + +import com.massivecraft.factions.cmd.type.TypeVote; +import com.massivecraft.factions.entity.MPerm; +import com.massivecraft.factions.entity.Vote; +import com.massivecraft.factions.event.EventFactionsVoteRemove; +import com.massivecraft.massivecore.MassiveException; + +public class CmdFactionsVoteRemove extends FactionsCommandWarp +{ + // -------------------------------------------- // + // CONSTRUCT + // -------------------------------------------- // + + public CmdFactionsVoteRemove() + { + // Parameters + this.addParameter(TypeVote.get(), "vote"); + } + + // -------------------------------------------- // + // OVERRIDE + // -------------------------------------------- // + + @Override + public void perform() throws MassiveException + { + // Args + Vote vote = TypeVote.get(msenderFaction).read(this.arg(), sender); + + // Any and MPerm + if ( ! MPerm.getPermCreateVote().has(msender, msenderFaction, true)) return; + + // Event + EventFactionsVoteRemove event = new EventFactionsVoteRemove(sender, msenderFaction, vote); + event.run(); + if (event.isCancelled()) return; + vote = event.getVote(); + + // Apply + vote.detach(); + + // Inform + msender.msg("%s removed the vote %s from your faction.", msender.describeTo(msenderFaction, true), vote.getName()); + } + +} diff --git a/src/com/massivecraft/factions/cmd/CmdFactionsVoteShow.java b/src/com/massivecraft/factions/cmd/CmdFactionsVoteShow.java new file mode 100644 index 00000000..ec8a4080 --- /dev/null +++ b/src/com/massivecraft/factions/cmd/CmdFactionsVoteShow.java @@ -0,0 +1,49 @@ +package com.massivecraft.factions.cmd; + +import com.massivecraft.factions.cmd.type.TypeVote; +import com.massivecraft.factions.entity.Vote; +import com.massivecraft.massivecore.MassiveException; +import com.massivecraft.massivecore.util.Txt; + +public class CmdFactionsVoteShow extends FactionsCommandWarp +{ + // -------------------------------------------- // + // CONSTRUCT + // -------------------------------------------- // + + public CmdFactionsVoteShow() + { + // Parameters + this.addParameter(TypeVote.get()); + } + + // -------------------------------------------- // + // OVERRIDE + // -------------------------------------------- // + + @Override + public void perform() throws MassiveException + { + // Args + Vote vote = TypeVote.get(msenderFaction).read(this.arg(), sender); + + // Clean the vote + vote.clean(); + + // Message + message(Txt.titleize("Votes for " + vote.getName())); + + int numberOfVotes = vote.getId2Vote().size(); + for (String option : vote.getOptions()) + { + long num = vote.getId2Vote().values().stream() + .filter(option::equalsIgnoreCase).count(); + + double percent = ((double) num / (double) numberOfVotes) * 100; + if (Double.isInfinite(percent)) percent = 0D; + String str = Txt.parse("%s: %d/%d (%.2f%%)", option, num, numberOfVotes, percent); + message(str); + } + } + +} diff --git a/src/com/massivecraft/factions/cmd/CmdFactionsWarpRemove.java b/src/com/massivecraft/factions/cmd/CmdFactionsWarpRemove.java index 8f6aebf1..1fcceb01 100644 --- a/src/com/massivecraft/factions/cmd/CmdFactionsWarpRemove.java +++ b/src/com/massivecraft/factions/cmd/CmdFactionsWarpRemove.java @@ -39,6 +39,7 @@ public class CmdFactionsWarpRemove extends FactionsCommandWarp EventFactionsWarpRemove event = new EventFactionsWarpRemove(sender, faction, warp); event.run(); if (event.isCancelled()) return; + warp = event.getWarp(); // Apply warp.detach(); diff --git a/src/com/massivecraft/factions/cmd/type/TypeEntityInternalFaction.java b/src/com/massivecraft/factions/cmd/type/TypeEntityInternalFaction.java index ccee4da4..91224c38 100644 --- a/src/com/massivecraft/factions/cmd/type/TypeEntityInternalFaction.java +++ b/src/com/massivecraft/factions/cmd/type/TypeEntityInternalFaction.java @@ -11,6 +11,7 @@ import java.util.Collection; import java.util.Collections; import java.util.Set; + public abstract class TypeEntityInternalFaction> extends TypeAbstractChoice { // -------------------------------------------- // diff --git a/src/com/massivecraft/factions/cmd/type/TypeRank.java b/src/com/massivecraft/factions/cmd/type/TypeRank.java index 37d0369c..f4bc3b1d 100644 --- a/src/com/massivecraft/factions/cmd/type/TypeRank.java +++ b/src/com/massivecraft/factions/cmd/type/TypeRank.java @@ -8,6 +8,7 @@ import org.bukkit.command.CommandSender; import java.util.Collection; import java.util.List; + import java.util.Set; public class TypeRank extends TypeEntityInternalFaction @@ -43,6 +44,7 @@ public class TypeRank extends TypeEntityInternalFaction private final Rank currentRank; + // -------------------------------------------- // // OVERRIDE // -------------------------------------------- // diff --git a/src/com/massivecraft/factions/cmd/type/TypeVote.java b/src/com/massivecraft/factions/cmd/type/TypeVote.java new file mode 100644 index 00000000..7ea3447d --- /dev/null +++ b/src/com/massivecraft/factions/cmd/type/TypeVote.java @@ -0,0 +1,37 @@ +package com.massivecraft.factions.cmd.type; + +import com.massivecraft.factions.entity.Faction; +import com.massivecraft.factions.entity.Vote; + +import java.util.Collection; + +public class TypeVote extends TypeEntityInternalFaction +{ + // -------------------------------------------- // + // INSTANCE & CONSTRUCT + // -------------------------------------------- // + + private static TypeVote i = new TypeVote(); + public static TypeVote get() { return i; } + private TypeVote() + { + super(Vote.class); + } + + public static TypeVote get(Faction faction) { return new TypeVote(faction); } + public TypeVote(Faction faction) + { + super(Vote.class, faction); + } + + // -------------------------------------------- // + // OVERRIDE + // -------------------------------------------- // + + @Override + public Collection getAll(Faction faction) + { + return faction.getVotes().getAll(); + } + +} diff --git a/src/com/massivecraft/factions/entity/Faction.java b/src/com/massivecraft/factions/entity/Faction.java index 55652f79..2032f7a2 100644 --- a/src/com/massivecraft/factions/entity/Faction.java +++ b/src/com/massivecraft/factions/entity/Faction.java @@ -77,6 +77,7 @@ public class Faction extends Entity implements FactionsParticipator, MP this.setPowerBoost(that.powerBoost); this.invitations.load(that.invitations); this.ranks.load(that.ranks); + this.votes.load(that.votes); this.setRelationWishes(that.relationWishes); this.setFlagIds(that.flags); this.perms = that.perms; @@ -143,12 +144,14 @@ public class Faction extends Entity implements FactionsParticipator, MP // 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 EntityInternalMap invitations = new EntityInternalMap<>(this, Invitation.class); // This is where all the ranks are, they are faction specific private EntityInternalMap ranks = this.createRankMap(); + // This is the votes currently open in the faction + private EntityInternalMap votes = new EntityInternalMap<>(this, Vote.class); + // The keys in this map are factionIds. // Null means no special relation whishes. private MassiveMapDef relationWishes = new MassiveMapDef<>(); @@ -569,6 +572,26 @@ public class Faction extends Entity implements FactionsParticipator, MP return ret; } + // -------------------------------------------- // + // FIELD: votes + // -------------------------------------------- // + + // RAW + + public EntityInternalMap getVotes() { return this.votes; } + + public void addVote(Vote vote) + { + if (vote == null) throw new NullPointerException("vote"); + this.getVotes().attach(vote); + } + + public Optional getVoteByName(String name) + { + if (name == null) throw new NullPointerException("name"); + return this.getVotes().getAll().stream().filter(vote -> vote.getName().equalsIgnoreCase(name)).findFirst(); + } + // -------------------------------------------- // // FIELD: relationWish // -------------------------------------------- // diff --git a/src/com/massivecraft/factions/entity/MPerm.java b/src/com/massivecraft/factions/entity/MPerm.java index 67414c19..db15a747 100644 --- a/src/com/massivecraft/factions/entity/MPerm.java +++ b/src/com/massivecraft/factions/entity/MPerm.java @@ -52,6 +52,8 @@ public class MPerm extends Entity implements Prioritized, Registerable, N public final static transient String ID_WITHDRAW = "withdraw"; public final static transient String ID_TERRITORY = "territory"; public final static transient String ID_ACCESS = "access"; + public final static transient String ID_VOTE = "VOTE"; + public final static transient String ID_CREATEVOTE = "createvote"; public final static transient String ID_CLAIMNEAR = "claimnear"; public final static transient String ID_REL = "rel"; public final static transient String ID_DISBAND = "disband"; @@ -78,6 +80,8 @@ public class MPerm extends Entity implements Prioritized, Registerable, N public final static transient int PRIORITY_WITHDRAW = 16000; public final static transient int PRIORITY_TERRITORY = 17000; public final static transient int PRIORITY_ACCESS = 18000; + public final static transient int PRIORITY_VOTE = 18200; + public final static transient int PRIORITY_CREATEVOTE = 18600; public final static transient int PRIORITY_CLAIMNEAR = 19000; public final static transient int PRIORITY_REL = 20000; public final static transient int PRIORITY_DISBAND = 21000; @@ -128,6 +132,8 @@ public class MPerm extends Entity implements Prioritized, Registerable, N getPermWithdraw(); getPermTerritory(); getPermAccess(); + getPermVote(); + getPermCreateVote(); getPermClaimnear(); getPermRel(); getPermDisband(); @@ -155,6 +161,8 @@ public class MPerm extends Entity implements Prioritized, Registerable, N public static MPerm getPermWithdraw() { return getCreative(PRIORITY_WITHDRAW, ID_WITHDRAW, ID_WITHDRAW, "withdraw money", MUtil.set("LEADER"), false, true, true); } public static MPerm getPermTerritory() { return getCreative(PRIORITY_TERRITORY, ID_TERRITORY, ID_TERRITORY, "claim or unclaim", MUtil.set("LEADER", "OFFICER"), false, true, true); } public static MPerm getPermAccess() { return getCreative(PRIORITY_ACCESS, ID_ACCESS, ID_ACCESS, "grant territory", MUtil.set("LEADER", "OFFICER"), false, true, true); } + public static MPerm getPermVote() { return getCreative(PRIORITY_VOTE, ID_VOTE, ID_VOTE, "vote", MUtil.set("LEADER", "OFFICER", "MEMBER", "RECRUIT"), false, true, true); } + public static MPerm getPermCreateVote() { return getCreative(PRIORITY_CREATEVOTE, ID_CREATEVOTE, ID_CREATEVOTE, "manage votes", MUtil.set("LEADER", "OFFICER"), false, true, true); } public static MPerm getPermClaimnear() { return getCreative(PRIORITY_CLAIMNEAR, ID_CLAIMNEAR, ID_CLAIMNEAR, "claim nearby", MUtil.set("LEADER", "OFFICER", "MEMBER", "RECRUIT", "ALLY"), false, false, false); } // non editable, non visible. public static MPerm getPermRel() { return getCreative(PRIORITY_REL, ID_REL, ID_REL, "change relations", MUtil.set("LEADER", "OFFICER"), false, true, true); } public static MPerm getPermDisband() { return getCreative(PRIORITY_DISBAND, ID_DISBAND, ID_DISBAND, "disband the faction", MUtil.set("LEADER"), false, true, true); } @@ -203,6 +211,7 @@ public class MPerm extends Entity implements Prioritized, Registerable, N private transient boolean registered = false; public boolean isRegistered() { return this.registered; } public void setRegistered(boolean registered) { this.registered = registered; } + // -------------------------------------------- // // VERSION // -------------------------------------------- // diff --git a/src/com/massivecraft/factions/entity/Vote.java b/src/com/massivecraft/factions/entity/Vote.java new file mode 100644 index 00000000..5aa5aa1e --- /dev/null +++ b/src/com/massivecraft/factions/entity/Vote.java @@ -0,0 +1,127 @@ +package com.massivecraft.factions.entity; + +import com.massivecraft.massivecore.Named; +import com.massivecraft.massivecore.collections.MassiveMap; +import com.massivecraft.massivecore.store.EntityInternal; +import com.massivecraft.massivecore.store.EntityInternalMap; + +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; + +public class Vote extends EntityInternal implements Named +{ + // -------------------------------------------- // + // OVERRIDE: ENTITY + // -------------------------------------------- // + + @Override + public Vote load(Vote that) + { + this.creatorId = that.creatorId; + this.creationMillis = that.creationMillis; + this.options = that.options; + this.id2Vote = that.id2Vote; + + return this; + } + + // -------------------------------------------- // + // FIELDS + // -------------------------------------------- // + + private String creatorId; + public String getCreatorId() { return this.creatorId; } + + private long creationMillis; + public long getCreationMillis() { return this.creationMillis; } + + private String name; + @Override public String getName() { return this.name; } + + private List options; + public List getOptions() { return this.options; } + + private Map id2Vote; + public Map getId2Vote() { return id2Vote; } + + // -------------------------------------------- // + // CONSTRUCT + // -------------------------------------------- // + + // For GSON + private Vote() + { + this(null, 0, null, null); + } + + public Vote(String creatorId, String name, List options) + { + this(creatorId, System.currentTimeMillis(), name, options); + } + + public Vote(String creatorId, long creationMillis, String name, List options) + { + this.creatorId = creatorId; + this.creationMillis = creationMillis; + this.name = name; + this.options = options; + this.id2Vote = new MassiveMap<>(); + } + + // -------------------------------------------- // + // OTHER + // -------------------------------------------- // + + public void setVote(MPlayer mplayer, String choice) + { + if (mplayer == null) throw new NullPointerException("mplayer"); + if (choice == null) throw new NullPointerException("choice"); + + if (!this.getOptions().contains(choice)) throw new IllegalArgumentException(choice + " is not in " + this.getOptions()); + + id2Vote.put(mplayer.getId(), choice); + this.changed(); + } + + public String getVote(MPlayer mplayer) + { + if (mplayer == null) throw new NullPointerException("mplayer"); + + return this.getId2Vote().get(mplayer.getId()); + } + + public Faction getFaction() + { + EntityInternalMap internalMap = (EntityInternalMap) this.getContainer(); + Faction faction = (Faction) internalMap.getEntity(); + return faction; + } + + public void clean() + { + Faction faction = this.getFaction(); + for (Iterator> it = this.id2Vote.entrySet().iterator(); it.hasNext();) + { + Entry entry = it.next(); + String id = entry.getKey(); + + // MPlayer must be a member + if ( ! faction.getMPlayerIds().contains(id)) + { + it.remove(); + break; + } + + // And they must have the perm + MPlayer mplayer = MPlayer.get(id); + if (! MPerm.getPermVote().has(mplayer, faction, false)) + { + it.remove(); + break; + } + } + } + +} diff --git a/src/com/massivecraft/factions/entity/migrator/MigratorMPlayer001Ranks.java b/src/com/massivecraft/factions/entity/migrator/MigratorMPlayer001Ranks.java index 91d51677..9c134b76 100644 --- a/src/com/massivecraft/factions/entity/migrator/MigratorMPlayer001Ranks.java +++ b/src/com/massivecraft/factions/entity/migrator/MigratorMPlayer001Ranks.java @@ -48,7 +48,7 @@ public class MigratorMPlayer001Ranks extends MigratorRoot // Get faction JsonElement jsonFaction = entity.get("factionId"); - + String factionId; if (jsonFaction == null) factionId = MConf.get().defaultPlayerFactionId; else factionId = jsonFaction.getAsString(); diff --git a/src/com/massivecraft/factions/event/EventFactionsVoteAdd.java b/src/com/massivecraft/factions/event/EventFactionsVoteAdd.java new file mode 100644 index 00000000..f3ed680c --- /dev/null +++ b/src/com/massivecraft/factions/event/EventFactionsVoteAdd.java @@ -0,0 +1,40 @@ +package com.massivecraft.factions.event; + +import com.massivecraft.factions.entity.Faction; +import com.massivecraft.factions.entity.Vote; +import org.bukkit.command.CommandSender; +import org.bukkit.event.HandlerList; + +public class EventFactionsVoteAdd extends EventFactionsAbstractSender +{ + // -------------------------------------------- // + // REQUIRED EVENT CODE + // -------------------------------------------- // + + private static final HandlerList handlers = new HandlerList(); + @Override public HandlerList getHandlers() { return handlers; } + public static HandlerList getHandlerList() { return handlers; } + + // -------------------------------------------- // + // FIELDS + // -------------------------------------------- // + + private final Faction faction; + public Faction getFaction() { return this.faction; } + + private Vote vote; + public Vote getVote() { return this.vote; } + public void setVote(Vote vote) { this.vote = vote; } + + // -------------------------------------------- // + // CONSTRUCT + // -------------------------------------------- // + + public EventFactionsVoteAdd(CommandSender sender, Faction faction, Vote vote) + { + super(sender); + this.faction = faction; + this.vote = vote; + } + +} diff --git a/src/com/massivecraft/factions/event/EventFactionsVoteRemove.java b/src/com/massivecraft/factions/event/EventFactionsVoteRemove.java new file mode 100644 index 00000000..f7b3c03e --- /dev/null +++ b/src/com/massivecraft/factions/event/EventFactionsVoteRemove.java @@ -0,0 +1,40 @@ +package com.massivecraft.factions.event; + +import com.massivecraft.factions.entity.Faction; +import com.massivecraft.factions.entity.Vote; +import org.bukkit.command.CommandSender; +import org.bukkit.event.HandlerList; + +public class EventFactionsVoteRemove extends EventFactionsAbstractSender +{ + // -------------------------------------------- // + // REQUIRED EVENT CODE + // -------------------------------------------- // + + private static final HandlerList handlers = new HandlerList(); + @Override public HandlerList getHandlers() { return handlers; } + public static HandlerList getHandlerList() { return handlers; } + + // -------------------------------------------- // + // FIELDS + // -------------------------------------------- // + + private final Faction faction; + public Faction getFaction() { return this.faction; } + + private Vote vote; + public Vote getVote() { return this.vote; } + public void setVote(Vote vote) { this.vote = vote; } + + // -------------------------------------------- // + // CONSTRUCT + // -------------------------------------------- // + + public EventFactionsVoteRemove(CommandSender sender, Faction faction, Vote vote) + { + super(sender); + this.faction = faction; + this.vote = vote; + } + +} diff --git a/src/com/massivecraft/factions/event/EventFactionsWarpRemove.java b/src/com/massivecraft/factions/event/EventFactionsWarpRemove.java index aa368832..7ac52b03 100644 --- a/src/com/massivecraft/factions/event/EventFactionsWarpRemove.java +++ b/src/com/massivecraft/factions/event/EventFactionsWarpRemove.java @@ -23,8 +23,8 @@ public class EventFactionsWarpRemove extends EventFactionsAbstractSender public Faction getFaction() { return this.faction; } private Warp warp; - public Warp getNewWarp() { return this.warp; } - public void setNewWarp(Warp warp) { this.warp = warp; } + public Warp getWarp() { return this.warp; } + public void setWarp(Warp warp) { this.warp = warp; } // -------------------------------------------- // // CONSTRUCT