Skip to content

Commit

Permalink
Removed support for legacy versions, fixed blood effect
Browse files Browse the repository at this point in the history
  • Loading branch information
jsuarezl committed Aug 18, 2024
1 parent ec7563c commit 7eb6e66
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 89 deletions.
6 changes: 4 additions & 2 deletions src/cl/mastercode/DamageIndicator/DIMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,10 @@ private void startTasks() {
}

public boolean isDamageIndicator(Entity entity) {
ArmorStand as = (ArmorStand) entity;
return as.hasMetadata("Mastercode-DamageIndicator") && as.isMarker() && !as.isVisible() && as.isCustomNameVisible() && !as.hasGravity();
if (entity instanceof ArmorStand armorStand && entity.isValid()) {
return armorStand.hasMetadata("Mastercode-DamageIndicator") && armorStand.isMarker() && !armorStand.isVisible() && armorStand.isCustomNameVisible() && !armorStand.hasGravity();
}
return false;
}

public StorageProvider getStorageProvider() {
Expand Down
35 changes: 3 additions & 32 deletions src/cl/mastercode/DamageIndicator/listener/BloodListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,12 @@
import cl.mastercode.DamageIndicator.util.ConfigUtil;
import org.bukkit.Color;
import org.bukkit.Effect;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.Particle;
import org.bukkit.World;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Item;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.event.Cancellable;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
Expand All @@ -44,7 +41,6 @@
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.metadata.FixedMetadataValue;
import org.bukkit.util.Vector;
import java.lang.reflect.Method;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.Map;
Expand All @@ -71,19 +67,9 @@ public class BloodListener implements Listener {
private boolean enableMonster = true;
private boolean enableAnimal = true;
private boolean sneaking = true;
private Method playEffect;

public BloodListener(DIMain plugin) {
this.plugin = plugin;
if (!CompatUtil.is113orHigher()) {
try {
playEffect = World.Spigot.class.getMethod("playEffect", Location.class, Effect.class, int.class, int.class, float.class, float.class, float.class, float.class, int.class, int.class);
} catch (ReflectiveOperationException e) {
plugin.getLogger().log(Level.WARNING, "Error using legacy blood particles, please report this issue.", e);
}
} else {
playEffect = null;
}
reload();
}

Expand Down Expand Up @@ -143,23 +129,8 @@ public void onEntityDamage(EntityDamageEvent e) {
if (!showBlood(entity, e.getCause(), e.getFinalDamage())) {
return;
}
if (CompatUtil.MINOR_VERSION >= 20) {
e.getEntity().getWorld().spawnParticle(Particle.valueOf("DUST"), ((LivingEntity) e.getEntity()).getEyeLocation(), 7, .5, 1, .5, new Particle.DustOptions(Color.RED, 3f));
} else if (CompatUtil.is113orHigher()) {
e.getEntity().getWorld().spawnParticle(Particle.valueOf("REDSTONE"), ((LivingEntity) e.getEntity()).getEyeLocation(), 7, .5, 1, .5, new Particle.DustOptions(Color.RED, 3f));
} else if (CompatUtil.MINOR_VERSION == 8) {
try {
if (playEffect != null) {
playEffect.invoke(e.getEntity().getWorld().spigot(), ((LivingEntity) e.getEntity()).getEyeLocation(), Effect.valueOf("COLOURED_DUST"), 0, 0, 0.4f, 0.3f, 0.4f, 0, 8, 16);
}
} catch (ReflectiveOperationException exception) {
plugin.getLogger().log(Level.WARNING, "Error trying to spawn blood particles, please report this issue.", exception);
}
} else {
for (int i = 0; i < 5; i++) {
e.getEntity().getNearbyEntities(20, 20, 20).stream().filter(nearbyEntity -> nearbyEntity instanceof Player).map(nearbyEntity -> (Player) nearbyEntity).forEach(player -> player.spawnParticle(Particle.valueOf("REDSTONE"), ((LivingEntity) e.getEntity()).getEyeLocation().clone().add(random.nextDouble(), random.nextDouble(), random.nextDouble()), 0, 255, 0, 0, 1));
}
}
LivingEntity livingEntity = (LivingEntity) entity;
e.getEntity().getWorld().spawnParticle(CompatUtil.BLOOD_PARTICLE, livingEntity.getEyeLocation(), 7, .5, .2, .5, 10, new Particle.DustOptions(Color.fromBGR(61, 61, 255), 3f));
}

@EventHandler(priority = EventPriority.MONITOR)
Expand Down Expand Up @@ -194,7 +165,7 @@ public void onPlayerDeath(PlayerDeathEvent e) {
return;
}
for (int i = 0; i < 14; i++) {
ItemStack is = new ItemStack(CompatUtil.RED_INK);
ItemStack is = new ItemStack(new ItemStack(Material.RED_DYE));
ItemMeta meta = is.getItemMeta();
meta.setDisplayName(BLOOD_NAME);
is.setItemMeta(meta);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import cl.mastercode.DamageIndicator.util.ConfigUtil;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.attribute.Attribute;
import org.bukkit.entity.ArmorStand;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
Expand Down Expand Up @@ -158,20 +159,19 @@ public void onChunkLoad(ChunkLoadEvent event) {

@EventHandler(priority = EventPriority.MONITOR)
public void onEntityRegainHealth(EntityRegainHealthEvent e) {
if (!(e.getEntity() instanceof LivingEntity)) {
if (!(e.getEntity() instanceof LivingEntity livingEntity)) {
return;
}
if (e.getEntity() instanceof Player) {
Player player = (Player) e.getEntity();
if (e.getEntity() instanceof Player player) {
if (player.isSneaking() && !sneaking) {
return;
}
}
if (((LivingEntity) e.getEntity()).getHealth() == CompatUtil.getMaxHealth((LivingEntity) e.getEntity())) {
if (livingEntity.getHealth() == Objects.requireNonNull(livingEntity.getAttribute(Attribute.GENERIC_MAX_HEALTH)).getValue()) {
return;
}
if (!e.isCancelled()) {
handleArmorStand((LivingEntity) e.getEntity(), e.getAmount());
handleArmorStand(livingEntity, e.getAmount());
}
}

Expand Down
59 changes: 9 additions & 50 deletions src/cl/mastercode/DamageIndicator/util/CompatUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,9 @@
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.attribute.Attribute;
import org.bukkit.Particle;
import org.bukkit.entity.ArmorStand;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.LivingEntity;
import org.bukkit.inventory.ItemStack;
import org.bukkit.metadata.FixedMetadataValue;
import java.util.Objects;

/**
* Class to manage compatibility with older minecraft versions.
Expand All @@ -39,37 +34,18 @@ public final class CompatUtil {

private static final MiniMessage miniMessage = MiniMessage.miniMessage();
private static final LegacyComponentSerializer legacySerializer = BukkitComponentSerializer.legacy();
public static ItemStack RED_INK = null;
public static int MINOR_VERSION = 8;
public static Particle BLOOD_PARTICLE = null;
public static int MINOR_VERSION = 16;

public static void onEnable() {
MINOR_VERSION = _getMinorVersion();
if (MINOR_VERSION >= 13) {
if (MINOR_VERSION == 13) {
RED_INK = new ItemStack(Material.valueOf("ROSE_RED"));
} else {
RED_INK = new ItemStack(Material.RED_DYE);
}
if (MINOR_VERSION >= 20) {
BLOOD_PARTICLE = Particle.valueOf("DUST");
} else {
RED_INK = new ItemStack(Material.valueOf("INK_SACK"), 1, (short) 1);
BLOOD_PARTICLE = Particle.valueOf("REDSTONE");
}
}

public static boolean isCanSetCollidable() {
return MINOR_VERSION > 8;
}

public static boolean is113orHigher() {
return _getMinorVersion() > 13;
}

public static double getMaxHealth(LivingEntity livingEntity) {
if (MINOR_VERSION > 8) {
return Objects.requireNonNull(livingEntity.getAttribute(Attribute.GENERIC_MAX_HEALTH)).getValue();
}
return livingEntity.getMaxHealth();
}

private static int _getMinorVersion() {
String ver = Bukkit.getServer().getBukkitVersion();
int verInt = -1;
Expand All @@ -86,38 +62,21 @@ private static int _getMinorVersion() {
}

public static ArmorStand buildArmorStand(Location location, double distance, FixedMetadataValue fixedMetadataValue, String name) {
ArmorStand armorStand;
if (MINOR_VERSION >= 11) {
armorStand = modernEntitySpawn(location, distance, fixedMetadataValue);
} else {
armorStand = legacyEntitySpawn(location, distance, fixedMetadataValue);
}
ArmorStand armorStand = location.getWorld().spawn(location.clone().add(0, location.getWorld().getMaxHeight() - location.getY(), 0), ArmorStand.class, stand -> setStandProperties(stand, location, distance, fixedMetadataValue));
armorStand.setCustomName(legacySerializer.serialize(miniMessage.deserialize(name)));
armorStand.setCustomNameVisible(true);
return armorStand;
}

private static ArmorStand modernEntitySpawn(Location location, double distance, FixedMetadataValue fixedMetadataValue) {
return location.getWorld().spawn(location.clone().add(0, location.getWorld().getMaxHeight() - location.getY(), 0), ArmorStand.class, stand -> setStandProperties(stand, location, distance, fixedMetadataValue));
}

private static ArmorStand legacyEntitySpawn(Location location, double distance, FixedMetadataValue fixedMetadataValue) {
ArmorStand armorStand = (ArmorStand) location.getWorld().spawnEntity(location, EntityType.ARMOR_STAND);
setStandProperties(armorStand, location, distance, fixedMetadataValue);
return armorStand;
}

private static void setStandProperties(ArmorStand armorStand, Location location, double distance, FixedMetadataValue fixedMetadataValue) {
armorStand.setVisible(false);
armorStand.setGravity(false);
armorStand.setMarker(true);
armorStand.setSmall(true);
armorStand.setCustomNameVisible(false);
armorStand.setMetadata("Mastercode-DamageIndicator", fixedMetadataValue);
if (CompatUtil.isCanSetCollidable()) {
armorStand.setCollidable(false);
armorStand.setInvulnerable(true);
}
armorStand.setCollidable(false);
armorStand.setInvulnerable(true);
armorStand.teleport(location.clone().add(0, distance, 0));
armorStand.setRemoveWhenFarAway(true);
}
Expand Down

0 comments on commit 7eb6e66

Please sign in to comment.