From 810d01ad028a3c4f715bb35fe045a7c1a0847690 Mon Sep 17 00:00:00 2001 From: YouHaveTrouble Date: Wed, 18 May 2022 19:01:33 +0200 Subject: [PATCH] rework lightning transforms mobs --- pom.xml | 9 +++ .../purpurextras/config/PurpurConfig.java | 18 +---- .../LightningTransformsMobsListener.java | 75 +++++++++++++++++-- 3 files changed, 78 insertions(+), 24 deletions(-) diff --git a/pom.xml b/pom.xml index eecf39a..e54c28a 100644 --- a/pom.xml +++ b/pom.xml @@ -71,6 +71,10 @@ Minecraft Libraries https://libraries.minecraft.net + + jitpack.io + https://jitpack.io + @@ -86,5 +90,10 @@ 1.18.2-R0.1-SNAPSHOT provided + + com.github.YouHaveTrouble + Entiddy + 1.2.0 + diff --git a/src/main/java/me/youhavetrouble/purpurextras/config/PurpurConfig.java b/src/main/java/me/youhavetrouble/purpurextras/config/PurpurConfig.java index 7502d64..e527519 100644 --- a/src/main/java/me/youhavetrouble/purpurextras/config/PurpurConfig.java +++ b/src/main/java/me/youhavetrouble/purpurextras/config/PurpurConfig.java @@ -29,7 +29,7 @@ public class PurpurConfig { public final String beeHiveLoreBees, beeHiveLoreHoney; public final HashMap anvilCrushBlocksIndex = new HashMap<>(); public final HashSet stonecutterDamageBlacklist = new HashSet<>(); - public final HashMap lightningTransformEntities = new HashMap<>(); + public final HashMap lightningTransformEntities = new HashMap<>(); public final double furnaceBurnTimeMultiplier; public PurpurConfig() { @@ -146,21 +146,7 @@ private void handleLightningTransformedEntities(boolean enable) { if (!enable) return; for (String key : section.getKeys(false)) { String value = section.getString(key); - if (value == null) continue; - EntityType keyType = null; - EntityType valueType = null; - for (EntityType entityType : EntityType.values()) { - if (!entityType.isSpawnable()) continue; - String entityKey = entityType.getKey().getKey(); - if (entityKey.equals(key.toLowerCase(Locale.ROOT))) { - keyType = entityType; - } - if (entityKey.equals(value.toLowerCase(Locale.ROOT))) { - valueType = entityType; - } - } - if (keyType == null || valueType == null) continue; - lightningTransformEntities.put(keyType, valueType); + lightningTransformEntities.put(key, value); } if (lightningTransformEntities.isEmpty()) return; plugin.registerListener(LightningTransformsMobsListener.class); diff --git a/src/main/java/me/youhavetrouble/purpurextras/listeners/LightningTransformsMobsListener.java b/src/main/java/me/youhavetrouble/purpurextras/listeners/LightningTransformsMobsListener.java index d5ab441..dd19410 100644 --- a/src/main/java/me/youhavetrouble/purpurextras/listeners/LightningTransformsMobsListener.java +++ b/src/main/java/me/youhavetrouble/purpurextras/listeners/LightningTransformsMobsListener.java @@ -1,27 +1,86 @@ package me.youhavetrouble.purpurextras.listeners; -import com.destroystokyo.paper.event.entity.EntityZapEvent; +import me.youhavetrouble.entiddy.Entiddy; import me.youhavetrouble.purpurextras.PurpurExtras; import org.bukkit.Location; import org.bukkit.entity.Entity; import org.bukkit.entity.EntityType; +import org.bukkit.entity.LivingEntity; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; import org.bukkit.event.Listener; import org.bukkit.event.entity.CreatureSpawnEvent; +import org.bukkit.event.entity.EntityDamageByEntityEvent; + +import java.util.HashMap; +import java.util.Locale; +import java.util.Map; public class LightningTransformsMobsListener implements Listener { + private final HashMap entities = new HashMap<>(); + + public LightningTransformsMobsListener() { + for (Map.Entry entry : PurpurExtras.getPurpurConfig().lightningTransformEntities.entrySet()) { + getEntityTypeOrSpecial(entry.getKey(), entry.getValue()); + } + } + @EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true) - public void onLightiningStrike(EntityZapEvent event) { - event.setCancelled(true); + public void onLightningStrike(EntityDamageByEntityEvent event) { + if (!event.getDamager().getType().equals(EntityType.LIGHTNING)) return; Entity entity = event.getEntity(); - EntityType type = entity.getType(); + if (!(entity instanceof LivingEntity livingEntity)) return; + if (entity.getEntitySpawnReason().equals(CreatureSpawnEvent.SpawnReason.LIGHTNING)) return; Location location = entity.getLocation(); - EntityType newEntityType = PurpurExtras.getPurpurConfig().lightningTransformEntities.get(type); - if (newEntityType == null) return; - + Entiddy specialEntity = Entiddy.fromEntity(livingEntity); + if (specialEntity != null) { + event.setCancelled(true); + entity.remove(); + String specialEntityKey = specialEntity.entiddy().toString().toLowerCase(Locale.ROOT); + Object targetEntity = entities.get(specialEntityKey); + spawnEntity(targetEntity, location); + return; + } + Object targetEntity = entities.get(entity.getType().getKey().getKey()); + if (targetEntity == null) return; + event.setCancelled(true); entity.remove(); - location.getWorld().spawnEntity(location, newEntityType, CreatureSpawnEvent.SpawnReason.LIGHTNING); + spawnEntity(targetEntity, location); + } + + private void spawnEntity(Object entity, Location location) { + if (entity instanceof EntityType entityType) { + location.getWorld().spawnEntity(location, entityType, CreatureSpawnEvent.SpawnReason.LIGHTNING); + } else if (entity instanceof Entiddy entiddy) { + entiddy.entiddy().spawn(location, CreatureSpawnEvent.SpawnReason.LIGHTNING); + } + } + + private void getEntityTypeOrSpecial(String key, String value) { + String sourceKey = null; + Object goal = null; + for (EntityType entityType : EntityType.values()) { + if (!entityType.isSpawnable()) continue; + String entityKey = entityType.getKey().getKey(); + if (entityKey.equals(key.toLowerCase(Locale.ROOT))) { + sourceKey = key; + } + if (entityKey.equals(value.toLowerCase(Locale.ROOT))) { + goal = entityType; + } + } + if (sourceKey == null) { + try { + Entiddy entiddy = Entiddy.valueOf(key); + sourceKey = key; + } catch (IllegalArgumentException ignored) {} + } + if (goal == null) { + try { + goal = Entiddy.valueOf(value); + } catch (IllegalArgumentException ignored) {} + } + this.entities.put(sourceKey, goal); } }