2019-08-30 18:55:25 +00:00
/ *
* This file is part of Baritone .
*
* Baritone is free software : you can redistribute it and / or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation , either version 3 of the License , or
* ( at your option ) any later version .
*
* Baritone is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU Lesser General Public License for more details .
*
* You should have received a copy of the GNU Lesser General Public License
* along with Baritone . If not , see < https : //www.gnu.org/licenses/>.
* /
2019-10-06 20:20:42 +00:00
package baritone.command.defaults ;
2019-08-30 18:55:25 +00:00
2019-09-21 07:07:10 +00:00
import baritone.Baritone ;
2019-09-19 20:30:40 +00:00
import baritone.api.IBaritone ;
2019-08-30 18:55:25 +00:00
import baritone.api.Settings ;
2019-10-06 20:20:42 +00:00
import baritone.api.command.Command ;
2021-01-30 04:30:51 +00:00
import baritone.api.command.argument.IArgConsumer ;
2019-10-06 20:20:42 +00:00
import baritone.api.command.exception.CommandException ;
import baritone.api.command.exception.CommandInvalidTypeException ;
2019-10-07 15:06:05 +00:00
import baritone.api.command.helpers.Paginator ;
import baritone.api.command.helpers.TabCompleteHelper ;
2021-01-30 04:30:51 +00:00
import baritone.api.utils.SettingsUtil ;
2019-10-07 00:02:41 +00:00
import net.minecraft.util.text.StringTextComponent ;
2020-07-19 20:50:56 +00:00
import net.minecraft.util.text.TextComponent ;
2019-08-30 18:55:25 +00:00
import net.minecraft.util.text.TextFormatting ;
import net.minecraft.util.text.event.ClickEvent ;
import net.minecraft.util.text.event.HoverEvent ;
2019-09-19 21:38:13 +00:00
import java.util.Arrays ;
2019-08-30 18:55:25 +00:00
import java.util.List ;
import java.util.Locale ;
import java.util.stream.Collectors ;
import java.util.stream.Stream ;
2021-01-30 04:30:51 +00:00
import static baritone.api.command.IBaritoneChatControl.FORCE_COMMAND_PREFIX ;
2019-08-30 18:55:25 +00:00
import static baritone.api.utils.SettingsUtil.settingTypeToString ;
import static baritone.api.utils.SettingsUtil.settingValueToString ;
public class SetCommand extends Command {
2019-09-19 20:40:46 +00:00
2019-09-19 20:30:40 +00:00
public SetCommand ( IBaritone baritone ) {
2019-10-05 00:22:15 +00:00
super ( baritone , " set " , " setting " , " settings " ) ;
2019-08-30 18:55:25 +00:00
}
@Override
2019-10-04 13:55:02 +00:00
public void execute ( String label , IArgConsumer args ) throws CommandException {
2019-09-20 23:32:43 +00:00
String arg = args . hasAny ( ) ? args . getString ( ) . toLowerCase ( Locale . US ) : " list " ;
2019-09-19 21:38:13 +00:00
if ( Arrays . asList ( " s " , " save " ) . contains ( arg ) ) {
2019-09-21 07:07:10 +00:00
SettingsUtil . save ( Baritone . settings ( ) ) ;
2019-09-01 20:12:06 +00:00
logDirect ( " Settings saved " ) ;
return ;
}
2019-09-19 21:38:13 +00:00
boolean viewModified = Arrays . asList ( " m " , " mod " , " modified " ) . contains ( arg ) ;
boolean viewAll = Arrays . asList ( " all " , " l " , " list " ) . contains ( arg ) ;
2019-09-04 06:46:20 +00:00
boolean paginate = viewModified | | viewAll ;
2019-08-30 18:55:25 +00:00
if ( paginate ) {
2019-09-20 23:32:43 +00:00
String search = args . hasAny ( ) & & args . peekAsOrNull ( Integer . class ) = = null ? args . getString ( ) : " " ;
2019-08-30 18:55:25 +00:00
args . requireMax ( 1 ) ;
List < ? extends Settings . Setting > toPaginate =
2019-09-21 07:07:10 +00:00
( viewModified ? SettingsUtil . modifiedSettings ( Baritone . settings ( ) ) : Baritone . settings ( ) . allSettings ) . stream ( )
2019-09-19 19:53:15 +00:00
. filter ( s - > ! s . getName ( ) . equals ( " logger " ) )
. filter ( s - > s . getName ( ) . toLowerCase ( Locale . US ) . contains ( search . toLowerCase ( Locale . US ) ) )
. sorted ( ( s1 , s2 ) - > String . CASE_INSENSITIVE_ORDER . compare ( s1 . getName ( ) , s2 . getName ( ) ) )
. collect ( Collectors . toList ( ) ) ;
2019-08-30 18:55:25 +00:00
Paginator . paginate (
2019-09-19 19:53:15 +00:00
args ,
new Paginator < > ( toPaginate ) ,
( ) - > logDirect (
! search . isEmpty ( )
? String . format ( " All %ssettings containing the string '%s': " , viewModified ? " modified " : " " , search )
: String . format ( " All %ssettings: " , viewModified ? " modified " : " " )
) ,
setting - > {
2020-07-19 20:50:56 +00:00
TextComponent typeComponent = new StringTextComponent ( String . format (
2019-09-19 19:53:15 +00:00
" (%s) " ,
settingTypeToString ( setting )
) ) ;
2020-07-26 03:53:22 +00:00
typeComponent . setStyle ( typeComponent . getStyle ( ) . setFormatting ( TextFormatting . DARK_GRAY ) ) ;
2020-07-19 20:50:56 +00:00
TextComponent hoverComponent = new StringTextComponent ( " " ) ;
2020-07-26 03:53:22 +00:00
hoverComponent . setStyle ( hoverComponent . getStyle ( ) . setFormatting ( TextFormatting . GRAY ) ) ;
hoverComponent . appendString ( setting . getName ( ) ) ;
hoverComponent . appendString ( String . format ( " \ nType: %s " , settingTypeToString ( setting ) ) ) ;
hoverComponent . appendString ( String . format ( " \ n \ nValue: \ n%s " , settingValueToString ( setting ) ) ) ;
2019-09-21 07:07:10 +00:00
String commandSuggestion = Baritone . settings ( ) . prefix . value + String . format ( " set %s " , setting . getName ( ) ) ;
2020-07-19 20:50:56 +00:00
TextComponent component = new StringTextComponent ( setting . getName ( ) ) ;
2020-07-26 03:53:22 +00:00
component . setStyle ( component . getStyle ( ) . setFormatting ( TextFormatting . GRAY ) ) ;
component . append ( typeComponent ) ;
component . setStyle ( component . getStyle ( )
2020-07-19 21:47:30 +00:00
. setHoverEvent ( new HoverEvent ( HoverEvent . Action . SHOW_TEXT , hoverComponent ) )
. setClickEvent ( new ClickEvent ( ClickEvent . Action . SUGGEST_COMMAND , commandSuggestion ) ) ) ;
2019-09-19 19:53:15 +00:00
return component ;
} ,
FORCE_COMMAND_PREFIX + " set " + arg + " " + search
2019-08-30 18:55:25 +00:00
) ;
return ;
}
args . requireMax ( 1 ) ;
boolean resetting = arg . equalsIgnoreCase ( " reset " ) ;
boolean toggling = arg . equalsIgnoreCase ( " toggle " ) ;
boolean doingSomething = resetting | | toggling ;
if ( resetting ) {
2019-09-20 23:32:43 +00:00
if ( ! args . hasAny ( ) ) {
2019-08-30 18:55:25 +00:00
logDirect ( " Please specify 'all' as an argument to reset to confirm you'd really like to do this " ) ;
logDirect ( " ALL settings will be reset. Use the 'set modified' or 'modified' commands to see what will be reset. " ) ;
logDirect ( " Specify a setting name instead of 'all' to only reset one setting " ) ;
2019-08-30 19:25:59 +00:00
} else if ( args . peekString ( ) . equalsIgnoreCase ( " all " ) ) {
2019-09-21 07:07:10 +00:00
SettingsUtil . modifiedSettings ( Baritone . settings ( ) ) . forEach ( Settings . Setting : : reset ) ;
2019-08-30 18:55:25 +00:00
logDirect ( " All settings have been reset to their default values " ) ;
2019-09-21 07:07:10 +00:00
SettingsUtil . save ( Baritone . settings ( ) ) ;
2019-08-30 18:55:25 +00:00
return ;
}
}
if ( toggling ) {
args . requireMin ( 1 ) ;
}
2019-08-30 19:25:59 +00:00
String settingName = doingSomething ? args . getString ( ) : arg ;
2019-09-21 07:07:10 +00:00
Settings . Setting < ? > setting = Baritone . settings ( ) . allSettings . stream ( )
2019-09-19 19:53:15 +00:00
. filter ( s - > s . getName ( ) . equalsIgnoreCase ( settingName ) )
. findFirst ( )
. orElse ( null ) ;
2019-09-19 21:09:09 +00:00
if ( setting = = null ) {
2019-08-30 18:55:25 +00:00
throw new CommandInvalidTypeException ( args . consumed ( ) , " a valid setting " ) ;
}
2019-09-20 23:32:43 +00:00
if ( ! doingSomething & & ! args . hasAny ( ) ) {
2019-08-30 18:55:25 +00:00
logDirect ( String . format ( " Value of setting %s: " , setting . getName ( ) ) ) ;
logDirect ( settingValueToString ( setting ) ) ;
} else {
String oldValue = settingValueToString ( setting ) ;
if ( resetting ) {
setting . reset ( ) ;
} else if ( toggling ) {
if ( setting . getValueClass ( ) ! = Boolean . class ) {
throw new CommandInvalidTypeException ( args . consumed ( ) , " a toggleable setting " , " some other setting " ) ;
}
//noinspection unchecked
( ( Settings . Setting < Boolean > ) setting ) . value ^ = true ;
logDirect ( String . format (
2019-09-19 19:53:15 +00:00
" Toggled setting %s to %s " ,
setting . getName ( ) ,
Boolean . toString ( ( Boolean ) setting . value )
2019-08-30 18:55:25 +00:00
) ) ;
} else {
2019-08-30 19:25:59 +00:00
String newValue = args . getString ( ) ;
2019-08-30 18:55:25 +00:00
try {
2019-09-21 07:07:10 +00:00
SettingsUtil . parseAndApply ( Baritone . settings ( ) , arg , newValue ) ;
2019-08-30 18:55:25 +00:00
} catch ( Throwable t ) {
t . printStackTrace ( ) ;
throw new CommandInvalidTypeException ( args . consumed ( ) , " a valid value " , t ) ;
}
}
if ( ! toggling ) {
logDirect ( String . format (
2019-09-19 19:53:15 +00:00
" Successfully %s %s to %s " ,
resetting ? " reset " : " set " ,
setting . getName ( ) ,
settingValueToString ( setting )
2019-08-30 18:55:25 +00:00
) ) ;
}
2020-07-19 20:50:56 +00:00
TextComponent oldValueComponent = new StringTextComponent ( String . format ( " Old value: %s " , oldValue ) ) ;
2020-07-26 03:53:22 +00:00
oldValueComponent . setStyle ( oldValueComponent . getStyle ( )
2020-07-19 21:47:30 +00:00
. setFormatting ( TextFormatting . GRAY )
. setHoverEvent ( new HoverEvent (
HoverEvent . Action . SHOW_TEXT ,
2019-10-07 00:02:41 +00:00
new StringTextComponent ( " Click to set the setting back to this value " )
2019-09-19 19:53:15 +00:00
) )
2020-07-19 21:47:30 +00:00
. setClickEvent ( new ClickEvent (
2019-09-19 19:53:15 +00:00
ClickEvent . Action . RUN_COMMAND ,
FORCE_COMMAND_PREFIX + String . format ( " set %s %s " , setting . getName ( ) , oldValue )
2020-07-19 20:50:56 +00:00
) ) ) ;
2019-09-06 10:59:10 +00:00
logDirect ( oldValueComponent ) ;
2019-09-21 07:07:10 +00:00
if ( ( setting . getName ( ) . equals ( " chatControl " ) & & ! ( Boolean ) setting . value & & ! Baritone . settings ( ) . chatControlAnyway . value ) | |
setting . getName ( ) . equals ( " chatControlAnyway " ) & & ! ( Boolean ) setting . value & & ! Baritone . settings ( ) . chatControl . value ) {
2019-08-30 18:55:25 +00:00
logDirect ( " Warning: Chat commands will no longer work. If you want to revert this change, use prefix control (if enabled) or click the old value listed above. " , TextFormatting . RED ) ;
} else if ( setting . getName ( ) . equals ( " prefixControl " ) & & ! ( Boolean ) setting . value ) {
logDirect ( " Warning: Prefixed commands will no longer work. If you want to revert this change, use chat control (if enabled) or click the old value listed above. " , TextFormatting . RED ) ;
}
}
2019-09-21 07:07:10 +00:00
SettingsUtil . save ( Baritone . settings ( ) ) ;
2019-08-30 18:55:25 +00:00
}
@Override
2019-10-04 13:55:02 +00:00
public Stream < String > tabComplete ( String label , IArgConsumer args ) throws CommandException {
2019-09-20 23:32:43 +00:00
if ( args . hasAny ( ) ) {
2019-08-30 19:25:59 +00:00
String arg = args . getString ( ) ;
2019-09-19 21:38:13 +00:00
if ( args . hasExactlyOne ( ) & & ! Arrays . asList ( " s " , " save " ) . contains ( args . peekString ( ) . toLowerCase ( Locale . US ) ) ) {
2019-08-30 18:55:25 +00:00
if ( arg . equalsIgnoreCase ( " reset " ) ) {
return new TabCompleteHelper ( )
2019-09-19 19:53:15 +00:00
. addModifiedSettings ( )
. prepend ( " all " )
. filterPrefix ( args . getString ( ) )
. stream ( ) ;
2019-08-30 18:55:25 +00:00
} else if ( arg . equalsIgnoreCase ( " toggle " ) ) {
return new TabCompleteHelper ( )
2019-09-19 19:53:15 +00:00
. addToggleableSettings ( )
. filterPrefix ( args . getString ( ) )
. stream ( ) ;
2019-08-30 18:55:25 +00:00
}
2019-09-21 07:07:10 +00:00
Settings . Setting setting = Baritone . settings ( ) . byLowerName . get ( arg . toLowerCase ( Locale . US ) ) ;
2019-09-19 21:09:09 +00:00
if ( setting ! = null ) {
2019-08-30 18:55:25 +00:00
if ( setting . getType ( ) = = Boolean . class ) {
TabCompleteHelper helper = new TabCompleteHelper ( ) ;
if ( ( Boolean ) setting . value ) {
2019-09-21 22:25:20 +00:00
helper . append ( " true " , " false " ) ;
2019-08-30 18:55:25 +00:00
} else {
2019-09-21 22:25:20 +00:00
helper . append ( " false " , " true " ) ;
2019-08-30 18:55:25 +00:00
}
2019-08-30 19:25:59 +00:00
return helper . filterPrefix ( args . getString ( ) ) . stream ( ) ;
2019-08-30 18:55:25 +00:00
} else {
return Stream . of ( settingValueToString ( setting ) ) ;
}
}
2019-09-20 23:32:43 +00:00
} else if ( ! args . hasAny ( ) ) {
2019-08-30 18:55:25 +00:00
return new TabCompleteHelper ( )
2019-09-19 19:53:15 +00:00
. addSettings ( )
. sortAlphabetically ( )
. prepend ( " list " , " modified " , " reset " , " toggle " , " save " )
. filterPrefix ( arg )
. stream ( ) ;
2019-08-30 18:55:25 +00:00
}
}
return Stream . empty ( ) ;
}
2019-09-06 10:59:10 +00:00
@Override
public String getShortDesc ( ) {
return " View or change settings " ;
}
2019-08-30 18:55:25 +00:00
@Override
public List < String > getLongDesc ( ) {
2019-09-19 21:38:13 +00:00
return Arrays . asList (
2019-09-19 19:53:15 +00:00
" Using the set command, you can manage all of Baritone's settings. Almost every aspect is controlled by these settings - go wild! " ,
" " ,
" Usage: " ,
" > set - Same as `set list` " ,
" > set list [page] - View all settings " ,
" > set modified [page] - View modified settings " ,
" > set <setting> - View the current value of a setting " ,
" > set <setting> <value> - Set the value of a setting " ,
" > set reset all - Reset ALL SETTINGS to their defaults " ,
" > set reset <setting> - Reset a setting to its default " ,
" > set toggle <setting> - Toggle a boolean setting " ,
" > set save - Save all settings (this is automatic tho) "
2019-08-30 18:55:25 +00:00
) ;
}
}