m9h6
本帖最后由 1582952890 于 2017-9-16 17:50 编辑

这是一个轻量的小插件
能让插件开发者更方便创建物品。

如果你要做一个带自定义描述和名称的物品
一般情况是这样的

ItemStack item = new ItemStack(1);或者ItemStack item = new ItemStack(Material.STONE);
ItemMeta meta = item.getItemMeta();
meta.setDisplayName("石头");
List<String>list = new ArrayList<String>();
list.add("这是一个普通的石头");
list.add("我编不下去了...");
meta.setLore(list);
item.setItemMeta(meta);

这种代码量简直烦得要死啊,尤其是当你弄好之后,在其他的地方想要“突发奇想”再给它加一行描述行的时候...
你还得这样

ItemMeta meta = item.getItemMeta();
List<String>lore = meta.getLore();
lore.add("这块石头又添加了一个描述行");
meta.setLore(lore);
Item.setItemMeta(meta);

这么多行就算我打字快也感觉很烦。。。
因此,寡人的懒人化JAVA小工具出场了~
它能让你少打一些代码
比如创建一块石头变成了这么简单的几行字:

ItemBuilder item = new ItemBuilder(1).setDisplay("石头");
item.addLore("这是一个普通的石头");
item.addLore("我编不下去了...");

你也可以连起来写,不过看起来不太规整。。

ItemBuilder item = new ItemBuilder(1).setDisplay("石头").addLore("这是一个普通的石头").addLore("我编不下去了...");

附魔也是可以的:

item.enchant(Enchantment.ARROW_DAMAGE, 2);
item.removeEnchant(Enchantment.ARROW_DAMAGE);

最后等你添加好自己喜欢的东西之后
在用它的时候把它换成item.create()就可以了
举个例子:
player.getInventory().addItem(item.create());
是不是很简单?
如果你想修改一个已有的ItemStack,也是可以的
ItemStack item = new ItemStack(Material.STONE, 1, (short) 1);
ItemBuilder builder = new ItemBuilder(item);
然后去修改你的东西吧~~

下载地址:

链接:http://pan.baidu.com/s/1boHNw3X 密码:0vsr

(注意:下载下来是一个.java后缀的源码,直接扔到你的插件所在的的包里,再改一下package声明就行了)

或者你可以直接复制这些东西到一个新的java类里



详细使用说明:

要创建一个ItemBuilder有很多种方法
1.用物品id创建:
new ItemBuilder(物品id);
如果你想创建像染料一样有分号后面id的物品
new ItemBuilder(物品id, 分号后面那个物品id);
2.用Material创建
new ItemBuilder(Material.方块名称);
3.用已有的ItemStack创建
new ItemBuilder(你的ItemStack);

给你的物品设置数量(如果不设置默认是一个):
ItemBuilder item = new ItemBuilder(1);
(下面我都用item表示itembuilder的实例,就不创建新的了)
item.setCount(数量);

设置名称:
item.setDisplay(随便写);

设置描述行:
1.添加一个描述行:
item.add(随便写);
2.添加一群描述行:
List<String>lore = new ArrayList<String>();
lore.add(第一行);
lore.add(第二行);
item.addLore(lore);
无特殊情况的话这种方法不如第一种简单
3.设置描述行:
item.setLore(lore);
这会清空原有描述行,设置成新的

获取各种数据:

String name = item.getDisplay();
int count = item.getCount();
List<String>lore = item.getLore();
String loreline = item.getLore(2);(2为索引,如果是第3行应该填2,以此类推)

附魔相关:

判断是否有附魔:
1.是否有附魔:
boolean hasench = item.hasEnchant();
2.是否有指定类型的附魔
boolean hasench = item.hasEnchant(附魔种类);
3.获取指定附魔的等级(没有的话就为0)
int level = item.getEnchantLevel(附魔种类);

