Skip to content

Commit

Permalink
Fix null safety warnings and format
Browse files Browse the repository at this point in the history
  • Loading branch information
Archy-X committed Jul 6, 2024
1 parent 6ebc1fb commit 1020815
Show file tree
Hide file tree
Showing 10 changed files with 31 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ public EntitySupplier getEntitySupplier(ConfigurationNode config) {

@Override
public boolean shouldUseParser(ConfigurationNode config) {
return config.node("entity").getString().startsWith("mythicmobs:");
return config.node("entity").getString("").startsWith("mythicmobs:");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public record EntityProperties(String entityId,
Float verticalVelocity) {

public static EntityProperties fromConfig(ConfigurationNode config) {
String[] id = config.node("entity").getString().split(":");
String[] id = config.node("entity").getString("").split(":");

return new EntityProperties(
id.length > 1 ? id[1] : id[0],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import org.spongepowered.configurate.ConfigurationNode;

public class VanillaEntityParser implements CustomEntityParser {

@Override
public EntitySupplier getEntitySupplier(ConfigurationNode config) {
return new VanillaEntitySupplier(EntityProperties.fromConfig(config));
Expand All @@ -13,8 +14,10 @@ public EntitySupplier getEntitySupplier(ConfigurationNode config) {
public boolean shouldUseParser(ConfigurationNode config) {
String entity = config.node("entity").getString();

if (entity == null) return false;

// If it has a colon, it's a custom entity
// But if it starts with minecraft:, it's a vanilla entity stated explicitly
return !entity.contains(":") || config.node("entity").getString().startsWith("minecraft:");
return !entity.contains(":") || entity.startsWith("minecraft:");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import dev.aurelium.auraskills.bukkit.AuraSkills;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.attribute.Attribute;
import org.bukkit.attribute.AttributeInstance;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.LivingEntity;
Expand All @@ -16,15 +18,24 @@ public VanillaEntitySupplier(EntityProperties entityProperties) {

@Override
public Entity spawnEntity(AuraSkills plugin, Location location) {
Entity entity = location.getWorld().spawnEntity(location, EntityType.valueOf(getEntityProperties().entityId().toUpperCase()));
World world = location.getWorld();
if (world == null) return null;

Entity entity = world.spawnEntity(location, EntityType.valueOf(getEntityProperties().entityId().toUpperCase()));

if (entity instanceof LivingEntity livingEntity) {
if (getEntityProperties().health() != null) {
livingEntity.getAttribute(Attribute.GENERIC_MAX_HEALTH).setBaseValue(getEntityProperties().health());
livingEntity.setHealth(getEntityProperties().health());
AttributeInstance attribute = livingEntity.getAttribute(Attribute.GENERIC_MAX_HEALTH);
if (attribute != null) {
attribute.setBaseValue(getEntityProperties().health());
livingEntity.setHealth(Math.min(getEntityProperties().health(), attribute.getValue()));
}
}
if (getEntityProperties().damage() != null) {
livingEntity.getAttribute(Attribute.GENERIC_ATTACK_DAMAGE).setBaseValue(getEntityProperties().damage());
AttributeInstance attribute = livingEntity.getAttribute(Attribute.GENERIC_ATTACK_DAMAGE);
if (attribute != null) {
attribute.setBaseValue(getEntityProperties().damage());
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public void onFish(PlayerFishEvent event) {
} else if (selectedLoot instanceof CommandLoot commandLoot) {
giveCommandLoot(player, commandLoot, source, skill);
} else if (selectedLoot instanceof EntityLoot entityLoot) {
giveFishingEntityLoot(player, entityLoot, event, source, skill, cause, table);
giveFishingEntityLoot(player, entityLoot, event, source, skill, cause);
}
break; // Stop iterating pools
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,12 +120,14 @@ protected void giveFishingItemLoot(Player player, ItemLoot loot, PlayerFishEvent
giveXp(player, loot, source, skill);
}

protected void giveFishingEntityLoot(Player player, EntityLoot loot, PlayerFishEvent event, @Nullable XpSource source, Skill skill, LootDropEvent.Cause cause, LootTable table) {
protected void giveFishingEntityLoot(Player player, EntityLoot loot, PlayerFishEvent event, @Nullable XpSource source, Skill skill, LootDropEvent.Cause cause) {
if (!(event.getCaught() instanceof Item itemEntity)) return;

Location location = event.getHook().getLocation();
Entity entity = loot.getEntity().spawnEntity(plugin, event.getHook().getLocation());

if (entity == null) return;

LootDropEvent dropEvent = new LootDropEvent(player, plugin.getUser(player).toApi(), entity, event.getHook().getLocation(), cause);
Bukkit.getPluginManager().callEvent(dropEvent);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,7 @@
import org.spongepowered.configurate.ConfigurationNode;

public interface CustomEntityParser extends CustomParser {

EntitySupplier getEntitySupplier(ConfigurationNode config);

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,7 @@
import org.spongepowered.configurate.ConfigurationNode;

public interface CustomItemParser extends CustomParser {

ItemStack parseCustomItem(ConfigurationNode config);

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,7 @@
import org.spongepowered.configurate.ConfigurationNode;

public interface CustomParser {

boolean shouldUseParser(ConfigurationNode config);

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import org.spongepowered.configurate.ConfigurationNode;
import org.spongepowered.configurate.serialize.SerializationException;


public class EntityLootParser implements LootParser {

protected final LootManager manager;
Expand Down

0 comments on commit 1020815

Please sign in to comment.