Add faction votes

This commit is contained in:
Magnus Ulf
2019-01-19 19:27:54 +01:00
parent 1157607397
commit 7171d43a2a
19 changed files with 573 additions and 6 deletions

View File

@@ -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();

View File

@@ -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();
}

View File

@@ -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<String> options = this.readArg();
// MPerm
if ( ! MPerm.getPermCreateVote().has(msender, msenderFaction, true)) return;
if (msenderFaction.getVoteByName(name).isPresent())
{
throw new MassiveException().addMsg("<b>There is already a vote called <h>%s<b>.", name);
}
// Make sure there are no duplicated
List<String> 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<i> added the vote <h>%s <i>to your faction. You can now use:", msender.describeTo(msenderFaction, true), vote.getName());
msenderFaction.sendMessage(CmdFactions.get().cmdFactionsVote.cmdFactionsVoteDo.getTemplateWithArgs(null, vote.getName()));
}
}

View File

@@ -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("<b>No option in <h>%s <b>matches <h>%s<b>.", vote.getName(), option);
}
vote.setVote(msender, option);
msg("<i>Succesfully voted for <h>%s <i>in <h>%s<i>.", option, vote.getName());
}
}

View File

@@ -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<Vote> pager = new Pager<>(this, "Faction Votes", idx, msenderFaction.getVotes().getAll());
pager.setMsonifier((Stringifier<Vote>) (vote, i) ->
{
String options = Txt.implodeCommaAndDot(vote.getOptions(), Txt.parse("<teal>%s"), Txt.parse("<i>, "), Txt.parse(" <i>and "), "");
return Txt.parse("<lime>%s<i>: %s", vote.getName(), options);
});
pager.message();
}
}

View File

@@ -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<i> removed the vote <h>%s <i>from your faction.", msender.describeTo(msenderFaction, true), vote.getName());
}
}

View File

@@ -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("<lime>%s<i>: <i>%d/%d (<h>%.2f%%<i>)", option, num, numberOfVotes, percent);
message(str);
}
}
}

View File

@@ -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();

View File

@@ -11,6 +11,7 @@ import java.util.Collection;
import java.util.Collections;
import java.util.Set;
public abstract class TypeEntityInternalFaction<E extends EntityInternal<E>> extends TypeAbstractChoice<E>
{
// -------------------------------------------- //

View File

@@ -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<Rank>
@@ -43,6 +44,7 @@ public class TypeRank extends TypeEntityInternalFaction<Rank>
private final Rank currentRank;
// -------------------------------------------- //
// OVERRIDE
// -------------------------------------------- //

View File

@@ -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<Vote>
{
// -------------------------------------------- //
// 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<Vote> getAll(Faction faction)
{
return faction.getVotes().getAll();
}
}