9
0
mirror of https://github.com/Auxilor/EcoMobs.git synced 2025-12-20 15:39:31 +00:00

Switched from BossType to Entities system

This commit is contained in:
Auxilor
2022-01-10 11:07:03 +00:00
parent 0f04ca86d9
commit 6ac3001e52
11 changed files with 50 additions and 219 deletions

View File

@@ -4,6 +4,9 @@ import com.google.common.collect.ImmutableMap;
import com.willfp.eco.core.EcoPlugin;
import com.willfp.eco.core.PluginDependent;
import com.willfp.eco.core.config.interfaces.Config;
import com.willfp.eco.core.entities.CustomEntity;
import com.willfp.eco.core.entities.Entities;
import com.willfp.eco.core.entities.TestableEntity;
import com.willfp.eco.core.integrations.placeholder.PlaceholderEntry;
import com.willfp.eco.core.items.Items;
import com.willfp.eco.core.items.builder.ItemBuilder;
@@ -16,8 +19,7 @@ import com.willfp.eco.util.NumberUtils;
import com.willfp.eco.util.StringUtils;
import com.willfp.ecobosses.bosses.effects.Effect;
import com.willfp.ecobosses.bosses.effects.Effects;
import com.willfp.ecobosses.bosses.util.bosstype.BossEntityUtils;
import com.willfp.ecobosses.bosses.util.bosstype.BossType;
import com.willfp.ecobosses.bosses.util.BossUtils;
import com.willfp.ecobosses.bosses.util.obj.ArgumentedEffectName;
import com.willfp.ecobosses.bosses.util.obj.BossbarProperties;
import com.willfp.ecobosses.bosses.util.obj.EquipmentPiece;
@@ -62,7 +64,7 @@ public class EcoBoss extends PluginDependent<EcoPlugin> {
* The name of the boss.
*/
@Getter
private final String name;
private final String id;
/**
* The config of the set.
@@ -79,7 +81,7 @@ public class EcoBoss extends PluginDependent<EcoPlugin> {
/**
* The base entity spawner.
*/
private final BossType bossType;
private final TestableEntity bossType;
/**
* If the boss bar is enabled.
@@ -317,16 +319,16 @@ public class EcoBoss extends PluginDependent<EcoPlugin> {
/**
* Create a new Boss.
*
* @param name The name of the set.
* @param id The name of the set.
* @param config The set's config.
* @param plugin Instance of EcoBosses.
*/
public EcoBoss(@NotNull final String name,
public EcoBoss(@NotNull final String id,
@NotNull final Config config,
@NotNull final EcoPlugin plugin) {
super(plugin);
this.config = config;
this.name = name;
this.id = id;
this.livingBosses = new HashMap<>();
this.isGlowing = this.getConfig().getBool("glowing");
this.baby = this.getConfig().getBool("baby");
@@ -334,7 +336,7 @@ public class EcoBoss extends PluginDependent<EcoPlugin> {
this.displayName = this.getConfig().getFormattedString("name");
// Boss Type
this.bossType = BossEntityUtils.getBossType(this.getConfig().getString("base-mob"));
this.bossType = Entities.lookup("base-mob");
// Boss Bar
this.bossbarEnabled = this.getConfig().getBool("bossbar.enabled");
@@ -419,7 +421,7 @@ public class EcoBoss extends PluginDependent<EcoPlugin> {
// Rewards
this.drops = new ArrayList<>();
drops.addAll(this.getConfig().getStrings("rewards.drops", false));
drops.addAll(this.getConfig().getStrings("rewards.drops"));
this.experienceOptions = new ExperienceOptions(
this.getConfig().getInt("rewards.xp.minimum"),
@@ -516,7 +518,7 @@ public class EcoBoss extends PluginDependent<EcoPlugin> {
this.topDamagerCommands = new HashMap<>();
for (int i = 1; i <= 3; i++) {
this.topDamagerCommands.put(i, new ArrayList<>());
for (String string : this.getConfig().getStrings("rewards.top-damager-commands." + i, false)) {
for (String string : this.getConfig().getStrings("rewards.top-damager-commands." + i)) {
double chance = 100;
if (string.contains("::")) {
String[] split = string.split("::");
@@ -532,7 +534,7 @@ public class EcoBoss extends PluginDependent<EcoPlugin> {
// Nearby Rewards
this.nearbyRadius = this.getConfig().getDouble("rewards.nearby-player-commands.radius");
this.nearbyPlayersCommands = new HashMap<>();
for (String string : this.getConfig().getStrings("rewards.nearby-player-commands.commands", false)) {
for (String string : this.getConfig().getStrings("rewards.nearby-player-commands.commands")) {
double chance = 100;
if (string.contains("::")) {
String[] split = string.split("::");
@@ -547,18 +549,13 @@ public class EcoBoss extends PluginDependent<EcoPlugin> {
for (String string : this.getConfig().getStrings("effects")) {
String effectName = string.split(":")[0];
List<String> args = new ArrayList<>(Arrays.asList(string.replace(effectName + ":", "").split(":")));
if (args.contains("mythicmobs")) {
String newArg = "mythicmobs:" + args.get(args.indexOf("mythicmobs")+1);
args.set(args.indexOf("mythicmobs"), newArg);
args.remove(args.get(args.indexOf(newArg)+1));
}
this.effectNames.add(new ArgumentedEffectName(effectName, args));
}
for (ArgumentedEffectName effectName : new ArrayList<>(this.effectNames)) {
if (Effects.getEffect(effectName.name(), effectName.args()) == null) {
this.effectNames.remove(effectName);
Bukkit.getLogger().warning("Invalid effect " + effectName.name() + " specified in " + this.name);
Bukkit.getLogger().warning("Invalid effect " + effectName.name() + " specified in " + this.id);
}
}
@@ -583,12 +580,12 @@ public class EcoBoss extends PluginDependent<EcoPlugin> {
}
new PlaceholderEntry(
"timeuntilspawn_"+this.name,
(player) -> new BigDecimal(this.timeUntilSpawn/20).setScale(1, RoundingMode.HALF_UP).toString(),
"timeuntilspawn_" + this.id,
(player) -> new BigDecimal(this.timeUntilSpawn / 20).setScale(1, RoundingMode.HALF_UP).toString(),
false
).register();
for (String req : config.getStrings("spawn-requirements", false)) {
for (String req : config.getStrings("spawn-requirements")) {
List<String> split = Arrays.asList(req.split(":"));
if (split.size() < 2) {
continue;
@@ -608,7 +605,7 @@ public class EcoBoss extends PluginDependent<EcoPlugin> {
ItemBuilder builder = new ItemStackBuilder(material)
.setDisplayName(this.getConfig().getString("spawn-egg.name"))
.addLoreLines(this.getConfig().getStrings("spawn-egg.lore"))
.writeMetaKey(this.getPlugin().getNamespacedKeyFactory().create("spawn_egg"), PersistentDataType.STRING, this.getName());
.writeMetaKey(this.getPlugin().getNamespacedKeyFactory().create("spawn_egg"), PersistentDataType.STRING, this.getId());
if (this.getConfig().getBool("spawn-egg.glow")) {
builder.addEnchantment(Enchantment.DURABILITY, 1)
@@ -620,9 +617,9 @@ public class EcoBoss extends PluginDependent<EcoPlugin> {
if (this.getConfig().getBool("spawn-egg.craftable")) {
Recipes.createAndRegisterRecipe(
this.getPlugin(),
"spawn_egg_" + this.getName(),
"spawn_egg_" + this.getId(),
this.getSpawnEgg(),
this.getConfig().getStrings("spawn-egg.recipe", false)
this.getConfig().getStrings("spawn-egg.recipe")
);
}
} else {
@@ -631,6 +628,17 @@ public class EcoBoss extends PluginDependent<EcoPlugin> {
if (this.getConfig().getBool("enabled")) {
EcoBosses.addBoss(this);
new CustomEntity(
this.getPlugin().getNamespacedKeyFactory().create(this.id),
test -> {
if (!(test instanceof LivingEntity living)) {
return false;
}
return Objects.equals(this, BossUtils.getBoss(living));
},
this::spawn
).register();
}
}
@@ -676,7 +684,7 @@ public class EcoBoss extends PluginDependent<EcoPlugin> {
ItemStack itemStack = Items.lookup(dropName).getItem();
if (itemStack.getType() == Material.AIR) {
Bukkit.getLogger().warning(this.getName() + " has an invalid drop configured! (" + dropName + ")");
Bukkit.getLogger().warning(this.getId() + " has an invalid drop configured! (" + dropName + ")");
continue;
}
@@ -707,16 +715,17 @@ public class EcoBoss extends PluginDependent<EcoPlugin> {
*
* @param location The location.
*/
public void spawn(@NotNull final Location location) {
public LivingEntity spawn(@NotNull final Location location) {
location.getChunk().load();
LivingEntity entity = bossType.spawnBossEntity(location);
LivingEntity entity = (LivingEntity) bossType.spawn(location);
this.livingBosses.put(entity, new LivingEcoBoss(
this.getPlugin(),
entity,
this
)
);
return entity;
}
/**
@@ -757,18 +766,18 @@ public class EcoBoss extends PluginDependent<EcoPlugin> {
return false;
}
return this.getName().equals(boss.getName());
return this.getId().equals(boss.getId());
}
@Override
public int hashCode() {
return Objects.hash(this.getName());
return Objects.hash(this.getId());
}
@Override
public String toString() {
return "EcoBoss{"
+ this.getName()
+ this.getId()
+ "}";
}
}

View File

@@ -89,8 +89,8 @@ public class EcoBosses {
* @param set The {@link EcoBoss} to add.
*/
public static void addBoss(@NotNull final EcoBoss set) {
BY_NAME.remove(set.getName());
BY_NAME.put(set.getName(), set);
BY_NAME.remove(set.getId());
BY_NAME.put(set.getId(), set);
}
/**
@@ -99,6 +99,6 @@ public class EcoBosses {
* @param set The {@link EcoBoss} to remove.
*/
public static void removeBoss(@NotNull final EcoBoss set) {
BY_NAME.remove(set.getName());
BY_NAME.remove(set.getId());
}
}

View File

@@ -98,7 +98,7 @@ public class LivingEcoBoss extends PluginDependent<EcoPlugin> {
}
private void onSpawn() {
entity.getPersistentDataContainer().set(this.getPlugin().getNamespacedKeyFactory().create("boss"), PersistentDataType.STRING, boss.getName());
entity.getPersistentDataContainer().set(this.getPlugin().getNamespacedKeyFactory().create("boss"), PersistentDataType.STRING, boss.getId());
entity.setPersistent(true);
entity.setRemoveWhenFarAway(false);

View File

@@ -1,10 +1,10 @@
package com.willfp.ecobosses.bosses.effects.effects;
import com.willfp.eco.core.entities.Entities;
import com.willfp.eco.core.entities.TestableEntity;
import com.willfp.eco.util.NumberUtils;
import com.willfp.ecobosses.bosses.EcoBoss;
import com.willfp.ecobosses.bosses.effects.Effect;
import com.willfp.ecobosses.bosses.util.bosstype.BossEntityUtils;
import com.willfp.ecobosses.bosses.util.bosstype.BossType;
import com.willfp.ecobosses.bosses.util.obj.OptionedSound;
import org.bukkit.Location;
import org.bukkit.Material;
@@ -17,7 +17,7 @@ import org.jetbrains.annotations.NotNull;
import java.util.List;
public class EffectSummon extends Effect {
private final BossType type;
private final TestableEntity type;
private final double chance;
public EffectSummon(@NotNull final List<String> args) {
@@ -27,7 +27,7 @@ public class EffectSummon extends Effect {
showConfigError("Incorrect amount of arguments!");
}
type = BossEntityUtils.getBossType(args.get(0));
type = Entities.lookup(args.get(0));
chance = Double.parseDouble(args.get(1));
}
@@ -53,7 +53,7 @@ public class EffectSummon extends Effect {
loc.add(0, 1, 0);
}
Entity summonedEntity = type.spawnBossEntity(loc);
Entity summonedEntity = type.spawn(loc);
if (summonedEntity instanceof Mob) {
((Mob) summonedEntity).setTarget(player);
}

View File

@@ -1,57 +0,0 @@
package com.willfp.ecobosses.bosses.util.bosstype;
import io.lumine.xikage.mythicmobs.MythicMobs;
import io.lumine.xikage.mythicmobs.mobs.MythicMob;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.UtilityClass;
import org.bukkit.entity.Creeper;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Zombie;
import org.jetbrains.annotations.NotNull;
import java.util.Arrays;
@UtilityClass
@SuppressWarnings("unchecked")
public class BossEntityUtils {
/**
* Get boss type.
*
* @param id The name.
* @return The boss type.
*/
public static BossType getBossType(@NotNull String id) {
if (id.startsWith("mythicmobs:")) {
int level;
try {
level = Integer.parseInt(Arrays.stream(id.split("_")).toList().get(id.split("_").length - 1));
} catch (NumberFormatException exception) {
level = 1;
}
MythicMob mob = MythicMobs.inst().getMobManager().getMythicMob(id.replace("mythicmobs:", "")
.replace("_" + level, ""));
if (mob != null) {
return new MythicMobsBossType(mob, level);
}
}
if (id.equalsIgnoreCase("charged_creeper")) {
return new ChargedCreeperBossType();
}
try {
Class<? extends LivingEntity> type = (Class<? extends LivingEntity>) EntityType.valueOf(id.toUpperCase()).getEntityClass();
assert type != null;
return new VanillaBossType(type);
} catch (IllegalArgumentException ignored) {
}
return new VanillaBossType(Zombie.class);
}
}

View File

@@ -1,15 +0,0 @@
package com.willfp.ecobosses.bosses.util.bosstype;
import org.bukkit.Location;
import org.bukkit.entity.LivingEntity;
import org.jetbrains.annotations.NotNull;
public abstract class BossType {
/**
* Spawn boss entity.
*
* @param location The location.
* @return The entity.
*/
public abstract LivingEntity spawnBossEntity(@NotNull Location location);
}

View File

@@ -1,30 +0,0 @@
package com.willfp.ecobosses.bosses.util.bosstype;
import org.bukkit.Location;
import org.bukkit.entity.Creeper;
import org.bukkit.entity.LivingEntity;
import org.jetbrains.annotations.NotNull;
import java.util.Objects;
class ChargedCreeperBossType extends VanillaBossType {
/**
* Create new Charged Creeper boss type.
*/
ChargedCreeperBossType() {
super(Creeper.class);
}
/**
* Spawn a charged creeper.
*
* @param location The location.
* @return The entity.
*/
@Override
public LivingEntity spawnBossEntity(@NotNull final Location location) {
Creeper creeper = Objects.requireNonNull(location.getWorld()).spawn(location, Creeper.class);
creeper.setPowered(true);
return creeper;
}
}

View File

@@ -1,46 +0,0 @@
package com.willfp.ecobosses.bosses.util.bosstype;
import io.lumine.xikage.mythicmobs.MythicMobs;
import io.lumine.xikage.mythicmobs.adapters.AbstractLocation;
import io.lumine.xikage.mythicmobs.adapters.AbstractWorld;
import io.lumine.xikage.mythicmobs.api.exceptions.InvalidMobTypeException;
import io.lumine.xikage.mythicmobs.mobs.MythicMob;
import org.bukkit.Location;
import org.bukkit.entity.LivingEntity;
import org.jetbrains.annotations.NotNull;
public class MythicMobsBossType extends BossType {
/**
* The entity type.
*/
private final MythicMob boss;
/**
* Level of MythicMobs mob
*/
private final int level;
/**
* Create new vanilla boss type.
*
* @param boss The MythicMob.
*/
public MythicMobsBossType(MythicMob boss, int level) {
this.boss = boss;
this.level = level;
}
@Override
public LivingEntity spawnBossEntity(@NotNull Location location) {
try {
return (LivingEntity) MythicMobs.inst().getAPIHelper().spawnMythicMob(boss, location, level);
} catch (InvalidMobTypeException e) {
e.printStackTrace();
}
return null;
}
}

View File

@@ -1,30 +0,0 @@
package com.willfp.ecobosses.bosses.util.bosstype;
import org.bukkit.Location;
import org.bukkit.entity.Creeper;
import org.bukkit.entity.LivingEntity;
import org.jetbrains.annotations.NotNull;
import java.util.Objects;
class VanillaBossType extends BossType {
/**
* The entity type.
*/
private final Class<? extends LivingEntity> entityClass;
/**
* Create new vanilla boss type.
*
* @param entityClass The entity class.
*/
VanillaBossType(@NotNull final Class<? extends LivingEntity> entityClass) {
this.entityClass = entityClass;
}
@Override
public LivingEntity spawnBossEntity(@NotNull final Location location) {
LivingEntity result = Objects.requireNonNull(location.getWorld()).spawn(location, entityClass);
return result;
}
}

View File

@@ -56,7 +56,7 @@ public class CommandGive extends Subcommand {
BOSS_NAMES.clear();
BOSS_NAMES.addAll(EcoBosses.values().stream()
.filter(boss -> boss.getSpawnEgg() != null)
.map(EcoBoss::getName)
.map(EcoBoss::getId)
.collect(Collectors.toList()));
}
@@ -101,7 +101,7 @@ public class CommandGive extends Subcommand {
}
String message = this.getPlugin().getLangYml().getMessage("give-success");
message = message.replace("%boss%", boss.getName()).replace("%recipient%", reciever.getName());
message = message.replace("%boss%", boss.getId()).replace("%recipient%", reciever.getName());
sender.sendMessage(message);
ItemStack itemStack = boss.getSpawnEgg();

View File

@@ -48,7 +48,7 @@ public class CommandSpawn extends Subcommand {
@ConfigUpdater
public static void reload() {
BOSS_NAMES.clear();
BOSS_NAMES.addAll(EcoBosses.values().stream().map(EcoBoss::getName).collect(Collectors.toList()));
BOSS_NAMES.addAll(EcoBosses.values().stream().map(EcoBoss::getId).collect(Collectors.toList()));
}
@Override