优化NBT#Weight & CustomWeight

This commit is contained in:
17146 2024-12-24 13:57:51 +08:00
parent 647754850a
commit c95c00457e
36 changed files with 79 additions and 67 deletions

View file

@ -260,8 +260,7 @@ public class ClientEventHandler {
gunSpread = Mth.lerp(0.14 * times, gunSpread, spread);
// 开火部分
double weight = stack.getOrCreateTag().getDouble("weight") + stack.getOrCreateTag().getDouble("CustomWeight");
double weight = GunsTool.getGunDoubleTag(stack, "Weight") + GunsTool.getGunDoubleTag(stack, "CustomWeight");
double speed = 1 - (0.04 * weight);
if (player.getPersistentData().getDouble("noRun") == 0 && player.isSprinting() && !zoom) {
@ -590,7 +589,7 @@ public class ClientEventHandler {
if (iVehicle instanceof SpeedboatEntity speedboat) {
float pitch = speedboat.getEntityData().get(HEAT) <= 60 ? 1 : (float) (1 - 0.011 * java.lang.Math.abs(60 - speedboat.getEntityData().get(HEAT)));
player.playSound(ModSounds.M_2_FIRE_1P.get(), 1f, pitch);
player.playSound(ModSounds.SHELL_CASING_50CAL.get(),0.3f, 1);
player.playSound(ModSounds.SHELL_CASING_50CAL.get(), 0.3f, 1);
}
}
@ -598,7 +597,8 @@ public class ClientEventHandler {
public static void handleWeaponBreathSway(TickEvent.RenderTickEvent event) {
Player player = Minecraft.getInstance().player;
if (player == null) return;
if (!player.getMainHandItem().is(ModTags.Items.GUN)) return;
ItemStack stack = player.getMainHandItem();
if (!stack.is(ModTags.Items.GUN)) return;
if (player.getVehicle() != null && player.getVehicle() instanceof ICannonEntity) return;
float pose;
@ -607,12 +607,12 @@ public class ClientEventHandler {
if (player.isCrouching() && player.getBbHeight() >= 1 && !PlayerEventHandler.isProne(player)) {
pose = 0.85f;
} else if (PlayerEventHandler.isProne(player)) {
pose = GunsTool.getAttachmentType(player.getMainHandItem(), GunsTool.AttachmentType.GRIP) == 3 ? 0 : 0.25f;
pose = GunsTool.getAttachmentType(stack, GunsTool.AttachmentType.GRIP) == 3 ? 0 : 0.25f;
} else {
pose = 1;
}
int stockType = GunsTool.getAttachmentType(player.getMainHandItem(), GunsTool.AttachmentType.STOCK);
int stockType = GunsTool.getAttachmentType(stack, GunsTool.AttachmentType.STOCK);
double sway = switch (stockType) {
case 1 -> 1;
@ -620,16 +620,15 @@ public class ClientEventHandler {
default -> 0.8;
};
double cusWeight = player.getMainHandItem().getOrCreateTag().getDouble("CustomWeight");
double customWeight = GunsTool.getGunDoubleTag(stack, "CustomWeight");
if (!player.getCapability(ModVariables.PLAYER_VARIABLES_CAPABILITY, null).orElse(new ModVariables.PlayerVariables()).breath &&
player.getCapability(ModVariables.PLAYER_VARIABLES_CAPABILITY, null).orElse(new ModVariables.PlayerVariables()).zoom) {
float newPitch = (float) (player.getXRot() - 0.01f * Mth.sin((float) (0.03 * player.tickCount)) * pose * Mth.nextDouble(RandomSource.create(), 0.1, 1) * times * sway * (1 - 0.03 * cusWeight));
float newPitch = (float) (player.getXRot() - 0.01f * Mth.sin((float) (0.03 * player.tickCount)) * pose * Mth.nextDouble(RandomSource.create(), 0.1, 1) * times * sway * (1 - 0.03 * customWeight));
player.setXRot(newPitch);
player.xRotO = player.getXRot();
float newYaw = (float) (player.getYRot() - 0.005f * Mth.cos((float) (0.025 * (player.tickCount + 2 * Math.PI))) * pose * Mth.nextDouble(RandomSource.create(), 0.05, 1.25) * times * sway * (1 - 0.03 * cusWeight));
float newYaw = (float) (player.getYRot() - 0.005f * Mth.cos((float) (0.025 * (player.tickCount + 2 * Math.PI))) * pose * Mth.nextDouble(RandomSource.create(), 0.05, 1.25) * times * sway * (1 - 0.03 * customWeight));
player.setYRot(newYaw);
player.yRotO = player.getYRot();
}
@ -955,12 +954,13 @@ public class ClientEventHandler {
private static void handleGunRecoil() {
Player player = Minecraft.getInstance().player;
if (player == null) return;
if (!player.getMainHandItem().is(ModTags.Items.GUN)) return;
ItemStack stack = player.getMainHandItem();
if (!stack.is(ModTags.Items.GUN)) return;
CompoundTag tag = player.getMainHandItem().getOrCreateTag();
CompoundTag tag = stack.getOrCreateTag();
float times = (float) Math.min(Minecraft.getInstance().getDeltaFrameTime(), 1.6);
int barrelType = GunsTool.getAttachmentType(player.getMainHandItem(), GunsTool.AttachmentType.BARREL);
int gripType = GunsTool.getAttachmentType(player.getMainHandItem(), GunsTool.AttachmentType.GRIP);
int barrelType = GunsTool.getAttachmentType(stack, GunsTool.AttachmentType.BARREL);
int gripType = GunsTool.getAttachmentType(stack, GunsTool.AttachmentType.GRIP);
double recoil = switch (barrelType) {
case 1 -> 1.5;
@ -980,24 +980,23 @@ public class ClientEventHandler {
default -> 2.0;
};
if (!player.getMainHandItem().is(ModTags.Items.CAN_CUSTOM_GUN)) {
if (!stack.is(ModTags.Items.CAN_CUSTOM_GUN)) {
recoil = 1.6;
gripRecoilX = 0.75;
gripRecoilY = 1.25;
}
double cusWeight = player.getMainHandItem().getOrCreateTag().getDouble("CustomWeight");
double customWeight = GunsTool.getGunDoubleTag(stack, "CustomWeight");
double rpm = 1;
if (player.getMainHandItem().is(ModItems.MINIGUN.get())) {
rpm = (double) player.getMainHandItem().getOrCreateTag().getInt("rpm") / 1800;
if (stack.is(ModItems.MINIGUN.get())) {
rpm = (double) stack.getOrCreateTag().getInt("rpm") / 1800;
}
float gunRecoilX = (float) tag.getDouble("recoil_x") * 60;
recoilHorizon = Mth.lerp(0.2 * times, recoilHorizon, 0) + recoilY;
recoilY = 0;
// 计算后坐力
@ -1005,7 +1004,7 @@ public class ClientEventHandler {
if (player.isShiftKeyDown() && player.getBbHeight() >= 1 && !PlayerEventHandler.isProne(player)) {
pose = 0.7f;
} else if (PlayerEventHandler.isProne(player)) {
if (GunsTool.getAttachmentType(player.getMainHandItem(), GunsTool.AttachmentType.GRIP) == 3) {
if (GunsTool.getAttachmentType(stack, GunsTool.AttachmentType.GRIP) == 3) {
pose = 0.1f;
} else {
pose = 0.5f;
@ -1013,8 +1012,7 @@ public class ClientEventHandler {
}
// 水平后座
float newYaw = player.getYRot() - (float) (0.6 * recoilHorizon * pose * times * (0.5 + fireSpread) * recoil * (1 - 0.06 * cusWeight) * gripRecoilX * rpm);
float newYaw = player.getYRot() - (float) (0.6 * recoilHorizon * pose * times * (0.5 + fireSpread) * recoil * (1 - 0.06 * customWeight) * gripRecoilX * rpm);
player.setYRot(newYaw);
player.yRotO = player.getYRot();
@ -1022,7 +1020,7 @@ public class ClientEventHandler {
// 竖直后座
if (0 < recoilTime && recoilTime < 0.5) {
float newPitch = (float) (player.getXRot() - 0.02f * gunRecoilX * times * recoil * (1 - 0.06 * cusWeight) * gripRecoilY * rpm);
float newPitch = (float) (player.getXRot() - 0.02f * gunRecoilX * times * recoil * (1 - 0.06 * customWeight) * gripRecoilY * rpm);
player.setXRot(newPitch);
player.xRotO = player.getXRot();
}
@ -1038,7 +1036,7 @@ public class ClientEventHandler {
}
if (0 < recoilTime && recoilTime < 2.5) {
float newPitch = player.getXRot() - (float) (1.5 * pose * gunRecoilX * (sinRes + Mth.clamp(0.5 - recoilTime, 0, 0.5)) * times * (0.5 + fireSpread) * recoil * (1 - 0.06 * cusWeight) * gripRecoilY * rpm);
float newPitch = player.getXRot() - (float) (1.5 * pose * gunRecoilX * (sinRes + Mth.clamp(0.5 - recoilTime, 0, 0.5)) * times * (0.5 + fireSpread) * recoil * (1 - 0.06 * customWeight) * gripRecoilY * rpm);
player.setXRot(newPitch);
player.xRotO = player.getXRot();
}
@ -1259,7 +1257,7 @@ public class ClientEventHandler {
private static void handleWeaponDraw(LivingEntity entity) {
float times = Minecraft.getInstance().getDeltaFrameTime();
ItemStack stack = entity.getMainHandItem();
double weight = stack.getOrCreateTag().getDouble("weight") + stack.getOrCreateTag().getDouble("CustomWeight");
double weight = GunsTool.getGunDoubleTag(stack, "Weight") + GunsTool.getGunDoubleTag(stack, "CustomWeight");
double speed = 3.2 - (0.13 * weight);
drawTime = Math.max(drawTime - Math.max(0.2 * speed * times * drawTime, 0.0008), 0);
}

View file

@ -143,7 +143,7 @@ public class PlayerEventHandler {
int sprintCost;
if (stack.is(ModTags.Items.GUN)) {
double weight = stack.getOrCreateTag().getDouble("weight") + stack.getOrCreateTag().getDouble("CustomWeight");
double weight = GunsTool.getGunDoubleTag(stack, "Weight") + GunsTool.getGunDoubleTag(stack, "CustomWeight");
sprintCost = (int) (5 + 0.2 * weight);
} else {
sprintCost = 5;

View file

@ -1,6 +1,9 @@
package com.atsuishio.superbwarfare.item.gun;
import com.atsuishio.superbwarfare.ModUtils;
import com.atsuishio.superbwarfare.client.tooltip.component.GunImageComponent;
import com.atsuishio.superbwarfare.init.ModPerks;
import com.atsuishio.superbwarfare.init.ModTags;
import com.atsuishio.superbwarfare.network.ModVariables;
import com.atsuishio.superbwarfare.perk.Perk;
import com.atsuishio.superbwarfare.perk.PerkHelper;
@ -8,9 +11,6 @@ import com.atsuishio.superbwarfare.tools.GunsTool;
import com.atsuishio.superbwarfare.tools.ItemNBTTool;
import com.google.common.collect.HashMultimap;
import com.google.common.collect.Multimap;
import com.atsuishio.superbwarfare.client.tooltip.component.GunImageComponent;
import com.atsuishio.superbwarfare.init.ModPerks;
import com.atsuishio.superbwarfare.init.ModTags;
import net.minecraft.core.BlockPos;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.resources.ResourceLocation;
@ -109,7 +109,7 @@ public abstract class GunItem extends Item {
map = HashMultimap.create(map);
map.put(Attributes.MOVEMENT_SPEED,
new AttributeModifier(uuid, ModUtils.ATTRIBUTE_MODIFIER,
-0.01f - 0.005f * (stack.getOrCreateTag().getDouble("weight") + stack.getOrCreateTag().getDouble("CustomWeight")),
-0.01f - 0.005f * (GunsTool.getGunDoubleTag(stack, "Weight") + GunsTool.getGunDoubleTag(stack, "CustomWeight")),
AttributeModifier.Operation.MULTIPLY_BASE));
}
return map;
@ -207,7 +207,7 @@ public abstract class GunItem extends Item {
double soundRadius = tag.getInt("Barrel") == 2 ? 0.6 : 1;
stack.getOrCreateTag().putDouble("CustomWeight", scopeWeight + barrelWeight + magazineWeight + stockWeight + gripWeight);
GunsTool.setGunDoubleTag(stack, "CustomWeight", scopeWeight + barrelWeight + magazineWeight + stockWeight + gripWeight);
stack.getOrCreateTag().putDouble("CustomSoundRadius", soundRadius);
}

View file

@ -55,8 +55,9 @@ public class GunsTool {
public static void initGun(Level level, ItemStack stack, String location) {
if (level.getServer() == null) return;
gunsData.get(location).forEach((k, v) -> {
if (k.equals("EmptyReloadTime") || k.equals("FireMode")) {
CompoundTag data = stack.getOrCreateTag().getCompound("GunData");
if (k.equals("EmptyReloadTime") || k.equals("FireMode") || k.equals("Weight")) {
CompoundTag tag = stack.getOrCreateTag();
CompoundTag data = tag.getCompound("GunData");
data.putDouble(k, v);
stack.addTagElement("GunData", data);
} else {
@ -68,8 +69,9 @@ public class GunsTool {
public static void initCreativeGun(ItemStack stack, String location) {
if (gunsData != null && gunsData.get(location) != null) {
gunsData.get(location).forEach((k, v) -> {
if (k.equals("EmptyReloadTime") || k.equals("FireMode")) {
CompoundTag data = stack.getOrCreateTag().getCompound("GunData");
if (k.equals("EmptyReloadTime") || k.equals("FireMode") || k.equals("Weight")) {
CompoundTag tag = stack.getOrCreateTag();
CompoundTag data = tag.getCompound("GunData");
data.putDouble(k, v);
stack.addTagElement("GunData", data);
} else {
@ -208,4 +210,16 @@ public class GunsTool {
return data.getInt(name);
}
public static void setGunDoubleTag(ItemStack stack, String name, double num) {
CompoundTag tag = stack.getOrCreateTag();
var data = tag.getCompound("GunData");
data.putDouble(name, num);
stack.addTagElement("GunData", data);
}
public static double getGunDoubleTag(ItemStack stack, String name) {
CompoundTag tag = stack.getOrCreateTag();
var data = tag.getCompound("GunData");
return data.getDouble(name);
}
}

View file

@ -7,7 +7,7 @@
"velocity": 17,
"mag": 25,
"projectile_amount": 12,
"weight": 7,
"Weight": 7,
"FireMode": 2,
"semi": 1,
"burst": 0,

View file

@ -7,7 +7,7 @@
"velocity": 15,
"mag": 2,
"projectile_amount": 12,
"weight": 1,
"Weight": 1,
"FireMode": 0,
"semi": 1,
"burst": 0,

View file

@ -9,7 +9,7 @@
"velocity": 44.5,
"mag": 30,
"projectile_amount": 1,
"weight": 4,
"Weight": 4,
"FireMode": 2,
"semi": 1,
"burst": 0,

View file

@ -9,7 +9,7 @@
"velocity": 36,
"mag": 30,
"projectile_amount": 1,
"weight": 5,
"Weight": 5,
"FireMode": 2,
"semi": 1,
"burst": 0,

View file

@ -3,6 +3,6 @@
"spread": 4,
"headshot": 2.5,
"damage": 48,
"weight": 3,
"Weight": 3,
"BypassesArmor": 0.25
}

View file

@ -8,7 +8,7 @@
"velocity": 40,
"mag": 55,
"projectile_amount": 1,
"weight": 6,
"Weight": 6,
"FireMode": 2,
"semi": 0,
"burst": 0,

View file

@ -7,7 +7,7 @@
"velocity": 17,
"mag": 17,
"projectile_amount": 1,
"weight": 1,
"Weight": 1,
"FireMode": 0,
"semi": 1,
"burst": 0,

View file

@ -7,7 +7,7 @@
"velocity": 17,
"mag": 17,
"projectile_amount": 1,
"weight": 1,
"Weight": 1,
"FireMode": 2,
"semi": 1,
"burst": 0,

View file

@ -9,7 +9,7 @@
"velocity": 44,
"mag": 30,
"projectile_amount": 1,
"weight": 4,
"Weight": 4,
"FireMode": 2,
"semi": 1,
"burst": 0,

View file

@ -7,7 +7,7 @@
"velocity": 38,
"mag": 1,
"projectile_amount": 1,
"weight": 5,
"Weight": 5,
"FireMode": 0,
"semi": 1,
"burst": 0,

View file

@ -3,7 +3,7 @@
"spread": 5,
"zoomSpread": 5,
"mag": 1,
"weight": 10,
"Weight": 10,
"EmptyReloadTime": 78,
"damage": 300,
"ExplosionDamage": 100,

View file

@ -8,7 +8,7 @@
"projectile_amount": 1,
"mag": 5,
"bolt_action_time": 22,
"weight": 5,
"Weight": 5,
"FireMode": 0,
"semi": 1,
"burst": 0,

View file

@ -7,7 +7,7 @@
"velocity": 15,
"mag": 7,
"projectile_amount": 1,
"weight": 2,
"Weight": 2,
"FireMode": 0,
"semi": 1,
"burst": 0,

View file

@ -9,7 +9,7 @@
"velocity": 44,
"mag": 30,
"projectile_amount": 1,
"weight": 4,
"Weight": 4,
"FireMode": 2,
"semi": 1,
"burst": 0,

View file

@ -7,7 +7,7 @@
"velocity": 43,
"mag": 100,
"projectile_amount": 1,
"weight": 8,
"Weight": 8,
"FireMode": 2,
"semi": 0,
"burst": 0,

View file

@ -7,6 +7,6 @@
"ExplosionRadius": 5,
"velocity": 3.75,
"mag": 1,
"weight": 4,
"Weight": 4,
"EmptyReloadTime": 64
}

View file

@ -8,7 +8,7 @@
"mag": 8,
"force_stop_reloading": 1,
"projectile_amount": 12,
"weight": 4,
"Weight": 4,
"FireMode": 0,
"semi": 1,
"burst": 0,

View file

@ -11,7 +11,7 @@
"projectile_amount": 1,
"mag": 5,
"bolt_action_time": 18,
"weight": 7,
"Weight": 7,
"FireMode": 0,
"semi": 1,
"burst": 0,

View file

@ -8,7 +8,7 @@
"mag": 8,
"force_stop_reloading": 1,
"projectile_amount": 1,
"weight": 3,
"Weight": 3,
"FireMode": 0,
"semi": 1,
"burst": 0,

View file

@ -5,7 +5,7 @@
"damage": 8,
"headshot": 2,
"velocity": 46,
"weight": 10,
"Weight": 10,
"FireMode": 2,
"projectile_amount": 1,
"BypassesArmor": 0.3,

View file

@ -9,7 +9,7 @@
"velocity": 42.5,
"mag": 20,
"projectile_amount": 1,
"weight": 5,
"Weight": 5,
"FireMode": 0,
"semi": 1,
"burst": 0,

View file

@ -9,7 +9,7 @@
"projectile_amount": 1,
"mag": 5,
"bolt_action_time": 22,
"weight": 5,
"Weight": 5,
"FireMode": 0,
"semi": 1,
"burst": 0,

View file

@ -9,7 +9,7 @@
"projectile_amount": 1,
"mag": 3,
"bolt_action_time": 37,
"weight": 10,
"Weight": 10,
"FireMode": 0,
"semi": 1,
"burst": 0,

View file

@ -9,7 +9,7 @@
"velocity": 46,
"mag": 30,
"projectile_amount": 1,
"weight": 4,
"Weight": 4,
"FireMode": 2,
"semi": 1,
"burst": 0,

View file

@ -7,6 +7,6 @@
"ExplosionRadius": 10,
"velocity": 4,
"mag": 1,
"weight": 7,
"Weight": 7,
"EmptyReloadTime": 103
}

View file

@ -7,7 +7,7 @@
"velocity": 38,
"mag": 75,
"projectile_amount": 1,
"weight": 6,
"Weight": 6,
"FireMode": 2,
"semi": 1,
"burst": 0,

View file

@ -9,7 +9,7 @@
"headshot": 3,
"velocity": 70,
"bolt_action_time": 22,
"weight": 7,
"Weight": 7,
"FireMode": 0,
"semi": 1,
"burst": 0,

View file

@ -7,7 +7,7 @@
"velocity": 36,
"mag": 20,
"projectile_amount": 1,
"weight": 4,
"Weight": 4,
"FireMode": 0,
"semi": 1,
"burst": 0,

View file

@ -8,7 +8,7 @@
"velocity": 42,
"mag": 10,
"projectile_amount": 1,
"weight": 5,
"Weight": 5,
"FireMode": 0,
"semi": 1,
"burst": 0,

View file

@ -5,6 +5,6 @@
"damage": 5,
"velocity": 3,
"mag": 1,
"weight": 1,
"Weight": 1,
"EmptyReloadTime": 58
}

View file

@ -7,7 +7,7 @@
"velocity": 24,
"mag": 6,
"projectile_amount": 1,
"weight": 2,
"Weight": 2,
"FireMode": 0,
"semi": 1,
"burst": 0,

View file

@ -7,7 +7,7 @@
"velocity": 16,
"mag": 13,
"projectile_amount": 1,
"weight": 3,
"Weight": 3,
"FireMode": 2,
"semi": 1,
"burst": 1,