More things for #11 😄

This commit is contained in:
Jitse Boonstra
2019-10-21 20:21:03 +02:00
parent 4d2140d24c
commit 3dceca16b9
7 changed files with 179 additions and 87 deletions
@@ -1,4 +1,17 @@
package net.jitse.npclib.hologram;
import org.bukkit.entity.Player;
import java.util.List;
import java.util.UUID;
public interface Hologram {
void show(Player player);
void hide(Player player);
void silentHide(UUID uuid);
void updateText(List<String> text);
}
@@ -1,5 +1,6 @@
package net.jitse.npclib.hologram;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.entity.Player;
@@ -18,17 +19,68 @@ public abstract class HologramBase implements Hologram, HologramPacketHandler {
protected List<String> text;
public HologramBase(Location start, List<String> text) {
this.start = start;
this.text = text;
// Generate the necessary show and hide packets.
createPackets();
}
abstract public void show(Player player);
@Override
public void show(Player player) {
UUID uuid = player.getUniqueId();
if (shown.contains(uuid))
throw new IllegalArgumentException("Hologram is already shown to player");
abstract public void hide(Player player);
sendShowPackets(player);
abstract public void silentHide(UUID uuid);
this.shown.add(uuid);
}
abstract public void updateText(List<String> text);
@Override
public void hide(Player player) {
UUID uuid = player.getUniqueId();
if (!shown.contains(uuid))
throw new IllegalArgumentException("Hologram is not shown to player");
sendHidePackets(player);
this.shown.remove(uuid);
}
@Override
public void silentHide(UUID uuid) {
if (!shown.contains(uuid))
throw new IllegalArgumentException("Hologram is not shown to player");
this.shown.remove(uuid);
}
@Override
public void updateText(List<String> text) {
if (this.text.size() != text.size())
throw new IllegalArgumentException("When updating the text, the old and new text should have the same amount of lines");
for (int i = 0; i < text.size(); i++) {
String oldLine = this.text.get(i);
String newLine = text.get(i);
if (oldLine.equals(newLine))
continue; // No need to update.
// Perhaps this should return an object so we can send all immediately to the shown players.
createTextUpdatePacket(oldLine, newLine);
}
for (UUID uuid : shown) {
Player player = Bukkit.getPlayer(uuid);
if (player == null || !player.isOnline()) {
throw new IllegalStateException("Tried to update hologram for offline player");
}
// sendTextUpdatePackets(player, );
}
this.text = text;
}
}
@@ -1,6 +1,16 @@
package net.jitse.npclib.hologram;
import org.bukkit.entity.Player;
public interface HologramPacketHandler {
void sendTextUpdatePackets(int index, String newLine);
void createPackets();
void sendShowPackets(Player player);
void sendHidePackets(Player player);
void createTextUpdatePacket(String oldLine, String newLine);
void sendTextUpdatePackets(Player player);
}