-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #153 from Y2Kwastaken/main
Add 1.20.6 support
- Loading branch information
Showing
6 changed files
with
148 additions
and
1 deletion.
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
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
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
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,8 @@ | ||
plugins { | ||
id("basics.nms-module") | ||
id("io.papermc.paperweight.userdev") version "1.7.0" | ||
} | ||
|
||
dependencies { | ||
paperweight.paperDevBundle("1.20.6-R0.1-SNAPSHOT") | ||
} |
82 changes: 82 additions & 0 deletions
82
nms/versions/1.20.6/src/main/kotlin/com/github/spigotbasics/nms/v1_20_6/MenuBuilderImpl.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,82 @@ | ||
package com.github.spigotbasics.nms.v1_20_6 | ||
|
||
import net.minecraft.core.BlockPos | ||
import net.minecraft.network.chat.Component | ||
import net.minecraft.server.level.ServerPlayer | ||
import net.minecraft.world.entity.player.Inventory | ||
import net.minecraft.world.inventory.AbstractContainerMenu | ||
import net.minecraft.world.inventory.AnvilMenu | ||
import net.minecraft.world.inventory.CartographyTableMenu | ||
import net.minecraft.world.inventory.ContainerLevelAccess | ||
import net.minecraft.world.inventory.EnchantmentMenu | ||
import net.minecraft.world.inventory.GrindstoneMenu | ||
import net.minecraft.world.inventory.LoomMenu | ||
import net.minecraft.world.inventory.MenuConstructor | ||
import net.minecraft.world.inventory.SmithingMenu | ||
import net.minecraft.world.inventory.StonecutterMenu | ||
import net.minecraft.world.level.block.Block | ||
import net.minecraft.world.level.block.state.BlockState | ||
import org.bukkit.event.inventory.InventoryType | ||
|
||
object MenuBuilderImpl { | ||
private val builder = mutableMapOf<InventoryType, ContainerProvider>() | ||
private val titles = mutableMapOf<InventoryType, Component>() | ||
|
||
init { | ||
builder[InventoryType.ANVIL] = worldAccess(::AnvilMenu) | ||
builder[InventoryType.ENCHANTING] = worldAccess(::EnchantmentMenu) | ||
builder[InventoryType.LOOM] = worldAccess(::LoomMenu) | ||
builder[InventoryType.CARTOGRAPHY] = worldAccess(::CartographyTableMenu) | ||
builder[InventoryType.GRINDSTONE] = worldAccess(::GrindstoneMenu) | ||
builder[InventoryType.SMITHING] = worldAccess(::SmithingMenu) | ||
builder[InventoryType.STONECUTTER] = worldAccess(::StonecutterMenu) | ||
} | ||
|
||
init { | ||
titles[InventoryType.ANVIL] = Component.translatable("container.repair") | ||
titles[InventoryType.ENCHANTING] = Component.translatable("container.enchant") | ||
titles[InventoryType.LOOM] = Component.translatable("container.loom") | ||
titles[InventoryType.CARTOGRAPHY] = Component.translatable("container.cartography_table") | ||
titles[InventoryType.GRINDSTONE] = Component.translatable("container.grindstone_title") | ||
titles[InventoryType.SMITHING] = Component.translatable("container.upgrade") | ||
titles[InventoryType.STONECUTTER] = Component.translatable("container.stonecutter") | ||
} | ||
|
||
fun build( | ||
serverPlayer: ServerPlayer, | ||
inventoryType: InventoryType, | ||
): AbstractContainerMenu? { | ||
return (builder[inventoryType] ?: return null).supply(serverPlayer, serverPlayer.inventory) | ||
} | ||
|
||
fun title(inventoryType: InventoryType): Component? { | ||
return titles[inventoryType] | ||
} | ||
|
||
private fun interface ContainerProvider { | ||
fun supply( | ||
player: ServerPlayer, | ||
playerInventory: Inventory, | ||
): AbstractContainerMenu | ||
} | ||
|
||
private fun tile( | ||
block: Block, | ||
entity: (BlockPos, BlockState) -> MenuConstructor, | ||
): ContainerProvider { | ||
return ContainerProvider { player, inventory -> | ||
return@ContainerProvider entity.invoke(BlockPos.ZERO, block.defaultBlockState()) | ||
.createMenu(player.nextContainerCounter(), inventory, player)!! | ||
} | ||
} | ||
|
||
private fun worldAccess(accessor: (Int, Inventory, ContainerLevelAccess) -> AbstractContainerMenu): ContainerProvider { | ||
return ContainerProvider { player, inventory -> | ||
return@ContainerProvider accessor.invoke( | ||
player.nextContainerCounter(), | ||
inventory, | ||
ContainerLevelAccess.create(player.level(), player.blockPosition()), | ||
) | ||
} | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
nms/versions/1.20.6/src/main/kotlin/com/github/spigotbasics/nms/v1_20_6/NMSImpl_v1_20_6.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,51 @@ | ||
package com.github.spigotbasics.nms.v1_20_6 | ||
|
||
import com.github.spigotbasics.nms.NMSFacade | ||
import net.minecraft.network.protocol.game.ClientboundOpenScreenPacket | ||
import org.bukkit.Bukkit | ||
import org.bukkit.craftbukkit.CraftServer | ||
import org.bukkit.craftbukkit.entity.CraftPlayer | ||
import org.bukkit.entity.HumanEntity | ||
import org.bukkit.entity.Player | ||
import org.bukkit.event.inventory.InventoryOpenEvent | ||
import org.bukkit.event.inventory.InventoryType | ||
import org.bukkit.inventory.InventoryView | ||
import java.lang.IllegalStateException | ||
|
||
class NMSImpl_v1_20_6 : NMSFacade { | ||
override fun getTps(): DoubleArray { | ||
val serverHandle = (Bukkit.getServer() as CraftServer).handle.server | ||
return doubleArrayOf(serverHandle.tps1.average, serverHandle.tps5.average, serverHandle.tps15.average) | ||
} | ||
|
||
override fun openWorkbench( | ||
entity: HumanEntity, | ||
type: InventoryType, | ||
): InventoryView? { | ||
if (entity !is Player) { | ||
throw IllegalStateException("this player does not have a connection so no packets could be sent") | ||
} | ||
|
||
val serverPlayer = (entity as CraftPlayer).handle | ||
|
||
val resultMenu = | ||
MenuBuilderImpl.build(serverPlayer, type) | ||
?: throw IllegalArgumentException("The given inventory type can not be used to create a workbench") | ||
resultMenu.title = MenuBuilderImpl.title(type) | ||
?: throw IllegalArgumentException("the given inventory title was not found with the specified workbench") | ||
resultMenu.checkReachable = false | ||
|
||
// Now that menu setup is done we will call the intended event in a much less naive manner as to support components | ||
val event = InventoryOpenEvent(resultMenu.bukkitView) | ||
Bukkit.getPluginManager().callEvent(event) | ||
if (event.isCancelled) { | ||
return null | ||
} | ||
|
||
val menuType = resultMenu.type | ||
serverPlayer.connection.send(ClientboundOpenScreenPacket(resultMenu.containerId, menuType, resultMenu.title)) | ||
serverPlayer.containerMenu = resultMenu | ||
serverPlayer.initMenu(resultMenu) | ||
return resultMenu.bukkitView | ||
} | ||
} |