Add XP cost display above 40.

Need ProtocoLib installed.
This commit is contained in:
alexcrea 2024-05-20 17:53:15 +02:00
parent 8cf84caf3c
commit de51617059
No known key found for this signature in database
GPG key ID: 43FD265DB0DBF91F
8 changed files with 119 additions and 16 deletions

View file

@ -15,14 +15,15 @@ the plugin can be downloaded on the
or [on GitHub](https://github.com/alexcrea/CustomAnvil/releases/latest) or [on GitHub](https://github.com/alexcrea/CustomAnvil/releases/latest)
--- ---
**Custom Anvil** have the following features: **Custom Anvil** have the following features:
- Vanilla like default configuration - Vanilla like default configuration.
- Custom enchantment level limit - Custom enchantment level limit.
- Custom anvil recipes - Custom anvil recipes.
- Custom enchant restrictions (allow unsafe enchantment only for a group of item or create new restriction) - Custom enchant restrictions (allow unsafe enchantment only for a group of item or create new restriction).
- Custom items of unit repairs (repair damaged with unit of "material", for example the repair of diamond sword by diamonds) - Custom items of unit repairs (repair damaged with unit of "material", for example the repair of diamond sword by diamonds).
- Custom XP cost for every aspect of the anvil - Custom XP cost for every aspect of the anvil.
- Permissions to bypass level limit or enchantment restriction. - Permissions to bypass level limit or enchantment restriction.
- Gui to configure the plugin in game - Display xp cost instead of "to expensive" when above lv 40. (need ProtocoLib)
- Gui to configure the plugin in game.
--- ---
### Permissions: ### Permissions:
```yml ```yml
@ -54,8 +55,8 @@ Default configuration can be found on following links:
There is non known issue, if you find one please report the issue. There is non known issue, if you find one please report the issue.
### Planned: ### Planned:
- Semi manual config update on pluign or minecraft update - Semi manual config update on plugin or minecraft update
- Check unknow registered enchantment & warn - Check unknown registered enchantment & warn
- Warn admin on unsuported minecraft version - Warn admin on unsupported minecraft version

View file

@ -4,11 +4,14 @@ plugins {
} }
group = "xyz.alexcrea" group = "xyz.alexcrea"
version = "1.4.4" version = "1.4.5"
repositories { repositories {
mavenCentral() mavenCentral()
maven(url = "https://hub.spigotmc.org/nexus/content/repositories/snapshots/") maven(url = "https://hub.spigotmc.org/nexus/content/repositories/snapshots/")
// ProtocoLib
maven (url = "https://repo.dmulloy2.net/repository/public/" )
} }
dependencies { dependencies {
@ -20,6 +23,8 @@ dependencies {
// Gui library // Gui library
compileOnly("com.github.stefvanschie.inventoryframework:IF:0.10.13") compileOnly("com.github.stefvanschie.inventoryframework:IF:0.10.13")
// Protocolib
compileOnly("com.comphenix.protocol:ProtocolLib:5.1.0")
testImplementation("org.junit.jupiter:junit-jupiter-api:5.9.0") testImplementation("org.junit.jupiter:junit-jupiter-api:5.9.0")

View file

@ -19,6 +19,7 @@ import org.bukkit.event.EventPriority.HIGHEST
import org.bukkit.event.Listener import org.bukkit.event.Listener
import org.bukkit.event.inventory.ClickType import org.bukkit.event.inventory.ClickType
import org.bukkit.event.inventory.InventoryClickEvent import org.bukkit.event.inventory.InventoryClickEvent
import org.bukkit.event.inventory.InventoryCloseEvent
import org.bukkit.event.inventory.PrepareAnvilEvent import org.bukkit.event.inventory.PrepareAnvilEvent
import org.bukkit.inventory.AnvilInventory import org.bukkit.inventory.AnvilInventory
import org.bukkit.inventory.InventoryView.Property.REPAIR_COST import org.bukkit.inventory.InventoryView.Property.REPAIR_COST
@ -26,6 +27,7 @@ import org.bukkit.inventory.ItemStack
import org.bukkit.inventory.meta.Repairable import org.bukkit.inventory.meta.Repairable
import xyz.alexcrea.cuanvil.config.ConfigHolder import xyz.alexcrea.cuanvil.config.ConfigHolder
import xyz.alexcrea.cuanvil.group.ConflictType import xyz.alexcrea.cuanvil.group.ConflictType
import xyz.alexcrea.cuanvil.packet.PacketManager
import xyz.alexcrea.cuanvil.recipe.AnvilCustomRecipe import xyz.alexcrea.cuanvil.recipe.AnvilCustomRecipe
import xyz.alexcrea.cuanvil.util.UnitRepairUtil.getRepair import xyz.alexcrea.cuanvil.util.UnitRepairUtil.getRepair
import kotlin.math.min import kotlin.math.min
@ -34,7 +36,7 @@ import kotlin.math.min
/** /**
* Listener for anvil events * Listener for anvil events
*/ */
class AnvilEventListener : Listener { class AnvilEventListener(private val packetManager: PacketManager) : Listener {
companion object { companion object {
// Anvil's output slot // Anvil's output slot
@ -544,13 +546,29 @@ class AnvilEventListener : Listener {
val player = event.view.player val player = event.view.player
if(player is Player){ if(player is Player){
if(player.gameMode != GameMode.CREATIVE){
packetManager.setInstantBuild(player, finalAnvilCost >= 40)
}
player.updateInventory() 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 class SlotContainer(val type: SlotType, val slot: Int)
private enum class SlotType { private enum class SlotType {

View file

@ -8,6 +8,9 @@ import xyz.alexcrea.cuanvil.command.EditConfigExecutor
import xyz.alexcrea.cuanvil.command.ReloadExecutor import xyz.alexcrea.cuanvil.command.ReloadExecutor
import xyz.alexcrea.cuanvil.config.ConfigHolder import xyz.alexcrea.cuanvil.config.ConfigHolder
import xyz.alexcrea.cuanvil.listener.ChatEventListener 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.Metrics
import xyz.alexcrea.cuanvil.util.MetricsUtil import xyz.alexcrea.cuanvil.util.MetricsUtil
import java.io.File import java.io.File
@ -66,14 +69,20 @@ class CustomAnvil : JavaPlugin() {
instance.logger.info(message) instance.logger.info(message)
} }
} }
} }
lateinit var packetManager: PacketManager
/** /**
* Setup plugin for use * Setup plugin for use
*/ */
override fun onEnable() { override fun onEnable() {
instance = this instance = this
val pluginManager = Bukkit.getPluginManager();
// Disable old plugin name if exist // Disable old plugin name if exist
val potentialPlugin = Bukkit.getPluginManager().getPlugin("UnsafeEnchantsPlus") val potentialPlugin = Bukkit.getPluginManager().getPlugin("UnsafeEnchantsPlus")
if (potentialPlugin != null) { if (potentialPlugin != null) {
@ -82,9 +91,15 @@ class CustomAnvil : JavaPlugin() {
logger.warning("Please note CustomAnvil is a more recent version of UnsafeEnchantsPlus") 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 // Load chat listener
chatListener = ChatEventListener() chatListener = ChatEventListener()
Bukkit.getPluginManager().registerEvents(chatListener, this) pluginManager.registerEvents(chatListener, this)
// Load config // Load config
val success = ConfigHolder.loadConfig() val success = ConfigHolder.loadConfig()
@ -98,7 +113,7 @@ class CustomAnvil : JavaPlugin() {
prepareCommand() prepareCommand()
server.pluginManager.registerEvents( server.pluginManager.registerEvents(
AnvilEventListener(), AnvilEventListener(packetManager),
this this
) )
} }

View 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) {}
}

View 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)
}

View file

@ -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()
}
}
}

View file

@ -1,7 +1,7 @@
main: io.delilaheve.CustomAnvil main: io.delilaheve.CustomAnvil
name: CustomAnvil name: CustomAnvil
prefix: "Custom Anvil" prefix: "Custom Anvil"
version: 1.4.4 version: 1.4.5
description: Allow to customise anvil mechanics description: Allow to customise anvil mechanics
api-version: 1.18 api-version: 1.18
load: POSTWORLD load: POSTWORLD
@ -45,3 +45,4 @@ permissions:
# as it is the old name for this plugin # as it is the old name for this plugin
softdepend: softdepend:
- UnsafeEnchantsPlus - UnsafeEnchantsPlus
- ProtocolLib