Skip to content

Commit

Permalink
- Added a way to disable specific Auto Advert ads (add '//' before th…
Browse files Browse the repository at this point in the history
…e ad in your ad file, or run '/advert ads toggle <index>')

- Fixed unenchantall not detecting enchantable items properly
- Removed 'excludeFrost' setting from /unenchantall command (doesn't apply)
- Fixed command completions not working for /enchantall
  • Loading branch information
hashalite committed May 14, 2023
1 parent 77621a0 commit 466f2ae
Show file tree
Hide file tree
Showing 27 changed files with 346 additions and 116 deletions.
48 changes: 24 additions & 24 deletions README.md

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ mc_version=1.19.4
yarn_mappings=1.19.4+build.2
loader_version=0.14.19
# Mod Properties
mod_version=1.1.4
mod_version=1.1.5
maven_group=net.xolt
archives_base_name=sbutils
# Dependencies
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/net/xolt/sbutils/SbUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,14 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;

public class SbUtils implements ClientModInitializer {

public static final Logger LOGGER = LoggerFactory.getLogger("sbutils");
public static final MinecraftClient MC = MinecraftClient.getInstance();
public static final List<String> commands = new ArrayList<>();

public static KeyBinding configKey;
public static KeyBinding islandKey;
Expand Down Expand Up @@ -56,6 +60,8 @@ public void onInitializeClient() {
Centered.registerCommand(dispatcher);
DeathCoords.registerCommand(dispatcher);
EventNotifier.registerCommand(dispatcher);


});

registerKeybindings();
Expand Down
11 changes: 9 additions & 2 deletions src/main/java/net/xolt/sbutils/features/AntiPlace.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,24 @@
import net.minecraft.util.Formatting;
import net.minecraft.util.Hand;
import net.minecraft.util.hit.BlockHitResult;
import net.xolt.sbutils.SbUtils;
import net.xolt.sbutils.config.ModConfig;
import net.xolt.sbutils.util.Messenger;

import java.util.List;

import static net.xolt.sbutils.SbUtils.MC;

public class AntiPlace {

private static final String COMMAND = "antiplace";
private static final String ALIAS = "noplace";

private static long lastMessageSentAt;

public static void registerCommand(CommandDispatcher<FabricClientCommandSource> dispatcher) {
LiteralCommandNode<FabricClientCommandSource> antiPlaceNode = dispatcher.register(ClientCommandManager.literal("antiplace")
SbUtils.commands.addAll(List.of(COMMAND, ALIAS));
final LiteralCommandNode<FabricClientCommandSource> antiPlaceNode = dispatcher.register(ClientCommandManager.literal(COMMAND)
.then(ClientCommandManager.literal("heads")
.executes(context -> {
Messenger.printSetting("text.sbutils.config.option.antiPlaceHeads", ModConfig.INSTANCE.getConfig().antiPlaceHeads);
Expand All @@ -49,7 +56,7 @@ public static void registerCommand(CommandDispatcher<FabricClientCommandSource>
return Command.SINGLE_SUCCESS;
}))));

dispatcher.register(ClientCommandManager.literal("noplace")
dispatcher.register(ClientCommandManager.literal(ALIAS)
.executes(context ->
dispatcher.execute("antiplace", context.getSource())
)
Expand Down
127 changes: 99 additions & 28 deletions src/main/java/net/xolt/sbutils/features/AutoAdvert.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource;
import net.minecraft.client.gui.screen.DownloadingTerrainScreen;
import net.minecraft.util.Formatting;
import net.xolt.sbutils.SbUtils;
import net.xolt.sbutils.config.ModConfig;
import net.xolt.sbutils.features.common.ServerDetector;
import net.xolt.sbutils.util.IOHandler;
Expand All @@ -21,13 +22,18 @@
import static net.xolt.sbutils.SbUtils.MC;

public class AutoAdvert {

private static final String COMMAND = "autoadvert";
private static final String ALIAS = "advert";

private static List<String> prevAdList;
private static int adIndex;
private static long lastAdSentAt;
private static long joinedAt;

public static void registerCommand(CommandDispatcher<FabricClientCommandSource> dispatcher) {
final LiteralCommandNode<FabricClientCommandSource> autoAdvertNode = dispatcher.register(ClientCommandManager.literal("autoadvert")
SbUtils.commands.addAll(List.of(COMMAND, ALIAS));
final LiteralCommandNode<FabricClientCommandSource> autoAdvertNode = dispatcher.register(ClientCommandManager.literal(COMMAND)
.executes(context -> {
ModConfig.INSTANCE.getConfig().autoAdvert = !ModConfig.INSTANCE.getConfig().autoAdvert;
ModConfig.INSTANCE.save();
Expand Down Expand Up @@ -75,25 +81,31 @@ public static void registerCommand(CommandDispatcher<FabricClientCommandSource>
Messenger.printChangedSetting("text.sbutils.config.option.classicAdFile", ModConfig.INSTANCE.getConfig().classicAdFile);
return Command.SINGLE_SUCCESS;
})))
.then(ClientCommandManager.literal("list")
.executes(context ->
onListCommand()
))
.then(ClientCommandManager.literal("add")
.then(ClientCommandManager.argument("advert", StringArgumentType.greedyString())
.executes(context ->
onAddCommand(StringArgumentType.getString(context, "advert"))
)))
.then(ClientCommandManager.literal("del")
.then(ClientCommandManager.argument("index", IntegerArgumentType.integer())
.then(ClientCommandManager.literal("ads")
.then(ClientCommandManager.literal("list")
.executes(context ->
onDelCommand(IntegerArgumentType.getInteger(context, "index"))
)))
.then(ClientCommandManager.literal("insert")
.then(ClientCommandManager.argument("index", IntegerArgumentType.integer())
onListCommand()
))
.then(ClientCommandManager.literal("add")
.then(ClientCommandManager.argument("advert", StringArgumentType.greedyString())
.executes(context ->
onInsertCommand(IntegerArgumentType.getInteger(context, "index"), StringArgumentType.getString(context, "advert"))
onAddCommand(StringArgumentType.getString(context, "advert"))
)))
.then(ClientCommandManager.literal("del")
.then(ClientCommandManager.argument("index", IntegerArgumentType.integer())
.executes(context ->
onDelCommand(IntegerArgumentType.getInteger(context, "index"))
)))
.then(ClientCommandManager.literal("insert")
.then(ClientCommandManager.argument("index", IntegerArgumentType.integer())
.then(ClientCommandManager.argument("advert", StringArgumentType.greedyString())
.executes(context ->
onInsertCommand(IntegerArgumentType.getInteger(context, "index"), StringArgumentType.getString(context, "advert"))
))))
.then(ClientCommandManager.literal("toggle")
.then(ClientCommandManager.argument("index", IntegerArgumentType.integer())
.executes(context ->
onToggleCommand(IntegerArgumentType.getInteger(context, "index"))
))))
.then(ClientCommandManager.literal("delay")
.executes(context -> {
Expand Down Expand Up @@ -149,7 +161,7 @@ public static void registerCommand(CommandDispatcher<FabricClientCommandSource>
return Command.SINGLE_SUCCESS;
})));

dispatcher.register(ClientCommandManager.literal("advert")
dispatcher.register(ClientCommandManager.literal(ALIAS)
.executes(context ->
dispatcher.execute("autoadvert", context.getSource())
)
Expand Down Expand Up @@ -186,14 +198,17 @@ private static int onDelCommand(int index) {
Messenger.printMessage("message.sbutils.autoAdvert.notOnSkyblock");
return Command.SINGLE_SUCCESS;
}

String adFile = getAdFile();
List<String> adverts = getAdList();
if (index - 1 < 0 || index - 1 >= adverts.size()) {

int adjustedIndex = index - 1;
if (adjustedIndex < 0 || adjustedIndex >= adverts.size()) {
Messenger.printMessage("message.sbutils.autoAdvert.invalidIndex");
return Command.SINGLE_SUCCESS;
}

adverts.remove(index - 1);
adverts.remove(adjustedIndex);
IOHandler.writeAdverts(adverts, adFile);

Messenger.printListSetting("message.sbutils.autoAdvert.deleteSuccess", formatAdList(adverts));
Expand All @@ -206,21 +221,52 @@ private static int onInsertCommand(int index, String advert) {
Messenger.printMessage("message.sbutils.autoAdvert.notOnSkyblock");
return Command.SINGLE_SUCCESS;
}

String adFile = getAdFile();
List<String> adverts = getAdList();
if (index - 1 < 0 || index - 1 > adverts.size()) {

int adjustedIndex = index - 1;
if (adjustedIndex < 0 || adjustedIndex >= adverts.size()) {
Messenger.printMessage("message.sbutils.autoAdvert.invalidIndex");
return Command.SINGLE_SUCCESS;
}

adverts.add(index - 1, advert);
adverts.add(adjustedIndex, advert);
IOHandler.writeAdverts(adverts, adFile);

Messenger.printListSetting("message.sbutils.autoAdvert.addSuccess", formatAdList(adverts));

return Command.SINGLE_SUCCESS;
}

private static int onToggleCommand(int index) {
if (ServerDetector.currentServer == null) {
Messenger.printMessage("message.sbutils.autoAdvert.notOnSkyblock");
return Command.SINGLE_SUCCESS;
}

String adFile = getAdFile();
List<String> adverts = getAdList();

int adjustedIndex = index - 1;
if (adjustedIndex < 0 || adjustedIndex >= adverts.size()) {
Messenger.printMessage("message.sbutils.autoAdvert.invalidIndex");
return Command.SINGLE_SUCCESS;
}

String currentValue = adverts.get(adjustedIndex);
if (currentValue.startsWith("//")) {
adverts.set(adjustedIndex, currentValue.substring(2));
} else {
adverts.set(adjustedIndex, "//" + currentValue);
}

IOHandler.writeAdverts(adverts, adFile);

Messenger.printListSetting("message.sbutils.autoAdvert.toggleSuccess", formatAdList(adverts));
return Command.SINGLE_SUCCESS;
}

private static int onAddUserCommand(String user) {
List<String> whitelist = new ArrayList<>(ModConfig.INSTANCE.getConfig().advertWhitelist);

Expand Down Expand Up @@ -269,7 +315,7 @@ public static void tick() {
}

List<String> newAdList = getAdList();
if (newAdList.size() == 0) {
if (findNextAd(newAdList, -1) == -1) {
ModConfig.INSTANCE.getConfig().autoAdvert = false;
ModConfig.INSTANCE.save();
reset();
Expand All @@ -281,7 +327,6 @@ public static void tick() {
prevAdList = newAdList;
sendAd();
lastAdSentAt = System.currentTimeMillis();
adIndex = (adIndex + 1) % prevAdList.size();
}

public static void onJoinGame() {
Expand All @@ -294,16 +339,32 @@ public static void onJoinGame() {

private static int getUpdatedAdIndex(List<String> newAdList) {
if (newAdList == null || prevAdList == null || prevAdList.size() != newAdList.size()) {
return 0;
return findNextAd(newAdList, -1);
}

for (int i = 0; i < prevAdList.size(); i++) {
if (!prevAdList.get(i).equals(newAdList.get(i))) {
return 0;
return findNextAd(newAdList, -1);
}
}

return findNextAd(newAdList, adIndex);
}

private static int findNextAd(List<String> newAdList, int index) {
if (newAdList == null) {
return -1;
}

int startIndex = index + 1;
for (int i = 0; i < newAdList.size(); i++) {
int currentIndex = (startIndex + i) % newAdList.size();
if (!newAdList.get(currentIndex).startsWith("//")) {
return currentIndex;
}
}

return adIndex;
return -1;
}

private static List<String> getAdList() {
Expand All @@ -322,7 +383,17 @@ private static List<String> getAdList() {
}

private static List<String> formatAdList(List<String> ads) {
return ads.stream().map((ad) -> ad.replaceAll("&([0-9a-fk-or])", Formatting.FORMATTING_CODE_PREFIX + "$1")).toList();
List<String> commentsFormatted = ads.stream().map((ad) -> {
if (ad.startsWith("//")) {
ad = ad.substring(2);
ad = ad.replaceAll("&([0-9a-fk-or])", "");
return Formatting.FORMATTING_CODE_PREFIX + "7" + Formatting.FORMATTING_CODE_PREFIX + "m" + ad;
} else {
return ad;
}
}).toList();

return commentsFormatted.stream().map((ad) -> ad.replaceAll("&([0-9a-fk-or])", Formatting.FORMATTING_CODE_PREFIX + "$1")).toList();
}

private static String getAdFile() {
Expand Down
11 changes: 9 additions & 2 deletions src/main/java/net/xolt/sbutils/features/AutoCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,24 @@
import com.mojang.brigadier.tree.LiteralCommandNode;
import net.fabricmc.fabric.api.client.command.v2.ClientCommandManager;
import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource;
import net.xolt.sbutils.SbUtils;
import net.xolt.sbutils.config.ModConfig;
import net.xolt.sbutils.util.Messenger;

import java.util.List;

import static net.xolt.sbutils.SbUtils.MC;

public class AutoCommand {

private static final String COMMAND = "autocmd";
private static final String ALIAS = "acmd";

private static long lastCmdSentAt;

public static void registerCommand(CommandDispatcher<FabricClientCommandSource> dispatcher) {
LiteralCommandNode<FabricClientCommandSource> autoCommandNode = dispatcher.register(ClientCommandManager.literal("autocmd")
SbUtils.commands.addAll(List.of(COMMAND, ALIAS));
final LiteralCommandNode<FabricClientCommandSource> autoCommandNode = dispatcher.register(ClientCommandManager.literal(COMMAND)
.executes(context -> {
ModConfig.INSTANCE.getConfig().autoCommandEnabled = !ModConfig.INSTANCE.getConfig().autoCommandEnabled;
ModConfig.INSTANCE.save();
Expand Down Expand Up @@ -54,7 +61,7 @@ public static void registerCommand(CommandDispatcher<FabricClientCommandSource>
return Command.SINGLE_SUCCESS;
}))));

dispatcher.register(ClientCommandManager.literal("acmd")
dispatcher.register(ClientCommandManager.literal(ALIAS)
.executes(context ->
dispatcher.execute("autocmd", context.getSource())
)
Expand Down
10 changes: 8 additions & 2 deletions src/main/java/net/xolt/sbutils/features/AutoFix.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,22 @@
import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource;
import net.minecraft.item.ItemStack;
import net.minecraft.text.Text;
import net.xolt.sbutils.SbUtils;
import net.xolt.sbutils.config.ModConfig;
import net.xolt.sbutils.util.InvUtils;
import net.xolt.sbutils.util.Messenger;
import net.xolt.sbutils.util.RegexFilters;

import java.util.List;
import java.util.regex.Matcher;

import static net.xolt.sbutils.SbUtils.MC;

public class AutoFix {

private static final String COMMAND = "autofix";
private static final String ALIAS = "af";

private static boolean enabled;
private static boolean fixing;
private static boolean waitingForResponse;
Expand All @@ -37,7 +42,8 @@ public static void init() {
}

public static void registerCommand(CommandDispatcher<FabricClientCommandSource> dispatcher) {
LiteralCommandNode<FabricClientCommandSource> autoFixNode = dispatcher.register(ClientCommandManager.literal("autofix")
SbUtils.commands.addAll(List.of(COMMAND, ALIAS));
final LiteralCommandNode<FabricClientCommandSource> autoFixNode = dispatcher.register(ClientCommandManager.literal(COMMAND)
.executes(context -> {
ModConfig.INSTANCE.getConfig().autoFix = !ModConfig.INSTANCE.getConfig().autoFix;
ModConfig.INSTANCE.save();
Expand Down Expand Up @@ -117,7 +123,7 @@ public static void registerCommand(CommandDispatcher<FabricClientCommandSource>
return Command.SINGLE_SUCCESS;
}))));

dispatcher.register(ClientCommandManager.literal("af")
dispatcher.register(ClientCommandManager.literal(ALIAS)
.executes(context ->
dispatcher.execute("autofix", context.getSource())
)
Expand Down
Loading

0 comments on commit 466f2ae

Please sign in to comment.