diff --git a/src/main/java/com/atsuishio/superbwarfare/client/overlay/SpyglassRangeOverlay.java b/src/main/java/com/atsuishio/superbwarfare/client/overlay/SpyglassRangeOverlay.java index aabbf5423..02e871a48 100644 --- a/src/main/java/com/atsuishio/superbwarfare/client/overlay/SpyglassRangeOverlay.java +++ b/src/main/java/com/atsuishio/superbwarfare/client/overlay/SpyglassRangeOverlay.java @@ -35,7 +35,7 @@ import net.neoforged.api.distmarker.OnlyIn; import javax.annotation.ParametersAreNonnullByDefault; import static com.atsuishio.superbwarfare.client.RenderHelper.preciseBlit; -import static com.atsuishio.superbwarfare.item.ArtilleryIndicator.TAG_MORTARS; +import static com.atsuishio.superbwarfare.item.ArtilleryIndicator.TAG_CANNON; @OnlyIn(Dist.CLIENT) public class SpyglassRangeOverlay implements LayeredDraw.Layer { @@ -99,7 +99,7 @@ public class SpyglassRangeOverlay implements LayeredDraw.Layer { // 火炮位置 - ListTag tags = NBTTool.getTag(stack).getList(TAG_MORTARS, Tag.TAG_COMPOUND); + ListTag tags = NBTTool.getTag(stack).getList(TAG_CANNON, Tag.TAG_COMPOUND); for (int m = 0; m < tags.size(); m++) { var tag = tags.getCompound(m); Entity entity = EntityFindUtil.findEntity(player.level(), tag.getString("UUID")); diff --git a/src/main/java/com/atsuishio/superbwarfare/entity/projectile/CannonShellEntity.java b/src/main/java/com/atsuishio/superbwarfare/entity/projectile/CannonShellEntity.java index 42ab655aa..8cdfde992 100644 --- a/src/main/java/com/atsuishio/superbwarfare/entity/projectile/CannonShellEntity.java +++ b/src/main/java/com/atsuishio/superbwarfare/entity/projectile/CannonShellEntity.java @@ -163,6 +163,9 @@ public class CannonShellEntity extends FastThrowableProjectile implements GeoEnt } this.level().destroyBlock(resultPos, true); } + } else { + causeExplode(blockHitResult.getLocation()); + this.discard(); } if (!ExplosionConfig.EXPLOSION_DESTROY.get()) { causeExplode(blockHitResult.getLocation()); 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 2a1e02316..bc02fa7d7 100644 --- a/src/main/java/com/atsuishio/superbwarfare/entity/vehicle/Mk42Entity.java +++ b/src/main/java/com/atsuishio/superbwarfare/entity/vehicle/Mk42Entity.java @@ -14,6 +14,7 @@ import com.atsuishio.superbwarfare.event.ClientMouseHandler; import com.atsuishio.superbwarfare.init.ModDamageTypes; import com.atsuishio.superbwarfare.init.ModItems; import com.atsuishio.superbwarfare.init.ModSounds; +import com.atsuishio.superbwarfare.item.ArtilleryIndicator; import com.atsuishio.superbwarfare.item.common.ammo.CannonShellItem; import com.atsuishio.superbwarfare.network.message.receive.ShakeClientMessage; import com.atsuishio.superbwarfare.tools.CustomExplosion; @@ -30,13 +31,17 @@ import net.minecraft.network.syncher.SynchedEntityData; import net.minecraft.resources.ResourceLocation; import net.minecraft.server.level.ServerLevel; import net.minecraft.server.level.ServerPlayer; +import net.minecraft.server.players.OldUsersConverter; +import net.minecraft.sounds.SoundEvents; import net.minecraft.sounds.SoundSource; import net.minecraft.util.Mth; +import net.minecraft.world.Container; import net.minecraft.world.InteractionHand; import net.minecraft.world.InteractionResult; import net.minecraft.world.entity.Entity; import net.minecraft.world.entity.EntityType; import net.minecraft.world.entity.MoverType; +import net.minecraft.world.entity.OwnableEntity; import net.minecraft.world.entity.player.Player; import net.minecraft.world.item.ItemStack; import net.minecraft.world.level.Explosion; @@ -56,14 +61,18 @@ import software.bernie.geckolib.animatable.instance.AnimatableInstanceCache; import software.bernie.geckolib.animation.*; import software.bernie.geckolib.util.GeckoLibUtil; +import java.util.Optional; +import java.util.UUID; + import static com.atsuishio.superbwarfare.tools.RangeTool.calculateLaunchVector; -public class Mk42Entity extends VehicleEntity implements GeoEntity, CannonEntity { +public class Mk42Entity extends VehicleEntity implements GeoEntity, CannonEntity, Container, OwnableEntity { public static final EntityDataAccessor COOL_DOWN = SynchedEntityData.defineId(Mk42Entity.class, EntityDataSerializers.INT); private final AnimatableInstanceCache cache = GeckoLibUtil.createInstanceCache(this); public static final EntityDataAccessor PITCH = SynchedEntityData.defineId(Mk42Entity.class, EntityDataSerializers.FLOAT); public static final EntityDataAccessor YAW = SynchedEntityData.defineId(Mk42Entity.class, EntityDataSerializers.FLOAT); + public static final EntityDataAccessor> OWNER_UUID = SynchedEntityData.defineId(Mk42Entity.class, EntityDataSerializers.OPTIONAL_UUID); private final float shellGravity = 0.1f; @@ -71,12 +80,16 @@ public class Mk42Entity extends VehicleEntity implements GeoEntity, CannonEntity super(type, world); } + // TODO cap + public ItemStack stack = ItemStack.EMPTY; + @Override protected void defineSynchedData(SynchedEntityData.Builder builder) { super.defineSynchedData(builder); builder.define(COOL_DOWN, 0) .define(PITCH, 0F) - .define(YAW, 0F); + .define(YAW, 0F) + .define(OWNER_UUID, Optional.empty()); } @Override @@ -116,6 +129,9 @@ public class Mk42Entity extends VehicleEntity implements GeoEntity, CannonEntity compound.putInt("CoolDown", this.entityData.get(COOL_DOWN)); compound.putFloat("Pitch", this.entityData.get(PITCH)); compound.putFloat("Yaw", this.entityData.get(YAW)); + if (this.getOwnerUUID() != null) { + compound.putUUID("Owner", this.getOwnerUUID()); + } } @Override @@ -124,12 +140,64 @@ public class Mk42Entity extends VehicleEntity implements GeoEntity, CannonEntity this.entityData.set(COOL_DOWN, compound.getInt("CoolDown")); this.entityData.set(PITCH, compound.getFloat("Pitch")); this.entityData.set(YAW, compound.getFloat("Yaw")); + UUID uuid; + if (compound.hasUUID("Owner")) { + uuid = compound.getUUID("Owner"); + } else { + String s = compound.getString("Owner"); + + assert this.getServer() != null; + uuid = OldUsersConverter.convertMobOwnerIfNecessary(this.getServer(), s); + } + + if (uuid != null) { + try { + this.setOwnerUUID(uuid); + } catch (Throwable ignored) { + } + } } + public void setOwnerUUID(@Nullable UUID pUuid) { + this.entityData.set(OWNER_UUID, Optional.ofNullable(pUuid)); + } + + @Nullable + public UUID getOwnerUUID() { + return this.entityData.get(OWNER_UUID).orElse(null); + } + + @Override public @NotNull InteractionResult interact(Player player, @NotNull InteractionHand hand) { ItemStack stack = player.getMainHandItem(); + if (player.isShiftKeyDown() && this.getOwner() == null) { + setOwnerUUID(player.getUUID()); + if (player instanceof ServerPlayer serverPlayer) { + serverPlayer.level().playSound(null, serverPlayer.getOnPos(), SoundEvents.ARROW_HIT_PLAYER, SoundSource.PLAYERS, 0.5F, 1); + } + return InteractionResult.SUCCESS; + } + + if (stack.getItem() instanceof ArtilleryIndicator indicator && player == getOwner() && this.getOwner() == player) { + if (indicator.addCannon(stack, getStringUUID())) { + if (player instanceof ServerPlayer serverPlayer) { + serverPlayer.level().playSound(null, serverPlayer.getOnPos(), SoundEvents.ARROW_HIT_PLAYER, SoundSource.PLAYERS, 0.5F, 1); + } + player.displayClientMessage(Component.literal("added"), true); + return InteractionResult.SUCCESS; + } else if (indicator.removeCannon(stack, getStringUUID())) { + if (player instanceof ServerPlayer serverPlayer) { + serverPlayer.level().playSound(null, serverPlayer.getOnPos(), SoundEvents.ARROW_HIT_PLAYER, SoundSource.PLAYERS, 0.5F, 1); + } + player.displayClientMessage(Component.literal("removed"), true); + return InteractionResult.SUCCESS; + } else { + return InteractionResult.FAIL; + } + } + if (stack.getItem() instanceof CannonShellItem) { if (this.entityData.get(COOL_DOWN) == 0) { var weaponType = stack.is(ModItems.AP_5_INCHES.get()) ? 0 : 1; @@ -175,7 +243,8 @@ public class Mk42Entity extends VehicleEntity implements GeoEntity, CannonEntity Vec3 shootPos = new Vec3(worldPosition.x, worldPosition.y, worldPosition.z); try { - Vec3 launchVector = calculateLaunchVector(shootPos, new Vec3(targetX, targetY, targetZ), 15, -shellGravity, isDepressed); + double adjust = 0.004 * new Vec3(targetX, targetY, targetZ).distanceTo(shootPos); + Vec3 launchVector = calculateLaunchVector(shootPos, new Vec3(targetX, targetY - adjust, targetZ), 15, -shellGravity, isDepressed); this.look(new Vec3(targetX, targetY, targetZ)); float angle = (float) -getXRotFromVector(launchVector); if (angle < -85 || angle > 14.9) { @@ -185,7 +254,34 @@ public class Mk42Entity extends VehicleEntity implements GeoEntity, CannonEntity } catch (Exception e) { return false; } + return true; + } + public boolean setTarget(ItemStack stack, boolean isDepressed) { + var parameters = stack.get(ModDataComponents.FIRING_PARAMETERS); + if (parameters == null) return false; + + var pos = parameters.pos(); + double targetX = pos.getX(); + double targetY = pos.getY(); + double targetZ = pos.getZ(); + + Matrix4f transform = getVehicleFlatTransform(1); + Vector4f worldPosition = transformPosition(transform, 0f, 2.16f, 0.5175f); + Vec3 shootPos = new Vec3(worldPosition.x, worldPosition.y, worldPosition.z); + + try { + double adjust = 0.004 * new Vec3(targetX, targetY, targetZ).distanceTo(shootPos); + Vec3 launchVector = calculateLaunchVector(shootPos, new Vec3(targetX, targetY - adjust, targetZ), 15, -shellGravity, isDepressed); + this.look(new Vec3(targetX, targetY, targetZ)); + float angle = (float) -getXRotFromVector(launchVector); + if (angle < -85 || angle > 14.9) { + return false; + } + entityData.set(PITCH, angle); + } catch (Exception e) { + return false; + } return true; } @@ -305,6 +401,10 @@ public class Mk42Entity extends VehicleEntity implements GeoEntity, CannonEntity InventoryTool.consumeItem(player.getInventory().items, ammo, 1); } + if (getFirstPassenger() != getOwner()) { + this.stack = ItemStack.EMPTY; + } + var entityToSpawn = ((CannonShellWeapon) getWeapon(0)).create(player); Matrix4f transform = getVehicleFlatTransform(1); @@ -315,11 +415,16 @@ public class Mk42Entity extends VehicleEntity implements GeoEntity, CannonEntity level.addFreshEntity(entityToSpawn); if (player instanceof ServerPlayer serverPlayer) { - SoundTool.playLocalSound(serverPlayer, ModSounds.MK_42_FIRE_1P.get(), 2, 1); - SoundTool.playLocalSound(serverPlayer, ModSounds.CANNON_RELOAD.get(), 2, 1); - serverPlayer.level().playSound(null, serverPlayer.getOnPos(), ModSounds.MK_42_FIRE_3P.get(), SoundSource.PLAYERS, 6, 1); - serverPlayer.level().playSound(null, serverPlayer.getOnPos(), ModSounds.MK_42_FAR.get(), SoundSource.PLAYERS, 16, 1); - serverPlayer.level().playSound(null, serverPlayer.getOnPos(), ModSounds.MK_42_VERYFAR.get(), SoundSource.PLAYERS, 32, 1); + if (player == getFirstPassenger()) { + SoundTool.playLocalSound(serverPlayer, ModSounds.MK_42_FIRE_1P.get(), 2, 1); + SoundTool.playLocalSound(serverPlayer, ModSounds.CANNON_RELOAD.get(), 2, 1); + } + } + + if (!this.level().isClientSide()) { + this.level().playSound(null, this.getX(), this.getY(), this.getZ(), ModSounds.MK_42_FIRE_3P.get(), SoundSource.PLAYERS, 24f, 1f); + this.level().playSound(null, this.getX(), this.getY(), this.getZ(), ModSounds.MK_42_FAR.get(), SoundSource.PLAYERS, 48f, 1f); + this.level().playSound(null, this.getX(), this.getY(), this.getZ(), ModSounds.MK_42_VERYFAR.get(), SoundSource.PLAYERS, 96f, 1f); } this.entityData.set(COOL_DOWN, 30); @@ -483,4 +588,70 @@ public class Mk42Entity extends VehicleEntity implements GeoEntity, CannonEntity public @Nullable ResourceLocation getVehicleItemIcon() { return Mod.loc("textures/gui/vehicle/type/defense.png"); } + + @Override + public int getContainerSize() { + return 1; + } + + @Override + public int getMaxStackSize() { + return 1; + } + + @Override + public boolean isEmpty() { + return stack == ItemStack.EMPTY; + } + + @Override + public @NotNull ItemStack getItem(int slot) { + return slot == 0 ? stack : ItemStack.EMPTY; + } + + @Override + public @NotNull ItemStack removeItem(int slot, int amount) { + if (slot != 0 || amount <= 0 || stack.isEmpty()) { + return ItemStack.EMPTY; + } + stack.shrink(1); + if (stack.isEmpty()) { + stack = ItemStack.EMPTY; + } + return stack; + } + + @Override + public @NotNull ItemStack removeItemNoUpdate(int slot) { + return removeItem(0, 1); + } + + @Override + public void setItem(int slot, @NotNull ItemStack stack) { + if (slot != 0) return; + this.stack = stack; + } + + @Override + public void setChanged() { +// if (!entityData.get(INTELLIGENT)) { +// fire(null); +// } + } + + @Override + public boolean stillValid(@NotNull Player player) { + return false; + } + + @Override + public void clearContent() { + this.stack = ItemStack.EMPTY; + } + + @Override + public boolean canPlaceItem(int slot, @NotNull ItemStack stack) { + if (slot != 0 || this.entityData.get(COOL_DOWN) != 0) return false; + return stack.getItem() instanceof CannonShellItem; + } } 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 b0160330a..b72c702f7 100644 --- a/src/main/java/com/atsuishio/superbwarfare/entity/vehicle/Mle1934Entity.java +++ b/src/main/java/com/atsuishio/superbwarfare/entity/vehicle/Mle1934Entity.java @@ -14,6 +14,7 @@ import com.atsuishio.superbwarfare.event.ClientMouseHandler; import com.atsuishio.superbwarfare.init.ModDamageTypes; import com.atsuishio.superbwarfare.init.ModItems; import com.atsuishio.superbwarfare.init.ModSounds; +import com.atsuishio.superbwarfare.item.ArtilleryIndicator; import com.atsuishio.superbwarfare.item.common.ammo.CannonShellItem; import com.atsuishio.superbwarfare.network.message.receive.ShakeClientMessage; import com.atsuishio.superbwarfare.tools.CustomExplosion; @@ -30,13 +31,17 @@ import net.minecraft.network.syncher.SynchedEntityData; import net.minecraft.resources.ResourceLocation; import net.minecraft.server.level.ServerLevel; import net.minecraft.server.level.ServerPlayer; +import net.minecraft.server.players.OldUsersConverter; +import net.minecraft.sounds.SoundEvents; import net.minecraft.sounds.SoundSource; import net.minecraft.util.Mth; +import net.minecraft.world.Container; import net.minecraft.world.InteractionHand; import net.minecraft.world.InteractionResult; import net.minecraft.world.entity.Entity; import net.minecraft.world.entity.EntityType; import net.minecraft.world.entity.MoverType; +import net.minecraft.world.entity.OwnableEntity; import net.minecraft.world.entity.player.Player; import net.minecraft.world.item.ItemStack; import net.minecraft.world.level.Explosion; @@ -56,21 +61,30 @@ import software.bernie.geckolib.animatable.instance.AnimatableInstanceCache; import software.bernie.geckolib.animation.*; import software.bernie.geckolib.util.GeckoLibUtil; +import java.util.Optional; +import java.util.UUID; + import static com.atsuishio.superbwarfare.tools.RangeTool.calculateLaunchVector; -public class Mle1934Entity extends VehicleEntity implements GeoEntity, CannonEntity { +public class Mle1934Entity extends VehicleEntity implements GeoEntity, CannonEntity, Container, OwnableEntity { public static final EntityDataAccessor COOL_DOWN = SynchedEntityData.defineId(Mle1934Entity.class, EntityDataSerializers.INT); public static final EntityDataAccessor TYPE = SynchedEntityData.defineId(Mle1934Entity.class, EntityDataSerializers.INT); public static final EntityDataAccessor PITCH = SynchedEntityData.defineId(Mle1934Entity.class, EntityDataSerializers.FLOAT); public static final EntityDataAccessor YAW = SynchedEntityData.defineId(Mle1934Entity.class, EntityDataSerializers.FLOAT); private final AnimatableInstanceCache cache = GeckoLibUtil.createInstanceCache(this); + + public static final EntityDataAccessor> OWNER_UUID = SynchedEntityData.defineId(Mle1934Entity.class, EntityDataSerializers.OPTIONAL_UUID); + private final float shellGravity = 0.1f; public Mle1934Entity(EntityType type, Level world) { super(type, world); } + // TODO cap + public ItemStack stack = ItemStack.EMPTY; + @Override public VehicleWeapon[][] initWeapons() { return new VehicleWeapon[][]{ @@ -108,7 +122,8 @@ public class Mle1934Entity extends VehicleEntity implements GeoEntity, CannonEnt builder.define(COOL_DOWN, 0) .define(TYPE, 0) .define(PITCH, 0f) - .define(YAW, 0f); + .define(YAW, 0f) + .define(OWNER_UUID, Optional.empty()); } @Override @@ -118,6 +133,9 @@ public class Mle1934Entity extends VehicleEntity implements GeoEntity, CannonEnt compound.putInt("Type", this.entityData.get(TYPE)); compound.putFloat("Pitch", this.entityData.get(PITCH)); compound.putFloat("Yaw", this.entityData.get(YAW)); + if (this.getOwnerUUID() != null) { + compound.putUUID("Owner", this.getOwnerUUID()); + } } @Override @@ -127,12 +145,64 @@ public class Mle1934Entity extends VehicleEntity implements GeoEntity, CannonEnt this.entityData.set(TYPE, compound.getInt("Type")); this.entityData.set(PITCH, compound.getFloat("Pitch")); this.entityData.set(YAW, compound.getFloat("Yaw")); + UUID uuid; + if (compound.hasUUID("Owner")) { + uuid = compound.getUUID("Owner"); + } else { + String s = compound.getString("Owner"); + + assert this.getServer() != null; + uuid = OldUsersConverter.convertMobOwnerIfNecessary(this.getServer(), s); + } + + if (uuid != null) { + try { + this.setOwnerUUID(uuid); + } catch (Throwable ignored) { + } + } } + public void setOwnerUUID(@Nullable UUID pUuid) { + this.entityData.set(OWNER_UUID, Optional.ofNullable(pUuid)); + } + + @Nullable + public UUID getOwnerUUID() { + return this.entityData.get(OWNER_UUID).orElse(null); + } + + @Override public @NotNull InteractionResult interact(Player player, @NotNull InteractionHand hand) { ItemStack stack = player.getMainHandItem(); + if (player.isShiftKeyDown() && this.getOwner() == null) { + setOwnerUUID(player.getUUID()); + if (player instanceof ServerPlayer serverPlayer) { + serverPlayer.level().playSound(null, serverPlayer.getOnPos(), SoundEvents.ARROW_HIT_PLAYER, SoundSource.PLAYERS, 0.5F, 1); + } + return InteractionResult.SUCCESS; + } + + if (stack.getItem() instanceof ArtilleryIndicator indicator && player == getOwner() && this.getOwner() == player) { + if (indicator.addCannon(stack, getStringUUID())) { + if (player instanceof ServerPlayer serverPlayer) { + serverPlayer.level().playSound(null, serverPlayer.getOnPos(), SoundEvents.ARROW_HIT_PLAYER, SoundSource.PLAYERS, 0.5F, 1); + } + player.displayClientMessage(Component.literal("added"), true); + return InteractionResult.SUCCESS; + } else if (indicator.removeCannon(stack, getStringUUID())) { + if (player instanceof ServerPlayer serverPlayer) { + serverPlayer.level().playSound(null, serverPlayer.getOnPos(), SoundEvents.ARROW_HIT_PLAYER, SoundSource.PLAYERS, 0.5F, 1); + } + player.displayClientMessage(Component.literal("removed"), true); + return InteractionResult.SUCCESS; + } else { + return InteractionResult.FAIL; + } + } + if (stack.getItem() instanceof CannonShellItem) { if (this.entityData.get(COOL_DOWN) == 0) { var weaponType = stack.is(ModItems.AP_5_INCHES.get()) ? 0 : 1; @@ -179,7 +249,8 @@ public class Mle1934Entity extends VehicleEntity implements GeoEntity, CannonEnt Vec3 shootPos = new Vec3(worldPosition.x, worldPosition.y, worldPosition.z); try { - Vec3 launchVector = calculateLaunchVector(shootPos, new Vec3(targetX, targetY, targetZ), 15, -shellGravity, isDepressed); + double adjust = 0.004 * new Vec3(targetX, targetY, targetZ).distanceTo(shootPos); + Vec3 launchVector = calculateLaunchVector(shootPos, new Vec3(targetX, targetY - adjust, targetZ), 15, -shellGravity, isDepressed); this.look(new Vec3(targetX, targetY, targetZ)); float angle = (float) -getXRotFromVector(launchVector); if (angle < -30 || angle > 2.7) { @@ -193,6 +264,34 @@ public class Mle1934Entity extends VehicleEntity implements GeoEntity, CannonEnt return true; } + public boolean setTarget(ItemStack stack, boolean isDepressed) { + var parameters = stack.get(ModDataComponents.FIRING_PARAMETERS); + if (parameters == null) return false; + + var pos = parameters.pos(); + double targetX = pos.getX(); + double targetY = pos.getY(); + double targetZ = pos.getZ(); + + Matrix4f transform = getVehicleFlatTransform(1); + Vector4f worldPosition = transformPosition(transform, 0f, 2.16f, 0.5175f); + Vec3 shootPos = new Vec3(worldPosition.x, worldPosition.y, worldPosition.z); + + try { + double adjust = 0.004 * new Vec3(targetX, targetY, targetZ).distanceTo(shootPos); + Vec3 launchVector = calculateLaunchVector(shootPos, new Vec3(targetX, targetY - adjust, targetZ), 15, -shellGravity, isDepressed); + this.look(new Vec3(targetX, targetY, targetZ)); + float angle = (float) -getXRotFromVector(launchVector); + if (angle < -85 || angle > 14.9) { + return false; + } + entityData.set(PITCH, angle); + } catch (Exception e) { + return false; + } + return true; + } + private void look(Vec3 pTarget) { Matrix4f transform = getVehicleFlatTransform(1); Vector4f worldPosition = transformPosition(transform, 0, 1.4992625f, 1.52065f); @@ -318,6 +417,10 @@ public class Mle1934Entity extends VehicleEntity implements GeoEntity, CannonEnt consumed = InventoryTool.consumeItem(player.getInventory().items, ammo, 2); } + if (getFirstPassenger() != getOwner()) { + this.stack = ItemStack.EMPTY; + } + boolean salvoShoot = consumed == 2; Matrix4f transform = getVehicleFlatTransform(1); @@ -392,11 +495,16 @@ public class Mle1934Entity extends VehicleEntity implements GeoEntity, CannonEnt } if (player instanceof ServerPlayer serverPlayer) { - SoundTool.playLocalSound(serverPlayer, ModSounds.MK_42_FIRE_1P.get(), 2, 1); - Mod.queueServerWork(44, () -> SoundTool.playLocalSound(serverPlayer, ModSounds.CANNON_RELOAD.get(), 2, 1)); - serverPlayer.level().playSound(null, serverPlayer.getOnPos(), ModSounds.MK_42_FIRE_3P.get(), SoundSource.PLAYERS, 6, 1); - serverPlayer.level().playSound(null, serverPlayer.getOnPos(), ModSounds.MK_42_FAR.get(), SoundSource.PLAYERS, 16, 1); - serverPlayer.level().playSound(null, serverPlayer.getOnPos(), ModSounds.MK_42_VERYFAR.get(), SoundSource.PLAYERS, 32, 1); + if (player == getFirstPassenger()) { + SoundTool.playLocalSound(serverPlayer, ModSounds.MK_42_FIRE_1P.get(), 2, 1); + Mod.queueServerWork(44, () -> SoundTool.playLocalSound(serverPlayer, ModSounds.CANNON_RELOAD.get(), 2, 1)); + } + } + + if (!this.level().isClientSide()) { + this.level().playSound(null, this.getX(), this.getY(), this.getZ(), ModSounds.MK_42_FIRE_3P.get(), SoundSource.PLAYERS, 24f, 1f); + this.level().playSound(null, this.getX(), this.getY(), this.getZ(), ModSounds.MK_42_FAR.get(), SoundSource.PLAYERS, 48f, 1f); + this.level().playSound(null, this.getX(), this.getY(), this.getZ(), ModSounds.MK_42_VERYFAR.get(), SoundSource.PLAYERS, 96f, 1f); } this.entityData.set(COOL_DOWN, 74); @@ -551,4 +659,71 @@ public class Mle1934Entity extends VehicleEntity implements GeoEntity, CannonEnt public @Nullable ResourceLocation getVehicleItemIcon() { return Mod.loc("textures/gui/vehicle/type/defense.png"); } + + @Override + public int getContainerSize() { + return 1; + } + + @Override + public int getMaxStackSize() { + return 2; + } + + @Override + public boolean isEmpty() { + return stack == ItemStack.EMPTY; + } + + @Override + public @NotNull ItemStack getItem(int slot) { + return slot == 0 ? stack : ItemStack.EMPTY; + } + + @Override + public @NotNull ItemStack removeItem(int slot, int amount) { + if (slot != 0 || amount <= 0 || stack.isEmpty()) { + return ItemStack.EMPTY; + } + stack.shrink(2); + if (stack.isEmpty()) { + stack = ItemStack.EMPTY; + } + return stack; + } + + @Override + public @NotNull ItemStack removeItemNoUpdate(int slot) { + return removeItem(0, 2); + } + + @Override + public void setItem(int slot, @NotNull ItemStack stack) { + if (slot != 0) return; + this.stack = stack; + } + + @Override + public void setChanged() { +// if (!entityData.get(INTELLIGENT)) { +// fire(null); +// } + } + + @Override + public boolean stillValid(@NotNull Player player) { + return false; + } + + @Override + public void clearContent() { + this.stack = ItemStack.EMPTY; + } + + @Override + public boolean canPlaceItem(int slot, @NotNull ItemStack stack) { + if (slot != 0 || this.entityData.get(COOL_DOWN) != 0) return false; + return stack.getItem() instanceof CannonShellItem; + } + } 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 b537e63c4..1fff093e0 100644 --- a/src/main/java/com/atsuishio/superbwarfare/entity/vehicle/MortarEntity.java +++ b/src/main/java/com/atsuishio/superbwarfare/entity/vehicle/MortarEntity.java @@ -160,13 +160,13 @@ public class MortarEntity extends VehicleEntity implements GeoEntity, Container, ItemStack mainHandItem = player.getMainHandItem(); if (mainHandItem.getItem() instanceof ArtilleryIndicator indicator && player == getOwner() && this.entityData.get(INTELLIGENT)) { - if (indicator.addMortar(mainHandItem, getStringUUID())) { + if (indicator.addCannon(mainHandItem, getStringUUID())) { if (player instanceof ServerPlayer serverPlayer) { serverPlayer.level().playSound(null, serverPlayer.getOnPos(), SoundEvents.ARROW_HIT_PLAYER, SoundSource.PLAYERS, 0.5F, 1); } player.displayClientMessage(Component.literal("added"), true); return InteractionResult.SUCCESS; - } else if (indicator.removeMortar(mainHandItem, getStringUUID())) { + } else if (indicator.removeCannon(mainHandItem, getStringUUID())) { if (player instanceof ServerPlayer serverPlayer) { serverPlayer.level().playSound(null, serverPlayer.getOnPos(), SoundEvents.ARROW_HIT_PLAYER, SoundSource.PLAYERS, 0.5F, 1); } @@ -180,6 +180,9 @@ public class MortarEntity extends VehicleEntity implements GeoEntity, Container, if (mainHandItem.getItem() instanceof Monitor && player.isShiftKeyDown() && !this.entityData.get(INTELLIGENT)) { setOwnerUUID(player.getUUID()); entityData.set(INTELLIGENT, true); + if (player instanceof ServerPlayer serverPlayer) { + serverPlayer.level().playSound(null, serverPlayer.getOnPos(), SoundEvents.ARROW_HIT_PLAYER, SoundSource.PLAYERS, 0.5F, 1); + } if (!player.isCreative()) { mainHandItem.shrink(1); } @@ -239,7 +242,7 @@ public class MortarEntity extends VehicleEntity implements GeoEntity, Container, var isDepressed = parameters.isDepressed(); try { - Vec3 launchVector = calculateLaunchVector(getEyePosition(), new Vec3(targetX, targetY, targetZ), 13, -0.11, isDepressed); + Vec3 launchVector = calculateLaunchVector(getEyePosition(), new Vec3(targetX, targetY - 1, targetZ), 13, -0.11, isDepressed); this.look(new Vec3(targetX, targetY, targetZ)); float angle = (float) -getXRotFromVector(launchVector); if (angle < -89 || angle > -20) { diff --git a/src/main/java/com/atsuishio/superbwarfare/init/ModCapabilities.java b/src/main/java/com/atsuishio/superbwarfare/init/ModCapabilities.java index b36d4ab4d..6a77d0296 100644 --- a/src/main/java/com/atsuishio/superbwarfare/init/ModCapabilities.java +++ b/src/main/java/com/atsuishio/superbwarfare/init/ModCapabilities.java @@ -38,7 +38,7 @@ public class ModCapabilities { // 充电站 event.registerBlockEntity(Capabilities.EnergyStorage.BLOCK, ModBlockEntities.CHARGING_STATION.value(), ChargingStationBlockEntity::getEnergyStorage); - // TODO 这注册对吗? + event.registerBlockEntity(Capabilities.ItemHandler.BLOCK, ModBlockEntities.CHARGING_STATION.value(), (object, context) -> { if (context == null || object.isRemoved()) return null; @@ -108,11 +108,25 @@ public class ModCapabilities { (object, context) -> new InvWrapper(object) ); + // 自动引导发射用 + // 迫击炮实体 event.registerEntity(Capabilities.ItemHandler.ENTITY, ModEntities.MORTAR.get(), (obj, ctx) -> new InvWrapper(obj) ); + // Mk42 + event.registerEntity(Capabilities.ItemHandler.ENTITY, + ModEntities.MK_42.get(), + (obj, ctx) -> new InvWrapper(obj) + ); + + // Mle1934 + event.registerEntity(Capabilities.ItemHandler.ENTITY, + ModEntities.MLE_1934.get(), + (obj, ctx) -> new InvWrapper(obj) + ); + } } diff --git a/src/main/java/com/atsuishio/superbwarfare/item/ArtilleryIndicator.java b/src/main/java/com/atsuishio/superbwarfare/item/ArtilleryIndicator.java index 73a53a2ba..a6fa5ecf7 100644 --- a/src/main/java/com/atsuishio/superbwarfare/item/ArtilleryIndicator.java +++ b/src/main/java/com/atsuishio/superbwarfare/item/ArtilleryIndicator.java @@ -22,7 +22,7 @@ import java.util.List; public class ArtilleryIndicator extends Item { - public static final String TAG_MORTARS = "Mortars"; + public static final String TAG_CANNON = "Cannons"; public ArtilleryIndicator() { super(new Properties().stacksTo(1).rarity(Rarity.UNCOMMON)); @@ -60,9 +60,9 @@ public class ArtilleryIndicator extends Item { pLivingEntity.playSound(SoundEvents.SPYGLASS_STOP_USING, 1.0F, 1.0F); } - public boolean addMortar(ItemStack stack, String uuid) { + public boolean addCannon(ItemStack stack, String uuid) { var tag = NBTTool.getTag(stack); - ListTag tags = tag.getList(TAG_MORTARS, Tag.TAG_COMPOUND); + ListTag tags = tag.getList(TAG_CANNON, Tag.TAG_COMPOUND); List list = new ArrayList<>(); for (int i = 0; i < tags.size(); i++) { list.add(tags.getCompound(i)); @@ -78,15 +78,15 @@ public class ArtilleryIndicator extends Item { ListTag listTag = new ListTag(); listTag.addAll(list); - tag.put(TAG_MORTARS, listTag); + tag.put(TAG_CANNON, listTag); NBTTool.saveTag(stack, tag); return true; } - public boolean removeMortar(ItemStack stack, String uuid) { + public boolean removeCannon(ItemStack stack, String uuid) { var tag = NBTTool.getTag(stack); - ListTag tags = tag.getList(TAG_MORTARS, Tag.TAG_COMPOUND); + ListTag tags = tag.getList(TAG_CANNON, Tag.TAG_COMPOUND); List list = new ArrayList<>(); boolean flag = false; for (int i = 0; i < tags.size(); i++) { @@ -100,7 +100,7 @@ public class ArtilleryIndicator extends Item { if (flag) { ListTag listTag = new ListTag(); listTag.addAll(list); - tag.put(TAG_MORTARS, listTag); + tag.put(TAG_CANNON, listTag); NBTTool.saveTag(stack, tag); } 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 1957e114e..8f1ffb414 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,35 +1,28 @@ package com.atsuishio.superbwarfare.network.message.send; import com.atsuishio.superbwarfare.Mod; -import com.atsuishio.superbwarfare.component.ModDataComponents; +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.init.ModItems; -import com.atsuishio.superbwarfare.init.ModSounds; -import com.atsuishio.superbwarfare.item.FiringParameters; +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 com.atsuishio.superbwarfare.tools.SoundTool; -import com.atsuishio.superbwarfare.tools.TraceTool; import io.netty.buffer.ByteBuf; -import net.minecraft.ChatFormatting; -import net.minecraft.core.BlockPos; import net.minecraft.nbt.ListTag; import net.minecraft.nbt.Tag; -import net.minecraft.network.chat.Component; import net.minecraft.network.codec.StreamCodec; import net.minecraft.network.protocol.common.custom.CustomPacketPayload; import net.minecraft.world.entity.Entity; import net.minecraft.world.entity.player.Player; import net.minecraft.world.item.ItemStack; -import net.minecraft.world.level.ClipContext; -import net.minecraft.world.phys.BlockHitResult; -import net.minecraft.world.phys.Vec3; import net.neoforged.neoforge.network.handling.IPayloadContext; import org.jetbrains.annotations.NotNull; -import java.util.Objects; - -import static com.atsuishio.superbwarfare.item.ArtilleryIndicator.TAG_MORTARS; +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 { INSTANCE; @@ -39,64 +32,39 @@ public enum ArtilleryIndicatorFireMessage implements CustomPacketPayload { public static void handler(final IPayloadContext context) { Player player = context.player(); - ItemStack stack = player.getOffhandItem(); - ItemStack mainStack = player.getMainHandItem(); - boolean lookAtEntity = false; - Entity lookingEntity = TraceTool.findLookingEntity(player, 520); + ItemStack stack = player.getMainHandItem(); - BlockHitResult result = player.level().clip(new ClipContext(player.getEyePosition(), player.getEyePosition().add(player.getViewVector(1).scale(512)), - ClipContext.Block.OUTLINE, ClipContext.Fluid.NONE, player)); - Vec3 hitPos = result.getLocation(); - - if (lookingEntity != null) { - lookAtEntity = true; - } - if (stack.is(ModItems.FIRING_PARAMETERS.get())) { - - var parameters = stack.get(ModDataComponents.FIRING_PARAMETERS); - var isDepressed = parameters != null && parameters.isDepressed(); - - if (lookAtEntity) { - stack.set(ModDataComponents.FIRING_PARAMETERS, new FiringParameters.Parameters(lookingEntity.blockPosition(), isDepressed)); - } else { - stack.set(ModDataComponents.FIRING_PARAMETERS, new FiringParameters.Parameters(new BlockPos((int) hitPos.x, (int) hitPos.y, (int) hitPos.z), isDepressed)); - } - - var pos = Objects.requireNonNull(stack.get(ModDataComponents.FIRING_PARAMETERS)).pos(); - - player.displayClientMessage(Component.translatable("tips.superbwarfare.mortar.target_pos") - .withStyle(ChatFormatting.GRAY) - .append(Component.literal("[" + pos.getX() - + "," + pos.getY() - + "," + pos.getZ() - + "]")), true); - } - - if (mainStack.is(ModItems.ARTILLERY_INDICATOR.get())) { - if (lookAtEntity) { - stack.set(ModDataComponents.FIRING_PARAMETERS, new FiringParameters.Parameters(lookingEntity.blockPosition(), false)); - } else { - stack.set(ModDataComponents.FIRING_PARAMETERS, new FiringParameters.Parameters(new BlockPos((int) hitPos.x, (int) hitPos.y, (int) hitPos.z), false)); - } - - var pos = Objects.requireNonNull(stack.get(ModDataComponents.FIRING_PARAMETERS)).pos(); - - player.displayClientMessage(Component.translatable("tips.superbwarfare.mortar.target_pos") - .withStyle(ChatFormatting.GRAY) - .append(Component.literal("[" + pos.getX() - + "," + pos.getY() - + "," + pos.getZ() - + "]")), true); - SoundTool.playLocalSound(player, ModSounds.CANNON_ZOOM_IN.get(), 2, 1); - - var mainTag = NBTTool.getTag(mainStack); - ListTag tags = mainTag.getList(TAG_MORTARS, Tag.TAG_COMPOUND); + if (stack.is(ModItems.ARTILLERY_INDICATOR.get())) { + ListTag tags = NBTTool.getTag(stack).getList(TAG_CANNON, Tag.TAG_COMPOUND); 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.setTarget(mainStack)) { - player.displayClientMessage(Component.translatable("tips.superbwarfare.mortar.warn").withStyle(ChatFormatting.RED), true); + if (mortarEntity.stack.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.stack.getItem() instanceof CannonShellItem && mk42Entity.getEntityData().get(COOL_DOWN) == 0) { + int randomNumber = (int) (Math.random() * 5) + 1; + var weaponType = stack.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.stack.getItem() instanceof CannonShellItem && mle1934Entity.getEntityData().get(COOL_DOWN) == 0) { + int randomNumber = (int) (Math.random() * 5) + 1; + var weaponType = stack.is(ModItems.AP_5_INCHES.get()) ? 0 : 1; + mle1934Entity.setWeaponIndex(0, weaponType); + Mod.queueServerWork(randomNumber, () -> { + mle1934Entity.vehicleShoot(player, 0); + }); } } } diff --git a/src/main/java/com/atsuishio/superbwarfare/network/message/send/SetFiringParametersMessage.java b/src/main/java/com/atsuishio/superbwarfare/network/message/send/SetFiringParametersMessage.java index b3371b887..44e9fb5bd 100644 --- a/src/main/java/com/atsuishio/superbwarfare/network/message/send/SetFiringParametersMessage.java +++ b/src/main/java/com/atsuishio/superbwarfare/network/message/send/SetFiringParametersMessage.java @@ -2,6 +2,8 @@ package com.atsuishio.superbwarfare.network.message.send; import com.atsuishio.superbwarfare.Mod; import com.atsuishio.superbwarfare.component.ModDataComponents; +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.init.ModItems; import com.atsuishio.superbwarfare.init.ModSounds; @@ -29,7 +31,7 @@ import org.jetbrains.annotations.NotNull; import java.util.Objects; -import static com.atsuishio.superbwarfare.item.ArtilleryIndicator.TAG_MORTARS; +import static com.atsuishio.superbwarfare.item.ArtilleryIndicator.TAG_CANNON; public enum SetFiringParametersMessage implements CustomPacketPayload { INSTANCE; @@ -73,13 +75,13 @@ public enum SetFiringParametersMessage implements CustomPacketPayload { } if (mainStack.is(ModItems.ARTILLERY_INDICATOR.get())) { + BlockPos pos; if (lookAtEntity) { - stack.set(ModDataComponents.FIRING_PARAMETERS, new FiringParameters.Parameters(lookingEntity.blockPosition(), false)); + pos = lookingEntity.blockPosition(); } else { - stack.set(ModDataComponents.FIRING_PARAMETERS, new FiringParameters.Parameters(new BlockPos((int) hitPos.x, (int) hitPos.y, (int) hitPos.z), false)); + pos = new BlockPos((int) hitPos.x, (int) hitPos.y, (int) hitPos.z); } - - var pos = Objects.requireNonNull(stack.get(ModDataComponents.FIRING_PARAMETERS)).pos(); + mainStack.set(ModDataComponents.FIRING_PARAMETERS, new FiringParameters.Parameters(pos, false)); player.displayClientMessage(Component.translatable("tips.superbwarfare.mortar.target_pos") .withStyle(ChatFormatting.GRAY) @@ -89,8 +91,7 @@ public enum SetFiringParametersMessage implements CustomPacketPayload { + "]")), true); SoundTool.playLocalSound(player, ModSounds.CANNON_ZOOM_IN.get(), 2, 1); - var mainTag = NBTTool.getTag(mainStack); - ListTag tags = mainTag.getList(TAG_MORTARS, Tag.TAG_COMPOUND); + ListTag tags = NBTTool.getTag(mainStack).getList(TAG_CANNON, Tag.TAG_COMPOUND); for (int i = 0; i < tags.size(); i++) { var tag = tags.getCompound(i); Entity entity = EntityFindUtil.findEntity(player.level(), tag.getString("UUID")); @@ -99,6 +100,16 @@ public enum SetFiringParametersMessage implements CustomPacketPayload { player.displayClientMessage(Component.translatable("tips.superbwarfare.mortar.warn").withStyle(ChatFormatting.RED), true); } } + if (entity instanceof Mk42Entity mk42Entity) { + if (!mk42Entity.setTarget(mainStack, true)) { + player.displayClientMessage(Component.translatable("tips.superbwarfare.mortar.warn").withStyle(ChatFormatting.RED), true); + } + } + if (entity instanceof Mle1934Entity mle1934Entity) { + if (!mle1934Entity.setTarget(mainStack, true)) { + player.displayClientMessage(Component.translatable("tips.superbwarfare.mortar.warn").withStyle(ChatFormatting.RED), true); + } + } } } } diff --git a/src/main/java/com/atsuishio/superbwarfare/tools/ChunkLoadTool.java b/src/main/java/com/atsuishio/superbwarfare/tools/ChunkLoadTool.java index 02dd720c0..1490bd813 100644 --- a/src/main/java/com/atsuishio/superbwarfare/tools/ChunkLoadTool.java +++ b/src/main/java/com/atsuishio/superbwarfare/tools/ChunkLoadTool.java @@ -57,7 +57,7 @@ public class ChunkLoadTool { public static void unloadAllChunks(ServerLevel level, Entity entity, Set loadedChunks) { loadedChunks.forEach(chunk -> { var chunkPos = new ChunkPos(chunk); - Mod.queueServerWork(10, () -> controller.forceChunk(level, entity, chunkPos.x, chunkPos.z, false, false)); + Mod.queueServerWork(20, () -> controller.forceChunk(level, entity, chunkPos.x, chunkPos.z, false, false)); }); }