Compare commits
4
Commits
b4b211e318
...
0.0.2
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
78ee8527b4 | ||
|
|
98e90565c4 | ||
|
|
8c6adfd18a | ||
|
|
e61c1d2657 |
No files matched your search
@@ -8,5 +8,4 @@ The KotlinBukkitKit Tooling is plugin for IntelliJ that helps developers using [
|
||||
|
||||
- Generator of KotlinBukkitKIT project
|
||||
|
||||
![Preview] (https://red-tea.gitbook.io/~gitbook/image?url=https:%2F%2F630270463-files.gitbook.io%2F%7E%2Ffiles%2Fv0%2Fb%2Fgitbook-x-prod.appspot.com%2Fo%2Fspaces%252F1PBqxCHnbDKJwzj8IhJk%252Fuploads%252Fo2jPveKWzw47uGTxMm2Q%252Fimage.png%3Falt=media%26token=403c70a1-fa12-435d-99c2-aab79493de97&width=400&dpr=3&quality=100&sign=5ca18a6e18c88ceb0b5a355577cfb45cadea88b46003ac032ac4f52d7cfe52b0)
|
||||
|
||||
<img src="https://red-tea.gitbook.io/~gitbook/image?url=https:%2F%2F630270463-files.gitbook.io%2F%7E%2Ffiles%2Fv0%2Fb%2Fgitbook-x-prod.appspot.com%2Fo%2Fspaces%252F1PBqxCHnbDKJwzj8IhJk%252Fuploads%252Fo2jPveKWzw47uGTxMm2Q%252Fimage.png%3Falt=media%26token=403c70a1-fa12-435d-99c2-aab79493de97&width=400&dpr=3&quality=100&sign=5ca18a6e18c88ceb0b5a355577cfb45cadea88b46003ac032ac4f52d7cfe52b0">
|
||||
@@ -0,0 +1,14 @@
|
||||
package tech.carcadex.kotlinbukkitkit.tooling.menu
|
||||
|
||||
data class MenuDeclaration(
|
||||
val displayname: String,
|
||||
val lines: Int,
|
||||
val slots: List<MenuSlotDeclaration>
|
||||
)
|
||||
|
||||
data class MenuSlotDeclaration(
|
||||
val line: Int,
|
||||
val slot: Int,
|
||||
val item: String,
|
||||
val isSelected: Boolean = false
|
||||
)
|
||||
@@ -0,0 +1,29 @@
|
||||
package tech.carcadex.kotlinbukkitkit.tooling.menu
|
||||
|
||||
import com.intellij.util.ImageLoader
|
||||
import tech.carcadex.kotlinbukkitkit.tooling.Assets
|
||||
import java.awt.Graphics
|
||||
import java.awt.Image
|
||||
import java.io.File
|
||||
import java.io.IOException
|
||||
import javax.imageio.ImageIO
|
||||
import javax.swing.JPanel
|
||||
|
||||
|
||||
class ImagePanel : JPanel() {
|
||||
private var image: Image? = null
|
||||
|
||||
init {
|
||||
|
||||
try {
|
||||
image = ImageIO.read((File(MenuPreviewFileEditorProvider.ASSETS_FOLDER.absolutePath + "/items-13/acacia_boat.png")))
|
||||
} catch (ex: IOException) {
|
||||
// handle exception...
|
||||
}
|
||||
}
|
||||
|
||||
override fun paintComponent(g: Graphics) {
|
||||
super.paintComponent(g)
|
||||
g.drawImage(image, 0, 0, this) // see javadoc for more info on the parameters
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,123 @@
|
||||
package tech.carcadex.kotlinbukkitkit.tooling.menu
|
||||
|
||||
import com.intellij.util.ImageLoader
|
||||
import org.jetbrains.kotlin.utils.keysToMap
|
||||
import tech.carcadex.kotlinbukkitkit.tooling.Assets
|
||||
import java.awt.*
|
||||
import java.io.File
|
||||
import javax.imageio.ImageIO
|
||||
import javax.swing.JPanel
|
||||
|
||||
class InventoryComponent(
|
||||
val declaration: MenuDeclaration
|
||||
) : JPanel() {
|
||||
companion object {
|
||||
val CHEST_SCALE = 2
|
||||
val CHEST_SINGLE_LINE_HEIGHT = 42
|
||||
val CHEST_LINE_DIFF = 18
|
||||
val CHEST_WIDTH = 176
|
||||
|
||||
private val minecraftFontResource = MenuPreviewFileEditorProvider.ASSETS_FOLDER.absolutePath + "/fonts/Minecraftia-Regular.ttf"
|
||||
private val spritesFolder = MenuPreviewFileEditorProvider.ASSETS_FOLDER.absolutePath + "/sprites"
|
||||
private val itemsSpriteFolder = MenuPreviewFileEditorProvider.ASSETS_FOLDER.absolutePath + "/items"
|
||||
private val items13SpriteFolder = MenuPreviewFileEditorProvider.ASSETS_FOLDER.absolutePath + "/items-13"
|
||||
|
||||
// the selected-border is 36x36 and not 32x32
|
||||
private const val selectedBorderSizeDiff = 2
|
||||
}
|
||||
|
||||
private val lines = when {
|
||||
declaration.lines > 6 -> 6
|
||||
declaration.lines < 1 -> 1
|
||||
else -> declaration.lines
|
||||
}
|
||||
|
||||
private val spriteImage = ImageIO.read(File(
|
||||
"$spritesFolder/chest-${lines}.png")
|
||||
)!!
|
||||
private val itemsImage = declaration.slots.filter {
|
||||
it.line in 1..declaration.lines && it.slot in 1..9
|
||||
}.keysToMap {
|
||||
// TODO: Support data
|
||||
val item13 = runCatching { MinecraftItem13.valueOf(it.item) }.getOrNull()
|
||||
|
||||
if(item13 != null) {
|
||||
ImageIO.read(File("$items13SpriteFolder/${item13.name.lowercase()}.png"))
|
||||
} else {
|
||||
val item = runCatching { MinecraftItem.valueOf(it.item) }.getOrNull()
|
||||
|
||||
if (item != null) {
|
||||
ImageIO.read(File("$itemsSpriteFolder/${item.id}-0.png"))
|
||||
} else {
|
||||
null
|
||||
}
|
||||
}
|
||||
}
|
||||
private val selectionImage = if(declaration.slots.any { it.isSelected })
|
||||
ImageIO.read(File("$spritesFolder/selected-border.png"))
|
||||
else null
|
||||
|
||||
private val minecraftFont = Font.createFont(
|
||||
Font.TRUETYPE_FONT,
|
||||
File(minecraftFontResource)
|
||||
).deriveFont(18.5f)
|
||||
|
||||
init {
|
||||
isVisible = true
|
||||
minimumSize = calculateDimension()
|
||||
}
|
||||
override fun paintComponent(g: Graphics) {
|
||||
val g = g as Graphics2D
|
||||
val oldTransform = g.transform
|
||||
|
||||
// drawing menu
|
||||
g.scale(CHEST_SCALE.toDouble(), CHEST_SCALE.toDouble())
|
||||
g.drawImage(spriteImage, 0, 0,this)
|
||||
|
||||
g.transform = oldTransform
|
||||
|
||||
// drawing items
|
||||
for ((slot, texture) in itemsImage) {
|
||||
val (x, y) = calculateXYForSlot(slot)
|
||||
|
||||
g.drawImage(texture ?: continue, x, y, this)
|
||||
|
||||
// if is selected draw selection border
|
||||
if(slot.isSelected) {
|
||||
g.drawImage(
|
||||
selectionImage ?: continue,
|
||||
x- selectedBorderSizeDiff,
|
||||
y- selectedBorderSizeDiff,
|
||||
this
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
// drawing displayname
|
||||
g.font = minecraftFont
|
||||
g.color = Color.DARK_GRAY
|
||||
g.drawString(declaration.displayname, 16, 40)
|
||||
}
|
||||
|
||||
fun calculateXYForSlot(menuSlotDeclaration: MenuSlotDeclaration): Pair<Int, Int> {
|
||||
val topXPadding = 18 * CHEST_SCALE
|
||||
val leftYPadding = 8 * CHEST_SCALE
|
||||
val itemSize = 16 * CHEST_SCALE
|
||||
val rightYPadding = 2 * CHEST_SCALE
|
||||
val bottomXPadding = 2 * CHEST_SCALE
|
||||
|
||||
val itemXPos = menuSlotDeclaration.line -1
|
||||
val itemYPos = menuSlotDeclaration.slot -1
|
||||
|
||||
val x = leftYPadding + (itemYPos * rightYPadding) + (itemYPos * itemSize)
|
||||
val y = topXPadding + (itemXPos * bottomXPadding) + (itemXPos * itemSize)
|
||||
|
||||
return x to y
|
||||
}
|
||||
|
||||
private fun calculateDimension(): Dimension {
|
||||
val height = CHEST_SINGLE_LINE_HEIGHT + (lines - 1) * CHEST_LINE_DIFF
|
||||
|
||||
return Dimension((CHEST_WIDTH * CHEST_SCALE).toInt(), (height * CHEST_SCALE).toInt())
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,200 @@
|
||||
package tech.carcadex.kotlinbukkitkit.tooling.menu
|
||||
|
||||
import com.esotericsoftware.kryo.kryo5.minlog.Log
|
||||
import com.intellij.codeInsight.codeVision.ui.mouseClicked
|
||||
import com.intellij.collaboration.ui.VerticalListPanel
|
||||
import com.intellij.openapi.editor.event.CaretEvent
|
||||
import com.intellij.openapi.editor.event.CaretListener
|
||||
import com.intellij.openapi.editor.event.DocumentEvent
|
||||
import com.intellij.openapi.editor.event.DocumentListener
|
||||
import com.intellij.openapi.fileEditor.*
|
||||
import com.intellij.openapi.project.Project
|
||||
import com.intellij.openapi.util.UserDataHolderBase
|
||||
import com.intellij.openapi.vfs.VirtualFile
|
||||
import com.intellij.platform.ide.progress.ModalTaskOwner.component
|
||||
import com.intellij.psi.PsiDocumentManager
|
||||
import com.intellij.ui.components.JBScrollPane
|
||||
import com.intellij.ui.layout.panel
|
||||
import com.intellij.util.Alarm
|
||||
import org.jetbrains.kotlin.psi.KtCallExpression
|
||||
import org.jetbrains.kotlin.psi.psiUtil.forEachDescendantOfType
|
||||
import java.awt.*
|
||||
import java.beans.PropertyChangeListener
|
||||
import javax.swing.*
|
||||
|
||||
class MenuPreviewFileEditor(
|
||||
private val myProject: Project,
|
||||
private val myFile: VirtualFile,
|
||||
private val mainEditor: TextEditor
|
||||
) : UserDataHolderBase(), FileEditor {
|
||||
|
||||
companion object {
|
||||
const val REBUILD_DELAY_MILLISECONDS = 80
|
||||
}
|
||||
|
||||
private val mySwingAlarm = Alarm(Alarm.ThreadToUse.SWING_THREAD, this)
|
||||
private val myDocument = FileDocumentManager.getInstance().getDocument(myFile)!!
|
||||
private val myUi = JPanel().apply {
|
||||
layout = BorderLayout()
|
||||
isVisible = true
|
||||
minimumSize = Dimension(
|
||||
InventoryComponent.CHEST_WIDTH * InventoryComponent.CHEST_SCALE + 10,
|
||||
0
|
||||
)
|
||||
}
|
||||
|
||||
private var currentScroll: JBScrollPane? = null
|
||||
private var currentSelectedLine: Int? = null
|
||||
|
||||
private val documentListener = object : DocumentListener {
|
||||
override fun beforeDocumentChange(event: DocumentEvent) {
|
||||
mySwingAlarm.cancelAllRequests()
|
||||
}
|
||||
override fun documentChanged(event: DocumentEvent) {
|
||||
mySwingAlarm.addRequest({
|
||||
rebuild(currentScroll?.viewport?.viewPosition)
|
||||
}, REBUILD_DELAY_MILLISECONDS)
|
||||
}
|
||||
}
|
||||
|
||||
private val caretListener = object : CaretListener {
|
||||
override fun caretPositionChanged(event: CaretEvent) {
|
||||
|
||||
currentSelectedLine = event.newPosition.line
|
||||
mySwingAlarm.addRequest({
|
||||
rebuild(currentScroll?.viewport?.viewPosition)
|
||||
}, REBUILD_DELAY_MILLISECONDS)
|
||||
}
|
||||
}
|
||||
|
||||
var registered = false
|
||||
fun setup() {
|
||||
if(registered) return
|
||||
rebuild(null)
|
||||
|
||||
// listen to the source code changes to rebuild the UI
|
||||
myDocument.addDocumentListener(documentListener)
|
||||
|
||||
// listen to cursor changes to make a slot selectable
|
||||
mainEditor.editor.caretModel.addCaretListener(caretListener)
|
||||
registered = true
|
||||
}
|
||||
|
||||
fun rebuild(currentViewPosition: Point?) {
|
||||
if(!myUi.isShowing) return
|
||||
|
||||
val tree = PsiDocumentManager.getInstance(myProject).getPsiFile(myDocument) ?: return
|
||||
|
||||
// clear the old UI
|
||||
myUi.removeAll()
|
||||
|
||||
// create the scroll pane with a centralized component with the inventories
|
||||
var menuCount = 0
|
||||
|
||||
currentScroll = JBScrollPane(
|
||||
panel {
|
||||
tree.forEachDescendantOfType<KtCallExpression> {
|
||||
val declaration =
|
||||
findMenuDeclaration(
|
||||
it,
|
||||
currentSelectedLine,
|
||||
myFile
|
||||
)
|
||||
if (declaration != null) {
|
||||
row {
|
||||
component(
|
||||
InventoryComponent(
|
||||
declaration
|
||||
)
|
||||
)
|
||||
}
|
||||
menuCount++
|
||||
}
|
||||
}
|
||||
}.centerComponent(),
|
||||
ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
|
||||
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER
|
||||
).apply {
|
||||
if(currentViewPosition != null)
|
||||
viewport.viewPosition = currentViewPosition
|
||||
}
|
||||
|
||||
|
||||
|
||||
if(menuCount > 0) {
|
||||
// add the scroll pane to the UI
|
||||
myUi.add(
|
||||
currentScroll,
|
||||
BorderLayout.CENTER
|
||||
)
|
||||
} else {
|
||||
myUi.add(
|
||||
JLabel("Could not render menu preview")
|
||||
)
|
||||
}
|
||||
// val p = Panel()
|
||||
// p.setSize(50, 100)
|
||||
// p.background = Color.CYAN
|
||||
// myUi.add(p)
|
||||
}
|
||||
|
||||
|
||||
override fun isModified(): Boolean = false
|
||||
|
||||
override fun isValid(): Boolean = true
|
||||
|
||||
override fun addPropertyChangeListener(listener: PropertyChangeListener) {
|
||||
|
||||
}
|
||||
|
||||
override fun removePropertyChangeListener(listener: PropertyChangeListener) {
|
||||
}
|
||||
|
||||
override fun getName(): String = "KotlinBukkitAPI Menu Viewer"
|
||||
|
||||
override fun setState(state: FileEditorState) {
|
||||
|
||||
}
|
||||
|
||||
override fun getComponent(): JComponent {
|
||||
return myUi
|
||||
}
|
||||
|
||||
override fun getPreferredFocusedComponent(): JComponent? {
|
||||
return null
|
||||
}
|
||||
|
||||
override fun getCurrentLocation(): FileEditorLocation? {
|
||||
return null
|
||||
}
|
||||
|
||||
override fun selectNotify() {
|
||||
setup()
|
||||
}
|
||||
|
||||
override fun deselectNotify() {
|
||||
dispose()
|
||||
}
|
||||
|
||||
|
||||
override fun dispose() {
|
||||
// mySwingAlarm.cancelAllRequests()
|
||||
// try {
|
||||
// if(!registered) return
|
||||
// myDocument.removeDocumentListener(documentListener)
|
||||
// mainEditor.editor.caretModel.removeCaretListener(caretListener)
|
||||
// } catch (e: Throwable) {
|
||||
// Log.warn(e.message)
|
||||
// }
|
||||
|
||||
}
|
||||
|
||||
private fun JComponent.centerComponent(): JPanel {
|
||||
return JPanel().apply {
|
||||
isVisible = true
|
||||
layout = GridBagLayout()
|
||||
|
||||
add(this@centerComponent)
|
||||
}
|
||||
}
|
||||
}
|
||||
+106
@@ -0,0 +1,106 @@
|
||||
package tech.carcadex.kotlinbukkitkit.tooling.menu
|
||||
|
||||
import com.intellij.openapi.fileEditor.*
|
||||
import com.intellij.openapi.fileEditor.impl.text.TextEditorProvider
|
||||
import com.intellij.openapi.module.ModuleUtilCore
|
||||
import com.intellij.openapi.project.Project
|
||||
import com.intellij.openapi.roots.ModuleRootManager
|
||||
import com.intellij.openapi.vfs.VirtualFile
|
||||
import com.intellij.openapi.vfs.readText
|
||||
import org.jetbrains.kotlin.idea.KotlinFileType
|
||||
import org.jetbrains.kotlin.incremental.createDirectory
|
||||
import tech.carcadex.kotlinbukkitkit.tooling.Assets
|
||||
import java.io.*
|
||||
import java.nio.file.Files
|
||||
import java.nio.file.Path
|
||||
import java.util.zip.ZipEntry
|
||||
import java.util.zip.ZipInputStream
|
||||
|
||||
|
||||
class MenuPreviewFileEditorProvider : WeighedFileEditorProvider() {
|
||||
|
||||
companion object {
|
||||
val MAIN_FOLDER = File("kbk-assets")
|
||||
val ASSETS_FOLDER = File("kbk-assets/assets")
|
||||
const val ID = "kotlinbukkitkit-menu-preview"
|
||||
init {
|
||||
MAIN_FOLDER.createDirectory()
|
||||
val zip = File(MAIN_FOLDER, "assets.zip")
|
||||
if(!zip.exists()) {
|
||||
zip.createNewFile()
|
||||
val input: InputStream = Assets::class.java.getResourceAsStream("/assets.zip")
|
||||
zip.writeBytes(input.readAllBytes())
|
||||
|
||||
fun unzip(`is`: InputStream?, targetDir: Path) {
|
||||
var targetDir: Path = targetDir
|
||||
targetDir = targetDir.toAbsolutePath()
|
||||
ZipInputStream(`is`).use { zipIn ->
|
||||
var ze: ZipEntry
|
||||
while (zipIn.nextEntry.also {
|
||||
if (it != null) {
|
||||
ze = it
|
||||
val resolvedPath: Path = targetDir.resolve(ze.name).normalize()
|
||||
if (!resolvedPath.startsWith(targetDir)) {
|
||||
throw RuntimeException(
|
||||
"Entry with an illegal path: "
|
||||
+ ze.name
|
||||
)
|
||||
}
|
||||
if (ze.isDirectory) {
|
||||
Files.createDirectories(resolvedPath)
|
||||
} else {
|
||||
Files.createDirectories(resolvedPath.getParent())
|
||||
Files.copy(zipIn, resolvedPath)
|
||||
}
|
||||
}
|
||||
} != null) {
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
unzip(FileInputStream(zip), MAIN_FOLDER.toPath())
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun getEditorTypeId(): String = ID
|
||||
|
||||
override fun accept(project: Project, file: VirtualFile): Boolean {
|
||||
val type = file.fileType
|
||||
|
||||
if(type != KotlinFileType.INSTANCE) return false
|
||||
|
||||
val module = ModuleUtilCore.findModuleForFile(file, project) ?: return false
|
||||
|
||||
val root = ModuleRootManager.getInstance(module)
|
||||
var hasKotlinBukkitAPI = false
|
||||
|
||||
root.orderEntries().withoutSdk().forEachLibrary {
|
||||
if((it.name ?: "").contains("kotlinbukkitkit", true)) {
|
||||
hasKotlinBukkitAPI = true
|
||||
|
||||
false
|
||||
} else {
|
||||
true
|
||||
}
|
||||
}
|
||||
|
||||
if(!file.readText().contains("tech.carcadex.kotlinbukkitkit.menu.dsl.menu")) hasKotlinBukkitAPI = false;
|
||||
|
||||
return hasKotlinBukkitAPI
|
||||
}
|
||||
|
||||
override fun createEditor(project: Project, file: VirtualFile): FileEditor {
|
||||
val editor: TextEditor = TextEditorProvider.getInstance().createEditor(project, file) as TextEditor
|
||||
val preview = MenuPreviewFileEditor(project, file, editor)
|
||||
val result = TextEditorWithPreview(editor, preview, "KotlinBukkitKit")
|
||||
return result
|
||||
}
|
||||
|
||||
//override fun getPolicy(): FileEditorPolicy = FileEditorPolicy.HIDE_DEFAULT_EDITOR
|
||||
override fun getPolicy(): FileEditorPolicy = FileEditorPolicy.HIDE_DEFAULT_EDITOR
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,390 @@
|
||||
package tech.carcadex.kotlinbukkitkit.tooling.menu
|
||||
|
||||
enum class MinecraftItem(val id: Int) {
|
||||
AIR(0),
|
||||
STONE(1),
|
||||
GRASS(2),
|
||||
DIRT(3),
|
||||
COBBLESTONE(4),
|
||||
WOOD(5),
|
||||
SAPLING(6),
|
||||
BEDROCK(7),
|
||||
WATER(8),
|
||||
STATIONARY_WATER(9),
|
||||
LAVA(10), STATIONARY_LAVA(11),
|
||||
SAND(12),
|
||||
GRAVEL(13),
|
||||
GOLD_ORE(14),
|
||||
IRON_ORE(15),
|
||||
COAL_ORE(16),
|
||||
LOG(17),
|
||||
LEAVES(18),
|
||||
SPONGE(19),
|
||||
GLASS(20),
|
||||
LAPIS_ORE(21),
|
||||
LAPIS_BLOCK(22),
|
||||
DISPENSER(23),
|
||||
SANDSTONE(24),
|
||||
NOTE_BLOCK(25),
|
||||
BED_BLOCK(26),
|
||||
POWERED_RAIL(27),
|
||||
DETECTOR_RAIL(28),
|
||||
PISTON_STICKY_BASE(29),
|
||||
WEB(30),
|
||||
LONG_GRASS(31),
|
||||
DEAD_BUSH(32),
|
||||
PISTON_BASE(33),
|
||||
PISTON_EXTENSION(34),
|
||||
WOOL(35),
|
||||
PISTON_MOVING_PIECE(36),
|
||||
YELLOW_FLOWER(37),
|
||||
RED_ROSE(38),
|
||||
BROWN_MUSHROOM(39),
|
||||
RED_MUSHROOM(40),
|
||||
GOLD_BLOCK(41),
|
||||
IRON_BLOCK(42),
|
||||
DOUBLE_STEP(43),
|
||||
STEP(44),
|
||||
BRICK(45),
|
||||
TNT(46),
|
||||
BOOKSHELF(47),
|
||||
MOSSY_COBBLESTONE(48),
|
||||
OBSIDIAN(49),
|
||||
TORCH(50),
|
||||
FIRE(51),
|
||||
MOB_SPAWNER(52),
|
||||
WOOD_STAIRS(53),
|
||||
CHEST(54),
|
||||
REDSTONE_WIRE(55),
|
||||
DIAMOND_ORE(56),
|
||||
DIAMOND_BLOCK(57),
|
||||
WORKBENCH(58),
|
||||
CROPS(59),
|
||||
SOIL(60),
|
||||
FURNACE(61),
|
||||
BURNING_FURNACE(62),
|
||||
SIGN_POST(63),
|
||||
WOODEN_DOOR(64),
|
||||
LADDER(65),
|
||||
RAILS(66),
|
||||
COBBLESTONE_STAIRS(67),
|
||||
WALL_SIGN(68),
|
||||
LEVER(69),
|
||||
STONE_PLATE(70),
|
||||
IRON_DOOR_BLOCK(71),
|
||||
WOOD_PLATE(72),
|
||||
REDSTONE_ORE(73),
|
||||
GLOWING_REDSTONE_ORE(74),
|
||||
REDSTONE_TORCH_OFF(75),
|
||||
REDSTONE_TORCH_ON(76),
|
||||
STONE_BUTTON(77),
|
||||
SNOW(78),
|
||||
ICE(79),
|
||||
SNOW_BLOCK(80),
|
||||
CACTUS(81),
|
||||
CLAY(82),
|
||||
SUGAR_CANE_BLOCK(83),
|
||||
JUKEBOX(84),
|
||||
FENCE(85),
|
||||
PUMPKIN(86),
|
||||
NETHERRACK(87),
|
||||
SOUL_SAND(88),
|
||||
GLOWSTONE(89),
|
||||
PORTAL(90),
|
||||
JACK_O_LANTERN(91),
|
||||
CAKE_BLOCK(92),
|
||||
DIODE_BLOCK_OFF(93),
|
||||
DIODE_BLOCK_ON(94),
|
||||
STAINED_GLASS(95),
|
||||
TRAP_DOOR(96),
|
||||
MONSTER_EGGS(97),
|
||||
SMOOTH_BRICK(98),
|
||||
HUGE_MUSHROOM_1(99),
|
||||
HUGE_MUSHROOM_2(100),
|
||||
IRON_FENCE(101),
|
||||
THIN_GLASS(102),
|
||||
MELON_BLOCK(103),
|
||||
PUMPKIN_STEM(104),
|
||||
MELON_STEM(105),
|
||||
VINE(106),
|
||||
FENCE_GATE(107),
|
||||
BRICK_STAIRS(108),
|
||||
SMOOTH_STAIRS(109),
|
||||
MYCEL(110),
|
||||
WATER_LILY(111),
|
||||
NETHER_BRICK(112),
|
||||
NETHER_FENCE(113),
|
||||
NETHER_BRICK_STAIRS(114),
|
||||
NETHER_WARTS(115),
|
||||
ENCHANTMENT_TABLE(116),
|
||||
BREWING_STAND(117),
|
||||
CAULDRON(118),
|
||||
ENDER_PORTAL(119),
|
||||
ENDER_PORTAL_FRAME(120),
|
||||
ENDER_STONE(121),
|
||||
DRAGON_EGG(122),
|
||||
REDSTONE_LAMP_OFF(123),
|
||||
REDSTONE_LAMP_ON(124),
|
||||
WOOD_DOUBLE_STEP(125),
|
||||
WOOD_STEP(126),
|
||||
COCOA(127),
|
||||
SANDSTONE_STAIRS(128),
|
||||
EMERALD_ORE(129),
|
||||
ENDER_CHEST(130),
|
||||
TRIPWIRE_HOOK(131),
|
||||
TRIPWIRE(132),
|
||||
EMERALD_BLOCK(133),
|
||||
SPRUCE_WOOD_STAIRS(134),
|
||||
BIRCH_WOOD_STAIRS(135),
|
||||
JUNGLE_WOOD_STAIRS(136),
|
||||
COMMAND(137),
|
||||
BEACON(138),
|
||||
COBBLE_WALL(139),
|
||||
FLOWER_POT(140),
|
||||
CARROT(141),
|
||||
POTATO(142),
|
||||
WOOD_BUTTON(143),
|
||||
SKULL(144),
|
||||
ANVIL(145),
|
||||
TRAPPED_CHEST(146),
|
||||
GOLD_PLATE(147),
|
||||
IRON_PLATE(148),
|
||||
REDSTONE_COMPARATOR_OFF(149),
|
||||
REDSTONE_COMPARATOR_ON(150),
|
||||
DAYLIGHT_DETECTOR(151),
|
||||
REDSTONE_BLOCK(152),
|
||||
QUARTZ_ORE(153),
|
||||
HOPPER(154),
|
||||
QUARTZ_BLOCK(155),
|
||||
QUARTZ_STAIRS(156),
|
||||
ACTIVATOR_RAIL(157),
|
||||
DROPPER(158),
|
||||
STAINED_CLAY(159),
|
||||
STAINED_GLASS_PANE(160),
|
||||
LEAVES_2(161),
|
||||
LOG_2(162),
|
||||
ACACIA_STAIRS(163),
|
||||
DARK_OAK_STAIRS(164),
|
||||
SLIME_BLOCK(165),
|
||||
BARRIER(166),
|
||||
IRON_TRAPDOOR(167),
|
||||
PRISMARINE(168),
|
||||
SEA_LANTERN(169),
|
||||
HAY_BLOCK(170),
|
||||
CARPET(171),
|
||||
HARD_CLAY(172),
|
||||
COAL_BLOCK(173),
|
||||
PACKED_ICE(174),
|
||||
DOUBLE_PLANT(175),
|
||||
STANDING_BANNER(176),
|
||||
WALL_BANNER(177),
|
||||
DAYLIGHT_DETECTOR_INVERTED(178),
|
||||
RED_SANDSTONE(179),
|
||||
RED_SANDSTONE_STAIRS(180),
|
||||
DOUBLE_STONE_SLAB2(181),
|
||||
STONE_SLAB2(182),
|
||||
SPRUCE_FENCE_GATE(183),
|
||||
BIRCH_FENCE_GATE(184),
|
||||
JUNGLE_FENCE_GATE(185),
|
||||
DARK_OAK_FENCE_GATE(186),
|
||||
ACACIA_FENCE_GATE(187),
|
||||
SPRUCE_FENCE(188),
|
||||
BIRCH_FENCE(189),
|
||||
JUNGLE_FENCE(190),
|
||||
DARK_OAK_FENCE(191),
|
||||
ACACIA_FENCE(192),
|
||||
SPRUCE_DOOR(193),
|
||||
BIRCH_DOOR(194),
|
||||
JUNGLE_DOOR(195),
|
||||
ACACIA_DOOR(196),
|
||||
DARK_OAK_DOOR(197),
|
||||
|
||||
// ----- Item Separator -----
|
||||
IRON_SPADE(256),
|
||||
IRON_PICKAXE(257),
|
||||
IRON_AXE(258),
|
||||
FLINT_AND_STEEL(259),
|
||||
APPLE(260),
|
||||
BOW(261),
|
||||
ARROW(262), COAL(263),
|
||||
DIAMOND(264),
|
||||
IRON_INGOT(265),
|
||||
GOLD_INGOT(266),
|
||||
IRON_SWORD(267),
|
||||
WOOD_SWORD(268),
|
||||
WOOD_SPADE(269),
|
||||
WOOD_PICKAXE(270),
|
||||
WOOD_AXE(271),
|
||||
STONE_SWORD(272),
|
||||
STONE_SPADE(273),
|
||||
STONE_PICKAXE(274),
|
||||
STONE_AXE(275),
|
||||
DIAMOND_SWORD(276),
|
||||
DIAMOND_SPADE(277),
|
||||
DIAMOND_PICKAXE(278),
|
||||
DIAMOND_AXE(279),
|
||||
STICK(280),
|
||||
BOWL(281),
|
||||
MUSHROOM_SOUP(282),
|
||||
GOLD_SWORD(283),
|
||||
GOLD_SPADE(284),
|
||||
GOLD_PICKAXE(285),
|
||||
GOLD_AXE(286),
|
||||
STRING(287),
|
||||
FEATHER(288),
|
||||
SULPHUR(289),
|
||||
WOOD_HOE(290),
|
||||
STONE_HOE(291),
|
||||
IRON_HOE(292),
|
||||
DIAMOND_HOE(293),
|
||||
GOLD_HOE(294),
|
||||
SEEDS(295),
|
||||
WHEAT(296),
|
||||
BREAD(297),
|
||||
LEATHER_HELMET(298),
|
||||
LEATHER_CHESTPLATE(299),
|
||||
LEATHER_LEGGINGS(300),
|
||||
LEATHER_BOOTS(301),
|
||||
CHAINMAIL_HELMET(302),
|
||||
CHAINMAIL_CHESTPLATE(303),
|
||||
CHAINMAIL_LEGGINGS(304),
|
||||
CHAINMAIL_BOOTS(305),
|
||||
IRON_HELMET(306),
|
||||
IRON_CHESTPLATE(307),
|
||||
IRON_LEGGINGS(308),
|
||||
IRON_BOOTS(309),
|
||||
DIAMOND_HELMET(310),
|
||||
DIAMOND_CHESTPLATE(311),
|
||||
DIAMOND_LEGGINGS(312),
|
||||
DIAMOND_BOOTS(313),
|
||||
GOLD_HELMET(314),
|
||||
GOLD_CHESTPLATE(315),
|
||||
GOLD_LEGGINGS(316),
|
||||
GOLD_BOOTS(317),
|
||||
FLINT(318),
|
||||
PORK(319),
|
||||
GRILLED_PORK(320),
|
||||
PAINTING(321),
|
||||
GOLDEN_APPLE(322),
|
||||
SIGN(323),
|
||||
WOOD_DOOR(324),
|
||||
BUCKET(325),
|
||||
WATER_BUCKET(326),
|
||||
LAVA_BUCKET(327),
|
||||
MINECART(328),
|
||||
SADDLE(329),
|
||||
IRON_DOOR(330),
|
||||
REDSTONE(331),
|
||||
SNOW_BALL(332),
|
||||
BOAT(333),
|
||||
LEATHER(334),
|
||||
MILK_BUCKET(335),
|
||||
CLAY_BRICK(336),
|
||||
CLAY_BALL(337),
|
||||
SUGAR_CANE(338),
|
||||
PAPER(339),
|
||||
BOOK(340),
|
||||
SLIME_BALL(341),
|
||||
STORAGE_MINECART(342),
|
||||
POWERED_MINECART(343),
|
||||
EGG(344),
|
||||
COMPASS(345),
|
||||
FISHING_ROD(346),
|
||||
WATCH(347),
|
||||
GLOWSTONE_DUST(348),
|
||||
RAW_FISH(349),
|
||||
COOKED_FISH(350),
|
||||
INK_SACK(351),
|
||||
BONE(352),
|
||||
SUGAR(353),
|
||||
CAKE(354),
|
||||
BED(355),
|
||||
DIODE(356),
|
||||
COOKIE(357),
|
||||
MAP(358),
|
||||
SHEARS(359),
|
||||
MELON(360),
|
||||
PUMPKIN_SEEDS(361),
|
||||
MELON_SEEDS(362),
|
||||
RAW_BEEF(363),
|
||||
COOKED_BEEF(364),
|
||||
RAW_CHICKEN(365),
|
||||
COOKED_CHICKEN(366),
|
||||
ROTTEN_FLESH(367),
|
||||
ENDER_PEARL(368),
|
||||
BLAZE_ROD(369),
|
||||
GHAST_TEAR(370),
|
||||
GOLD_NUGGET(371),
|
||||
NETHER_STALK(372),
|
||||
|
||||
POTION(373),
|
||||
GLASS_BOTTLE(374),
|
||||
SPIDER_EYE(375),
|
||||
FERMENTED_SPIDER_EYE(376),
|
||||
BLAZE_POWDER(377),
|
||||
MAGMA_CREAM(378),
|
||||
BREWING_STAND_ITEM(379),
|
||||
CAULDRON_ITEM(380),
|
||||
EYE_OF_ENDER(381),
|
||||
SPECKLED_MELON(382),
|
||||
MONSTER_EGG(383),
|
||||
EXP_BOTTLE(384),
|
||||
FIREBALL(385),
|
||||
BOOK_AND_QUILL(386),
|
||||
WRITTEN_BOOK(387),
|
||||
EMERALD(388),
|
||||
ITEM_FRAME(389),
|
||||
FLOWER_POT_ITEM(390),
|
||||
CARROT_ITEM(391),
|
||||
POTATO_ITEM(392),
|
||||
BAKED_POTATO(393),
|
||||
POISONOUS_POTATO(394),
|
||||
EMPTY_MAP(395),
|
||||
GOLDEN_CARROT(396),
|
||||
SKULL_ITEM(397),
|
||||
CARROT_STICK(398),
|
||||
NETHER_STAR(399),
|
||||
PUMPKIN_PIE(400),
|
||||
FIREWORK(401),
|
||||
FIREWORK_CHARGE(402),
|
||||
ENCHANTED_BOOK(403),
|
||||
REDSTONE_COMPARATOR(404),
|
||||
NETHER_BRICK_ITEM(405),
|
||||
QUARTZ(406),
|
||||
EXPLOSIVE_MINECART(407),
|
||||
HOPPER_MINECART(408),
|
||||
PRISMARINE_SHARD(409),
|
||||
PRISMARINE_CRYSTALS(410),
|
||||
RABBIT(411),
|
||||
COOKED_RABBIT(412),
|
||||
RABBIT_STEW(413),
|
||||
RABBIT_FOOT(414),
|
||||
RABBIT_HIDE(415),
|
||||
ARMOR_STAND(416),
|
||||
IRON_BARDING(417),
|
||||
GOLD_BARDING(418),
|
||||
DIAMOND_BARDING(419),
|
||||
LEASH(420),
|
||||
NAME_TAG(421),
|
||||
COMMAND_MINECART(422),
|
||||
MUTTON(423),
|
||||
COOKED_MUTTON(424),
|
||||
BANNER(425),
|
||||
SPRUCE_DOOR_ITEM(427),
|
||||
BIRCH_DOOR_ITEM(428),
|
||||
JUNGLE_DOOR_ITEM(429),
|
||||
ACACIA_DOOR_ITEM(430),
|
||||
DARK_OAK_DOOR_ITEM(431),
|
||||
GOLD_RECORD(2256),
|
||||
GREEN_RECORD(2257),
|
||||
RECORD_3(2258),
|
||||
RECORD_4(2259),
|
||||
RECORD_5(2260),
|
||||
RECORD_6(2261),
|
||||
RECORD_7(2262),
|
||||
RECORD_8(2263),
|
||||
RECORD_9(2264),
|
||||
RECORD_10(2265),
|
||||
RECORD_11(2266),
|
||||
RECORD_12(2267);
|
||||
}
|
||||
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,116 @@
|
||||
package tech.carcadex.kotlinbukkitkit.tooling.menu
|
||||
|
||||
import com.intellij.openapi.vfs.VirtualFile
|
||||
import com.intellij.openapi.vfs.readText
|
||||
import org.jetbrains.kotlin.builtins.getReceiverTypeFromFunctionType
|
||||
import org.jetbrains.kotlin.builtins.getReturnTypeFromFunctionType
|
||||
import org.jetbrains.kotlin.builtins.isExtensionFunctionType
|
||||
import org.jetbrains.kotlin.idea.base.psi.getLineNumber
|
||||
import org.jetbrains.kotlin.idea.caches.resolve.analyze
|
||||
import org.jetbrains.kotlin.idea.core.resolveType
|
||||
import org.jetbrains.kotlin.idea.inspections.collections.isCalling
|
||||
import org.jetbrains.kotlin.idea.refactoring.fqName.fqName
|
||||
import org.jetbrains.kotlin.idea.refactoring.fqName.getKotlinFqName
|
||||
import org.jetbrains.kotlin.idea.references.mainReference
|
||||
import org.jetbrains.kotlin.name.FqName
|
||||
import org.jetbrains.kotlin.psi.KtCallExpression
|
||||
import org.jetbrains.kotlin.psi.KtDotQualifiedExpression
|
||||
import org.jetbrains.kotlin.psi.KtLambdaExpression
|
||||
import org.jetbrains.kotlin.psi.KtNameReferenceExpression
|
||||
import org.jetbrains.kotlin.psi.psiUtil.collectDescendantsOfType
|
||||
import org.jetbrains.kotlin.psi.psiUtil.findDescendantOfType
|
||||
import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode
|
||||
import org.jetbrains.kotlin.types.typeUtil.isUnit
|
||||
|
||||
private val menuFqSupportedNames = listOf(
|
||||
"tech.carcadex.kotlinbukkitkit.dsl.menu"
|
||||
)
|
||||
|
||||
fun findMenuDeclaration(
|
||||
element: KtCallExpression,
|
||||
currentSelectedLine: Int? = null,
|
||||
file: VirtualFile
|
||||
): MenuDeclaration? {
|
||||
val simpleName = "menu"
|
||||
val nm = file.readText()
|
||||
if (!element.isCallFrom(simpleName, menuFqSupportedNames)) return null
|
||||
if (!nm.contains("tech.carcadex.kotlinbukkitkit.menu.dsl.menu"))
|
||||
return null;
|
||||
val menuFunction = element.findDescendantOfType<KtCallExpression> {
|
||||
isMenuCall(it)
|
||||
} ?: return null
|
||||
|
||||
// TODO: Support named parameters
|
||||
|
||||
val displayName = menuFunction.valueArguments.getOrNull(0)?.text?.replace("\"", "") ?: return null
|
||||
val lines = menuFunction.valueArguments.getOrNull(1)?.text?.toIntOrNull() ?: return null
|
||||
|
||||
val menuLambdaBlock = element.findDescendantOfType<KtLambdaExpression> {
|
||||
it.isBuilderBlockFor("tech.carcadex.kotlinbukkitkit.menu.dsl.MenuDSL")
|
||||
} ?: return null
|
||||
|
||||
val slotFqName = listOf("tech.carcadex.kotlinbukkitkit.menu.dsl.menu.slot")
|
||||
val slotSimpleName = "slot"
|
||||
|
||||
val slotCalls = menuLambdaBlock.collectDescendantsOfType<KtCallExpression> {
|
||||
it.isCallFrom(slotSimpleName, slotFqName)
|
||||
}.mapNotNull {
|
||||
|
||||
val args = it.valueArguments
|
||||
|
||||
// TODO: support named parameters
|
||||
|
||||
val line = args.getOrNull(0)?.text?.toIntOrNull() ?: return@mapNotNull null
|
||||
val slot = args.getOrNull(1)?.text?.toIntOrNull() ?: return@mapNotNull null
|
||||
|
||||
// TODO: support just slot: slot(15, item(...)) {}
|
||||
|
||||
val itemValue = args.getOrNull(2) ?: return@mapNotNull null
|
||||
val expression = itemValue.getArgumentExpression() ?: return@mapNotNull null
|
||||
|
||||
val materialExpression = expression.findDescendantOfType<KtDotQualifiedExpression> {
|
||||
it.text.startsWith("Material")
|
||||
} ?: return@mapNotNull null
|
||||
|
||||
val typeExpression = materialExpression.selectorExpression as? KtNameReferenceExpression ?: return@mapNotNull null
|
||||
val materialName = typeExpression.text ?: return@mapNotNull null
|
||||
|
||||
val isSelected = it.getLineNumber() == currentSelectedLine
|
||||
|
||||
MenuSlotDeclaration(
|
||||
line,
|
||||
slot,
|
||||
materialName,
|
||||
isSelected
|
||||
)
|
||||
}
|
||||
|
||||
return MenuDeclaration(displayName, lines, slotCalls)
|
||||
}
|
||||
|
||||
fun isMenuCall(it: KtCallExpression) =
|
||||
it.isCallFrom("menu", menuFqSupportedNames)
|
||||
|
||||
fun KtLambdaExpression.isBuilderBlockFor(fqName: String): Boolean {
|
||||
return (text.replace(" ", "").replace("\t", "").replace("\n", "")
|
||||
.contains(Regex("\\{[\\s\\S]*?slot"))) && (parent.parent.text.contains("menu"))
|
||||
// val type = resolveType()
|
||||
//
|
||||
// val extensionFunctionType = type?.isExtensionFunctionType
|
||||
//
|
||||
// if (extensionFunctionType != true) return false
|
||||
//
|
||||
// val returnTypeFromFunctionType = type.getReturnTypeFromFunctionType()
|
||||
// val receiverTypeFromFunctionType = type.getReceiverTypeFromFunctionType() ?: return false
|
||||
// val receiverFqName = receiverTypeFromFunctionType.fqName ?: return false
|
||||
//
|
||||
// return returnTypeFromFunctionType.isUnit() && receiverFqName.asString().equals(fqName)
|
||||
}
|
||||
|
||||
fun KtCallExpression.isCallFrom(
|
||||
simpleName: String,
|
||||
fqNames: List<String>
|
||||
|
||||
): Boolean {
|
||||
return text.startsWith(simpleName)
|
||||
}
|
||||
@@ -2,12 +2,14 @@ package tech.carcadex.kotlinbukkitkit.tooling.wizard
|
||||
|
||||
object Versions {
|
||||
val `1-0-0-3-SNAPSHOT` = KotlinBukkitAPIVersion("1.0.0.3-SNAPSHOT")
|
||||
val `1-0-0-3` = KotlinBukkitAPIVersion("1.0.0.3")
|
||||
|
||||
val ALL = arrayOf(
|
||||
`1-0-0-3`,
|
||||
`1-0-0-3-SNAPSHOT`
|
||||
)
|
||||
|
||||
val LAST = `1-0-0-3-SNAPSHOT`
|
||||
val LAST = `1-0-0-3`
|
||||
|
||||
val visitorAll = ALL.associate { it.version to it }
|
||||
|
||||
|
||||
Binary file not shown.
Reference in new issue
Block a user