Skip to content

Commit

Permalink
patch: 实现锻造物品的属性计算
Browse files Browse the repository at this point in the history
  • Loading branch information
Mcayear committed Sep 14, 2024
1 parent 209e284 commit 1126ce3
Show file tree
Hide file tree
Showing 6 changed files with 193 additions and 119 deletions.
64 changes: 43 additions & 21 deletions src/main/java/RcRPG/AttrManager/FootageAttr.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,17 @@
import cn.nukkit.nbt.tag.CompoundTag;
import cn.nukkit.nbt.tag.FloatTag;
import cn.nukkit.nbt.tag.ListTag;
import cn.nukkit.nbt.tag.Tag;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class FootageAttr extends Manager {

public FootageAttr() {
myAttr.put("Main", new HashMap<>());
}
/**
* 属性结构
* {
Expand All @@ -21,6 +25,10 @@ public class FootageAttr extends Manager {
*/
public Map<String, Map<String, float[]>> myAttr = new HashMap<>();

public Map<String, float[]> getMainAttr() {
return myAttr.get("Main");
}

public void setItemAttrConfig(String id, Map<String, Object> newAttr) {
Map<String, float[]> attrMap = new HashMap<>();
for (Map.Entry<String, Object> entry : newAttr.entrySet()) {
Expand Down Expand Up @@ -91,40 +99,54 @@ public void setItemAttrConfig(String id, Map<String, Object> newAttr) {
* @return 返回包含所有属性的 CompoundTag,用于保存至物品
*/
public CompoundTag toNBT() {
// 创建一个根CompoundTag来存储所有属性
// 创建一个根CompoundTag来存储主属性
CompoundTag nbt = new CompoundTag();

// 遍历属性映射,将每个属性转换为NBT格式
for (Map.Entry<String, Map<String, float[]>> entry : myAttr.entrySet()) {
// 获取当前条目的属性ID
String id = entry.getKey();
// 获取当前条目的属性映射
Map<String, float[]> attrMap = entry.getValue();

// 为当前属性创建一个CompoundTag
CompoundTag attrTag = new CompoundTag();
// 遍历属性映射,将每个属性值转换为ListTag并添加到attrTag
for (Map.Entry<String, float[]> attrEntry : attrMap.entrySet()) {
// 获取当前属性的键
// 获取主属性
Map<String, float[]> mainAttr = getMainAttr();

// 如果主属性不为空,遍历它并将其转换为NBT格式
if (mainAttr != null) {
for (Map.Entry<String, float[]> attrEntry : mainAttr.entrySet()) {
// 获取属性键和值
String attrKey = attrEntry.getKey();
// 获取当前属性的值,是一个浮点数数组
float[] values = attrEntry.getValue();

// 创建一个ListTag来存储属性值
ListTag<FloatTag> valueList = new ListTag<>();
// 将属性值添加到ListTag中
valueList.add(new FloatTag("", values[0]));
valueList.add(new FloatTag("", values[1]));

// 将ListTag添加到attrTag中
attrTag.putList(attrKey, valueList);
// 将ListTag添加到根CompoundTag中
nbt.putList(attrKey, valueList);
}

// 将attrTag添加到根CompoundTag中
nbt.putCompound(id, attrTag);
}

// 返回包含所有属性的CompoundTag
// 返回包含主属性的CompoundTag
return nbt;
}

public static Map<String, float[]> fromNBT(CompoundTag compoundTag) {
// 创建一个Map来存储反序列化后的主属性
Map<String, float[]> mainAttr = new HashMap<>();

// 遍历CompoundTag中的每个Tag
for (Tag tag : compoundTag.getAllTags()) {
if (tag instanceof ListTag<?> valueList && tag.getName() != null) {
String attrKey = tag.getName(); // 获取属性的键

// 提取ListTag中的两个值并存储到一个float数组中
float[] values = new float[2];
values[0] = ((FloatTag) valueList.get(0)).getData(); // 确保是FloatTag
values[1] = ((FloatTag) valueList.get(1)).getData();

// 将该属性键和值添加到主属性Map中
mainAttr.put(attrKey, values);
}
}

// 返回反序列化后的主属性Map
return mainAttr;
}

}
4 changes: 2 additions & 2 deletions src/main/java/RcRPG/AttrManager/PlayerAttr.java
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ public void update() {
});
}

setItemAttrConfig(weapon.getLabel(), weapon.getMainAttr());
setItemAttrConfig(weapon.getLabel(), ForgingItem.isForgingItem(rcItem) ? ForgingItem.fromNBT(rcItem.getNamedTag().getCompound("attr")) : weapon.getMainAttr());
checkItemStoneAttr(weapon.getLabel(), Weapon.getStones(rcItem), beforeLabel, labelList);

beforeLabel.remove(weapon.getLabel());
Expand All @@ -153,7 +153,7 @@ public void update() {
if (rcItem.getNamedTag() == null) continue;
Armour armour = RcRPGMain.loadArmour.get(rcItem.getNamedTag().getString("name"));
if (armour == null) continue;
setItemAttrConfig(armour.getLabel(), armour.getMainAttr());
setItemAttrConfig(armour.getLabel(), ForgingItem.isForgingItem(rcItem) ? ForgingItem.fromNBT(rcItem.getNamedTag().getCompound("attr")) : armour.getMainAttr());
checkItemStoneAttr(armour.getLabel(), Armour.getStones(rcItem), beforeLabel, labelList);

if (!armour.getSuit().isEmpty()) {// 套装
Expand Down
72 changes: 40 additions & 32 deletions src/main/java/RcRPG/RPG/Armour.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

@Getter
@Setter
public class Armour extends ItemAttr {
public class Armour extends ItemAttr implements Cloneable {

private Config config;

Expand Down Expand Up @@ -88,45 +88,53 @@ public Armour(String name, Config config) {
this.config = config;
}

public static Armour loadArmour(String name, Config config) {
try {
Armour armour = new Armour(name, config);
public Armour initArmour() {
this.setLabel(config.getString("标签"));
this.setShowName(config.getString("显示名称"));
this.setItem(Item.fromString(config.getString("物品ID")));
if (config.exists("属性")) {
this.setAttr((Map<String, Object>) config.get("属性"));
}
this.setMessage(config.getString("介绍", ""));

armour.setLabel(config.getString("标签"));
armour.setShowName(config.getString("显示名称"));
armour.setItem(Item.fromString(config.getString("物品ID")));
if (config.exists("属性")) {
armour.setAttr((Map<String, Object>) config.get("属性"));
this.setColor(loadColorFromConfig(config));

this.setDismantle(config.getString("分解", ""));

if (config.exists("套装")) {
List<String> suitList;
if (config.isList("套装")) {
suitList = config.getStringList("套装");
} else {
suitList = new ArrayList<>(Arrays.asList(config.getString("套装", "").split(",")));
}
armour.setMessage(config.getString("介绍", ""));
this.setSuit(suitList);
}

armour.setColor(loadColorFromConfig(config));
this.setTipText(config.getString("底部显示"));
this.setMyMessage(config.getString("个人通知"));
this.setServerMessage(config.getString("全服通知"));
this.setStone(config.getInt("宝石孔数"));

armour.setDismantle(config.getString("分解", ""));
this.setEffects(loadEffectsFromConfig(config));

if (config.exists("套装")) {
List<String> suitList;
if (config.isList("套装")) {
suitList = config.getStringList("套装");
} else {
suitList = new ArrayList<>(Arrays.asList(config.getString("套装", "").split(",")));
}
armour.setSuit(suitList);
}
ArrayList<String> loreList = new ArrayList<>(config.getStringList("显示"));
this.setLoreList(loreList);
ArrayList<String> stoneList = new ArrayList<>(config.getStringList("宝石槽"));
this.setStoneList(stoneList);

armour.setTipText(config.getString("底部显示"));
armour.setMyMessage(config.getString("个人通知"));
armour.setServerMessage(config.getString("全服通知"));
armour.setStone(config.getInt("宝石孔数"));
return this;
}

armour.setEffects(loadEffectsFromConfig(config));
@Override
public Armour clone() {
return new Armour(this.name, this.config).initArmour();
}

ArrayList<String> loreList = new ArrayList<>(config.getStringList("显示"));
armour.setLoreList(loreList);
ArrayList<String> stoneList = new ArrayList<>(config.getStringList("宝石槽"));
armour.setStoneList(stoneList);
public static Armour loadArmour(String name, Config config) {
try {

return armour;
return new Armour(name, config).initArmour();
} catch (Exception e) {
e.printStackTrace();
RcRPGMain.getInstance().getLogger().error("加载盔甲" + name + "配置文件失败");
Expand Down Expand Up @@ -195,7 +203,7 @@ public static boolean delArmourConfig(String name) {

public static Item getItem(String name, int count, LangCode langCode) {
Armour armour = RcRPGMain.loadArmour.get(name);
Item item = armour.getItem();
Item item = armour.getItem().clone();
item.setCount(count);
CompoundTag tag = item.hasCompoundTag() ? item.getNamedTag() : new CompoundTag();
tag.putString("type", "armour");
Expand Down
20 changes: 20 additions & 0 deletions src/main/java/RcRPG/RPG/ForgingItem.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package RcRPG.RPG;

import RcRPG.AttrManager.FootageAttr;
import cn.nukkit.item.Item;
import cn.nukkit.nbt.tag.CompoundTag;

import java.util.Map;

public class ForgingItem {
public static boolean isForgingItem(Item item) {
if (item.getNamedTag() == null) {
return false;
}
return item.getNamedTag().containsCompound("attr");
}

public static Map<String, float[]> fromNBT(CompoundTag compoundTag) {
return FootageAttr.fromNBT(compoundTag);
}
}
Loading

0 comments on commit 1126ce3

Please sign in to comment.