Minecraft 1.12.2模组开发(二十三) 霰弹枪!
Posted Gendan
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Minecraft 1.12.2模组开发(二十三) 霰弹枪!相关的知识,希望对你有一定的参考价值。
package com.Joy187.newmod.entity;
import java.util.Random;
import java.util.UUID;
import javax.annotation.Nullable;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.monster.EntityWitherSkeleton;
import net.minecraft.entity.monster.EntityZombie;
import net.minecraft.entity.projectile.EntityFireball;
import net.minecraft.entity.projectile.EntitySnowball;
import net.minecraft.entity.projectile.EntityThrowable;
import net.minecraft.network.datasync.DataParameter;
import net.minecraft.network.datasync.DataSerializers;
import net.minecraft.network.datasync.EntityDataManager;
import net.minecraft.util.DamageSource;
import net.minecraft.util.math.RayTraceResult;
import net.minecraft.world.World;
import net.minecraft.world.WorldServer;
public class EntityM1897B extends EntitySnowball{
private static final DataParameter<Byte> CRITICAL = EntityDataManager.<Byte>createKey(EntityM1897B.class, DataSerializers.BYTE);
protected EntityLivingBase thrower;
private String throwerName;
public Entity shootingEntity;
private double damage; //定义子弹伤害
public EntityM1897B(World worldIn)
{
super(worldIn);
this.setSize(1.0F, 1.0F); //设置子弹大小
this.damage = 8.0D; //设定子弹伤害
}
public EntityM1897B(World worldIn, EntityLivingBase throwerIn)
{
super(worldIn, throwerIn);
}
public EntityM1897B(World worldIn, double x, double y, double z)
{
this(worldIn);
this.setPosition(x, y, z);
}
//定义子弹撞击物体的函数
protected void onImpact(RayTraceResult result)
{
Random rand = new Random();
float k = rand.nextFloat();
if (result.entityHit != null)
{
int i = 6;
//如果子弹打到Zombie或凋零,[金融期货](https://www.gendan5.com/futures/ff.html)就增大3点攻击力
if (result.entityHit instanceof EntityZombie || result.entityHit instanceof EntityWitherSkeleton)
{
i = 9;
} result.entityHit.attackEntityFrom(DamageSource.causeThrownDamage(this, this.getThrower()), (float)i*(1+k));
}
//this.world.newExplosion((Entity)null, this.posX, this.posY, this.posZ, (float)this.explosionPower, false, true);
if (!this.world.isRemote)
{
this.world.setEntityState(this, (byte)3);
this.setDead();
}
}
@Nullable
public EntityLivingBase getThrower()
{
if (this.thrower == null && this.throwerName != null && !this.throwerName.isEmpty())
{
this.thrower = this.world.getPlayerEntityByName(this.throwerName);
if (this.thrower == null && this.world instanceof WorldServer)
{
try
{
Entity entity = ((WorldServer)this.world).getEntityFromUuid(UUID.fromString(this.throwerName));
if (entity instanceof EntityLivingBase)
{
this.thrower = (EntityLivingBase)entity;
}
}
catch (Throwable var2)
{
this.thrower = null;
}
}
}
return this.thrower;
}
public void setIsCritical(boolean critical)
{
byte b0 = ((Byte)this.dataManager.get(CRITICAL)).byteValue();
if (critical)
{
this.dataManager.set(CRITICAL, Byte.valueOf((byte)(b0 | 1)));
}
else
{
this.dataManager.set(CRITICAL, Byte.valueOf((byte)(b0 & -2)));
}
}
public double getDamage()
{
return this.damage;
}
public void setDamage(double damageIn)
{
this.damage = damageIn;
}
}
以上是关于Minecraft 1.12.2模组开发(二十三) 霰弹枪!的主要内容,如果未能解决你的问题,请参考以下文章
Minecraft Fabric模组开发踩坑:缺失Fabric API
Minecraft Fabric模组开发踩坑:缺失Fabric API
Minecraft Fabric模组开发踩坑:缺失Fabric API
Minecraft java edition 模组开发:通过对岩浆怪和雪傀儡的源码分析,自己制作一个雪球怪