Skip to content

Instantly share code, notes, and snippets.

@gepron1x
Last active November 23, 2020 18:30
Show Gist options
  • Save gepron1x/58c0e770a268f46d8c28cd8a7cf6f0df to your computer and use it in GitHub Desktop.
Save gepron1x/58c0e770a268f46d8c28cd8a7cf6f0df to your computer and use it in GitHub Desktop.
package me.gepron1x.customItemsAPI.utils.persistentdatatype;
import me.gepron1x.customItemsAPI.utils.DataTypes;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.NamespacedKey;
import org.bukkit.persistence.PersistentDataAdapterContext;
import org.bukkit.persistence.PersistentDataContainer;
import org.bukkit.persistence.PersistentDataType;
public class LocationTagType implements PersistentDataType<PersistentDataContainer, Location> {
private static final NamespacedKey WORLD = DataTypes.key("location_world");
private static final NamespacedKey X = DataTypes.key("location_x");
private static final NamespacedKey Y = DataTypes.key("location_y");
private static final NamespacedKey Z = DataTypes.key("location_z");
private static final NamespacedKey YAW = DataTypes.key("location_yaw");
private static final NamespacedKey PITCH = DataTypes.key("location_pitch");
@Override
public Class<PersistentDataContainer> getPrimitiveType() {
return PersistentDataContainer.class;
}
@Override
public Class<Location> getComplexType() {
return Location.class;
}
@Override
public PersistentDataContainer toPrimitive(Location location, PersistentDataAdapterContext context) {
PersistentDataContainer container = context.newPersistentDataContainer();
container.set(WORLD, DataTypes.UUID, location.getWorld().getUID());
container.set(X, PersistentDataType.DOUBLE, location.getX());
container.set(Y, PersistentDataType.DOUBLE, location.getY());
container.set(Z, PersistentDataType.DOUBLE, location.getZ());
container.set(YAW, PersistentDataType.FLOAT, location.getYaw());
container.set(PITCH, PersistentDataType.FLOAT, location.getPitch());
return container;
}
@Override
public Location fromPrimitive(PersistentDataContainer container, PersistentDataAdapterContext persistentDataAdapterContext) {
return new Location(Bukkit.getWorld(container.get(WORLD, DataTypes.UUID)),
container.get(X, PersistentDataType.DOUBLE),
container.get(Y, PersistentDataType.DOUBLE),
container.get(Z, PersistentDataType.DOUBLE),
container.get(YAW, PersistentDataType.FLOAT),
container.get(PITCH, PersistentDataType.FLOAT));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment