优化部分调用
This commit is contained in:
parent
53fe093be7
commit
c6802f6865
5 changed files with 43 additions and 84 deletions
|
@ -704,7 +704,7 @@ public class ClientEventHandler {
|
|||
playGunClientSounds(player);
|
||||
handleClientShoot();
|
||||
} else if (stack.is(ModItems.MINIGUN.get())) {
|
||||
// TODO 提取通用处理方法
|
||||
// TODO 提取通用过热处理方法
|
||||
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")));
|
||||
|
||||
|
|
|
@ -9,7 +9,6 @@ import com.atsuishio.superbwarfare.init.ModTags;
|
|||
import com.atsuishio.superbwarfare.item.gun.GunItem;
|
||||
import com.atsuishio.superbwarfare.item.gun.data.GunData;
|
||||
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.SoundTool;
|
||||
import net.minecraft.core.registries.BuiltInRegistries;
|
||||
|
@ -274,40 +273,11 @@ public class GunEventHandler {
|
|||
|
||||
// 一阶段结束,检查备弹,如果有则二阶段启动,无则直接跳到三阶段
|
||||
if ((reload.prepareTimer.get() == 1 || reload.prepareLoadTimer.get() == 1)) {
|
||||
if (!InventoryTool.hasCreativeAmmoBox(player)) {
|
||||
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();
|
||||
} else {
|
||||
reload.setStage(2);
|
||||
}
|
||||
if (!data.hasBackupAmmo(player) || data.ammo.get() >= data.magazine()) {
|
||||
reload.stage3Starter.markStart();
|
||||
} else {
|
||||
if (stack.is(ModItems.SECONDARY_CATACLYSM.get()) && data.ammo.get() >= data.magazine()) {
|
||||
reload.stage3Starter.markStart();
|
||||
} else {
|
||||
reload.setStage(2);
|
||||
}
|
||||
reload.setStage(2);
|
||||
}
|
||||
// 检查备弹
|
||||
}
|
||||
|
||||
// 强制停止换弹,进入三阶段
|
||||
|
@ -350,26 +320,11 @@ public class GunEventHandler {
|
|||
|
||||
// 二阶段结束
|
||||
if (reload.iterativeLoadTimer.get() == 1) {
|
||||
// 装满结束
|
||||
if (data.ammo.get() >= data.magazine()) {
|
||||
// 装满或备弹耗尽结束
|
||||
if (!data.hasBackupAmmo(player) || data.ammo.get() >= data.magazine()) {
|
||||
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()) {
|
||||
reload.setStage(3);
|
||||
|
|
|
@ -144,12 +144,9 @@ public class LivingEventHandler {
|
|||
if (DamageTypeTool.isGunDamage(source) && stack.getItem() instanceof GunItem) {
|
||||
double distance = entity.position().distanceTo(sourceEntity.position());
|
||||
|
||||
var ammoInfo = data.ammoTypeInfo();
|
||||
if (ammoInfo.type() == GunData.AmmoConsumeType.PLAYER_AMMO) {
|
||||
var type = Ammo.getType(ammoInfo.value());
|
||||
assert type != null;
|
||||
|
||||
switch (type) {
|
||||
var ammoType = data.ammoTypeInfo().playerAmmoType();
|
||||
if (ammoType != null) {
|
||||
switch (ammoType) {
|
||||
case SHOTGUN -> {
|
||||
var perk = data.perk.get(Perk.Type.AMMO);
|
||||
|
||||
|
@ -661,16 +658,13 @@ public class LivingEventHandler {
|
|||
|
||||
boolean flag = InventoryTool.hasCreativeAmmoBox(player);
|
||||
|
||||
var ammoTypeInfo = data.ammoTypeInfo();
|
||||
if (ammoTypeInfo.type() == GunData.AmmoConsumeType.PLAYER_AMMO) {
|
||||
var type = Ammo.getType(ammoTypeInfo.value());
|
||||
assert type != null;
|
||||
|
||||
int ammoFinal = Math.min(type.get(cap), ammoNeed);
|
||||
var ammoType = data.ammoTypeInfo().playerAmmoType();
|
||||
if (ammoType != null) {
|
||||
int ammoFinal = Math.min(ammoType.get(cap), ammoNeed);
|
||||
if (flag) {
|
||||
ammoFinal = ammoNeed;
|
||||
} else {
|
||||
type.add(cap, -ammoFinal);
|
||||
ammoType.add(cap, -ammoFinal);
|
||||
}
|
||||
|
||||
data.ammo.set(Math.min(mag, ammo + ammoFinal));
|
||||
|
|
|
@ -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.perk.AmmoPerk;
|
||||
import com.atsuishio.superbwarfare.perk.Perk;
|
||||
import com.atsuishio.superbwarfare.tools.Ammo;
|
||||
import net.minecraft.client.model.HumanoidModel;
|
||||
import net.minecraft.client.renderer.BlockEntityWithoutLevelRenderer;
|
||||
import net.minecraft.core.BlockPos;
|
||||
|
@ -82,12 +81,9 @@ public abstract class GunItem extends Item implements CustomRendererItem {
|
|||
int count = ammoCount - magazine - (hasBulletInBarrel ? 1 : 0);
|
||||
var capability = entity.getData(ModAttachments.PLAYER_VARIABLE).watch();
|
||||
|
||||
var ammoTypeInfo = data.ammoTypeInfo();
|
||||
if (ammoTypeInfo.type() == GunData.AmmoConsumeType.PLAYER_AMMO) {
|
||||
var type = Ammo.getType(ammoTypeInfo.value());
|
||||
assert type != null;
|
||||
|
||||
type.add(capability, count);
|
||||
var ammoType = data.ammoTypeInfo().playerAmmoType();
|
||||
if (ammoType != null) {
|
||||
ammoType.add(capability, count);
|
||||
}
|
||||
|
||||
entity.setData(ModAttachments.PLAYER_VARIABLE, capability);
|
||||
|
@ -453,11 +449,8 @@ public abstract class GunItem extends Item implements CustomRendererItem {
|
|||
* 右下角弹药显示名称
|
||||
*/
|
||||
public String getAmmoDisplayName(GunData data) {
|
||||
var ammoTypeInfo = data.ammoTypeInfo();
|
||||
if (ammoTypeInfo.type() == GunData.AmmoConsumeType.PLAYER_AMMO) {
|
||||
var type = Ammo.getType(ammoTypeInfo.value());
|
||||
assert type != null;
|
||||
|
||||
var type = data.ammoTypeInfo().playerAmmoType();
|
||||
if (type != null) {
|
||||
return type.displayName;
|
||||
}
|
||||
return "";
|
||||
|
|
|
@ -13,11 +13,16 @@ import net.minecraft.core.component.DataComponents;
|
|||
import net.minecraft.nbt.CompoundTag;
|
||||
import net.minecraft.resources.ResourceLocation;
|
||||
import net.minecraft.tags.ItemTags;
|
||||
import net.minecraft.tags.TagKey;
|
||||
import net.minecraft.util.Mth;
|
||||
import net.minecraft.world.entity.player.Player;
|
||||
import net.minecraft.world.item.Item;
|
||||
import net.minecraft.world.item.ItemStack;
|
||||
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.WeakHashMap;
|
||||
|
||||
|
@ -276,6 +281,23 @@ public class GunData {
|
|||
}
|
||||
|
||||
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() {
|
||||
|
@ -334,7 +356,7 @@ public class GunData {
|
|||
player.inventoryMenu.getCraftSlots()
|
||||
);
|
||||
case TAG -> player.getInventory().clearOrCountMatchingItems(
|
||||
p -> p.is(ItemTags.create(ResourceLocation.parse(info.value()))),
|
||||
p -> p.is(info.toTag()),
|
||||
0,
|
||||
player.inventoryMenu.getCraftSlots()
|
||||
);
|
||||
|
@ -350,19 +372,14 @@ public class GunData {
|
|||
|
||||
var info = ammoTypeInfo();
|
||||
switch (info.type()) {
|
||||
case PLAYER_AMMO -> {
|
||||
var type = Ammo.getType(info.value());
|
||||
assert type != null;
|
||||
|
||||
type.set(player, type.get(player) - count);
|
||||
}
|
||||
case PLAYER_AMMO -> info.toPlayerAmmoType().set(player, info.toPlayerAmmoType().get(player) - count);
|
||||
case ITEM -> player.getInventory().clearOrCountMatchingItems(
|
||||
p -> p.getItem().toString().equals(info.value()),
|
||||
count,
|
||||
player.inventoryMenu.getCraftSlots()
|
||||
);
|
||||
case TAG -> player.getInventory().clearOrCountMatchingItems(
|
||||
p -> p.is(ItemTags.create(ResourceLocation.parse(info.value()))),
|
||||
p -> p.is(info.toTag()),
|
||||
count,
|
||||
player.inventoryMenu.getCraftSlots()
|
||||
);
|
||||
|
|
Loading…
Add table
Reference in a new issue