Skip to content

Commit

Permalink
Merge branch 'RPKit-205' into release/v1.2
Browse files Browse the repository at this point in the history
  • Loading branch information
renbinden committed Mar 4, 2017
2 parents 39dc2ba + 93eef31 commit 0e5ffa8
Show file tree
Hide file tree
Showing 18 changed files with 169 additions and 39 deletions.
34 changes: 34 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,38 @@ project(":bukkit:rpk-stat-lib-bukkit") {

}

project(":bukkit:rpk-food-lib-bukkit") {

dependencies {
compile project(":bukkit:rpk-core-bukkit")
}

processResources {
filter ReplaceTokens, tokens: [
"version": version
]
}

artifacts {
archives jar
}

uploadArchives {
repositories.mavenDeployer {

repository(url: "https://repo.seventh-root.com/artifactory/libs-release-local/") {
authentication(userName: repoUserName, password: repoPassword)
}

pom.version = project(":bukkit:rpk-food-lib-bukkit").version
pom.artifactId = project(":bukkit:rpk-food-lib-bukkit").name
pom.groupId = project(":bukkit:rpk-food-lib-bukkit").group

}
}

}

project(":bukkit:rpk-payment-lib-bukkit") {

dependencies {
Expand Down Expand Up @@ -1071,6 +1103,7 @@ project(":bukkit:rpk-trade-bukkit") {
compile project(":bukkit:rpk-core-bukkit")
compile project(":bukkit:rpk-character-lib-bukkit")
compile project(":bukkit:rpk-economy-lib-bukkit")
compile project(":bukkit:rpk-food-lib-bukkit")
}

processResources {
Expand Down Expand Up @@ -1209,6 +1242,7 @@ project(":bukkit:rpk-food-bukkit") {

dependencies {
compile project(":bukkit:rpk-core-bukkit")
compile project(":bukkit:rpk-food-lib-bukkit")
}

processResources {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@
package com.rpkit.food.bukkit

import com.rpkit.core.bukkit.plugin.RPKBukkitPlugin
import com.rpkit.food.bukkit.expiry.ExpiryProvider
import com.rpkit.food.bukkit.listener.EntityDeathListener
import com.rpkit.food.bukkit.listener.FurnaceSmeltListener
import com.rpkit.food.bukkit.expiry.RPKExpiryProviderImpl
import com.rpkit.food.bukkit.listener.*

/**
Expand All @@ -30,16 +28,18 @@ class RPKFoodBukkit: RPKBukkitPlugin() {
override fun onEnable() {
saveDefaultConfig()
serviceProviders = arrayOf(
ExpiryProvider(this)
RPKExpiryProviderImpl(this)
)
}

override fun registerListeners() {
registerListeners(
EntityDeathListener(this),
FurnaceSmeltListener(this),
InventoryOpenListener(this),
PlayerFishListener(this),
PlayerItemConsumeListener(this),
PlayerJoinListener(this),
PlayerPickupItemListener(this),
PrepareItemCraftListener(this)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,29 +16,32 @@

package com.rpkit.food.bukkit.expiry

import com.rpkit.core.service.ServiceProvider
import com.rpkit.food.bukkit.RPKFoodBukkit
import org.bukkit.inventory.ItemStack
import java.text.SimpleDateFormat
import java.util.*


class ExpiryProvider(private val plugin: RPKFoodBukkit): ServiceProvider {
class RPKExpiryProviderImpl(private val plugin: RPKFoodBukkit): RPKExpiryProvider {

val dateFormat = SimpleDateFormat(plugin.config.getString("date-format"))

fun setExpiry(item: ItemStack, timestamp: Long) {
override fun setExpiry(item: ItemStack, expiryDate: Date) {
if (!item.type.isEdible) return
val itemMeta = item.itemMeta
val lore = if (itemMeta.hasLore()) itemMeta.lore else mutableListOf<String>()
lore.removeAll(lore.filter { it.startsWith("Expires: ") })
val expiryDate = Date(timestamp)
lore.add("Expires: ${dateFormat.format(expiryDate)}")
itemMeta.lore = lore
item.itemMeta = itemMeta
}

fun getExpiry(item: ItemStack): Long? {
override fun setExpiry(item: ItemStack) {
setExpiry(item, Date(System.currentTimeMillis() + (plugin.config.getLong("food-expiry.${item.type}",
plugin.config.getLong("food-expiry.default")) * 1000)))
}

override fun getExpiry(item: ItemStack): Date? {
if (!item.type.isEdible) return null
val itemMeta = item.itemMeta
if (itemMeta.hasLore()) {
Expand All @@ -47,15 +50,15 @@ class ExpiryProvider(private val plugin: RPKFoodBukkit): ServiceProvider {
if (expiryLore != null) {
val expiryDateString = expiryLore.drop("Expires: ".length)
val expiryDate = dateFormat.parse(expiryDateString)
return expiryDate.time
return expiryDate
}
}
return null
}

fun isExpired(item: ItemStack): Boolean {
override fun isExpired(item: ItemStack): Boolean {
val expiryTimestamp = getExpiry(item) ?: return true
return expiryTimestamp <= System.currentTimeMillis()
return expiryTimestamp.time <= System.currentTimeMillis()
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
package com.rpkit.food.bukkit.listener

import com.rpkit.food.bukkit.RPKFoodBukkit
import com.rpkit.food.bukkit.expiry.ExpiryProvider
import com.rpkit.food.bukkit.expiry.RPKExpiryProviderImpl
import org.bukkit.entity.Player
import org.bukkit.event.EventHandler
import org.bukkit.event.Listener
Expand All @@ -32,13 +32,11 @@ class EntityDeathListener(private val plugin: RPKFoodBukkit): Listener {
@EventHandler
fun onEntityDeath(event: EntityDeathEvent) {
if (event.entity is Player) return
val expiryProvider = plugin.core.serviceManager.getServiceProvider(ExpiryProvider::class)
val expiryProvider = plugin.core.serviceManager.getServiceProvider(RPKExpiryProviderImpl::class)
val newDrops = mutableListOf<ItemStack>()
for (drop in event.drops) {
if (drop.type.isEdible) {
expiryProvider.setExpiry(drop,
System.currentTimeMillis() + (plugin.config.getLong("food-expiry.${drop.type}",
plugin.config.getLong("food-expiry.default")) * 1000))
expiryProvider.setExpiry(drop)
}
newDrops.add(drop)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@
package com.rpkit.food.bukkit.listener

import com.rpkit.food.bukkit.RPKFoodBukkit
import com.rpkit.food.bukkit.expiry.ExpiryProvider
import com.rpkit.food.bukkit.expiry.RPKExpiryProviderImpl
import org.bukkit.event.EventHandler
import org.bukkit.event.Listener
import org.bukkit.event.inventory.FurnaceSmeltEvent
import java.util.*

/**
* Furnace smelt listener for adding expiry dates.
Expand All @@ -30,9 +31,8 @@ class FurnaceSmeltListener(private val plugin: RPKFoodBukkit): Listener {
@EventHandler
fun onFurnaceSmelt(event: FurnaceSmeltEvent) {
val item = event.result
val expiryProvider = plugin.core.serviceManager.getServiceProvider(ExpiryProvider::class)
expiryProvider.setExpiry(item, System.currentTimeMillis() + (plugin.config.getLong("food-expiry.${item.type}",
plugin.config.getLong("food-expiry.default")) * 1000))
val expiryProvider = plugin.core.serviceManager.getServiceProvider(RPKExpiryProviderImpl::class)
expiryProvider.setExpiry(item)
event.result = item
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.rpkit.food.bukkit.listener

import com.rpkit.food.bukkit.RPKFoodBukkit
import com.rpkit.food.bukkit.expiry.RPKExpiryProvider
import org.bukkit.event.EventHandler
import org.bukkit.event.Listener
import org.bukkit.event.inventory.InventoryOpenEvent


class InventoryOpenListener(private val plugin: RPKFoodBukkit): Listener {

@EventHandler
fun onInventoryOpen(event: InventoryOpenEvent) {
val expiryProvider = plugin.core.serviceManager.getServiceProvider(RPKExpiryProvider::class)
event.inventory.contents
.filterNotNull()
.filter { it.type.isEdible }
.forEach { item ->
if (expiryProvider.getExpiry(item) == null) {
expiryProvider.setExpiry(item)
}
}
event.player.inventory.contents
.filterNotNull()
.filter { it.type.isEdible }
.forEach { item ->
if (expiryProvider.getExpiry(item) == null) {
expiryProvider.setExpiry(item)
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
package com.rpkit.food.bukkit.listener

import com.rpkit.food.bukkit.RPKFoodBukkit
import com.rpkit.food.bukkit.expiry.ExpiryProvider
import com.rpkit.food.bukkit.expiry.RPKExpiryProviderImpl
import org.bukkit.entity.Item
import org.bukkit.event.EventHandler
import org.bukkit.event.Listener
Expand All @@ -34,9 +34,8 @@ class PlayerFishListener(private val plugin: RPKFoodBukkit): Listener {
if (caught != null) {
if (caught is Item) {
val item = caught.itemStack
val expiryProvider = plugin.core.serviceManager.getServiceProvider(ExpiryProvider::class)
expiryProvider.setExpiry(item, System.currentTimeMillis() + (plugin.config.getLong("food-expiry.${item.type}",
plugin.config.getLong("food-expiry.default")) * 1000))
val expiryProvider = plugin.core.serviceManager.getServiceProvider(RPKExpiryProviderImpl::class)
expiryProvider.setExpiry(item)
caught.itemStack = item
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
package com.rpkit.food.bukkit.listener

import com.rpkit.food.bukkit.RPKFoodBukkit
import com.rpkit.food.bukkit.expiry.ExpiryProvider
import com.rpkit.food.bukkit.expiry.RPKExpiryProviderImpl
import org.bukkit.event.EventHandler
import org.bukkit.event.Listener
import org.bukkit.event.player.PlayerItemConsumeEvent
Expand All @@ -32,7 +32,7 @@ class PlayerItemConsumeListener(private val plugin: RPKFoodBukkit): Listener {
@EventHandler
fun onPlayerItemConsume(event: PlayerItemConsumeEvent) {
val item = event.item
val expiryProvider = plugin.core.serviceManager.getServiceProvider(ExpiryProvider::class)
val expiryProvider = plugin.core.serviceManager.getServiceProvider(RPKExpiryProviderImpl::class)
if (item.type.isEdible) {
if (expiryProvider.isExpired(item)) {
event.player.addPotionEffect(PotionEffect(PotionEffectType.HUNGER, 1200, 0))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.rpkit.food.bukkit.listener

import com.rpkit.food.bukkit.RPKFoodBukkit
import com.rpkit.food.bukkit.expiry.RPKExpiryProvider
import org.bukkit.event.EventHandler
import org.bukkit.event.Listener
import org.bukkit.event.player.PlayerJoinEvent

class PlayerJoinListener(private val plugin: RPKFoodBukkit): Listener {

@EventHandler
fun onPlayerJoin(event: PlayerJoinEvent) {
val expiryProvider = plugin.core.serviceManager.getServiceProvider(RPKExpiryProvider::class)
event.player.inventory.contents
.filterNotNull()
.filter { it.type.isEdible }
.forEach { item ->
if (expiryProvider.getExpiry(item) == null) {
expiryProvider.setExpiry(item)
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
package com.rpkit.food.bukkit.listener

import com.rpkit.food.bukkit.RPKFoodBukkit
import com.rpkit.food.bukkit.expiry.ExpiryProvider
import com.rpkit.food.bukkit.expiry.RPKExpiryProviderImpl
import org.bukkit.event.EventHandler
import org.bukkit.event.Listener
import org.bukkit.event.player.PlayerPickupItemEvent
Expand All @@ -31,11 +31,9 @@ class PlayerPickupItemListener(private val plugin: RPKFoodBukkit): Listener {
fun onPlayerPickupItem(event: PlayerPickupItemEvent) {
val itemStack = event.item.itemStack
if (itemStack.type.isEdible) {
val expiryProvider = plugin.core.serviceManager.getServiceProvider(ExpiryProvider::class)
val expiryProvider = plugin.core.serviceManager.getServiceProvider(RPKExpiryProviderImpl::class)
if (expiryProvider.getExpiry(itemStack) == null) {
expiryProvider.setExpiry(itemStack,
System.currentTimeMillis() + (plugin.config.getLong("food-expiry.${itemStack.type}",
plugin.config.getLong("food-expiry.default")) * 1000))
expiryProvider.setExpiry(itemStack)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
package com.rpkit.food.bukkit.listener

import com.rpkit.food.bukkit.RPKFoodBukkit
import com.rpkit.food.bukkit.expiry.ExpiryProvider
import com.rpkit.food.bukkit.expiry.RPKExpiryProviderImpl
import org.bukkit.event.EventHandler
import org.bukkit.event.Listener
import org.bukkit.event.inventory.PrepareItemCraftEvent
Expand All @@ -31,9 +31,8 @@ class PrepareItemCraftListener(private val plugin: RPKFoodBukkit): Listener {
fun onPrepareItemCraft(event: PrepareItemCraftEvent) {
val item = event.inventory.result
if (item.type.isEdible) {
val expiryProvider = plugin.core.serviceManager.getServiceProvider(ExpiryProvider::class)
expiryProvider.setExpiry(item, System.currentTimeMillis() + (plugin.config.getLong("food-expiry.${item.type}",
plugin.config.getLong("food-expiry.default")) * 1000))
val expiryProvider = plugin.core.serviceManager.getServiceProvider(RPKExpiryProviderImpl::class)
expiryProvider.setExpiry(item)
event.inventory.result = item
}
}
Expand Down
4 changes: 3 additions & 1 deletion bukkit/rpk-food-bukkit/src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@ name: rpk-food-bukkit
author: alyphen
version: @version@
main: com.rpkit.food.bukkit.RPKFoodBukkit
depend: [rpk-core-bukkit]
depend:
- rpk-core-bukkit
- rpk-food-lib-bukkit
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.rpkit.food.bukkit

import com.rpkit.core.bukkit.plugin.RPKBukkitPlugin


class RPKFoodLibBukkit : RPKBukkitPlugin()
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.rpkit.food.bukkit.expiry

import com.rpkit.core.service.ServiceProvider
import org.bukkit.inventory.ItemStack
import java.util.*

interface RPKExpiryProvider: ServiceProvider {

fun setExpiry(item: ItemStack, expiryDate: Date)
fun setExpiry(item: ItemStack)
fun getExpiry(item: ItemStack): Date?
fun isExpired(item: ItemStack): Boolean

}
6 changes: 6 additions & 0 deletions bukkit/rpk-food-lib-bukkit/src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
name: rpk-food-lib-bukkit
author: alyphen
version: @version@
main: com.rpkit.food.bukkit.RPKFoodLibBukkit
depend:
- rpk-core-bukkit
Loading

0 comments on commit 0e5ffa8

Please sign in to comment.