版本信息
IDE:Eclipse
IDE版本:4.12
Forge版本: 14.23.5.2823
Minecraft版本: 1.12
Mapping 文件版本: stable_39
错误情况简述
让玩家朝着一个方向运动,我尝试调用EntityLivingBase#setVelocity()和EntityLivingBase#addVelocity(),但是在此方法调用之后玩家没有任何的反应
(在此之前我也查询过其他论坛的问题,只得到一些匪夷所思的结论,但却没法解决这个问题)
相关代码
ItemClaw.class
(物品)
public class ItemClaw extends Item{
private int progress;
public ItemClaw() {
super();
this.maxStackSize = 1;
setMaxDamage(0);
setNoRepair();
setCreativeTab(MRCreativeTab.TOOL);
this.addPropertyOverride(new ResourceLocation("midgard_reborn", "less"), new IItemPropertyGetter() {
@Override
public float apply(ItemStack stack, World worldIn, EntityLivingBase entityIn) {
if(stack.getItem() instanceof ItemClaw)
{
return entityIn != null && !isLess(stack) ? 0.0F : 1.0F;
}
else
{
return 0.0F;
}
}
});
}
@Override
public ActionResult<ItemStack> onItemRightClick(World worldIn, EntityPlayer playerIn, EnumHand handIn) {
if(!worldIn.isRemote)
{
ItemStack itemstack = playerIn.getHeldItem(handIn);
if(itemstack != null && itemstack.getItem() instanceof ItemClaw && !isLess(itemstack))
{
ProjectileClaw claw = createClaw(playerIn);
claw.shoot(playerIn, playerIn.rotationPitch, playerIn.rotationYaw, 0.0F, 3.0F, 1.0F);
setLess(itemstack, true);
worldIn.spawnEntity(claw);
}
}
return new ActionResult<ItemStack>(EnumActionResult.SUCCESS, playerIn.getHeldItem(handIn));
}
@Override
public void onUpdate(ItemStack stack, World worldIn, Entity entityIn, int itemSlot, boolean isSelected) {
if(!worldIn.isRemote && stack.getItem() instanceof ItemClaw && isLess(stack))
{
NBTTagCompound nbt = stack.getTagCompound();
if(nbt == null)
{
nbt = new NBTTagCompound();
nbt.setInteger("progress", 0);
}
progress = nbt.getInteger("progress");
if(++progress >= 25)
{
this.setLess(stack, false);
progress = 0;
}
nbt.setInteger("progress", progress);
}
}
protected ProjectileClaw createClaw(EntityLivingBase throwerIn)
{
return new ProjectileClaw(throwerIn.world, throwerIn);
}
public void setLess(ItemStack itemclaw, boolean value)
{
NBTTagCompound nbt = itemclaw.getTagCompound();
if(nbt == null)
{
nbt = new NBTTagCompound();
}
nbt.setBoolean("less", value);
itemclaw.setTagCompound(nbt);
}
public boolean isLess(ItemStack itemclaw)
{
NBTTagCompound nbt = itemclaw.getTagCompound();
return nbt == null ? false : nbt.getBoolean("less");
}
}
ProjectileClaw.class
(抛射物)
public class ProjectileClaw extends EntityThrowable{
private double initmotionX;
private double initmotionY;
private double initmotionZ;
private EntityLivingBase clawthrower;
public ProjectileClaw(World worldIn) {
super(worldIn);
}
public ProjectileClaw(World worldIn, double x, double y, double z)
{
super(worldIn, x, y, z);
}
public ProjectileClaw(World worldIn, EntityLivingBase throwerIn) {
super(worldIn, throwerIn);
this.clawthrower = throwerIn;
}
@Override
public void onUpdate() {
EntityLivingBase entitylivingbase = this.getThrower();
if(entitylivingbase != null && entitylivingbase instanceof EntityPlayer && !entitylivingbase.isEntityAlive())
{
this.setDead();
}
else
{
super.onUpdate();
}
}
public static void registerFixesClaw(DataFixer fixer)
{
registerFixesThrowable(fixer, "ThrownClaw");
}
@Override
public void shoot(double x, double y, double z, float velocity, float inaccuracy) {
this.initmotionX = x;
this.initmotionY = y;
this.initmotionZ = z;
super.shoot(x, y, z, velocity, inaccuracy);
}
@Override
protected void onImpact(RayTraceResult result) {
EntityLivingBase entitylivingbase = getThrower();
if(result.entityHit != null)
{
if(result.entityHit == this.clawthrower)
{
return;
}
result.entityHit.attackEntityFrom(DamageSource.causeThrownDamage(this, entitylivingbase), 1.0F);
}
if(result.typeOfHit == RayTraceResult.Type.BLOCK)
{
if(entitylivingbase != null)
{
motorize(0.1F);
this.world.setEntityState(this, (byte) 15);
this.setDead();
}
}
if(!this.world.isRemote)
{
if(this.onGround)
{
this.setDead();
}
}
}
protected void motorize(float velocity)
{
EntityLivingBase entitylivingbase = getThrower();
if(entitylivingbase != null)
{
double x = entitylivingbase.posX - this.posX;
double y = entitylivingbase.posY - this.posY;
double z = entitylivingbase.posZ - this.posZ;
x = x * (double)velocity;
y = y * (double)velocity;
z = z * (double)velocity;
entitylivingbase.setVelocity(x, y, z);
entitylivingbase.sendMessage(new TextComponentString("motorize(x:" + x + " y:" + y + " z:" + z + ")"));//这是测试用的信息,测试时此项是有值且正确的
}
}
@Override
public void writeEntityToNBT(NBTTagCompound compound) {
super.writeEntityToNBT(compound);
compound.setDouble("init_motionX", initmotionX);
compound.setDouble("init_motionY", initmotionY);
compound.setDouble("init_motionZ", initmotionZ);
}
@Override
public void readEntityFromNBT(NBTTagCompound compound) {
this.initmotionX = compound.getDouble("init_motionX");
this.initmotionY = compound.getDouble("init_motionY");
this.initmotionZ = compound.getDouble("init_motionZ");
super.readEntityFromNBT(compound);
}
}
加速度的实现在 ProjectileClaw#motorize() 在 ProjectileClaw#onImpact() 调用