2
2
3
3
import com .comphenix .protocol .PacketType ;
4
4
import com .comphenix .protocol .events .PacketContainer ;
5
- import com .willfp .eco .core .proxy .ProxyConstants ;
6
- import com .willfp .eco .util .protocollib . AbstractPacketAdapter ;
5
+ import com .willfp .eco .core .proxy .proxies . VillagerTradeProxy ;
6
+ import com .willfp .eco .util .ProxyUtils ;
7
7
import com .willfp .eco .util .plugin .AbstractEcoPlugin ;
8
- import com .willfp .ecoenchants . display . EnchantDisplay ;
8
+ import com .willfp .eco . util . protocollib . AbstractPacketAdapter ;
9
9
import com .willfp .ecoenchants .enchantments .meta .EnchantmentTarget ;
10
- import org .bukkit .inventory .ItemStack ;
11
10
import org .bukkit .inventory .MerchantRecipe ;
12
11
import org .jetbrains .annotations .NotNull ;
13
12
14
- import java .lang .reflect .Field ;
15
- import java .lang .reflect .InvocationTargetException ;
16
- import java .lang .reflect .Modifier ;
17
13
import java .util .List ;
18
14
import java .util .stream .Collectors ;
19
15
@@ -32,46 +28,11 @@ public void onSend(@NotNull final PacketContainer packet) {
32
28
List <MerchantRecipe > recipes = packet .getMerchantRecipeLists ().readSafely (0 );
33
29
34
30
recipes = recipes .stream ().peek (merchantRecipe -> {
35
- try {
36
- if (!EnchantmentTarget .ALL .getMaterials ().contains (merchantRecipe .getResult ().getType ())) {
37
- return ;
38
- }
39
-
40
- // Enables removing final modifier
41
- Field modifiersField = Field .class .getDeclaredField ("modifiers" );
42
- modifiersField .setAccessible (true );
43
-
44
- // Bukkit MerchantRecipe result
45
- Field fResult = merchantRecipe .getClass ().getSuperclass ().getDeclaredField ("result" );
46
- fResult .setAccessible (true );
47
- ItemStack result = EnchantDisplay .displayEnchantments (merchantRecipe .getResult ());
48
- result = EnchantDisplay .addV (result );
49
- fResult .set (merchantRecipe , result );
50
-
51
- // Get NMS MerchantRecipe from CraftMerchantRecipe
52
- Field fHandle = merchantRecipe .getClass ().getDeclaredField ("handle" );
53
- fHandle .setAccessible (true );
54
- Object handle = fHandle .get (merchantRecipe ); // NMS Recipe
55
- modifiersField .setInt (fHandle , fHandle .getModifiers () & ~Modifier .FINAL ); // Remove final
56
-
57
- // NMS MerchantRecipe
58
- Field fSelling = fHandle .get (merchantRecipe ).getClass ().getDeclaredField ("sellingItem" );
59
- fSelling .setAccessible (true );
60
- Object selling = fSelling .get (handle ); // NMS Selling ItemStack
61
- modifiersField .setInt (fSelling , fSelling .getModifiers () & ~Modifier .FINAL );
62
-
63
- // Reflectively access CraftItemStack.class for respective version
64
- Class <?> craftItemStack = Class .forName ("org.bukkit.craftbukkit." + ProxyConstants .NMS_VERSION + ".inventory.CraftItemStack" );
65
-
66
- // Bukkit Result ItemStack from NMS Result ItemStack
67
- ItemStack nmsSelling = (ItemStack ) craftItemStack .getMethod ("asBukkitCopy" , selling .getClass ()).invoke (null , selling );
68
- nmsSelling = EnchantDisplay .displayEnchantments (nmsSelling );
69
- nmsSelling = EnchantDisplay .addV (nmsSelling );
70
- fSelling .set (handle , craftItemStack .getMethod ("asNMSCopy" , ItemStack .class ).invoke (null , nmsSelling ));
71
-
72
- } catch (IllegalAccessException | NoSuchFieldException | ClassNotFoundException | NoSuchMethodException | InvocationTargetException e ) {
73
- e .printStackTrace ();
31
+ if (!EnchantmentTarget .ALL .getMaterials ().contains (merchantRecipe .getResult ().getType ())) {
32
+ return ;
74
33
}
34
+
35
+ ProxyUtils .getProxy (VillagerTradeProxy .class ).displayTradeEnchantments (merchantRecipe );
75
36
}).collect (Collectors .toList ());
76
37
77
38
packet .getMerchantRecipeLists ().writeSafely (0 , recipes );
0 commit comments