Swings.java 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. package mod.adrenix.oldswing.command.executors;
  2. import com.mojang.brigadier.arguments.IntegerArgumentType;
  3. import com.mojang.brigadier.builder.LiteralArgumentBuilder;
  4. import com.mojang.brigadier.suggestion.SuggestionProvider;
  5. import mod.adrenix.oldswing.command.ColorUtil;
  6. import mod.adrenix.oldswing.config.ClientConfig;
  7. import mod.adrenix.oldswing.config.ConfigHandler;
  8. import mod.adrenix.oldswing.config.CustomSwing;
  9. import net.minecraft.command.CommandSource;
  10. import net.minecraft.command.Commands;
  11. import net.minecraft.command.ISuggestionProvider;
  12. import net.minecraft.command.arguments.ItemArgument;
  13. import net.minecraft.command.arguments.ItemInput;
  14. import net.minecraft.item.Item;
  15. import net.minecraft.util.ResourceLocation;
  16. import net.minecraft.util.text.ITextComponent;
  17. import net.minecraft.util.text.TextFormatting;
  18. import net.minecraftforge.registries.ForgeRegistries;
  19. import java.util.ArrayList;
  20. public class Swings {
  21. private static final ArrayList<String> VALID_SPEEDS = new ArrayList<>();
  22. private static final int MIN = ConfigHandler.MIN;
  23. private static final int MAX = ConfigHandler.MAX;
  24. static {
  25. for (int i = MIN; i <= MAX; i++) {
  26. VALID_SPEEDS.add(Integer.toString(i));
  27. }
  28. }
  29. private static final SuggestionProvider<CommandSource> SPEED_SUGGESTION = (context, builder) ->
  30. ISuggestionProvider.suggest(VALID_SPEEDS, builder);
  31. public static LiteralArgumentBuilder<CommandSource> register() {
  32. return Commands.literal("swing")
  33. .then(Commands.literal("items")
  34. .then(Commands.argument("speed", IntegerArgumentType.integer())
  35. .suggests(SPEED_SUGGESTION)
  36. .executes(context -> changeSwingSpeed(
  37. context.getSource(), IntegerArgumentType.getInteger(context, "speed"), "items"
  38. ))
  39. )
  40. )
  41. .then(Commands.literal("swords")
  42. .then(Commands.argument("speed", IntegerArgumentType.integer())
  43. .suggests(SPEED_SUGGESTION)
  44. .executes(context -> changeSwingSpeed(
  45. context.getSource(), IntegerArgumentType.getInteger(context, "speed"), "swords"
  46. ))
  47. )
  48. )
  49. .then(Commands.literal("tools")
  50. .then(Commands.argument("speed", IntegerArgumentType.integer())
  51. .suggests(SPEED_SUGGESTION)
  52. .executes(context -> changeSwingSpeed(
  53. context.getSource(), IntegerArgumentType.getInteger(context, "speed"), "tools"
  54. ))
  55. )
  56. )
  57. .then(Commands.literal("custom")
  58. .then(Commands.argument("item", ItemArgument.item())
  59. .then(Commands.argument("speed", IntegerArgumentType.integer())
  60. .suggests(SPEED_SUGGESTION)
  61. .executes(context -> addCustomSwing(
  62. context.getSource(),
  63. ItemArgument.getItem(context, "item"),
  64. IntegerArgumentType.getInteger(context, "speed")
  65. ))
  66. )
  67. )
  68. );
  69. }
  70. private static int rangeError(CommandSource source) {
  71. source.sendErrorMessage(ITextComponent.func_244388_a(String.format("Swing speed out of range! [Range: %d ~ %d]", MIN, MAX)));
  72. return 0;
  73. }
  74. private static int changeSwingSpeed(CommandSource source, int speed, String on) {
  75. if (speed < MIN || speed > MAX) {
  76. return rangeError(source);
  77. }
  78. switch (on) {
  79. case "items":
  80. ClientConfig.swing_speed.set(speed);
  81. break;
  82. case "swords":
  83. ClientConfig.sword_speed.set(speed);
  84. break;
  85. case "tools":
  86. ClientConfig.tool_speed.set(speed);
  87. break;
  88. }
  89. ConfigHandler.bake();
  90. final String oldSwing = String.format("%s: %s",
  91. ColorUtil.format("Alpha/Beta Minecraft", TextFormatting.LIGHT_PURPLE),
  92. ColorUtil.format("8", TextFormatting.YELLOW)
  93. );
  94. final String newSwing = String.format("%s: %s",
  95. ColorUtil.format("Modern Minecraft", TextFormatting.LIGHT_PURPLE),
  96. ColorUtil.format("6", TextFormatting.YELLOW)
  97. );
  98. final String info = String.format("%s\n%s\nSuccessfully changed %s swing speed to: %s.",
  99. oldSwing, newSwing, ColorUtil.format(on, TextFormatting.GOLD), ColorUtil.format(Integer.toString(speed), TextFormatting.AQUA));
  100. source.sendFeedback(ITextComponent.func_244388_a(info), true);
  101. return 1;
  102. }
  103. private static int addCustomSwing(CommandSource source, ItemInput itemIn, int speed) {
  104. Item item = itemIn.getItem();
  105. ResourceLocation resource = ForgeRegistries.ITEMS.getKey(item);
  106. if (resource == null) {
  107. source.sendErrorMessage(ITextComponent.func_244388_a("Item [" + itemIn.toString() + "] does not exist in the forge item registry."));
  108. return 0;
  109. } else if (speed < MIN || speed > MAX) {
  110. return rangeError(source);
  111. }
  112. CustomSwing.add(resource.toString(), speed);
  113. ConfigHandler.bake();
  114. final String out = String.format("Successfully added [%s] with speed: %s",
  115. ColorUtil.format(resource.toString(), TextFormatting.GREEN),
  116. ColorUtil.format(String.valueOf(speed), TextFormatting.YELLOW));
  117. source.sendFeedback(ITextComponent.func_244388_a(out), true);
  118. return 1;
  119. }
  120. }