Skip to content

Commit e5438e7

Browse files
committed
Reapply potion effects to the player if he already has it
Fixes #205
1 parent fc66ed6 commit e5438e7

File tree

3 files changed

+28
-6
lines changed

3 files changed

+28
-6
lines changed

src/com/dre/brewery/BEffect.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ public void apply(int quality, Player player) {
9393
if (!P.use1_14) {
9494
duration /= type.getDurationModifier();
9595
}
96-
type.createEffect(duration, lvl - 1).apply(player);
96+
Util.reapplyPotionEffect(player, type.createEffect(duration, lvl - 1), true);
9797
}
9898

9999
public int calcDuration(float quality) {

src/com/dre/brewery/BPlayer.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -497,7 +497,7 @@ public static void addQualityEffects(int quality, int brewAlc, Player player) {
497497
duration *= 4;
498498
}
499499
if (duration > 0) {
500-
PotionEffectType.POISON.createEffect(duration, 0).apply(player);
500+
Util.reapplyPotionEffect(player, PotionEffectType.POISON.createEffect(duration, 0), true);
501501
}
502502

503503
if (brewAlc > 10) {
@@ -511,7 +511,7 @@ public static void addQualityEffects(int quality, int brewAlc, Player player) {
511511
if (!P.use1_14) {
512512
duration *= 4;
513513
}
514-
PotionEffectType.BLINDNESS.createEffect(duration, 0).apply(player);
514+
Util.reapplyPotionEffect(player, PotionEffectType.BLINDNESS.createEffect(duration, 0), true);
515515
}
516516
}
517517

@@ -531,12 +531,12 @@ public void hangoverEffects(final Player player) {
531531
}
532532
int amplifier = getHangoverQuality() / 3;
533533

534-
PotionEffectType.SLOW.createEffect(duration, amplifier).apply(player);
535-
PotionEffectType.HUNGER.createEffect(duration, amplifier).apply(player);
534+
Util.reapplyPotionEffect(player, PotionEffectType.SLOW.createEffect(duration, amplifier), true);
535+
Util.reapplyPotionEffect(player, PotionEffectType.HUNGER.createEffect(duration, amplifier), true);
536536
}
537537

538538

539-
// #### Sheduled ####
539+
// #### Scheduled ####
540540

541541
public static void drunkeness() {
542542
for (Map.Entry<String, BPlayer> entry : players.entrySet()) {

src/com/dre/brewery/Util.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
import org.bukkit.command.CommandSender;
88
import org.bukkit.configuration.ConfigurationSection;
99
import org.bukkit.entity.Player;
10+
import org.bukkit.potion.PotionEffect;
11+
import org.bukkit.potion.PotionEffectType;
1012

1113
import java.io.File;
1214
import java.io.FileOutputStream;
@@ -54,6 +56,26 @@ public static Player getPlayerfromString(String name) {
5456
return Bukkit.getPlayerExact(name);
5557
}
5658

59+
// Apply a Potion Effect, if player already has this effect, overwrite the existing effect.
60+
// Optionally only overwrite if the new one is stronger, i.e. has higher level or longer duration
61+
public static void reapplyPotionEffect(Player player, PotionEffect effect, boolean onlyIfStronger) {
62+
final PotionEffectType type = effect.getType();
63+
if (player.hasPotionEffect(type)) {
64+
PotionEffect plEffect;
65+
if (P.use1_11) {
66+
plEffect = player.getPotionEffect(type);
67+
} else {
68+
plEffect = player.getActivePotionEffects().stream().filter(e -> e.getType().equals(type)).findAny().get();
69+
}
70+
if (plEffect.getAmplifier() < effect.getAmplifier() || (plEffect.getAmplifier() == effect.getAmplifier() && plEffect.getDuration() < effect.getDuration())) {
71+
player.removePotionEffect(type);
72+
} else {
73+
return;
74+
}
75+
}
76+
effect.apply(player);
77+
}
78+
5779
/********** Other Utils **********/
5880

5981
// prints a list of Strings at the specified page

0 commit comments

Comments
 (0)