From 26a3cd882eb65328d07bef0a73155fe426d60b52 Mon Sep 17 00:00:00 2001 From: Gabriel-Paulucci Date: Wed, 11 Sep 2024 11:11:29 -0300 Subject: [PATCH] Add: block item drop --- .../nyaruko/auth/commands/LoginCommand.kt | 10 ++++----- .../nyaruko/auth/handlers/AuthHandler.kt | 2 ++ .../nyaruko/auth/handlers/CheckHandler.kt | 20 ++++++++++++++++-- .../nyaruko/auth/messages/LoginMessage.kt | 11 ++++++++++ .../nyaruko/core/events/PlayerMoveEvent.kt | 4 ++-- .../core/handlers/PlayerTickHandler.kt | 6 ++++-- .../nyaruko/extensions/EntityPlayer.kt | 5 +++++ .../nyaruko/services/SessionsService.kt | 21 +++++++++++++++---- .../nyaruko/viewmodels/PlayerInfoViewModel.kt | 4 +++- 9 files changed, 67 insertions(+), 16 deletions(-) diff --git a/src/main/kotlin/com/fardragi/nyaruko/auth/commands/LoginCommand.kt b/src/main/kotlin/com/fardragi/nyaruko/auth/commands/LoginCommand.kt index 688e91e..d49be13 100644 --- a/src/main/kotlin/com/fardragi/nyaruko/auth/commands/LoginCommand.kt +++ b/src/main/kotlin/com/fardragi/nyaruko/auth/commands/LoginCommand.kt @@ -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 { diff --git a/src/main/kotlin/com/fardragi/nyaruko/auth/handlers/AuthHandler.kt b/src/main/kotlin/com/fardragi/nyaruko/auth/handlers/AuthHandler.kt index b320591..9ab85c2 100644 --- a/src/main/kotlin/com/fardragi/nyaruko/auth/handlers/AuthHandler.kt +++ b/src/main/kotlin/com/fardragi/nyaruko/auth/handlers/AuthHandler.kt @@ -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 @@ -28,6 +29,7 @@ class AuthHandler( CoroutineScope(Dispatchers.IO).launch { val user = userService.getOrCreateUser(userId, userName) + sessionsService.loggedIn(userId, userName, PositionViewModel(player)) delay(2000) diff --git a/src/main/kotlin/com/fardragi/nyaruko/auth/handlers/CheckHandler.kt b/src/main/kotlin/com/fardragi/nyaruko/auth/handlers/CheckHandler.kt index 55dfc10..a2d1272 100644 --- a/src/main/kotlin/com/fardragi/nyaruko/auth/handlers/CheckHandler.kt +++ b/src/main/kotlin/com/fardragi/nyaruko/auth/handlers/CheckHandler.kt @@ -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) + } } diff --git a/src/main/kotlin/com/fardragi/nyaruko/auth/messages/LoginMessage.kt b/src/main/kotlin/com/fardragi/nyaruko/auth/messages/LoginMessage.kt index dfde18f..478072c 100644 --- a/src/main/kotlin/com/fardragi/nyaruko/auth/messages/LoginMessage.kt +++ b/src/main/kotlin/com/fardragi/nyaruko/auth/messages/LoginMessage.kt @@ -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 @@ -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 + } } diff --git a/src/main/kotlin/com/fardragi/nyaruko/core/events/PlayerMoveEvent.kt b/src/main/kotlin/com/fardragi/nyaruko/core/events/PlayerMoveEvent.kt index 1fc2f2d..dec77f1 100644 --- a/src/main/kotlin/com/fardragi/nyaruko/core/events/PlayerMoveEvent.kt +++ b/src/main/kotlin/com/fardragi/nyaruko/core/events/PlayerMoveEvent.kt @@ -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() { } diff --git a/src/main/kotlin/com/fardragi/nyaruko/core/handlers/PlayerTickHandler.kt b/src/main/kotlin/com/fardragi/nyaruko/core/handlers/PlayerTickHandler.kt index 1e1c4d4..33e10ff 100644 --- a/src/main/kotlin/com/fardragi/nyaruko/core/handlers/PlayerTickHandler.kt +++ b/src/main/kotlin/com/fardragi/nyaruko/core/handlers/PlayerTickHandler.kt @@ -40,9 +40,11 @@ class PlayerTickHandler : NyarukoHandlerBase() { player.teleport(oldPosition) playerPositions[player.uniqueID] = oldPosition } - } else { - playerPositions[player.uniqueID] = newPosition + + return } + + playerPositions[player.uniqueID] = newPosition } } diff --git a/src/main/kotlin/com/fardragi/nyaruko/extensions/EntityPlayer.kt b/src/main/kotlin/com/fardragi/nyaruko/extensions/EntityPlayer.kt index 283e6fb..e1b9667 100644 --- a/src/main/kotlin/com/fardragi/nyaruko/extensions/EntityPlayer.kt +++ b/src/main/kotlin/com/fardragi/nyaruko/extensions/EntityPlayer.kt @@ -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() @@ -42,3 +43,7 @@ fun EntityPlayer.teleport(position: PositionViewModel) { ) ) } + +fun EntityPlayer.isTruePlayer(): Boolean { + return this is EntityPlayerMP && this !is FakePlayer +} diff --git a/src/main/kotlin/com/fardragi/nyaruko/services/SessionsService.kt b/src/main/kotlin/com/fardragi/nyaruko/services/SessionsService.kt index 461dbbb..e73338b 100644 --- a/src/main/kotlin/com/fardragi/nyaruko/services/SessionsService.kt +++ b/src/main/kotlin/com/fardragi/nyaruko/services/SessionsService.kt @@ -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() - 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 } @@ -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 } } diff --git a/src/main/kotlin/com/fardragi/nyaruko/viewmodels/PlayerInfoViewModel.kt b/src/main/kotlin/com/fardragi/nyaruko/viewmodels/PlayerInfoViewModel.kt index 52fb20b..eaad9e5 100644 --- a/src/main/kotlin/com/fardragi/nyaruko/viewmodels/PlayerInfoViewModel.kt +++ b/src/main/kotlin/com/fardragi/nyaruko/viewmodels/PlayerInfoViewModel.kt @@ -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 +}