Skip to content

Commit

Permalink
Add a wrapper for getting potion names post-1.20.4. (TownyAdvanced#7201)
Browse files Browse the repository at this point in the history
  • Loading branch information
LlmDl authored Jan 22, 2024
1 parent 372b355 commit 786b437
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -294,29 +294,7 @@ public void onPotionSplashEvent(PotionSplashEvent event) {
if (!TownyAPI.getInstance().isTownyWorld(event.getEntity().getWorld()))
return;

boolean detrimental = false;

/*
* List of potion effects blocked from PvP.
*/
List<String> detrimentalPotions = TownySettings.getPotionTypes();

for (PotionEffect effect : event.getPotion().getEffects()) {

/*
* Check to see if any of the potion effects are protected.
* TODO: Make up a wrapper of some kind in order to support older versions while
* using the new methods when possible.
*/
@SuppressWarnings("deprecation")
String name = effect.getType().getName();
if (detrimentalPotions.contains(name)) {
detrimental = true;
break;
}
}

if (!detrimental)
if (!hasDetrimentalEffects(event.getPotion().getEffects()))
return;

for (LivingEntity defender : event.getAffectedEntities()) {
Expand Down Expand Up @@ -935,31 +913,26 @@ private boolean entityProtectedFromExplosiveDamageHere(Entity entity, DamageCaus
private boolean hasDetrimentalEffects(Collection<PotionEffect> effects) {
if (effects.isEmpty())
return false;

/*
* List of potion effects blocked from PvP.
*/
final List<String> detrimentalPotions = TownySettings.getPotionTypes().stream().map(type -> type.toLowerCase(Locale.ROOT)).collect(Collectors.toList());

for (final PotionEffect effect : effects) {
// TODO: Make up a wrapper of some kind in order to support older versions while
// using the new methods when possible.
@SuppressWarnings("deprecation")
final String name = effect.getType().getName().toLowerCase(Locale.ROOT);
return effects.stream()
.map(effect -> BukkitTools.potionEffectName(effect.getType()))
.anyMatch(name -> {
// Check to see if any of the potion effects are protected against.
if (detrimentalPotions.contains(name))
return true;

/*
* Check to see if any of the potion effects are protected.
*/
if (detrimentalPotions.contains(name))
return true;

// Account for PotionEffect#getType possibly returning the new name post enum removal.
final String legacyName = POTION_LEGACY_NAMES.inverse().get(name);
if (legacyName != null && detrimentalPotions.contains(legacyName))
return true;
}

return false;
// Account for PotionEffect#getType possibly returning the new name post enum removal.
final String legacyName = POTION_LEGACY_NAMES.inverse().get(name);
if (legacyName != null && detrimentalPotions.contains(legacyName))
return true;

return false;
});
}

@ApiStatus.Internal
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.bukkit.event.Event;
import org.bukkit.metadata.MetadataValue;
import org.bukkit.plugin.PluginManager;
import org.bukkit.potion.PotionEffectType;
import org.bukkit.scheduler.BukkitScheduler;
import org.bukkit.scoreboard.Criteria;
import org.bukkit.scoreboard.Objective;
Expand Down Expand Up @@ -328,6 +329,14 @@ public static List<String> getWorldNames(boolean lowercased) {
return lowercased ? getWorlds().stream().map(world -> world.getName().toLowerCase(Locale.ROOT)).collect(Collectors.toList()) : getWorldNames();
}

@SuppressWarnings("deprecation")
public static String potionEffectName(PotionEffectType type) {
if (MinecraftVersion.CURRENT_VERSION.isOlderThanOrEquals(MinecraftVersion.MINECRAFT_1_20_3))
return type.getName().toLowerCase(Locale.ROOT);
else
return type.getKey().getKey().toLowerCase(Locale.ROOT);
}

@SuppressWarnings("deprecation")
public static Objective objective(Scoreboard board, @NotNull String name, @NotNull String displayName) {
Objective objective;
Expand Down

0 comments on commit 786b437

Please sign in to comment.