From 325eff00b4bdbf137b4a843a105dbbe64cf5a7db Mon Sep 17 00:00:00 2001 From: 17146 <1714673995@qq.com> Date: Sat, 22 Feb 2025 22:26:19 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B3=A8=E5=86=8C=E8=8D=AF=E6=B0=B4=E5=BC=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../superbwarfare/entity/MortarEntity.java | 18 ++++---- .../entity/projectile/MortarShellEntity.java | 45 +++++++++++++++++++ .../superbwarfare/init/ModItems.java | 1 + .../item/common/ammo/MortarShell.java | 10 +++++ .../item/common/ammo/PotionMortarShell.java | 28 ++++++++++++ .../assets/superbwarfare/lang/en_us.json | 1 + .../assets/superbwarfare/lang/zh_cn.json | 1 + 7 files changed, 94 insertions(+), 10 deletions(-) create mode 100644 src/main/java/com/atsuishio/superbwarfare/item/common/ammo/PotionMortarShell.java diff --git a/src/main/java/com/atsuishio/superbwarfare/entity/MortarEntity.java b/src/main/java/com/atsuishio/superbwarfare/entity/MortarEntity.java index cbda4be49..0bb3b7826 100644 --- a/src/main/java/com/atsuishio/superbwarfare/entity/MortarEntity.java +++ b/src/main/java/com/atsuishio/superbwarfare/entity/MortarEntity.java @@ -7,6 +7,7 @@ import com.atsuishio.superbwarfare.entity.vehicle.VehicleEntity; import com.atsuishio.superbwarfare.init.ModEntities; import com.atsuishio.superbwarfare.init.ModItems; import com.atsuishio.superbwarfare.init.ModSounds; +import com.atsuishio.superbwarfare.item.common.ammo.MortarShell; import net.minecraft.ChatFormatting; import net.minecraft.commands.arguments.EntityAnchorArgument; import net.minecraft.core.BlockPos; @@ -47,13 +48,13 @@ import software.bernie.geckolib.core.object.PlayState; import software.bernie.geckolib.util.GeckoLibUtil; public class MortarEntity extends VehicleEntity implements GeoEntity, AnimatedEntity { + public static final EntityDataAccessor FIRE_TIME = SynchedEntityData.defineId(MortarEntity.class, EntityDataSerializers.INT); public static final EntityDataAccessor PITCH = SynchedEntityData.defineId(MortarEntity.class, EntityDataSerializers.FLOAT); public static final EntityDataAccessor YAW = SynchedEntityData.defineId(MortarEntity.class, EntityDataSerializers.FLOAT); private final AnimatableInstanceCache cache = GeckoLibUtil.createInstanceCache(this); - public MortarEntity(PlayMessages.SpawnEntity packet, Level world) { this(ModEntities.MORTAR.get(), world); } @@ -118,9 +119,9 @@ public class MortarEntity extends VehicleEntity implements GeoEntity, AnimatedEn @Override public InteractionResult interact(Player player, InteractionHand hand) { - ItemStack mainHandItem = player.getMainHandItem(); + ItemStack stack = player.getMainHandItem(); - if (mainHandItem.getItem() == ModItems.MORTAR_SHELL.get() && !player.isShiftKeyDown() && this.entityData.get(FIRE_TIME) == 0) { + if (stack.getItem() instanceof MortarShell shell && !player.isShiftKeyDown() && this.entityData.get(FIRE_TIME) == 0) { this.entityData.set(FIRE_TIME, 25); if (!player.isCreative()) { @@ -134,7 +135,7 @@ public class MortarEntity extends VehicleEntity implements GeoEntity, AnimatedEn ModUtils.queueServerWork(20, () -> { Level level = this.level(); if (level instanceof ServerLevel server) { - MortarShellEntity entityToSpawn = new MortarShellEntity(player, level); + MortarShellEntity entityToSpawn = shell.createShell(player, level, stack); entityToSpawn.setPos(this.getX(), this.getEyeY(), this.getZ()); entityToSpawn.shoot(this.getLookAngle().x, this.getLookAngle().y, this.getLookAngle().z, 11.4f, (float) 0.1); level.addFreshEntity(entityToSpawn); @@ -165,7 +166,7 @@ public class MortarEntity extends VehicleEntity implements GeoEntity, AnimatedEn } if (player.isShiftKeyDown()) { - if (mainHandItem.getItem() == ModItems.CROWBAR.get()) { + if (stack.getItem() == ModItems.CROWBAR.get()) { this.discard(); ItemHandlerHelper.giveItemToPlayer(player, new ItemStack(ModItems.MORTAR_DEPLOYER.get())); return InteractionResult.SUCCESS; @@ -249,11 +250,8 @@ public class MortarEntity extends VehicleEntity implements GeoEntity, AnimatedEn @Override public void travel() { - float diffY; - float diffX; - - diffY = (float) Mth.wrapDegrees(entityData.get(YAW) - this.getYRot()); - diffX = (float) Mth.wrapDegrees(entityData.get(PITCH) - this.getXRot()); + float diffY = Mth.wrapDegrees(entityData.get(YAW) - this.getYRot()); + float diffX = Mth.wrapDegrees(entityData.get(PITCH) - this.getXRot()); this.setYRot(this.getYRot() + Mth.clamp(0.5f * diffY, -20f, 20f)); this.setXRot(Mth.clamp(this.getXRot() + Mth.clamp(0.5f * diffX, -20f, 20f), -89, -20)); diff --git a/src/main/java/com/atsuishio/superbwarfare/entity/projectile/MortarShellEntity.java b/src/main/java/com/atsuishio/superbwarfare/entity/projectile/MortarShellEntity.java index 4d935c708..efd789899 100644 --- a/src/main/java/com/atsuishio/superbwarfare/entity/projectile/MortarShellEntity.java +++ b/src/main/java/com/atsuishio/superbwarfare/entity/projectile/MortarShellEntity.java @@ -7,6 +7,7 @@ import com.atsuishio.superbwarfare.init.ModItems; import com.atsuishio.superbwarfare.tools.ChunkLoadTool; import com.atsuishio.superbwarfare.tools.ParticleTool; import com.atsuishio.superbwarfare.tools.ProjectileTool; +import com.google.common.collect.Sets; import net.minecraft.core.BlockPos; import net.minecraft.core.particles.ParticleTypes; import net.minecraft.nbt.CompoundTag; @@ -14,11 +15,16 @@ import net.minecraft.nbt.ListTag; import net.minecraft.network.protocol.Packet; import net.minecraft.network.protocol.game.ClientGamePacketListener; import net.minecraft.server.level.ServerLevel; +import net.minecraft.world.effect.MobEffectInstance; import net.minecraft.world.entity.Entity; import net.minecraft.world.entity.EntityType; import net.minecraft.world.entity.LivingEntity; import net.minecraft.world.entity.projectile.ThrowableItemProjectile; import net.minecraft.world.item.Item; +import net.minecraft.world.item.ItemStack; +import net.minecraft.world.item.alchemy.Potion; +import net.minecraft.world.item.alchemy.PotionUtils; +import net.minecraft.world.item.alchemy.Potions; import net.minecraft.world.level.Level; import net.minecraft.world.level.block.BellBlock; import net.minecraft.world.level.block.state.BlockState; @@ -26,12 +32,15 @@ import net.minecraft.world.phys.BlockHitResult; import net.minecraft.world.phys.EntityHitResult; import net.minecraftforge.network.NetworkHooks; import net.minecraftforge.network.PlayMessages; +import net.minecraftforge.registries.ForgeRegistries; import software.bernie.geckolib.animatable.GeoEntity; import software.bernie.geckolib.core.animatable.instance.AnimatableInstanceCache; import software.bernie.geckolib.core.animation.AnimatableManager; import software.bernie.geckolib.util.GeckoLibUtil; +import java.util.Collection; import java.util.HashSet; +import java.util.Objects; import java.util.Set; public class MortarShellEntity extends ThrowableItemProjectile implements GeoEntity { @@ -42,6 +51,9 @@ public class MortarShellEntity extends ThrowableItemProjectile implements GeoEnt private final AnimatableInstanceCache cache = GeckoLibUtil.createInstanceCache(this); public Set loadedChunks = new HashSet<>(); + private Potion potion = Potions.EMPTY; + private final Set effects = Sets.newHashSet(); + public MortarShellEntity(EntityType type, Level world) { super(type, world); this.noCulling = true; @@ -60,6 +72,21 @@ public class MortarShellEntity extends ThrowableItemProjectile implements GeoEnt this(ModEntities.MORTAR_SHELL.get(), level); } + public void setEffectsFromItem(ItemStack pStack) { + if (pStack.is(ModItems.POTION_MORTAR_SHELL.get())) { + this.potion = PotionUtils.getPotion(pStack); + Collection collection = PotionUtils.getCustomEffects(pStack); + if (!collection.isEmpty()) { + for (MobEffectInstance mobeffectinstance : collection) { + this.effects.add(new MobEffectInstance(mobeffectinstance)); + } + } + } else if (pStack.is(ModItems.MORTAR_SHELL.get())) { + this.potion = Potions.EMPTY; + this.effects.clear(); + } + } + @Override public void addAdditionalSaveData(CompoundTag pCompound) { super.addAdditionalSaveData(pCompound); @@ -74,6 +101,18 @@ public class MortarShellEntity extends ThrowableItemProjectile implements GeoEnt listTag.add(tag); } pCompound.put("Chunks", listTag); + + if (this.potion != Potions.EMPTY) { + pCompound.putString("Potion", Objects.requireNonNullElse(ForgeRegistries.POTIONS.getKey(this.potion), "empty").toString()); + } + + if (!this.effects.isEmpty()) { + ListTag listtag = new ListTag(); + for (MobEffectInstance mobeffectinstance : this.effects) { + listtag.add(mobeffectinstance.save(new CompoundTag())); + } + pCompound.put("CustomPotionEffects", listtag); + } } @Override @@ -104,6 +143,12 @@ public class MortarShellEntity extends ThrowableItemProjectile implements GeoEnt this.loadedChunks.add(tag.getLong("Pos")); } } + + if (pCompound.contains("Potion", 8)) { + this.potion = PotionUtils.getPotion(pCompound); + } + + this.effects.addAll(PotionUtils.getCustomEffects(pCompound)); } @Override diff --git a/src/main/java/com/atsuishio/superbwarfare/init/ModItems.java b/src/main/java/com/atsuishio/superbwarfare/init/ModItems.java index d4e449b2b..89fc22fb3 100644 --- a/src/main/java/com/atsuishio/superbwarfare/init/ModItems.java +++ b/src/main/java/com/atsuishio/superbwarfare/init/ModItems.java @@ -99,6 +99,7 @@ public class ModItems { public static final RegistryObject GRENADE_40MM = AMMO.register("grenade_40mm", () -> new Item(new Item.Properties())); public static final RegistryObject JAVELIN_MISSILE = AMMO.register("javelin_missile", () -> new Item(new Item.Properties())); public static final RegistryObject MORTAR_SHELL = AMMO.register("mortar_shell", MortarShell::new); + public static final RegistryObject POTION_MORTAR_SHELL = AMMO.register("potion_mortar_shell", PotionMortarShell::new); public static final RegistryObject ROCKET = AMMO.register("rocket", Rocket::new); public static final RegistryObject LUNGE_MINE = AMMO.register("lunge_mine", LungeMine::new); public static final RegistryObject HE_5_INCHES = AMMO.register("he_5_inches", () -> new CannonShellItem(new Item.Properties().rarity(Rarity.RARE))); diff --git a/src/main/java/com/atsuishio/superbwarfare/item/common/ammo/MortarShell.java b/src/main/java/com/atsuishio/superbwarfare/item/common/ammo/MortarShell.java index cc33bc61a..94a0eeb42 100644 --- a/src/main/java/com/atsuishio/superbwarfare/item/common/ammo/MortarShell.java +++ b/src/main/java/com/atsuishio/superbwarfare/item/common/ammo/MortarShell.java @@ -1,10 +1,20 @@ package com.atsuishio.superbwarfare.item.common.ammo; +import com.atsuishio.superbwarfare.entity.projectile.MortarShellEntity; +import net.minecraft.world.entity.LivingEntity; import net.minecraft.world.item.Item; +import net.minecraft.world.item.ItemStack; +import net.minecraft.world.level.Level; public class MortarShell extends Item { public MortarShell() { super(new Properties()); } + + public MortarShellEntity createShell(LivingEntity entity, Level level, ItemStack stack) { + MortarShellEntity shellEntity = new MortarShellEntity(entity, level); + shellEntity.setEffectsFromItem(stack); + return shellEntity; + } } diff --git a/src/main/java/com/atsuishio/superbwarfare/item/common/ammo/PotionMortarShell.java b/src/main/java/com/atsuishio/superbwarfare/item/common/ammo/PotionMortarShell.java new file mode 100644 index 000000000..02c12860b --- /dev/null +++ b/src/main/java/com/atsuishio/superbwarfare/item/common/ammo/PotionMortarShell.java @@ -0,0 +1,28 @@ +package com.atsuishio.superbwarfare.item.common.ammo; + +import net.minecraft.network.chat.Component; +import net.minecraft.world.item.ItemStack; +import net.minecraft.world.item.TooltipFlag; +import net.minecraft.world.item.alchemy.PotionUtils; +import net.minecraft.world.item.alchemy.Potions; +import net.minecraft.world.level.Level; + +import javax.annotation.Nullable; +import java.util.List; + +public class PotionMortarShell extends MortarShell { + + public PotionMortarShell() { + super(); + } + + @Override + public ItemStack getDefaultInstance() { + return PotionUtils.setPotion(super.getDefaultInstance(), Potions.POISON); + } + + @Override + public void appendHoverText(ItemStack pStack, @Nullable Level pLevel, List pTooltip, TooltipFlag pFlag) { + PotionUtils.addPotionTooltip(pStack, pTooltip, 0.125F); + } +} diff --git a/src/main/resources/assets/superbwarfare/lang/en_us.json b/src/main/resources/assets/superbwarfare/lang/en_us.json index f7ea7ef20..077a70ed1 100644 --- a/src/main/resources/assets/superbwarfare/lang/en_us.json +++ b/src/main/resources/assets/superbwarfare/lang/en_us.json @@ -132,6 +132,7 @@ "item.superbwarfare.buckshot": "Buckshot", "item.superbwarfare.taser_electrode": "Taser Electrode", "item.superbwarfare.mortar_shell": "Mortar Shell", + "item.superbwarfare.potion_mortar_shell": "Potion Mortar Shell", "item.superbwarfare.grenade_40mm": "40mm Gun Grenade", "item.superbwarfare.rocket": "RPG Rocket", "item.superbwarfare.sniper_ammo": "Sniper Ammo *2", diff --git a/src/main/resources/assets/superbwarfare/lang/zh_cn.json b/src/main/resources/assets/superbwarfare/lang/zh_cn.json index fc3268520..5278b6f82 100644 --- a/src/main/resources/assets/superbwarfare/lang/zh_cn.json +++ b/src/main/resources/assets/superbwarfare/lang/zh_cn.json @@ -132,6 +132,7 @@ "item.superbwarfare.buckshot": "霰弹弹丸", "item.superbwarfare.taser_electrode": "泰瑟枪电极", "item.superbwarfare.mortar_shell": "迫击炮弹", + "item.superbwarfare.potion_mortar_shell": "药水炮弹", "item.superbwarfare.grenade_40mm": "40mm枪榴弹", "item.superbwarfare.rocket": "RPG火箭", "item.superbwarfare.sniper_ammo": "狙击枪弹药 *2",