Skip to content

Commit

Permalink
Add: block item drop
Browse files Browse the repository at this point in the history
  • Loading branch information
vulcandragi committed Sep 11, 2024
1 parent 3269f93 commit 26a3cd8
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,18 @@ class LoginCommand(
}

val userId = player.uniqueID
val name = player.displayName
val password = args[0]

if (!userService.checkPassword(userId, password)) {
throw MessageException(DefaultMessage.error("Senha incorreta"))
if (sessionsService.isAuthenticated(userId)){
throw MessageException(DefaultMessage.error("Você ja esta logado"))
}

if (!sessionsService.loggedIn(userId, name)){
throw MessageException(DefaultMessage.error("Você ja logado"))
if (!userService.checkPassword(userId, password)) {
throw MessageException(DefaultMessage.error("Senha incorreta"))
}

player.sendMessages(DefaultMessage.success("Logado com sucesso"))
sessionsService.setAuthenticate(userId)
}

override fun getRequiredPermissionLevel(): Int {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import com.fardragi.nyaruko.extensions.sendMessages
import com.fardragi.nyaruko.services.SessionsService
import com.fardragi.nyaruko.services.UserService
import com.fardragi.nyaruko.shared.handlers.NyarukoHandlerBase
import com.fardragi.nyaruko.viewmodels.PositionViewModel
import cpw.mods.fml.common.eventhandler.EventPriority
import cpw.mods.fml.common.eventhandler.SubscribeEvent
import cpw.mods.fml.common.gameevent.PlayerEvent.PlayerLoggedInEvent
Expand All @@ -28,6 +29,7 @@ class AuthHandler(

CoroutineScope(Dispatchers.IO).launch {
val user = userService.getOrCreateUser(userId, userName)
sessionsService.loggedIn(userId, userName, PositionViewModel(player))

delay(2000)

Expand Down
20 changes: 18 additions & 2 deletions src/main/kotlin/com/fardragi/nyaruko/auth/handlers/CheckHandler.kt
Original file line number Diff line number Diff line change
@@ -1,18 +1,34 @@
package com.fardragi.nyaruko.auth.handlers

import com.fardragi.nyaruko.auth.messages.LoginMessage
import com.fardragi.nyaruko.core.events.PlayerMoveEvent
import com.fardragi.nyaruko.extensions.isTruePlayer
import com.fardragi.nyaruko.extensions.sendMessages
import com.fardragi.nyaruko.services.SessionsService
import com.fardragi.nyaruko.shared.handlers.NyarukoHandlerBase
import cpw.mods.fml.common.eventhandler.EventPriority
import cpw.mods.fml.common.eventhandler.SubscribeEvent
import net.minecraftforge.event.entity.item.ItemTossEvent

class CheckHandler(private val sessionsService: SessionsService) : NyarukoHandlerBase() {
@SubscribeEvent(priority = EventPriority.HIGHEST)
fun onPlayerMove(event: PlayerMoveEvent) {
if (sessionsService.isLoggedIn(event.player.uniqueID)) {
if (sessionsService.isAuthenticated(event.player.uniqueID) || !event.player.isTruePlayer())
return
}

event.player.sendMessages(LoginMessage.loginBeforeGaming())
event.isCanceled = true
}

@SubscribeEvent(priority = EventPriority.HIGHEST)
fun onDropItem(event: ItemTossEvent) {
if (sessionsService.isAuthenticated(event.player.uniqueID) || !event.player.isTruePlayer())
return

event.player.sendMessages(LoginMessage.loginBeforeGaming())
event.isCanceled = true

val stack = event.entityItem.entityItem
event.player.inventory.addItemStackToInventory(stack)
}
}
11 changes: 11 additions & 0 deletions src/main/kotlin/com/fardragi/nyaruko/auth/messages/LoginMessage.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.fardragi.nyaruko.auth.messages

import com.fardragi.nyaruko.utils.MessageBuilder
import com.fardragi.nyaruko.utils.TextBuilder
import net.minecraft.event.ClickEvent
import net.minecraft.event.HoverEvent
Expand All @@ -17,4 +18,14 @@ object LoginMessage {

return textBuilder
}

fun loginBeforeGaming(): MessageBuilder {
val messageBuilder = MessageBuilder()
.addLine { builder ->
builder.append("Efetue o login antes de jogar", EnumChatFormatting.RED)
}
.addLine(usageAction())

return messageBuilder
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import net.minecraft.entity.player.EntityPlayerMP
@Cancelable
data class PlayerMoveEvent(
val player: EntityPlayerMP,
val old: PositionViewModel,
val new: PositionViewModel
val oldPosition: PositionViewModel,
val newPosition: PositionViewModel
) : NyarukoEvent() {
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,11 @@ class PlayerTickHandler : NyarukoHandlerBase() {
player.teleport(oldPosition)
playerPositions[player.uniqueID] = oldPosition
}
} else {
playerPositions[player.uniqueID] = newPosition

return
}

playerPositions[player.uniqueID] = newPosition
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import com.fardragi.nyaruko.viewmodels.PositionViewModel
import net.minecraft.entity.player.EntityPlayer
import net.minecraft.entity.player.EntityPlayerMP
import net.minecraft.server.MinecraftServer
import net.minecraftforge.common.util.FakePlayer

fun EntityPlayer.teleport(position: PositionViewModel) {
val server = MinecraftServer.getServer()
Expand Down Expand Up @@ -42,3 +43,7 @@ fun EntityPlayer.teleport(position: PositionViewModel) {
)
)
}

fun EntityPlayer.isTruePlayer(): Boolean {
return this is EntityPlayerMP && this !is FakePlayer
}
21 changes: 17 additions & 4 deletions src/main/kotlin/com/fardragi/nyaruko/services/SessionsService.kt
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
package com.fardragi.nyaruko.services

import com.fardragi.nyaruko.exceptions.NotFoundException
import com.fardragi.nyaruko.viewmodels.PlayerInfoViewModel
import com.fardragi.nyaruko.viewmodels.PositionViewModel
import java.util.UUID

class SessionsService {
private val authUsers = mutableMapOf<UUID, PlayerInfoViewModel>()

fun loggedIn(userId: UUID, name: String): Boolean {
fun loggedIn(userId: UUID, name: String, initialPosition: PositionViewModel): Boolean {
if (userId in authUsers) {
return false
}

authUsers[userId] = PlayerInfoViewModel(name)
authUsers[userId] = PlayerInfoViewModel(name, initialPosition)
return true
}

Expand All @@ -24,7 +26,18 @@ class SessionsService {
return true
}

fun isLoggedIn(userId: UUID): Boolean {
return userId in authUsers
fun setAuthenticate(userId: UUID){
val playerInfo = authUsers[userId]
?: throw NotFoundException(PlayerInfoViewModel::class.simpleName, userId.toString())

playerInfo.authenticated = true

}

fun isAuthenticated(userId: UUID): Boolean {
val playerInfo = authUsers[userId]
?: throw NotFoundException(PlayerInfoViewModel::class.simpleName, userId.toString())

return playerInfo.authenticated
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
package com.fardragi.nyaruko.viewmodels

data class PlayerInfoViewModel(val name: String)
data class PlayerInfoViewModel(val name: String, val initialPosition: PositionViewModel) {
var authenticated: Boolean = false
}

0 comments on commit 26a3cd8

Please sign in to comment.