Skip to content

Commit

Permalink
feat: Add block check, toggle check, distance check
Browse files Browse the repository at this point in the history
  • Loading branch information
half-nothing committed Sep 22, 2024
1 parent d86991d commit 279baf9
Show file tree
Hide file tree
Showing 22 changed files with 235 additions and 56 deletions.
11 changes: 6 additions & 5 deletions src/main/kotlin/net/superricky/tpaplusplus/TpaPlusPlus.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,17 @@ import net.superricky.tpaplusplus.async.AsyncCommandHelper
import net.superricky.tpaplusplus.command.CommandRegister
import net.superricky.tpaplusplus.config.AdvancedSpec
import net.superricky.tpaplusplus.config.Config
import net.superricky.tpaplusplus.database.DataService
import net.superricky.tpaplusplus.database.PlayerDataManager
import net.superricky.tpaplusplus.database.service.DataService
import net.superricky.tpaplusplus.database.DataManager
import java.nio.file.Files
import kotlin.coroutines.CoroutineContext
import net.superricky.tpaplusplus.event.PlayerEvent as PlayerEventListener
import net.superricky.tpaplusplus.utility.PlayerEvent as PlayerEventListener

object TpaPlusPlus : ModInitializer, CoroutineScope {
lateinit var server: MinecraftServer
override val coroutineContext: CoroutineContext = Dispatchers.IO
val version: Version = FabricLoader.getInstance().getModContainer(MOD_ID).get().metadata.version
var dataService: DataService = PlayerDataManager
lateinit var dataService: DataService

override fun onInitialize() {
logger.info("Initializing TPA++ ${version.friendlyString}")
Expand Down Expand Up @@ -76,6 +76,7 @@ object TpaPlusPlus : ModInitializer, CoroutineScope {
logger.info("Server starting...")
logger.info("Main logic initializing...")
this.server = server
this.dataService = DataManager

logger.info("Data service initializing...")
dataService.initDataService()
Expand All @@ -102,6 +103,6 @@ object TpaPlusPlus : ModInitializer, CoroutineScope {
logger.info("Shutting down TPA++")
AsyncCommandHelper.stopTickLoop()
coroutineContext.cancel()
dataService.savePlayerData()
dataService.saveData()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import kotlinx.atomicfu.atomic
import kotlinx.coroutines.*
import net.minecraft.server.network.ServerPlayerEntity
import net.superricky.tpaplusplus.GlobalConst.ONE_SECOND
import net.superricky.tpaplusplus.GlobalConst.logger
import net.superricky.tpaplusplus.config.CommonSpec
import net.superricky.tpaplusplus.config.Config
import java.util.*
Expand All @@ -26,7 +27,13 @@ object AsyncCommandHelper : CoroutineScope {
tickDelay = ONE_SECOND / tickRate
mainLoopJob = launch {
while (isActive && ticking.value) {
runTick()
launch {
try {
runTick()
} catch (e: Exception) {
logger.error(e)
}
}
delay(tickDelay)
}
}
Expand All @@ -49,7 +56,19 @@ object AsyncCommandHelper : CoroutineScope {
playerData[type] = type.handler.getCooldownTime() * tickRate
}

fun asyncWindupCheck(
fun checkRequestExist(
sender: ServerPlayerEntity,
receiver: ServerPlayerEntity,
commandType: AsyncCommandType
): Boolean {
return requests.find {
it.getRequest().sender == sender &&
it.getRequest().receiver == receiver &&
it.getRequest().commandType == commandType
} != null
}

private fun asyncWindupCheck(
asyncCommandData: AsyncCommandData,
successCallback: Function1<AsyncCommandData, Unit>? = null,
errorCallback: Function1<AsyncCommandData, Unit>? = null,
Expand All @@ -76,12 +95,13 @@ object AsyncCommandHelper : CoroutineScope {
}
delayTime -= 1
asyncCommandData.updateDelay(delayTime)
if (delayTime < 1.0) break
progressCallback?.invoke(asyncCommandData)
if (delayTime < 1.0) {
break
}
}
delay((delayTime * ONE_SECOND).toLong())
if (delayTime != 0.0) {
progressCallback?.invoke(asyncCommandData)
delay((delayTime * ONE_SECOND).toLong())
}
if (asyncCommandData.isCanceled()) {
return@launch
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ package net.superricky.tpaplusplus.command
import net.minecraft.command.argument.EntityArgumentType
import net.minecraft.server.network.ServerPlayerEntity
import net.minecraft.text.Text
import net.superricky.tpaplusplus.TpaPlusPlus
import net.superricky.tpaplusplus.utility.Context
import net.superricky.tpaplusplus.utility.TextColorPallet
import net.superricky.tpaplusplus.utility.sendMessageWithPlayerName

object CommandHelper {

Expand Down Expand Up @@ -51,4 +53,30 @@ object CommandHelper {
}
return CommandResult.NORMAL
}

fun checkBlocked(sender: ServerPlayerEntity, receiver: ServerPlayerEntity): Boolean {
if (TpaPlusPlus.dataService.checkPlayerBlocked(receiver.uuid, sender.uuid)) {
sender.sendMessageWithPlayerName("command.error.request.blocked.sender", receiver)
return true
}
if (TpaPlusPlus.dataService.checkPlayerBlocked(sender.uuid, receiver.uuid)) {
sender.sendMessageWithPlayerName("command.error.request.blocked.receiver", receiver)
return true
}
return false
}

fun checkToggled(sender: ServerPlayerEntity, receiver: ServerPlayerEntity): Boolean {
if (TpaPlusPlus.dataService.checkPlayerToggle(sender.uuid)) {
sender.sendMessage(
Text.translatable("command.error.request.toggled.sender").setStyle(TextColorPallet.error)
)
return true
}
if (TpaPlusPlus.dataService.checkPlayerToggle(receiver.uuid)) {
sender.sendMessageWithPlayerName("command.error.request.toggled.receiver", receiver)
return true
}
return false
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ object ToggleCommand : AsyncCommand(), BuildableCommand {
val sender = source.player
sender ?: return CommandResult.SENDER_NOT_EXIST.status
TpaPlusPlus.launch {
val blocked = TpaPlusPlus.dataService.playerSwitchBlock(sender.uuid)
val blocked = TpaPlusPlus.dataService.playerSwitchToggle(sender.uuid)
if (blocked) {
source.sendFeedback({ Text.translatable("command.toggle.success.on") }, false)
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import net.minecraft.server.command.CommandManager.literal
import net.minecraft.text.Text
import net.superricky.tpaplusplus.async.*
import net.superricky.tpaplusplus.command.BuildableCommand
import net.superricky.tpaplusplus.command.CommandHelper
import net.superricky.tpaplusplus.command.CommandHelper.checkSenderReceiver
import net.superricky.tpaplusplus.command.CommandResult
import net.superricky.tpaplusplus.config.Config
Expand Down Expand Up @@ -92,6 +93,20 @@ object TpaCommand : AsyncCommand(), BuildableCommand {
if (result != CommandResult.NORMAL) return result.status
sender!!
target!!
if (CommandHelper.checkToggled(sender, target) ||
CommandHelper.checkBlocked(sender, target)
) {
return CommandResult.NORMAL.status
}
if (AsyncCommandHelper.checkRequestExist(sender, target, AsyncCommandType.TPA)) {
sender.sendMessageWithPlayerName("command.error.request.exist", target)
return CommandResult.NORMAL.status
}
val limitResult = LimitationHelper.checkLimitation(sender, target)
limitResult?.let {
sender.sendMessage(limitResult)
return CommandResult.NORMAL.status
}
val asyncCommandData = AsyncCommandData(
AsyncRequest(sender, target, AsyncCommandType.TPA, sender, target),
LevelBoundVec3(sender.getDimension(), sender.pos),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ object ColorSpec : ConfigSpec("common.color") {
val secondary by required<String>()
val secondaryVariant by required<String>()
val error by required<String>()
val errorVariant by required<String>()
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ import net.superricky.tpaplusplus.GlobalConst.PLAYER_DATA_FILE_NAME
import net.superricky.tpaplusplus.GlobalConst.logger
import net.superricky.tpaplusplus.TpaPlusPlus
import net.superricky.tpaplusplus.config.AdvancedSpec
import net.superricky.tpaplusplus.config.CommonSpec
import net.superricky.tpaplusplus.config.Config
import net.superricky.tpaplusplus.database.service.DataService
import net.superricky.tpaplusplus.utility.LevelBoundVec3
import java.io.File
import java.io.FileReader
Expand All @@ -20,7 +22,7 @@ import java.io.IOException
import java.nio.file.Files
import java.util.*

object PlayerDataManager : DataService {
object DataManager : DataService {
private var playerDataMap: MutableMap<UUID, PlayerData> = Collections.synchronizedMap(HashMap())
private val gson = GsonBuilder().setPrettyPrinting().create()
private val dataSavePath: File = Config.getDatabasePath().resolve(PLAYER_DATA_FILE_NAME).toFile()
Expand All @@ -39,7 +41,7 @@ object PlayerDataManager : DataService {
loadPlayerData()
TpaPlusPlus.launch {
while (isActive) {
savePlayerData()
saveData()
delay(Config.getConfig()[AdvancedSpec.autoSaveInterval] * ONE_SECOND)
}
}
Expand All @@ -66,15 +68,15 @@ object PlayerDataManager : DataService {
return false
}

override fun playerSwitchBlock(uuid: UUID): Boolean {
override fun playerSwitchToggle(uuid: UUID): Boolean {
if (playerDataMap.containsKey(uuid) && playerDataMap[uuid] != null) {
playerDataMap[uuid]!!.toggle = !playerDataMap[uuid]!!.toggle
return playerDataMap[uuid]!!.toggle
}
return false
}

override fun playerSwitchBlock(uuid: UUID, toggle: Boolean): Boolean {
override fun playerSwitchToggle(uuid: UUID, toggle: Boolean): Boolean {
if (playerDataMap.containsKey(uuid) && playerDataMap[uuid] != null) {
playerDataMap[uuid]!!.toggle = toggle
return true
Expand All @@ -97,7 +99,13 @@ object PlayerDataManager : DataService {
playerData.lastDeathPos.backed = false
}

override fun savePlayerData() {
override fun checkPlayerBlocked(srcUUID: UUID, destUuid: UUID): Boolean =
playerDataMap[srcUUID]!!.blockPlayers.contains(destUuid)

override fun checkPlayerToggle(uuid: UUID): Boolean =
playerDataMap[uuid]!!.toggle && !Config.getConfig()[CommonSpec.toggledPlayerCommand]

override fun saveData() {
try {
FileWriter(dataSavePath).use {
gson.toJson(playerDataMap, it)
Expand Down
17 changes: 0 additions & 17 deletions src/main/kotlin/net/superricky/tpaplusplus/database/DataService.kt

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package net.superricky.tpaplusplus.database.service

import java.util.*

interface BlockDataService {
fun checkPlayerBlocked(srcUUID: UUID, destUuid: UUID): Boolean
fun addBlockPlayer(uuid: UUID, blockPlayer: UUID): Boolean
fun removeBlockPlayer(uuid: UUID, blockPlayer: UUID): Boolean
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package net.superricky.tpaplusplus.database.service

interface DataService : BlockDataService, ToggleDataService, PlayerDataService {
fun initDataService()
fun saveData()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package net.superricky.tpaplusplus.database.service

import net.minecraft.server.network.ServerPlayerEntity
import net.superricky.tpaplusplus.database.PlayerData
import net.superricky.tpaplusplus.utility.LevelBoundVec3
import java.util.*

interface PlayerDataService {
fun getPlayerData(player: ServerPlayerEntity): PlayerData
fun insertPlayer(uuid: UUID)
fun insertDeath(uuid: UUID, pos: LevelBoundVec3)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package net.superricky.tpaplusplus.database.service

import java.util.*

interface ToggleDataService {
fun checkPlayerToggle(uuid: UUID): Boolean
fun playerSwitchToggle(uuid: UUID): Boolean
fun playerSwitchToggle(uuid: UUID, toggle: Boolean): Boolean
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ fun String.getWorld(): ServerDimension =
RegistryKey.of(RegistryKeys.WORLD, Identifier.of(this))

fun PlayerEntity.toggleOn() = TpaPlusPlus.launch {
TpaPlusPlus.dataService.playerSwitchBlock(uuid, true)
TpaPlusPlus.dataService.playerSwitchToggle(uuid, true)
}

fun PlayerEntity.toggleOff() = TpaPlusPlus.launch {
TpaPlusPlus.dataService.playerSwitchBlock(uuid, false)
TpaPlusPlus.dataService.playerSwitchToggle(uuid, false)
}

fun PlayerEntity.getColoredName(color: Style): MutableText? = this.name.literalString?.literal()?.setStyle(color)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import net.minecraft.util.math.Position
import net.minecraft.util.math.Vec3d
import net.minecraft.world.World
import net.superricky.tpaplusplus.GlobalConst.NETHER_COEFFICIENT
import net.superricky.tpaplusplus.config.Config
import net.superricky.tpaplusplus.config.command.CommandLimitationsSpec

class LevelBoundVec3(
val serverLevel: ServerDimension,
Expand All @@ -18,17 +16,11 @@ class LevelBoundVec3(

fun distance(other: LevelBoundVec3): Double {
if (serverLevel != other.serverLevel) {
if (!Config.getConfig()[CommandLimitationsSpec.crossDimAllowed]) {
return -1.0
}
if (Config.getConfig()[CommandLimitationsSpec.ignoreDistanceCrossDim]) {
return 0.0
}
if (serverLevel == World.NETHER && other.serverLevel != World.NETHER) {
return other.distanceTo(
Vec3d(
x * NETHER_COEFFICIENT,
y * NETHER_COEFFICIENT,
y,
z * NETHER_COEFFICIENT
)
)
Expand All @@ -37,7 +29,7 @@ class LevelBoundVec3(
return distanceTo(
Vec3d(
other.x * NETHER_COEFFICIENT,
other.y * NETHER_COEFFICIENT,
other.y,
other.z * NETHER_COEFFICIENT
)
)
Expand Down
Loading

0 comments on commit 279baf9

Please sign in to comment.