|
1 | 1 | package net.htmlcsjs.htmlTech.common.item.behaviors; |
2 | 2 |
|
3 | 3 | import codechicken.lib.vec.Vector3; |
| 4 | +import com.google.common.base.Predicates; |
4 | 5 | import gregtech.api.GTValues; |
5 | 6 | import gregtech.api.items.metaitem.stats.IItemBehaviour; |
6 | 7 | import gregtech.client.particle.GTLaserBeamParticle; |
7 | 8 | import gregtech.client.particle.GTParticleManager; |
| 9 | +import net.minecraft.entity.Entity; |
| 10 | +import net.minecraft.entity.EntityLivingBase; |
8 | 11 | import net.minecraft.entity.player.EntityPlayer; |
9 | 12 | import net.minecraft.item.ItemStack; |
10 | | -import net.minecraft.util.ActionResult; |
11 | | -import net.minecraft.util.EnumActionResult; |
12 | | -import net.minecraft.util.EnumHand; |
13 | | -import net.minecraft.util.ResourceLocation; |
| 13 | +import net.minecraft.util.*; |
| 14 | +import net.minecraft.util.math.*; |
14 | 15 | import net.minecraft.world.World; |
15 | 16 |
|
| 17 | +import java.util.List; |
| 18 | + |
16 | 19 | public class LaserGunBehaviour implements IItemBehaviour { |
| 20 | + |
| 21 | + private GTLaserBeamParticle particle; |
| 22 | + |
17 | 23 | @Override |
18 | 24 | public ActionResult<ItemStack> onItemRightClick(World world, EntityPlayer player, EnumHand hand) { |
19 | | - GTLaserBeamParticle particle = new GTLaserBeamParticle(world, Vector3.fromEntity(player), new Vector3(player.getLookVec()).add(0,-0.5,0).multiply(0,-1,0).multiply(100.1)) |
20 | | - .setBody(new ResourceLocation(GTValues.MODID,"textures/fx/laser/laser.png")); // create a beam particle and set its texture. |
| 25 | + RayTraceResult rayTracedLooking = rayTrace(player, 100, 1.0F, false, true); |
| 26 | + |
| 27 | + if (particle != null) { |
| 28 | + GTParticleManager.INSTANCE.clearAllEffects(true); |
| 29 | + } |
| 30 | + particle = new GTLaserBeamParticle(world, Vector3.fromEntity(player).add(0, player.getEyeHeight(), 0), Vector3.fromEntity(player).subtract(new Vector3(rayTracedLooking.hitVec))) |
| 31 | + .setBody(new ResourceLocation(GTValues.MODID, "textures/fx/laser/laser.png")); // create a beam particle and set its texture. |
21 | 32 | GTParticleManager.INSTANCE.addEffect(particle); // add it to the particle manager. |
22 | 33 | ItemStack itemStack = player.getHeldItem(hand); |
23 | | - return ActionResult.newResult(EnumActionResult.SUCCESS, itemStack); |
| 34 | + return ActionResult.newResult(EnumActionResult.PASS, itemStack); |
24 | 35 | } |
| 36 | + |
| 37 | + /** |
| 38 | + * raytraces player, taken from https://github.com/Lach01298/QMD with permission |
| 39 | + */ |
| 40 | + private RayTraceResult rayTrace(EntityLivingBase entity, double reachDistance, float partialTicks, boolean hitBlocks, boolean hitEntities) { |
| 41 | + Vec3d eyesPos = entity.getPositionEyes(partialTicks); |
| 42 | + Vec3d lookVec = entity.getLook(1.0F); |
| 43 | + Vec3d rayVec = eyesPos.add(lookVec.scale(reachDistance)); |
| 44 | + Entity pointedEntity = null; |
| 45 | + Vec3d hitVec = null; |
| 46 | + RayTraceResult entityHit = null; |
| 47 | + RayTraceResult blockHit = null; |
| 48 | + RayTraceResult missHit = new RayTraceResult(RayTraceResult.Type.MISS, eyesPos.subtract(rayVec), EnumFacing.UP, new BlockPos(MathHelper.floor(eyesPos.subtract(rayVec).x), MathHelper.floor(eyesPos.subtract(rayVec).y), MathHelper.floor(eyesPos.subtract(rayVec).z))); |
| 49 | + |
| 50 | + if (hitEntities) { |
| 51 | + List<Entity> list = entity.world.getEntitiesInAABBexcluding(entity, entity.getEntityBoundingBox().expand(lookVec.x * reachDistance, lookVec.y * reachDistance, lookVec.z * reachDistance).grow(1.0D, 1.0D, 1.0D), |
| 52 | + Predicates.and(EntitySelectors.NOT_SPECTATING, entity12 -> entity12 != null && entity12.canBeCollidedWith())); |
| 53 | + |
| 54 | + double d2 = reachDistance; |
| 55 | + |
| 56 | + for (Entity entity1 : list) { |
| 57 | + AxisAlignedBB axisalignedbb = entity1.getEntityBoundingBox().grow((double) entity1.getCollisionBorderSize()); |
| 58 | + RayTraceResult raytraceresult = axisalignedbb.calculateIntercept(eyesPos, rayVec); |
| 59 | + |
| 60 | + if (axisalignedbb.contains(eyesPos)) { |
| 61 | + pointedEntity = entity1; |
| 62 | + hitVec = raytraceresult == null ? eyesPos : raytraceresult.hitVec; |
| 63 | + } else if (raytraceresult != null) { |
| 64 | + double d3 = eyesPos.distanceTo(raytraceresult.hitVec); |
| 65 | + |
| 66 | + if (d3 < d2 || d2 == 0.0D) { |
| 67 | + if (entity1.getLowestRidingEntity() == entity.getLowestRidingEntity() && !entity1.canRiderInteract()) { |
| 68 | + if (d2 == 0.0D) { |
| 69 | + pointedEntity = entity1; |
| 70 | + hitVec = raytraceresult.hitVec; |
| 71 | + } |
| 72 | + } else { |
| 73 | + pointedEntity = entity1; |
| 74 | + hitVec = raytraceresult.hitVec; |
| 75 | + d2 = d3; |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | + if (pointedEntity != null) { |
| 81 | + entityHit = new RayTraceResult(pointedEntity, hitVec); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + if (hitBlocks) { |
| 86 | + blockHit = entity.world.rayTraceBlocks(eyesPos, rayVec, false, false, true); |
| 87 | + } |
| 88 | + |
| 89 | + double blockDistance = 0; |
| 90 | + double entityDistance = 0; |
| 91 | + |
| 92 | + if (entityHit != null) { |
| 93 | + entityDistance = entityHit.hitVec.distanceTo(entity.getPositionVector()); |
| 94 | + } |
| 95 | + |
| 96 | + if (blockHit != null) { |
| 97 | + blockDistance = blockHit.hitVec.distanceTo(entity.getPositionVector()); |
| 98 | + } |
| 99 | + |
| 100 | + if (hitEntities && hitBlocks) { |
| 101 | + if (entityDistance < blockDistance && entityDistance > 0) { |
| 102 | + return entityHit; |
| 103 | + } else { |
| 104 | + return blockHit; |
| 105 | + } |
| 106 | + } else if (hitEntities) { |
| 107 | + if (entityHit == null) { |
| 108 | + return missHit; |
| 109 | + } |
| 110 | + return entityHit; |
| 111 | + } else { |
| 112 | + if (blockHit == null) { |
| 113 | + return missHit; |
| 114 | + } |
| 115 | + return blockHit; |
| 116 | + } |
| 117 | + } |
| 118 | + |
25 | 119 | } |
0 commit comments