尝试修复自动换弹问题
This commit is contained in:
parent
3119dc089c
commit
58e5ab348a
3 changed files with 33 additions and 45 deletions
|
@ -369,19 +369,19 @@ public class GunEventHandler {
|
|||
if (count == 0) {
|
||||
if (stack.is(ModTags.Items.USE_SHOTGUN_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 {
|
||||
GunsTool.reload(player, GunInfo.Type.SHOTGUN);
|
||||
GunsTool.reload(player, stack, GunInfo.Type.SHOTGUN);
|
||||
}
|
||||
} 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)) {
|
||||
GunsTool.reload(player, GunInfo.Type.HANDGUN, true);
|
||||
GunsTool.reload(player, stack, GunInfo.Type.HANDGUN, true);
|
||||
} else if (stack.is(ModTags.Items.USE_RIFLE_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 {
|
||||
GunsTool.reload(player, GunInfo.Type.RIFLE);
|
||||
GunsTool.reload(player, stack, GunInfo.Type.RIFLE);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
|
@ -396,7 +396,6 @@ public class GunEventHandler {
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
stack.getOrCreateTag().putBoolean("is_normal_reloading", false);
|
||||
stack.getOrCreateTag().putBoolean("is_empty_reloading", false);
|
||||
|
||||
|
@ -415,13 +414,13 @@ public class GunEventHandler {
|
|||
|
||||
if (count == 0) {
|
||||
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)) {
|
||||
GunsTool.reload(player, GunInfo.Type.SNIPER);
|
||||
GunsTool.reload(player, stack, GunInfo.Type.SNIPER);
|
||||
} 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)) {
|
||||
GunsTool.reload(player, GunInfo.Type.RIFLE);
|
||||
GunsTool.reload(player, stack, GunInfo.Type.RIFLE);
|
||||
} else if (stack.getItem() == ModItems.TASER.get()) {
|
||||
stack.getOrCreateTag().putInt("ammo", 1);
|
||||
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().putBoolean("is_normal_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)) {
|
||||
|
||||
int count = 0;
|
||||
for (var inv : player.getInventory().items) {
|
||||
if (inv.is(ModItems.CREATIVE_AMMO_BOX.get())) {
|
||||
|
@ -603,14 +599,12 @@ public class GunEventHandler {
|
|||
|
||||
// 二阶段结束
|
||||
if (tag.getInt("iterative") == 1) {
|
||||
|
||||
// 装满结束
|
||||
if (tag.getInt("ammo") >= (int) tag.getDouble("mag") + tag.getInt("customMag")) {
|
||||
tag.putInt("reload_stage", 3);
|
||||
}
|
||||
|
||||
// 备弹耗尽结束
|
||||
|
||||
int count = 0;
|
||||
for (var inv : player.getInventory().items) {
|
||||
if (inv.is(ModItems.CREATIVE_AMMO_BOX.get())) {
|
||||
|
@ -631,7 +625,6 @@ public class GunEventHandler {
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
// 强制结束
|
||||
if (tag.getBoolean("stop")) {
|
||||
tag.putInt("reload_stage", 3);
|
||||
|
@ -696,7 +689,6 @@ public class GunEventHandler {
|
|||
});
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static void playGunPrepareReloadSounds(Player player) {
|
||||
|
|
|
@ -304,16 +304,16 @@ public class PlayerEventHandler {
|
|||
var cap = player.getCapability(ModVariables.PLAYER_VARIABLES_CAPABILITY, null).orElse(new ModVariables.PlayerVariables());
|
||||
|
||||
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) {
|
||||
GunsTool.reload(player, GunInfo.Type.SNIPER);
|
||||
GunsTool.reload(player, stack, GunInfo.Type.SNIPER);
|
||||
}
|
||||
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) {
|
||||
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) {
|
||||
stack.getOrCreateTag().putInt("ammo", 1);
|
||||
|
|
|
@ -7,8 +7,7 @@ import net.mcreator.superbwarfare.network.message.GunsDataMessage;
|
|||
import net.minecraft.nbt.CompoundTag;
|
||||
import net.minecraft.server.level.ServerPlayer;
|
||||
import net.minecraft.server.packs.resources.ResourceManager;
|
||||
import net.minecraft.world.entity.Entity;
|
||||
import net.minecraft.world.entity.LivingEntity;
|
||||
import net.minecraft.world.entity.player.Player;
|
||||
import net.minecraft.world.item.ItemStack;
|
||||
import net.minecraft.world.level.Level;
|
||||
import net.minecraftforge.event.entity.player.PlayerEvent;
|
||||
|
@ -81,33 +80,30 @@ public class GunsTool {
|
|||
initJsonData(event.getServer().getResourceManager());
|
||||
}
|
||||
|
||||
public static void reload(Entity entity, GunInfo.Type type) {
|
||||
reload(entity, type, false);
|
||||
public static void reload(Player player, ItemStack stack, GunInfo.Type type) {
|
||||
reload(player, stack, type, false);
|
||||
}
|
||||
|
||||
public static void reload(Entity entity, GunInfo.Type type, boolean extraOne) {
|
||||
if (!(entity instanceof LivingEntity living)) return;
|
||||
|
||||
CompoundTag tag = living.getMainHandItem().getOrCreateTag();
|
||||
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 ammo = tag.getInt("ammo");
|
||||
int ammoToAdd = mag - ammo + (extraOne ? 1 : 0);
|
||||
/*
|
||||
* 空仓换弹的栓动武器应该在换单后取消待上膛标记
|
||||
*/
|
||||
|
||||
// 空仓换弹的栓动武器应该在换单后取消待上膛标记
|
||||
if (ammo == 0 && tag.getDouble("bolt_action_time") > 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 HANDGUN -> c.handgunAmmo;
|
||||
case SHOTGUN -> c.shotgunAmmo;
|
||||
case SNIPER -> c.sniperAmmo;
|
||||
}).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);
|
||||
switch (type) {
|
||||
case RIFLE -> capability.rifleAmmo = newAmmoCount;
|
||||
|
@ -116,7 +112,7 @@ public class GunsTool {
|
|||
case SNIPER -> capability.sniperAmmo = newAmmoCount;
|
||||
}
|
||||
|
||||
capability.syncPlayerVariables(entity);
|
||||
capability.syncPlayerVariables(player);
|
||||
});
|
||||
|
||||
int needToAdd = ammo + Math.min(ammoToAdd, playerAmmo);
|
||||
|
|
Loading…
Add table
Reference in a new issue