Fixed killmob not giving exp with flame & fire aspect

This commit is contained in:
Jules 2023-06-26 22:54:03 +02:00
parent 00f1581707
commit 2708dd2072
3 changed files with 57 additions and 17 deletions

View File

@ -2,7 +2,8 @@ package net.Indyuce.mmocore.experience.source;
import io.lumine.mythic.lib.MythicLib; import io.lumine.mythic.lib.MythicLib;
import io.lumine.mythic.lib.api.MMOLineConfig; import io.lumine.mythic.lib.api.MMOLineConfig;
import io.lumine.mythic.lib.api.event.PlayerKillEntityEvent; import io.lumine.mythic.lib.api.event.PlayerAttackEvent;
import io.lumine.mythic.lib.util.FlushableRegistry;
import net.Indyuce.mmocore.MMOCore; import net.Indyuce.mmocore.MMOCore;
import net.Indyuce.mmocore.api.player.PlayerData; import net.Indyuce.mmocore.api.player.PlayerData;
import net.Indyuce.mmocore.api.util.MMOCoreUtils; import net.Indyuce.mmocore.api.util.MMOCoreUtils;
@ -15,9 +16,11 @@ import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType; import org.bukkit.entity.EntityType;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority; import org.bukkit.event.EventPriority;
import org.bukkit.event.entity.EntityDeathEvent;
import org.bukkit.persistence.PersistentDataType; import org.bukkit.persistence.PersistentDataType;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import java.util.UUID;
public class KillMobExperienceSource extends SpecificExperienceSource<Entity> { public class KillMobExperienceSource extends SpecificExperienceSource<Entity> {
private final EntityType type; private final EntityType type;
@ -37,17 +40,36 @@ public class KillMobExperienceSource extends SpecificExperienceSource<Entity> {
public ExperienceSourceManager<KillMobExperienceSource> newManager() { public ExperienceSourceManager<KillMobExperienceSource> newManager() {
return new ExperienceSourceManager<KillMobExperienceSource>() { return new ExperienceSourceManager<KillMobExperienceSource>() {
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) /**
public void a(PlayerKillEntityEvent event) { * This map is used to keep track of the last player who
Bukkit.getScheduler().runTaskLater(MMOCore.plugin, () -> { * hit some entity. It is flushed on entity death.
if (event.getTarget().isDead() && !event.getTarget().getPersistentDataContainer().has(new NamespacedKey(MMOCore.plugin, "spawner_spawned"), PersistentDataType.STRING)) { */
PlayerData data = PlayerData.get(event.getPlayer()); private final FlushableRegistry<UUID, UUID> registry = new FlushableRegistry<>((entity, attacker) -> Bukkit.getEntity(entity) == null, 20 * 60);
for (KillMobExperienceSource source : getSources()) @Override
if (source.matches(data, event.getTarget())) public void whenClosed() {
source.giveExperience(data, 1, MMOCoreUtils.getCenterLocation(event.getTarget())); registry.close();
} }
}, 2);
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void registerLastAttacker(PlayerAttackEvent event) {
registry.getRegistry().put(event.getEntity().getUniqueId(), event.getAttacker().getData().getUniqueId());
}
@EventHandler(priority = EventPriority.MONITOR)
public void giveExp(EntityDeathEvent event) {
// Always remove entry from map
final @Nullable UUID lastAttacker = this.registry.getRegistry().remove(event.getEntity().getUniqueId());
if (lastAttacker == null) return;
if (event.getEntity().getPersistentDataContainer().has(new NamespacedKey(MMOCore.plugin, "spawner_spawned"), PersistentDataType.STRING))
return;
final PlayerData data = PlayerData.get(lastAttacker);
for (KillMobExperienceSource source : getSources())
if (source.matches(data, event.getEntity()))
source.giveExperience(data, 1, MMOCoreUtils.getCenterLocation(event.getEntity()));
} }
}; };
} }

View File

