Updated Faction Name constraints

This commit is contained in:
gregor0812 2017-07-12 20:44:22 +02:00 committed by Gregor0812
parent c1d322a723
commit fa4b850b01
6 changed files with 100 additions and 74 deletions

View File

@ -3,6 +3,7 @@ package com.massivecraft.factions.cmd;
import com.massivecraft.factions.Factions;
import com.massivecraft.factions.Rel;
import com.massivecraft.factions.cmd.req.ReqHasntFaction;
import com.massivecraft.factions.cmd.type.TypeFactionNameStrict;
import com.massivecraft.factions.entity.Faction;
import com.massivecraft.factions.entity.FactionColl;
import com.massivecraft.factions.entity.MConf;
@ -10,13 +11,10 @@ import com.massivecraft.factions.event.EventFactionsCreate;
import com.massivecraft.factions.event.EventFactionsMembershipChange;
import com.massivecraft.factions.event.EventFactionsMembershipChange.MembershipChangeReason;
import com.massivecraft.massivecore.MassiveException;
import com.massivecraft.massivecore.command.type.primitive.TypeString;
import com.massivecraft.massivecore.mson.Mson;
import com.massivecraft.massivecore.store.MStore;
import org.bukkit.ChatColor;
import java.util.ArrayList;
public class CmdFactionsCreate extends FactionsCommand
{
// -------------------------------------------- //
@ -27,14 +25,14 @@ public class CmdFactionsCreate extends FactionsCommand
{
// Aliases
this.addAliases("new");
// Parameters
this.addParameter(TypeString.get(), "name");
this.addParameter(TypeFactionNameStrict.get(), "name");
// Requirements
this.addRequirements(ReqHasntFaction.get());
}
// -------------------------------------------- //
// OVERRIDE
// -------------------------------------------- //
@ -45,20 +43,6 @@ public class CmdFactionsCreate extends FactionsCommand
// Args
String newName = this.readArg();
// Verify
if (FactionColl.get().isNameTaken(newName))
{
msg("<b>That name is already in use.");
return;
}
ArrayList<String> nameValidationErrors = FactionColl.get().validateName(newName);
if (nameValidationErrors.size() > 0)
{
message(nameValidationErrors);
return;
}
// Pre-Generate Id
String factionId = MStore.createId();
@ -81,11 +65,11 @@ public class CmdFactionsCreate extends FactionsCommand
// Inform
msg("<i>You created the faction %s", faction.getName(msender));
message(Mson.mson(mson("You should now: ").color(ChatColor.YELLOW), CmdFactions.get().cmdFactionsDescription.getTemplate()));
// Log
if (MConf.get().logFactionCreate)
{
Factions.get().log(msender.getName()+" created a new faction: "+newName);
Factions.get().log(msender.getName() + " created a new faction: " + newName);
}
}

View File

@ -1,6 +1,7 @@
package com.massivecraft.factions.cmd;
import com.massivecraft.factions.cmd.type.TypeFaction;
import com.massivecraft.factions.cmd.type.TypeFactionNameLenient;
import com.massivecraft.factions.entity.Faction;
import com.massivecraft.factions.entity.FactionColl;
import com.massivecraft.factions.entity.MPerm;
@ -20,7 +21,7 @@ public class CmdFactionsName extends FactionsCommand
public CmdFactionsName()
{
// Parameters
this.addParameter(TypeString.get(), "new name");
this.addParameter(TypeFactionNameLenient.get(), "new name");
this.addParameter(TypeFaction.get(), "faction", "you");
}
@ -38,21 +39,6 @@ public class CmdFactionsName extends FactionsCommand
// MPerm
if ( ! MPerm.getPermName().has(msender, faction, true)) return;
// TODO does not first test cover selfcase?
if (FactionColl.get().isNameTaken(newName) && ! MiscUtil.getComparisonString(newName).equals(faction.getComparisonName()))
{
msg("<b>That name is already taken");
return;
}
ArrayList<String> errors = new ArrayList<>();
errors.addAll(FactionColl.get().validateName(newName));
if (errors.size() > 0)
{
message(errors);
return;
}
// Event
EventFactionsNameChange event = new EventFactionsNameChange(sender, faction, newName);
event.run();

View File

@ -0,0 +1,59 @@
package com.massivecraft.factions.cmd.type;
import com.massivecraft.factions.entity.Faction;
import com.massivecraft.factions.entity.FactionColl;
import com.massivecraft.factions.entity.MConf;
import com.massivecraft.factions.entity.MPlayer;
import com.massivecraft.factions.util.MiscUtil;
import com.massivecraft.massivecore.Named;
import com.massivecraft.massivecore.command.type.TypeNameAbstract;
import org.bukkit.command.CommandSender;
public class TypeFactionNameAbstract extends TypeNameAbstract
{
// -------------------------------------------- //
// CONSTRUCT
// -------------------------------------------- //
public TypeFactionNameAbstract(boolean strict)
{
super(strict);
}
// -------------------------------------------- //
// OVERRIDE
// -------------------------------------------- //
@Override
public Named getCurrent(CommandSender sender)
{
MPlayer mplayer = MPlayer.get(sender);
Faction faction = mplayer.getFaction();
return faction;
}
@Override
public boolean isNameTaken(String name)
{
return FactionColl.get().isNameTaken(name);
}
@Override
public boolean isCharacterAllowed(char character)
{
return MiscUtil.substanceChars.contains(String.valueOf(character));
}
@Override
public Integer getLengthMin()
{
return MConf.get().factionNameLengthMin;
}
@Override
public Integer getLengthMax()
{
return MConf.get().factionNameLengthMax;
}
}

View File

@ -0,0 +1,16 @@
package com.massivecraft.factions.cmd.type;
public class TypeFactionNameLenient extends TypeFactionNameAbstract
{
// -------------------------------------------- //
// INSTANCE & CONSTRUCT
// -------------------------------------------- //
private static TypeFactionNameLenient i = new TypeFactionNameLenient();
public static TypeFactionNameLenient get() { return i; }
public TypeFactionNameLenient()
{
super(false);
}
}

View File

@ -0,0 +1,16 @@
package com.massivecraft.factions.cmd.type;
public class TypeFactionNameStrict extends TypeFactionNameAbstract
{
// -------------------------------------------- //
// INSTANCE & CONSTRUCT
// -------------------------------------------- //
private static TypeFactionNameStrict i = new TypeFactionNameStrict();
public static TypeFactionNameStrict get() {return i; }
public TypeFactionNameStrict()
{
super(true);
}
}

View File

@ -207,41 +207,6 @@ public class FactionColl extends Coll<Faction>
}
}
// -------------------------------------------- //
// FACTION NAME
// -------------------------------------------- //
public ArrayList<String> validateName(String str)
{
// Create
ArrayList<String> errors = new ArrayList<>();
// Fill
// Check minimum length
if (MiscUtil.getComparisonString(str).length() < MConf.get().factionNameLengthMin)
{
errors.add(Txt.parse("<i>The faction name can't be shorter than <h>%s<i> chars.", MConf.get().factionNameLengthMin));
}
// Check maximum length
if (str.length() > MConf.get().factionNameLengthMax)
{
errors.add(Txt.parse("<i>The faction name can't be longer than <h>%s<i> chars.", MConf.get().factionNameLengthMax));
}
// Check characters used
for (char c : str.toCharArray())
{
if (!MiscUtil.substanceChars.contains(String.valueOf(c)))
{
errors.add(Txt.parse("<i>Faction name must be alphanumeric. \"<h>%s<i>\" is not allowed.", c));
}
}
// Return
return errors;
}
@Override
public Faction getByName(String name)
{