From 2cef535e6f244ec5261ef6ee4fbec5e8580aa3ea Mon Sep 17 00:00:00 2001 From: ci010 Date: Fri, 14 Sep 2018 14:09:55 +0800 Subject: [PATCH] add revert skill limit; revert skill return exp is configurable now; --- .../goki/common/config/GokiConfig.java | 8 ++ .../network/packet/PacketStatAlter.java | 90 ++++++++++--------- .../goki/common/utils/DataHelper.java | 17 ++++ 3 files changed, 74 insertions(+), 41 deletions(-) diff --git a/src/main/java/net/infstudio/goki/common/config/GokiConfig.java b/src/main/java/net/infstudio/goki/common/config/GokiConfig.java index e686465..b7e71d2 100644 --- a/src/main/java/net/infstudio/goki/common/config/GokiConfig.java +++ b/src/main/java/net/infstudio/goki/common/config/GokiConfig.java @@ -39,5 +39,13 @@ public static class GlobalModifiers { @Config.Name("Death Loss Multiplier") @Config.Comment("Multiplier of levels you will lose, between 0~1.") public float loseStatsMultiplier = 1; + + @Config.Name("Maximum revertable skill level") + @Config.Comment("An integer that constrains the max number of level of the skill can be reverted. -1 for no limit.") + public int globalMaxRevertLevel = -1; + + @Config.Name("Revert Factor") + @Config.Comment("How much percentage of exp will be given back to player if a player revert a skill.") + public float globalRevertFactor = 0.8F; } } diff --git a/src/main/java/net/infstudio/goki/common/network/packet/PacketStatAlter.java b/src/main/java/net/infstudio/goki/common/network/packet/PacketStatAlter.java index 897cc0c..7018907 100644 --- a/src/main/java/net/infstudio/goki/common/network/packet/PacketStatAlter.java +++ b/src/main/java/net/infstudio/goki/common/network/packet/PacketStatAlter.java @@ -4,6 +4,7 @@ import io.netty.channel.ChannelHandlerContext; import net.infstudio.goki.GokiStats; import net.infstudio.goki.common.utils.DataHelper; +import net.infstudio.goki.common.config.GokiConfig; import net.infstudio.goki.common.stats.StatBase; import net.infstudio.goki.common.stats.StatMaxHealth; import net.minecraft.entity.SharedMonsterAttributes; @@ -40,50 +41,57 @@ public void handleClientSide(EntityPlayer player) { @Override public void handleServerSide(EntityPlayer player) { - if (player != null) { - StatBase stat = StatBase.stats.get(this.stat); - if (stat.enabled) { - int level = DataHelper.getPlayerStatLevel(player, stat); - if (this.amount > 0) { - int cost = stat.getCost(level + this.amount - 1); - int currentXP = DataHelper.getXPTotal(player.experienceLevel, - player.experience); - - if (stat.enabled) { - if (level + this.amount > stat.getLimit()) { - this.amount = 0; - } - - if ((currentXP >= cost) && (this.amount != 0)) { - DataHelper.setPlayerStatLevel(player, - stat, - level + this.amount); - if (stat instanceof StatMaxHealth) { - player.getEntityAttribute(SharedMonsterAttributes.MAX_HEALTH).setBaseValue(20 + level + this.amount); - } - - if (this.amount > 0) { - DataHelper.setPlayersExpTo(player, currentXP - cost); - } - } - } - } else if (DataHelper.getPlayerStatLevel(player, stat) > 0) { - player.addExperience((int) (stat.getCost(level + this.amount - 2) * 0.8)); // TODO Configure 0.8 - DataHelper.setPlayerStatLevel(player, - stat, - level + this.amount); - if (stat instanceof StatMaxHealth) { - player.getEntityAttribute(SharedMonsterAttributes.MAX_HEALTH).setBaseValue(20 + level + this.amount); - } + if (player == null) + return; + StatBase stat = StatBase.stats.get(this.stat); + if (!stat.enabled) + return; + int level = DataHelper.getPlayerStatLevel(player, stat); + if (this.amount > 0) { + + if (level + this.amount > stat.getLimit()) { + this.amount = stat.getLimit() - level; + } + + int cost = stat.getCost(level + this.amount - 1); + int currentXP = DataHelper.getXPTotal(player.experienceLevel, player.experience); + + if (currentXP >= cost) { + int reverted = DataHelper.getPlayerRevertStatLevel(player, stat); + reverted = Math.max(reverted - this.amount, 0); + DataHelper.setPlayerRevertStatLevel(player, stat, reverted); + + DataHelper.setPlayerStatLevel(player, stat, level + this.amount); + if (stat instanceof StatMaxHealth) { + player.getEntityAttribute(SharedMonsterAttributes.MAX_HEALTH) + .setBaseValue(20 + level + this.amount); } + + DataHelper.setPlayersExpTo(player, currentXP - cost); + } + + GokiStats.packetPipeline.sendTo(new PacketStatSync(player), (EntityPlayerMP) player); + GokiStats.packetPipeline.sendTo(new PacketSyncXP(player), (EntityPlayerMP) player); + } else if (DataHelper.getPlayerStatLevel(player, stat) > 0) { + + int reverted = DataHelper.getPlayerRevertStatLevel(player, stat); + int maxRevert = GokiConfig.globalModifiers.globalMaxRevertLevel; + + if (maxRevert > 0 && reverted + (-this.amount) > maxRevert) { // check if we limit the max revert, and if the revert overflow the value + this.amount = -(maxRevert - reverted); // ceil the value for max revert + } + + reverted += (-this.amount); + DataHelper.setPlayerRevertStatLevel(player, stat, reverted); + + player.addExperience((int) (stat.getCost(level + this.amount - 2) * GokiConfig.globalModifiers.globalRevertFactor)); + DataHelper.setPlayerStatLevel(player, stat, level + this.amount); + if (stat instanceof StatMaxHealth) { + player.getEntityAttribute(SharedMonsterAttributes.MAX_HEALTH).setBaseValue(20 + level + this.amount); } -// else if (this.amount >= 0) -// ; - GokiStats.packetPipeline.sendTo(new PacketStatSync(player), - (EntityPlayerMP) player); - GokiStats.packetPipeline.sendTo(new PacketSyncXP(player), - (EntityPlayerMP) player); + GokiStats.packetPipeline.sendTo(new PacketStatSync(player), (EntityPlayerMP) player); + GokiStats.packetPipeline.sendTo(new PacketSyncXP(player), (EntityPlayerMP) player); } } } \ No newline at end of file diff --git a/src/main/java/net/infstudio/goki/common/utils/DataHelper.java b/src/main/java/net/infstudio/goki/common/utils/DataHelper.java index ff5987b..55dd900 100644 --- a/src/main/java/net/infstudio/goki/common/utils/DataHelper.java +++ b/src/main/java/net/infstudio/goki/common/utils/DataHelper.java @@ -24,6 +24,23 @@ public static NBTTagCompound getPlayerPersistentNBT(EntityPlayer player) { return nbt; } + public static int getPlayerRevertStatLevel(EntityPlayer player, StatBase stat) { + NBTTagCompound nbt = getPlayerPersistentNBT(player); + if (nbt.hasKey("gokistats_Stats")) { + return ((NBTTagCompound) nbt.getTag("gokistats_Stats")).getInteger(stat.key + ".revert"); + } + return 0; + } + + public static int setPlayerRevertStatLevel(EntityPlayer player, StatBase stat, int level) { + NBTTagCompound nbt = getPlayerPersistentNBT(player); + if (nbt.hasKey("gokistats_Stats")) { + ((NBTTagCompound) nbt.getTag("gokistats_Stats")).setInteger(stat.key + ".revert", + (byte) level); + } + return 0; + } + public static int getPlayerStatLevel(EntityPlayer player, StatBase stat) { NBTTagCompound nbt = getPlayerPersistentNBT(player); if (nbt.hasKey("gokistats_Stats")) {