Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master' into Compatibility
Browse files Browse the repository at this point in the history
# Conflicts:
#	build.gradle
#	src/main/kotlin/cf/wayzer/SuperItem/Commander.kt
#	src/main/kotlin/cf/wayzer/SuperItem/ConfigManager.kt
#	src/main/kotlin/cf/wayzer/SuperItem/Item.kt
#	src/main/kotlin/cf/wayzer/SuperItem/ItemManager.kt
#	src/main/kotlin/cf/wayzer/SuperItem/Main.kt
#	src/main/kotlin/cf/wayzer/SuperItem/ScriptSupporter.kt
#	src/main/kotlin/cf/wayzer/SuperItem/features/ItemInfo.kt
  • Loading branch information
way-zer committed Mar 1, 2020
2 parents bc210e1 + de95380 commit aa08152
Show file tree
Hide file tree
Showing 23 changed files with 473 additions and 210 deletions.
96 changes: 21 additions & 75 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,83 +4,29 @@ An extensible custom item minecraft plugin
* every item is a class file(runtime load)
* can write custom item feature
* every item has Automatically generated detailed configuration(from name to potion effect)
* support **kotlin DSL** and **kotlin Script(alse Superitem.kts)**
(see wiki for example)
## First Glance
Save it as hello.superitem.kts in plugins/Superitem/items to use
```kotlin
require(ItemInfo(Material.NAME_TAG,
"&cHello SuperItem",
listOf("&aWelcome to use SuperItem",
"&a欢迎使用Superitem",
"&c This Item doesn't have other effect")))
```
## For More information
see [Wiki]
## For Chinese
see README.zh.md
## Thanks
* [LibraryManager] (shadowed) For runtime dependencies management
* [PowerNBT] (shadowed) For NBT support
## License
you should leave link to [this](https://github.com/way-zer/SuperItem/) and keep the author name in command help
you should leave link to [this] and keep the author name in command help
feel free to fork and pull requests
## How to write an item
```kotlin
package cf.wayzer.example

//for full see items/src/main/kotin/Damascus_knife
class Damascus_knife : Item() {
lateinit var effect: Effect

override fun loadFeatures() {
require(ItemInfo(Material.IRON_SWORD, "§3大马士革刀",
listOf("§b§o特殊的冶炼方式", "§b§o让大马士革刀的纹路中", "§b§o含有一种奇特的化学物质", "§b§o小小的砍伤足以致人死地")))
require(Recipe("2@0;0;265;0;265;265;280;42;0"))
require(Effect(
Effect.EffectData(PotionEffectType.POISON, 300)
))
}

@EventHandler(priority = EventPriority.LOWEST)
fun onAttack(e: EntityDamageByEntityEvent) {
if (e.isCancelled) return
if (e.damager is Player) {
val p = e.damager as Player
if (isItem(p.inventory.itemInMainHand) && get<Permission>().hasPermission(p)) {
if (!e.entity.isDead) {
get<Effect>().setEffect(e.entity as LivingEntity)
}
}
}
}
}

```
1. create an class extends *Item*
2. override function loadFeatures():
in this function,you should require features you need(Permission and Texture is default required)
3. Item is *Listener*,which means you can write your eventHandler

## How to write custom Feature
### What is a *Feature*
* Feature can use for a kind of feature.(custom Recipe,custom Durability or so on)
* Feature is a bridge of Item and configuration
### Example
if it isn't *Item*,you should put it into Package *lib*
```kotlin
package cf.wayzer.example.lib

class Durability(override val defaultData: Int) : Feature<Int>(), Feature.HasListener {
override val listener: Listener
get() = mListener

//Feature custom function(API)
fun setDurability(item: ItemStack, now: Int = data) {}

fun getDurability(item: ItemStack): Int {}

companion object {
private class MListener : Listener {
@EventHandler(ignoreCancelled = true)
fun onDurability(e: PlayerItemDamageEvent) {
// ...
}
}

private val mListener = MListener()
}
}
```
#### About *defaultData*
you can use either primary type or custom class.
you can return it build by class parameters

#### Other
* If you need listen event,you can implement *HasListener*
* If you need other feature,you can implement *onPostLoad*
* If you need handle disableEvent(such as clearing potion effect),you can implement *OnDisable*
[this]: https://github.com/way-zer/SuperItem/
[Wiki]: https://github.com/way-zer/SuperItem/wiki
[LibraryManager]: https://github.com/way-zer/LibraryManager
[PowerNBT]: https://github.com/steakteam/PowerNBT
23 changes: 21 additions & 2 deletions README.zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,29 @@
* 每个物品都是一个独立的class文件(运行时加载)
* 可以自己编写Feature(见下方说明)
* 每个物品都可以自动生成详细的配置文件(从名字到药水效果,取决于Feature)

* 支持**kotlin DSL****kotlin Script(也称 Superitem.kts)**(例子详见[Wiki])
## 授权
保留[本站](https://github.com/way-zer/SuperItem/)链接并保留指令帮助页的作者信息
保留[本站]链接并保留指令帮助页的作者信息
欢迎fork并提出pull请求,有任何疑问请提出issue
## 举个栗子
保存为 hello.superitem.kts 于plugins/Superitem/items, 使用
```kotlin
require(ItemInfo(Material.NAME_TAG,
"&cHello SuperItem",
listOf("&aWelcome to use SuperItem",
"&a欢迎使用Superitem",
"&c This Item doesn't have other effect")))
```
## 感谢
* [LibraryManager] (shadowed) 提供运行依赖管理
* [PowerNBT] (shadowed) 提供NBT修改支持

[本站]: https://github.com/way-zer/SuperItem/
[Wiki]: https://github.com/way-zer/SuperItem/wiki
[LibraryManager]: https://github.com/way-zer/LibraryManager
[PowerNBT]: https://github.com/steakteam/PowerNBT

# ------------过期内容(详见[Wiki])---------------
## 怎样写一个item
```kotlin
package cf.wayzer.example
Expand Down
17 changes: 17 additions & 0 deletions Scripts/Mythicmobs_Support.superitem.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
@file:ImportClass("io.lumine.xikage.mythicmobs.api.bukkit.events.MythicDropLoadEvent")

import cf.wayzer.SuperItem.ItemManager
import io.lumine.xikage.mythicmobs.adapters.bukkit.BukkitItemStack
import io.lumine.xikage.mythicmobs.api.bukkit.events.MythicDropLoadEvent
import io.lumine.xikage.mythicmobs.drops.droppables.ItemDrop

require(ItemInfo(Material.NAME_TAG,"&cMythicmobs_Support", listOf("&c This Item doesn't have other effect")))

listen<MythicDropLoadEvent>{
val name = (it.dropName as String).toUpperCase()
if(!name.startsWith("SI_"))return@listen
val item = ItemManager.getItem(name.substring(3))
//if(item==null)it.register(NothingDrop(it.config.line,it.config))
if(item!=null)it.register(ItemDrop(it.config.line,it.config,BukkitItemStack(item.get<ItemInfo>().newItemStack())))
}

56 changes: 44 additions & 12 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
plugins {
id 'com.github.johnrengelman.shadow' version '5.0.0'
id 'java'
id 'org.jetbrains.kotlin.jvm' version "1.3.31"
id 'org.jetbrains.kotlin.jvm' version "1.3.41"
id 'me.qoomon.git-versioning' version '1.2.1'
}
ext{
kotlin_version ="1.3.31"
kotlin_version ="1.3.41"
mc_version = '1.14-R0.1-SNAPSHOT'
bc_version ='1.12-SNAPSHOT'
}
Expand All @@ -18,30 +19,58 @@ allprojects{
maven {
url = "https://hub.spigotmc.org/nexus/content/groups/public/"
}
maven {
url "https://dl.bintray.com/way-zer/maven"
}
}
}
shadowJar {
// minimize()
configurations = [project.configurations.compile]
configurations = [project.configurations.shadow]
relocate("me.dpohvar.powernbt.api","cf.wayzer.util.nbt")
relocate("me.dpohvar.powernbt.utils","cf.wayzer.util.nbt.utils")
// exclude("me/**")
// exclude("example/**")
// exclude("(?!plugin).yml")
include("*.jar")
//for NBT
include("me/dpohvar/powernbt/api/**")
include("me/dpohvar/powernbt/utils/**")

include("cf/wayzer/**")
include("*.kts")
include("META-INF/**")
include("plugin.yml")
}


//start

dependencies{
shadow 'cf.wayzer:LibraryManager:1.2'
shadow(files("$rootDir/lib/PowerNBT.jar"))
compile "org.jetbrains.kotlin:kotlin-script-util:$kotlin_version"
compile "org.jetbrains.kotlin:kotlin-scripting-jvm-host:$kotlin_version"
compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"

compileOnly "org.spigotmc:spigot-api:$mc_version"
compileOnly "net.md-5:bungeecord-chat:$bc_version"
compileOnly files("$rootDir/lib/PowerNBT.jar")
// compile "me.dpohvar.powernbt:PowerNBT:0.8.9.2"
}
version="1.3.2"
version "1.3-SNAPSHOT"
gitVersioning{
tag {
pattern = 'v(?<tagVersion>[0-9].*)'
versionFormat = '${tagVersion}'
}
commit {
versionFormat = '${version}-${commit.short}'
}
}
processResources{
filesMatching(["plugin.yml","bungee.yml"]){
filter{
it.replace("@version@", project.version.toString())
it.replace("@version@", version.toString())
}
}
}
Expand All @@ -54,10 +83,13 @@ task buildArtifact(type: Zip){
include "**/*.class"
into("items")
}
from(jar.destinationDir)
archiveName ="SuperItem-${project.version}.zip"
destinationDir =file("$rootDir/artifacts/")
}
artifacts {
buildArtifact
// from(jar.getArchiveFile()){
// rename(".jar","-developLib.jar")
// }
from("Scripts")
from(shadowJar.getArchiveFile()){
rename("-all","")
}
archiveFileName = "SuperItem-${rootProject.version}.zip"
destinationDirectory =file("$rootDir/artifacts/")
}
4 changes: 2 additions & 2 deletions lib/PowerNBT.jar
Git LFS file not shown
29 changes: 28 additions & 1 deletion src/main/kotlin/cf/wayzer/SuperItem/Commander.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import org.bukkit.command.Command
import org.bukkit.command.CommandExecutor
import org.bukkit.command.CommandSender
import org.bukkit.entity.Player
import java.io.File

class Commander : CommandExecutor {

Expand All @@ -24,6 +25,10 @@ class Commander : CommandExecutor {
giveItem(s, args)
return true
}
"load" ->{
loadItem(s,args)
return true
}
}
}
help(s)
Expand Down Expand Up @@ -59,6 +64,11 @@ class Commander : CommandExecutor {
}
if (item.givePlayer(player))
s.sendMessage("§a给予成功")
else{
item.drop(player.location,player)
player.sendMessage("§a背包已满,已掉落")
s.sendMessage("§e背包已满,已掉落")
}
}

private fun getItem(s: CommandSender, args: Array<String>) {
Expand Down Expand Up @@ -118,19 +128,36 @@ class Commander : CommandExecutor {
var i = pages * 10 - 10
while (i < list.size && i < pages * 10) {
s.sendMessage(String.format("§e%03d §a|§e %-20s §a|§e %s",
i, list[i].name, list[i].get<ItemInfo>().itemStack.itemMeta?.displayName))
i, list[i].name, list[i].get<ItemInfo>().newItemStack().itemMeta.displayName))
i++
}
s.sendMessage("§a================ §7$pages/$maxpages §a================")
}

private fun loadItem(s:CommandSender, args: Array<String>){
if (!s.hasPermission("SuperItem.command.load")) {
s.sendMessage("§c没有权限")
return
}
if (args.size < 2) {
s.sendMessage("§c请输入Path")
return
}
if (!File(ItemManager.rootDir,args[1]).exists()){
s.sendMessage("§c请输入Path")
return
}
TODO("From path to get item name")
}

private fun help(s: CommandSender) {
s.sendMessage("§c========= §c§lSuper Item§c =========")
s.sendMessage("§a §l-------§5 By: §5§lWay__Zer §a§l-------")
s.sendMessage("§c========== §c§l使用说明§c ==========")
s.sendMessage("§a§l+§5/SuperItem list <页码> §e打开Item列表")
s.sendMessage("§a§l+§5/SuperItem get <ID/ClassName> §e获取Item")
s.sendMessage("§a§l+§5/SuperItem give <ID/ClassName> <PLAYER> §e给予玩家Item")
s.sendMessage("§a§l+§5/SuperItem load <Path> §e加载或重载Item")
}

}
3 changes: 2 additions & 1 deletion src/main/kotlin/cf/wayzer/SuperItem/ConfigManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ object ConfigManager{
}

fun <H : Any> loadForFeature(item: Item, feature: Feature<H>){
if(feature.defaultData is Nothing)return
val config = item.config
if(!config.has(feature.name))config.add(feature.name, gson.toJsonTree(feature.defaultData))
feature.data= gson.fromJson(config.get(feature.name),feature.defaultData::class.java)
Expand Down Expand Up @@ -43,4 +44,4 @@ object ConfigManager{

private val Item.configFile:File
get() = File(rootDir, "$packageName ${File.pathSeparator} $name.json")
}
}
22 changes: 19 additions & 3 deletions src/main/kotlin/cf/wayzer/SuperItem/Item.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import cf.wayzer.SuperItem.features.CoolDown
import cf.wayzer.SuperItem.features.ItemInfo
import cf.wayzer.SuperItem.features.Permission
import org.bukkit.Bukkit
import org.bukkit.Location
import org.bukkit.entity.Player
import org.bukkit.event.Event
import org.bukkit.event.EventPriority
Expand Down Expand Up @@ -36,9 +37,9 @@ abstract class Item : Listener {

val features : MutableMap<Class<*>,MutableList<Feature<*>>> = mutableMapOf()

@Deprecated("保证扩展和安全,请使用get,1.3版本弃用",ReplaceWith("get<ItemInfo>().itemStack"))
@Deprecated("保证扩展和安全,请使用get,1.3版本弃用",ReplaceWith("get<ItemInfo>().newItemStack()"))
val item
get() = get<ItemInfo>().itemStack
get() = get<ItemInfo>().newItemStack()

@Deprecated("保证扩展和安全,请使用get,1.3版本弃用",ReplaceWith("get<Permission>()"))
val permission
Expand Down Expand Up @@ -74,6 +75,17 @@ abstract class Item : Listener {
*/
inline fun <reified T:Feature<*>> get(index: Int=0):T = get(T::class.java,index)

/**
* 判断是否有对应feature
*/
fun <T: Feature<*>> has(c : Class<T>):Boolean = features[c]?.isNotEmpty()?:false

/**
* @see has(c)
* 只能在kotlin下调用
*/
inline fun <reified T:Feature<*>> has():Boolean = has(T::class.java)

/**
* 给予玩家道具
*/
Expand All @@ -85,11 +97,15 @@ abstract class Item : Listener {
return false
}
inv.setItem(i, inv.itemInMainHand)
inv.setItemInMainHand(get<ItemInfo>().itemStack.clone())
inv.setItemInMainHand(get<ItemInfo>().newItemStack(p))
p.updateInventory()
return true
}

fun drop(location: Location,player: Player?=null){
location.world.dropItem(location,get<ItemInfo>().newItemStack(player))
}

/**
* 判断物品是否是当前Item的道具
*/
Expand Down
Loading

0 comments on commit aa08152

Please sign in to comment.