优化NBT#Magazine

This commit is contained in:
17146 2024-12-24 16:06:43 +08:00
parent 2668a19e72
commit 8d2e5f7ed6
36 changed files with 67 additions and 65 deletions

View file

@ -422,13 +422,13 @@ public class GunEventHandler {
}
} else {
if (stack.is(ModTags.Items.USE_SHOTGUN_AMMO)) {
stack.getOrCreateTag().putInt("ammo", stack.getOrCreateTag().getInt("mag") + stack.getOrCreateTag().getInt("customMag") + (stack.is(ModTags.Items.EXTRA_ONE_AMMO) ? 1 : 0));
stack.getOrCreateTag().putInt("ammo", GunsTool.getGunIntTag(stack, "Magazine", 0) + stack.getOrCreateTag().getInt("customMag") + (stack.is(ModTags.Items.EXTRA_ONE_AMMO) ? 1 : 0));
} else if (stack.is(ModTags.Items.USE_SNIPER_AMMO)) {
stack.getOrCreateTag().putInt("ammo", stack.getOrCreateTag().getInt("mag") + stack.getOrCreateTag().getInt("customMag") + (stack.is(ModTags.Items.EXTRA_ONE_AMMO) ? 1 : 0));
stack.getOrCreateTag().putInt("ammo", GunsTool.getGunIntTag(stack, "Magazine", 0) + stack.getOrCreateTag().getInt("customMag") + (stack.is(ModTags.Items.EXTRA_ONE_AMMO) ? 1 : 0));
} else if (stack.is(ModTags.Items.USE_HANDGUN_AMMO)) {
stack.getOrCreateTag().putInt("ammo", stack.getOrCreateTag().getInt("mag") + stack.getOrCreateTag().getInt("customMag") + (stack.is(ModTags.Items.EXTRA_ONE_AMMO) ? 1 : 0));
stack.getOrCreateTag().putInt("ammo", GunsTool.getGunIntTag(stack, "Magazine", 0) + stack.getOrCreateTag().getInt("customMag") + (stack.is(ModTags.Items.EXTRA_ONE_AMMO) ? 1 : 0));
} else if (stack.is(ModTags.Items.USE_RIFLE_AMMO)) {
stack.getOrCreateTag().putInt("ammo", stack.getOrCreateTag().getInt("mag") + stack.getOrCreateTag().getInt("customMag") + (stack.is(ModTags.Items.EXTRA_ONE_AMMO) ? 1 : 0));
stack.getOrCreateTag().putInt("ammo", GunsTool.getGunIntTag(stack, "Magazine", 0) + stack.getOrCreateTag().getInt("customMag") + (stack.is(ModTags.Items.EXTRA_ONE_AMMO) ? 1 : 0));
}
}
@ -471,7 +471,7 @@ public class GunEventHandler {
player.getInventory().clearOrCountMatchingItems(p -> p.getItem() == ModItems.JAVELIN_MISSILE.get(), 1, player.inventoryMenu.getCraftSlots());
}
} else {
stack.getOrCreateTag().putInt("ammo", stack.getOrCreateTag().getInt("mag") + stack.getOrCreateTag().getInt("customMag"));
stack.getOrCreateTag().putInt("ammo", GunsTool.getGunIntTag(stack, "Magazine", 0) + stack.getOrCreateTag().getInt("customMag"));
}
stack.getOrCreateTag().putBoolean("is_normal_reloading", false);
@ -610,7 +610,7 @@ public class GunEventHandler {
&& tag.getInt("reload_stage") == 2
&& tag.getInt("iterative") == 0
&& !tag.getBoolean("stop")
&& tag.getInt("ammo") < (int) tag.getDouble("mag") + tag.getInt("customMag")) {
&& tag.getInt("ammo") < GunsTool.getGunIntTag(stack, "Magazine", 0) + tag.getInt("customMag")) {
playGunLoopReloadSounds(player);
tag.putDouble("iterative", (int) tag.getDouble("iterative_time"));
@ -639,7 +639,7 @@ public class GunEventHandler {
// 二阶段结束
if (tag.getInt("iterative") == 1) {
// 装满结束
if (tag.getInt("ammo") >= (int) tag.getDouble("mag") + tag.getInt("customMag")) {
if (tag.getInt("ammo") >= GunsTool.getGunIntTag(stack, "Magazine", 0) + tag.getInt("customMag")) {
tag.putInt("reload_stage", 3);
}

View file

@ -615,7 +615,7 @@ public class LivingEventHandler {
float rate = level * 0.1f + (stack.is(ModTags.Items.SMG) || stack.is(ModTags.Items.RIFLE) ? 0.07f : 0f);
player.getCapability(ModVariables.PLAYER_VARIABLES_CAPABILITY, null).ifPresent(capability -> {
int mag = stack.getOrCreateTag().getInt("mag") + stack.getOrCreateTag().getInt("customMag");
int mag = GunsTool.getGunIntTag(stack, "Magazine", 0) + stack.getOrCreateTag().getInt("customMag");
int ammo = stack.getOrCreateTag().getInt("ammo");
int ammoReload = (int) Math.min(mag, mag * rate);
int ammoNeed = Math.min(mag - ammo, ammoReload);

View file

@ -334,7 +334,7 @@ public class PlayerEventHandler {
player.getInventory().clearOrCountMatchingItems(p -> p.getItem() == ModItems.JAVELIN_MISSILE.get(), 1, player.inventoryMenu.getCraftSlots());
}
} else {
stack.getOrCreateTag().putInt("ammo", stack.getOrCreateTag().getInt("mag") + stack.getOrCreateTag().getInt("customMag"));
stack.getOrCreateTag().putInt("ammo", GunsTool.getGunIntTag(stack, "Magazine", 0) + stack.getOrCreateTag().getInt("customMag"));
}
stack.getOrCreateTag().putBoolean("HoldOpen", false);
}

View file

@ -49,44 +49,44 @@ public abstract class GunItem extends Item {
}
@Override
public void inventoryTick(ItemStack itemstack, Level level, Entity entity, int slot, boolean selected) {
public void inventoryTick(ItemStack stack, Level level, Entity entity, int slot, boolean selected) {
if (entity instanceof LivingEntity) {
if (!itemstack.is(ModTags.Items.GUN)) {
if (!stack.is(ModTags.Items.GUN)) {
return;
}
if (!ItemNBTTool.getBoolean(itemstack, "init", false)) {
GunsTool.initGun(level, itemstack, this.getDescriptionId().substring(this.getDescriptionId().lastIndexOf('.') + 1));
GunsTool.generateAndSetUUID(itemstack);
ItemNBTTool.setBoolean(itemstack, "init", true);
if (!ItemNBTTool.getBoolean(stack, "init", false)) {
GunsTool.initGun(level, stack, this.getDescriptionId().substring(this.getDescriptionId().lastIndexOf('.') + 1));
GunsTool.generateAndSetUUID(stack);
ItemNBTTool.setBoolean(stack, "init", true);
}
if (itemstack.getOrCreateTag().getBoolean("draw")) {
itemstack.getOrCreateTag().putBoolean("draw", false);
if (stack.getOrCreateTag().getBoolean("draw")) {
stack.getOrCreateTag().putBoolean("draw", false);
}
handleGunPerks(itemstack);
handleGunAttachment(itemstack);
handleGunPerks(stack);
handleGunAttachment(stack);
if ((itemstack.is(ModTags.Items.EXTRA_ONE_AMMO) && itemstack.getOrCreateTag().getInt("ammo") > itemstack.getOrCreateTag().getInt("mag") + itemstack.getOrCreateTag().getInt("customMag") + 1)
|| (!itemstack.is(ModTags.Items.EXTRA_ONE_AMMO) && itemstack.getOrCreateTag().getInt("ammo") > itemstack.getOrCreateTag().getInt("mag") + itemstack.getOrCreateTag().getInt("customMag"))
if ((stack.is(ModTags.Items.EXTRA_ONE_AMMO) && stack.getOrCreateTag().getInt("ammo") > GunsTool.getGunIntTag(stack, "Magazine", 0) + stack.getOrCreateTag().getInt("customMag") + 1)
|| (!stack.is(ModTags.Items.EXTRA_ONE_AMMO) && stack.getOrCreateTag().getInt("ammo") > GunsTool.getGunIntTag(stack, "Magazine", 0) + stack.getOrCreateTag().getInt("customMag"))
) {
int count = itemstack.getOrCreateTag().getInt("ammo") - itemstack.getOrCreateTag().getInt("mag") + itemstack.getOrCreateTag().getInt("customMag") - (itemstack.is(ModTags.Items.EXTRA_ONE_AMMO) ? 1 : 0);
int count = stack.getOrCreateTag().getInt("ammo") - GunsTool.getGunIntTag(stack, "Magazine", 0) + stack.getOrCreateTag().getInt("customMag") - (stack.is(ModTags.Items.EXTRA_ONE_AMMO) ? 1 : 0);
entity.getCapability(ModVariables.PLAYER_VARIABLES_CAPABILITY, null).ifPresent(capability -> {
if (itemstack.is(ModTags.Items.USE_SHOTGUN_AMMO)) {
if (stack.is(ModTags.Items.USE_SHOTGUN_AMMO)) {
capability.shotgunAmmo = entity.getCapability(ModVariables.PLAYER_VARIABLES_CAPABILITY, null).orElse(new ModVariables.PlayerVariables()).shotgunAmmo + count;
} else if (itemstack.is(ModTags.Items.USE_SNIPER_AMMO)) {
} else if (stack.is(ModTags.Items.USE_SNIPER_AMMO)) {
capability.sniperAmmo = entity.getCapability(ModVariables.PLAYER_VARIABLES_CAPABILITY, null).orElse(new ModVariables.PlayerVariables()).sniperAmmo + count;
} else if (itemstack.is(ModTags.Items.USE_HANDGUN_AMMO)) {
} else if (stack.is(ModTags.Items.USE_HANDGUN_AMMO)) {
capability.handgunAmmo = entity.getCapability(ModVariables.PLAYER_VARIABLES_CAPABILITY, null).orElse(new ModVariables.PlayerVariables()).handgunAmmo + count;
} else if (itemstack.is(ModTags.Items.USE_RIFLE_AMMO)) {
} else if (stack.is(ModTags.Items.USE_RIFLE_AMMO)) {
capability.rifleAmmo = entity.getCapability(ModVariables.PLAYER_VARIABLES_CAPABILITY, null).orElse(new ModVariables.PlayerVariables()).rifleAmmo + count;
}
capability.syncPlayerVariables(entity);
});
itemstack.getOrCreateTag().putInt("ammo", itemstack.getOrCreateTag().getInt("mag") + itemstack.getOrCreateTag().getInt("customMag") + (itemstack.is(ModTags.Items.EXTRA_ONE_AMMO) ? 1 : 0));
stack.getOrCreateTag().putInt("ammo", GunsTool.getGunIntTag(stack, "Magazine", 0) + stack.getOrCreateTag().getInt("customMag") + (stack.is(ModTags.Items.EXTRA_ONE_AMMO) ? 1 : 0));
}
}
}
@ -165,7 +165,7 @@ public abstract class GunItem extends Item {
GunsTool.setPerkIntTag(stack, "FourthTimesCharmTick", 0);
GunsTool.setPerkIntTag(stack, "FourthTimesCharmCount", 0);
int mag = stack.getOrCreateTag().getInt("mag") + stack.getOrCreateTag().getInt("customMag");
int mag = GunsTool.getGunIntTag(stack, "Magazine", 0) + stack.getOrCreateTag().getInt("customMag");
stack.getOrCreateTag().putInt("ammo", Math.min(mag, stack.getOrCreateTag().getInt("ammo") + 2));
}
}

View file

@ -96,24 +96,26 @@ public class ReloadMessage {
}
if (canReload || clipLoad) {
int magazine = GunsTool.getGunIntTag(stack, "Magazine", 0);
if (stack.is(ModTags.Items.OPEN_BOLT)) {
if (stack.is(ModTags.Items.EXTRA_ONE_AMMO)) {
if (tag.getInt("ammo") < tag.getDouble("mag") + tag.getInt("customMag") + 1) {
if (tag.getInt("ammo") < magazine + tag.getInt("customMag") + 1) {
tag.putBoolean("start_reload", true);
}
} else {
if (tag.getInt("ammo") < tag.getDouble("mag") + tag.getInt("customMag")) {
if (tag.getInt("ammo") < magazine + tag.getInt("customMag")) {
tag.putBoolean("start_reload", true);
}
}
} else if (tag.getInt("ammo") < tag.getDouble("mag") + tag.getInt("customMag")) {
} else if (tag.getInt("ammo") < magazine + tag.getInt("customMag")) {
tag.putBoolean("start_reload", true);
}
return;
}
if (canSingleReload) {
if (tag.getInt("ammo") < tag.getDouble("mag") + tag.getInt("customMag")) {
if (tag.getInt("ammo") < GunsTool.getGunIntTag(stack, "Magazine", 0) + tag.getInt("customMag")) {
tag.putBoolean("start_single_reload", true);
}
}

View file

@ -56,7 +56,7 @@ public class GunsTool {
// TODO 临时使用移植完毕后删除
private static final Set<String> STRING_SET = Set.of("EmptyReloadTime", "FireMode", "Weight", "SoundRadius", "BurstSize", "ProjectileAmount",
"Spread", "NormalReloadTime", "Headshot", "Semi", "Burst", "Auto", "RecoilX", "RecoilY", "Velocity", "Damage", "BypassesArmor",
"RPM");
"RPM", "Magazine");
public static void initGun(Level level, ItemStack stack, String location) {
if (level.getServer() == null) return;
@ -84,7 +84,7 @@ public class GunsTool {
stack.getOrCreateTag().putDouble(k, v);
}
});
stack.getOrCreateTag().putInt("ammo", stack.getOrCreateTag().getInt("mag") + stack.getOrCreateTag().getInt("customMag"));
stack.getOrCreateTag().putInt("ammo", GunsTool.getGunIntTag(stack, "Magazine", 0) + stack.getOrCreateTag().getInt("customMag"));
}
}
@ -115,7 +115,7 @@ public class GunsTool {
public static void reload(Player player, ItemStack stack, GunInfo.Type type, boolean extraOne) {
CompoundTag tag = stack.getOrCreateTag();
int mag = tag.getInt("mag") + tag.getInt("customMag");
int mag = GunsTool.getGunIntTag(stack, "Magazine", 0) + tag.getInt("customMag");
int ammo = tag.getInt("ammo");
int ammoToAdd = mag - ammo + (extraOne ? 1 : 0);

View file

@ -5,7 +5,7 @@
"Damage": 2.5,
"Headshot": 1.5,
"Velocity": 17,
"mag": 25,
"Magazine": 25,
"ProjectileAmount": 12,
"Weight": 7,
"FireMode": 2,

View file

@ -5,7 +5,7 @@
"Damage": 5,
"Headshot": 1.5,
"Velocity": 15,
"mag": 2,
"Magazine": 2,
"ProjectileAmount": 12,
"Weight": 1,
"FireMode": 0,

View file

@ -7,7 +7,7 @@
"Damage": 7.7,
"Headshot": 2,
"Velocity": 44.5,
"mag": 30,
"Magazine": 30,
"Weight": 4,
"FireMode": 2,
"Semi": 1,

View file

@ -7,7 +7,7 @@
"Damage": 8.5,
"Headshot": 2,
"Velocity": 36,
"mag": 30,
"Magazine": 30,
"Weight": 5,
"FireMode": 2,
"Semi": 1,

View file

@ -6,7 +6,7 @@
"Damage": 9.5,
"Headshot": 2,
"Velocity": 40,
"mag": 55,
"Magazine": 55,
"Weight": 6,
"FireMode": 2,
"Auto": 1,

View file

@ -5,7 +5,7 @@
"Damage": 5.5,
"Headshot": 1.5,
"Velocity": 17,
"mag": 17,
"Magazine": 17,
"Weight": 1,
"FireMode": 0,
"Semi": 1,

View file

@ -5,7 +5,7 @@
"Damage": 5.5,
"Headshot": 1.5,
"Velocity": 17,
"mag": 17,
"Magazine": 17,
"Weight": 1,
"FireMode": 2,
"Semi": 1,

View file

@ -7,7 +7,7 @@
"Damage": 7.5,
"Headshot": 2,
"Velocity": 44,
"mag": 30,
"Magazine": 30,
"Weight": 4,
"FireMode": 2,
"Semi": 1,

View file

@ -5,7 +5,7 @@
"Damage": 65,
"Headshot": 3,
"Velocity": 38,
"mag": 1,
"Magazine": 1,
"Weight": 5,
"FireMode": 0,
"Semi": 1,

View file

@ -2,7 +2,7 @@
"CustomZoom": 1.75,
"Spread": 5,
"zoomSpread": 5,
"mag": 1,
"Magazine": 1,
"Weight": 10,
"EmptyReloadTime": 78,
"Damage": 300,

View file

@ -5,7 +5,7 @@
"Damage": 35,
"Headshot": 3,
"Velocity": 37.75,
"mag": 5,
"Magazine": 5,
"bolt_action_time": 22,
"Weight": 5,
"FireMode": 0,

View file

@ -5,7 +5,7 @@
"Damage": 9.5,
"Headshot": 1.5,
"Velocity": 15,
"mag": 7,
"Magazine": 7,
"Weight": 2,
"FireMode": 0,
"Semi": 1,

View file

@ -7,7 +7,7 @@
"Damage": 7,
"Headshot": 2,
"Velocity": 44,
"mag": 30,
"Magazine": 30,
"Weight": 4,
"FireMode": 2,
"Semi": 1,

View file

@ -5,7 +5,7 @@
"Damage": 9,
"Headshot": 2,
"Velocity": 43,
"mag": 100,
"Magazine": 100,
"Weight": 8,
"FireMode": 2,
"Auto": 1,

View file

@ -6,7 +6,7 @@
"ExplosionDamage": 80,
"ExplosionRadius": 5,
"Velocity": 3.75,
"mag": 1,
"Magazine": 1,
"Weight": 4,
"EmptyReloadTime": 64
}

View file

@ -5,7 +5,7 @@
"Damage": 3,
"Headshot": 1.5,
"Velocity": 17,
"mag": 8,
"Magazine": 8,
"force_stop_reloading": 1,
"ProjectileAmount": 12,
"Weight": 4,

View file

@ -8,7 +8,7 @@
"Damage": 38,
"Headshot": 3,
"Velocity": 47.2,
"mag": 5,
"Magazine": 5,
"bolt_action_time": 18,
"Weight": 7,
"FireMode": 0,

View file

@ -5,7 +5,7 @@
"Damage": 16,
"Headshot": 2.5,
"Velocity": 38,
"mag": 8,
"Magazine": 8,
"force_stop_reloading": 1,
"Weight": 3,
"FireMode": 0,

View file

@ -7,7 +7,7 @@
"Damage": 12,
"Headshot": 2.5,
"Velocity": 42.5,
"mag": 20,
"Magazine": 20,
"Weight": 5,
"FireMode": 0,
"Semi": 1,

View file

@ -6,7 +6,7 @@
"Damage": 33,
"Headshot": 3,
"Velocity": 42,
"mag": 5,
"Magazine": 5,
"bolt_action_time": 22,
"Weight": 5,
"FireMode": 0,

View file

@ -6,7 +6,7 @@
"Damage": 140,
"Headshot": 3,
"Velocity": 36,
"mag": 3,
"Magazine": 3,
"bolt_action_time": 37,
"Weight": 10,
"FireMode": 0,

View file

@ -7,7 +7,7 @@
"Damage": 8.25,
"Headshot": 2,
"Velocity": 46,
"mag": 30,
"Magazine": 30,
"Weight": 4,
"FireMode": 2,
"Semi": 1,

View file

@ -6,7 +6,7 @@
"ExplosionDamage": 150,
"ExplosionRadius": 10,
"Velocity": 4,
"mag": 1,
"Magazine": 1,
"Weight": 7,
"EmptyReloadTime": 103
}

View file

@ -5,7 +5,7 @@
"Damage": 8.75,
"Headshot": 2,
"Velocity": 38,
"mag": 75,
"Magazine": 75,
"Weight": 6,
"FireMode": 2,
"Semi": 1,

View file

@ -4,7 +4,7 @@
"RecoilX": 0.007,
"RecoilY": 0.018,
"Damage": 35,
"mag": 5,
"Magazine": 5,
"Headshot": 3,
"Velocity": 70,
"bolt_action_time": 22,

View file

@ -5,7 +5,7 @@
"Damage": 9.5,
"Headshot": 2,
"Velocity": 36,
"mag": 20,
"Magazine": 20,
"Weight": 4,
"FireMode": 0,
"Semi": 1,

View file

@ -6,7 +6,7 @@
"Damage": 18,
"Headshot": 2,
"Velocity": 42,
"mag": 10,
"Magazine": 10,
"Weight": 5,
"FireMode": 0,
"Semi": 1,

View file

@ -4,7 +4,7 @@
"RecoilY": 0.002,
"Damage": 5,
"Velocity": 3,
"mag": 1,
"Magazine": 1,
"Weight": 1,
"EmptyReloadTime": 58
}

View file

@ -5,7 +5,7 @@
"Damage": 19,
"Headshot": 2,
"Velocity": 24,
"mag": 6,
"Magazine": 6,
"Weight": 2,
"FireMode": 0,
"Semi": 1,

View file

@ -5,7 +5,7 @@
"Damage": 6,
"Headshot": 1.5,
"Velocity": 16,
"mag": 13,
"Magazine": 13,
"Weight": 3,
"FireMode": 2,
"Semi": 1,