添加附魔:
1.增加一级特定种类的附魔(如果原来没有就升到一级)
item.enchant(附魔种类);
2.设定这个种类的附魔的等级
item.enchant(附魔种类, 等级);
3.删除一个种类的附魔
item.removeEnchant(附魔种类);

消耗物品:
ItemBuilder.consumeItem(player);
这个功能是给我自己的服务器用的,用来让一个玩家手中的物品数量-1

而且所有方法都可以连起来写:
item.setDisplay(名字).addLore(描述).enchant(某附魔).create();
一气呵成

使用方法就是这些,感谢那些把所有内容看完再关掉网页的人。


2021.12 数据,可能有更多内容这是一个轻量的小插件能让插件开发者更方便创建物品。
如果你要做一个带自定义描述和名称的物品一般情况是这样的
ItemStack item = new ItemStack(1);或者ItemStack item = new ItemStack(Material.STONE);ItemMeta meta = item.getItemMeta();meta.setDisplayName(&quot;石头&quot;);List&lt;String&gt;list = new ArrayList&lt;String&gt;();list.add(&quot;这是一个普通的石头&quot;);list.add(&quot;我编不下去了...&quot;);meta.setLore(list);item.setItemMeta(meta);
这种代码量简直烦得要死啊,尤其是当你弄好之后,在其他的地方想要“突发奇想”再给它加一行描述行的时候...你还得这样
ItemMeta meta = item.getItemMeta();List&lt;String&gt;lore = meta.getLore();lore.add(&quot;这块石头又添加了一个描述行&quot;);meta.setLore(lore);Item.setItemMeta(meta);
这么多行就算我打字快也感觉很烦。。。因此,寡人的懒人化JAVA小工具出场了~它能让你少打一些代码比如创建一块石头变成了这么简单的几行字:
ItemBuilder item = new ItemBuilder(1).setDisplay(&quot;石头&quot;);item.addLore(&quot;这是一个普通的石头&quot;);item.addLore(&quot;我编不下去了...&quot;);
你也可以连起来写,不过看起来不太规整。。
ItemBuilder item = new ItemBuilder(1).setDisplay(&quot;石头&quot;).addLore(&quot;这是一个普通的石头&quot;).addLore(&quot;我编不下去了...&quot;);
附魔也是可以的:
item.enchant(Enchantment.ARROW_DAMAGE, 2);item.removeEnchant(Enchantment.ARROW_DAMAGE);
最后等你添加好自己喜欢的东西之后在用它的时候把它换成item.create()就可以了举个例子:player.getInventory().addItem(item.create());是不是很简单?如果你想修改一个已有的ItemStack,也是可以的ItemStack item = new ItemStack(Material.STONE, 1, (short) 1);ItemBuilder builder = new ItemBuilder(item);然后去修改你的东西吧~~
下载地址:
链接:http://pan.baidu.com/s/1boHNw3X 密码:0vsr
(注意:下载下来是一个.java后缀的源码,直接扔到你的插件所在的的包里,再改一下package声明就行了)
或者你可以直接复制这些东西到一个新的java类里

