Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
* 1.17 -> 1.20
*
* @version 1.2.2
* @author SkytAsul
*/
public class GlowingEntities implements Listener {
private final @NotNull Plugin plugin;
private Map glowing;
private boolean enabled = false;
private int uuid;
/**
* Initializes the Glowing API.
*
* @param plugin plugin that will be used to register the events.
*/
public GlowingEntities(@NotNull Plugin plugin) {
if (!Packets.enabled)
throw new IllegalStateException(
"The Glowing Entities API is disabled. An error has occured during initialization.");
this.plugin = Objects.requireNonNull(plugin);
enable();
}
/**
* Enables the Glowing API.
*
* @see #disable()
*/
public void enable() {
if (enabled)
throw new IllegalStateException("The Glowing Entities API has already been enabled.");
plugin.getServer().getPluginManager().registerEvents(this, plugin);
glowing = new HashMap<>();
uuid = ThreadLocalRandom.current().nextInt(Integer.MAX_VALUE);
enabled = true;
}
/**
* Disables the API.
*
* Methods such as {@link #setGlowing(int, String, Player, ChatColor, byte)} and
* {@link #unsetGlowing(int, Player)} will no longer be usable.
*
* @see #enable()
*/
public void disable() {
if (!enabled)
return;
HandlerList.unregisterAll(this);
glowing.values().forEach(playerData -> {
try {
Packets.removePacketsHandler(playerData);
} catch (ReflectiveOperationException e) {
e.printStackTrace();
}
});
glowing = null;
uuid = 0;
enabled = false;
}
private void ensureEnabled() {
if (!enabled)
throw new IllegalStateException("The Glowing Entities API is not enabled.");
}
@EventHandler
public void onQuit(PlayerQuitEvent event) {
glowing.remove(event.getPlayer());
}
/**
* Make the {@link Entity} passed as a parameter glow with its default team color.
*
* @param entity entity to make glow
* @param receiver player which will see the entity glowing
* @throws ReflectiveOperationException
*/
public void setGlowing(Entity entity, Player receiver) throws ReflectiveOperationException {
setGlowing(entity, receiver, null);
}
/**
* Make the {@link Entity} passed as a parameter glow with the specified color.
*
* @param entity entity to make glow
* @param receiver player which will see the entity glowing
* @param color color of the glowing effect
* @throws ReflectiveOperationException
*/
public void setGlowing(Entity entity, Player receiver, ChatColor color) throws ReflectiveOperationException {
String teamID = entity instanceof Player ? entity.getName() : entity.getUniqueId().toString();
setGlowing(entity.getEntityId(), teamID, receiver, color, Packets.getEntityFlags(entity));
}
/**
* Make the entity with specified entity ID glow with its default team color.
*
* @param entityID entity id of the entity to make glow
* @param teamID internal string used to add the entity to a team
* @param receiver player which will see the entity glowing
* @throws ReflectiveOperationException
*/
public void setGlowing(int entityID, String teamID, Player receiver) throws ReflectiveOperationException {
setGlowing(entityID, teamID, receiver, null, (byte) 0);
}
/**
* Make the entity with specified entity ID glow with the specified color.
*
* @param entityID entity id of the entity to make glow
* @param teamID internal string used to add the entity to a team
* @param receiver player which will see the entity glowing
* @param color color of the glowing effect
* @throws ReflectiveOperationException
*/
public void setGlowing(int entityID, String teamID, Player receiver, ChatColor color)
throws ReflectiveOperationException {
setGlowing(entityID, teamID, receiver, color, (byte) 0);
}
/**
* Make the entity with specified entity ID glow with the specified color, and keep some flags.
*
* @param entityID entity id of the entity to make glow
* @param teamID internal string used to add the entity to a team
* @param receiver player which will see the entity glowing
* @param color color of the glowing effect
* @param otherFlags internal flags that must be kept (on fire, crouching...). See
* wiki.vg for more informations.
* @throws ReflectiveOperationException
*/
public void setGlowing(int entityID, String teamID, Player receiver, ChatColor color, byte otherFlags)
throws ReflectiveOperationException {
ensureEnabled();
if (color != null && !color.isColor())
throw new IllegalArgumentException("ChatColor must be a color format");
PlayerData playerData = glowing.get(receiver);
if (playerData == null) {
playerData = new PlayerData(this, receiver);
Packets.addPacketsHandler(playerData);
glowing.put(receiver, playerData);
}
GlowingData glowingData = playerData.glowingDatas.get(entityID);
if (glowingData == null) {
// the player did not have datas related to the entity: we must create the glowing status
glowingData = new GlowingData(playerData, entityID, teamID, color, otherFlags);
playerData.glowingDatas.put(entityID, glowingData);
Packets.createGlowing(glowingData);
if (color != null)
Packets.setGlowingColor(glowingData);
} else {
// the player already had datas related to the entity: we must update the glowing status
if (Objects.equals(glowingData.color, color))
return; // nothing changed
if (color == null) {
Packets.removeGlowingColor(glowingData);
glowingData.color = color; // we must set the color after in order to fetch the previous team
} else {
glowingData.color = color;
Packets.setGlowingColor(glowingData);
}
}
}
/**
* Make the {@link Entity} passed as a parameter loose its custom glowing effect.
*
* This has no effect on glowing status given by another plugin or vanilla behavior.
*
* @param entity entity to remove glowing effect from
* @param receiver player which will no longer see the glowing effect
* @throws ReflectiveOperationException
*/
public void unsetGlowing(Entity entity, Player receiver) throws ReflectiveOperationException {
unsetGlowing(entity.getEntityId(), receiver);
}
/**
* Make the entity with specified entity ID passed as a parameter loose its custom glowing effect.
*
* This has no effect on glowing status given by another plugin or vanilla behavior.
*
* @param entityID entity id of the entity to remove glowing effect from
* @param receiver player which will no longer see the glowing effect
* @throws ReflectiveOperationException
*/
public void unsetGlowing(int entityID, Player receiver) throws ReflectiveOperationException {
ensureEnabled();
PlayerData playerData = glowing.get(receiver);
if (playerData == null)
return; // the player do not have any entity glowing
GlowingData glowingData = playerData.glowingDatas.remove(entityID);
if (glowingData == null)
return; // the player did not have this entity glowing
Packets.removeGlowing(glowingData);
if (glowingData.color != null)
Packets.removeGlowingColor(glowingData);
/*
* if (playerData.glowingDatas.isEmpty()) { //NOSONAR // if the player do not have any other entity
* glowing, // we can safely remove all of its data to free some memory
* Packets.removePacketsHandler(playerData); glowing.remove(receiver); }
*/
// actually no, we should not remove the player datas
// as it stores which teams did it receive.
// if we do not save this information, team would be created
// twice for the player, and BungeeCord does not like that
}
private static class PlayerData {
final GlowingEntities instance;
final Player player;
final Map glowingDatas;
ChannelHandler packetsHandler;
EnumSet sentColors;
PlayerData(GlowingEntities instance, Player player) {
this.instance = instance;
this.player = player;
this.glowingDatas = new HashMap<>();
}
}
private static class GlowingData {
// unfortunately this cannot be a Java Record
// as the "color" field is not final
final PlayerData player;
final int entityID;
final String teamID;
ChatColor color;
byte otherFlags;
boolean enabled;
GlowingData(PlayerData player, int entityID, String teamID, ChatColor color, byte otherFlags) {
this.player = player;
this.entityID = entityID;
this.teamID = teamID;
this.color = color;
this.otherFlags = otherFlags;
this.enabled = true;
}
}
private static class Packets {
private static final byte GLOWING_FLAG = 1 << 6;
private static Cache