diff --git a/src/main/java/net/mcreator/superbwarfare/client/ClickHandler.java b/src/main/java/net/mcreator/superbwarfare/client/ClickHandler.java index c3d5409ab..105737de8 100644 --- a/src/main/java/net/mcreator/superbwarfare/client/ClickHandler.java +++ b/src/main/java/net/mcreator/superbwarfare/client/ClickHandler.java @@ -43,7 +43,7 @@ public class ClickHandler { int button = event.getButton(); if (button == GLFW.GLFW_MOUSE_BUTTON_LEFT) { 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 (Minecraft.getInstance().player.hasEffect(ModMobEffects.SHOCK.get())) { diff --git a/src/main/java/net/mcreator/superbwarfare/entity/ICannonEntity.java b/src/main/java/net/mcreator/superbwarfare/entity/ICannonEntity.java new file mode 100644 index 000000000..5a9ffd98c --- /dev/null +++ b/src/main/java/net/mcreator/superbwarfare/entity/ICannonEntity.java @@ -0,0 +1,7 @@ +package net.mcreator.superbwarfare.entity; + +import net.minecraft.world.entity.player.Player; + +public interface ICannonEntity { + void cannonShoot(Player player); +} diff --git a/src/main/java/net/mcreator/superbwarfare/entity/Mk42Entity.java b/src/main/java/net/mcreator/superbwarfare/entity/Mk42Entity.java index e8827f262..99e0a167c 100644 --- a/src/main/java/net/mcreator/superbwarfare/entity/Mk42Entity.java +++ b/src/main/java/net/mcreator/superbwarfare/entity/Mk42Entity.java @@ -47,7 +47,7 @@ import software.bernie.geckolib.core.animation.RawAnimation; import software.bernie.geckolib.core.object.PlayState; 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 SHOOT = SynchedEntityData.defineId(Mk42Entity.class, EntityDataSerializers.BOOLEAN); public static final EntityDataAccessor ANIMATION = SynchedEntityData.defineId(Mk42Entity.class, EntityDataSerializers.STRING); public static final EntityDataAccessor TEXTURE = SynchedEntityData.defineId(Mk42Entity.class, EntityDataSerializers.STRING); @@ -135,18 +135,16 @@ public class Mk42Entity extends PathfinderMob implements GeoEntity { return super.hurt(source, amount - 32); } - @Override - public InteractionResult mobInteract(Player sourceentity, InteractionHand hand) { - InteractionResult retval = InteractionResult.sidedSuccess(this.level().isClientSide()); - super.mobInteract(sourceentity, hand); - sourceentity.setXRot(this.getXRot()); - sourceentity.setYRot(this.getYRot()); - sourceentity.startRiding(this); - return retval; + public InteractionResult mobInteract(Player player, InteractionHand hand) { + InteractionResult result = InteractionResult.sidedSuccess(this.level().isClientSide()); + super.mobInteract(player, hand); + player.setXRot(this.getXRot()); + player.setYRot(this.getYRot()); + player.startRiding(this); + return result; } - @Override public void die(DamageSource source) { super.die(source); @@ -189,11 +187,11 @@ public class Mk42Entity extends PathfinderMob implements GeoEntity { Entity gunner = this.getFirstPassenger(); - if (this.getPersistentData().getInt("fire_cooldown") > 0) { - this.getPersistentData().putInt("fire_cooldown", this.getPersistentData().getInt("fire_cooldown") - 1); + if (this.getPersistentData().getInt("FireCooldown") > 0) { + 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 -> { 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(); } + @Override public void cannonShoot(Player player) { + if (this.getPersistentData().getInt("FireCooldown") > 0) { + return; + } + Level level = player.level(); if (level instanceof ServerLevel server) { 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); level.addFreshEntity(entityToSpawn); - if (player instanceof ServerPlayer serverPlayer) { SoundTool.playLocalSound(serverPlayer, ModSounds.MK_42_FIRE_1P.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); } - this.getPersistentData().putInt("fire_cooldown", 30); + this.getPersistentData().putInt("FireCooldown", 30); server.sendParticles(ParticleTypes.CAMPFIRE_COSY_SMOKE, this.getX() + 5 * this.getLookAngle().x, @@ -319,14 +317,13 @@ public class Mk42Entity extends PathfinderMob implements GeoEntity { this.getEyeY() + 13 * this.getLookAngle().y, this.getZ() + 13 * this.getLookAngle().z, 1, 0.15, 0.15, 0.15, 0.0075); - } } @Override public void travel(Vec3 dir) { Entity entity = this.getPassengers().isEmpty() ? null : this.getPassengers().get(0); - if (this.isVehicle()) { + if (this.isVehicle() && entity != null) { this.setYRot(entity.getYRot()); this.yRotO = this.getYRot(); this.setXRot(Mth.clamp(entity.getXRot() - 1.35f, -85, 15)); @@ -354,7 +351,6 @@ public class Mk42Entity extends PathfinderMob implements GeoEntity { super.travel(dir); } - @Override public EntityDimensions getDimensions(Pose p_33597_) { 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 (mk42.getFirstPassenger() == null) return; Entity gunner = mk42.getFirstPassenger(); - if (event.getSource().getDirectEntity() == gunner){ + if (event.getSource().getDirectEntity() == gunner) { event.setCanceled(true); } } diff --git a/src/main/java/net/mcreator/superbwarfare/entity/Mle1934Entity.java b/src/main/java/net/mcreator/superbwarfare/entity/Mle1934Entity.java index c04137c59..5e677a353 100644 --- a/src/main/java/net/mcreator/superbwarfare/entity/Mle1934Entity.java +++ b/src/main/java/net/mcreator/superbwarfare/entity/Mle1934Entity.java @@ -48,7 +48,7 @@ import software.bernie.geckolib.core.animation.RawAnimation; import software.bernie.geckolib.core.object.PlayState; 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 ANIMATION = SynchedEntityData.defineId(Mle1934Entity.class, EntityDataSerializers.STRING); private final AnimatableInstanceCache cache = GeckoLibUtil.createInstanceCache(this); @@ -124,18 +124,16 @@ public class Mle1934Entity extends PathfinderMob implements GeoEntity { return super.hurt(source, amount - 34); } - @Override - public InteractionResult mobInteract(Player sourceentity, InteractionHand hand) { - InteractionResult retval = InteractionResult.sidedSuccess(this.level().isClientSide()); - super.mobInteract(sourceentity, hand); - sourceentity.setXRot(this.getXRot()); - sourceentity.setYRot(this.getYRot()); - sourceentity.startRiding(this); - return retval; + public InteractionResult mobInteract(Player player, InteractionHand hand) { + InteractionResult result = InteractionResult.sidedSuccess(this.level().isClientSide()); + super.mobInteract(player, hand); + player.setXRot(this.getXRot()); + player.setYRot(this.getYRot()); + player.startRiding(this); + return result; } - @Override public void die(DamageSource source) { super.die(source); @@ -175,25 +173,26 @@ public class Mle1934Entity extends PathfinderMob implements GeoEntity { Entity gunner = this.getFirstPassenger(); - if (this.getPersistentData().getInt("fire_cooldown") > 0) { - this.getPersistentData().putInt("fire_cooldown", this.getPersistentData().getInt("fire_cooldown") - 1); + if (this.getPersistentData().getInt("FireCooldown") > 0) { + 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 -> { capability.cannonRecoil = 10; capability.syncPlayerVariables(gunner); }); } - if (this.getPersistentData().getBoolean("firing") && gunner instanceof Player player && this.getPersistentData().getInt("fire_cooldown") == 0) { - cannonShoot(player); - } - this.refreshDimensions(); } + @Override public void cannonShoot(Player player) { + if (this.getPersistentData().getInt("FireCooldown") > 0) { + return; + } + Level level = player.level(); if (level instanceof ServerLevel server) { ItemStack stack = player.getMainHandItem(); @@ -230,7 +229,7 @@ public class Mle1934Entity extends PathfinderMob implements GeoEntity { } if (!player.isCreative()) { - stack.shrink(salvoShoot? 2 : 1); + stack.shrink(salvoShoot ? 2 : 1); } // TODO 将炮弹生成在正确的位置(现在均在中心) @@ -261,7 +260,6 @@ public class Mle1934Entity extends PathfinderMob implements GeoEntity { }); } - if (player instanceof ServerPlayer serverPlayer) { SoundTool.playLocalSound(serverPlayer, ModSounds.MK_42_FIRE_1P.get(), 2, 1); 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); } - this.getPersistentData().putInt("fire_cooldown", 74); + this.getPersistentData().putInt("FireCooldown", 74); server.sendParticles(ParticleTypes.CAMPFIRE_COSY_SMOKE, 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.CAMPFIRE_COSY_SMOKE, this.getX() + 9.5 * this.getLookAngle().x, 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.getZ() + 13 * this.getLookAngle().z, 1, 0.15, 0.15, 0.15, 0.0075); - } } @Override public void travel(Vec3 dir) { Entity entity = this.getPassengers().isEmpty() ? null : this.getPassengers().get(0); - if (this.isVehicle()) { + if (this.isVehicle() && entity != null) { this.setYRot(entity.getYRot()); this.yRotO = this.getYRot(); this.setXRot(Mth.clamp(entity.getXRot() - 1f, -30, 4)); @@ -359,7 +355,6 @@ public class Mle1934Entity extends PathfinderMob implements GeoEntity { super.travel(dir); } - @Override public EntityDimensions getDimensions(Pose p_33597_) { 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 (mle1934.getFirstPassenger() == null) return; Entity gunner = mle1934.getFirstPassenger(); - if (event.getSource().getDirectEntity() == gunner){ + if (event.getSource().getDirectEntity() == gunner) { event.setCanceled(true); } } diff --git a/src/main/java/net/mcreator/superbwarfare/network/message/VehicleFireMessage.java b/src/main/java/net/mcreator/superbwarfare/network/message/VehicleFireMessage.java index ed16c8b27..53375644a 100644 --- a/src/main/java/net/mcreator/superbwarfare/network/message/VehicleFireMessage.java +++ b/src/main/java/net/mcreator/superbwarfare/network/message/VehicleFireMessage.java @@ -1,9 +1,7 @@ package net.mcreator.superbwarfare.network.message; -import net.mcreator.superbwarfare.entity.Mk42Entity; -import net.mcreator.superbwarfare.entity.Mle1934Entity; +import net.mcreator.superbwarfare.entity.ICannonEntity; import net.minecraft.network.FriendlyByteBuf; -import net.minecraft.world.entity.Entity; import net.minecraft.world.entity.player.Player; import net.minecraft.world.level.Level; import net.minecraftforge.network.NetworkEvent; @@ -36,20 +34,14 @@ public class VehicleFireMessage { } 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; } - if (player.getVehicle() != null && (player.getVehicle() instanceof Mk42Entity || player.getVehicle() instanceof Mle1934Entity)) { - Entity cannon = player.getVehicle(); - cannon.getPersistentData().putBoolean("firing",true); - if (type == 0) { - cannon.getPersistentData().putBoolean("firing",true); - } else if (type == 1) { - cannon.getPersistentData().putBoolean("firing",false); - } + if (player.getVehicle() instanceof ICannonEntity entity) { + entity.cannonShoot(player); } } }