Skip to content

Commit

Permalink
Add source code
Browse files Browse the repository at this point in the history
  • Loading branch information
alikindsys committed Aug 2, 2020
1 parent 1276eaf commit e143e86
Show file tree
Hide file tree
Showing 5 changed files with 150 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/main/kotlin/me/roridev/safemine/Safemine.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package me.roridev.safemine

import me.roridev.safemine.handlers.BreakHandler
import me.roridev.safemine.handlers.DropHandler
import org.bukkit.Bukkit
import org.bukkit.plugin.java.JavaPlugin

class Safemine : JavaPlugin(){
override fun onEnable() {
saveDefaultConfig()
reloadConfig()
server.pluginManager.registerEvents(BreakHandler(), this)
server.pluginManager.registerEvents(DropHandler(), this)
}

override fun onDisable() {
}
companion object{
val logger get() = Bukkit.getPluginManager().getPlugin("safemine")!!.logger
val config get() = Bukkit.getPluginManager().getPlugin("safemine")!!.config
}
}
38 changes: 38 additions & 0 deletions src/main/kotlin/me/roridev/safemine/handlers/BreakHandler.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package me.roridev.safemine.handlers

import com.destroystokyo.paper.Title
import me.roridev.safemine.Safemine
import net.md_5.bungee.api.ChatColor
import net.md_5.bungee.api.chat.TextComponent
import org.bukkit.GameMode
import org.bukkit.Sound
import org.bukkit.enchantments.Enchantment
import org.bukkit.event.EventHandler
import org.bukkit.event.Listener
import org.bukkit.event.block.BlockBreakEvent

class BreakHandler : Listener {
@EventHandler
fun Break(event : BlockBreakEvent){
if(event.player.gameMode != GameMode.SURVIVAL) return
if(!event.isDropItems) return
if(!event.player.inventory.hasSpace(event.block.drops.toMutableList())) cancelEvent(event)
if(event.player.inventory.itemInMainHand.containsEnchantment(Enchantment.MENDING)
|| event.player.inventory.itemInOffHand.containsEnchantment(Enchantment.MENDING)
|| event.player.inventory.armorContents.any{it.containsEnchantment(Enchantment.MENDING)}) {
event.player.giveExp(event.expToDrop, true)
} else {
event.player.giveExp(event.expToDrop, false)
}
event.expToDrop = 0
}
private fun cancelEvent(event: BlockBreakEvent){
event.isCancelled = true
val player = event.player
val component = TextComponent(Safemine.config.getString("message.fullInv"))
component.color = ChatColor.RED
player.sendTitle(Title(component))
player.playSound(player.location, Sound.ENTITY_PLAYER_LEVELUP,0.3f,1.0f)
}
}

83 changes: 83 additions & 0 deletions src/main/kotlin/me/roridev/safemine/handlers/DropHandler.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package me.roridev.safemine.handlers

import com.destroystokyo.paper.Title
import me.roridev.safemine.Safemine
import net.md_5.bungee.api.ChatColor
import net.md_5.bungee.api.chat.TextComponent
import org.bukkit.GameMode
import org.bukkit.Sound
import org.bukkit.event.EventHandler
import org.bukkit.event.Listener
import org.bukkit.event.block.BlockDropItemEvent
import org.bukkit.inventory.ItemStack
import org.bukkit.inventory.PlayerInventory

class DropHandler : Listener {
@EventHandler
fun Drop(event: BlockDropItemEvent){
if(event.player.gameMode != GameMode.SURVIVAL) return
if(!event.player.inventory.hasSpace(event.items.map{x -> x.itemStack}.toMutableList())) {
cancelEvent(event)
return
}
event.isCancelled = true
for(item in event.items){
transferItems(event,item.itemStack)
}
}

private fun cancelEvent(event: BlockDropItemEvent){
event.isCancelled = true
val player = event.player
val component = TextComponent(Safemine.config.getString("message.fullInv"))
component.color = ChatColor.RED
player.sendTitle(Title(component))
player.playSound(player.location, Sound.ENTITY_PLAYER_LEVELUP,0.3f,1.0f)
}

private fun transferItems(event : BlockDropItemEvent, item: ItemStack){
event.isCancelled = true
val inventory = event.player.inventory
val stack = inventory.contents.filterNotNull().firstOrNull {it.type == item.type}
if(stack == null){
if(!inventory.hasSpace(event.items.map{x -> x.itemStack}.toMutableList())) {
cancelEvent(event)
return
}
newItemStack(inventory,item)
return
}
val index = inventory.contents.indexOfFirst { it?.type == item.type }
if(stack.amount + item.amount <= stack.maxStackSize) {
stack.amount += item.amount
inventory.contents[index] = stack
} else {
val reminder = stack.amount + item.amount - stack.maxStackSize
stack.amount = stack.maxStackSize
item.amount = reminder
inventory.contents[index] = stack
newItemStack(inventory,item)
}
}

private fun newItemStack(inv: PlayerInventory, stack: ItemStack) {
if(!inv.hasSpace(mutableListOf(stack))) return
inv.addItem(stack)
}

}
fun PlayerInventory.hasSpace(items: MutableList<ItemStack>): Boolean {

if(contents.filterNotNull().size < 36) { //Slot available
return true
}
//From now on the check is only done if the inventory has no spaces left.
val materials = items.map { x -> x.type }
val intersection = contents.map{x -> x?.type}.intersect(materials)
if(intersection.isEmpty()) {
return false
} else {
val pos = materials.map{x -> (contents.indexOfFirst {it?.amount == it?.maxStackSize && it?.type == x })}
return pos.none { it != -1 }
}
}
1 change: 1 addition & 0 deletions src/main/resources/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
message.fullInv: "Inventory Full"
6 changes: 6 additions & 0 deletions src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
name: "safemine"
version: ${project.version}
api-version: "1.13"
authors : ["roridev"]
description: "Puts mined blocks directly into the players inventory whenever possible"
main: me.roridev.safemine.Safemine

0 comments on commit e143e86

Please sign in to comment.