diff options
Diffstat (limited to 'src')
5 files changed, 79 insertions, 30 deletions
diff --git a/src/main/java/at/hannibal2/skyhanni/api/GetFromSackAPI.kt b/src/main/java/at/hannibal2/skyhanni/api/GetFromSackAPI.kt index 58d03a32f..be1694a86 100644 --- a/src/main/java/at/hannibal2/skyhanni/api/GetFromSackAPI.kt +++ b/src/main/java/at/hannibal2/skyhanni/api/GetFromSackAPI.kt @@ -9,8 +9,13 @@ import at.hannibal2.skyhanni.events.LorenzTickEvent import at.hannibal2.skyhanni.events.LorenzToolTipEvent import at.hannibal2.skyhanni.events.MessageSendToServerEvent import at.hannibal2.skyhanni.events.RepositoryReloadEvent +import at.hannibal2.skyhanni.features.commands.tabcomplete.GetFromSacksTabComplete +import at.hannibal2.skyhanni.test.command.ErrorManager import at.hannibal2.skyhanni.utils.ChatUtils import at.hannibal2.skyhanni.utils.ChatUtils.isCommand +import at.hannibal2.skyhanni.utils.ChatUtils.senderIsSkyhanni +import at.hannibal2.skyhanni.utils.DelayedRun +import at.hannibal2.skyhanni.utils.ItemUtils.itemNameWithoutColor import at.hannibal2.skyhanni.utils.LorenzUtils import at.hannibal2.skyhanni.utils.NEUInternalName import at.hannibal2.skyhanni.utils.NEUInternalName.Companion.asInternalName @@ -68,7 +73,10 @@ object GetFromSackAPI { private var lastItemStack: PrimitiveItemStack? = null - var sackList = emptyList<NEUInternalName>() + var sackListInternalNames = emptySet<String>() + private set + + var sackListNames = emptySet<String>() private set private fun addToQueue(items: List<PrimitiveItemStack>) = queue.addAll(items) @@ -115,13 +123,22 @@ object GetFromSackAPI { if (!LorenzUtils.inSkyBlock) return if (!config.queuedGFS && !config.bazaarGFS) return if (!event.isCommand(commandsWithSlash)) return - queuedHandler(event) - bazaarHandler(event) + val replacedEvent = GetFromSacksTabComplete.handleUnderlineReplace(event) + queuedHandler(replacedEvent) + bazaarHandler(replacedEvent) + if (replacedEvent.isCanceled) { + event.isCanceled = true + return + } + if (replacedEvent !== event) { + event.isCanceled = true + ChatUtils.sendMessageToServer(replacedEvent.message) + } } private fun queuedHandler(event: MessageSendToServerEvent) { if (!config.queuedGFS) return - if (event.originatingModContainer?.modId == "skyhanni") return + if (event.senderIsSkyhanni()) return val (result, stack) = commandValidator(event.splitMessage.drop(1)) @@ -130,6 +147,7 @@ object GetFromSackAPI { CommandResult.WRONG_ARGUMENT -> ChatUtils.userError("Missing arguments! Usage: /getfromsacks <name/id> <amount>") CommandResult.WRONG_IDENTIFIER -> ChatUtils.userError("Couldn't find an item with this name or identifier!") CommandResult.WRONG_AMOUNT -> ChatUtils.userError("Invalid amount!") + CommandResult.INTERNAL_ERROR -> {} } event.isCanceled = true } @@ -146,23 +164,33 @@ object GetFromSackAPI { ) private fun commandValidator(args: List<String>): Pair<CommandResult, PrimitiveItemStack?> { - if (args.size != 2) { + if (args.size <= 1) { return CommandResult.WRONG_ARGUMENT to null } - val item = args[0].asInternalName() + val amountString = args.last() - if (!sackList.contains(item)) { - return CommandResult.WRONG_IDENTIFIER to null + if (!amountString.isInt()) { + return CommandResult.WRONG_AMOUNT to null } - val amountString = args[1] + val itemString = args.dropLast(1).joinToString(" ").uppercase() - if (!amountString.isInt()) { - return CommandResult.WRONG_AMOUNT to null + val item = when { + sackListInternalNames.contains(itemString) -> itemString.asInternalName() + sackListNames.contains(itemString) -> NEUInternalName.fromItemNameOrNull(itemString) ?: run { + ErrorManager.logErrorStateWithData( + "Couldn't resolve item name", + "Query failed", + "itemName" to itemString + ) + return CommandResult.INTERNAL_ERROR to null + } + + else -> return CommandResult.WRONG_IDENTIFIER to null } - return CommandResult.VALID to item.makePrimitiveStack(amountString.toInt()) + return CommandResult.VALID to PrimitiveItemStack(item, amountString.toInt()) } @SubscribeEvent @@ -189,11 +217,17 @@ object GetFromSackAPI { VALID, WRONG_ARGUMENT, WRONG_IDENTIFIER, - WRONG_AMOUNT + WRONG_AMOUNT, + INTERNAL_ERROR } @SubscribeEvent fun onRepoReload(event: RepositoryReloadEvent) { - sackList = event.getConstant<SacksJson>("Sacks").sackList.map { it.replace(" ", "_").asInternalName() } + sackListInternalNames = event.getConstant<SacksJson>("Sacks").sackItems.toSet() + + DelayedRun.runNextTick { + sackListNames = sackListInternalNames.map { it.asInternalName().itemNameWithoutColor.uppercase() }.toSet() + } + //sackListNames = event.getConstant<SacksJson>("Sacks").sackList.map { it.uppercase() }.toSet() } } diff --git a/src/main/java/at/hannibal2/skyhanni/data/ChatManager.kt b/src/main/java/at/hannibal2/skyhanni/data/ChatManager.kt index 8b9ebc0d0..4650bda6d 100644 --- a/src/main/java/at/hannibal2/skyhanni/data/ChatManager.kt +++ b/src/main/java/at/hannibal2/skyhanni/data/ChatManager.kt @@ -93,7 +93,13 @@ object ChatManager { ) messageHistory[IdentityCharacteristics(component)] = result - if (MessageSendToServerEvent(message, message.split(" "), originatingModContainer).postAndCatch()) { + val trimmedMessage = message.trimEnd() + if (MessageSendToServerEvent( + trimmedMessage, + trimmedMessage.split(" "), + originatingModContainer + ).postAndCatch() + ) { event.isCanceled = true messageHistory[IdentityCharacteristics(component)] = result.copy(actionKind = ActionKind.OUTGOING_BLOCKED) } diff --git a/src/main/java/at/hannibal2/skyhanni/data/jsonobjects/repo/SacksJson.java b/src/main/java/at/hannibal2/skyhanni/data/jsonobjects/repo/SacksJson.java index 8a2765210..a2c6dc66e 100644 --- a/src/main/java/at/hannibal2/skyhanni/data/jsonobjects/repo/SacksJson.java +++ b/src/main/java/at/hannibal2/skyhanni/data/jsonobjects/repo/SacksJson.java @@ -5,6 +5,11 @@ import com.google.gson.annotations.Expose; import java.util.List; public class SacksJson { + + @Deprecated @Expose public List<String> sackList; + + @Expose + public List<String> sackItems; } diff --git a/src/main/java/at/hannibal2/skyhanni/features/commands/tabcomplete/GetFromSacksTabComplete.kt b/src/main/java/at/hannibal2/skyhanni/features/commands/tabcomplete/GetFromSacksTabComplete.kt index a777c76b4..424d72198 100644 --- a/src/main/java/at/hannibal2/skyhanni/features/commands/tabcomplete/GetFromSacksTabComplete.kt +++ b/src/main/java/at/hannibal2/skyhanni/features/commands/tabcomplete/GetFromSacksTabComplete.kt @@ -4,11 +4,9 @@ import at.hannibal2.skyhanni.SkyHanniMod import at.hannibal2.skyhanni.api.GetFromSackAPI import at.hannibal2.skyhanni.api.GetFromSackAPI.commands import at.hannibal2.skyhanni.events.MessageSendToServerEvent -import at.hannibal2.skyhanni.utils.ChatUtils -import at.hannibal2.skyhanni.utils.ChatUtils.isCommand +import at.hannibal2.skyhanni.utils.ChatUtils.eventWithNewMessage +import at.hannibal2.skyhanni.utils.ChatUtils.senderIsSkyhanni import at.hannibal2.skyhanni.utils.LorenzUtils -import at.hannibal2.skyhanni.utils.NEUInternalName.Companion.asInternalName -import net.minecraftforge.fml.common.eventhandler.SubscribeEvent object GetFromSacksTabComplete { @@ -18,21 +16,22 @@ object GetFromSacksTabComplete { if (!isEnabled()) return null if (command !in commands) return null - return GetFromSackAPI.sackList.map { it.asString() } + return GetFromSackAPI.sackListNames.map { it.replace(" ", "_") } } - @SubscribeEvent - fun onMessageSendToServer(event: MessageSendToServerEvent) { - if (!isEnabled()) return + //No subscribe since it needs to be called from the GetFromSackAPI + fun handleUnderlineReplace(event: MessageSendToServerEvent): MessageSendToServerEvent { + if (!isEnabled()) return event - if (!event.isCommand(GetFromSackAPI.commandsWithSlash)) return + if (event.senderIsSkyhanni()) return event - val rawName = event.splitMessage[1] - val realName = rawName.asInternalName() - if (realName.asString() == rawName) return - if (realName !in GetFromSackAPI.sackList) return - event.isCanceled = true - ChatUtils.sendMessageToServer(event.message.replace(rawName, realName.asString())) + if (event.splitMessage.isEmpty()) return event + + val rawName = event.splitMessage[1].uppercase() + val realName = rawName.replace("_", " ") + if (realName == rawName) return event + if (realName !in GetFromSackAPI.sackListNames) return event + return event.eventWithNewMessage(event.message.replace(rawName, realName)) } fun isEnabled() = LorenzUtils.inSkyBlock && config.gfsSack diff --git a/src/main/java/at/hannibal2/skyhanni/utils/ChatUtils.kt b/src/main/java/at/hannibal2/skyhanni/utils/ChatUtils.kt index a65da4387..ae04b4535 100644 --- a/src/main/java/at/hannibal2/skyhanni/utils/ChatUtils.kt +++ b/src/main/java/at/hannibal2/skyhanni/utils/ChatUtils.kt @@ -219,4 +219,9 @@ object ChatUtils { fun MessageSendToServerEvent.isCommand(commandsWithSlash: Collection<String>) = splitMessage.takeIf { it.isNotEmpty() }?.get(0) in commandsWithSlash + + fun MessageSendToServerEvent.senderIsSkyhanni() = originatingModContainer?.modId == "skyhanni" + + fun MessageSendToServerEvent.eventWithNewMessage(message: String) = + MessageSendToServerEvent(message, message.split(" "), this.originatingModContainer) } |