179 lines
4.7 KiB
Java
179 lines
4.7 KiB
Java
package com.craftaro.epicanchors;
|
|
|
|
import com.craftaro.core.database.Data;
|
|
import com.craftaro.core.database.SerializedLocation;
|
|
import com.craftaro.epicanchors.api.Anchor;
|
|
import com.craftaro.epicanchors.utils.WorldUtils;
|
|
import org.bukkit.Bukkit;
|
|
import org.bukkit.Chunk;
|
|
import org.bukkit.Location;
|
|
import org.bukkit.World;
|
|
import org.bukkit.plugin.Plugin;
|
|
import org.jetbrains.annotations.NotNull;
|
|
import org.jetbrains.annotations.Nullable;
|
|
|
|
import java.util.LinkedHashMap;
|
|
import java.util.Map;
|
|
import java.util.Objects;
|
|
import java.util.UUID;
|
|
|
|
public class AnchorImpl implements Anchor {
|
|
private final int dbId;
|
|
|
|
private final UUID owner;
|
|
|
|
private final Location location;
|
|
private int ticksLeft;
|
|
|
|
/**
|
|
* Default constructor for deserialization.
|
|
*/
|
|
public AnchorImpl() {
|
|
dbId = 0;
|
|
owner = null;
|
|
location = null;
|
|
}
|
|
|
|
public AnchorImpl(int dbId, @Nullable UUID owner, @NotNull Location location, int ticksLeft) {
|
|
if (dbId <= 0) throw new IllegalArgumentException("Invalid value for dbId");
|
|
if (ticksLeft <= 0 && ticksLeft != -1) throw new IllegalArgumentException("Invalid value for ticksLeft");
|
|
|
|
Objects.requireNonNull(location.getWorld()); // Sanity check
|
|
|
|
this.dbId = dbId;
|
|
|
|
this.owner = owner;
|
|
|
|
this.location = location;
|
|
this.ticksLeft = ticksLeft;
|
|
}
|
|
|
|
/**
|
|
* <b></b>This method is automatically synchronized with the server's main thread using
|
|
* {@link org.bukkit.scheduler.BukkitScheduler#runTask(Plugin, Runnable)}</b>
|
|
*
|
|
* @see Bukkit#isPrimaryThread()
|
|
* @see org.bukkit.scheduler.BukkitScheduler#runTask(Plugin, Runnable)
|
|
*/
|
|
protected void init(Plugin plugin) {
|
|
if (Bukkit.isPrimaryThread()) {
|
|
WorldUtils.loadAnchoredChunk(getChunk(), plugin);
|
|
} else {
|
|
Bukkit.getScheduler().runTask(plugin, () -> init(plugin));
|
|
}
|
|
}
|
|
|
|
/**
|
|
* <b></b>This method is automatically synchronized with the server's main thread using
|
|
* {@link org.bukkit.scheduler.BukkitScheduler#runTask(Plugin, Runnable)}</b>
|
|
*
|
|
* @see Bukkit#isPrimaryThread()
|
|
* @see org.bukkit.scheduler.BukkitScheduler#runTask(Plugin, Runnable)
|
|
*/
|
|
protected void deInit(Plugin plugin) {
|
|
// TODO: Document that holograms are not removed or add boolean flag to remove them
|
|
|
|
if (Bukkit.isPrimaryThread()) {
|
|
WorldUtils.unloadAnchoredChunk(getChunk(), plugin);
|
|
} else {
|
|
Bukkit.getScheduler().runTask(plugin, () -> deInit(plugin));
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public int getDbId() {
|
|
return this.dbId;
|
|
}
|
|
|
|
@Override
|
|
public UUID getOwner() {
|
|
return this.owner;
|
|
}
|
|
|
|
@Override
|
|
public boolean isLegacy() {
|
|
return this.owner == null;
|
|
}
|
|
|
|
@Override
|
|
public @NotNull Location getLocation() {
|
|
return this.location.clone();
|
|
}
|
|
|
|
@Override
|
|
public @NotNull World getWorld() {
|
|
return this.location.getWorld();
|
|
}
|
|
|
|
@Override
|
|
public @NotNull Chunk getChunk() {
|
|
return this.location.getChunk();
|
|
}
|
|
|
|
@Override
|
|
public int getTicksLeft() {
|
|
return this.ticksLeft;
|
|
}
|
|
|
|
@Override
|
|
@SuppressWarnings("unused")
|
|
public void setTicksLeft(int ticksLeft) {
|
|
if (ticksLeft < 0) throw new IllegalArgumentException("Invalid value for ticksLeft");
|
|
|
|
this.ticksLeft = ticksLeft;
|
|
}
|
|
|
|
@Override
|
|
@SuppressWarnings("UnusedReturnValue")
|
|
public int addTicksLeft(int ticks) {
|
|
if (!isInfinite()) {
|
|
this.ticksLeft += ticks;
|
|
}
|
|
|
|
return this.ticksLeft;
|
|
}
|
|
|
|
@Override
|
|
public int removeTicksLeft(int ticks) {
|
|
if (!isInfinite()) {
|
|
this.ticksLeft -= ticks;
|
|
|
|
if (this.ticksLeft < 0) {
|
|
this.ticksLeft = 0;
|
|
}
|
|
}
|
|
|
|
return this.ticksLeft;
|
|
}
|
|
|
|
@Override
|
|
public boolean isInfinite() {
|
|
return this.ticksLeft == -1;
|
|
}
|
|
|
|
@Override
|
|
public Map<String, Object> serialize() {
|
|
Map<String, Object> map = new LinkedHashMap<>();
|
|
map.put("id", dbId);
|
|
map.putAll(SerializedLocation.of(location));
|
|
map.put("ticks_left", ticksLeft);
|
|
map.put("owner", owner == null ? null : owner.toString());
|
|
return map;
|
|
}
|
|
|
|
@Override
|
|
public Data deserialize(Map<String, Object> map) {
|
|
return new AnchorImpl(
|
|
(int) map.get("id"),
|
|
map.get("owner") == null ? null : UUID.fromString((String) map.get("owner")),
|
|
SerializedLocation.of(map),
|
|
(int) map.get("ticks_left")
|
|
);
|
|
}
|
|
|
|
@Override
|
|
public String getTableName() {
|
|
return "anchors";
|
|
}
|
|
}
|