2015-01-09 22:36:12 +01:00
package com.massivecraft.factions.cmd ;
import com.massivecraft.factions.Rel ;
2015-10-21 19:35:41 +02:00
import com.massivecraft.factions.cmd.type.TypeFaction ;
import com.massivecraft.factions.cmd.type.TypeMPerm ;
import com.massivecraft.factions.cmd.type.TypeRel ;
2015-01-09 22:36:12 +01:00
import com.massivecraft.factions.entity.Faction ;
import com.massivecraft.factions.entity.MPerm ;
import com.massivecraft.factions.entity.MPlayer ;
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 ;
2017-03-24 13:05:58 +01:00
import java.util.ArrayList ;
import java.util.List ;
2015-01-09 22:36:12 +01:00
public class CmdFactionsPermSet extends FactionsCommand
{
// -------------------------------------------- //
// CONSTRUCT
// -------------------------------------------- //
public CmdFactionsPermSet ( )
{
2015-10-21 19:35:41 +02:00
// Parameters
this . addParameter ( TypeMPerm . get ( ) , " perm " ) ;
this . addParameter ( TypeRel . get ( ) , " relation " ) ;
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
2015-05-06 17:04:35 +02:00
MPerm perm = this . readArg ( ) ;
Rel rel = this . readArg ( ) ;
Boolean value = this . readArg ( ) ;
Faction faction = this . readArg ( msenderFaction ) ;
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
{
msg ( " <b>The perm <h>%s <b>is not editable. " , perm . getName ( ) ) ;
return ;
}
2015-11-23 03:12:13 +01:00
// Event
EventFactionsPermChange event = new EventFactionsPermChange ( sender , faction , perm , rel , value ) ;
event . run ( ) ;
if ( event . isCancelled ( ) ) return ;
value = event . getNewValue ( ) ;
2015-01-09 22:36:12 +01:00
// No change
if ( faction . getPermitted ( perm ) . contains ( rel ) = = value )
{
msg ( " %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 " ) , rel . getColor ( ) + rel . getDescPlayerMany ( ) ) ;
return ;
}
// Apply
faction . setRelationPermitted ( perm , rel , value ) ;
// The following is to make sure the leader always has the right to change perms if that is our goal.
if ( perm = = MPerm . getPermPerms ( ) & & MPerm . getPermPerms ( ) . getStandard ( ) . contains ( Rel . LEADER ) )
{
faction . setRelationPermitted ( MPerm . getPermPerms ( ) , Rel . LEADER , true ) ;
}
// Create messages
2016-03-15 20:43:54 +01:00
List < Object > messages = new ArrayList < > ( ) ;
2015-01-09 22:36:12 +01:00
// Inform sender
messages . add ( Txt . titleize ( " Perm for " + faction . describeTo ( msender , true ) ) ) ;
messages . add ( MPerm . getStateHeaders ( ) ) ;
messages . add ( Txt . parse ( perm . getStateInfo ( faction . getPermitted ( perm ) , true ) ) ) ;
2015-09-03 09:33:01 +02:00
message ( messages ) ;
2015-01-09 22:36:12 +01:00
// Inform faction (their message is slighly different)
List < MPlayer > recipients = faction . getMPlayers ( ) ;
recipients . remove ( msender ) ;
for ( MPlayer recipient : recipients )
{
2015-12-10 20:00:06 +01:00
recipient . msg ( " <h>%s <i>set a perm for <h>%s<i>. " , msender . describeTo ( recipient , true ) , faction . describeTo ( recipient , true ) ) ;
2015-09-03 09:33:01 +02:00
recipient . message ( messages ) ;
2015-01-09 22:36:12 +01:00
}
}
}