尝试修复自动换弹问题

This commit is contained in:
17146 2024-10-19 01:25:34 +08:00
parent 3119dc089c
commit 58e5ab348a
3 changed files with 33 additions and 45 deletions

View file

@ -83,7 +83,7 @@ public class GunEventHandler {
var tag = stack.getOrCreateTag(); var tag = stack.getOrCreateTag();
if ((player.getCapability(ModVariables.PLAYER_VARIABLES_CAPABILITY, null).orElse(new ModVariables.PlayerVariables())).holdFire|| (player.getCapability(ModVariables.PLAYER_VARIABLES_CAPABILITY, null).orElse(new ModVariables.PlayerVariables())).zoom) { if ((player.getCapability(ModVariables.PLAYER_VARIABLES_CAPABILITY, null).orElse(new ModVariables.PlayerVariables())).holdFire || (player.getCapability(ModVariables.PLAYER_VARIABLES_CAPABILITY, null).orElse(new ModVariables.PlayerVariables())).zoom) {
if (tag.getDouble("minigun_rotation") < 10) { if (tag.getDouble("minigun_rotation") < 10) {
tag.putDouble("minigun_rotation", (tag.getDouble("minigun_rotation") + 1)); tag.putDouble("minigun_rotation", (tag.getDouble("minigun_rotation") + 1));
} }
@ -369,19 +369,19 @@ public class GunEventHandler {
if (count == 0) { if (count == 0) {
if (stack.is(ModTags.Items.USE_SHOTGUN_AMMO)) { if (stack.is(ModTags.Items.USE_SHOTGUN_AMMO)) {
if (stack.is(ModTags.Items.EXTRA_ONE_AMMO)) { if (stack.is(ModTags.Items.EXTRA_ONE_AMMO)) {
GunsTool.reload(player, GunInfo.Type.SHOTGUN, true); GunsTool.reload(player, stack, GunInfo.Type.SHOTGUN, true);
} else { } else {
GunsTool.reload(player, GunInfo.Type.SHOTGUN); GunsTool.reload(player, stack, GunInfo.Type.SHOTGUN);
} }
} else if (stack.is(ModTags.Items.USE_SNIPER_AMMO)) { } else if (stack.is(ModTags.Items.USE_SNIPER_AMMO)) {
GunsTool.reload(player, GunInfo.Type.SNIPER, true); GunsTool.reload(player, stack, GunInfo.Type.SNIPER, true);
} else if (stack.is(ModTags.Items.USE_HANDGUN_AMMO)) { } else if (stack.is(ModTags.Items.USE_HANDGUN_AMMO)) {
GunsTool.reload(player, GunInfo.Type.HANDGUN, true); GunsTool.reload(player, stack, GunInfo.Type.HANDGUN, true);
} else if (stack.is(ModTags.Items.USE_RIFLE_AMMO)) { } else if (stack.is(ModTags.Items.USE_RIFLE_AMMO)) {
if (stack.is(ModTags.Items.EXTRA_ONE_AMMO)) { if (stack.is(ModTags.Items.EXTRA_ONE_AMMO)) {
GunsTool.reload(player, GunInfo.Type.RIFLE, true); GunsTool.reload(player, stack, GunInfo.Type.RIFLE, true);
} else { } else {
GunsTool.reload(player, GunInfo.Type.RIFLE); GunsTool.reload(player, stack, GunInfo.Type.RIFLE);
} }
} }
} else { } else {
@ -396,7 +396,6 @@ public class GunEventHandler {
} }
} }
stack.getOrCreateTag().putBoolean("is_normal_reloading", false); stack.getOrCreateTag().putBoolean("is_normal_reloading", false);
stack.getOrCreateTag().putBoolean("is_empty_reloading", false); stack.getOrCreateTag().putBoolean("is_empty_reloading", false);
@ -415,13 +414,13 @@ public class GunEventHandler {
if (count == 0) { if (count == 0) {
if (stack.is(ModTags.Items.USE_SHOTGUN_AMMO)) { if (stack.is(ModTags.Items.USE_SHOTGUN_AMMO)) {
GunsTool.reload(player, GunInfo.Type.SHOTGUN); GunsTool.reload(player, stack, GunInfo.Type.SHOTGUN);
} else if (stack.is(ModTags.Items.USE_SNIPER_AMMO)) { } else if (stack.is(ModTags.Items.USE_SNIPER_AMMO)) {
GunsTool.reload(player, GunInfo.Type.SNIPER); GunsTool.reload(player, stack, GunInfo.Type.SNIPER);
} else if (stack.is(ModTags.Items.USE_HANDGUN_AMMO)) { } else if (stack.is(ModTags.Items.USE_HANDGUN_AMMO)) {
GunsTool.reload(player, GunInfo.Type.HANDGUN); GunsTool.reload(player, stack, GunInfo.Type.HANDGUN);
} else if (stack.is(ModTags.Items.USE_RIFLE_AMMO)) { } else if (stack.is(ModTags.Items.USE_RIFLE_AMMO)) {
GunsTool.reload(player, GunInfo.Type.RIFLE); GunsTool.reload(player, stack, GunInfo.Type.RIFLE);
} else if (stack.getItem() == ModItems.TASER.get()) { } else if (stack.getItem() == ModItems.TASER.get()) {
stack.getOrCreateTag().putInt("ammo", 1); stack.getOrCreateTag().putInt("ammo", 1);
player.getInventory().clearOrCountMatchingItems(p -> p.getItem() == ModItems.TASER_ELECTRODE.get(), 1, player.inventoryMenu.getCraftSlots()); player.getInventory().clearOrCountMatchingItems(p -> p.getItem() == ModItems.TASER_ELECTRODE.get(), 1, player.inventoryMenu.getCraftSlots());
@ -439,8 +438,6 @@ public class GunEventHandler {
stack.getOrCreateTag().putInt("ammo", stack.getOrCreateTag().getInt("mag") + stack.getOrCreateTag().getInt("customMag")); stack.getOrCreateTag().putInt("ammo", stack.getOrCreateTag().getInt("mag") + stack.getOrCreateTag().getInt("customMag"));
} }
stack.getOrCreateTag().putBoolean("is_normal_reloading", false); stack.getOrCreateTag().putBoolean("is_normal_reloading", false);
stack.getOrCreateTag().putBoolean("is_empty_reloading", false); stack.getOrCreateTag().putBoolean("is_empty_reloading", false);
@ -537,7 +534,6 @@ public class GunEventHandler {
// 一阶段结束检查备弹如果有则二阶段启动无则直接跳到三阶段 // 一阶段结束检查备弹如果有则二阶段启动无则直接跳到三阶段
if ((tag.getDouble("prepare") == 1 || tag.getDouble("prepare_load") == 1)) { if ((tag.getDouble("prepare") == 1 || tag.getDouble("prepare_load") == 1)) {
int count = 0; int count = 0;
for (var inv : player.getInventory().items) { for (var inv : player.getInventory().items) {
if (inv.is(ModItems.CREATIVE_AMMO_BOX.get())) { if (inv.is(ModItems.CREATIVE_AMMO_BOX.get())) {
@ -603,14 +599,12 @@ public class GunEventHandler {
// 二阶段结束 // 二阶段结束
if (tag.getInt("iterative") == 1) { if (tag.getInt("iterative") == 1) {
// 装满结束 // 装满结束
if (tag.getInt("ammo") >= (int) tag.getDouble("mag") + tag.getInt("customMag")) { if (tag.getInt("ammo") >= (int) tag.getDouble("mag") + tag.getInt("customMag")) {
tag.putInt("reload_stage", 3); tag.putInt("reload_stage", 3);
} }
// 备弹耗尽结束 // 备弹耗尽结束
int count = 0; int count = 0;
for (var inv : player.getInventory().items) { for (var inv : player.getInventory().items) {
if (inv.is(ModItems.CREATIVE_AMMO_BOX.get())) { if (inv.is(ModItems.CREATIVE_AMMO_BOX.get())) {
@ -631,7 +625,6 @@ public class GunEventHandler {
} }
} }
// 强制结束 // 强制结束
if (tag.getBoolean("stop")) { if (tag.getBoolean("stop")) {
tag.putInt("reload_stage", 3); tag.putInt("reload_stage", 3);
@ -696,7 +689,6 @@ public class GunEventHandler {
}); });
} }
} }
} }
public static void playGunPrepareReloadSounds(Player player) { public static void playGunPrepareReloadSounds(Player player) {
@ -790,7 +782,7 @@ public class GunEventHandler {
private static void handleSentinelCharge(Player player) { private static void handleSentinelCharge(Player player) {
ItemStack stack = player.getMainHandItem(); ItemStack stack = player.getMainHandItem();
CompoundTag tag = stack.getOrCreateTag(); CompoundTag tag = stack.getOrCreateTag();
//启动换弹 // 启动换弹
if (tag.getBoolean("start_sentinel_charge")) { if (tag.getBoolean("start_sentinel_charge")) {
tag.putInt("sentinel_charge_time", 127); tag.putInt("sentinel_charge_time", 127);
stack.getOrCreateTag().putBoolean("sentinel_is_charging", true); stack.getOrCreateTag().putBoolean("sentinel_is_charging", true);

View file

@ -304,16 +304,16 @@ public class PlayerEventHandler {
var cap = player.getCapability(ModVariables.PLAYER_VARIABLES_CAPABILITY, null).orElse(new ModVariables.PlayerVariables()); var cap = player.getCapability(ModVariables.PLAYER_VARIABLES_CAPABILITY, null).orElse(new ModVariables.PlayerVariables());
if (stack.is(ModTags.Items.USE_SHOTGUN_AMMO) && cap.shotgunAmmo > 0) { if (stack.is(ModTags.Items.USE_SHOTGUN_AMMO) && cap.shotgunAmmo > 0) {
GunsTool.reload(player, GunInfo.Type.SHOTGUN); GunsTool.reload(player, stack, GunInfo.Type.SHOTGUN);
} }
if (stack.is(ModTags.Items.USE_SNIPER_AMMO) && cap.sniperAmmo > 0) { if (stack.is(ModTags.Items.USE_SNIPER_AMMO) && cap.sniperAmmo > 0) {
GunsTool.reload(player, GunInfo.Type.SNIPER); GunsTool.reload(player, stack, GunInfo.Type.SNIPER);
} }
if (stack.is(ModTags.Items.USE_HANDGUN_AMMO)&& cap.handgunAmmo > 0) { if (stack.is(ModTags.Items.USE_HANDGUN_AMMO) && cap.handgunAmmo > 0) {
GunsTool.reload(player, GunInfo.Type.HANDGUN); GunsTool.reload(player, stack, GunInfo.Type.HANDGUN);
} }
if (stack.is(ModTags.Items.USE_RIFLE_AMMO)&& cap.rifleAmmo > 0) { if (stack.is(ModTags.Items.USE_RIFLE_AMMO) && cap.rifleAmmo > 0) {
GunsTool.reload(player, GunInfo.Type.RIFLE); GunsTool.reload(player, stack, GunInfo.Type.RIFLE);
} }
if (stack.getItem() == ModItems.TASER.get() && stack.getOrCreateTag().getInt("max_ammo") > 0) { if (stack.getItem() == ModItems.TASER.get() && stack.getOrCreateTag().getInt("max_ammo") > 0) {
stack.getOrCreateTag().putInt("ammo", 1); stack.getOrCreateTag().putInt("ammo", 1);
@ -354,7 +354,7 @@ public class PlayerEventHandler {
for (var stack : player.getInventory().items) { for (var stack : player.getInventory().items) {
if (stack.is(ModItems.ARMOR_PLATE.get())) { if (stack.is(ModItems.ARMOR_PLATE.get())) {
for (int index0 = 0; index0 < Math.ceil(((armorLevel * 30) - armorPlate) / 30); index0++) { for (int index0 = 0; index0 < Math.ceil(((armorLevel * 30) - armorPlate) / 30); index0++) {
stack.finishUsingItem(player.level(),player); stack.finishUsingItem(player.level(), player);
} }
} }
} }

View file

@ -7,8 +7,7 @@ import net.mcreator.superbwarfare.network.message.GunsDataMessage;
import net.minecraft.nbt.CompoundTag; import net.minecraft.nbt.CompoundTag;
import net.minecraft.server.level.ServerPlayer; import net.minecraft.server.level.ServerPlayer;
import net.minecraft.server.packs.resources.ResourceManager; import net.minecraft.server.packs.resources.ResourceManager;
import net.minecraft.world.entity.Entity; import net.minecraft.world.entity.player.Player;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.Level; import net.minecraft.world.level.Level;
import net.minecraftforge.event.entity.player.PlayerEvent; import net.minecraftforge.event.entity.player.PlayerEvent;
@ -81,33 +80,30 @@ public class GunsTool {
initJsonData(event.getServer().getResourceManager()); initJsonData(event.getServer().getResourceManager());
} }
public static void reload(Entity entity, GunInfo.Type type) { public static void reload(Player player, ItemStack stack, GunInfo.Type type) {
reload(entity, type, false); reload(player, stack, type, false);
} }
public static void reload(Entity entity, GunInfo.Type type, boolean extraOne) { public static void reload(Player player, ItemStack stack, GunInfo.Type type, boolean extraOne) {
if (!(entity instanceof LivingEntity living)) return; CompoundTag tag = stack.getOrCreateTag();
CompoundTag tag = living.getMainHandItem().getOrCreateTag();
int mag = tag.getInt("mag") + tag.getInt("customMag"); int mag = tag.getInt("mag") + tag.getInt("customMag");
int ammo = tag.getInt("ammo"); int ammo = tag.getInt("ammo");
int ammoToAdd = mag - ammo + (extraOne ? 1 : 0); int ammoToAdd = mag - ammo + (extraOne ? 1 : 0);
/*
* 空仓换弹的栓动武器应该在换单后取消待上膛标记 // 空仓换弹的栓动武器应该在换单后取消待上膛标记
*/
if (ammo == 0 && tag.getDouble("bolt_action_time") > 0) { if (ammo == 0 && tag.getDouble("bolt_action_time") > 0) {
tag.putDouble("need_bolt_action", 0); tag.putDouble("need_bolt_action", 0);
} }
int playerAmmo = entity.getCapability(ModVariables.PLAYER_VARIABLES_CAPABILITY, null).map(c -> switch (type) { int playerAmmo = player.getCapability(ModVariables.PLAYER_VARIABLES_CAPABILITY, null).map(c -> switch (type) {
case RIFLE -> c.rifleAmmo; case RIFLE -> c.rifleAmmo;
case HANDGUN -> c.handgunAmmo; case HANDGUN -> c.handgunAmmo;
case SHOTGUN -> c.shotgunAmmo; case SHOTGUN -> c.shotgunAmmo;
case SNIPER -> c.sniperAmmo; case SNIPER -> c.sniperAmmo;
}).orElse(0); }).orElse(0);
entity.getCapability(ModVariables.PLAYER_VARIABLES_CAPABILITY, null).ifPresent(capability -> { player.getCapability(ModVariables.PLAYER_VARIABLES_CAPABILITY, null).ifPresent(capability -> {
var newAmmoCount = Math.max(0, playerAmmo - ammoToAdd); var newAmmoCount = Math.max(0, playerAmmo - ammoToAdd);
switch (type) { switch (type) {
case RIFLE -> capability.rifleAmmo = newAmmoCount; case RIFLE -> capability.rifleAmmo = newAmmoCount;
@ -116,7 +112,7 @@ public class GunsTool {
case SNIPER -> capability.sniperAmmo = newAmmoCount; case SNIPER -> capability.sniperAmmo = newAmmoCount;
} }
capability.syncPlayerVariables(entity); capability.syncPlayerVariables(player);
}); });
int needToAdd = ammo + Math.min(ammoToAdd, playerAmmo); int needToAdd = ammo + Math.min(ammoToAdd, playerAmmo);