2015-01-09 22:36:12 +01:00
package com.massivecraft.factions.cmd ;
2015-10-21 19:35:41 +02:00
import com.massivecraft.factions.cmd.type.TypeFaction ;
import com.massivecraft.factions.cmd.type.TypeMPerm ;
2018-12-20 01:35:30 +01:00
import com.massivecraft.factions.cmd.type.TypeMPermable ;
2015-01-09 22:36:12 +01:00
import com.massivecraft.factions.entity.Faction ;
import com.massivecraft.factions.entity.MPerm ;
2015-11-23 03:12:13 +01:00
import com.massivecraft.factions.event.EventFactionsPermChange ;
2015-02-12 12:00:55 +01:00
import com.massivecraft.massivecore.MassiveException ;
2016-06-08 13:00:15 +02:00
import com.massivecraft.massivecore.command.type.primitive.TypeBooleanYes ;
2015-01-09 22:36:12 +01:00
import com.massivecraft.massivecore.util.Txt ;
public class CmdFactionsPermSet extends FactionsCommand
{
// -------------------------------------------- //
// CONSTRUCT
// -------------------------------------------- //
public CmdFactionsPermSet ( )
{
2015-10-21 19:35:41 +02:00
// Parameters
this . addParameter ( TypeMPerm . get ( ) , " perm " ) ;
2018-12-30 10:48:23 +01:00
this . addParameter ( TypeMPermable . get ( ) , " rank/rel/player/faction " ) ;
2016-06-08 13:00:15 +02:00
this . addParameter ( TypeBooleanYes . get ( ) , " yes/no " ) ;
2015-10-21 19:35:41 +02:00
this . addParameter ( TypeFaction . get ( ) , " faction " , " you " ) ;
2015-01-09 22:36:12 +01:00
}
// -------------------------------------------- //
// OVERRIDE
// -------------------------------------------- //
@Override
2015-02-12 12:00:55 +01:00
public void perform ( ) throws MassiveException
2015-01-09 22:36:12 +01:00
{
// Args
2018-12-20 01:35:30 +01:00
MPerm perm = this . readArgAt ( 0 ) ;
Boolean value = this . readArgAt ( 2 ) ;
Faction faction = this . readArgAt ( 3 , msenderFaction ) ;
MPerm . MPermable permable = TypeMPermable . get ( faction ) . read ( this . argAt ( 1 ) , sender ) ;
2015-01-09 22:36:12 +01:00
// Do the sender have the right to change perms for this faction?
if ( ! MPerm . getPermPerms ( ) . has ( msender , faction , true ) ) return ;
// Is this perm editable?
2016-02-02 19:03:11 +01:00
if ( ! msender . isOverriding ( ) & & ! perm . isEditable ( ) )
2015-01-09 22:36:12 +01:00
{
2018-12-22 01:55:46 +01:00
throw new MassiveException ( ) . addMsg ( " <b>The perm <h>%s <b>is not editable. " , perm . getName ( ) ) ;
}
if ( permable = = faction )
{
throw new MassiveException ( ) . addMsg ( " <b>A faction can't have perms for itself. Perhaps try ranks. " ) ;
2015-01-09 22:36:12 +01:00
}
2015-11-23 03:12:13 +01:00
// Event
2018-12-20 01:35:30 +01:00
EventFactionsPermChange event = new EventFactionsPermChange ( sender , faction , perm , permable , value ) ;
2015-11-23 03:12:13 +01:00
event . run ( ) ;
if ( event . isCancelled ( ) ) return ;
value = event . getNewValue ( ) ;
2018-12-20 01:35:30 +01:00
// Apply
boolean change = faction . setPermitted ( permable , perm , value ) ;
2015-11-23 03:12:13 +01:00
2015-01-09 22:36:12 +01:00
// No change
2018-12-20 01:35:30 +01:00
if ( ! change )
2015-01-09 22:36:12 +01:00
{
2018-12-22 01:55:46 +01:00
throw new MassiveException ( ) . addMsg ( " %s <i>already has %s <i>set to %s <i>for %s<i>. " , faction . describeTo ( msender ) , perm . getDesc ( true , false ) , Txt . parse ( value ? " <g>YES " : " <b>NOO " ) , permable . getDisplayName ( msender ) ) ;
2015-01-09 22:36:12 +01:00
}
// The following is to make sure the leader always has the right to change perms if that is our goal.
2018-12-22 01:55:46 +01:00
if ( perm = = MPerm . getPermPerms ( ) & & MPerm . getPermPerms ( ) . getStandard ( ) . contains ( " LEADER " ) )
2015-01-09 22:36:12 +01:00
{
2018-12-20 01:35:30 +01:00
faction . setPermitted ( faction . getLeaderRank ( ) , MPerm . getPermPerms ( ) , true ) ;
2015-01-09 22:36:12 +01:00
}
2018-12-22 01:55:46 +01:00
2015-01-09 22:36:12 +01:00
// Inform sender
2018-12-22 01:55:46 +01:00
String yesNo = Txt . parse ( value ? " <g>YES " : " <b>NOO " ) ;
msg ( " <i>Set perm <h>%s <i>to <h>%s <i>for <reset>%s<i> in <reset>%s<i>. " , perm . getName ( ) , yesNo , permable . getDisplayName ( msender ) , faction . describeTo ( msender ) ) ;
2015-01-09 22:36:12 +01:00
}
}