Skip to content

Commit

Permalink
Turns out that EntityPersistor wasn't working for 1.8 because of ambi…
Browse files Browse the repository at this point in the history
…guous method definitions in the Spigot API. Added an alternative method that returns an EntityPersistor<T>. More annoying, but at least it works. To my knowledge, the standard persist method will work for versions above 1.8.
  • Loading branch information
boxbeam committed May 27, 2020
1 parent f2c62b2 commit 1536676
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 13 deletions.
49 changes: 48 additions & 1 deletion src/redempt/redlib/misc/EntityPersistor.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package redempt.redlib.misc;

import org.bukkit.Bukkit;
import org.bukkit.World;
import org.bukkit.entity.Entity;
import org.bukkit.entity.LivingEntity;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
Expand All @@ -14,7 +16,7 @@
* the Entity instance is still valid. If it isn't, it will attempt to replace it with a valid instance by re-fetching
* the Entity from Bukkit.
*/
public class EntityPersistor {
public class EntityPersistor<T extends Entity> {

/**
* Wraps an Entity object with a proxy which will attempt to ensure the Entity object remains valid
Expand All @@ -23,6 +25,7 @@ public class EntityPersistor {
* interact with {@link Object#equals(Object)} reflexively. You must call .equals() on the Entity
* which has been wrapped, not on another Entity comparing it to this one. This could not be avoided,
* unfortunately, but as long as you are aware of that, it should work fine.
* Seems to break in 1.8 because of API fuckery. Use {@link EntityPersistor#wrap(Entity)}
* @param entity The Entity to wrap
* @param <T> The type of the Entity
* @return The wrapped Entity
Expand All @@ -37,6 +40,7 @@ public static <T extends Entity> T persist(T entity) {
break;
}
}
System.out.println(clazz.getName());
if (!foundInterface) {
throw new IllegalArgumentException("The provided object cannot be wrapped!");
}
Expand Down Expand Up @@ -64,4 +68,47 @@ public Object invoke(Object proxy, Method method, Object[] args) throws Throwabl
});
}

/**
* Wraps an Entity in an EntityPersistor. Calling {@link EntityPersistor#get()} will refresh the reference
* if it is invalid. Use for 1.8
* @param entity The Entity to wrap
* @param <T> The type of the Entity
* @return An EntityPersistor wrapping the given Entity
*/
public static <T extends Entity> EntityPersistor<T> wrap(T entity) {
return new EntityPersistor<T>(entity);
}

private T entity;

private EntityPersistor(T entity) {
this.entity = entity;
}

/**
* Gets the Entity held in this EntityPersistor. If the reference is invalid, the EntityPersistor will attempt
* to refresh it.
* @return
*/
public T get() {
refresh:
if (!entity.isValid()) {
for (Entity entity : this.entity.getLocation().getChunk().getEntities()) {
if (entity.getUniqueId().equals(this.entity.getUniqueId())) {
this.entity = (T) entity;
break refresh;
}
}
for (World world : Bukkit.getWorlds()) {
for (Entity entity : world.getEntities()) {
if (this.entity.getUniqueId().equals(entity.getUniqueId())) {
this.entity = (T) entity;
break;
}
}
}
}
return entity;
}

}
26 changes: 14 additions & 12 deletions src/redempt/redlib/misc/Hologram.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.util.Arrays;
import java.util.List;
import java.util.Random;
import java.util.stream.Collectors;

import org.bukkit.Bukkit;
import org.bukkit.Location;
Expand Down Expand Up @@ -84,7 +85,7 @@ public static Hologram getAt(Location loc) {
}

private int id;
private List<ArmorStand> stands = new ArrayList<>();
private List<EntityPersistor<ArmorStand>> stands = new ArrayList<>();
private Location start;
private double lineSpacing = 0.35;
private int task = -1;
Expand All @@ -105,7 +106,8 @@ private Hologram(int id, Location start) {

private Hologram(int id, Location start, List<ArmorStand> stands) {
this.id = id;
this.stands = stands;
this.stands.clear();
stands.stream().map(EntityPersistor::wrap).forEach(this.stands::add);
this.start = start;
}

Expand All @@ -121,15 +123,15 @@ private void fixStands(int start) {
start = iter;
}
for (int i = start; i < stands.size(); i++) {
ArmorStand stand = stands.get(i);
ArmorStand stand = stands.get(i).get();
stand.teleport(new Location(loc.getWorld(), 0, 1, 0));
locs[i] = loc.clone();
loc.subtract(0, lineSpacing, 0);
}
iter = start;
task = Bukkit.getScheduler().scheduleSyncDelayedTask(RedLib.getInstance(), () -> {
for (int i = iter; i < locs.length; i++) {
stands.get(i).teleport(locs[i]);
stands.get(i).get().teleport(locs[i]);
}
task = -1;
iter = -1;
Expand All @@ -141,7 +143,7 @@ private void fixStands(int start) {
* @return The line of text at the given index
*/
public String getLine(int line) {
return stands.get(line).getCustomName();
return stands.get(line).get().getCustomName();
}

/**
Expand All @@ -164,7 +166,7 @@ public Location getLocation() {
* @return All the ArmorStands in this Hologram
*/
public List<ArmorStand> getStands() {
return stands;
return stands.stream().map(EntityPersistor::get).collect(Collectors.toList());
}

/**
Expand All @@ -173,15 +175,15 @@ public List<ArmorStand> getStands() {
* @param text The text to set the line to
*/
public void setLine(int line, String text) {
stands.get(line).setCustomName(text);
stands.get(line).get().setCustomName(text);
}

/**
* Removes a line from this Hologram
* @param line The line number to remove
*/
public void remove(int line) {
ArmorStand stand = stands.remove(line);
ArmorStand stand = stands.remove(line).get();
stand.remove();
fixStands(0);
}
Expand All @@ -190,7 +192,7 @@ public void remove(int line) {
* Clears this Hologram
*/
public void clear() {
stands.forEach(ArmorStand::remove);
stands.forEach(s -> s.get().remove());
stands.clear();
}

Expand All @@ -216,7 +218,7 @@ public void setLineSpacing(double lineSpacing) {
*/
public void append(String text) {
ArmorStand stand = spawn(stands.size(), text);
stands.add(stand);
stands.add(EntityPersistor.wrap(stand));
fixStands(stands.size() - 1);
}

Expand All @@ -235,7 +237,7 @@ public void prepend(String text) {
*/
public void insert(int line, String text) {
ArmorStand stand = spawn(line, text);
stands.add(line, stand);
stands.add(line, EntityPersistor.wrap(stand));
fixStands(line - 1);
}

Expand All @@ -249,7 +251,7 @@ public int size() {
private ArmorStand spawn(int line, String text) {
Location loc;
if (stands.size() > 0) {
loc = stands.get(0).getLocation();
loc = stands.get(0).get().getLocation();
loc.subtract(0, lineSpacing * (double) line, 0);
} else {
loc = start;
Expand Down

0 comments on commit 1536676

Please sign in to comment.