Skip to content

Commit

Permalink
fix: 吸血可能造成的假死问题
Browse files Browse the repository at this point in the history
add: RcRPG 等级为可开关配置
change: PlayerAttrUpdateTask 现在会立即更新玩家属性
  • Loading branch information
Mcayear committed Dec 28, 2023
1 parent db8bb1d commit f3ae9d0
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 49 deletions.
1 change: 1 addition & 0 deletions src/main/java/RcRPG/AttrManager/Manager.java
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ protected Map<String, float[]> deepCopyMap(Map<String, float[]> originalMap) {

return copiedMap;
}

/**
* 检查 float[] 是否为空
* @param array
Expand Down
46 changes: 24 additions & 22 deletions src/main/java/RcRPG/AttrManager/PlayerAttr.java
Original file line number Diff line number Diff line change
Expand Up @@ -203,28 +203,6 @@ public void showAttrWindow(Player p) {
player.sendMessage("[NWeapon] 没有玩家§8" + p.getName() + "§f的数据");
return;
}
/** 开始
Main.instance.getLogger().warning("属性结构:");
Main.instance.getLogger().warning("{");
for (Map.Entry<String, Map<String, float[]>> entry : myAttr.entrySet()) {
String outerKey = entry.getKey();
Map<String, float[]> innerMapValue = entry.getValue();
Main.instance.getLogger().warning(" \"" + outerKey + "\": {");
for (Map.Entry<String, float[]> innerEntry : innerMapValue.entrySet()) {
String innerKey = innerEntry.getKey();
float[] innerArray = innerEntry.getValue();
Main.instance.getLogger().warning(" \"" + innerKey + "\": " + "[" + innerArray[0] + ", " + innerArray[1] + "]");
}
Main.instance.getLogger().warning(" }");
}
Main.instance.getLogger().warning("}");
// 结束
*/
String str = "";
Map<String, Map<String, float[]>> data = pAttr.myAttr;

Expand Down Expand Up @@ -284,6 +262,30 @@ public void showAttrWindow(Player p) {
player.showFormWindow(win);
}

