Swings.java 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. private static final SuggestionProvider<CommandSource> SPEED_SUGGESTION = (context, builder) ->
  29. ISuggestionProvider.suggest(VALID_SPEEDS, builder);
  30. public static LiteralArgumentBuilder<CommandSource> register() {
  31. return Commands.literal("swing")
  32. .then(Commands.literal("else")
  33. .then(Commands.argument("speed", IntegerArgumentType.integer())
  34. .suggests(SPEED_SUGGESTION)
  35. .executes(context -> changeSwingSpeed(
  36. context.getSource(), IntegerArgumentType.getInteger(context, "speed"), "else"
  37. ))
  38. )
  39. )
  40. .then(Commands.literal("swords")
  41. .then(Commands.argument("speed", IntegerArgumentType.integer())
  42. .suggests(SPEED_SUGGESTION)
  43. .executes(context -> changeSwingSpeed(
  44. context.getSource(), IntegerArgumentType.getInteger(context, "speed"), "swords"
  45. ))
  46. )
  47. )
  48. .then(Commands.literal("tools")
  49. .then(Commands.argument("speed", IntegerArgumentType.integer())
  50. .suggests(SPEED_SUGGESTION)
  51. .executes(context -> changeSwingSpeed(
  52. context.getSource(), IntegerArgumentType.getInteger(context, "speed"), "tools"
  53. ))
  54. )
  55. )
  56. .then(Commands.literal("custom")
  57. .then(Commands.argument("item", ItemArgument.item())
  58. .then(Commands.argument("speed", IntegerArgumentType.integer())
  59. .suggests(SPEED_SUGGESTION)
  60. .executes(context -> addCustomSwing(
  61. context.getSource(),
  62. ItemArgument.getItem(context, "item"),
  63. IntegerArgumentType.getInteger(context, "speed")
  64. ))
  65. )
  66. )
  67. );
  68. }
  69. private static int rangeError(CommandSource source) {
  70. source.sendErrorMessage(ITextComponent.func_244388_a(String.format("Swing speed out of range! [Range: %d ~ %d]", MIN, MAX)));
  71. return 0;
  72. }
  73. private static int changeSwingSpeed(CommandSource source, int speed, String on) {
  74. if (speed < MIN || speed > MAX)
  75. return rangeError(source);
  76. switch (on) {
  77. case "else":
  78. ClientConfig.swing_speed.set(speed);
  79. break;
  80. case "swords":
  81. ClientConfig.sword_speed.set(speed);
  82. break;
  83. case "tools":
  84. ClientConfig.tool_speed.set(speed);
  85. break;
  86. }
  87. ConfigHandler.bake();
  88. final String oldSwing = String.format("%s: %s",
  89. ColorUtil.format("Alpha/Beta Minecraft", TextFormatting.LIGHT_PURPLE),
  90. ColorUtil.format("8", TextFormatting.YELLOW)
  91. );
  92. final String newSwing = String.format("%s: %s",
  93. ColorUtil.format("Modern Minecraft", TextFormatting.LIGHT_PURPLE),
  94. ColorUtil.format("6", TextFormatting.YELLOW)
  95. );
  96. if (on.equals("else")) {
  97. on = "everything else";
  98. }
  99. final String info = String.format("%s\n%s\nSuccessfully changed %s swing speed to: %s.",
  100. oldSwing, newSwing, ColorUtil.format(on, TextFormatting.GOLD), ColorUtil.format(Integer.toString(speed), TextFormatting.AQUA));
  101. source.sendFeedback(ITextComponent.func_244388_a(info), true);
  102. return 1;
  103. }
  104. private static int addCustomSwing(CommandSource source, ItemInput itemIn, int speed) {
  105. Item item = itemIn.getItem();
  106. ResourceLocation resource = ForgeRegistries.ITEMS.getKey(item);
  107. if (resource == null) {
  108. source.sendErrorMessage(ITextComponent.func_244388_a("Item [" + itemIn.toString() + "] does not exist in the forge item registry."));
  109. return 0;
  110. } else if (speed < MIN || speed > MAX) {
  111. return rangeError(source);
  112. }
  113. CustomSwing.add(resource.toString(), speed);
  114. ConfigHandler.bake();
  115. final String out = String.format("Successfully added [%s] with speed: %s",
  116. ColorUtil.format(resource.toString(), TextFormatting.GREEN),
  117. ColorUtil.format(String.valueOf(speed), TextFormatting.YELLOW));
  118. source.sendFeedback(ITextComponent.func_244388_a(out), true);
  119. return 1;
  120. }
  121. }