代码:

  1. <div>package woPH.function;</div><div>
  2. </div><div>import java.util.ArrayList;
  3. import java.util.HashMap;
  4. import java.util.List;
  5. import java.util.Map;</div><div>import org.bukkit.Material;
  6. import org.bukkit.enchantments.Enchantment;
  7. import org.bukkit.entity.Player;
  8. import org.bukkit.inventory.ItemStack;
  9. import org.bukkit.inventory.PlayerInventory;
  10. import org.bukkit.inventory.meta.ItemMeta;</div><div>
  11. </div><div>public class ItemBuilder{
  12. List<String>lore = new ArrayList<String>();
  13. String name = null;
  14. int id;
  15. int count = 1;
  16. short id2 = 0;
  17. Map<Enchantment, Integer>ench = null;

  18. public ItemBuilder(int id){
  19. this.id = id;
  20. }
  21. @SuppressWarnings("deprecation")
  22. public ItemBuilder(Material material){
  23. this.id = material.getId();
  24. }
  25. public ItemBuilder(int id, int id2){
  26. this.id = id;
  27. this.id2 = (short) id2;
  28. }
  29. @SuppressWarnings("deprecation")
  30. public ItemBuilder(ItemStack item){
  31. id = item.getTypeId();
  32. id2 = item.getDurability();
  33. ItemMeta meta = item.getItemMeta();
  34. if(meta.hasDisplayName()){
  35.    name = meta.getDisplayName();
  36. }else{
  37.    name = null;
  38. }
  39. if(meta.hasLore()){
  40.    lore = meta.getLore();
  41. }else{
  42.    lore = new ArrayList<String>();
  43. }
  44. if(item.getItemMeta().hasEnchants()){
  45.    ench = meta.getEnchants();
  46. }
  47. }
  48. public ItemStack create(){
  49. @SuppressWarnings("deprecation")
  50. ItemStack item = new ItemStack(id, count, (short) id2);
  51. ItemMeta meta = item.getItemMeta();
  52. if(!(name == null)){
  53.    meta.setDisplayName(name);
  54. }
  55. if(!lore.isEmpty()){
  56.    meta.setLore(lore);
  57. }
  58. item.setItemMeta(meta);
  59. if(!(ench == null)){
  60.    item.addEnchantments(ench);
  61. }
  62. return item;
  63. }
  64. public int getCount(){
  65. return count;
  66. }
  67. public ItemBuilder setCount(int count){
  68. if(count > 0){
  69.    this.count = count;
  70. }
  71. return this;
  72. }
  73. public String getDisplay(){
  74. return name;
  75. }
  76. public ItemBuilder setDisplay(String Display){
  77. name = Display;
  78. return this;
  79. }
  80. public List<String> getLore(){
  81. return lore;
  82. }
  83. public String getLore(int index){
  84. if(lore.size() >= index - 1){
  85.    return lore.get(index);
  86. }else{
  87.    return null;
  88. }
  89. }
  90. public ItemBuilder setLore(List<String> Lore){
  91. this.lore = Lore;
  92. return this;
  93. }
  94. public ItemBuilder addLore(List<String> Lore){
  95. lore.addAll(Lore);
  96. return this;
  97. }
  98. public ItemBuilder addLore(String Line){
  99. lore.add(Line);
  100. return this;
  101. }
  102. public Map<Enchantment, Integer> getEnchant(){
  103. return this.ench;
  104. }
  105. public boolean hasEnchant(Enchantment enchant){
  106. if(!hasEnchant()){
  107.    return false;
  108. }else{
  109.    return ench.containsKey(enchant);
  110. }
  111. }
  112. public boolean hasEnchant(){
  113. if(ench == null){
  114.    return false;
  115. }else{
  116.    return !ench.isEmpty();
  117. }
  118. }
  119. public int getEnchantLevel(Enchantment enchant){
  120. if(!hasEnchant(enchant)){
  121.    return 0;
  122. }else{
  123.    return ench.get(enchant);
  124. }
  125. }
  126. public ItemBuilder enchant(Enchantment enchant, int level){
  127. if(ench == null){
  128.    ench = new HashMap<Enchantment, Integer>();
  129. }
  130. ench.put(enchant, level);
  131. return this;
  132. }
  133. public ItemBuilder enchant(Enchantment enchant){
  134. if(ench == null){
  135.    ench = new HashMap<Enchantment, Integer>();
  136. }else if(ench.containsKey(enchant)){
  137.    ench.put(enchant, ench.get(enchant) + 1);
  138. }else{
  139.    ench.put(enchant, 1);
  140. }
  141. return this;
  142. }
  143. public ItemBuilder removeEnchant(Enchantment enchant){
  144. if(ench == null){
  145.    return this;
  146. }
  147. ench.remove(enchant);
  148. return this;
  149. }
  150. public static void consumeItem(Player player){
  151. PlayerInventory inv = player.getInventory();
  152. ItemStack item = inv.getItemInHand();
  153. short amount = (short) (item.getAmount() - 1);
  154. if(amount <= 0){
  155.    inv.clear(inv.getHeldItemSlot());
  156. }else{
  157.    item.setAmount(amount);
  158.    inv.setItem(inv.getHeldItemSlot(), item);
  159. }
  160. }
  161. }</div>



