All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.shynixn.petblocks.bukkit.logic.business.service.PersistenceServiceImpl.kt Maven / Gradle / Ivy

package com.github.shynixn.petblocks.bukkit.logic.business.service

import com.github.shynixn.petblocks.api.business.controller.PetBlockController
import com.github.shynixn.petblocks.api.business.service.PersistenceService
import com.github.shynixn.petblocks.api.persistence.controller.PetMetaController
import com.github.shynixn.petblocks.api.persistence.entity.PetMeta
import com.google.inject.Inject
import org.bukkit.entity.Player
import org.bukkit.plugin.Plugin
import java.util.*
import java.util.concurrent.CompletableFuture

/**
 * Created by Shynixn 2018.
 * 

* Version 1.2 *

* MIT License *

* Copyright (c) 2018 by Shynixn *

* Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: *

* The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software. *

* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ class PersistenceServiceImpl @Inject constructor(private val plugin: Plugin, private val petBlockController: PetBlockController, private val petMetaController: PetMetaController) : PersistenceService { /** * Returns [CompletableFuture] with the [PetMeta] instance of the given player. Creates a new [PetMeta] instance and * stores it in the storage if it does not already exist. */ override fun

getOrCreateFromPlayer(player: P): CompletableFuture { if (player !is Player) { throw IllegalArgumentException("Player has to be an BukkitPlayer!") } val completableFuture = CompletableFuture() val optPetBlock = petBlockController.getFromPlayer(player) if (optPetBlock.isPresent) { val meta = optPetBlock.get().meta plugin.server.scheduler.runTaskAsynchronously(plugin, { completableFuture.complete(meta) }) } else { plugin.server.scheduler.runTaskAsynchronously(plugin, { val optResult = petMetaController.getFromPlayer(player) if (optPetBlock.isPresent) { completableFuture.complete(optResult.get()) } else { val petMeta = petMetaController.create(player) petMetaController.store(petMeta) completableFuture.complete(petMeta) } }) } return completableFuture } /** * Returns [CompletableFuture] with a list of stored [PetMeta]. */ override fun getAll(): CompletableFuture> { val completableFuture = CompletableFuture>() val activePetMetas = petBlockController.all.map { p -> p.meta } plugin.server.scheduler.runTaskAsynchronously(plugin, { val petMetaList = petMetaController.all var i = 0 while (i < petMetaList.size) { val petMeta = petMetaList[i] if (activePetMetas.count { p -> p.playerMeta.uuid == petMeta.playerMeta.uuid } > 0) { petMetaList.removeAt(i) i = 0 } i++ } completableFuture.complete(petMetaList) }) return completableFuture } /** * Returns [CompletableFuture] with optional newly created [PetMeta] instance or empty optional if not found. */ override fun

getFromPlayer(player: P): CompletableFuture> { if (player !is Player) { throw IllegalArgumentException("Player has to be an BukkitPlayer!") } val completableFuture = CompletableFuture>() val optPetBlock = petBlockController.getFromPlayer(player) if (optPetBlock.isPresent) { val meta = optPetBlock.get().meta plugin.server.scheduler.runTaskAsynchronously(plugin, { completableFuture.complete(Optional.of(meta)) }) } else { plugin.server.scheduler.runTaskAsynchronously(plugin, { val optResult = petMetaController.getFromPlayer(player) completableFuture.complete(optResult) }) } return completableFuture } /** * Saves the given [petMeta] instance and returns a [CompletableFuture] with the same petMeta instance. */ override fun save(petMeta: PetMeta): CompletableFuture { val completableFuture = CompletableFuture() plugin.server.scheduler.runTaskAsynchronously(plugin, { petMetaController.store(petMeta) completableFuture.complete(petMeta) }) plugin.server.scheduler.runTask(plugin, { val petBlock = petBlockController.getFromPlayer(petMeta.playerMeta.getPlayer()) if (petBlock.isPresent) { petBlock.get().respawn() } }) return completableFuture } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy