HolographicDisplays/core/src/main/java/me/filoghost/holographicdisplays/core/base/BaseHologramLines.java

149 lines
3.7 KiB
Java
Raw Normal View History

/*
* Copyright (C) filoghost and contributors
*
* SPDX-License-Identifier: GPL-3.0-or-later
*/
package me.filoghost.holographicdisplays.core.base;
2022-06-04 18:04:18 +02:00
import me.filoghost.holographicdisplays.api.Position;
import me.filoghost.holographicdisplays.core.CoreGlobalConfig;
2022-11-14 00:12:58 +01:00
import me.filoghost.holographicdisplays.core.CorePreconditions;
2021-08-23 19:28:15 +02:00
import org.jetbrains.annotations.NotNull;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
public class BaseHologramLines<T extends EditableHologramLine> implements Iterable<T> {
private final BaseHologram hologram;
private final List<T> lines;
private final List<T> unmodifiableLinesView;
public BaseHologramLines(BaseHologram hologram) {
this.hologram = hologram;
this.lines = new ArrayList<>();
this.unmodifiableLinesView = Collections.unmodifiableList(lines);
}
@Override
public Iterator<T> iterator() {
2022-11-14 00:12:58 +01:00
CorePreconditions.checkMainThread();
return unmodifiableLinesView.iterator();
}
public int size() {
2022-11-14 00:12:58 +01:00
CorePreconditions.checkMainThread();
return lines.size();
}
2021-08-23 19:28:15 +02:00
public @NotNull T get(int index) {
2022-11-14 00:12:58 +01:00
CorePreconditions.checkMainThread();
return lines.get(index);
}
public void add(T line) {
2022-11-14 00:12:58 +01:00
CorePreconditions.checkMainThread();
checkNotDeleted();
lines.add(line);
2021-08-23 19:28:15 +02:00
updatePositions();
}
2021-08-23 19:28:15 +02:00
public void insert(int beforeIndex, T line) {
2022-11-14 00:12:58 +01:00
CorePreconditions.checkMainThread();
checkNotDeleted();
2021-08-23 19:28:15 +02:00
lines.add(beforeIndex, line);
updatePositions();
}
public void remove(int index) {
2022-11-14 00:12:58 +01:00
CorePreconditions.checkMainThread();
checkNotDeleted();
lines.remove(index).setDeleted();
2021-08-23 19:28:15 +02:00
updatePositions();
}
public boolean remove(T line) {
2022-11-14 00:12:58 +01:00
CorePreconditions.checkMainThread();
checkNotDeleted();
boolean removed = lines.remove(line);
if (removed) {
line.setDeleted();
2021-08-23 19:28:15 +02:00
updatePositions();
}
return removed;
}
public void clear() {
2022-11-14 00:12:58 +01:00
CorePreconditions.checkMainThread();
checkNotDeleted();
Iterator<T> iterator = lines.iterator();
while (iterator.hasNext()) {
T line = iterator.next();
iterator.remove();
line.setDeleted();
}
// No need to update positions, since there are no lines
}
/**
* The top part of the first line should be exactly on the Y position of the hologram.
* The second line is below the first, and so on.
*/
2021-08-23 19:28:15 +02:00
public void updatePositions() {
Position hologramPosition = hologram.getPosition();
2021-08-19 15:36:10 +02:00
double currentLineY = hologramPosition.getY();
for (int i = 0; i < lines.size(); i++) {
T line = lines.get(i);
currentLineY -= line.getHeight();
if (i > 0) {
currentLineY -= CoreGlobalConfig.spaceBetweenLines;
}
2022-06-04 10:28:44 +02:00
line.setCoordinates(hologramPosition.getX(), currentLineY, hologramPosition.getZ());
}
}
2021-08-23 13:45:41 +02:00
public double getHeight() {
2022-11-14 00:12:58 +01:00
CorePreconditions.checkMainThread();
2022-06-04 17:30:21 +02:00
if (lines.isEmpty()) {
2021-08-23 13:45:41 +02:00
return 0;
}
double height = 0.0;
for (EditableHologramLine line : lines) {
height += line.getHeight();
}
height += CoreGlobalConfig.spaceBetweenLines * (lines.size() - 1);
2021-08-23 13:45:41 +02:00
return height;
}
public void setDeleted() {
for (T line : lines) {
line.setDeleted();
}
}
2021-08-23 19:28:15 +02:00
protected void checkNotDeleted() {
hologram.checkNotDeleted();
}
@Override
public String toString() {
return lines.toString();
}
}