详细使用说明:
要创建一个ItemBuilder有很多种方法1.用物品id创建:new ItemBuilder(物品id);如果你想创建像染料一样有分号后面id的物品new ItemBuilder(物品id, 分号后面那个物品id);2.用Material创建new ItemBuilder(Material.方块名称);3.用已有的ItemStack创建new ItemBuilder(你的ItemStack);
给你的物品设置数量(如果不设置默认是一个):ItemBuilder item = new ItemBuilder(1);(下面我都用item表示itembuilder的实例,就不创建新的了)item.setCount(数量);
设置名称:item.setDisplay(随便写);
设置描述行:1.添加一个描述行:item.add(随便写);2.添加一群描述行:List&lt;String&gt;lore = new ArrayList&lt;String&gt;();lore.add(第一行);lore.add(第二行);item.addLore(lore);无特殊情况的话这种方法不如第一种简单3.设置描述行:item.setLore(lore);这会清空原有描述行,设置成新的
获取各种数据:
String name = item.getDisplay();int count = item.getCount();List&lt;String&gt;lore = item.getLore();String loreline = item.getLore(2);(2为索引,如果是第3行应该填2,以此类推)
附魔相关:
判断是否有附魔:1.是否有附魔:boolean hasench = item.hasEnchant();2.是否有指定类型的附魔boolean hasench = item.hasEnchant(附魔种类);3.获取指定附魔的等级(没有的话就为0)int level = item.getEnchantLevel(附魔种类);
添加附魔:1.增加一级特定种类的附魔(如果原来没有就升到一级)item.enchant(附魔种类);2.设定这个种类的附魔的等级item.enchant(附魔种类, 等级);3.删除一个种类的附魔item.removeEnchant(附魔种类);
消耗物品:ItemBuilder.consumeItem(player);这个功能是给我自己的服务器用的,用来让一个玩家手中的物品数量-1
而且所有方法都可以连起来写:item.setDisplay(名字).addLore(描述).enchant(某附魔).create();一气呵成
使用方法就是这些,感谢那些把所有内容看完再关掉网页的人。

kevinss
不错哦~支持

m9h6

谢谢支持~刚发帖就有人回,感谢感谢

MCGO1
支持原创插件!

kevinss
m9h6 发表于 2017-1-7 11:14
谢谢支持~刚发帖就有人回,感谢感谢

职业抢沙发30年~

Java_command
本帖最后由 Java_command 于 2017-1-6 15:34 编辑

也就是说直接跟自己的插件源码放在一起就可以了~
算是一个不错的“插件”吧

tallmoon
支持楼主,是挺方便的

Fisherman_wcz
额。兼容1.8以下,那1.8也兼容咯。建议改成[1.7-1.10]这样的

m9h6
Fisherman_wcz 发表于 2017-1-7 11:57
额。兼容1.8以下,那1.8也兼容咯。建议改成[1.7-1.10]这样的

其实因为我这个是利用物品id储存数据的,所以只要是物品id机制还保留的版本都可以用

m9h6
Java_command 发表于 2017-1-7 11:27
也就是说直接跟自己的插件源码放在一起就可以了~
算是一个不错的“插件”吧 ...

这的确不算是一个插件,顶多是一段源码。主要是我不知道这种东西发到哪。。。就说它是插件发出来了。
话说我感觉国内mc界会java做插件的人不是挺多的。。。所以这玩意其实没什么用。。。
主要是我自己做插件,有一些自己做的API比较好用,就挑一个发出来分享

2334072588
支持楼主,是挺方便的