forked from Tofaa2/EntityLib
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModernHologram.java
More file actions
219 lines (186 loc) · 6.14 KB
/
Copy pathModernHologram.java
File metadata and controls
219 lines (186 loc) · 6.14 KB
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
package me.tofaa.entitylib.wrapper.hologram;
import com.github.retrooper.packetevents.protocol.entity.type.EntityTypes;
import com.github.retrooper.packetevents.protocol.world.Location;
import me.tofaa.entitylib.meta.display.TextDisplayMeta;
import me.tofaa.entitylib.utils.Check;
import me.tofaa.entitylib.wrapper.WrapperEntity;
import net.kyori.adventure.text.Component;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.function.Consumer;
final class ModernHologram implements Hologram.Modern {
private Location location;
private final List<WrapperEntity> lines = new ArrayList<>(3);
private Consumer<TextDisplayMeta> modifier;
private boolean spawned = false;
private WrapperEntity parent;
ModernHologram(@NotNull Location location) {
this.location = location;
}
ModernHologram(@NotNull Location location, List<Component> lines) {
this(location);
for (Component line : lines) {
addLine(line);
}
}
@Override
public void show() {
for (WrapperEntity line : lines) {
line.spawn(location);
}
teleport(location);
spawned = true;
}
@Override
public void hide() {
for (WrapperEntity line : lines) {
line.despawn();
}
spawned = false;
}
private static final float LINE_SPACING = 0.28f;
@Override
public void teleport(Location location) {
this.location = location;
if (lines.isEmpty()) return;
if (parent != null) {
return;
}
for (int i = 0; i < lines.size(); i++) {
double y = location.getY() + (lines.size() - 1 - i) * LINE_SPACING;
Location lineLoc = new Location(location.getX(), y, location.getZ(), location.getYaw(), location.getPitch());
lines.get(i).teleport(lineLoc);
}
}
@Override
public void setParent(@NotNull WrapperEntity parent) {
this.parent = parent;
if (lines.isEmpty()) return;
WrapperEntity first = lines.get(0);
for (WrapperEntity e : lines) {
if (e.getUuid().equals(first.getUuid())) continue;
try {
first.addPassenger(e);
} catch (Exception ignored) {}
}
try {
parent.addPassenger(first);
} catch (Exception ignored) {}
}
@Override
public boolean updateLineContent(int index, @NotNull Component line) {
if (index < 0 || index >= lines.size()) {
return false;
}
TextDisplayMeta meta = (TextDisplayMeta) lines.get(index).getEntityMeta();
meta.setText(line);
return true;
}
@Override
public void setLines(List<Component> newLines) {
int existingCount = lines.size();
int newCount = newLines.size();
if (newCount == 0) {
for (WrapperEntity line : lines) {
line.remove();
}
lines.clear();
return;
}
for (int i = 0; i < Math.min(existingCount, newCount); i++) {
TextDisplayMeta meta = (TextDisplayMeta) lines.get(i).getEntityMeta();
meta.setText(newLines.get(i));
}
for (int i = existingCount - 1; i >= newCount; i--) {
lines.get(i).remove();
lines.remove(i);
}
for (int i = existingCount; i < newCount; i++) {
WrapperEntity e = new WrapperEntity(EntityTypes.TEXT_DISPLAY);
TextDisplayMeta meta = (TextDisplayMeta) e.getEntityMeta();
meta.setInvisible(true);
meta.setHasNoGravity(true);
meta.setText(newLines.get(i));
meta.setBillboardConstraints(me.tofaa.entitylib.meta.display.AbstractDisplayMeta.BillboardConstraints.CENTER);
if (this.modifier != null) {
this.modifier.accept(meta);
}
if (spawned) {
e.spawn(location);
}
lines.add(e);
}
if (spawned && parent == null) {
teleport(location);
}
}
@Override
public @Nullable Component getLine(int index) {
if (index < 0 || index >= lines.size()) {
return null;
}
TextDisplayMeta meta = (TextDisplayMeta) lines.get(index).getEntityMeta();
return meta.getText();
}
@Override
public void setLine(int index, @Nullable Component line) {
WrapperEntity e = new WrapperEntity(EntityTypes.TEXT_DISPLAY);
e.spawn(location);
TextDisplayMeta meta = (TextDisplayMeta) e.getEntityMeta();
meta.setInvisible(true);
meta.setHasNoGravity(true);
meta.setText(line);
meta.setBillboardConstraints(me.tofaa.entitylib.meta.display.AbstractDisplayMeta.BillboardConstraints.CENTER);
if (this.modifier != null) {
this.modifier.accept(meta);
}
Check.arrayLength(lines, index, e);
if (spawned) {
e.spawn(location);
teleport(location);
}
}
@Override
public void removeLine(int index) {
if (index < 0 || index >= lines.size()) {
return;
}
this.lines.get(index).remove();
this.lines.remove(index);
}
@Override
public void addLine(@Nullable Component line) {
setLine(lines.size(), line);
}
@Override
public void addViewer(@NotNull UUID viewer) {
for (WrapperEntity line : lines) {
line.addViewer(viewer);
}
}
@Override
public void removeViewer(@NotNull UUID viewer) {
for (WrapperEntity line : lines) {
line.removeViewer(viewer);
}
}
@Override
public int length() {
return lines.size();
}
@Override
public @NotNull Location getLocation() {
return location;
}
@Override
public @NotNull WrapperEntity getEntity() {
return lines.isEmpty() ? null : lines.get(0);
}
@Override
public void setModifier(@NotNull Consumer<TextDisplayMeta> consumer) {
this.modifier = consumer;
}
}