注册药水弹

This commit is contained in:
17146 2025-02-22 22:26:19 +08:00
parent 2b11c42d1d
commit 325eff00b4
7 changed files with 94 additions and 10 deletions

View file

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

View file

@ -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<Long> loadedChunks = new HashSet<>();
private Potion potion = Potions.EMPTY;
private final Set<MobEffectInstance> effects = Sets.newHashSet();
public MortarShellEntity(EntityType<? extends MortarShellEntity> 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<MobEffectInstance> 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

View file

@ -99,6 +99,7 @@ public class ModItems {
public static final RegistryObject<Item> GRENADE_40MM = AMMO.register("grenade_40mm", () -> new Item(new Item.Properties()));
public static final RegistryObject<Item> JAVELIN_MISSILE = AMMO.register("javelin_missile", () -> new Item(new Item.Properties()));
public static final RegistryObject<Item> MORTAR_SHELL = AMMO.register("mortar_shell", MortarShell::new);
public static final RegistryObject<Item> POTION_MORTAR_SHELL = AMMO.register("potion_mortar_shell", PotionMortarShell::new);
public static final RegistryObject<Item> ROCKET = AMMO.register("rocket", Rocket::new);
public static final RegistryObject<Item> LUNGE_MINE = AMMO.register("lunge_mine", LungeMine::new);
public static final RegistryObject<Item> HE_5_INCHES = AMMO.register("he_5_inches", () -> new CannonShellItem(new Item.Properties().rarity(Rarity.RARE)));

View file

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

View file

@ -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<Component> pTooltip, TooltipFlag pFlag) {
PotionUtils.addPotionTooltip(pStack, pTooltip, 0.125F);
}
}

View file

@ -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",

View file

@ -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",