mirror of
https://github.com/alexcrea/CustomAnvil.git
synced 2026-06-23 16:16:17 +02:00
Add XP cost display above 40.
Need ProtocoLib installed.
This commit is contained in:
parent
8cf84caf3c
commit
de51617059
8 changed files with 119 additions and 16 deletions
|
|
@ -19,6 +19,7 @@ import org.bukkit.event.EventPriority.HIGHEST
|
|||
import org.bukkit.event.Listener
|
||||
import org.bukkit.event.inventory.ClickType
|
||||
import org.bukkit.event.inventory.InventoryClickEvent
|
||||
import org.bukkit.event.inventory.InventoryCloseEvent
|
||||
import org.bukkit.event.inventory.PrepareAnvilEvent
|
||||
import org.bukkit.inventory.AnvilInventory
|
||||
import org.bukkit.inventory.InventoryView.Property.REPAIR_COST
|
||||
|
|
@ -26,6 +27,7 @@ import org.bukkit.inventory.ItemStack
|
|||
import org.bukkit.inventory.meta.Repairable
|
||||
import xyz.alexcrea.cuanvil.config.ConfigHolder
|
||||
import xyz.alexcrea.cuanvil.group.ConflictType
|
||||
import xyz.alexcrea.cuanvil.packet.PacketManager
|
||||
import xyz.alexcrea.cuanvil.recipe.AnvilCustomRecipe
|
||||
import xyz.alexcrea.cuanvil.util.UnitRepairUtil.getRepair
|
||||
import kotlin.math.min
|
||||
|
|
@ -34,7 +36,7 @@ import kotlin.math.min
|
|||
/**
|
||||
* Listener for anvil events
|
||||
*/
|
||||
class AnvilEventListener : Listener {
|
||||
class AnvilEventListener(private val packetManager: PacketManager) : Listener {
|
||||
|
||||
companion object {
|
||||
// Anvil's output slot
|
||||
|
|
@ -544,14 +546,30 @@ class AnvilEventListener : Listener {
|
|||
|
||||
val player = event.view.player
|
||||
if(player is Player){
|
||||
if(player.gameMode != GameMode.CREATIVE){
|
||||
packetManager.setInstantBuild(player, finalAnvilCost >= 40)
|
||||
}
|
||||
player.updateInventory()
|
||||
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
fun onAnvilClose(event: InventoryCloseEvent){
|
||||
val player = event.player
|
||||
if(event.inventory !is AnvilInventory) return
|
||||
if(player is Player && GameMode.CREATIVE != player.gameMode){
|
||||
packetManager.setInstantBuild(player, false)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
private class SlotContainer(val type: SlotType, val slot: Int)
|
||||
private enum class SlotType {
|
||||
CURSOR,
|
||||
|
|
|
|||
|
|
@ -8,6 +8,9 @@ import xyz.alexcrea.cuanvil.command.EditConfigExecutor
|
|||
import xyz.alexcrea.cuanvil.command.ReloadExecutor
|
||||
import xyz.alexcrea.cuanvil.config.ConfigHolder
|
||||
import xyz.alexcrea.cuanvil.listener.ChatEventListener
|
||||
import xyz.alexcrea.cuanvil.packet.NoProtocoLib
|
||||
import xyz.alexcrea.cuanvil.packet.PacketManager
|
||||
import xyz.alexcrea.cuanvil.packet.ProtocoLibWrapper
|
||||
import xyz.alexcrea.cuanvil.util.Metrics
|
||||
import xyz.alexcrea.cuanvil.util.MetricsUtil
|
||||
import java.io.File
|
||||
|
|
@ -66,14 +69,20 @@ class CustomAnvil : JavaPlugin() {
|
|||
instance.logger.info(message)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
lateinit var packetManager: PacketManager
|
||||
|
||||
/**
|
||||
* Setup plugin for use
|
||||
*/
|
||||
override fun onEnable() {
|
||||
instance = this
|
||||
|
||||
val pluginManager = Bukkit.getPluginManager();
|
||||
|
||||
// Disable old plugin name if exist
|
||||
val potentialPlugin = Bukkit.getPluginManager().getPlugin("UnsafeEnchantsPlus")
|
||||
if (potentialPlugin != null) {
|
||||
|
|
@ -82,9 +91,15 @@ class CustomAnvil : JavaPlugin() {
|
|||
logger.warning("Please note CustomAnvil is a more recent version of UnsafeEnchantsPlus")
|
||||
}
|
||||
|
||||
// Load ProtocolLib dependency if exist
|
||||
packetManager = if(pluginManager.isPluginEnabled("ProtocolLib"))
|
||||
{ ProtocoLibWrapper(); }
|
||||
else
|
||||
{ NoProtocoLib(); }
|
||||
|
||||
// Load chat listener
|
||||
chatListener = ChatEventListener()
|
||||
Bukkit.getPluginManager().registerEvents(chatListener, this)
|
||||
pluginManager.registerEvents(chatListener, this)
|
||||
|
||||
// Load config
|
||||
val success = ConfigHolder.loadConfig()
|
||||
|
|
@ -98,7 +113,7 @@ class CustomAnvil : JavaPlugin() {
|
|||
prepareCommand()
|
||||
|
||||
server.pluginManager.registerEvents(
|
||||
AnvilEventListener(),
|
||||
AnvilEventListener(packetManager),
|
||||
this
|
||||
)
|
||||
}
|
||||
|
|
|
|||
13
src/main/kotlin/xyz/alexcrea/cuanvil/packet/NoProtocoLib.kt
Normal file
13
src/main/kotlin/xyz/alexcrea/cuanvil/packet/NoProtocoLib.kt
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
package xyz.alexcrea.cuanvil.packet
|
||||
|
||||
import org.bukkit.entity.Player
|
||||
|
||||
class NoProtocoLib: PacketManager {
|
||||
override val isProtocoLibInstalled: Boolean
|
||||
get() = false
|
||||
|
||||
// ProtocoLib not installed: We do nothing
|
||||
|
||||
override fun setInstantBuild(player: Player, instantBuild: Boolean) {}
|
||||
|
||||
}
|
||||
11
src/main/kotlin/xyz/alexcrea/cuanvil/packet/PacketManager.kt
Normal file
11
src/main/kotlin/xyz/alexcrea/cuanvil/packet/PacketManager.kt
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
package xyz.alexcrea.cuanvil.packet
|
||||
|
||||
import org.bukkit.entity.Player
|
||||
|
||||
interface PacketManager {
|
||||
|
||||
val isProtocoLibInstalled: Boolean
|
||||
|
||||
fun setInstantBuild(player: Player, instantBuild: Boolean)
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,39 @@
|
|||
package xyz.alexcrea.cuanvil.packet
|
||||
|
||||
import com.comphenix.protocol.PacketType
|
||||
import com.comphenix.protocol.ProtocolLibrary
|
||||
import com.comphenix.protocol.ProtocolManager
|
||||
import com.comphenix.protocol.events.PacketContainer
|
||||
import org.bukkit.entity.Player
|
||||
import java.lang.reflect.InvocationTargetException
|
||||
|
||||
class ProtocoLibWrapper: PacketManager {
|
||||
|
||||
private val protocolManager: ProtocolManager = ProtocolLibrary.getProtocolManager();
|
||||
|
||||
override val isProtocoLibInstalled: Boolean
|
||||
get() = true
|
||||
|
||||
override fun setInstantBuild(player: Player, instantBuild: Boolean) {
|
||||
val packet = PacketContainer(PacketType.Play.Server.ABILITIES)
|
||||
|
||||
// Set player's properties
|
||||
packet.float
|
||||
.write(0, player.flySpeed / 2)
|
||||
.write(1, player.walkSpeed / 2)
|
||||
|
||||
packet.booleans
|
||||
.write(0, player.isInvulnerable)
|
||||
.write(1, player.isFlying)
|
||||
.write(2, player.allowFlight)
|
||||
.write(3, instantBuild)
|
||||
|
||||
// Send packet
|
||||
try {
|
||||
protocolManager.sendServerPacket(player, packet)
|
||||
} catch (e: InvocationTargetException) {
|
||||
e.printStackTrace()
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue