Skip to content

Make AutoExcuse external only #409

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jan 8, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 69 additions & 29 deletions src/main/kotlin/com/lambda/client/module/modules/chat/AutoExcuse.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.lambda.client.module.modules.chat

import com.lambda.client.LambdaMod
import com.lambda.client.commons.extension.synchronized
import com.lambda.client.event.SafeClientEvent
import com.lambda.client.event.events.PacketEvent
import com.lambda.client.module.Category
Expand All @@ -10,27 +11,43 @@ import com.lambda.client.util.TickTimer
import com.lambda.client.util.TimeUnit
import com.lambda.client.util.text.MessageSendHelper
import com.lambda.client.util.text.MessageSendHelper.sendServerMessage
import com.lambda.client.util.threads.defaultScope
import com.lambda.client.util.threads.safeListener
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import net.minecraft.init.Items
import net.minecraft.network.play.server.SPacketUpdateHealth
import net.minecraft.util.EnumHand
import java.io.File
import kotlin.random.Random

object AutoExcuse : Module(
name = "AutoExcuse",
description = "Makes an excuse for you when you die",
category = Category.CHAT,
modulePriority = 500
) {
private val mode by setting("Mode", Mode.INTERNAL)
private val modeSetting by setting("Order", Mode.RANDOM_ORDER)
private val modeSource by setting("Source", SourceMode.INTERNAL)

private val file = File(FolderUtils.lambdaFolder + "excuses.txt")
private val loadedExcuses = ArrayList<String>().synchronized()
private var currentLine = 0

private val timer = TickTimer(TimeUnit.SECONDS)

private enum class Mode {
IN_ORDER, RANDOM_ORDER
}

private enum class SourceMode {
INTERNAL, EXTERNAL
}

private const val CLIENT_NAME = "%CLIENT%"

private val defaultExcuses = arrayOf(
"Sorry, im using $CLIENT_NAME client",
"Sorry, im using $CLIENT_NAME",
"My ping is so bad",
"I was changing my config :(",
"Why did my AutoTotem break",
Expand All @@ -39,10 +56,10 @@ object AutoExcuse : Module(
"Wow, so many try hards",
"Lagggg",
"I wasn't trying",
"I'm not using $CLIENT_NAME client",
"I'm not using $CLIENT_NAME",
"Thers to much lag",
"My dog ate my pc",
"Sorry, $CLIENT_NAME Client is really bad",
"Sorry, $CLIENT_NAME is really bad",
"I was lagging",
"He was cheating!",
"Your hacking!",
Expand All @@ -52,54 +69,77 @@ object AutoExcuse : Module(
"My wifi went down",
"I'm playing vanila",
"My optifine didn't work",
"The CPU cheated!"
"The CPU cheated!",
"I am using a cracked client",
"My brother was playing.",
"Phobos hacked my pc!!",
"I didn't have enough totems",
"I died for you <3",
"I was trying the popbob exploit!!",
"Sorry, let me relog with ${LambdaMod.NAME}",
"I was alt tabbing",
"I was trying out a new mod",
)

private val file = File(FolderUtils.lambdaFolder + "excuses.txt")
private var loadedExcuses = defaultExcuses

private val clients = arrayOf(
"Future",
"Future Client",
"Salhack",
"Pyro",
"Impact"
)

private val timer = TickTimer(TimeUnit.SECONDS)

init {
safeListener<PacketEvent.Receive> {
if (loadedExcuses.isEmpty() || it.packet !is SPacketUpdateHealth) return@safeListener
if (it.packet.health <= 0.0f && !isHoldingTotem && timer.tick(3L)) {
sendServerMessage(getExcuse())
val message = if (modeSetting == Mode.IN_ORDER) getOrdered() else getRandom()
sendServerMessage(message.replace("%CLIENT%", clients.random()))
}
}

onEnable {
loadedExcuses = if (mode == Mode.EXTERNAL) {
if (file.exists()) {
val cacheList = ArrayList<String>()
try {
file.forEachLine { if (it.isNotBlank()) cacheList.add(it.trim()) }
MessageSendHelper.sendChatMessage("$chatName Loaded spammer messages!")
} catch (e: Exception) {
LambdaMod.LOG.error("Failed loading excuses", e)
loadedExcuses.clear()
currentLine = 0

when (modeSource) {
SourceMode.INTERNAL -> loadedExcuses.addAll(defaultExcuses)
SourceMode.EXTERNAL -> {
defaultScope.launch(Dispatchers.IO) {
if (!file.exists()) {
file.createNewFile()
MessageSendHelper.sendErrorMessage("$chatName Excuses file is empty!" +
", please add them in the &7excuses.txt&f under the &7.minecraft/lambda&f directory.")
disable()
return@launch
}

try {
file.forEachLine { if (it.isNotBlank()) loadedExcuses.add(it.trim()) }
MessageSendHelper.sendChatMessage("$chatName Loaded excuse messages!")
} catch (e: Exception) {
MessageSendHelper.sendErrorMessage("$chatName Failed loading excuses, $e")
disable()
}
}
cacheList.toTypedArray()
} else {
file.createNewFile()
MessageSendHelper.sendErrorMessage("$chatName Excuses file is empty!" +
", please add them in the &7excuses.txt&f under the &7.minecraft/lambda&f directory.")
defaultExcuses
}
} else {
defaultExcuses
}
}
}

private val SafeClientEvent.isHoldingTotem: Boolean
get() = EnumHand.values().any { player.getHeldItem(it).item == Items.TOTEM_OF_UNDYING }

private fun getExcuse() = loadedExcuses.random().replace(CLIENT_NAME, clients.random())
private fun getOrdered(): String {
currentLine %= loadedExcuses.size
return loadedExcuses[currentLine++]
}

private fun getRandom(): String {
val prevLine = currentLine
// Avoids sending the same message
while (loadedExcuses.size != 1 && currentLine == prevLine) {
currentLine = Random.nextInt(loadedExcuses.size)
}
return loadedExcuses[currentLine]
}
}