优化部分调用

This commit is contained in:
Light_Quanta 2025-04-18 01:16:59 +08:00
parent 53fe093be7
commit c6802f6865
No known key found for this signature in database
GPG key ID: 11A39A1B8C890959
5 changed files with 43 additions and 84 deletions

View file

@ -704,7 +704,7 @@ public class ClientEventHandler {
playGunClientSounds(player); playGunClientSounds(player);
handleClientShoot(); handleClientShoot();
} else if (stack.is(ModItems.MINIGUN.get())) { } else if (stack.is(ModItems.MINIGUN.get())) {
// TODO 提取通用处理方法 // TODO 提取通用过热处理方法
var perk = data.perk.get(Perk.Type.AMMO); var perk = data.perk.get(Perk.Type.AMMO);
float pitch = tag.getDouble("heat") <= 40 ? 1 : (float) (1 - 0.025 * Math.abs(40 - tag.getDouble("heat"))); float pitch = tag.getDouble("heat") <= 40 ? 1 : (float) (1 - 0.025 * Math.abs(40 - tag.getDouble("heat")));

View file

@ -9,7 +9,6 @@ import com.atsuishio.superbwarfare.init.ModTags;
import com.atsuishio.superbwarfare.item.gun.GunItem; import com.atsuishio.superbwarfare.item.gun.GunItem;
import com.atsuishio.superbwarfare.item.gun.data.GunData; import com.atsuishio.superbwarfare.item.gun.data.GunData;
import com.atsuishio.superbwarfare.item.gun.data.value.ReloadState; import com.atsuishio.superbwarfare.item.gun.data.value.ReloadState;
import com.atsuishio.superbwarfare.tools.Ammo;
import com.atsuishio.superbwarfare.tools.InventoryTool; import com.atsuishio.superbwarfare.tools.InventoryTool;
import com.atsuishio.superbwarfare.tools.SoundTool; import com.atsuishio.superbwarfare.tools.SoundTool;
import net.minecraft.core.registries.BuiltInRegistries; import net.minecraft.core.registries.BuiltInRegistries;
@ -274,40 +273,11 @@ public class GunEventHandler {
// 一阶段结束检查备弹如果有则二阶段启动无则直接跳到三阶段 // 一阶段结束检查备弹如果有则二阶段启动无则直接跳到三阶段
if ((reload.prepareTimer.get() == 1 || reload.prepareLoadTimer.get() == 1)) { if ((reload.prepareTimer.get() == 1 || reload.prepareLoadTimer.get() == 1)) {
if (!InventoryTool.hasCreativeAmmoBox(player)) { if (!data.hasBackupAmmo(player) || data.ammo.get() >= data.magazine()) {
var capability = player.getData(ModAttachments.PLAYER_VARIABLE);
var startStage3 = false;
var ammoTypeInfo = data.ammoTypeInfo();
if (ammoTypeInfo.type() == GunData.AmmoConsumeType.PLAYER_AMMO) {
var type = Ammo.getType(ammoTypeInfo.value());
assert type != null;
if (type.get(capability) == 0) {
startStage3 = true;
}
}
// TODO 优化这坨判断
if (stack.is(ModTags.Items.LAUNCHER) && !data.hasBackupAmmo(player)
|| stack.is(ModItems.SECONDARY_CATACLYSM.get()) && data.ammo.get() >= data.magazine()
) {
startStage3 = true;
}
if (startStage3) {
reload.stage3Starter.markStart(); reload.stage3Starter.markStart();
} else { } else {
reload.setStage(2); reload.setStage(2);
} }
} else {
if (stack.is(ModItems.SECONDARY_CATACLYSM.get()) && data.ammo.get() >= data.magazine()) {
reload.stage3Starter.markStart();
} else {
reload.setStage(2);
}
}
// 检查备弹
} }
// 强制停止换弹进入三阶段 // 强制停止换弹进入三阶段
@ -350,26 +320,11 @@ public class GunEventHandler {
// 二阶段结束 // 二阶段结束
if (reload.iterativeLoadTimer.get() == 1) { if (reload.iterativeLoadTimer.get() == 1) {
// 装满结束 // 装满或备弹耗尽结束
if (data.ammo.get() >= data.magazine()) { if (!data.hasBackupAmmo(player) || data.ammo.get() >= data.magazine()) {
reload.setStage(3); reload.setStage(3);
} }
// 备弹耗尽结束
if (!InventoryTool.hasCreativeAmmoBox(player)) {
var capability = player.getData(ModAttachments.PLAYER_VARIABLE);
var ammoTypeInfo = data.ammoTypeInfo();
if (ammoTypeInfo.type() == GunData.AmmoConsumeType.PLAYER_AMMO) {
var type = Ammo.getType(ammoTypeInfo.value());
assert type != null;
if (type.get(capability) == 0) {
reload.setStage(3);
}
}
}
// 强制结束 // 强制结束
if (data.stopped.get()) { if (data.stopped.get()) {
reload.setStage(3); reload.setStage(3);

View file

@ -144,12 +144,9 @@ public class LivingEventHandler {
if (DamageTypeTool.isGunDamage(source) && stack.getItem() instanceof GunItem) { if (DamageTypeTool.isGunDamage(source) && stack.getItem() instanceof GunItem) {
double distance = entity.position().distanceTo(sourceEntity.position()); double distance = entity.position().distanceTo(sourceEntity.position());
var ammoInfo = data.ammoTypeInfo(); var ammoType = data.ammoTypeInfo().playerAmmoType();
if (ammoInfo.type() == GunData.AmmoConsumeType.PLAYER_AMMO) { if (ammoType != null) {
var type = Ammo.getType(ammoInfo.value()); switch (ammoType) {
assert type != null;
switch (type) {
case SHOTGUN -> { case SHOTGUN -> {
var perk = data.perk.get(Perk.Type.AMMO); var perk = data.perk.get(Perk.Type.AMMO);
@ -661,16 +658,13 @@ public class LivingEventHandler {
boolean flag = InventoryTool.hasCreativeAmmoBox(player); boolean flag = InventoryTool.hasCreativeAmmoBox(player);
var ammoTypeInfo = data.ammoTypeInfo(); var ammoType = data.ammoTypeInfo().playerAmmoType();
if (ammoTypeInfo.type() == GunData.AmmoConsumeType.PLAYER_AMMO) { if (ammoType != null) {
var type = Ammo.getType(ammoTypeInfo.value()); int ammoFinal = Math.min(ammoType.get(cap), ammoNeed);
assert type != null;
int ammoFinal = Math.min(type.get(cap), ammoNeed);
if (flag) { if (flag) {
ammoFinal = ammoNeed; ammoFinal = ammoNeed;
} else { } else {
type.add(cap, -ammoFinal); ammoType.add(cap, -ammoFinal);
} }
data.ammo.set(Math.min(mag, ammo + ammoFinal)); data.ammo.set(Math.min(mag, ammo + ammoFinal));

View file

@ -10,7 +10,6 @@ import com.atsuishio.superbwarfare.item.gun.data.GunData;
import com.atsuishio.superbwarfare.item.gun.data.value.AttachmentType; import com.atsuishio.superbwarfare.item.gun.data.value.AttachmentType;
import com.atsuishio.superbwarfare.perk.AmmoPerk; import com.atsuishio.superbwarfare.perk.AmmoPerk;
import com.atsuishio.superbwarfare.perk.Perk; import com.atsuishio.superbwarfare.perk.Perk;
import com.atsuishio.superbwarfare.tools.Ammo;
import net.minecraft.client.model.HumanoidModel; import net.minecraft.client.model.HumanoidModel;
import net.minecraft.client.renderer.BlockEntityWithoutLevelRenderer; import net.minecraft.client.renderer.BlockEntityWithoutLevelRenderer;
import net.minecraft.core.BlockPos; import net.minecraft.core.BlockPos;
@ -82,12 +81,9 @@ public abstract class GunItem extends Item implements CustomRendererItem {
int count = ammoCount - magazine - (hasBulletInBarrel ? 1 : 0); int count = ammoCount - magazine - (hasBulletInBarrel ? 1 : 0);
var capability = entity.getData(ModAttachments.PLAYER_VARIABLE).watch(); var capability = entity.getData(ModAttachments.PLAYER_VARIABLE).watch();
var ammoTypeInfo = data.ammoTypeInfo(); var ammoType = data.ammoTypeInfo().playerAmmoType();
if (ammoTypeInfo.type() == GunData.AmmoConsumeType.PLAYER_AMMO) { if (ammoType != null) {
var type = Ammo.getType(ammoTypeInfo.value()); ammoType.add(capability, count);
assert type != null;
type.add(capability, count);
} }
entity.setData(ModAttachments.PLAYER_VARIABLE, capability); entity.setData(ModAttachments.PLAYER_VARIABLE, capability);
@ -453,11 +449,8 @@ public abstract class GunItem extends Item implements CustomRendererItem {
* 右下角弹药显示名称 * 右下角弹药显示名称
*/ */
public String getAmmoDisplayName(GunData data) { public String getAmmoDisplayName(GunData data) {
var ammoTypeInfo = data.ammoTypeInfo(); var type = data.ammoTypeInfo().playerAmmoType();
if (ammoTypeInfo.type() == GunData.AmmoConsumeType.PLAYER_AMMO) { if (type != null) {
var type = Ammo.getType(ammoTypeInfo.value());
assert type != null;
return type.displayName; return type.displayName;
} }
return ""; return "";

View file

@ -13,11 +13,16 @@ import net.minecraft.core.component.DataComponents;
import net.minecraft.nbt.CompoundTag; import net.minecraft.nbt.CompoundTag;
import net.minecraft.resources.ResourceLocation; import net.minecraft.resources.ResourceLocation;
import net.minecraft.tags.ItemTags; import net.minecraft.tags.ItemTags;
import net.minecraft.tags.TagKey;
import net.minecraft.util.Mth; import net.minecraft.util.Mth;
import net.minecraft.world.entity.player.Player; import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.component.CustomData; import net.minecraft.world.item.component.CustomData;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Objects;
import java.util.UUID; import java.util.UUID;
import java.util.WeakHashMap; import java.util.WeakHashMap;
@ -276,6 +281,23 @@ public class GunData {
} }
public record AmmoTypeInfo(AmmoConsumeType type, String value) { public record AmmoTypeInfo(AmmoConsumeType type, String value) {
/**
* 尝试返回Ammo类型
*/
public @Nullable Ammo playerAmmoType() {
if (type != AmmoConsumeType.PLAYER_AMMO) return null;
return toPlayerAmmoType();
}
public @NotNull Ammo toPlayerAmmoType() {
if (type != AmmoConsumeType.PLAYER_AMMO) throw new IllegalArgumentException("not PLAYER_AMMO type!");
return Objects.requireNonNull(Ammo.getType(value));
}
public TagKey<Item> toTag() {
if (type != AmmoConsumeType.TAG) throw new IllegalArgumentException("not TAG type!");
return ItemTags.create(ResourceLocation.parse(this.value()));
}
} }
public AmmoTypeInfo ammoTypeInfo() { public AmmoTypeInfo ammoTypeInfo() {
@ -334,7 +356,7 @@ public class GunData {
player.inventoryMenu.getCraftSlots() player.inventoryMenu.getCraftSlots()
); );
case TAG -> player.getInventory().clearOrCountMatchingItems( case TAG -> player.getInventory().clearOrCountMatchingItems(
p -> p.is(ItemTags.create(ResourceLocation.parse(info.value()))), p -> p.is(info.toTag()),
0, 0,
player.inventoryMenu.getCraftSlots() player.inventoryMenu.getCraftSlots()
); );
@ -350,19 +372,14 @@ public class GunData {
var info = ammoTypeInfo(); var info = ammoTypeInfo();
switch (info.type()) { switch (info.type()) {
case PLAYER_AMMO -> { case PLAYER_AMMO -> info.toPlayerAmmoType().set(player, info.toPlayerAmmoType().get(player) - count);
var type = Ammo.getType(info.value());
assert type != null;
type.set(player, type.get(player) - count);
}
case ITEM -> player.getInventory().clearOrCountMatchingItems( case ITEM -> player.getInventory().clearOrCountMatchingItems(
p -> p.getItem().toString().equals(info.value()), p -> p.getItem().toString().equals(info.value()),
count, count,
player.inventoryMenu.getCraftSlots() player.inventoryMenu.getCraftSlots()
); );
case TAG -> player.getInventory().clearOrCountMatchingItems( case TAG -> player.getInventory().clearOrCountMatchingItems(
p -> p.is(ItemTags.create(ResourceLocation.parse(info.value()))), p -> p.is(info.toTag()),
count, count,
player.inventoryMenu.getCraftSlots() player.inventoryMenu.getCraftSlots()
); );