Skip to content

Commit

Permalink
feat : add 1.21 compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
fhebuterne committed Jul 6, 2024
1 parent a7b6a6d commit f0cb0b1
Show file tree
Hide file tree
Showing 11 changed files with 94 additions and 22 deletions.
1 change: 1 addition & 0 deletions common/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ dependencies {
implementation(project(":nms:v1_20_R2"))
implementation(project(":nms:v1_20_R3"))
implementation(project(":nms:v1_20_R4"))
implementation(project(":nms:v1_21_R1"))
}

tasks.processResources {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package fr.fabienhebuterne.pickspawner

import com.mojang.brigadier.builder.LiteralArgumentBuilder
import fr.fabienhebuterne.pickspawner.command.CommandsRegistration
import fr.fabienhebuterne.pickspawner.config.*
import fr.fabienhebuterne.pickspawner.module.ItemInitService
import fr.fabienhebuterne.pickspawner.module.breakspawner.*
import fr.fabienhebuterne.pickspawner.module.cancelenchant.EnchantItemEventListener
import fr.fabienhebuterne.pickspawner.module.cancelrepair.PlayerCommandPreprocessListener
import fr.fabienhebuterne.pickspawner.module.cancelrepair.PrepareAnvilEventListener
import fr.fabienhebuterne.pickspawner.module.commodore.CommodoreService
import fr.fabienhebuterne.pickspawner.module.entitydamage.EntityDamageByEntityEventListener
import fr.fabienhebuterne.pickspawner.module.interactspawner.PlayerInteractEventListener
import fr.fabienhebuterne.pickspawner.module.pickaxeMigration.PickaxeMigrationPlayerInteractEventListener
Expand Down Expand Up @@ -58,6 +58,7 @@ class PickSpawner : JavaPlugin() {
// checking version in class name has changed since 1.20.6
when (minecraftVersion) {
"1.20.6-R0.1-SNAPSHOT" -> Utils_1_20_R4()
"1.21-R0.1-SNAPSHOT" -> Utils_1_21_R1()
else -> {
Bukkit.getLogger().severe("Your server version $currentVersion / $minecraftVersion isn't compatible with PickSpawner")
server.pluginManager.disablePlugin(this)
Expand Down Expand Up @@ -108,17 +109,16 @@ class PickSpawner : JavaPlugin() {

private fun registerCommands(itemInitService: ItemInitService) {
val baseCommand = "pickspawner"
val pickSpawnerBaseCommodore = LiteralArgumentBuilder.literal<String>(baseCommand)
val commandsRegistration = CommandsRegistration(this, itemInitService, pickSpawnerBaseCommodore)
val commandsRegistration = CommandsRegistration(this, itemInitService, baseCommand)

// Register base command
val command = getCommand(baseCommand)
command?.setExecutor(commandsRegistration)

// Register commodore
val commodoreBuild = commandsRegistration.getCommodoreBuild()
val commodore = CommodoreProvider.getCommodore(this)
commodore.register(command, commodoreBuild) { commandsRegistration.hasCommodorePermission(it) }
if (CommodoreProvider.isSupported()) {
CommodoreService().init(this, commandsRegistration, command)
}
}

private fun setupEconomy(): Boolean {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,7 @@ class BuyCommand(
}

private fun buyCustomPickaxe(playerSender: Player, quantity: Int) {
if(!playerSender.hasPermission("pickspawner.buy.pickaxe"))
{
if (!playerSender.hasPermission("pickspawner.buy.pickaxe")) {
playerSender.sendMessage(instance.translationConfig.errors.missingPermission.toColorHex())
return
}
Expand Down Expand Up @@ -121,8 +120,7 @@ class BuyCommand(
}

private fun buyCustomPickaxeDurability(playerSender: Player, durability: Int) {
if(!playerSender.hasPermission("pickspawner.buy.durability"))
{
if (!playerSender.hasPermission("pickspawner.buy.durability")) {
playerSender.sendMessage(instance.translationConfig.errors.missingPermission.toColorHex())
return
}
Expand All @@ -138,8 +136,7 @@ class BuyCommand(
val damage = (itemMeta as Damageable).damage
val totalDurability = (maxDurability - damage) + durability

if(durability <= 0)
{
if (durability <= 0) {
playerSender.sendMessage(instance.translationConfig.errors.cancelBuyDurabilityBadDurability.toColorHex())
return
}
Expand Down Expand Up @@ -195,7 +192,12 @@ class BuyCommand(
playerSender.inventory.removeItem(ItemStack(materialName, quantityToBuy))
itemMeta.damage = damage - durability
itemInMainHand.itemMeta = itemInitService.refreshDurabilityOnItemMetaLore(itemMeta, maxDurability.toInt())
playerSender.sendMessage(instance.translationConfig.buyCustomPickaxeDurability.replace("{{durability}}", durability.toString()).toColorHex())
playerSender.sendMessage(
instance.translationConfig.buyCustomPickaxeDurability.replace(
"{{durability}}",
durability.toString()
).toColorHex()
)
}

private fun buyCustomPickaxeDurabilityOnMoneyEconomy(
Expand All @@ -220,7 +222,12 @@ class BuyCommand(
if (withdrawPlayer.transactionSuccess()) {
itemMeta.damage = damage - durability
itemInMainHand.itemMeta = itemInitService.refreshDurabilityOnItemMetaLore(itemMeta, maxDurability.toInt())
playerSender.sendMessage(instance.translationConfig.buyCustomPickaxeDurability.replace("{{durability}}", durability.toString()).toColorHex())
playerSender.sendMessage(
instance.translationConfig.buyCustomPickaxeDurability.replace(
"{{durability}}",
durability.toString()
).toColorHex()
)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ typealias CommandName = String
class CommandsRegistration(
private val instance: PickSpawner,
itemInitService: ItemInitService,
private val pickSpawnerBaseCommodore: LiteralArgumentBuilder<String>
baseCommand: String
) : CommandExecutor {

private val pickSpawnerBaseCommodore = LiteralArgumentBuilder.literal<String>(baseCommand)
private val commands: MutableMap<CommandName, CommandInfoInit> = mutableMapOf()

init {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class GiveCommand(
)

private fun LiteralArgumentBuilder<String>.entityTypesToCommodore(): LiteralArgumentBuilder<String> {
return EntityType.values().fold(this) { accumulator, entity ->
return EntityType.entries.fold(this) { accumulator, entity ->
accumulator.then(LiteralArgumentBuilder.literal(entity.name))
}
}
Expand Down Expand Up @@ -72,8 +72,7 @@ class GiveCommand(
) {
val damage = args.getOrNull(2)?.toIntOrNull() ?: 0
val result = player.inventory.addItem(itemInitService.initCustomPickaxeItemStack(damage))
if(!result.isEmpty())
{
if (result.isNotEmpty()) {
result.forEach { (_: Int, itemStack: ItemStack) ->
player.world.dropItem(player.location, itemStack);
}
Expand All @@ -88,8 +87,7 @@ class GiveCommand(
) {
val entityType = EntityType.valueOf(args[2].uppercase())
val result = player.inventory.addItem(itemInitService.initSpawnerItemStack(entityType))
if(!result.isEmpty())
{
if (result.isNotEmpty()) {
result.forEach { (_: Int, itemStack: ItemStack) ->
player.world.dropItem(player.location, itemStack);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package fr.fabienhebuterne.pickspawner.module.commodore

import fr.fabienhebuterne.pickspawner.command.CommandsRegistration
import me.lucko.commodore.CommodoreProvider
import org.bukkit.command.PluginCommand
import org.bukkit.plugin.java.JavaPlugin

class CommodoreService {

fun init(plugin: JavaPlugin, commandsRegistration: CommandsRegistration, command: PluginCommand?) {
val commodoreBuild = commandsRegistration.getCommodoreBuild()
val commodore = CommodoreProvider.getCommodore(plugin)
commodore.register(command, commodoreBuild) { commandsRegistration.hasCommodorePermission(it) }
}

}
1 change: 1 addition & 0 deletions install-dependency-in-cache.sh
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ versionsMd5=(
'1.20.2:spigot-1.20.2-R0.1-SNAPSHOT.jar:e7c864e4a13e2486e4509bf8cc89af3e'
'1.20.4:spigot-1.20.4-R0.1-SNAPSHOT.jar:44282b85c0c886d5abb812b13c8667de'
'1.20.6:spigot-1.20.6-R0.1-SNAPSHOT.jar:80814391c6c38ebcb1ea1b98632a9590'
'1.21:spigot-1.21-R0.1-SNAPSHOT.jar:9961507a636e6704086c0cf5705d8443'
)

for versionWithMd5 in "${versionsMd5[@]}"; do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ class Utils_1_20_R4 : Utils {
/**
* DataComponents.c is maxStackSize, other var is default value for blocks, see [DataComponents.af]
*/
val dataComponentsUpdated: DataComponentMap = DataComponentMap.a().a(DataComponents.c, 1)
val dataComponentsUpdated: DataComponentMap = DataComponentMap.a()
.a(DataComponents.c, maxStack)
.a(DataComponents.i, ItemLore.a)
.a(DataComponents.k, ItemEnchantments.a)
.a(DataComponents.r, 0)
Expand Down
9 changes: 9 additions & 0 deletions nms/v1_21_R1/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
plugins {
kotlin("jvm")
}

dependencies {
compileOnly(project(":nms:interfaces"))
compileOnly("org.spigotmc:spigot-api:1.21-R0.1-SNAPSHOT")
compileOnly(files("../../tmp/1.21/spigot-1.21-R0.1-SNAPSHOT.jar"))
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package fr.fabienhebuterne.pickspawner.nms

import net.minecraft.core.component.DataComponentMap
import net.minecraft.core.component.DataComponents
import net.minecraft.world.item.EnumItemRarity
import net.minecraft.world.item.Item
import net.minecraft.world.item.component.ItemAttributeModifiers
import net.minecraft.world.item.component.ItemLore
import net.minecraft.world.item.enchantment.ItemEnchantments
import net.minecraft.world.level.block.Blocks
import org.bukkit.Bukkit
import java.lang.reflect.Field

class Utils_1_21_R1 : Utils {

override fun setMaxStackSize(maxStack: Int) {
try {
/**
* DataComponents.c is maxStackSize, other var is default value for blocks, see [DataComponents.ag]
*/
val dataComponentsUpdated: DataComponentMap = DataComponentMap.a()
.a(DataComponents.c, maxStack)
.a(DataComponents.i, ItemLore.a)
.a(DataComponents.k, ItemEnchantments.a)
.a(DataComponents.r, 0)
.a(DataComponents.n, ItemAttributeModifiers.a)
.a(DataComponents.j, EnumItemRarity.a)
.a()
val dataComponentMapField: Field = Item::class.java.getDeclaredField("c")
dataComponentMapField.isAccessible = true
dataComponentMapField.set(Blocks.ct.r(), dataComponentsUpdated)
} catch (e: Exception) {
Bukkit.getLogger().warning("Can't update maxStackSize for spawner because : $e")
e.printStackTrace()
}
}

}
1 change: 1 addition & 0 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ include(
"nms:v1_20_R2",
"nms:v1_20_R3",
"nms:v1_20_R4",
"nms:v1_21_R1",
)

0 comments on commit f0cb0b1

Please sign in to comment.