Skip to content

Commit

Permalink
#424 Added additional check for possible cache misses. --snapshot
Browse files Browse the repository at this point in the history
  • Loading branch information
Shynixn committed Aug 24, 2020
1 parent 3c70d3a commit 3cae12f
Show file tree
Hide file tree
Showing 28 changed files with 288 additions and 37 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ allprojects {

subprojects {
group 'com.github.shynixn.petblocks'
version '8.21.0'
version '8.22.0-SNAPSHOT'

sourceCompatibility = 1.8

Expand Down
4 changes: 2 additions & 2 deletions docs/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@
# built documents.
#
# The short X.Y version.
version = '8.21.0'
version = '8.22.0'
# The full version, including alpha/beta/rc tags.
release = '8.21.0'
release = '8.22.0'

# The language for content autogenerated by Sphinx. Refer to documentation
# for a list of supported languages.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,13 @@ interface PersistencePetMetaService {
*/
fun save(petMeta: PetMeta): CompletableFuture<PetMeta>

/**
* Gets if the given player has got a cached pet meta.
* Only makes sense to be checked in special cases. Do only check it
* before calling getPetMetaFromPlayer(#player) if you get cache miss errors.
*/
fun <P> hasPetMeta(player: P): Boolean

/**
* Gets all currently loaded pet metas.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ class PetBlocksPlugin : JavaPlugin(), PluginProxy {
try {
val entityName = entity.simpleName + "Entity"
return Class.forName("com.github.shynixn.petblocks.core.logic.persistence.entity.$entityName")
.newInstance() as E
.getDeclaredConstructor().newInstance() as E
} catch (e: Exception) {
throw IllegalArgumentException("Entity could not be created.", e)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ class DamagePetListener @Inject constructor(
}

if (event.cause != EntityDamageEvent.DamageCause.FALL && event.entity is Player) {
if (!persistencePetMetaService.hasPetMeta(event.entity)) {
return
}

val petMeta = persistencePetMetaService.getPetMetaFromPlayer(event.entity)

combatPetService.flee(petMeta)
Expand All @@ -88,7 +92,8 @@ class DamagePetListener @Inject constructor(
val pet = petService.findPetByEntity(event.entity)!!
combatPetService.flee(pet.meta)

val vector = event.entity.location.toVector().subtract(event.damager.location.toVector()).normalize().multiply(0.5)
val vector =
event.entity.location.toVector().subtract(event.damager.location.toVector()).normalize().multiply(0.5)
vector.y = 0.1

pet.setVelocity(vector)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ class PetListener @Inject constructor(
private val configurationService: ConfigurationService,
private val aiService: AIService,
private val petActionService: PetActionService,
private val version: Version
private val version: Version,
private val loggingService: LoggingService
) : Listener {
private val joinCooldown = 20 * 6L
private val alreadyLoading = HashSet<UUID>()
Expand All @@ -92,6 +93,7 @@ class PetListener @Inject constructor(
return
}

loggingService.info("Loading pet of player [${event.player.name}]...")
alreadyLoading.add(uuid)

sync(concurrencyService, joinCooldown) {
Expand All @@ -105,6 +107,8 @@ class PetListener @Inject constructor(
*/
@EventHandler
fun onPetBlocksLoginEvent(event: PetBlocksLoginEvent) {
loggingService.info("Loaded pet of player [${event.player.name}].")

val overwrite = configurationService.findValue<Boolean>("global-configuration.overwrite-previous-pet")

if (overwrite) {
Expand Down Expand Up @@ -158,8 +162,11 @@ class PetListener @Inject constructor(
}
}

persistencePetMetaService.getPetMetaFromPlayer(event.player).enabled = activePet
persistencePetMetaService.clearResources(event.player)
if (persistencePetMetaService.hasPetMeta(event.player)) {
persistencePetMetaService.getPetMetaFromPlayer(event.player).enabled = activePet
persistencePetMetaService.clearResources(event.player)
}

debugService.unRegister(event.player)
petService.clearPlayerResources(event.player)
}
Expand Down Expand Up @@ -208,6 +215,10 @@ class PetListener @Inject constructor(
*/
@EventHandler
fun onEntityToggleSneakEvent(event: PlayerToggleSneakEvent) {
if (!persistencePetMetaService.hasPetMeta(event.player)) {
return
}

unMountPet(event.player)

val petMeta = persistencePetMetaService.getPetMetaFromPlayer(event.player)
Expand All @@ -228,6 +239,10 @@ class PetListener @Inject constructor(
return
}

if (!persistencePetMetaService.hasPetMeta(event.entity)) {
return
}

unMountPet(event.entity as Player)

val petMeta = persistencePetMetaService.getPetMetaFromPlayer(event.entity as Player)
Expand Down Expand Up @@ -397,6 +412,9 @@ class PetListener @Inject constructor(
*/
private fun loadPetBlocks(player: Player) {
if (!player.isOnline || (player.world as World?) == null) {
if (alreadyLoading.contains(player.uniqueId)) {
alreadyLoading.remove(player.uniqueId)
}
return
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ class DependencyHeadDatabaseServiceImpl @Inject constructor(
return
}

if (!petMetaService.hasPetMeta(player)) {
return
}

sync(concurrencyService, 10L) {
headDatabasePlayers.add(player)
player.performCommand("hdb")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@ class DependencyPlaceholderApiServiceImpl @Inject constructor(
return null
}

if (!persistencePetMetaService.hasPetMeta(player)) {
return null
}

if (placeHolderName == null) {
return null
}
Expand Down
2 changes: 1 addition & 1 deletion petblocks-bukkit-plugin/src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: PetBlocks
version: 8.21.0
version: 8.22.0-SNAPSHOT
author: Shynixn
main: com.github.shynixn.petblocks.bukkit.PetBlocksPlugin
softdepend: [PlaceholderAPI]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package helper

import com.github.shynixn.petblocks.api.business.service.LoggingService

class MockedLoggingService : LoggingService {
/**
* Logs an info text.
*/
override fun info(text: String, e: Throwable?) {
println(text)
e?.printStackTrace()
}

/**
* Logs an warning text.
*/
override fun warn(text: String, e: Throwable?) {
println(text)
e?.printStackTrace()
}

/**
* Logs an error text.
*/
override fun error(text: String, e: Throwable?) {
println(text)
e?.printStackTrace()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import com.github.shynixn.petblocks.core.logic.business.service.*
import com.github.shynixn.petblocks.core.logic.persistence.context.SqlDbContextImpl
import com.github.shynixn.petblocks.core.logic.persistence.entity.AIMovementEntity
import com.github.shynixn.petblocks.core.logic.persistence.repository.PetMetaSqlRepository
import helper.MockedLoggingService
import org.bukkit.configuration.file.YamlConfiguration
import org.bukkit.entity.Player
import org.bukkit.plugin.Plugin
Expand Down Expand Up @@ -266,7 +267,7 @@ class PersistenceMySQLIT {
dbContext!!,
aiService, guiItemLoadService, configService
)
return PersistencePetMetaServiceImpl(MockedProxyService(), sqlite, MockedConcurrencyService(), MockedEventService(), aiService)
return PersistencePetMetaServiceImpl(MockedProxyService(), sqlite, MockedConcurrencyService(), MockedEventService(), aiService, MockedLoggingService())
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import com.github.shynixn.petblocks.core.logic.business.service.*
import com.github.shynixn.petblocks.core.logic.persistence.context.SqlDbContextImpl
import com.github.shynixn.petblocks.core.logic.persistence.entity.AIMovementEntity
import com.github.shynixn.petblocks.core.logic.persistence.repository.PetMetaSqlRepository
import helper.MockedLoggingService
import org.apache.commons.io.FileUtils
import org.bukkit.configuration.file.YamlConfiguration
import org.bukkit.entity.Player
Expand Down Expand Up @@ -99,7 +100,10 @@ class PersistenceSQLiteIT {
Assertions.assertEquals("CHICKEN_WALK", (actual.aiGoals[0] as AIMovementEntity).movementSound.name)
Assertions.assertEquals(1.0, (actual.aiGoals[0] as AIMovementEntity).movementSound.volume)
Assertions.assertEquals(1.0, (actual.aiGoals[0] as AIMovementEntity).movementSound.pitch)
Assertions.assertEquals(ParticleType.REDSTONE.name, (actual.aiGoals[0] as AIMovementEntity).movementParticle.typeName)
Assertions.assertEquals(
ParticleType.REDSTONE.name,
(actual.aiGoals[0] as AIMovementEntity).movementParticle.typeName
)
Assertions.assertEquals(20, (actual.aiGoals[0] as AIMovementEntity).movementParticle.amount)

Assertions.assertEquals("follow-owner", (actual.aiGoals[1] as AIFollowOwner).type)
Expand Down Expand Up @@ -240,8 +244,14 @@ class PersistenceSQLiteIT {
method.invoke(PetBlocksApi, MockedPluginProxy())

val configService = ConfigurationServiceImpl(plugin)
val aiService = AIServiceImpl(LoggingUtilServiceImpl(Logger.getAnonymousLogger()), MockedProxyService(), YamlServiceImpl(), configService)
val localizationService = LocalizationServiceImpl(configService, LoggingUtilServiceImpl(Logger.getAnonymousLogger()))
val aiService = AIServiceImpl(
LoggingUtilServiceImpl(Logger.getAnonymousLogger()),
MockedProxyService(),
YamlServiceImpl(),
configService
)
val localizationService =
LocalizationServiceImpl(configService, LoggingUtilServiceImpl(Logger.getAnonymousLogger()))
localizationService.reload()

val guiItemLoadService =
Expand All @@ -254,14 +264,21 @@ class PersistenceSQLiteIT {

EntityServiceImpl(
MockedProxyService(),
Mockito.mock(EntityRegistrationService::class.java), Mockito.mock(PetService::class.java), YamlSerializationServiceImpl(),
plugin, Version.VERSION_1_8_R1, aiService
Mockito.mock(EntityRegistrationService::class.java),
Mockito.mock(PetService::class.java),
YamlSerializationServiceImpl(),
plugin,
Version.VERSION_1_8_R1,
aiService
)

dbContext = SqlDbContextImpl(configService, LoggingUtilServiceImpl(Logger.getAnonymousLogger()))

val sqlite = PetMetaSqlRepository(dbContext!!, aiService, guiItemLoadService, configService)
return PersistencePetMetaServiceImpl(MockedProxyService(), sqlite, MockedConcurrencyService(), MockedEventService(), aiService)
return PersistencePetMetaServiceImpl(
MockedProxyService(), sqlite, MockedConcurrencyService(), MockedEventService(), aiService,
MockedLoggingService()
)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,13 @@ class DamagePetListenerTest {
throw IllegalArgumentException()
}

/**
* Gets if the given player has got a cached pet meta.
*/
override fun <P> hasPetMeta(player: P): Boolean {
return true
}

/**
* Gets all currently loaded pet metas.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import com.github.shynixn.petblocks.bukkit.logic.business.listener.PetListener
import com.github.shynixn.petblocks.core.logic.persistence.entity.PetMetaEntity
import com.github.shynixn.petblocks.core.logic.persistence.entity.PlayerMetaEntity
import com.github.shynixn.petblocks.core.logic.persistence.entity.SkinEntity
import helper.MockedLoggingService
import org.bukkit.entity.Player
import org.bukkit.event.player.PlayerQuitEvent
import org.junit.jupiter.api.Assertions
Expand Down Expand Up @@ -151,7 +152,8 @@ class PetListenerTest {
configurationService,
aiService,
petActionService,
Version.VERSION_UNKNOWN
Version.VERSION_UNKNOWN,
MockedLoggingService()
)
}
}
Expand Down Expand Up @@ -234,6 +236,13 @@ class PetListenerTest {
throw IllegalArgumentException()
}

/**
* Gets if the given player has got a cached pet meta.
*/
override fun <P> hasPetMeta(player: P): Boolean {
return true
}

/**
* Gets all currently loaded pet metas.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@ package com.github.shynixn.petblocks.core.logic.business.commandexecutor

import com.github.shynixn.petblocks.api.business.command.PlayerCommand
import com.github.shynixn.petblocks.api.business.enumeration.Permission
import com.github.shynixn.petblocks.api.business.service.ConfigurationService
import com.github.shynixn.petblocks.api.business.service.GUIService
import com.github.shynixn.petblocks.api.business.service.PetActionService
import com.github.shynixn.petblocks.api.business.service.ProxyService
import com.github.shynixn.petblocks.api.business.service.*
import com.github.shynixn.petblocks.core.logic.business.extension.mergeArgs
import com.google.inject.Inject

Expand Down Expand Up @@ -40,19 +37,36 @@ class PlayerPetActionCommandExecutorImpl @Inject constructor(
private val petActionService: PetActionService,
private val guiService: GUIService,
private val proxyService: ProxyService,
private val configurationService: ConfigurationService
private val configurationService: ConfigurationService,
private val persistencePetMetaService: PersistencePetMetaService
) : PlayerCommand {
/**
* Gets called when the given [player] executes the defined command with the given [args].
*/
override fun <P> onPlayerExecuteCommand(player: P, args: Array<out String>): Boolean {
if (!persistencePetMetaService.hasPetMeta(player)) {
return true
}

if (args.size == 1 && args[0].equals("call", true) && proxyService.hasPermission(player, Permission.CALL)) {
petActionService.callPet(player)
} else if (args.size == 1 && args[0].equals("toggle", true) && proxyService.hasPermission(player, Permission.TOGGLE)) {
} else if (args.size == 1 && args[0].equals("toggle", true) && proxyService.hasPermission(
player,
Permission.TOGGLE
)
) {
petActionService.togglePet(player)
} else if (args.size >= 2 && args[0].equals("rename", true) && proxyService.hasPermission(player, Permission.RENAME)) {
} else if (args.size >= 2 && args[0].equals("rename", true) && proxyService.hasPermission(
player,
Permission.RENAME
)
) {
petActionService.renamePet(player, mergeArgs(args))
} else if (args.size == 2 && args[0].equals("skin", true) && proxyService.hasPermission(player, Permission.CUSTOMHEAD)) {
} else if (args.size == 2 && args[0].equals("skin", true) && proxyService.hasPermission(
player,
Permission.CUSTOMHEAD
)
) {
petActionService.changePetSkin(player, args[1])
} else if (args.size == 1 && proxyService.hasPermission(
player,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,10 @@ class GUIServiceImpl @Inject constructor(
override fun <P> open(player: P, pageName: String?) {
require(player is Any)

if (!persistenceService.hasPetMeta(player)) {
return
}

if (placeHolderService == null && dependencyService.isInstalled(PluginDependency.PLACEHOLDERAPI)) {
placeHolderService = PetBlocksApi.resolve(DependencyPlaceholderApiService::class.java)
}
Expand Down
Loading

0 comments on commit 3cae12f

Please sign in to comment.