Skip to content

Commit

Permalink
Merge pull request #3 from xap3y/dev
Browse files Browse the repository at this point in the history
Homes added
  • Loading branch information
xap3y authored Apr 16, 2024
2 parents edb5dfc + ec2a4c6 commit e7f1dc6
Show file tree
Hide file tree
Showing 16 changed files with 390 additions and 126 deletions.
10 changes: 9 additions & 1 deletion src/main/java/me/xap3y/xacore/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import me.xap3y.xacore.utils.HookManager
import net.milkbowl.vault.chat.Chat
import net.milkbowl.vault.permission.Permission
import org.apache.logging.log4j.CloseableThreadContext.Instance
import org.bukkit.Location
import org.bukkit.command.CommandSender
import org.bukkit.entity.Player
import org.bukkit.event.Listener
Expand All @@ -27,16 +28,20 @@ class Main : JavaPlugin() {
lateinit var configFile: File
lateinit var messageFile: File
lateinit var logFile: File
lateinit var logFileDebug: File
lateinit var storageFile: File
lateinit var vaultPair: Pair<Chat, Permission>

var chatLocked = false
var useVault = false
var usePapi = false
var debugMode = false

val helper: Helper by lazy { Helper(this) }
val textApi: Texter by lazy { Texter(this) }
val whisperPlayers: HashMap<CommandSender, CommandSender> = hashMapOf()
val cmdSpyToggles: MutableSet<Player> = mutableSetOf()
val playerHomes: HashMap<String, HashMap<String, Location?>> = hashMapOf()


override fun onEnable() {
Expand All @@ -47,11 +52,13 @@ class Main : JavaPlugin() {
configFile = File(dataFolder, "config.yml")
messageFile = File(dataFolder, "lang.yml")
logFile = File(dataFolder, "logs.txt")
// storageFile = File(datafolder, "storage.yml")
logFileDebug = File(dataFolder, "logsDebug.txt")
storageFile = File(dataFolder, "storage.yml")

storageManager = StorageManager(this)
storageManager.loadConfig()
storageManager.loadLang()
storageManager.loadStorage()

useVault = config.getBoolean("hookVault")

Expand Down Expand Up @@ -163,4 +170,5 @@ class Main : JavaPlugin() {
fun isLangFileInit() = ::messageFile.isInitialized
fun isVaultPairInit() = ::vaultPair.isInitialized
fun isLogFileInit() = ::logFile.isInitialized
fun isStorageFileInit() = ::storageFile.isInitialized
}
92 changes: 85 additions & 7 deletions src/main/java/me/xap3y/xacore/api/config/StorageManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,28 @@ package me.xap3y.xacore.api.config

import me.clip.placeholderapi.PlaceholderAPI
import me.xap3y.xacore.Main
import org.bukkit.Location
import org.bukkit.configuration.ConfigurationSection
import org.bukkit.configuration.file.FileConfiguration
import org.bukkit.configuration.file.YamlConfiguration
import org.bukkit.entity.Player

class StorageManager(private val plugin: Main) {

private var langYML: FileConfiguration? = null
var storageYML: FileConfiguration? = null

fun loadConfig() {
if (!plugin.isConfigFileInit()) return
if (!plugin.configFile.exists()) {
plugin.saveResource(plugin.configFile.name, false)
}
plugin.config.load(plugin.configFile)
plugin.debugMode = plugin.config.getBoolean("debug", false)

logInfo(
"[CFG] LOAD > ${plugin.config}",
true)
}

fun loadLang() {
Expand All @@ -24,12 +32,69 @@ class StorageManager(private val plugin: Main) {
plugin.saveResource(plugin.messageFile.name, false)
}
langYML = YamlConfiguration.loadConfiguration(plugin.messageFile)

logInfo(
"[LANG] LOAD > $langYML",
true)
}

fun loadStorage() {
if (!plugin.isStorageFileInit()) return
if (!plugin.storageFile.exists()) {
plugin.storageFile.createNewFile()
}

storageYML = YamlConfiguration.loadConfiguration(plugin.storageFile)

logInfo(
"[STORAGE-YML] LOAD > $storageYML",
true)
}

fun getLocationFromStorage(key: String): Location? {
if (storageYML === null) return null

val selection: ConfigurationSection = storageYML?.getConfigurationSection(key) ?: return null
val world = selection.getString("world") ?: return null
val x = selection.getDouble("x")
val y = selection.getDouble("y")
val z = selection.getDouble("z")
val yaw = selection.getDouble("yaw", 0.0).toFloat()
val pitch = selection.getDouble("pitch", 0.0).toFloat()
val realWorld = plugin.server.getWorld(world) ?: return null

return Location(realWorld, x, y, z, yaw, pitch)
}


fun setLocationToStorage(key: String, location: Location): Boolean {
if (storageYML === null) return false

storageYML?.set("$key.world", location.world.name)
storageYML?.set("$key.x", location.x)
storageYML?.set("$key.y", location.y)
storageYML?.set("$key.z", location.z)
storageYML?.set("$key.yaw", location.yaw)
storageYML?.set("$key.pitch", location.pitch)

try {
storageYML?.save(plugin.storageFile)
return true
} catch (e: Exception) {
logInfo("[ERR-C1] ${e.message} ${e.stackTrace.joinToString("\n")}", true)
}

return false
}

fun getMessage(value: String, default: String, player: Player? = null): String {
var key = langYML?.getString(value) ?: default
val prefix = plugin.config.getString("prefix")

plugin.storageManager.logInfo(
"[STORAGE-MAN] getMessage - VAL: $value DEF: $default P: $player GOT: $key", true
)

key = key.replace("<prefix>", prefix ?: "")
if (plugin.usePapi && player !== null) {
plugin.textApi.console("Converting..")
Expand All @@ -51,17 +116,30 @@ class StorageManager(private val plugin: Main) {
return list.map { it.toString().replace("%", "%%") }.toMutableList()
}

fun logInfo(message: String) {
if (!plugin.isLogFileInit()) return
if (!plugin.config.getBoolean("logToFile", false)) return
fun logInfo(message: String, debug: Boolean = false) {
if (debug) {
if (!plugin.debugMode) return

if (!plugin.logFileDebug.exists())
plugin.logFileDebug.createNewFile()

if (!plugin.logFile.exists())
plugin.logFile.createNewFile()
} else {
if (!plugin.isLogFileInit()) return
if (!plugin.config.getBoolean("logToFile", false)) return

if (!plugin.logFile.exists())
plugin.logFile.createNewFile()
}

plugin.server.scheduler.runTaskAsynchronously(plugin, Runnable {
plugin.logFile.appendText("$message\n")
if (!debug) plugin.logFile.appendText("$message\n")
else {
val time = System.currentTimeMillis()
val date = java.util.Date(time)
val humanDate = date.toString()
plugin.logFileDebug.appendText("[$humanDate] $message\n")
}
})

}

}
4 changes: 4 additions & 0 deletions src/main/java/me/xap3y/xacore/api/text/Texter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ class Texter(private val plugin: Main) {
}
}

plugin.storageManager.logInfo(
"[TEXTAPI-REPLACER] ORIG: $original REG: $regex RES: $result", true
)

if (wPrefix) result = result.replace("<prefix>", plugin.config.getString("prefix") ?: "&7[&6XaCore&7]")
return result
}
Expand Down
77 changes: 77 additions & 0 deletions src/main/java/me/xap3y/xacore/commands/ChatCommands.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package me.xap3y.xacore.commands
import me.xap3y.xacore.Main
import org.bukkit.command.CommandSender
import org.bukkit.entity.Player
import org.incendo.cloud.annotations.Argument
import org.incendo.cloud.annotations.Command
import org.incendo.cloud.annotations.CommandDescription
import org.incendo.cloud.annotations.Permission
Expand Down Expand Up @@ -86,4 +87,80 @@ class ChatCommands(private val plugin: Main) {
default = "<prefix> &fChat cleared!"
)
}

@Command("whisper|msg|tell|w <player> <message>")
@CommandDescription("Send a private message")
fun onWhisperCommand(
commandSender: CommandSender,
@Argument("player") target: Player,
@Argument("message") message: Array<String>
) {
if (target == commandSender)
return plugin.textApi.commandReply(commandSender, "messages.whisperSelf", wPrefix = true)

processWhisper(commandSender, target, message.joinToString(" "))

plugin.whisperPlayers[commandSender] = target
}

@Command("reply|r <message>")
@CommandDescription("Reply to a private message")
fun onReplyCommand(
commandSender: CommandSender,
@Argument("message") message: Array<String>
) {
val target = plugin.whisperPlayers[commandSender]

if (target === null)
return plugin.textApi.commandReply(commandSender, "messages.whisperNobody", default = "<prefix> &cNobody to reply!", wPrefix = true)

else if (target is Player && !target.isOnline) {
plugin.textApi.commandReply(
commandSender,
"messages.whisperOffline",
hashMapOf("player" to target.name),
default = "<prefix> &cNobody to reply!",
wPrefix = true)

plugin.whisperPlayers.remove(commandSender)
return
}

processWhisper(commandSender, target, message.joinToString(" "))
}

private fun processWhisper(from: CommandSender, to: CommandSender, content: String) {
val formats = plugin.helper.getWhisperFormats()

plugin.storageManager.logInfo(
"[WHISPER] FORMAT1: ${formats.first} FORMAT2: ${formats.second}" +
"FROM: $from | TO: $to | CONTENT: $content", true
)
val messageToSend = plugin.textApi.coloredMessage(content)

from.sendMessage(
plugin.textApi.coloredMessage(
plugin.textApi.replace(
formats.second,
hashMapOf("player" to to.name, "message" to messageToSend),
false
)
)
)

to.sendMessage(
plugin.textApi.coloredMessage(
plugin.textApi.replace(
formats.first,
hashMapOf("player" to from.name, "message" to messageToSend),
false
)
)
)

plugin.whisperPlayers[from] = to
plugin.whisperPlayers[to] = from

plugin.storageManager.logInfo("[PM] ${from.name} -> ${to.name} : $content")
}
}
2 changes: 1 addition & 1 deletion src/main/java/me/xap3y/xacore/commands/RootCommand.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class RootCommand(private val plugin: Main) {
@CommandDescription("Reloads the plugin configuration")
@Permission(value = ["xacore.reload", "xacore.*"], mode = Permission.Mode.ANY_OF)
fun onReloadCommand(commandSender: CommandSender) {
plugin.reloadConfig()
plugin.storageManager.loadConfig()
plugin.storageManager.loadLang()

plugin.textApi.commandReply(commandSender, "reloadMessage", wPrefix = true, default = "<prefix> &aConfiguration reloaded!")
Expand Down
Loading

0 comments on commit e7f1dc6

Please sign in to comment.