Skip to content

Commit

Permalink
rework lightning transforms mobs
Browse files Browse the repository at this point in the history
  • Loading branch information
YouHaveTrouble committed May 18, 2022
1 parent 68f4ab5 commit 810d01a
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 24 deletions.
9 changes: 9 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@
<name>Minecraft Libraries</name>
<url>https://libraries.minecraft.net</url>
</repository>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>

<dependencies>
Expand All @@ -86,5 +90,10 @@
<version>1.18.2-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.github.YouHaveTrouble</groupId>
<artifactId>Entiddy</artifactId>
<version>1.2.0</version>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public class PurpurConfig {
public final String beeHiveLoreBees, beeHiveLoreHoney;
public final HashMap<Material, Material> anvilCrushBlocksIndex = new HashMap<>();
public final HashSet<EntityType> stonecutterDamageBlacklist = new HashSet<>();
public final HashMap<EntityType, EntityType> lightningTransformEntities = new HashMap<>();
public final HashMap<String, String> lightningTransformEntities = new HashMap<>();
public final double furnaceBurnTimeMultiplier;

public PurpurConfig() {
Expand Down Expand Up @@ -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);
Expand Down
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);
}
}

0 comments on commit 810d01a

Please sign in to comment.