Skip to content

Commit

Permalink
Remove legacy support code and fix cost calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
minacle committed Dec 14, 2024
1 parent b6200c3 commit 6c97ea9
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 119 deletions.
20 changes: 13 additions & 7 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar
import io.papermc.paperweight.userdev.ReobfArtifactConfiguration

plugins {
id "java"
id "io.github.goooler.shadow" version "8.1.7"
id "xyz.jpenilla.run-paper" version "2.3.0"
id "com.gradleup.shadow" version "8.3.5"
id "io.papermc.paperweight.userdev" version "1.7.2"
id "xyz.jpenilla.run-paper" version "2.3.1"
}

group = "moe.minacle.minecraft"
version = "0.4.1"
version = "0.5.0"

repositories {
mavenCentral()
Expand All @@ -26,9 +28,11 @@ repositories {
}

dependencies {
compileOnly "io.papermc.paper:paper-api:1.21-R0.1-SNAPSHOT"
implementation "org.bstats:bstats-bukkit:3.0.2"
implementation "org.jetbrains:annotations:24.1.0"
compileOnly "io.papermc.paper:paper-api:1.21.4-R0.1-SNAPSHOT"
implementation "org.bstats:bstats-bukkit:3.1.0"
paperweight {
paperDevBundle "1.21.4-R0.1-SNAPSHOT"
}
}

java {
Expand All @@ -48,7 +52,7 @@ processResources {

tasks {
runServer {
minecraftVersion "1.21"
minecraftVersion "1.21.4"
}
}

Expand All @@ -61,3 +65,5 @@ shadowJar {
archiveClassifier.set("")
minimize()
}

paperweight.reobfArtifactConfiguration = ReobfArtifactConfiguration.getMOJANG_PRODUCTION()
126 changes: 16 additions & 110 deletions src/main/java/moe/minacle/minecraft/plugins/enchantedbook/Plugin.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package moe.minacle.minecraft.plugins.enchantedbook;

import java.lang.reflect.Method;
import java.util.Map;

import org.jetbrains.annotations.NotNull;

import org.bstats.bukkit.Metrics;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.craftbukkit.enchantments.CraftEnchantment;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
Expand All @@ -15,21 +17,15 @@
import org.bukkit.inventory.meta.EnchantmentStorageMeta;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.inventory.meta.Repairable;
import org.bukkit.inventory.view.AnvilView;
import org.bukkit.plugin.java.JavaPlugin;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public final class Plugin extends JavaPlugin implements Listener {

private static final int BSTATS_PLUGIN_ID = 20341;

private static final Integer ENCHANTMENT_WEIGHT_COMMON = 10;
private static final Integer ENCHANTMENT_WEIGHT_UNCOMMON = 5;
private static final Integer ENCHANTMENT_WEIGHT_RARE = 2;
private static final Integer ENCHANTMENT_WEIGHT_VERY_RARE = 1;

/**
* Calculates the cost of an enchantment based on its rarity and level.
* Calculates the cost of an enchantment based on its anvil cost and level.
*
* @param enchantment
* the enchantment to calculate the cost for
Expand All @@ -38,117 +34,27 @@ public final class Plugin extends JavaPlugin implements Listener {
* the level of the enchantment
*
* @return
* the cost of the enchantment, or -1 if the rarity is invalid
* the cost of the enchantment, or -1 if the enchantment is invalid
*/
private static int getEnchantmentCost(final @NotNull Enchantment enchantment, int level) {
int enchantmentCost;
final Object handle;
handle = getHandle(enchantment);
if (handle == null)
return -1;
if ((enchantmentCost = getEnchantmentCostByWeight(handle, level)) >= 0)
return enchantmentCost;
if ((enchantmentCost = getEnchantmentCostByAnvilCost(handle, level)) >= 0)
return enchantmentCost;
try {
enchantmentCost = getEnchantmentCostByRarity(enchantment, level);
}
catch (final UnsupportedOperationException exception) {
}
if (enchantmentCost >= 0)
return enchantmentCost;
Bukkit.getLogger().severe("Enchantment cost calculation is not supported for this server version.");
return -1;
}

@SuppressWarnings("unused")
private static int getEnchantmentCostByAnvilCost(final @NotNull Enchantment enchantment, int level) {
return getEnchantmentCostByAnvilCost(getHandle(enchantment), level);
}

private static int getEnchantmentCostByAnvilCost(final @Nullable Object handle, int level) {
Method getAnvilCostMethod = null;
final Object anvilCost;
try {
getAnvilCostMethod = handle.getClass().getMethod("getAnvilCost");
}
catch (final Exception exception) {
final int anvilCost;
final CraftEnchantment craftEnchantment;
final net.minecraft.world.item.enchantment.Enchantment nmsEnchantment;
if (level <= 0)
return -1;
}
if (getAnvilCostMethod == null)
if ((craftEnchantment = (CraftEnchantment)enchantment) == null)
return -1;
try {
anvilCost = getAnvilCostMethod.invoke(handle);
}
catch (final Exception exception) {
if ((nmsEnchantment = craftEnchantment.getHandle()) == null)
return -1;
}
if (anvilCost instanceof Integer)
return (int)anvilCost * level;
return -1;
}

@SuppressWarnings({"deprecation", "removal"})
private static int getEnchantmentCostByRarity(final @NotNull Enchantment enchantment, int level) {
switch (enchantment.getRarity()) {
case COMMON:
case UNCOMMON:
return level;
case RARE:
return level * 2;
case VERY_RARE:
return level * 4;
default:
if ((anvilCost = nmsEnchantment.getAnvilCost()) <= 0)
return -1;
}
}

@SuppressWarnings("unused")
private static int getEnchantmentCostByWeight(final @NotNull Enchantment enchantment, int level) {
return getEnchantmentCostByWeight(getHandle(enchantment), level);
}

private static int getEnchantmentCostByWeight(final @Nullable Object handle, int level) {
Method getWeightMethod = null;
final Object weight;
try {
getWeightMethod = handle.getClass().getMethod("getWeight");
}
catch (final Exception exception) {
return -1;
}
if (getWeightMethod == null)
return -1;
try {
weight = getWeightMethod.invoke(handle);
}
catch (final Exception exception) {
return -1;
}
if (
ENCHANTMENT_WEIGHT_COMMON.equals(weight) ||
ENCHANTMENT_WEIGHT_UNCOMMON.equals(weight)
)
return level;
if (ENCHANTMENT_WEIGHT_RARE.equals(weight))
return level * 2;
if (ENCHANTMENT_WEIGHT_VERY_RARE.equals(weight))
return level * 4;
return -1;
}

private static @Nullable Object getHandle(final @NotNull Enchantment enchantment) {
try {
return enchantment.getClass().getMethod("getHandle").invoke(enchantment);
}
catch (final Exception exception) {
return null;
}
return Math.max(anvilCost / 2 * level, 1);
}

@EventHandler
private void onPrepareAnvil(final @NotNull PrepareAnvilEvent event) {
final AnvilInventory anvilInventory = event.getInventory();
final AnvilView anvilView = event.getView();
final ItemStack firstItem;
final ItemMeta firstItemMeta;
final Material firstItemType;
Expand Down Expand Up @@ -278,7 +184,7 @@ private void onPrepareAnvil(final @NotNull PrepareAnvilEvent event) {
else
((Repairable)resultMeta).setRepairCost(secondItemRepairCost);
result.setItemMeta(resultMeta);
anvilInventory.setRepairCost(totalEnchantmentCost);
anvilView.setRepairCost(totalEnchantmentCost);
event.setResult(result);
}

Expand Down
3 changes: 1 addition & 2 deletions src/main/resources/paper-plugin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,4 @@ description: A plugin to extract enchantments from your armour into an Enchanted
authors:
- Minacle
website: https://modrinth.com/plugin/enchantedbook
api-version: "1.20"
folia-supported: true
api-version: "1.21.4"

0 comments on commit 6c97ea9

Please sign in to comment.