Add faction votes
This commit is contained in:
@@ -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();
|
||||
|
||||
15
src/com/massivecraft/factions/cmd/CmdFactionsVote.java
Normal file
15
src/com/massivecraft/factions/cmd/CmdFactionsVote.java
Normal 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();
|
||||
|
||||
}
|
||||
71
src/com/massivecraft/factions/cmd/CmdFactionsVoteCreate.java
Normal file
71
src/com/massivecraft/factions/cmd/CmdFactionsVoteCreate.java
Normal 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()));
|
||||
}
|
||||
|
||||
}
|
||||
46
src/com/massivecraft/factions/cmd/CmdFactionsVoteDo.java
Normal file
46
src/com/massivecraft/factions/cmd/CmdFactionsVoteDo.java
Normal 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());
|
||||
}
|
||||
|
||||
}
|
||||
42
src/com/massivecraft/factions/cmd/CmdFactionsVoteList.java
Normal file
42
src/com/massivecraft/factions/cmd/CmdFactionsVoteList.java
Normal 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();
|
||||
}
|
||||
|
||||
}
|
||||
47
src/com/massivecraft/factions/cmd/CmdFactionsVoteRemove.java
Normal file
47
src/com/massivecraft/factions/cmd/CmdFactionsVoteRemove.java
Normal 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());
|
||||
}
|
||||
|
||||
}
|
||||
49
src/com/massivecraft/factions/cmd/CmdFactionsVoteShow.java
Normal file
49
src/com/massivecraft/factions/cmd/CmdFactionsVoteShow.java
Normal 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);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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();
|
||||
|
||||
@@ -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>
|
||||
{
|
||||
// -------------------------------------------- //
|
||||
|
||||
@@ -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
|
||||
// -------------------------------------------- //
|
||||
|
||||
37
src/com/massivecraft/factions/cmd/type/TypeVote.java
Normal file
37
src/com/massivecraft/factions/cmd/type/TypeVote.java
Normal 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();
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user