优化火炮的开火逻辑

This commit is contained in:
17146 2024-08-13 16:14:21 +08:00
parent d1b0d2da3d
commit 053803492c
5 changed files with 52 additions and 62 deletions

View file

@ -43,7 +43,7 @@ public class ClickHandler {
int button = event.getButton(); int button = event.getButton();
if (button == GLFW.GLFW_MOUSE_BUTTON_LEFT) { if (button == GLFW.GLFW_MOUSE_BUTTON_LEFT) {
ModUtils.PACKET_HANDLER.sendToServer(new FireMessage(1)); ModUtils.PACKET_HANDLER.sendToServer(new FireMessage(1));
ModUtils.PACKET_HANDLER.sendToServer(new VehicleFireMessage(1)); ModUtils.PACKET_HANDLER.sendToServer(new VehicleFireMessage(0));
} }
if (button == GLFW.GLFW_MOUSE_BUTTON_RIGHT) { if (button == GLFW.GLFW_MOUSE_BUTTON_RIGHT) {
if (Minecraft.getInstance().player.hasEffect(ModMobEffects.SHOCK.get())) { if (Minecraft.getInstance().player.hasEffect(ModMobEffects.SHOCK.get())) {

View file

@ -0,0 +1,7 @@
package net.mcreator.superbwarfare.entity;
import net.minecraft.world.entity.player.Player;
public interface ICannonEntity {
void cannonShoot(Player player);
}

View file

@ -47,7 +47,7 @@ import software.bernie.geckolib.core.animation.RawAnimation;
import software.bernie.geckolib.core.object.PlayState; import software.bernie.geckolib.core.object.PlayState;
import software.bernie.geckolib.util.GeckoLibUtil; import software.bernie.geckolib.util.GeckoLibUtil;
public class Mk42Entity extends PathfinderMob implements GeoEntity { public class Mk42Entity extends PathfinderMob implements GeoEntity, ICannonEntity {
public static final EntityDataAccessor<Boolean> SHOOT = SynchedEntityData.defineId(Mk42Entity.class, EntityDataSerializers.BOOLEAN); public static final EntityDataAccessor<Boolean> SHOOT = SynchedEntityData.defineId(Mk42Entity.class, EntityDataSerializers.BOOLEAN);
public static final EntityDataAccessor<String> ANIMATION = SynchedEntityData.defineId(Mk42Entity.class, EntityDataSerializers.STRING); public static final EntityDataAccessor<String> ANIMATION = SynchedEntityData.defineId(Mk42Entity.class, EntityDataSerializers.STRING);
public static final EntityDataAccessor<String> TEXTURE = SynchedEntityData.defineId(Mk42Entity.class, EntityDataSerializers.STRING); public static final EntityDataAccessor<String> TEXTURE = SynchedEntityData.defineId(Mk42Entity.class, EntityDataSerializers.STRING);
@ -135,18 +135,16 @@ public class Mk42Entity extends PathfinderMob implements GeoEntity {
return super.hurt(source, amount - 32); return super.hurt(source, amount - 32);
} }
@Override @Override
public InteractionResult mobInteract(Player sourceentity, InteractionHand hand) { public InteractionResult mobInteract(Player player, InteractionHand hand) {
InteractionResult retval = InteractionResult.sidedSuccess(this.level().isClientSide()); InteractionResult result = InteractionResult.sidedSuccess(this.level().isClientSide());
super.mobInteract(sourceentity, hand); super.mobInteract(player, hand);
sourceentity.setXRot(this.getXRot()); player.setXRot(this.getXRot());
sourceentity.setYRot(this.getYRot()); player.setYRot(this.getYRot());
sourceentity.startRiding(this); player.startRiding(this);
return retval; return result;
} }
@Override @Override
public void die(DamageSource source) { public void die(DamageSource source) {
super.die(source); super.die(source);
@ -189,11 +187,11 @@ public class Mk42Entity extends PathfinderMob implements GeoEntity {
Entity gunner = this.getFirstPassenger(); Entity gunner = this.getFirstPassenger();
if (this.getPersistentData().getInt("fire_cooldown") > 0) { if (this.getPersistentData().getInt("FireCooldown") > 0) {
this.getPersistentData().putInt("fire_cooldown", this.getPersistentData().getInt("fire_cooldown") - 1); this.getPersistentData().putInt("FireCooldown", this.getPersistentData().getInt("FireCooldown") - 1);
} }
if (this.getPersistentData().getInt("fire_cooldown") > 28) { if (this.getPersistentData().getInt("FireCooldown") > 28) {
gunner.getCapability(ModVariables.PLAYER_VARIABLES_CAPABILITY, null).ifPresent(capability -> { gunner.getCapability(ModVariables.PLAYER_VARIABLES_CAPABILITY, null).ifPresent(capability -> {
if (Math.random() < 0.5) { if (Math.random() < 0.5) {
@ -207,14 +205,15 @@ public class Mk42Entity extends PathfinderMob implements GeoEntity {
}); });
} }
if (this.getPersistentData().getBoolean("firing") && gunner instanceof Player player && this.getPersistentData().getInt("fire_cooldown") == 0) {
cannonShoot(player);
}
this.refreshDimensions(); this.refreshDimensions();
} }
@Override
public void cannonShoot(Player player) { public void cannonShoot(Player player) {
if (this.getPersistentData().getInt("FireCooldown") > 0) {
return;
}
Level level = player.level(); Level level = player.level();
if (level instanceof ServerLevel server) { if (level instanceof ServerLevel server) {
ItemStack stack = player.getMainHandItem(); ItemStack stack = player.getMainHandItem();
@ -258,7 +257,6 @@ public class Mk42Entity extends PathfinderMob implements GeoEntity {
entityToSpawn.shoot(this.getLookAngle().x, this.getLookAngle().y, this.getLookAngle().z, 15, 0.1f); entityToSpawn.shoot(this.getLookAngle().x, this.getLookAngle().y, this.getLookAngle().z, 15, 0.1f);
level.addFreshEntity(entityToSpawn); level.addFreshEntity(entityToSpawn);
if (player instanceof ServerPlayer serverPlayer) { if (player instanceof ServerPlayer serverPlayer) {
SoundTool.playLocalSound(serverPlayer, ModSounds.MK_42_FIRE_1P.get(), 2, 1); SoundTool.playLocalSound(serverPlayer, ModSounds.MK_42_FIRE_1P.get(), 2, 1);
SoundTool.playLocalSound(serverPlayer, ModSounds.MK_42_RELOAD.get(), 2, 1); SoundTool.playLocalSound(serverPlayer, ModSounds.MK_42_RELOAD.get(), 2, 1);
@ -267,7 +265,7 @@ public class Mk42Entity extends PathfinderMob implements GeoEntity {
serverPlayer.level().playSound(null, serverPlayer.getOnPos(), ModSounds.MK_42_VERYFAR.get(), SoundSource.PLAYERS, 32, 1); serverPlayer.level().playSound(null, serverPlayer.getOnPos(), ModSounds.MK_42_VERYFAR.get(), SoundSource.PLAYERS, 32, 1);
} }
this.getPersistentData().putInt("fire_cooldown", 30); this.getPersistentData().putInt("FireCooldown", 30);
server.sendParticles(ParticleTypes.CAMPFIRE_COSY_SMOKE, server.sendParticles(ParticleTypes.CAMPFIRE_COSY_SMOKE,
this.getX() + 5 * this.getLookAngle().x, this.getX() + 5 * this.getLookAngle().x,
@ -319,14 +317,13 @@ public class Mk42Entity extends PathfinderMob implements GeoEntity {
this.getEyeY() + 13 * this.getLookAngle().y, this.getEyeY() + 13 * this.getLookAngle().y,
this.getZ() + 13 * this.getLookAngle().z, this.getZ() + 13 * this.getLookAngle().z,
1, 0.15, 0.15, 0.15, 0.0075); 1, 0.15, 0.15, 0.15, 0.0075);
} }
} }
@Override @Override
public void travel(Vec3 dir) { public void travel(Vec3 dir) {
Entity entity = this.getPassengers().isEmpty() ? null : this.getPassengers().get(0); Entity entity = this.getPassengers().isEmpty() ? null : this.getPassengers().get(0);
if (this.isVehicle()) { if (this.isVehicle() && entity != null) {
this.setYRot(entity.getYRot()); this.setYRot(entity.getYRot());
this.yRotO = this.getYRot(); this.yRotO = this.getYRot();
this.setXRot(Mth.clamp(entity.getXRot() - 1.35f, -85, 15)); this.setXRot(Mth.clamp(entity.getXRot() - 1.35f, -85, 15));
@ -354,7 +351,6 @@ public class Mk42Entity extends PathfinderMob implements GeoEntity {
super.travel(dir); super.travel(dir);
} }
@Override @Override
public EntityDimensions getDimensions(Pose p_33597_) { public EntityDimensions getDimensions(Pose p_33597_) {
return super.getDimensions(p_33597_).scale((float) 1); return super.getDimensions(p_33597_).scale((float) 1);
@ -452,7 +448,7 @@ public class Mk42Entity extends PathfinderMob implements GeoEntity {
if (entity instanceof Mk42Entity mk42) { if (entity instanceof Mk42Entity mk42) {
if (mk42.getFirstPassenger() == null) return; if (mk42.getFirstPassenger() == null) return;
Entity gunner = mk42.getFirstPassenger(); Entity gunner = mk42.getFirstPassenger();
if (event.getSource().getDirectEntity() == gunner){ if (event.getSource().getDirectEntity() == gunner) {
event.setCanceled(true); event.setCanceled(true);
} }
} }

View file

@ -48,7 +48,7 @@ import software.bernie.geckolib.core.animation.RawAnimation;
import software.bernie.geckolib.core.object.PlayState; import software.bernie.geckolib.core.object.PlayState;
import software.bernie.geckolib.util.GeckoLibUtil; import software.bernie.geckolib.util.GeckoLibUtil;
public class Mle1934Entity extends PathfinderMob implements GeoEntity { public class Mle1934Entity extends PathfinderMob implements GeoEntity, ICannonEntity {
public static final EntityDataAccessor<String> ANIMATION = SynchedEntityData.defineId(Mle1934Entity.class, EntityDataSerializers.STRING); public static final EntityDataAccessor<String> ANIMATION = SynchedEntityData.defineId(Mle1934Entity.class, EntityDataSerializers.STRING);
private final AnimatableInstanceCache cache = GeckoLibUtil.createInstanceCache(this); private final AnimatableInstanceCache cache = GeckoLibUtil.createInstanceCache(this);
@ -124,18 +124,16 @@ public class Mle1934Entity extends PathfinderMob implements GeoEntity {
return super.hurt(source, amount - 34); return super.hurt(source, amount - 34);
} }
@Override @Override
public InteractionResult mobInteract(Player sourceentity, InteractionHand hand) { public InteractionResult mobInteract(Player player, InteractionHand hand) {
InteractionResult retval = InteractionResult.sidedSuccess(this.level().isClientSide()); InteractionResult result = InteractionResult.sidedSuccess(this.level().isClientSide());
super.mobInteract(sourceentity, hand); super.mobInteract(player, hand);
sourceentity.setXRot(this.getXRot()); player.setXRot(this.getXRot());
sourceentity.setYRot(this.getYRot()); player.setYRot(this.getYRot());
sourceentity.startRiding(this); player.startRiding(this);
return retval; return result;
} }
@Override @Override
public void die(DamageSource source) { public void die(DamageSource source) {
super.die(source); super.die(source);
@ -175,25 +173,26 @@ public class Mle1934Entity extends PathfinderMob implements GeoEntity {
Entity gunner = this.getFirstPassenger(); Entity gunner = this.getFirstPassenger();
if (this.getPersistentData().getInt("fire_cooldown") > 0) { if (this.getPersistentData().getInt("FireCooldown") > 0) {
this.getPersistentData().putInt("fire_cooldown", this.getPersistentData().getInt("fire_cooldown") - 1); this.getPersistentData().putInt("FireCooldown", this.getPersistentData().getInt("FireCooldown") - 1);
} }
if (this.getPersistentData().getInt("fire_cooldown") > 72) { if (this.getPersistentData().getInt("FireCooldown") > 72) {
gunner.getCapability(ModVariables.PLAYER_VARIABLES_CAPABILITY, null).ifPresent(capability -> { gunner.getCapability(ModVariables.PLAYER_VARIABLES_CAPABILITY, null).ifPresent(capability -> {
capability.cannonRecoil = 10; capability.cannonRecoil = 10;
capability.syncPlayerVariables(gunner); capability.syncPlayerVariables(gunner);
}); });
} }
if (this.getPersistentData().getBoolean("firing") && gunner instanceof Player player && this.getPersistentData().getInt("fire_cooldown") == 0) {
cannonShoot(player);
}
this.refreshDimensions(); this.refreshDimensions();
} }
@Override
public void cannonShoot(Player player) { public void cannonShoot(Player player) {
if (this.getPersistentData().getInt("FireCooldown") > 0) {
return;
}
Level level = player.level(); Level level = player.level();
if (level instanceof ServerLevel server) { if (level instanceof ServerLevel server) {
ItemStack stack = player.getMainHandItem(); ItemStack stack = player.getMainHandItem();
@ -230,7 +229,7 @@ public class Mle1934Entity extends PathfinderMob implements GeoEntity {
} }
if (!player.isCreative()) { if (!player.isCreative()) {
stack.shrink(salvoShoot? 2 : 1); stack.shrink(salvoShoot ? 2 : 1);
} }
// TODO 将炮弹生成在正确的位置现在均在中心 // TODO 将炮弹生成在正确的位置现在均在中心
@ -261,7 +260,6 @@ public class Mle1934Entity extends PathfinderMob implements GeoEntity {
}); });
} }
if (player instanceof ServerPlayer serverPlayer) { if (player instanceof ServerPlayer serverPlayer) {
SoundTool.playLocalSound(serverPlayer, ModSounds.MK_42_FIRE_1P.get(), 2, 1); SoundTool.playLocalSound(serverPlayer, ModSounds.MK_42_FIRE_1P.get(), 2, 1);
ModUtils.queueServerWork(44, () -> { ModUtils.queueServerWork(44, () -> {
@ -272,7 +270,7 @@ public class Mle1934Entity extends PathfinderMob implements GeoEntity {
serverPlayer.level().playSound(null, serverPlayer.getOnPos(), ModSounds.MK_42_VERYFAR.get(), SoundSource.PLAYERS, 32, 1); serverPlayer.level().playSound(null, serverPlayer.getOnPos(), ModSounds.MK_42_VERYFAR.get(), SoundSource.PLAYERS, 32, 1);
} }
this.getPersistentData().putInt("fire_cooldown", 74); this.getPersistentData().putInt("FireCooldown", 74);
server.sendParticles(ParticleTypes.CAMPFIRE_COSY_SMOKE, server.sendParticles(ParticleTypes.CAMPFIRE_COSY_SMOKE,
this.getX() + 5 * this.getLookAngle().x, this.getX() + 5 * this.getLookAngle().x,
@ -288,7 +286,6 @@ public class Mle1934Entity extends PathfinderMob implements GeoEntity {
server.sendParticles(ParticleTypes.CLOUD, x, y, z, 10, 0.4, 0.4, 0.4, 0.0075); server.sendParticles(ParticleTypes.CLOUD, x, y, z, 10, 0.4, 0.4, 0.4, 0.0075);
server.sendParticles(ParticleTypes.CAMPFIRE_COSY_SMOKE, server.sendParticles(ParticleTypes.CAMPFIRE_COSY_SMOKE,
this.getX() + 9.5 * this.getLookAngle().x, this.getX() + 9.5 * this.getLookAngle().x,
this.getEyeY() + 9.5 * this.getLookAngle().y, this.getEyeY() + 9.5 * this.getLookAngle().y,
@ -324,14 +321,13 @@ public class Mle1934Entity extends PathfinderMob implements GeoEntity {
this.getEyeY() + 13 * this.getLookAngle().y, this.getEyeY() + 13 * this.getLookAngle().y,
this.getZ() + 13 * this.getLookAngle().z, this.getZ() + 13 * this.getLookAngle().z,
1, 0.15, 0.15, 0.15, 0.0075); 1, 0.15, 0.15, 0.15, 0.0075);
} }
} }
@Override @Override
public void travel(Vec3 dir) { public void travel(Vec3 dir) {
Entity entity = this.getPassengers().isEmpty() ? null : this.getPassengers().get(0); Entity entity = this.getPassengers().isEmpty() ? null : this.getPassengers().get(0);
if (this.isVehicle()) { if (this.isVehicle() && entity != null) {
this.setYRot(entity.getYRot()); this.setYRot(entity.getYRot());
this.yRotO = this.getYRot(); this.yRotO = this.getYRot();
this.setXRot(Mth.clamp(entity.getXRot() - 1f, -30, 4)); this.setXRot(Mth.clamp(entity.getXRot() - 1f, -30, 4));
@ -359,7 +355,6 @@ public class Mle1934Entity extends PathfinderMob implements GeoEntity {
super.travel(dir); super.travel(dir);
} }
@Override @Override
public EntityDimensions getDimensions(Pose p_33597_) { public EntityDimensions getDimensions(Pose p_33597_) {
return super.getDimensions(p_33597_).scale((float) 1); return super.getDimensions(p_33597_).scale((float) 1);
@ -457,7 +452,7 @@ public class Mle1934Entity extends PathfinderMob implements GeoEntity {
if (entity instanceof Mle1934Entity mle1934) { if (entity instanceof Mle1934Entity mle1934) {
if (mle1934.getFirstPassenger() == null) return; if (mle1934.getFirstPassenger() == null) return;
Entity gunner = mle1934.getFirstPassenger(); Entity gunner = mle1934.getFirstPassenger();
if (event.getSource().getDirectEntity() == gunner){ if (event.getSource().getDirectEntity() == gunner) {
event.setCanceled(true); event.setCanceled(true);
} }
} }

View file

@ -1,9 +1,7 @@
package net.mcreator.superbwarfare.network.message; package net.mcreator.superbwarfare.network.message;
import net.mcreator.superbwarfare.entity.Mk42Entity; import net.mcreator.superbwarfare.entity.ICannonEntity;
import net.mcreator.superbwarfare.entity.Mle1934Entity;
import net.minecraft.network.FriendlyByteBuf; import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.player.Player; import net.minecraft.world.entity.player.Player;
import net.minecraft.world.level.Level; import net.minecraft.world.level.Level;
import net.minecraftforge.network.NetworkEvent; import net.minecraftforge.network.NetworkEvent;
@ -36,20 +34,14 @@ public class VehicleFireMessage {
} }
public static void pressAction(Player player, int type) { public static void pressAction(Player player, int type) {
Level world = player.level(); Level level = player.level();
if (!world.isLoaded(player.blockPosition())) { if (!level.isLoaded(player.blockPosition())) {
return; return;
} }
if (player.getVehicle() != null && (player.getVehicle() instanceof Mk42Entity || player.getVehicle() instanceof Mle1934Entity)) { if (player.getVehicle() instanceof ICannonEntity entity) {
Entity cannon = player.getVehicle(); entity.cannonShoot(player);
cannon.getPersistentData().putBoolean("firing",true);
if (type == 0) {
cannon.getPersistentData().putBoolean("firing",true);
} else if (type == 1) {
cannon.getPersistentData().putBoolean("firing",false);
}
} }
} }
} }