Changes for Paper enchantment API support; renaming EcoEnchant#getRarity to EcoEnchant#getEnchantmentRarity

This commit is contained in:
Auxilor
2021-09-18 15:39:51 +01:00
parent 6e0a750e9b
commit b2135beae9
10 changed files with 152 additions and 16 deletions

View File

@@ -3,10 +3,10 @@ version rootProject.version
dependencies {
compileOnly project(":eco-core:core-proxy")
compileOnly 'org.spigotmc:spigot:1.17-R0.1-SNAPSHOT'
compileOnly 'org.spigotmc:spigot-api:1.17-R0.1-SNAPSHOT'
compileOnly 'commons-io:commons-io:2.8.0'
compileOnly 'com.comphenix.protocol:ProtocolLib:4.6.0-SNAPSHOT'
compileOnly 'net.essentialsx:EssentialsX:2.19.0'
compileOnly 'com.destroystokyo.paper:paper-api:1.16.3-R0.1-SNAPSHOT'
compileOnly 'io.papermc.paper:paper-api:1.17.1-R0.1-SNAPSHOT'
compileOnly 'com.sk89q.worldguard:worldguard-bukkit:7.0.4-SNAPSHOT'
}

View File

@@ -102,7 +102,7 @@ public class CommandDebug extends Subcommand {
Set<EcoEnchant> withIssues = new HashSet<>();
EcoEnchants.values().forEach(enchant -> {
if (enchant.getRarity() == null) {
if (enchant.getEnchantmentRarity() == null) {
withIssues.add(enchant);
}
if (enchant.getTargets().isEmpty()) {

View File

@@ -140,7 +140,7 @@ public class CommandEnchantinfo extends PluginCommand {
final String finalName = EnchantmentCache.getEntry(enchantment).getName();
final String finalDescription = EnchantmentCache.getEntry(enchantment).getStringDescription(1);
final EnchantmentRarity finalRarity = enchantment.getRarity();
final EnchantmentRarity finalRarity = enchantment.getEnchantmentRarity();
final String finalTargets = allTargets;
final String finalConflicts = allConflicts;
final String finalMaxLevel = maxLevel;

View File

@@ -73,7 +73,7 @@ public class CommandGiverandombook extends Subcommand {
if (!(enchantment instanceof EcoEnchant)) {
return false;
}
return ((EcoEnchant) enchantment).getRarity().equals(rarity);
return ((EcoEnchant) enchantment).getEnchantmentRarity().equals(rarity);
}
return true;
}).collect(Collectors.toList());

View File

@@ -109,7 +109,7 @@ public class EnchantmentCache {
description = StringUtils.formatList(ecoEnchant.getWrappedDescription());
name = ecoEnchant.getDisplayName();
type = ecoEnchant.getType();
rarity = ecoEnchant.getRarity();
rarity = ecoEnchant.getEnchantmentRarity();
} else {
description = Arrays.asList(
WordUtils.wrap(

View File

@@ -5,13 +5,16 @@ import com.willfp.eco.core.requirement.Requirement;
import com.willfp.eco.core.requirement.Requirements;
import com.willfp.ecoenchants.EcoEnchantsPlugin;
import com.willfp.ecoenchants.config.configs.EnchantmentConfig;
import com.willfp.ecoenchants.display.EnchantmentCache;
import com.willfp.ecoenchants.enchantments.meta.EnchantmentRarity;
import com.willfp.ecoenchants.enchantments.meta.EnchantmentTarget;
import com.willfp.ecoenchants.enchantments.meta.EnchantmentType;
import com.willfp.ecoenchants.enchantments.util.EnchantmentUtils;
import com.willfp.ecoenchants.enchantments.util.PaperHelper;
import com.willfp.ecoenchants.enchantments.util.Watcher;
import lombok.AccessLevel;
import lombok.Getter;
import net.kyori.adventure.text.Component;
import org.apache.commons.lang.Validate;
import org.apache.commons.lang.WordUtils;
import org.bukkit.Bukkit;
@@ -19,9 +22,11 @@ import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.World;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.EntityCategory;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.event.Listener;
import org.bukkit.inventory.EquipmentSlot;
import org.bukkit.inventory.ItemStack;
import org.bukkit.permissions.Permission;
import org.bukkit.permissions.PermissionDefault;
@@ -140,7 +145,7 @@ public abstract class EcoEnchant extends Enchantment implements Listener, Watche
* The rarity of the enchantment.
*/
@Getter
private EnchantmentRarity rarity;
private EnchantmentRarity enchantmentRarity;
/**
* If the enchantment is enabled.
@@ -220,8 +225,8 @@ public abstract class EcoEnchant extends Enchantment implements Listener, Watche
*/
public void update() {
config.loadFromLang();
rarity = config.getRarity();
Validate.notNull(rarity, "Rarity specified in " + this.permissionName + " is invalid!");
enchantmentRarity = config.getRarity();
Validate.notNull(enchantmentRarity, "Rarity specified in " + this.permissionName + " is invalid!");
conflicts = config.getEnchantments(EcoEnchants.GENERAL_LOCATION + "conflicts");
grindstoneable = config.getBool(EcoEnchants.GENERAL_LOCATION + "grindstoneable");
availableFromTable = config.getBool(EcoEnchants.OBTAINING_LOCATION + "table");
@@ -439,4 +444,110 @@ public abstract class EcoEnchant extends Enchantment implements Listener, Watche
public boolean canEnchantItem(@NotNull final ItemStack itemStack) {
return targetMaterials.contains(itemStack.getType()) || itemStack.getType().equals(Material.BOOK) || itemStack.getType().equals(Material.ENCHANTED_BOOK);
}
/**
* Paper parity.
* <p>
* You should use EnchantmentCache instead.
*
* @param level The level.
* @return The display name.
* @deprecated Use {@link EnchantmentCache#getEntry(Enchantment)} instead.
*/
@Deprecated
@Override
public @NotNull Component displayName(final int level) {
return PaperHelper.toComponent(EnchantmentCache.getEntry(this).getNameWithLevel(level));
}
/**
* Paper parity.
* <p>
* You should use {@link EcoEnchant#isAvailableFromVillager()} instead.
*
* @return If tradeable.
* @deprecated Use {@link EcoEnchant#isAvailableFromVillager()} instead.
*/
@Deprecated
@Override
public boolean isTradeable() {
return this.isAvailableFromVillager();
}
/**
* Paper parity.
* <p>
* You should use {@link EcoEnchant#isAvailableFromLoot()} instead.
*
* @return If discoverable.
* @deprecated Use {@link EcoEnchant#isAvailableFromLoot()} instead.
*/
@Deprecated
@Override
public boolean isDiscoverable() {
return this.isAvailableFromLoot();
}
/**
* Paper parity.
* <p>
* EcoEnchants has its own systems for everything like this. Will always return 0.
*
* @param level The level.
* @param entityCategory The category.
* @return 0
* @deprecated EcoEnchants has its own systems for this.
*/
@Deprecated
@Override
public float getDamageIncrease(final int level,
@NotNull final EntityCategory entityCategory) {
return 0;
}
/**
* Paper parity.
* <p>
* EcoEnchants has its own systems for targets.
* <p>
* Use {@link EcoEnchant#getTargets()} instead.
*
* @return An empty set.
* @deprecated Use {@link EcoEnchant#getTargets()}.
*/
@Deprecated
@Override
public @NotNull Set<EquipmentSlot> getActiveSlots() {
return new HashSet<>();
}
/**
* Paper parity.
* <p>
* eco / EcoEnchants recodes display entirely.
*
* @return A translation key.
* @deprecated Useless method, all items will be display differently using eco.
*/
@Deprecated
@Override
public @NotNull String translationKey() {
return "ecoenchants:enchantment." + this.getKey().getKey();
}
/**
* Paper parity.
* <p>
* EcoEnchants has its own systems for rarity.
* <p>
* Use {@link EcoEnchant#getEnchantmentRarity()} instead.
*
* @return {@link io.papermc.paper.enchantments.EnchantmentRarity#COMMON}.
* @deprecated Use {@link EcoEnchant#getEnchantmentRarity()}.
*/
@Deprecated
@Override
public @NotNull io.papermc.paper.enchantments.EnchantmentRarity getRarity() {
return io.papermc.paper.enchantments.EnchantmentRarity.COMMON;
}
}

View File

@@ -125,10 +125,10 @@ public class EnchantingListeners extends PluginDependent<EcoPlugin> implements L
if (!enchantment.canEnchantItem(item)) {
continue;
}
if (NumberUtils.randFloat(0, 1) > enchantment.getRarity().getTableProbability() * multiplier) {
if (NumberUtils.randFloat(0, 1) > enchantment.getEnchantmentRarity().getTableProbability() * multiplier) {
continue;
}
if (enchantment.getRarity().getMinimumLevel() > cost) {
if (enchantment.getEnchantmentRarity().getMinimumLevel() > cost) {
continue;
}
if (!enchantment.isEnabled()) {
@@ -172,7 +172,7 @@ public class EnchantingListeners extends PluginDependent<EcoPlugin> implements L
level = (int) Math.ceil(enchantlevel2 / enchantlevel3);
} else {
int maxLevel = this.getPlugin().getConfigYml().getInt("enchanting-table.maximum-obtainable-level");
double enchantlevel1 = (cost / (double) enchantment.getRarity().getMinimumLevel()) / (maxLevel / (double) enchantment.getRarity().getMinimumLevel());
double enchantlevel1 = (cost / (double) enchantment.getEnchantmentRarity().getMinimumLevel()) / (maxLevel / (double) enchantment.getEnchantmentRarity().getMinimumLevel());
double enchantlevel2 = NumberUtils.triangularDistribution(0, 1, enchantlevel1);
double enchantlevel3 = 1 / maxLevelDouble;
level = (int) Math.ceil(enchantlevel2 / enchantlevel3);

View File

@@ -92,11 +92,11 @@ public class LootPopulator extends BlockPopulator {
int cap = 0;
for (EcoEnchant enchantment : enchantments) {
if (enchantment == null || enchantment.getRarity() == null) {
if (enchantment == null || enchantment.getEnchantmentRarity() == null) {
continue;
}
if (NumberUtils.randFloat(0, 1) > enchantment.getRarity().getLootProbability() * multiplier) {
if (NumberUtils.randFloat(0, 1) > enchantment.getEnchantmentRarity().getLootProbability() * multiplier) {
continue;
}

View File

@@ -73,7 +73,7 @@ public class VillagerListeners extends PluginDependent<EcoPlugin> implements Lis
EcoEnchant applied = null;
for (EcoEnchant enchantment : enchantments) {
if (NumberUtils.randFloat(0, 1) > enchantment.getRarity().getVillagerProbability() * multiplier) {
if (NumberUtils.randFloat(0, 1) > enchantment.getEnchantmentRarity().getVillagerProbability() * multiplier) {
continue;
}
@@ -167,7 +167,7 @@ public class VillagerListeners extends PluginDependent<EcoPlugin> implements Lis
double multiplier = 0.01;
for (EcoEnchant enchantment : enchantments) {
if (NumberUtils.randFloat(0, 1) > enchantment.getRarity().getVillagerProbability() * multiplier) {
if (NumberUtils.randFloat(0, 1) > enchantment.getEnchantmentRarity().getVillagerProbability() * multiplier) {
continue;
}

View File

@@ -0,0 +1,25 @@
package com.willfp.ecoenchants.enchantments.util;
import lombok.experimental.UtilityClass;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
import org.jetbrains.annotations.NotNull;
@UtilityClass
public class PaperHelper {
private static final LegacyComponentSerializer SERIALIZER = LegacyComponentSerializer.builder()
.hexColors()
.useUnusualXRepeatedCharacterHexFormat()
.character('§')
.build();
/**
* Convert string to a component.
*
* @param string The string.
* @return The component.
*/
public static Component toComponent(@NotNull final String string) {
return SERIALIZER.deserialize(string);
}
}