优化NBT#Level Exp UpgradePoint

This commit is contained in:
17146 2025-01-04 01:51:36 +08:00
parent 645effa77b
commit ef1cedbf0a
5 changed files with 37 additions and 41 deletions

View file

@ -8,7 +8,6 @@ import com.atsuishio.superbwarfare.perk.AmmoPerk;
import com.atsuishio.superbwarfare.perk.Perk;
import com.atsuishio.superbwarfare.perk.PerkHelper;
import com.atsuishio.superbwarfare.tools.GunsTool;
import com.atsuishio.superbwarfare.tools.ItemNBTTool;
import com.atsuishio.superbwarfare.tools.TooltipTool;
import net.minecraft.ChatFormatting;
import net.minecraft.client.gui.Font;
@ -23,7 +22,6 @@ import org.jetbrains.annotations.NotNull;
import java.text.DecimalFormat;
// TODO 等nbt重置后修改nbt位置
public class ClientGunImageTooltip implements ClientTooltipComponent {
protected final int width;
@ -106,7 +104,7 @@ public class ClientGunImageTooltip implements ClientTooltipComponent {
if (this.stack.getItem() instanceof GunItem gunItem && gunItem.autoWeapon(this.stack)) {
return Component.translatable("des.superbwarfare.tips.rpm").withStyle(ChatFormatting.GRAY)
.append(Component.literal("").withStyle(ChatFormatting.RESET))
.append(Component.literal(new DecimalFormat("##").format(GunsTool.getGunIntTag(stack, "RPM", 0) + ItemNBTTool.getDouble(stack, "customRpm", 0)))
.append(Component.literal(new DecimalFormat("##").format(GunsTool.getGunIntTag(stack, "RPM", 0)))
.withStyle(ChatFormatting.GREEN));
}
return Component.literal("");
@ -125,8 +123,8 @@ public class ClientGunImageTooltip implements ClientTooltipComponent {
* 获取武器等级文本组件
*/
protected Component getLevelComponent() {
int level = ItemNBTTool.getInt(stack, "Level", 0);
double rate = ItemNBTTool.getDouble(stack, "Exp", 0) / (20 * Math.pow(level, 2) + 160 * level + 20);
int level = GunsTool.getGunIntTag(stack, "Level", 0);
double rate = GunsTool.getGunDoubleTag(stack, "Exp", 0) / (20 * Math.pow(level, 2) + 160 * level + 20);
ChatFormatting formatting;
if (level < 10) {
@ -152,7 +150,7 @@ public class ClientGunImageTooltip implements ClientTooltipComponent {
* 获取武器强化点数文本组件
*/
protected Component getUpgradePointComponent() {
int upgradePoint = Mth.floor(ItemNBTTool.getDouble(stack, "UpgradePoint", 0));
int upgradePoint = Mth.floor(GunsTool.getGunDoubleTag(stack, "UpgradePoint", 0));
return Component.translatable("des.superbwarfare.tips.upgrade_point").withStyle(ChatFormatting.GRAY)
.append(Component.literal("").withStyle(ChatFormatting.RESET))
.append(Component.literal(String.valueOf(upgradePoint)).withStyle(ChatFormatting.WHITE).withStyle(ChatFormatting.BOLD));

View file

@ -446,7 +446,6 @@ public class ClientEventHandler {
burstFireSize--;
}
if (stack.is(ModItems.DEVOTION.get())) {
int perkLevel = PerkHelper.getItemPerkLevel(ModPerks.TURBO_CHARGER.get(), stack);
customRpm = Math.min(customRpm + 15 + ((perkLevel > 0 ? 5 : 0) + 3 * perkLevel), 500);

View file

@ -189,14 +189,14 @@ public class LivingEventHandler {
// 先处理发射器类武器或高爆弹的爆炸伤害
if (source.is(ModDamageTypes.PROJECTILE_BOOM)) {
if (stack.is(ModTags.Items.LAUNCHER) || PerkHelper.getItemPerkLevel(ModPerks.HE_BULLET.get(), stack) > 0) {
stack.getOrCreateTag().putDouble("Exp", stack.getOrCreateTag().getDouble("Exp") + amount);
GunsTool.setGunDoubleTag(stack, "Exp", GunsTool.getGunDoubleTag(stack, "Exp", 0) + amount);
}
}
// 再判断是不是枪械能造成的伤害
if (!DamageTypeTool.isGunDamage(source)) return;
stack.getOrCreateTag().putDouble("Exp", stack.getOrCreateTag().getDouble("Exp") + amount);
GunsTool.setGunDoubleTag(stack, "Exp", GunsTool.getGunDoubleTag(stack, "Exp", 0) + amount);
}
private static void giveKillExpToWeapon(LivingDeathEvent event) {
@ -209,32 +209,31 @@ public class LivingEventHandler {
if (event.getEntity() instanceof TargetEntity) return;
double amount = 20 + 2 * event.getEntity().getMaxHealth();
var tag = stack.getOrCreateTag();
// 先处理发射器类武器或高爆弹的爆炸伤害
if (source.is(ModDamageTypes.PROJECTILE_BOOM)) {
if (stack.is(ModTags.Items.LAUNCHER) || PerkHelper.getItemPerkLevel(ModPerks.HE_BULLET.get(), stack) > 0) {
tag.putDouble("Exp", tag.getDouble("Exp") + amount);
GunsTool.setGunDoubleTag(stack, "Exp", GunsTool.getGunDoubleTag(stack, "Exp", 0) + amount);
}
}
// 再判断是不是枪械能造成的伤害
if (DamageTypeTool.isGunDamage(source)) {
tag.putDouble("Exp", tag.getDouble("Exp") + amount);
GunsTool.setGunDoubleTag(stack, "Exp", GunsTool.getGunDoubleTag(stack, "Exp", 0) + amount);
}
// 提升武器等级
int level = tag.getInt("Level");
double exp = tag.getDouble("Exp");
int level = GunsTool.getGunIntTag(stack, "Level", 0);
double exp = GunsTool.getGunDoubleTag(stack, "Exp", 0);
double upgradeExpNeeded = 20 * Math.pow(level, 2) + 160 * level + 20;
while (exp >= upgradeExpNeeded) {
exp -= upgradeExpNeeded;
level = tag.getInt("Level") + 1;
level = GunsTool.getGunIntTag(stack, "Level", 0) + 1;
upgradeExpNeeded = 20 * Math.pow(level, 2) + 160 * level + 20;
tag.putDouble("Exp", exp);
tag.putInt("Level", level);
tag.putDouble("UpgradePoint", tag.getDouble("UpgradePoint") + 0.5);
GunsTool.setGunDoubleTag(stack, "Exp", exp);
GunsTool.setGunIntTag(stack, "Level", level);
GunsTool.setGunDoubleTag(stack, "UpgradePoint", GunsTool.getGunDoubleTag(stack, "UpgradePoint", 0) + 0.5);
}
}
@ -247,18 +246,17 @@ public class LivingEventHandler {
if (!stack.is(ModTags.Items.GUN)) return;
if (event.getEntity() instanceof TargetEntity) return;
var tag = stack.getOrCreateTag();
int level = tag.getInt("Level");
double exp = tag.getDouble("Exp");
int level = GunsTool.getGunIntTag(stack, "Level", 0);
double exp = GunsTool.getGunDoubleTag(stack, "Exp", 0);
double upgradeExpNeeded = 20 * Math.pow(level, 2) + 160 * level + 20;
while (exp >= upgradeExpNeeded) {
exp -= upgradeExpNeeded;
level = tag.getInt("Level") + 1;
level = GunsTool.getGunIntTag(stack, "Level", 0) + 1;
upgradeExpNeeded = 20 * Math.pow(level, 2) + 160 * level + 20;
tag.putDouble("Exp", exp);
tag.putInt("Level", level);
tag.putDouble("UpgradePoint", tag.getDouble("UpgradePoint") + 0.5);
GunsTool.setGunDoubleTag(stack, "Exp", exp);
GunsTool.setGunIntTag(stack, "Level", level);
GunsTool.setGunDoubleTag(stack, "UpgradePoint", GunsTool.getGunDoubleTag(stack, "UpgradePoint", 0) + 0.5);
}
}

View file

@ -380,7 +380,7 @@ public class PlayerEventHandler {
if (left.is(ModTags.Items.GUN) && right.getItem() == ModItems.SHORTCUT_PACK.get()) {
ItemStack output = left.copy();
output.getOrCreateTag().putDouble("UpgradePoint", output.getOrCreateTag().getDouble("UpgradePoint") + 1);
GunsTool.setGunDoubleTag(output, "UpgradePoint", GunsTool.getGunDoubleTag(output, "UpgradePoint", 0) + 1);
event.setOutput(output);
event.setCost(10);

View file

@ -7,6 +7,7 @@ import com.atsuishio.superbwarfare.item.PerkItem;
import com.atsuishio.superbwarfare.item.gun.GunItem;
import com.atsuishio.superbwarfare.perk.Perk;
import com.atsuishio.superbwarfare.perk.PerkHelper;
import com.atsuishio.superbwarfare.tools.GunsTool;
import net.minecraft.util.Mth;
import net.minecraft.world.Container;
import net.minecraft.world.SimpleContainer;
@ -204,13 +205,13 @@ public class ReforgingTableMenu extends AbstractContainerMenu {
return;
}
double oldPoint = stack.getOrCreateTag().getDouble("UpgradePoint");
double oldPoint = GunsTool.getGunDoubleTag(stack, "UpgradePoint", 0);
int point = (int) oldPoint;
int newPoint = this.upgradePoint.get();
int delta = newPoint - point;
if (delta != 0) {
stack.getOrCreateTag().putDouble("UpgradePoint", oldPoint + delta);
GunsTool.setGunDoubleTag(stack, "UpgradePoint", oldPoint + delta);
}
}
@ -286,14 +287,14 @@ public class ReforgingTableMenu extends AbstractContainerMenu {
int level = PerkHelper.getItemPerkLevel(perkItem.getPerk(), gun);
if (level <= 0) {
this.upgradePoint.set((int) gun.getOrCreateTag().getDouble("UpgradePoint"));
this.upgradePoint.set((int) GunsTool.getGunDoubleTag(gun, "UpgradePoint", 0));
return;
}
ItemStack output = gun.copy();
PerkHelper.removePerkByType(output, perkItem.getPerk().type);
output.getOrCreateTag().putDouble("UpgradePoint", Math.min(MAX_UPGRADE_POINT, level - 1 + output.getOrCreateTag().getDouble("UpgradePoint")));
this.upgradePoint.set((int) output.getOrCreateTag().getDouble("UpgradePoint"));
GunsTool.setGunDoubleTag(output, "UpgradePoint", Math.min(MAX_UPGRADE_POINT, level - 1 + GunsTool.getGunDoubleTag(output, "UpgradePoint", 0)));
this.upgradePoint.set((int) GunsTool.getGunDoubleTag(gun, "UpgradePoint", 0));
this.container.setItem(INPUT_SLOT, output);
this.container.setChanged();
@ -320,33 +321,33 @@ public class ReforgingTableMenu extends AbstractContainerMenu {
/**
* 将枪械放入输入槽中时根据枪械上已有的Perk生成对应的Perk物品并将等级调整为当前的等级
*
* @param pStack 输入的枪械
* @param stack 输入的枪械
*/
private void onPlaceGun(ItemStack pStack) {
if (!(pStack.getItem() instanceof GunItem)) {
private void onPlaceGun(ItemStack stack) {
if (!(stack.getItem() instanceof GunItem)) {
return;
}
int point = (int) pStack.getOrCreateTag().getDouble("UpgradePoint");
int point = (int) GunsTool.getGunDoubleTag(stack, "UpgradePoint", 0);
this.upgradePoint.set(Mth.clamp(point, 0, MAX_UPGRADE_POINT));
var ammoPerk = PerkHelper.getPerkByType(pStack, Perk.Type.AMMO);
var ammoPerk = PerkHelper.getPerkByType(stack, Perk.Type.AMMO);
if (ammoPerk != null) {
this.ammoPerkLevel.set(PerkHelper.getItemPerkLevel(ammoPerk, pStack));
this.ammoPerkLevel.set(PerkHelper.getItemPerkLevel(ammoPerk, stack));
var ammoPerkItem = PerkHelper.getPerkItem(ammoPerk);
ammoPerkItem.ifPresent(registryObject -> this.container.setItem(AMMO_PERK_SLOT, registryObject.get().getDefaultInstance()));
}
var funcPerk = PerkHelper.getPerkByType(pStack, Perk.Type.FUNCTIONAL);
var funcPerk = PerkHelper.getPerkByType(stack, Perk.Type.FUNCTIONAL);
if (funcPerk != null) {
this.funcPerkLevel.set(PerkHelper.getItemPerkLevel(funcPerk, pStack));
this.funcPerkLevel.set(PerkHelper.getItemPerkLevel(funcPerk, stack));
var funcPerkItem = PerkHelper.getPerkItem(funcPerk);
funcPerkItem.ifPresent(registryObject -> this.container.setItem(FUNC_PERK_SLOT, registryObject.get().getDefaultInstance()));
}
var damagePerk = PerkHelper.getPerkByType(pStack, Perk.Type.DAMAGE);
var damagePerk = PerkHelper.getPerkByType(stack, Perk.Type.DAMAGE);
if (damagePerk != null) {
this.damagePerkLevel.set(PerkHelper.getItemPerkLevel(damagePerk, pStack));
this.damagePerkLevel.set(PerkHelper.getItemPerkLevel(damagePerk, stack));
var damagePerkItem = PerkHelper.getPerkItem(damagePerk);
damagePerkItem.ifPresent(registryObject -> this.container.setItem(DAMAGE_PERK_SLOT, registryObject.get().getDefaultInstance()));
}