提取RemoteControllableTurret接口

This commit is contained in:
Light_Quanta 2025-07-13 15:32:22 +08:00
parent 64f0c88898
commit 6ff0e587d1
No known key found for this signature in database
GPG key ID: 11A39A1B8C890959
6 changed files with 52 additions and 37 deletions

View file

@ -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<Integer> 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;

View file

@ -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<Integer> COOL_DOWN = SynchedEntityData.defineId(Mle1934Entity.class, EntityDataSerializers.INT);
public static final EntityDataAccessor<Integer> 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();

View file

@ -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<Integer> FIRE_TIME = SynchedEntityData.defineId(MortarEntity.class, EntityDataSerializers.INT);
public static final EntityDataAccessor<Float> 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();

View file

@ -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);
}

View file

@ -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)) {

View file

@ -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));
}
}
}