添加手雷的功能

This commit is contained in:
17146 2024-07-30 02:55:14 +08:00
parent e2ac6b8804
commit e02410ee84
2 changed files with 26 additions and 16 deletions

View file

@ -59,18 +59,15 @@ public class HandGrenadeEntity extends ThrowableItemProjectile {
@Override @Override
protected void onHit(HitResult result) protected void onHit(HitResult result) {
{ switch (result.getType()) {
switch(result.getType())
{
case BLOCK: case BLOCK:
BlockHitResult blockResult = (BlockHitResult) result; BlockHitResult blockResult = (BlockHitResult) result;
BlockPos resultPos = blockResult.getBlockPos(); BlockPos resultPos = blockResult.getBlockPos();
BlockState state = this.level().getBlockState(resultPos); BlockState state = this.level().getBlockState(resultPos);
SoundEvent event = state.getBlock().getSoundType(state, this.level(), resultPos, this).getStepSound(); SoundEvent event = state.getBlock().getSoundType(state, this.level(), resultPos, this).getStepSound();
double speed = this.getDeltaMovement().length(); double speed = this.getDeltaMovement().length();
if(speed > 0.1) if (speed > 0.1) {
{
this.level().playSound(null, result.getLocation().x, result.getLocation().y, result.getLocation().z, event, SoundSource.AMBIENT, 1.0F, 1.0F); this.level().playSound(null, result.getLocation().x, result.getLocation().y, result.getLocation().z, event, SoundSource.AMBIENT, 1.0F, 1.0F);
} }
this.bounce(blockResult.getDirection()); this.bounce(blockResult.getDirection());
@ -81,8 +78,7 @@ public class HandGrenadeEntity extends ThrowableItemProjectile {
Entity entity = entityResult.getEntity(); Entity entity = entityResult.getEntity();
double speed_e = this.getDeltaMovement().length(); double speed_e = this.getDeltaMovement().length();
if(speed_e > 0.1) if (speed_e > 0.1) {
{
entity.hurt(entity.damageSources().thrown(this, this.getOwner()), 1.0F); entity.hurt(entity.damageSources().thrown(this, this.getOwner()), 1.0F);
} }
this.bounce(Direction.getNearest(this.getDeltaMovement().x(), this.getDeltaMovement().y(), this.getDeltaMovement().z()).getOpposite()); this.bounce(Direction.getNearest(this.getDeltaMovement().x(), this.getDeltaMovement().y(), this.getDeltaMovement().z()).getOpposite());
@ -93,17 +89,14 @@ public class HandGrenadeEntity extends ThrowableItemProjectile {
} }
} }
private void bounce(Direction direction) private void bounce(Direction direction) {
{ switch (direction.getAxis()) {
switch(direction.getAxis())
{
case X: case X:
this.setDeltaMovement(this.getDeltaMovement().multiply(-0.5, 0.75, 0.75)); this.setDeltaMovement(this.getDeltaMovement().multiply(-0.5, 0.75, 0.75));
break; break;
case Y: case Y:
this.setDeltaMovement(this.getDeltaMovement().multiply(0.75, -0.25, 0.75)); this.setDeltaMovement(this.getDeltaMovement().multiply(0.75, -0.25, 0.75));
if(this.getDeltaMovement().y() < this.getGravity()) if (this.getDeltaMovement().y() < this.getGravity()) {
{
this.setDeltaMovement(this.getDeltaMovement().multiply(1, 0, 1)); this.setDeltaMovement(this.getDeltaMovement().multiply(1, 0, 1));
} }
break; break;

View file

@ -1,6 +1,9 @@
package net.mcreator.superbwarfare.item; package net.mcreator.superbwarfare.item;
import net.mcreator.superbwarfare.entity.HandGrenadeEntity; import net.mcreator.superbwarfare.entity.HandGrenadeEntity;
import net.mcreator.superbwarfare.init.ModDamageTypes;
import net.mcreator.superbwarfare.tools.CustomExplosion;
import net.mcreator.superbwarfare.tools.ParticleTool;
import net.minecraft.world.InteractionHand; import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResultHolder; import net.minecraft.world.InteractionResultHolder;
import net.minecraft.world.entity.LivingEntity; import net.minecraft.world.entity.LivingEntity;
@ -9,6 +12,7 @@ import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Rarity; import net.minecraft.world.item.Rarity;
import net.minecraft.world.item.UseAnim; import net.minecraft.world.item.UseAnim;
import net.minecraft.world.level.Explosion;
import net.minecraft.world.level.Level; import net.minecraft.world.level.Level;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
@ -33,8 +37,6 @@ public class HandGrenade extends Item {
public void releaseUsing(ItemStack stack, Level worldIn, LivingEntity entityLiving, int timeLeft) { public void releaseUsing(ItemStack stack, Level worldIn, LivingEntity entityLiving, int timeLeft) {
if (!worldIn.isClientSide) { if (!worldIn.isClientSide) {
if (entityLiving instanceof Player player) { if (entityLiving instanceof Player player) {
int usingTime = this.getUseDuration(stack) - timeLeft; int usingTime = this.getUseDuration(stack) - timeLeft;
float power = Math.min(usingTime / 10.0f, 1.5f); float power = Math.min(usingTime / 10.0f, 1.5f);
@ -52,6 +54,21 @@ public class HandGrenade extends Item {
} }
} }
@Override
public ItemStack finishUsingItem(ItemStack pStack, Level pLevel, LivingEntity pLivingEntity) {
if (!pLevel.isClientSide) {
CustomExplosion explosion = new CustomExplosion(pLevel, null,
ModDamageTypes.causeProjectileBoomDamage(pLevel.registryAccess(), pLivingEntity, pLivingEntity), 90,
pLivingEntity.getX(), pLivingEntity.getY(), pLivingEntity.getZ(), 10f, Explosion.BlockInteraction.KEEP).setDamageMultiplier(2);
explosion.explode();
net.minecraftforge.event.ForgeEventFactory.onExplosionStart(pLevel, explosion);
explosion.finalizeExplosion(false);
ParticleTool.spawnMediumExplosionParticles(pLevel, pLivingEntity.position());
}
return super.finishUsingItem(pStack, pLevel, pLivingEntity);
}
@Override @Override
public int getUseDuration(ItemStack stack) { public int getUseDuration(ItemStack stack) {
return 100; return 100;