下面列出了怎么用net.minecraftforge.common.IShearable的API类实例代码及写法,或者点击链接到github查看源代码。
public static void shearEntity(ItemStack stack, IWorld world, BlockPos pos, IShearable target) {
if (!(target instanceof Entity)) {
throw new IllegalArgumentException("Tried to call shearEntity on something that was not an entity!");
}
Entity entity = (Entity) target;
List<ItemStack> drops = target.onSheared(stack, world, pos, EnchantmentHelper.getLevel(Enchantments.FORTUNE, stack));
Random rand = world.getRandom();
for (ItemStack drop : drops) {
ItemEntity item = entity.dropStack(drop, 1.0F);
if (item == null) {
continue;
}
float accelerationX = (rand.nextFloat() - rand.nextFloat()) * 0.1F;
float accelerationY = rand.nextFloat() * 0.05F;
float accelerationZ = (rand.nextFloat() - rand.nextFloat()) * 0.1F;
item.setVelocity(item.getVelocity().add(accelerationX, accelerationY, accelerationZ));
}
if (stack.damage(1, world.getRandom(), null)) {
stack.setCount(0);
}
}
@Override
public boolean useOnEntity(ItemStack stack, PlayerEntity playerIn, LivingEntity entity, Hand hand) {
if (entity.world.isClient) {
return false;
}
// Avoid duplicating vanilla interactions
if (this == Items.SHEARS) {
EntityType<?> type = entity.getType();
if (type == EntityType.MOOSHROOM || type == EntityType.SHEEP || type == EntityType.SNOW_GOLEM) {
return false;
}
}
if (entity instanceof IShearable) {
IShearable target = (IShearable) entity;
BlockPos pos = entity.getBlockPos();
if (target.isShearable(stack, entity.world, pos)) {
Shearables.shearEntity(stack, entity.world, pos, target);
}
return true;
}
return false;
}
public static boolean applyShearBehavior(ItemStack itemStack, BlockPos pos, EntityPlayer player) {
Block block = player.world.getBlockState(pos).getBlock();
if (block instanceof IShearable) {
IShearable target = (IShearable) block;
if (target.isShearable(itemStack, player.world, pos)) {
int fortuneLevel = EnchantmentHelper.getEnchantmentLevel(Enchantments.FORTUNE, itemStack);
List<ItemStack> drops = target.onSheared(itemStack, player.world, pos, fortuneLevel);
dropListOfItems(player.world, pos, drops);
player.world.setBlockState(pos, Blocks.AIR.getDefaultState(), 11);
return true;
}
}
return false;
}
public static ArrayList<ItemStack> getIdentifierItems(World world, EntityPlayer player, MovingObjectPosition hit) {
BlockPos pos = hit.getBlockPos();
IBlockState state = world.getBlockState(pos);
Block block = state.getBlock();
ArrayList<ItemStack> items = new ArrayList<ItemStack>();
ArrayList<IHighlightHandler> handlers = new ArrayList<IHighlightHandler>();
if (highlightIdentifiers.containsKey(null))
handlers.addAll(highlightIdentifiers.get(null));
if (highlightIdentifiers.containsKey(block))
handlers.addAll(highlightIdentifiers.get(block));
for (IHighlightHandler ident : handlers) {
ItemStack item = ident.identifyHighlight(world, player, hit);
if (item != null)
items.add(item);
}
if (items.size() > 0)
return items;
ItemStack pick = block.getPickBlock(hit, world, pos);
if (pick != null)
items.add(pick);
try {
items.addAll(block.getDrops(world, pos, state, 0));
} catch (Exception ignored) {}
if (block instanceof IShearable) {
IShearable shearable = (IShearable) block;
if (shearable.isShearable(new ItemStack(Items.shears), world, pos))
items.addAll(shearable.onSheared(new ItemStack(Items.shears), world, pos, 0));
}
if (items.size() == 0)
items.add(0, new ItemStack(block, 1, block.getMetaFromState(state)));
return items;
}
@Override
public boolean onBlockStartBreak(ItemStack stack, int x, int y, int z, EntityPlayer player) {
if (player.worldObj.isRemote) {
return false;
}
Block block = player.worldObj.getBlock(x, y, z);
if (block instanceof IShearable) {
IShearable target = (IShearable) block;
if (target.isShearable(stack, player.worldObj, x, y, z)) {
ArrayList<ItemStack> drops = target.onSheared(stack, player.worldObj, x, y, z,
EnchantmentHelper.getEnchantmentLevel(Enchantment.fortune.effectId, stack));
Random rand = new Random();
for (ItemStack drop : drops) {
float f = 0.7F;
double d = (double) (rand.nextFloat() * f) + (double) (1.0F - f) * 0.5D;
double d1 = (double) (rand.nextFloat() * f) + (double) (1.0F - f) * 0.5D;
double d2 = (double) (rand.nextFloat() * f) + (double) (1.0F - f) * 0.5D;
EntityItem entityitem = new EntityItem(player.worldObj, (double) x + d, (double) y + d1, (double) z + d2, drop);
entityitem.delayBeforeCanPickup = 10;
player.worldObj.spawnEntityInWorld(entityitem);
}
stack.damageItem(1, player);
player.addStat(StatList.mineBlockStatArray[Block.getIdFromBlock(block)], 1);
}
}
return false;
}
@Override
public boolean onBlockStartBreak(ItemStack stack, int x, int y, int z, EntityPlayer player) {
if (player.worldObj.isRemote) {
return false;
}
Block block = player.worldObj.getBlock(x, y, z);
if (block instanceof IShearable) {
IShearable target = (IShearable) block;
if (target.isShearable(stack, player.worldObj, x, y, z)) {
ArrayList<ItemStack> drops = target.onSheared(stack, player.worldObj, x, y, z,
EnchantmentHelper.getEnchantmentLevel(Enchantment.fortune.effectId, stack));
Random rand = new Random();
for (ItemStack drop : drops) {
float f = 0.7F;
double d = (double) (rand.nextFloat() * f) + (double) (1.0F - f) * 0.5D;
double d1 = (double) (rand.nextFloat() * f) + (double) (1.0F - f) * 0.5D;
double d2 = (double) (rand.nextFloat() * f) + (double) (1.0F - f) * 0.5D;
EntityItem entityitem = new EntityItem(player.worldObj, (double) x + d, (double) y + d1, (double) z + d2, drop);
entityitem.delayBeforeCanPickup = 10;
player.worldObj.spawnEntityInWorld(entityitem);
}
stack.damageItem(1, player);
player.addStat(StatList.mineBlockStatArray[Block.getIdFromBlock(block)], 1);
}
}
return false;
}
@Override
protected void onImpact(MovingObjectPosition objectPosition){
if(objectPosition.entityHit != null) {
Entity entity = objectPosition.entityHit;
entity.motionX += motionX;
entity.motionY += motionY;
entity.motionZ += motionZ;
if(!entity.worldObj.isRemote && entity instanceof IShearable) {
IShearable shearable = (IShearable)entity;
int x = (int)Math.floor(posX);
int y = (int)Math.floor(posY);
int z = (int)Math.floor(posZ);
if(shearable.isShearable(null, worldObj, x, y, z)) {
List<ItemStack> drops = shearable.onSheared(null, worldObj, x, y, z, 0);
for(ItemStack stack : drops) {
PneumaticCraftUtils.dropItemOnGround(stack, worldObj, entity.posX, entity.posY, entity.posZ);
}
}
}
} else {
Block block = worldObj.getBlock(objectPosition.blockX, objectPosition.blockY, objectPosition.blockZ);
if(block instanceof IPlantable || block instanceof BlockLeaves) {
motionX = oldMotionX;
motionY = oldMotionY;
motionZ = oldMotionZ;
} else {
setDead();
}
}
hitCounter++;
if(hitCounter > 20) setDead();
}