/
EntityModArrow.java
88 lines (72 loc) · 2.45 KB
/
EntityModArrow.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package knightminer.inspirations.tools.entity;
import static knightminer.inspirations.tools.InspirationsTools.redstoneCharge;
import knightminer.inspirations.tools.InspirationsTools;
import knightminer.inspirations.utility.block.BlockRedstoneCharge;
import net.minecraft.block.state.IBlockState;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.projectile.EntityArrow;
import net.minecraft.init.SoundEvents;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.SoundCategory;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.RayTraceResult;
import net.minecraft.world.World;
public class EntityModArrow extends EntityArrow {
private int meta;
public EntityModArrow(World world) {
super(world);
}
public EntityModArrow(World world, double x, double y, double z, int meta) {
super(world, x, y, z);
init(meta);
}
public EntityModArrow(World world, EntityLivingBase shooter, int meta) {
super(world, shooter);
init(meta);
}
private void init(int meta) {
this.meta = meta;
this.setDamage(0.25);
}
@Override
protected ItemStack getArrowStack() {
return new ItemStack(InspirationsTools.arrow, 1, meta);
}
/**
* Called when the arrow hits a block or an entity
*/
@Override
protected void onHit(RayTraceResult raytrace) {
if(raytrace.typeOfHit == RayTraceResult.Type.BLOCK) {
// get to the block the arrow is on
BlockPos pos = raytrace.getBlockPos().offset(raytrace.sideHit);
// if there is a block there, try the block next to that
if(!redstoneCharge.canPlaceBlockAt(world, pos)) {
pos = pos.offset(raytrace.sideHit);
if(!redstoneCharge.canPlaceBlockAt(world, pos)) {
super.onHit(raytrace);
return;
}
}
world.playSound(null, pos, SoundEvents.ITEM_FLINTANDSTEEL_USE, SoundCategory.BLOCKS, 1.0F, world.rand.nextFloat() * 0.4F + 0.8F);
IBlockState state = redstoneCharge.getDefaultState().withProperty(BlockRedstoneCharge.FACING, raytrace.sideHit.getOpposite());
world.setBlockState(pos, state, 11);
this.setDead();
return;
}
super.onHit(raytrace);
}
/* NBT */
public static final String TAG_META = "meta";
@Override
public void writeEntityToNBT(NBTTagCompound compound) {
super.writeEntityToNBT(compound);
compound.setInteger(TAG_META, meta);
}
@Override
public void readEntityFromNBT(NBTTagCompound compound) {
super.readEntityFromNBT(compound);
this.meta = compound.getInteger(TAG_META);
}
}