From 6ff0e587d1c38252cba2193deda3519145d17d03 Mon Sep 17 00:00:00 2001 From: Light_Quanta Date: Sun, 13 Jul 2025 15:32:22 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8F=90=E5=8F=96RemoteControllableTurret?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../entity/vehicle/Mk42Entity.java | 15 +++++++-- .../entity/vehicle/Mle1934Entity.java | 15 +++++++-- .../entity/vehicle/MortarEntity.java | 14 ++++++-- ...ity.java => RemoteControllableTurret.java} | 9 +++++- .../item/ArtilleryIndicator.java | 4 +-- .../send/ArtilleryIndicatorFireMessage.java | 32 +++---------------- 6 files changed, 52 insertions(+), 37 deletions(-) rename src/main/java/com/atsuishio/superbwarfare/entity/vehicle/base/{LockTargetEntity.java => RemoteControllableTurret.java} (53%) diff --git a/src/main/java/com/atsuishio/superbwarfare/entity/vehicle/Mk42Entity.java b/src/main/java/com/atsuishio/superbwarfare/entity/vehicle/Mk42Entity.java index 73d1ae804..cb82b8806 100644 --- a/src/main/java/com/atsuishio/superbwarfare/entity/vehicle/Mk42Entity.java +++ b/src/main/java/com/atsuishio/superbwarfare/entity/vehicle/Mk42Entity.java @@ -5,7 +5,7 @@ import com.atsuishio.superbwarfare.component.ModDataComponents; import com.atsuishio.superbwarfare.config.server.ExplosionConfig; import com.atsuishio.superbwarfare.config.server.VehicleConfig; import com.atsuishio.superbwarfare.entity.vehicle.base.CannonEntity; -import com.atsuishio.superbwarfare.entity.vehicle.base.LockTargetEntity; +import com.atsuishio.superbwarfare.entity.vehicle.base.RemoteControllableTurret; import com.atsuishio.superbwarfare.entity.vehicle.base.ThirdPersonCameraPosition; import com.atsuishio.superbwarfare.entity.vehicle.base.VehicleEntity; import com.atsuishio.superbwarfare.entity.vehicle.damage.DamageModifier; @@ -59,7 +59,7 @@ import software.bernie.geckolib.util.GeckoLibUtil; import static com.atsuishio.superbwarfare.tools.RangeTool.calculateLaunchVector; -public class Mk42Entity extends VehicleEntity implements GeoEntity, CannonEntity, LockTargetEntity { +public class Mk42Entity extends VehicleEntity implements GeoEntity, CannonEntity, RemoteControllableTurret { public static final EntityDataAccessor COOL_DOWN = SynchedEntityData.defineId(Mk42Entity.class, EntityDataSerializers.INT); private final AnimatableInstanceCache cache = GeckoLibUtil.createInstanceCache(this); @@ -359,6 +359,17 @@ public class Mk42Entity extends VehicleEntity implements GeoEntity, CannonEntity return new Vec3(worldPosition.x, worldPosition.y, worldPosition.z); } + @Override + public boolean canRemoteFire() { + return this.getItem(0).getItem() instanceof CannonShellItem && this.entityData.get(COOL_DOWN) == 0; + } + + @Override + public void remoteFire(Player player) { + this.setWeaponIndex(0, this.getItem(0).is(ModItems.AP_5_INCHES.get()) ? 0 : 1); + this.vehicleShoot(player, 0); + } + @Override public void vehicleShoot(Player player, int type) { if (this.entityData.get(COOL_DOWN) > 0) return; diff --git a/src/main/java/com/atsuishio/superbwarfare/entity/vehicle/Mle1934Entity.java b/src/main/java/com/atsuishio/superbwarfare/entity/vehicle/Mle1934Entity.java index 51fe96ae5..cb1a887da 100644 --- a/src/main/java/com/atsuishio/superbwarfare/entity/vehicle/Mle1934Entity.java +++ b/src/main/java/com/atsuishio/superbwarfare/entity/vehicle/Mle1934Entity.java @@ -5,7 +5,7 @@ import com.atsuishio.superbwarfare.component.ModDataComponents; import com.atsuishio.superbwarfare.config.server.ExplosionConfig; import com.atsuishio.superbwarfare.config.server.VehicleConfig; import com.atsuishio.superbwarfare.entity.vehicle.base.CannonEntity; -import com.atsuishio.superbwarfare.entity.vehicle.base.LockTargetEntity; +import com.atsuishio.superbwarfare.entity.vehicle.base.RemoteControllableTurret; import com.atsuishio.superbwarfare.entity.vehicle.base.ThirdPersonCameraPosition; import com.atsuishio.superbwarfare.entity.vehicle.base.VehicleEntity; import com.atsuishio.superbwarfare.entity.vehicle.damage.DamageModifier; @@ -60,7 +60,7 @@ import software.bernie.geckolib.util.GeckoLibUtil; import static com.atsuishio.superbwarfare.tools.RangeTool.calculateLaunchVector; -public class Mle1934Entity extends VehicleEntity implements GeoEntity, CannonEntity, LockTargetEntity { +public class Mle1934Entity extends VehicleEntity implements GeoEntity, CannonEntity, RemoteControllableTurret { public static final EntityDataAccessor COOL_DOWN = SynchedEntityData.defineId(Mle1934Entity.class, EntityDataSerializers.INT); public static final EntityDataAccessor TYPE = SynchedEntityData.defineId(Mle1934Entity.class, EntityDataSerializers.INT); @@ -157,6 +157,17 @@ public class Mle1934Entity extends VehicleEntity implements GeoEntity, CannonEnt } } + @Override + public boolean canRemoteFire() { + return this.getItem(0).getItem() instanceof CannonShellItem && this.getEntityData().get(COOL_DOWN) == 0; + } + + @Override + public void remoteFire(@Nullable Player player) { + this.setWeaponIndex(0, this.getItem(0).is(ModItems.AP_5_INCHES.get()) ? 0 : 1); + this.vehicleShoot(player, 0); + } + @Override public @NotNull InteractionResult interact(Player player, @NotNull InteractionHand hand) { ItemStack stack = player.getMainHandItem(); diff --git a/src/main/java/com/atsuishio/superbwarfare/entity/vehicle/MortarEntity.java b/src/main/java/com/atsuishio/superbwarfare/entity/vehicle/MortarEntity.java index 017b2ab27..5feee6db2 100644 --- a/src/main/java/com/atsuishio/superbwarfare/entity/vehicle/MortarEntity.java +++ b/src/main/java/com/atsuishio/superbwarfare/entity/vehicle/MortarEntity.java @@ -2,7 +2,7 @@ package com.atsuishio.superbwarfare.entity.vehicle; import com.atsuishio.superbwarfare.component.ModDataComponents; import com.atsuishio.superbwarfare.entity.projectile.MortarShellEntity; -import com.atsuishio.superbwarfare.entity.vehicle.base.LockTargetEntity; +import com.atsuishio.superbwarfare.entity.vehicle.base.RemoteControllableTurret; import com.atsuishio.superbwarfare.entity.vehicle.base.VehicleEntity; import com.atsuishio.superbwarfare.init.ModEntities; import com.atsuishio.superbwarfare.init.ModItems; @@ -47,7 +47,7 @@ import software.bernie.geckolib.util.GeckoLibUtil; import static com.atsuishio.superbwarfare.tools.RangeTool.calculateLaunchVector; -public class MortarEntity extends VehicleEntity implements GeoEntity, LockTargetEntity { +public class MortarEntity extends VehicleEntity implements GeoEntity, RemoteControllableTurret { public static final EntityDataAccessor FIRE_TIME = SynchedEntityData.defineId(MortarEntity.class, EntityDataSerializers.INT); public static final EntityDataAccessor PITCH = SynchedEntityData.defineId(MortarEntity.class, EntityDataSerializers.FLOAT); @@ -149,6 +149,16 @@ public class MortarEntity extends VehicleEntity implements GeoEntity, LockTarget } } + @Override + public boolean canRemoteFire() { + return this.getItem(0).getItem() instanceof MortarShell && this.getEntityData().get(FIRE_TIME) == 0; + } + + @Override + public void remoteFire(@Nullable Player player) { + this.fire(player); + } + @Override public @NotNull InteractionResult interact(Player player, @NotNull InteractionHand hand) { ItemStack mainHandItem = player.getMainHandItem(); diff --git a/src/main/java/com/atsuishio/superbwarfare/entity/vehicle/base/LockTargetEntity.java b/src/main/java/com/atsuishio/superbwarfare/entity/vehicle/base/RemoteControllableTurret.java similarity index 53% rename from src/main/java/com/atsuishio/superbwarfare/entity/vehicle/base/LockTargetEntity.java rename to src/main/java/com/atsuishio/superbwarfare/entity/vehicle/base/RemoteControllableTurret.java index 312c3a325..9815f67d2 100644 --- a/src/main/java/com/atsuishio/superbwarfare/entity/vehicle/base/LockTargetEntity.java +++ b/src/main/java/com/atsuishio/superbwarfare/entity/vehicle/base/RemoteControllableTurret.java @@ -1,13 +1,20 @@ package com.atsuishio.superbwarfare.entity.vehicle.base; +import net.minecraft.world.entity.player.Player; import net.minecraft.world.item.ItemStack; import net.minecraft.world.phys.Vec3; -public interface LockTargetEntity { +import javax.annotation.Nullable; + +public interface RemoteControllableTurret { boolean setTarget(ItemStack stack); void resetTarget(); void look(Vec3 pTarget); + + boolean canRemoteFire(); + + void remoteFire(@Nullable Player player); } diff --git a/src/main/java/com/atsuishio/superbwarfare/item/ArtilleryIndicator.java b/src/main/java/com/atsuishio/superbwarfare/item/ArtilleryIndicator.java index d93eb263b..fcd20c101 100644 --- a/src/main/java/com/atsuishio/superbwarfare/item/ArtilleryIndicator.java +++ b/src/main/java/com/atsuishio/superbwarfare/item/ArtilleryIndicator.java @@ -2,7 +2,7 @@ package com.atsuishio.superbwarfare.item; import com.atsuishio.superbwarfare.client.TooltipTool; import com.atsuishio.superbwarfare.client.screens.ArtilleryIndicatorScreen; -import com.atsuishio.superbwarfare.entity.vehicle.base.LockTargetEntity; +import com.atsuishio.superbwarfare.entity.vehicle.base.RemoteControllableTurret; import com.atsuishio.superbwarfare.tools.EntityFindUtil; import com.atsuishio.superbwarfare.tools.NBTTool; import net.minecraft.ChatFormatting; @@ -148,7 +148,7 @@ public class ArtilleryIndicator extends Item implements ItemScreenProvider { var tag = tags.getCompound(i); Entity entity = EntityFindUtil.findEntity(player.level(), tag.getString("UUID")); - if (entity instanceof LockTargetEntity lockTargetEntity) { + if (entity instanceof RemoteControllableTurret lockTargetEntity) { list.add(tag); if (!lockTargetEntity.setTarget(stack)) { diff --git a/src/main/java/com/atsuishio/superbwarfare/network/message/send/ArtilleryIndicatorFireMessage.java b/src/main/java/com/atsuishio/superbwarfare/network/message/send/ArtilleryIndicatorFireMessage.java index 430e72535..7b9c9bbdc 100644 --- a/src/main/java/com/atsuishio/superbwarfare/network/message/send/ArtilleryIndicatorFireMessage.java +++ b/src/main/java/com/atsuishio/superbwarfare/network/message/send/ArtilleryIndicatorFireMessage.java @@ -1,12 +1,8 @@ package com.atsuishio.superbwarfare.network.message.send; import com.atsuishio.superbwarfare.Mod; -import com.atsuishio.superbwarfare.entity.vehicle.Mk42Entity; -import com.atsuishio.superbwarfare.entity.vehicle.Mle1934Entity; -import com.atsuishio.superbwarfare.entity.vehicle.MortarEntity; +import com.atsuishio.superbwarfare.entity.vehicle.base.RemoteControllableTurret; import com.atsuishio.superbwarfare.init.ModItems; -import com.atsuishio.superbwarfare.item.common.ammo.CannonShellItem; -import com.atsuishio.superbwarfare.item.common.ammo.MortarShell; import com.atsuishio.superbwarfare.tools.EntityFindUtil; import com.atsuishio.superbwarfare.tools.NBTTool; import io.netty.buffer.ByteBuf; @@ -20,8 +16,6 @@ import net.minecraft.world.item.ItemStack; import net.neoforged.neoforge.network.handling.IPayloadContext; import org.jetbrains.annotations.NotNull; -import static com.atsuishio.superbwarfare.entity.vehicle.Mk42Entity.COOL_DOWN; -import static com.atsuishio.superbwarfare.entity.vehicle.MortarEntity.FIRE_TIME; import static com.atsuishio.superbwarfare.item.ArtilleryIndicator.TAG_CANNON; public enum ArtilleryIndicatorFireMessage implements CustomPacketPayload { @@ -44,27 +38,9 @@ public enum ArtilleryIndicatorFireMessage implements CustomPacketPayload { for (int i = 0; i < tags.size(); i++) { var tag = tags.getCompound(i); Entity entity = EntityFindUtil.findEntity(player.level(), tag.getString("UUID")); - if (entity instanceof MortarEntity mortarEntity) { - if (mortarEntity.getItem(0).getItem() instanceof MortarShell && mortarEntity.getEntityData().get(FIRE_TIME) == 0) { - int randomNumber = (int) (Math.random() * 5) + 1; - Mod.queueServerWork(randomNumber, () -> mortarEntity.fire(player)); - } - } - if (entity instanceof Mk42Entity mk42Entity) { - if (mk42Entity.getItem(0).getItem() instanceof CannonShellItem && mk42Entity.getEntityData().get(COOL_DOWN) == 0) { - int randomNumber = (int) (Math.random() * 5) + 1; - var weaponType = mk42Entity.getItem(0).is(ModItems.AP_5_INCHES.get()) ? 0 : 1; - mk42Entity.setWeaponIndex(0, weaponType); - Mod.queueServerWork(randomNumber, () -> mk42Entity.vehicleShoot(player, 0)); - } - } - if (entity instanceof Mle1934Entity mle1934Entity) { - if (mle1934Entity.getItem(0).getItem() instanceof CannonShellItem && mle1934Entity.getEntityData().get(COOL_DOWN) == 0) { - int randomNumber = (int) (Math.random() * 5) + 1; - var weaponType = mle1934Entity.getItem(0).is(ModItems.AP_5_INCHES.get()) ? 0 : 1; - mle1934Entity.setWeaponIndex(0, weaponType); - Mod.queueServerWork(randomNumber, () -> mle1934Entity.vehicleShoot(player, 0)); - } + + if (entity instanceof RemoteControllableTurret turret && turret.canRemoteFire()) { + Mod.queueServerWork(i % 5 + 1, () -> turret.remoteFire(player)); } } }