-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1276eaf
commit e143e86
Showing
5 changed files
with
150 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
38
src/main/kotlin/me/roridev/safemine/handlers/BreakHandler.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
83
src/main/kotlin/me/roridev/safemine/handlers/DropHandler.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
message.fullInv: "Inventory Full" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |