-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* Add inCombatEffects based on custom Events * Improve give back of potion effects. * Fix merge conflicts and add okaeri bukkit serdes. * Simplification of loops, new methods and optimization * Change HashMap based on Player to UUID for better security * Fix issues * Update src/main/java/com/eternalcode/combat/config/implementation/PluginConfig.java Co-authored-by: DMK <81445555+imDMK@users.noreply.github.com> * Update src/main/java/com/eternalcode/combat/fight/effect/EffectService.java Co-authored-by: DMK <81445555+imDMK@users.noreply.github.com> * Update src/main/java/com/eternalcode/combat/fight/effect/EffectService.java Co-authored-by: DMK <81445555+imDMK@users.noreply.github.com> * Update src/main/java/com/eternalcode/combat/fight/effect/EffectService.java Co-authored-by: DMK <81445555+imDMK@users.noreply.github.com> * Update src/main/java/com/eternalcode/combat/fight/event/FightDeathEvent.java Co-authored-by: DMK <81445555+imDMK@users.noreply.github.com> * Update src/main/java/com/eternalcode/combat/fight/event/FightTagEvent.java Co-authored-by: DMK <81445555+imDMK@users.noreply.github.com> * Update src/main/java/com/eternalcode/combat/fight/event/FightDeathEvent.java Co-authored-by: DMK <81445555+imDMK@users.noreply.github.com> * Update src/main/java/com/eternalcode/combat/fight/FightManager.java Co-authored-by: DMK <81445555+imDMK@users.noreply.github.com> * Update src/main/java/com/eternalcode/combat/fight/FightManager.java Co-authored-by: DMK <81445555+imDMK@users.noreply.github.com> * Update src/main/java/com/eternalcode/combat/fight/effect/EffectService.java Co-authored-by: DMK <81445555+imDMK@users.noreply.github.com> * Codestyle fixes * Codestyle fixes * Remove unnesesary FightDeathEvent.java, refactor class names to match project style, fixes in codestyle, move to UUID event pass down instead of bukkit player. * Add brackets * Follow Rollczi's review * Follow DMK's review. * simple rename --------- Co-authored-by: Jakubk15 <77227023+Jakubk15@users.noreply.github.com> Co-authored-by: DMK <81445555+imDMK@users.noreply.github.com>
- Loading branch information
1 parent
92535dd
commit cceeb82
Showing
11 changed files
with
333 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
src/main/java/com/eternalcode/combat/event/EventCaller.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package com.eternalcode.combat.event; | ||
|
||
import org.bukkit.Server; | ||
import org.bukkit.event.Event; | ||
|
||
public class EventCaller { | ||
|
||
private final Server server; | ||
|
||
public EventCaller(Server server) { | ||
this.server = server; | ||
} | ||
|
||
public <T extends Event> T callEvent(T event) { | ||
this.server.getPluginManager().callEvent(event); | ||
|
||
return event; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
116 changes: 116 additions & 0 deletions
116
src/main/java/com/eternalcode/combat/fight/effect/FightEffectController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
package com.eternalcode.combat.fight.effect; | ||
|
||
import com.eternalcode.combat.fight.FightManager; | ||
import com.eternalcode.combat.fight.event.FightTagEvent; | ||
import com.eternalcode.combat.fight.event.FightUntagEvent; | ||
import org.bukkit.Server; | ||
import org.bukkit.entity.Player; | ||
import org.bukkit.event.EventHandler; | ||
import org.bukkit.event.Listener; | ||
import org.bukkit.event.entity.EntityPotionEffectEvent; | ||
import org.bukkit.event.entity.PlayerDeathEvent; | ||
import org.bukkit.event.player.PlayerQuitEvent; | ||
import org.bukkit.potion.PotionEffect; | ||
|
||
public class FightEffectController implements Listener { | ||
|
||
private final FightEffectService effectService; | ||
private final FightEffectSettings effectSettings; | ||
private final FightManager fightManager; | ||
private final Server server; | ||
|
||
public FightEffectController(FightEffectSettings settings, FightEffectService effectService, FightManager fightManager, Server server) { | ||
this.effectSettings = settings; | ||
this.effectService = effectService; | ||
this.fightManager = fightManager; | ||
this.server = server; | ||
} | ||
|
||
@EventHandler | ||
public void onTag(FightTagEvent event) { | ||
if (!this.effectSettings.customEffectsEnabled) { | ||
return; | ||
} | ||
|
||
Player player = this.server.getPlayer(event.getPlayer()); | ||
|
||
if (player == null) { | ||
return; | ||
} | ||
|
||
this.effectSettings.customEffects.forEach((key, value) -> | ||
this.effectService.applyCustomEffect(player, key, value)); | ||
} | ||
|
||
@EventHandler | ||
public void onQuit(PlayerQuitEvent event) { | ||
if (!this.effectSettings.customEffectsEnabled) { | ||
return; | ||
} | ||
|
||
Player player = event.getPlayer(); | ||
this.effectService.restoreActiveEffects(player); | ||
} | ||
|
||
@EventHandler | ||
public void onUntag(FightUntagEvent event) { | ||
if (!this.effectSettings.customEffectsEnabled) { | ||
return; | ||
} | ||
|
||
Player player = this.server.getPlayer(event.getPlayer()); | ||
|
||
if (player == null) { | ||
return; | ||
} | ||
|
||
this.effectSettings.customEffects.forEach((key, value) -> this.effectService.removeCustomEffect(player, key, value)); | ||
|
||
this.effectService.restoreActiveEffects(player); | ||
} | ||
|
||
@EventHandler | ||
public void onDeath(PlayerDeathEvent event) { | ||
if (!this.effectSettings.customEffectsEnabled) { | ||
return; | ||
} | ||
|
||
Player player = event.getEntity(); | ||
this.effectService.clearStoredEffects(player); | ||
} | ||
|
||
@EventHandler | ||
public void onEffectChange(EntityPotionEffectEvent event) { | ||
if (!this.effectSettings.customEffectsEnabled) { | ||
return; | ||
} | ||
|
||
if (!(event.getEntity() instanceof Player player)) { | ||
return; | ||
} | ||
|
||
if (!this.fightManager.isInCombat(player.getUniqueId())) { | ||
return; | ||
} | ||
|
||
PotionEffect newEffect = event.getNewEffect(); | ||
PotionEffect oldEffect = event.getOldEffect(); | ||
|
||
if (!this.isRemovedEffect(newEffect, oldEffect)) { | ||
return; | ||
} | ||
|
||
Integer customAmplifier = this.effectSettings.customEffects.get(oldEffect.getType()); | ||
|
||
if (customAmplifier == null) { | ||
return; | ||
} | ||
|
||
player.addPotionEffect(new PotionEffect(oldEffect.getType(), -1, customAmplifier)); | ||
} | ||
|
||
private boolean isRemovedEffect(PotionEffect newEffect, PotionEffect oldEffect) { | ||
return newEffect == null && oldEffect != null; | ||
} | ||
|
||
} |
75 changes: 75 additions & 0 deletions
75
src/main/java/com/eternalcode/combat/fight/effect/FightEffectService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package com.eternalcode.combat.fight.effect; | ||
|
||
import org.bukkit.entity.Player; | ||
import org.bukkit.potion.PotionEffect; | ||
import org.bukkit.potion.PotionEffectType; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.UUID; | ||
import java.util.List; | ||
import java.util.ArrayList; | ||
|
||
public class FightEffectService { | ||
|
||
private final Map<UUID, List<PotionEffect>> activeEffects = new HashMap<>(); | ||
private static final int INFINITE_DURATION = -1; | ||
|
||
public void storeActiveEffect(Player player, PotionEffect effect) { | ||
List<PotionEffect> effects = this.activeEffects.computeIfAbsent(player.getUniqueId(), k -> new ArrayList<>()); | ||
|
||
effects.add(effect); | ||
} | ||
|
||
public void restoreActiveEffects(Player player) { | ||
List<PotionEffect> currentEffects = this.getCurrentEffects(player); | ||
|
||
for (PotionEffect effect : currentEffects) { | ||
player.addPotionEffect(effect); | ||
} | ||
|
||
this.clearStoredEffects(player); | ||
} | ||
|
||
public void clearStoredEffects(Player player) { | ||
this.activeEffects.remove(player.getUniqueId()); | ||
} | ||
|
||
public List<PotionEffect> getCurrentEffects(Player player) { | ||
return this.activeEffects.getOrDefault(player.getUniqueId(), new ArrayList<>()); | ||
} | ||
|
||
public void applyCustomEffect(Player player, PotionEffectType type, Integer amplifier) { | ||
PotionEffect activeEffect = player.getPotionEffect(type); | ||
|
||
if (activeEffect == null) { | ||
player.addPotionEffect(new PotionEffect(type, INFINITE_DURATION, amplifier)); | ||
return; | ||
} | ||
|
||
if (activeEffect.getAmplifier() > amplifier) { | ||
return; | ||
} | ||
|
||
if (activeEffect.getDuration() == -1) { | ||
return; | ||
} | ||
|
||
this.storeActiveEffect(player, activeEffect); | ||
player.addPotionEffect(new PotionEffect(type, INFINITE_DURATION, amplifier)); | ||
} | ||
|
||
public void removeCustomEffect(Player player, PotionEffectType type, Integer amplifier) { | ||
PotionEffect activeEffect = player.getPotionEffect(type); | ||
|
||
if (activeEffect == null) { | ||
return; | ||
} | ||
|
||
if (activeEffect.getAmplifier() != amplifier) { | ||
return; | ||
} | ||
|
||
player.removePotionEffect(type); | ||
} | ||
} |
Oops, something went wrong.