-
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
68f4ab5
commit 810d01a
Showing
3 changed files
with
78 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 67 additions & 8 deletions
75
src/main/java/me/youhavetrouble/purpurextras/listeners/LightningTransformsMobsListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<String, Object> entities = new HashMap<>(); | ||
|
||
public LightningTransformsMobsListener() { | ||
for (Map.Entry<String, String> 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); | ||
} | ||
} |