Skip to content

Commit

Permalink
Fixed NotSerializableException when killing certain entities (fixes #589
Browse files Browse the repository at this point in the history
)
  • Loading branch information
Intelli committed Jul 24, 2024
1 parent 77a4a94 commit a93bf2f
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 5 deletions.
8 changes: 7 additions & 1 deletion src/main/java/net/coreprotect/bukkit/Bukkit_v1_19.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
import java.util.HashSet;
import java.util.List;

import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Frog;
import org.bukkit.entity.Goat;
Expand All @@ -31,7 +33,7 @@ public Bukkit_v1_19() {
public boolean getEntityMeta(LivingEntity entity, List<Object> info) {
if (entity instanceof Frog) {
Frog frog = (Frog) entity;
info.add(frog.getVariant());
info.add(frog.getVariant().getKey().asString());
}
else if (entity instanceof Tadpole) {
Tadpole tadpole = (Tadpole) entity;
Expand All @@ -58,6 +60,10 @@ public boolean setEntityMeta(Entity entity, Object value, int count) {
if (entity instanceof Frog) {
Frog frog = (Frog) entity;
if (count == 0) {
if (value instanceof String) {
NamespacedKey namespacedKey = NamespacedKey.fromString((String) value);
value = Bukkit.getRegistry(Frog.Variant.class).get(namespacedKey);
}
Frog.Variant set = (Frog.Variant) value;
frog.setVariant(set);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ else if (entity instanceof IronGolem) {
}
else if (entity instanceof Cat) {
Cat cat = (Cat) entity;
info.add(cat.getCatType());
info.add(cat.getCatType().getKey().asString());
info.add(cat.getCollarColor());
}
else if (entity instanceof Fox) {
Expand Down Expand Up @@ -404,8 +404,8 @@ else if (entity instanceof AbstractVillager) {

if (abstractVillager instanceof Villager) {
Villager villager = (Villager) abstractVillager;
info.add(villager.getProfession());
info.add(villager.getVillagerType());
info.add(villager.getProfession().getKey().asString());
info.add(villager.getVillagerType().getKey().asString());
info.add(recipes);
info.add(villager.getVillagerLevel());
info.add(villager.getVillagerExperience());
Expand Down Expand Up @@ -433,7 +433,7 @@ else if (entity instanceof Wolf) {
else if (entity instanceof ZombieVillager) {
ZombieVillager zombieVillager = (ZombieVillager) entity;
info.add(zombieVillager.isBaby());
info.add(zombieVillager.getVillagerProfession());
info.add(zombieVillager.getVillagerProfession().getKey().asString());
}
else if (entity instanceof Zombie) {
Zombie zombie = (Zombie) entity;
Expand Down
18 changes: 18 additions & 0 deletions src/main/java/net/coreprotect/utility/entity/EntityUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.bukkit.Bukkit;
import org.bukkit.DyeColor;
import org.bukkit.Location;
import org.bukkit.NamespacedKey;
import org.bukkit.OfflinePlayer;
import org.bukkit.attribute.Attributable;
import org.bukkit.attribute.Attribute;
Expand Down Expand Up @@ -231,6 +232,11 @@ else if (entity instanceof IronGolem) {
else if (entity instanceof Cat) {
Cat cat = (Cat) entity;
if (count == 0) {
if (value instanceof String) {
NamespacedKey namespacedKey = NamespacedKey.fromString((String) value);
value = Bukkit.getRegistry(Cat.Type.class).get(namespacedKey);
// value = RegistryAccess.registryAccess().getRegistry(RegistryKey.CAT_VARIANT).get((NamespacedKey)value);
}
Cat.Type set = (Cat.Type) value;
cat.setCatType(set);
}
Expand Down Expand Up @@ -327,13 +333,21 @@ else if (entity instanceof AbstractVillager) {
if (count == 0) {
if (abstractVillager instanceof Villager) {
Villager villager = (Villager) abstractVillager;
if (value instanceof String) {
NamespacedKey namespacedKey = NamespacedKey.fromString((String) value);
value = Bukkit.getRegistry(Profession.class).get(namespacedKey);
}
Profession set = (Profession) value;
villager.setProfession(set);
}
}
else if (count == 1) {
if (abstractVillager instanceof Villager && value instanceof Villager.Type) {
Villager villager = (Villager) abstractVillager;
if (value instanceof String) {
NamespacedKey namespacedKey = NamespacedKey.fromString((String) value);
value = Bukkit.getRegistry(Villager.Type.class).get(namespacedKey);
}
Villager.Type set = (Villager.Type) value;
villager.setVillagerType(set);
}
Expand Down Expand Up @@ -427,6 +441,10 @@ else if (entity instanceof ZombieVillager) {
zombieVillager.setBaby(set);
}
else if (count == 1) {
if (value instanceof String) {
NamespacedKey namespacedKey = NamespacedKey.fromString((String) value);
value = Bukkit.getRegistry(Profession.class).get(namespacedKey);
}
Profession set = (Profession) value;
zombieVillager.setVillagerProfession(set);
}
Expand Down

0 comments on commit a93bf2f

Please sign in to comment.