我们今天对于物品右键点击的事件进行专门设置
1. items 包中新建 Item类名(以 ItemGaisRiser作为演示)
对新建的 ItemGaisRiser.java 进行编辑
package com.Joy187.newmod.items;
import com.Joy187.newmod.entity.EntityGaia;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.projectile.EntityLargeFireball;
import net.minecraft.item.ItemStack;
import net.minecraft.util.ActionResult;
import net.minecraft.util.EnumActionResult;
import net.minecraft.util.EnumHand;
import net.minecraft.util.math.Vec3d;
import net.minecraft.world.World;
public class ItemGaiaRiser extends ItemBase{
public ItemGaiaRiser(String name,CreativeTabs tab) {
super(name, tab);
}
}
2.发射火球
在Minecraft中,火球、箭矢等物品其实是作为一个生物(Entity)实体被召唤出来的,所以我们要定义一个右键召唤火球生物的函数ActionResult<ItemStack> onItemRightClick(物品右键功能)
在ItemGaisRiser.java 中添加函数
@Override
public ActionResult<ItemStack> onItemRightClick(World worldIn,EntityPlayer playerIn, EnumHand handIn) {
ItemStack item = playerIn.getHeldItem(handIn);
Vec3d aim = playerIn.getLookVec();
EntityGaia g = new EntityGaia(worldIn, playerIn, 1, 1, 1);
g.setPosition(playerIn.posX+ aim.x *1.5D, playerIn.posY + aim.y *1.50, playerIn.posZ + aim.z*1.50);
g.accelerationX = aim.x * 0.1;
g.accelerationY = aim.y * 0.1;
g.accelerationZ = aim.z * 0.1;
worldIn.spawnEntity(g);
item.damageItem(1, playerIn);
return new ActionResult<ItemStack>(EnumActionResult.SUCCESS, item);
}
保存设置,进入游戏
拿出我们的物品点击右键,发现火球真正的被创造了出来
3.召唤生物
区别于可抛掷类(throwable)生物,一般生物的生成不需要加速度(当然你如果想要做生物发射器什么的也可以233),所以函数可以这样写
@Override
public ActionResult<ItemStack> onItemRightClick(World worldIn,EntityPlayer playerIn, EnumHand handIn) {
ItemStack item = playerIn.getHeldItem(handIn);
Vec3d aim = playerIn.getLookVec();
EntityGaia g = new EntityGaia(worldIn, playerIn, 1, 1, 1);
g.setPosition(playerIn.posX+ aim.x *1.5D, playerIn.posY + aim.y *1.50, playerIn.posZ + aim.z*1.50);
worldIn.spawnEntity(g);
item.damageItem(1, playerIn);
return new ActionResult<ItemStack>(EnumActionResult.SUCCESS, item);
}