de.pewpewproject.lasertag.entity.LaserRayEntity   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 128
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 58
dl 0
loc 128
rs 10
c 0
b 0
f 0
wmc 14

12 Methods

Rating   Name   Duplication   Size   Complexity  
A getColor() 0 2 1
A initDataTracker() 0 2 1
A LaserRayEntity(EntityType,World) 0 2 1
A getStart() 0 1 1
A createSpawnPacket() 0 26 1
A getEnd() 0 2 1
A readCustomDataFromNbt(NbtCompound) 0 2 1
A getDimensions(EntityPose) 0 4 1
A calculateBoundingBox() 0 10 3
A LaserRayEntity(World,LivingEntity,int,HitResult) 0 6 1
A LaserRayEntity(World,Vec3d,float,float,int,Vec3d) 0 15 1
A writeCustomDataToNbt(NbtCompound) 0 2 1
1
package de.pewpewproject.lasertag.entity;
2
3
import de.pewpewproject.lasertag.networking.NetworkingConstants;
4
import io.netty.buffer.Unpooled;
5
import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking;
6
import net.minecraft.entity.*;
7
import net.minecraft.nbt.NbtCompound;
8
import net.minecraft.network.Packet;
9
import net.minecraft.network.PacketByteBuf;
10
import net.minecraft.util.hit.HitResult;
11
import net.minecraft.util.math.Box;
12
import net.minecraft.util.math.Vec3d;
13
import net.minecraft.world.World;
14
15
/**
16
 * An Entity to show the laser ray
17
 *
18
 * @author Étienne Muser
19
 */
20
public class LaserRayEntity extends Entity {
21
22
    /**
23
     * The color of the ray
24
     */
25
    private int color;
26
    /**
27
     * The end of the laser ray in world coordinates
28
     */
29
    private Vec3d end;
30
31
    /**
32
     * The start of the laser ray in world coordinates
33
     */
34
    private Vec3d start;
35
36
    public LaserRayEntity(EntityType<? extends LaserRayEntity> type, World world) {
37
        super(type, world);
38
    }
39
40
    public LaserRayEntity(World world, LivingEntity owner, int color, HitResult hit) {
41
        this(world,
42
                owner.getEyePos().add(new Vec3d(0.0, -0.2, 0.0)),
43
                owner.getYaw(),
44
                owner.getPitch(),
45
                color, hit.getPos());
46
    }
47
48
    public LaserRayEntity(World world, Vec3d startPos, float yaw, float pitch, int color, Vec3d endPos) {
49
        super(Entities.LASER_RAY, world);
50
51
        this.color = color;
52
        this.start = startPos;
53
        this.end = endPos;
54
55
        this.setYaw(yaw);
56
        this.setPitch(pitch);
57
        this.setPosition(startPos);
58
59
        float xWidth = (float) Math.abs(endPos.x - startPos.x);
60
        float zWidth = (float) Math.abs(endPos.z - startPos.z);
61
        float height = (float) Math.abs(endPos.y - startPos.y);
62
        this.dimensions = EntityDimensions.changing(Math.max(xWidth, zWidth), height);
63
    }
64
65
    @Override
66
    public EntityDimensions getDimensions(EntityPose pose) {
67
        // Overwrite entity dimensions
68
        return this.dimensions;
69
    }
70
71
    @Override
72
    protected Box calculateBoundingBox() {
73
        // If start or end pos are not set yet
74
        if (this.start == null || this.end == null) {
75
            // Bounding box cannot be calculated
76
            return null;
77
        }
78
79
        // Create bounding box to go from laser ray start to end
80
        return new Box(this.start, this.end);
81
    }
82
83
    public int getColor() {
84
        return this.color;
85
    }
86
87
    /**
88
     * Gets the end position of the ray in world coordinates
89
     *
90
     * @return the position vector of the end position
91
     */
92
    public Vec3d getEnd() {
93
        return end;
94
    }
95
96
    /**
97
     * Gets the start position of the ray in world coordinates
98
     *
99
     * @return the position vector of the start position
100
     */
101
    public Vec3d getStart() { return start; }
102
103
    @Override
104
    protected void initDataTracker() {
105
        // Empty
106
    }
107
108
    @Override
109
    protected void readCustomDataFromNbt(NbtCompound nbt) {
110
111
    }
112
113
    @Override
114
    protected void writeCustomDataToNbt(NbtCompound nbt) {
115
116
    }
117
118
    /**
119
     * Send the spawn information to the client.
120
     * This manual step is necessary, because for some reason only living entities have this done automatically
121
     */
122
    @Override
123
    public Packet<?> createSpawnPacket() {
124
        PacketByteBuf buf = new PacketByteBuf(Unpooled.buffer());
125
126
        // Put position
127
        buf.writeDouble(getX());
128
        buf.writeDouble(getY());
129
        buf.writeDouble(getZ());
130
131
        // Put end position
132
        buf.writeDouble(end.x);
133
        buf.writeDouble(end.y);
134
        buf.writeDouble(end.z);
135
136
        // Put yaw & pitch
137
        buf.writeFloat(getYaw());
138
        buf.writeFloat(getPitch());
139
140
        // Put id
141
        buf.writeInt(getId());
142
        buf.writeUuid(getUuid());
143
144
        // Put color
145
        buf.writeInt(color);
146
147
        return ServerPlayNetworking.createS2CPacket(NetworkingConstants.LASER_RAY_SPAWNED, buf);
148
    }
149
}
150