Skip to content

Commit

Permalink
Add compatibility with new PotionEffectType
Browse files Browse the repository at this point in the history
  • Loading branch information
kernitus committed Jun 2, 2024
1 parent 649f50b commit 76fbcc9
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 40 deletions.
9 changes: 5 additions & 4 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ dependencies {
// Spartan API
compileOnly("me.vagdedes.spartan:SpartanAPI:9.0")
// For BSON file serialisation
implementation("org.mongodb:bson:4.11.1")
implementation("org.mongodb:bson:5.0.1")
// Spigot
compileOnly("org.spigotmc:spigot-api:1.20.3-R0.1-SNAPSHOT")
compileOnly("org.spigotmc:spigot-api:1.20.6-R0.1-SNAPSHOT")
// ProtocolLib
compileOnly("com.comphenix.protocol:ProtocolLib:5.1.0")

Expand All @@ -72,8 +72,9 @@ description = "OldCombatMechanics"

java {
toolchain {
// At least 17 required for MC 1.19 for ingametesting
languageVersion.set(JavaLanguageVersion.of(8))
// We can build with Java 17 but still support MC >=1.9
// This is because MC >=1.9 server can be run with higher Java versions
languageVersion.set(JavaLanguageVersion.of(17))
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public void onRodLand(ProjectileHitEvent e) {
final Entity hookEntity = e.getEntity();
final World world = hookEntity.getWorld();

if (e.getEntityType() != EntityType.FISHING_HOOK) return;
if (e.getEntityType() != EntityType.FISHING_BOBBER) return;
final FishHook hook = (FishHook) hookEntity;

if(!(hook.getShooter() instanceof Player)) return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.google.common.collect.ImmutableSet;
import kernitus.plugin.OldCombatMechanics.OCMMain;
import kernitus.plugin.OldCombatMechanics.utilities.Messenger;
import kernitus.plugin.OldCombatMechanics.utilities.potions.PotionEffectTypeCompat;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
Expand Down Expand Up @@ -43,7 +44,7 @@ public class ModuleGoldenApple extends OCMModule {
PotionEffectType.REGENERATION);
// Napple: absorption IV, regen II, fire resistance I, resistance I
private static final Set<PotionEffectType> nappleEffects = ImmutableSet.of(PotionEffectType.ABSORPTION,
PotionEffectType.REGENERATION, PotionEffectType.FIRE_RESISTANCE, PotionEffectType.DAMAGE_RESISTANCE);
PotionEffectType.REGENERATION, PotionEffectType.FIRE_RESISTANCE, PotionEffectTypeCompat.RESISTANCE.get());
private List<PotionEffect> enchantedGoldenAppleEffects, goldenAppleEffects;
private ShapedRecipe enchantedAppleRecipe;

Expand Down Expand Up @@ -96,7 +97,7 @@ public static ModuleGoldenApple getInstance() {

private void registerCrafting() {
if (isEnabled() && module().getBoolean("enchanted-golden-apple-crafting")) {
if (Bukkit.getRecipesFor(ENCHANTED_GOLDEN_APPLE.newInstance()).size() > 0) return;
if (!Bukkit.getRecipesFor(ENCHANTED_GOLDEN_APPLE.newInstance()).isEmpty()) return;
Bukkit.addRecipe(enchantedAppleRecipe);
debug("Added napple recipe");
}
Expand Down Expand Up @@ -216,7 +217,7 @@ private List<PotionEffect> getPotionEffects(String apple) {
final int duration = sect.getInt(key + ".duration");
final int amplifier = sect.getInt(key + ".amplifier");

final PotionEffectType type = PotionEffectType.getByName(key);
final PotionEffectType type = PotionEffectTypeCompat.valueOf(key).get();
Objects.requireNonNull(type, String.format("Invalid potion effect type '%s'!", key));

final PotionEffect fx = new PotionEffect(type, duration, amplifier);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public void onItemDamage(PlayerItemDamageEvent e) {
int reduction = module().getInt("reduction");

// 60 + (40 / (level + 1) ) % chance that durability is reduced (for each point of durability)
final int damageChance = 60 + (40 / (item.getEnchantmentLevel(Enchantment.DURABILITY) + 1));
final int damageChance = 60 + (40 / (item.getEnchantmentLevel(Enchantment.UNBREAKING) + 1));
final Random random = new Random();
final int randomInt = random.nextInt(100); // between 0 (inclusive) and 100 (exclusive)
if (randomInt >= damageChance)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

package kernitus.plugin.OldCombatMechanics.utilities.damage;

import kernitus.plugin.OldCombatMechanics.utilities.potions.PotionEffectTypeCompat;
import kernitus.plugin.OldCombatMechanics.utilities.reflection.Reflector;
import kernitus.plugin.OldCombatMechanics.utilities.reflection.SpigotFunctionChooser;
import kernitus.plugin.OldCombatMechanics.utilities.reflection.VersionCompatUtils;
Expand All @@ -17,7 +18,6 @@
import org.bukkit.event.entity.EntityDamageEvent;
import org.bukkit.inventory.EquipmentSlot;
import org.bukkit.inventory.ItemStack;
import org.bukkit.potion.PotionEffectType;

import java.util.Collection;
import java.util.EnumSet;
Expand Down Expand Up @@ -111,8 +111,8 @@ public static void calculateDefenceDamageReduction(LivingEntity damagedEntity,
// Apply resistance effect
if (damageModifiers.containsKey(EntityDamageEvent.DamageModifier.RESISTANCE) &&
damageCause != EntityDamageEvent.DamageCause.VOID &&
damagedEntity.hasPotionEffect(PotionEffectType.DAMAGE_RESISTANCE)) {
final int level = damagedEntity.getPotionEffect(PotionEffectType.DAMAGE_RESISTANCE).getAmplifier() + 1;
damagedEntity.hasPotionEffect(PotionEffectTypeCompat.RESISTANCE.get())) {
final int level = damagedEntity.getPotionEffect(PotionEffectTypeCompat.RESISTANCE.get()).getAmplifier() + 1;
// Make sure we don't go over 100% protection
final double resistanceReductionFactor = Math.min(1.0, level * REDUCTION_PER_RESISTANCE_LEVEL);
final double resistanceReduction = -resistanceReductionFactor * currentDamage;
Expand Down Expand Up @@ -163,8 +163,8 @@ public static double getDamageAfterArmour1_8(LivingEntity defender, double baseD
double finalDamage = baseDamage - (ARMOUR_IGNORING_CAUSES.contains(damageCause) ? 0 : (baseDamage * reductionFactor));

// Calculate resistance
if (defender.hasPotionEffect(PotionEffectType.DAMAGE_RESISTANCE)) {
int resistanceLevel = defender.getPotionEffect(PotionEffectType.DAMAGE_RESISTANCE).getAmplifier() + 1;
if (defender.hasPotionEffect(PotionEffectTypeCompat.RESISTANCE.get())) {
int resistanceLevel = defender.getPotionEffect(PotionEffectTypeCompat.RESISTANCE.get()).getAmplifier() + 1;
finalDamage *= 1.0 - (resistanceLevel * 0.2);
}

Expand Down Expand Up @@ -261,7 +261,7 @@ private enum EnchantmentType {

return damageCauses;
},
0.75, Enchantment.PROTECTION_ENVIRONMENTAL),
0.75, Enchantment.PROTECTION),
FIRE_PROTECTION(() -> {
EnumSet<EntityDamageEvent.DamageCause> damageCauses = EnumSet.of(
EntityDamageEvent.DamageCause.FIRE,
Expand All @@ -274,17 +274,17 @@ private enum EnchantmentType {
}

return damageCauses;
}, 1.25, Enchantment.PROTECTION_FIRE),
}, 1.25, Enchantment.FIRE_PROTECTION),
BLAST_PROTECTION(() -> EnumSet.of(
EntityDamageEvent.DamageCause.ENTITY_EXPLOSION,
EntityDamageEvent.DamageCause.BLOCK_EXPLOSION
), 1.5, Enchantment.PROTECTION_EXPLOSIONS),
), 1.5, Enchantment.BLAST_PROTECTION),
PROJECTILE_PROTECTION(() -> EnumSet.of(
EntityDamageEvent.DamageCause.PROJECTILE
), 1.5, Enchantment.PROTECTION_PROJECTILE),
), 1.5, Enchantment.PROJECTILE_PROTECTION),
FALL_PROTECTION(() -> EnumSet.of(
EntityDamageEvent.DamageCause.FALL
), 2.5, Enchantment.PROTECTION_FALL);
), 2.5, Enchantment.FEATHER_FALLING);

private final Set<EntityDamageEvent.DamageCause> protection;
private final double typeModifier;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,26 +20,26 @@ public class MobDamage {
Map<String, Enchantment> allMobs = ImmutableMap.<String, Enchantment>builder()

// Undead (https://minecraft.gamepedia.com/Undead)
.put("SKELETON", Enchantment.DAMAGE_UNDEAD)
.put("ZOMBIE", Enchantment.DAMAGE_UNDEAD)
.put("WITHER", Enchantment.DAMAGE_UNDEAD)
.put("WITHER_SKELETON", Enchantment.DAMAGE_UNDEAD)
.put("ZOMBIFIED_PIGLIN", Enchantment.DAMAGE_UNDEAD)
.put("SKELETON_HORSE", Enchantment.DAMAGE_UNDEAD)
.put("STRAY", Enchantment.DAMAGE_UNDEAD)
.put("HUSK", Enchantment.DAMAGE_UNDEAD)
.put("PHANTOM", Enchantment.DAMAGE_UNDEAD)
.put("DROWNED", Enchantment.DAMAGE_UNDEAD)
.put("ZOGLIN", Enchantment.DAMAGE_UNDEAD)
.put("ZOMBIE_HORSE", Enchantment.DAMAGE_UNDEAD)
.put("ZOMBIE_VILLAGER", Enchantment.DAMAGE_UNDEAD)
.put("SKELETON", Enchantment.SMITE)
.put("ZOMBIE", Enchantment.SMITE)
.put("WITHER", Enchantment.SMITE)
.put("WITHER_SKELETON", Enchantment.SMITE)
.put("ZOMBIFIED_PIGLIN", Enchantment.SMITE)
.put("SKELETON_HORSE", Enchantment.SMITE)
.put("STRAY", Enchantment.SMITE)
.put("HUSK", Enchantment.SMITE)
.put("PHANTOM", Enchantment.SMITE)
.put("DROWNED", Enchantment.SMITE)
.put("ZOGLIN", Enchantment.SMITE)
.put("ZOMBIE_HORSE", Enchantment.SMITE)
.put("ZOMBIE_VILLAGER", Enchantment.SMITE)

// Arthropods (https://minecraft.gamepedia.com/Arthropod)
.put("SPIDER", Enchantment.DAMAGE_ARTHROPODS)
.put("CAVE_SPIDER", Enchantment.DAMAGE_ARTHROPODS)
.put("BEE", Enchantment.DAMAGE_ARTHROPODS)
.put("SILVERFISH", Enchantment.DAMAGE_ARTHROPODS)
.put("ENDERMITE", Enchantment.DAMAGE_ARTHROPODS)
.put("SPIDER", Enchantment.BANE_OF_ARTHROPODS)
.put("CAVE_SPIDER", Enchantment.BANE_OF_ARTHROPODS)
.put("BEE", Enchantment.BANE_OF_ARTHROPODS)
.put("SILVERFISH", Enchantment.BANE_OF_ARTHROPODS)
.put("ENDERMITE", Enchantment.BANE_OF_ARTHROPODS)

.build();

Expand Down Expand Up @@ -67,7 +67,7 @@ public class MobDamage {
public static double getEntityEnchantmentsDamage(EntityType entity, ItemStack item) {
final Enchantment enchantment = enchants.get(entity);

if (enchantment == null || enchantment != Enchantment.DAMAGE_UNDEAD || enchantment != Enchantment.DAMAGE_ARTHROPODS)
if (enchantment == null || enchantment != Enchantment.SMITE || enchantment != Enchantment.BANE_OF_ARTHROPODS)
return 0;

return 2.5 * item.getEnchantmentLevel(enchantment);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/
package kernitus.plugin.OldCombatMechanics.utilities.damage;

import kernitus.plugin.OldCombatMechanics.utilities.potions.PotionEffectTypeCompat;
import kernitus.plugin.OldCombatMechanics.utilities.potions.PotionEffects;
import org.bukkit.Material;
import org.bukkit.enchantments.Enchantment;
Expand Down Expand Up @@ -115,7 +116,7 @@ however we only do this for DamageByEntity, so there could still be environmenta


mobEnchantmentsDamage = MobDamage.getEntityEnchantmentsDamage(damageeType, weapon);
sharpnessLevel = weapon.getEnchantmentLevel(Enchantment.DAMAGE_ALL);
sharpnessLevel = weapon.getEnchantmentLevel(Enchantment.SHARPNESS);
sharpnessDamage = DamageUtils.getNewSharpnessDamage(sharpnessLevel);

// Scale enchantment damage by attack cooldown
Expand Down Expand Up @@ -152,7 +153,7 @@ however we only do this for DamageByEntity, so there could still be environmenta
}

// amplifier 0 = Strength I amplifier 1 = Strength II
strengthLevel = PotionEffects.get(livingDamager, PotionEffectType.INCREASE_DAMAGE)
strengthLevel = PotionEffects.get(livingDamager, PotionEffectTypeCompat.STRENGTH.get())
.map(PotionEffect::getAmplifier)
.orElse(-1) + 1;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/

package kernitus.plugin.OldCombatMechanics.utilities.potions;

import org.bukkit.potion.PotionEffectType;

public enum PotionEffectTypeCompat {
RESISTANCE("DAMAGE_RESISTANCE"),
NAUSEA("CONFUSION"),
HASTE("FAST_DIGGING"),
INSTANT_DAMAGE("HARM"),
INSTANT_HEALTH("HEAL"),
STRENGTH("INCREASE_DAMAGE"),
JUMP_BOOST("JUMP"),
SLOWNESS("SLOW"),
MINING_FATIGUE("SLOW_DIGGING"),
;

private PotionEffectType potionEffectType;

PotionEffectTypeCompat(String oldName) {
// Try loading the new name first
// This only happens once per enum name
potionEffectType = PotionEffectType.getByName(name());
// If the new name doesn't exist, fall back to the old name
if (potionEffectType == null) {
potionEffectType = PotionEffectType.getByName(oldName);
}

if (potionEffectType == null) {
throw new IllegalStateException("PotionEffectType not found for: " + name() + " or " + oldName);
}
}

public PotionEffectType get() {
return potionEffectType;
}

// Static method to get the custom enum from the Bukkit PotionEffectType
public static PotionEffectTypeCompat fromBukkit(PotionEffectType bukkitType) {
for (PotionEffectTypeCompat compatType : values()) {
if (compatType.get().equals(bukkitType)) {
return compatType;
}
}
throw new IllegalArgumentException("No matching PotionEffectTypeCompat for " + bukkitType.getName());
}
}

0 comments on commit 76fbcc9

Please sign in to comment.