|
1 | 1 | package fr.jamailun.ultimatespellsystem.plugin.entities; |
2 | 2 |
|
| 3 | +import fr.jamailun.ultimatespellsystem.api.entities.SpellEntity; |
3 | 4 | import fr.jamailun.ultimatespellsystem.api.entities.SummonAttributes; |
4 | 5 | import fr.jamailun.ultimatespellsystem.api.entities.SummonsManager; |
5 | 6 | import fr.jamailun.ultimatespellsystem.api.events.EntitySummonedEvent; |
6 | | -import fr.jamailun.ultimatespellsystem.plugin.listeners.AggroListener; |
7 | 7 | import fr.jamailun.ultimatespellsystem.api.runner.SpellRuntime; |
8 | | -import fr.jamailun.ultimatespellsystem.api.entities.SpellEntity; |
9 | | -import fr.jamailun.ultimatespellsystem.plugin.utils.Clock; |
10 | 8 | import fr.jamailun.ultimatespellsystem.plugin.configuration.UssConfig; |
| 9 | +import fr.jamailun.ultimatespellsystem.plugin.listeners.AggroListener; |
| 10 | +import fr.jamailun.ultimatespellsystem.plugin.utils.Clock; |
11 | 11 | import org.bukkit.Bukkit; |
12 | | -import org.bukkit.entity.Mob; |
| 12 | +import org.bukkit.entity.*; |
13 | 13 | import org.jetbrains.annotations.NotNull; |
14 | 14 | import org.jetbrains.annotations.Nullable; |
15 | 15 |
|
|
20 | 20 | */ |
21 | 21 | public class SummonsManagerImpl implements SummonsManager { |
22 | 22 |
|
23 | | - private final Map<UUID, SummonAttributes> summonedEntities = new HashMap<>(); |
24 | | - private final Clock aggroClock; |
25 | | - |
26 | | - public SummonsManagerImpl(@NotNull UssConfig config) { |
27 | | - config.registerObserver(this::refreshConfig); |
28 | | - aggroClock = new Clock(this::recomputeAggro, UssConfig.getTicksAggroSummons()); |
29 | | - } |
30 | | - |
31 | | - private void recomputeAggro() { |
32 | | - for(SummonAttributes summon : summonedEntities.values()) { |
33 | | - if(summon.getEntity().getBukkitEntity().orElse(null) instanceof Mob mob && (mob.getTarget() == null || !mob.getTarget().isValid())) { |
34 | | - mob.setTarget(AggroListener.findAggro(summon)); |
35 | | - } |
| 23 | + private final Map<UUID, SummonAttributes> summonedEntities = new HashMap<>(); |
| 24 | + private final Clock aggroClock; |
| 25 | + |
| 26 | + public SummonsManagerImpl(@NotNull UssConfig config) { |
| 27 | + config.registerObserver(this::refreshConfig); |
| 28 | + aggroClock = new Clock(this::recomputeAggro, UssConfig.getTicksAggroSummons()); |
| 29 | + } |
| 30 | + |
| 31 | + private void recomputeAggro() { |
| 32 | + for (SummonAttributes summon : summonedEntities.values()) { |
| 33 | + if (summon.getEntity().getBukkitEntity().orElse(null) instanceof Mob mob && (mob.getTarget() == null || !mob.getTarget().isValid())) { |
| 34 | + LivingEntity target = AggroListener.findAggro(summon); |
| 35 | + if (target != null) { |
| 36 | + mob.setTarget(AggroListener.findAggro(summon)); |
| 37 | + summon.getEntity().getBukkitEntity().ifPresent(SummonsManagerImpl::forceAngry); |
36 | 38 | } |
| 39 | + } |
37 | 40 | } |
| 41 | + } |
38 | 42 |
|
39 | | - @Override |
40 | | - public @NotNull SpellEntity summon(@NotNull SummonAttributes summon, @NotNull SpellRuntime runtime) { |
41 | | - // Summon |
42 | | - summon.summon(this::remove, runtime); |
43 | | - summonedEntities.put(summon.getUUID(), summon); |
44 | | - |
45 | | - // propagate info |
46 | | - Bukkit.getPluginManager().callEvent(new EntitySummonedEvent(summon)); |
47 | | - |
48 | | - // Return entity |
49 | | - return summon.getEntity(); |
| 43 | + private static void forceAngry(Entity entity) { |
| 44 | + // Only wolves ? |
| 45 | + if(entity instanceof Wolf wolf) { |
| 46 | + wolf.setAngry(true); |
50 | 47 | } |
51 | | - |
52 | | - @Override |
53 | | - public void remove(@NotNull UUID uuid) { |
54 | | - SummonAttributes removed = summonedEntities.remove(uuid); |
55 | | - if(removed != null) { |
56 | | - removed.getKillTask().cancel(); |
57 | | - SpellEntity entity = removed.getEntity(); |
58 | | - if(entity != null) |
59 | | - entity.remove(); |
60 | | - } |
61 | | - } |
62 | | - |
63 | | - @Override |
64 | | - public boolean isASummonedEntity(@NotNull UUID uuid) { |
65 | | - return summonedEntities.containsKey(uuid); |
| 48 | + } |
| 49 | + |
| 50 | + @Override |
| 51 | + public @NotNull SpellEntity summon(@NotNull SummonAttributes summon, @NotNull SpellRuntime runtime) { |
| 52 | + // Summon |
| 53 | + summon.summon(this::remove, runtime); |
| 54 | + summonedEntities.put(summon.getUUID(), summon); |
| 55 | + |
| 56 | + // propagate info |
| 57 | + Bukkit.getPluginManager().callEvent(new EntitySummonedEvent(summon)); |
| 58 | + |
| 59 | + // Return entity |
| 60 | + return summon.getEntity(); |
| 61 | + } |
| 62 | + |
| 63 | + @Override |
| 64 | + public void remove(@NotNull UUID uuid) { |
| 65 | + SummonAttributes removed = summonedEntities.remove(uuid); |
| 66 | + if (removed != null) { |
| 67 | + removed.getKillTask().cancel(); |
| 68 | + SpellEntity entity = removed.getEntity(); |
| 69 | + if (entity != null) |
| 70 | + entity.remove(); |
66 | 71 | } |
67 | | - |
68 | | - @Override |
69 | | - public @Nullable UUID getUuidOfSummoner(@NotNull UUID uuid) { |
70 | | - return Optional.ofNullable(find(uuid)) |
71 | | - .map(a -> a.getSummoner().getUniqueId()) |
72 | | - .orElse(null); |
73 | | - } |
74 | | - |
75 | | - @Override |
76 | | - public @Nullable SummonAttributes find(@NotNull UUID uuid) { |
77 | | - return summonedEntities.get(uuid); |
78 | | - } |
79 | | - |
80 | | - public void refreshConfig(@NotNull UssConfig config) { |
81 | | - aggroClock.setFrequency(UssConfig.getTicksAggroSummons()); |
82 | | - } |
83 | | - |
84 | | - @Override |
85 | | - public int purgeAll() { |
86 | | - int size = summonedEntities.size(); |
87 | | - for(UUID uuid : List.copyOf(summonedEntities.keySet())) { |
88 | | - var summon = summonedEntities.get(uuid); |
89 | | - // Clean |
90 | | - summon.getKillTask().cancel(); |
91 | | - summonedEntities.remove(uuid); |
92 | | - // Remove from world |
93 | | - summon.getEntity().remove(); |
94 | | - } |
95 | | - return size; |
| 72 | + } |
| 73 | + |
| 74 | + @Override |
| 75 | + public boolean isASummonedEntity(@NotNull UUID uuid) { |
| 76 | + return summonedEntities.containsKey(uuid); |
| 77 | + } |
| 78 | + |
| 79 | + @Override |
| 80 | + public @Nullable UUID getUuidOfSummoner(@NotNull UUID uuid) { |
| 81 | + return Optional.ofNullable(find(uuid)) |
| 82 | + .map(a -> a.getSummoner().getUniqueId()) |
| 83 | + .orElse(null); |
| 84 | + } |
| 85 | + |
| 86 | + @Override |
| 87 | + public @Nullable SummonAttributes find(@NotNull UUID uuid) { |
| 88 | + return summonedEntities.get(uuid); |
| 89 | + } |
| 90 | + |
| 91 | + public void refreshConfig(@NotNull UssConfig config) { |
| 92 | + aggroClock.setFrequency(UssConfig.getTicksAggroSummons()); |
| 93 | + } |
| 94 | + |
| 95 | + @Override |
| 96 | + public int purgeAll() { |
| 97 | + int size = summonedEntities.size(); |
| 98 | + for (UUID uuid : List.copyOf(summonedEntities.keySet())) { |
| 99 | + var summon = summonedEntities.get(uuid); |
| 100 | + // Clean |
| 101 | + summon.getKillTask().cancel(); |
| 102 | + summonedEntities.remove(uuid); |
| 103 | + // Remove from world |
| 104 | + summon.getEntity().remove(); |
96 | 105 | } |
| 106 | + return size; |
| 107 | + } |
97 | 108 |
|
98 | 109 | } |
0 commit comments