@ -9,7 +9,7 @@ import net.Indyuce.mmocore.manager.profession.ExperienceSourceManager;
import org.apache.commons.lang.Validate; import org.apache.commons.lang.Validate;
import org.bukkit.configuration.ConfigurationSection; import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.event.HandlerList; import org.jetbrains.annotations.NotNull;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import java.io.File; import java.io.File;
@ -25,10 +25,8 @@ public class ExperienceManager implements MMOCoreManager {
* Experience sources from the exp-sources.yml config file where you can * Experience sources from the exp-sources.yml config file where you can
* input any exp source which can later be used along with the 'from' * input any exp source which can later be used along with the 'from'
* exp source anywhere in the plugin. * exp source anywhere in the plugin.
* <p>
* TODO First needs to edit the exp-source current structure. This is going to break a lot of things
* *
* @deprecated See TODO * @deprecated TODO First needs to edit the exp-source current structure. This is going to break a lot of things
*/ */
@Deprecated @Deprecated
private final Map<String, List<ExperienceSource<?>>> publicExpSources = new HashMap<>(); private final Map<String, List<ExperienceSource<?>>> publicExpSources = new HashMap<>();
@ -55,6 +53,7 @@ public class ExperienceManager implements MMOCoreManager {
return expCurves.containsKey(id); return expCurves.containsKey(id);
} }
@NotNull
public ExpCurve getCurveOrThrow(String id) { public ExpCurve getCurveOrThrow(String id) {
Validate.isTrue(hasCurve(id), "Could not find exp curve with ID '" + id + "'"); Validate.isTrue(hasCurve(id), "Could not find exp curve with ID '" + id + "'");
return expCurves.get(id); return expCurves.get(id);
@ -70,10 +69,12 @@ public class ExperienceManager implements MMOCoreManager {
return expTables.containsKey(id); return expTables.containsKey(id);
} }
@NotNull
public ExperienceTable getTableOrThrow(String id) { public ExperienceTable getTableOrThrow(String id) {
return Objects.requireNonNull(expTables.get(id), "Could not find exp table with ID '" + id + "'"); return Objects.requireNonNull(expTables.get(id), "Could not find exp table with ID '" + id + "'");
} }
@NotNull
public ExperienceTable loadExperienceTable(Object obj) { public ExperienceTable loadExperienceTable(Object obj) {
if (obj instanceof ConfigurationSection) if (obj instanceof ConfigurationSection)
@ -99,7 +100,7 @@ public class ExperienceManager implements MMOCoreManager {
expCurves.clear(); expCurves.clear();
expTables.clear(); expTables.clear();
managers.values().forEach(HandlerList::unregisterAll); managers.forEach((c, manager) -> manager.close());
managers.clear(); managers.clear();
} }

View File

@ -1,19 +1,23 @@
package net.Indyuce.mmocore.manager.profession; package net.Indyuce.mmocore.manager.profession;
import io.lumine.mythic.lib.util.Closeable;
import net.Indyuce.mmocore.MMOCore; import net.Indyuce.mmocore.MMOCore;
import net.Indyuce.mmocore.experience.source.type.ExperienceSource; import net.Indyuce.mmocore.experience.source.type.ExperienceSource;
import org.apache.commons.lang.Validate;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.event.HandlerList;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
import java.util.HashSet; import java.util.HashSet;
import java.util.Set; import java.util.Set;
public abstract class ExperienceSourceManager<T extends ExperienceSource> implements Listener { public abstract class ExperienceSourceManager<T extends ExperienceSource> implements Listener, Closeable {
/** /**
* List of all active experience sources * List of all active experience sources
*/ */
private final Set<T> sources = new HashSet<>(); private final Set<T> sources = new HashSet<>();
private boolean open = true;
public ExperienceSourceManager() { public ExperienceSourceManager() {
Bukkit.getPluginManager().registerEvents(this, MMOCore.plugin); Bukkit.getPluginManager().registerEvents(this, MMOCore.plugin);
@ -23,6 +27,19 @@ public abstract class ExperienceSourceManager<T extends ExperienceSource> implem
sources.add(source); sources.add(source);
} }
public void whenClosed() {
// Nothing by default
}
@Override
public void close() {
Validate.isTrue(open, "Manager is already closed");
open = false;
HandlerList.unregisterAll(this);
whenClosed();
}
public Set<T> getSources() { public Set<T> getSources() {
return sources; return sources;
} }