Skip to content

Commit

Permalink
feat: add picksaw item
Browse files Browse the repository at this point in the history
  • Loading branch information
acrylic-style committed Jul 25, 2024
1 parent 18e0411 commit 5d4c736
Show file tree
Hide file tree
Showing 190 changed files with 2,558 additions and 1 deletion.
3 changes: 2 additions & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ plugins {
}

group = "net.azisaba"
version = "6.15.1"
version = "1.15.2+6.16.0"

java {
toolchain.languageVersion.set(JavaLanguageVersion.of(8))
Expand Down Expand Up @@ -119,6 +119,7 @@ tasks {
}

processResources {
doNotTrackState("plugin.yml should be updated every time")
duplicatesStrategy = DuplicatesStrategy.INCLUDE

from(sourceSets.main.get().resources.srcDirs) {
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/com/github/mori01231/lifecore/LifeCore.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.github.mori01231.lifecore
import com.github.mori01231.lifecore.block.CustomBlockManager
import com.github.mori01231.lifecore.command.*
import com.github.mori01231.lifecore.config.*
import com.github.mori01231.lifecore.data.DataLoader
import com.github.mori01231.lifecore.gui.CommandListScreen
import com.github.mori01231.lifecore.gui.DropProtectScreen
import com.github.mori01231.lifecore.gui.TrashProtectScreen
Expand All @@ -19,10 +20,14 @@ import org.bukkit.Bukkit
import org.bukkit.command.CommandExecutor
import org.bukkit.entity.ItemFrame
import org.bukkit.plugin.java.JavaPlugin
import org.bukkit.plugin.java.PluginClassLoader
import java.io.File
import java.io.IOException
import java.net.InetSocketAddress
import java.nio.file.Files
import java.util.concurrent.Executor
import java.util.concurrent.Executors
import java.util.stream.Collectors

class LifeCore : JavaPlugin() {
val gcListener = GCListener(this)
Expand All @@ -33,6 +38,7 @@ class LifeCore : JavaPlugin() {
val asyncExecutor = Executor { Bukkit.getScheduler().runTaskAsynchronously(this, it) }
val lifeCoreConfig = LifeCoreConfig.load(this)
val playerRegionManager = PlayerRegionManager()
val dataLoader = DataLoader(slF4JLogger, getAsFileSystem().getPath("/data"))
private var databaseConfig: DatabaseConfig? = null
lateinit var voteConfig: VoteConfig
private set
Expand Down Expand Up @@ -301,6 +307,7 @@ class LifeCore : JavaPlugin() {
pm.registerEvents(CommandListScreen.EventListener(), this)
pm.registerEvents(UpdateInventoryOnCloseListener(this), this)
pm.registerEvents(PromptSignListener, this)
pm.registerEvents(PicksawItemListener(dataLoader), this)

// Items
pm.registerEvents(OreOnlyItemListener(), this)
Expand Down Expand Up @@ -357,4 +364,12 @@ class LifeCore : JavaPlugin() {
lateinit var instance: LifeCore
private set
}

fun getAsFileSystem() =
this::class.java
.protectionDomain
.codeSource
.location
.file
.let { FileSystemUtil.openFileAsFileSystem(File(it)) }
}
43 changes: 43 additions & 0 deletions src/main/java/com/github/mori01231/lifecore/data/DataLoader.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.github.mori01231.lifecore.data

import kotlinx.serialization.Serializable
import kotlinx.serialization.json.Json
import org.slf4j.Logger
import java.nio.file.Files
import java.nio.file.Path
import kotlin.io.path.isDirectory
import kotlin.io.path.readText
import kotlin.io.path.relativeTo

class DataLoader(private val logger: Logger, private val dataFolder: Path) {
val tags: List<Tag> by lazy { loadTags() }

fun findTag(name: String): Tag? = tags.firstOrNull { it.name == name }

fun loadTags(): List<Tag> {
val tags = mutableListOf<Tag>()
val tagsPath = dataFolder.resolve("tags")
Files.walk(tagsPath).forEach { path ->
if (path.isDirectory()) return@forEach
val tagName =
path.relativeTo(tagsPath)
.toString()
.let { it.substring(0, it.length - ".json".length) }
.let { it.substring(it.indexOf('/') + 1) }
logger.info("Loading tag $tagName")
val values = mutableListOf<TagValue>()
Json.decodeFromString<RawTag>(path.readText()).values.forEach { tagValue ->
if (tagValue.startsWith("#")) {
values.add(TagReference(this, tagValue.substring(1)))
} else {
values.add(StringTagValue(tagValue))
}
}
tags.add(Tag("minecraft:$tagName", values))
}
return tags
}

@Serializable
data class RawTag(val values: List<String>)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.github.mori01231.lifecore.data

data class StringTagValue(val value: String) : TagValue {
override fun resolve(): Set<String> = setOf(value)
}
5 changes: 5 additions & 0 deletions src/main/java/com/github/mori01231/lifecore/data/Tag.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.github.mori01231.lifecore.data

data class Tag(val name: String, val values: List<TagValue>) : TagValue {
override fun resolve(): Set<String> = values.flatMap(TagValue::resolve).toSet()
}
10 changes: 10 additions & 0 deletions src/main/java/com/github/mori01231/lifecore/data/TagReference.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.github.mori01231.lifecore.data

class TagReference(private val dataLoader: DataLoader, val name: String) : TagValue {
val values by lazy {
val tag = dataLoader.findTag(name) ?: error("missing tag $name")
tag.values.flatMap(TagValue::resolve).toSet()
}

override fun resolve(): Set<String> = values
}
5 changes: 5 additions & 0 deletions src/main/java/com/github/mori01231/lifecore/data/TagValue.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.github.mori01231.lifecore.data

interface TagValue {
fun resolve(): Set<String>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.github.mori01231.lifecore.listener.item

import com.github.mori01231.lifecore.data.DataLoader
import com.github.mori01231.lifecore.util.ItemUtil
import org.bukkit.Material
import org.bukkit.craftbukkit.v1_15_R1.block.data.CraftBlockData
import org.bukkit.event.EventHandler
import org.bukkit.event.Listener
import org.bukkit.event.block.Action
import org.bukkit.event.player.PlayerInteractEvent
import org.bukkit.inventory.EquipmentSlot

class PicksawItemListener(private val dataLoader: DataLoader) : Listener {
companion object {
const val ITEM_ID = "2dd646f1-70a1-4613-92c3-431de7c0126f"
}

@EventHandler
fun onPlayerInteract(e: PlayerInteractEvent) {
if (e.hand != EquipmentSlot.HAND) return
if (e.action != Action.LEFT_CLICK_BLOCK) return
val item = e.player.inventory.itemInMainHand
if (ItemUtil.getStringTag(item, "LifeItemId") != ITEM_ID) return
val minecraftName = "minecraft:" + ((e.clickedBlock ?: return).blockData as CraftBlockData).state.block.item.toString()
if (dataLoader.findTag("minecraft:mineable/axe")?.resolve()?.contains(minecraftName) == true) {
item.type = Material.valueOf(item.type.name.substring(0, item.type.name.lastIndexOf('_')) + "_AXE")
e.player.inventory.setItemInMainHand(item)
} else if (dataLoader.findTag("minecraft:mineable/pickaxe")?.resolve()?.contains(minecraftName) == true) {
item.type = Material.valueOf(item.type.name.substring(0, item.type.name.lastIndexOf('_')) + "_PICKAXE")
e.player.inventory.setItemInMainHand(item)
} else if (dataLoader.findTag("minecraft:mineable/shovel")?.resolve()?.contains(minecraftName) == true) {
item.type = Material.valueOf(item.type.name.substring(0, item.type.name.lastIndexOf('_')) + "_SHOVEL")
e.player.inventory.setItemInMainHand(item)
} else if (dataLoader.findTag("minecraft:mineable/hoe")?.resolve()?.contains(minecraftName) == true) {
item.type = Material.valueOf(item.type.name.substring(0, item.type.name.lastIndexOf('_')) + "_HOE")
e.player.inventory.setItemInMainHand(item)
}
}
}
16 changes: 16 additions & 0 deletions src/main/java/com/github/mori01231/lifecore/util/FileSystemUtil.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.github.mori01231.lifecore.util

import java.io.File
import java.nio.file.FileSystem
import java.nio.file.FileSystems
import java.nio.file.Path

object FileSystemUtil {
@JvmStatic
fun openFileAsFileSystem(file: File): FileSystem =
FileSystems.newFileSystem(file.toPath(), null as ClassLoader?)

@JvmStatic
fun getPathInFile(file: File, path: String): Path =
openFileAsFileSystem(file).getPath(path)
}
8 changes: 8 additions & 0 deletions src/main/resources/data/tags/blocks/acacia_logs.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"values": [
"minecraft:acacia_log",
"minecraft:acacia_wood",
"minecraft:stripped_acacia_log",
"minecraft:stripped_acacia_wood"
]
}
6 changes: 6 additions & 0 deletions src/main/resources/data/tags/blocks/all_hanging_signs.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"values": [
"#minecraft:ceiling_hanging_signs",
"#minecraft:wall_hanging_signs"
]
}
6 changes: 6 additions & 0 deletions src/main/resources/data/tags/blocks/all_signs.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"values": [
"#minecraft:signs",
"#minecraft:all_hanging_signs"
]
}
16 changes: 16 additions & 0 deletions src/main/resources/data/tags/blocks/ancient_city_replaceable.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"values": [
"minecraft:deepslate",
"minecraft:deepslate_bricks",
"minecraft:deepslate_tiles",
"minecraft:deepslate_brick_slab",
"minecraft:deepslate_tile_slab",
"minecraft:deepslate_brick_stairs",
"minecraft:deepslate_tile_wall",
"minecraft:deepslate_brick_wall",
"minecraft:cobbled_deepslate",
"minecraft:cracked_deepslate_bricks",
"minecraft:cracked_deepslate_tiles",
"minecraft:gray_wool"
]
}
5 changes: 5 additions & 0 deletions src/main/resources/data/tags/blocks/animals_spawnable_on.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"values": [
"minecraft:grass_block"
]
}
7 changes: 7 additions & 0 deletions src/main/resources/data/tags/blocks/anvil.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"values": [
"minecraft:anvil",
"minecraft:chipped_anvil",
"minecraft:damaged_anvil"
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"values": [
"#minecraft:animals_spawnable_on",
"#minecraft:badlands_terracotta",
"minecraft:red_sand",
"minecraft:coarse_dirt"
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"values": [
"minecraft:clay"
]
}
9 changes: 9 additions & 0 deletions src/main/resources/data/tags/blocks/azalea_grows_on.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"values": [
"#minecraft:dirt",
"#minecraft:sand",
"#minecraft:terracotta",
"minecraft:snow_block",
"minecraft:powder_snow"
]
}
13 changes: 13 additions & 0 deletions src/main/resources/data/tags/blocks/azalea_root_replaceable.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"values": [
"#minecraft:base_stone_overworld",
"#minecraft:dirt",
"#minecraft:terracotta",
"minecraft:red_sand",
"minecraft:clay",
"minecraft:gravel",
"minecraft:sand",
"minecraft:snow_block",
"minecraft:powder_snow"
]
}
11 changes: 11 additions & 0 deletions src/main/resources/data/tags/blocks/badlands_terracotta.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"values": [
"minecraft:terracotta",
"minecraft:white_terracotta",
"minecraft:yellow_terracotta",
"minecraft:orange_terracotta",
"minecraft:red_terracotta",
"minecraft:brown_terracotta",
"minecraft:light_gray_terracotta"
]
}
6 changes: 6 additions & 0 deletions src/main/resources/data/tags/blocks/bamboo_blocks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"values": [
"minecraft:bamboo_block",
"minecraft:stripped_bamboo_block"
]
}
10 changes: 10 additions & 0 deletions src/main/resources/data/tags/blocks/bamboo_plantable_on.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"values": [
"#minecraft:sand",
"#minecraft:dirt",
"minecraft:bamboo",
"minecraft:bamboo_sapling",
"minecraft:gravel",
"minecraft:suspicious_gravel"
]
}
36 changes: 36 additions & 0 deletions src/main/resources/data/tags/blocks/banners.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"values": [
"minecraft:white_banner",
"minecraft:orange_banner",
"minecraft:magenta_banner",
"minecraft:light_blue_banner",
"minecraft:yellow_banner",
"minecraft:lime_banner",
"minecraft:pink_banner",
"minecraft:gray_banner",
"minecraft:light_gray_banner",
"minecraft:cyan_banner",
"minecraft:purple_banner",
"minecraft:blue_banner",
"minecraft:brown_banner",
"minecraft:green_banner",
"minecraft:red_banner",
"minecraft:black_banner",
"minecraft:white_wall_banner",
"minecraft:orange_wall_banner",
"minecraft:magenta_wall_banner",
"minecraft:light_blue_wall_banner",
"minecraft:yellow_wall_banner",
"minecraft:lime_wall_banner",
"minecraft:pink_wall_banner",
"minecraft:gray_wall_banner",
"minecraft:light_gray_wall_banner",
"minecraft:cyan_wall_banner",
"minecraft:purple_wall_banner",
"minecraft:blue_wall_banner",
"minecraft:brown_wall_banner",
"minecraft:green_wall_banner",
"minecraft:red_wall_banner",
"minecraft:black_wall_banner"
]
}
7 changes: 7 additions & 0 deletions src/main/resources/data/tags/blocks/base_stone_nether.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"values": [
"minecraft:netherrack",
"minecraft:basalt",
"minecraft:blackstone"
]
}
10 changes: 10 additions & 0 deletions src/main/resources/data/tags/blocks/base_stone_overworld.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"values": [
"minecraft:stone",
"minecraft:granite",
"minecraft:diorite",
"minecraft:andesite",
"minecraft:tuff",
"minecraft:deepslate"
]
}
Loading

0 comments on commit 5d4c736

Please sign in to comment.