public String toString() {
StringBuilder result = new StringBuilder();
result.append("属性结构:\n");
result.append("{\n");

for (Map.Entry<String, Map<String, float[]>> entry : myAttr.entrySet()) {
String outerKey = entry.getKey();
Map<String, float[]> innerMapValue = entry.getValue();

result.append(" \"" + outerKey + "\": {\n");

for (Map.Entry<String, float[]> innerEntry : innerMapValue.entrySet()) {
String innerKey = innerEntry.getKey();
float[] innerArray = innerEntry.getValue();

result.append(" \"" + innerKey + "\": " + "[" + innerArray[0] + ", " + innerArray[1] + "]\n");
}

result.append(" }\n");
}
result.append("}\n");

return result.toString();
}
/**
* 将数据可视化,输入data,属性输出min-max或x%
*/
Expand Down
58 changes: 35 additions & 23 deletions src/main/java/RcRPG/Events.java
Original file line number Diff line number Diff line change
Expand Up @@ -110,13 +110,16 @@ public void blockBreak(BlockBreakEvent event){
public void onHeld(PlayerItemHeldEvent event){
Player player = event.getPlayer();
Item item = event.getItem();
if(!item.isNull() && Weapon.isWeapon(item)){
Weapon weapon = Main.loadWeapon.get(item.getNamedTag().getString("name"));
if (weapon == null) return;// TODO: 可能要做无效装备的清除?
if(Level.getLevel(player) < weapon.getLevel()){
player.sendMessage("等级不足武器使用等级");
event.setCancelled();
}
if (item.isNull() || !Weapon.isWeapon(item)) {
return;
}
Weapon weapon = Main.loadWeapon.get(item.getNamedTag().getString("name"));

if (weapon == null) return;// TODO: 可能要做无效装备的清除?

if (Level.enable ? Level.getLevel(player) < weapon.getLevel() : player.getExperienceLevel() < weapon.getLevel()) {
player.sendMessage("等级不足武器使用等级");
event.setCancelled();
}
}

Expand Down Expand Up @@ -544,23 +547,32 @@ public void damageEvent(EntityDamageByEntityEvent event){

// 吸血处理
if (lifeSteal > 0) {
float wHealth = wounded.getHealth();
if (hasHealthAPI && woundedIsPlayer) {// 血量核心
PlayerHealth playerHealth = PlayerHealth.getPlayerHealth((Player) wounded);
wHealth = (float) playerHealth.getHealth();
}
// 吸血不超过受害者现有血量
if (wHealth < lifeSteal) lifeSteal = wHealth;
if (hasHealthAPI && damagerIsPlayer) {// 若存在血量核心
float dHealth = damager.getHealth();
if (hasHealthAPI && damagerIsPlayer) {// 血量核心
PlayerHealth playerHealth = PlayerHealth.getPlayerHealth((Player) damager);
int MaxH = playerHealth.getMaxHealth();
double H = playerHealth.getHealth() + lifeSteal;
playerHealth.setHealth(H > MaxH ? MaxH : H);
} else {
damager.heal(new EntityRegainHealthEvent(damager, (float) lifeSteal, 3));
dHealth = (float) playerHealth.getHealth();
}
if (damagerIsPlayer) {
((Player) damager).sendMessage("你已汲取对方 §c§l" + lifeSteal + "§r 血量值");
if (dHealth - finalDamage < 1) {// 这似乎不需要减去 finalDamage
// 不处理吸血,因为这会导致血量陷入假死
} else {
float wHealth = wounded.getHealth();
if (hasHealthAPI && woundedIsPlayer) {// 血量核心
PlayerHealth playerHealth = PlayerHealth.getPlayerHealth((Player) wounded);
wHealth = (float) playerHealth.getHealth();
}
// 吸血不超过受害者现有血量
if (wHealth < lifeSteal) lifeSteal = wHealth;
if (hasHealthAPI && damagerIsPlayer) {// 若存在血量核心
PlayerHealth playerHealth = PlayerHealth.getPlayerHealth((Player) damager);
int MaxH = playerHealth.getMaxHealth();
double H = playerHealth.getHealth() + lifeSteal;
playerHealth.setHealth(H > MaxH ? MaxH : H);
} else {
damager.heal(new EntityRegainHealthEvent(damager, (float) lifeSteal, 3));
}
if (damagerIsPlayer) {
((Player) damager).sendMessage("你已汲取对方 §c§l" + lifeSteal + "§r 血量值");
}
}
}

Expand Down Expand Up @@ -633,7 +645,7 @@ public void chatEvent(PlayerChatEvent event){
}

@EventHandler
public void joinEvent(PlayerJoinEvent event){
public void joinEvent(PlayerPreLoginEvent event){
Player player = event.getPlayer();
String name = player.getName();

Expand Down
3 changes: 3 additions & 0 deletions src/main/java/RcRPG/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ public void onEnable(){
if (config.exists("称号.disable") && config.getBoolean("称号.disable")) {
disablePrefix = true;
}
if (config.exists("RcRPG经验.disable") && config.getBoolean("RcRPG经验.disable")) {
Level.enable = false;
}

this.getServer().getPluginManager().registerEvents(new Events(),this);
if (config.exists("底部显示") && !config.getString("底部显示").equals("")) {
Expand Down
1 change: 1 addition & 0 deletions src/main/java/RcRPG/RPG/Level.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
public class Level {

public static Level instance;
public static boolean enable = true;

public Level(){
instance = this;
Expand Down
15 changes: 11 additions & 4 deletions src/main/java/RcRPG/Task/PlayerAttrUpdateTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,18 @@ public void onRun(int i) {
Objects.requireNonNull(PlayerAttr.getPlayerAttr(player)).update();
PlayerAttr pAttr = PlayerAttr.getPlayerAttr(player);
if (pAttr == null) continue;
int maxh;
if (pAttr.hp != 0) {
maxh = (int) (pAttr.hp * (1 + pAttr.hpRegenMultiplier));

pAttr.updateComp();

int maxh = 20;// 默认血量上限
if (Events.hasHealthAPI) {// 若存在血量核心
maxh = 0;
}

if (pAttr.hp > 0) {
maxh = (int) ((maxh + pAttr.hp) * (1 + pAttr.hpRegenMultiplier));
} else {
maxh = 20 * (1 + (int) pAttr.hpRegenMultiplier);
maxh = maxh * (1 + (int) pAttr.hpRegenMultiplier);
}

addHealth += pAttr.hpPerSecond;
Expand Down
2 changes: 2 additions & 0 deletions src/main/resources/Config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
底部显示: ""
# "§a玩家:§e @name §f||§a 等级:§e @level§f ||§a 经验:§e @exp§c/§e@maxexp§f ||§a 公会:§e @guild@n§a血量:§e @hp§c/§e@maxhp§f ||§a 称号:§e @prefix§f ||§a 金币:§e @money§f ||§a 点券:§e @point"

RcRPG经验:
disable: true
经验增加提示: "你获得了@exp点经验"
等级增加提示: "你提升了@level等级,达到了@newLevel"
经验增量: 100
Expand Down

0 comments on commit f3ae9d0

Please sign in to comment.