Skip to content

Commit b9b440c

Browse files
committed
feat: folia support
1 parent af074e4 commit b9b440c

File tree

9 files changed

+151
-75
lines changed

9 files changed

+151
-75
lines changed

build.gradle

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ plugins {
44
}
55

66
group = 'com.artillexstudios.axsmithing'
7-
version = '1.2'
7+
version = '1.3'
88

99
repositories {
1010
mavenCentral()
@@ -20,15 +20,15 @@ repositories {
2020
maven {
2121
url = uri('https://repo.viaversion.com')
2222
}
23+
24+
maven {
25+
url = uri('https://repo.artillex-studios.com/releases')
26+
}
2327
}
2428

2529
dependencies {
2630
implementation 'net.byteflux:libby-bukkit:1.2.0'
27-
implementation 'dev.dejvokep:boosted-yaml:1.3'
28-
implementation 'net.kyori:adventure-text-minimessage:4.14.0'
29-
implementation 'net.kyori:adventure-text-serializer-legacy:4.14.0'
30-
implementation 'net.kyori:adventure-platform-bukkit:4.3.0'
31-
implementation 'net.kyori:adventure-api:4.14.0'
31+
implementation 'com.artillexstudios.axapi:axapi:1.3.9'
3232
implementation 'org.bstats:bstats-bukkit:3.0.2'
3333
compileOnly 'com.google.code.gson:gson:2.10.1'
3434
compileOnly 'org.spigotmc:spigot-api:1.20.1-R0.1-SNAPSHOT'
@@ -48,8 +48,8 @@ processResources {
4848

4949
shadowJar {
5050
relocate("net.byteflux.libby", "com.artillexstudios.axsmithing.libs.libby")
51-
relocate("net.kyori", "com.artillexstudios.axsmithing.libs.kyori")
5251
relocate("org.bstats", "com.artillexstudios.axsmithing.libs.bstats")
52+
relocate("com.artillexstudios.axapi", "com.artillexstudios.axsmithing.libs.axapi")
5353
minimize()
5454
}
5555

src/main/java/com/artillexstudios/axsmithing/AxSmithingPlugin.java

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,28 @@
11
package com.artillexstudios.axsmithing;
22

3+
import com.artillexstudios.axapi.AxPlugin;
4+
import com.artillexstudios.axapi.libs.boostedyaml.boostedyaml.YamlDocument;
5+
import com.artillexstudios.axapi.libs.boostedyaml.boostedyaml.dvs.versioning.BasicVersioning;
6+
import com.artillexstudios.axapi.libs.boostedyaml.boostedyaml.settings.dumper.DumperSettings;
7+
import com.artillexstudios.axapi.libs.boostedyaml.boostedyaml.settings.general.GeneralSettings;
8+
import com.artillexstudios.axapi.libs.boostedyaml.boostedyaml.settings.loader.LoaderSettings;
9+
import com.artillexstudios.axapi.libs.boostedyaml.boostedyaml.settings.updater.UpdaterSettings;
10+
import com.artillexstudios.axapi.utils.Version;
311
import com.artillexstudios.axsmithing.command.AxSmithingCommand;
412
import com.artillexstudios.axsmithing.command.AxSmithingTabComplete;
513
import com.artillexstudios.axsmithing.gui.SmithingTable;
614
import com.artillexstudios.axsmithing.gui.impl.SmithingTable_V1_16;
715
import com.artillexstudios.axsmithing.gui.impl.SmithingTable_V1_20;
816
import com.artillexstudios.axsmithing.listener.InteractListener;
917
import com.artillexstudios.axsmithing.listener.InventoryListener;
10-
import dev.dejvokep.boostedyaml.YamlDocument;
11-
import dev.dejvokep.boostedyaml.dvs.versioning.BasicVersioning;
12-
import dev.dejvokep.boostedyaml.settings.dumper.DumperSettings;
13-
import dev.dejvokep.boostedyaml.settings.general.GeneralSettings;
14-
import dev.dejvokep.boostedyaml.settings.loader.LoaderSettings;
15-
import dev.dejvokep.boostedyaml.settings.updater.UpdaterSettings;
1618
import org.bstats.bukkit.Metrics;
1719
import org.bukkit.Bukkit;
18-
import org.bukkit.plugin.java.JavaPlugin;
1920
import org.jetbrains.annotations.NotNull;
2021

2122
import java.io.File;
2223
import java.io.IOException;
2324

24-
public class AxSmithingPlugin extends JavaPlugin {
25+
public class AxSmithingPlugin extends AxPlugin {
2526
private static YamlDocument config;
2627
private static SmithingTable smithingTableImpl;
2728
private static AxSmithingPlugin instance;
@@ -52,7 +53,7 @@ public static boolean is1_20() {
5253
// }
5354

5455
@Override
55-
public void onEnable() {
56+
public void enable() {
5657
instance = this;
5758
new Metrics(this, 19575);
5859
try {
@@ -68,23 +69,18 @@ public void onEnable() {
6869
Bukkit.getPluginCommand("axsmithing").setTabCompleter(new AxSmithingTabComplete());
6970
}
7071

71-
public String reload() {
72-
long now = System.currentTimeMillis();
72+
public void reload() {
7373
try {
7474
config.reload();
7575
} catch (IOException e) {
7676
throw new RuntimeException(e);
7777
}
7878

7979
initializeSmithingTableImpl();
80-
long took = System.currentTimeMillis() - now;
81-
return String.valueOf(took);
8280
}
8381

8482
private void initializeSmithingTableImpl() {
85-
String version = Bukkit.getVersion();
86-
87-
if (version.contains("20")) {
83+
if (Version.getServerVersion().isNewerThanOrEqualTo(Version.v1_20_1)) {
8884
v1_20 = true;
8985
smithingTableImpl = new SmithingTable_V1_20();
9086
} else {

src/main/java/com/artillexstudios/axsmithing/command/AxSmithingCommand.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command
1616
return true;
1717
}
1818

19-
String took = AxSmithingPlugin.getInstance().reload();
19+
String took = String.valueOf(AxSmithingPlugin.getInstance().reloadWithTime());
2020
sender.sendMessage(StringUtils.format(AxSmithingPlugin.getConfiguration().getString("messages.prefix") + AxSmithingPlugin.getConfiguration().getString("messages.reload").replace("%time%", took)));
2121
return true;
2222
}

src/main/java/com/artillexstudios/axsmithing/gui/impl/SmithingTable_V1_16.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
package com.artillexstudios.axsmithing.gui.impl;
22

3+
import com.artillexstudios.axapi.libs.boostedyaml.boostedyaml.YamlDocument;
4+
import com.artillexstudios.axapi.libs.boostedyaml.boostedyaml.block.implementation.Section;
5+
import com.artillexstudios.axapi.scheduler.Scheduler;
36
import com.artillexstudios.axsmithing.AxSmithingPlugin;
47
import com.artillexstudios.axsmithing.gui.SmithingTable;
58
import com.artillexstudios.axsmithing.utils.ItemBuilder;
69
import com.artillexstudios.axsmithing.utils.StringUtils;
7-
import dev.dejvokep.boostedyaml.YamlDocument;
8-
import dev.dejvokep.boostedyaml.block.implementation.Section;
910
import org.bukkit.Bukkit;
1011
import org.bukkit.Material;
1112
import org.bukkit.entity.Player;
@@ -159,7 +160,7 @@ public Inventory getInventory() {
159160
}
160161

161162
private void updateGui(Inventory inv) {
162-
Bukkit.getScheduler().runTaskLater(AxSmithingPlugin.getInstance(), t -> {
163+
Scheduler.get().runLater(t -> {
163164
ItemStack base = inv.getItem(itemSlot);
164165
ItemStack addition = inv.getItem(upgradeSlot);
165166
if (addition == null || addition.getType().isAir()) {
@@ -178,7 +179,7 @@ private void updateGui(Inventory inv) {
178179
if (!successful) {
179180
checkRecipe(inv, finalAddition, finalBase);
180181
}
181-
}, 0L);
182+
}, 1L);
182183
}
183184

184185
private boolean checkRecipe(Inventory inventory, ItemStack finalBase, ItemStack finalAddition) {

src/main/java/com/artillexstudios/axsmithing/gui/impl/SmithingTable_V1_20.java

Lines changed: 110 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
package com.artillexstudios.axsmithing.gui.impl;
22

3+
import com.artillexstudios.axapi.libs.boostedyaml.boostedyaml.YamlDocument;
4+
import com.artillexstudios.axapi.libs.boostedyaml.boostedyaml.block.implementation.Section;
5+
import com.artillexstudios.axapi.scheduler.Scheduler;
36
import com.artillexstudios.axsmithing.AxSmithingPlugin;
47
import com.artillexstudios.axsmithing.gui.SmithingTable;
58
import com.artillexstudios.axsmithing.utils.ItemBuilder;
69
import com.artillexstudios.axsmithing.utils.StringUtils;
7-
import dev.dejvokep.boostedyaml.YamlDocument;
8-
import dev.dejvokep.boostedyaml.block.implementation.Section;
910
import org.bukkit.Bukkit;
1011
import org.bukkit.Material;
1112
import org.bukkit.entity.Player;
@@ -16,6 +17,8 @@
1617
import org.bukkit.inventory.InventoryHolder;
1718
import org.bukkit.inventory.ItemStack;
1819
import org.bukkit.inventory.Recipe;
20+
import org.bukkit.inventory.RecipeChoice;
21+
import org.bukkit.inventory.SmithingRecipe;
1922
import org.bukkit.inventory.SmithingTransformRecipe;
2023
import org.bukkit.inventory.SmithingTrimRecipe;
2124
import org.bukkit.inventory.meta.ArmorMeta;
@@ -26,11 +29,13 @@
2629
import org.jetbrains.annotations.NotNull;
2730
import org.jetbrains.annotations.Nullable;
2831

32+
import java.lang.reflect.Field;
2933
import java.util.ArrayList;
3034
import java.util.Iterator;
3135
import java.util.List;
3236
import java.util.Map;
3337
import java.util.Set;
38+
import java.util.stream.IntStream;
3439

3540
public class SmithingTable_V1_20 implements SmithingTable, InventoryHolder {
3641
private final int outputSlot;
@@ -39,6 +44,10 @@ public class SmithingTable_V1_20 implements SmithingTable, InventoryHolder {
3944
private final int templateSlot;
4045
private final boolean needNetheriteTemplate;
4146
private final boolean dontConvertWithModelData;
47+
private final Field templateField;
48+
private final Field baseField;
49+
private final Field additionField;
50+
private final Field resultField;
4251

4352
public SmithingTable_V1_20() {
4453
YamlDocument config = AxSmithingPlugin.getConfiguration();
@@ -48,6 +57,33 @@ public SmithingTable_V1_20() {
4857
itemSlot = config.getInt("menu.1_20.item-slot");
4958
needNetheriteTemplate = config.getBoolean("menu.1_20.need-netherite-template");
5059
dontConvertWithModelData = config.getBoolean("menu.1_20.dont-convert-with-modeldata");
60+
try {
61+
templateField = SmithingTransformRecipe.class.getDeclaredField("template");
62+
templateField.setAccessible(true);
63+
} catch (NoSuchFieldException e) {
64+
throw new RuntimeException(e);
65+
}
66+
67+
try {
68+
baseField = SmithingRecipe.class.getDeclaredField("base");
69+
baseField.setAccessible(true);
70+
} catch (NoSuchFieldException e) {
71+
throw new RuntimeException(e);
72+
}
73+
74+
try {
75+
additionField = SmithingRecipe.class.getDeclaredField("addition");
76+
additionField.setAccessible(true);
77+
} catch (NoSuchFieldException e) {
78+
throw new RuntimeException(e);
79+
}
80+
81+
try {
82+
resultField = SmithingRecipe.class.getDeclaredField("result");
83+
resultField.setAccessible(true);
84+
} catch (NoSuchFieldException e) {
85+
throw new RuntimeException(e);
86+
}
5187
}
5288

5389
@Override
@@ -188,7 +224,7 @@ public Inventory getInventory() {
188224
}
189225

190226
private void updateGui(Inventory inv) {
191-
Bukkit.getScheduler().runTaskLater(AxSmithingPlugin.getInstance(), t -> {
227+
Scheduler.get().runLater(t -> {
192228
ItemStack template = inv.getItem(templateSlot);
193229
ItemStack base = inv.getItem(itemSlot);
194230
ItemStack addition = inv.getItem(upgradeSlot);
@@ -230,7 +266,7 @@ private void updateGui(Inventory inv) {
230266
checkRecipe(inv, finalAddition, finalTemplate, finalBase);
231267
}
232268

233-
}, 0L);
269+
}, 1L);
234270
}
235271

236272
private boolean checkRecipe(Inventory inventory, ItemStack finalTemplate, ItemStack finalBase, ItemStack finalAddition) {
@@ -262,18 +298,28 @@ private boolean checkRecipe(Inventory inventory, ItemStack finalTemplate, ItemSt
262298
}
263299

264300
if (recipe instanceof SmithingTransformRecipe transformRecipe) {
265-
boolean test1 = transformRecipe.getTemplate().test(finalTemplate);
266-
boolean test2 = transformRecipe.getBase().test(finalBase);
301+
RecipeChoice template = getTemplate(transformRecipe);
302+
RecipeChoice base = getBase(transformRecipe);
303+
RecipeChoice addition = getAddition(transformRecipe);
304+
if (template == null || base == null || addition == null) {
305+
return false;
306+
}
307+
308+
boolean test1 = template.test(finalTemplate);
309+
boolean test2 = base.test(finalBase);
267310
ItemMeta baseItemMeta = finalBase.getItemMeta();
268311
if (baseItemMeta == null) return false;
269-
boolean test3 = transformRecipe.getAddition().test(finalAddition);
312+
boolean test3 = addition.test(finalAddition);
270313

271314
if (dontConvertWithModelData && baseItemMeta.hasCustomModelData()) {
272315
return false;
273316
}
274317

275318
if (test1 && test2 && test3) {
276-
ItemStack item = transformRecipe.getResult();
319+
ItemStack item = getResult(transformRecipe);
320+
if (item == null) {
321+
return false;
322+
}
277323
item.setItemMeta(baseItemMeta);
278324
inventory.setItem(outputSlot, item);
279325
return true;
@@ -286,6 +332,62 @@ private boolean checkRecipe(Inventory inventory, ItemStack finalTemplate, ItemSt
286332
return false;
287333
}
288334

335+
public RecipeChoice getTemplate(SmithingTransformRecipe recipe) {
336+
RecipeChoice recipeChoice;
337+
try {
338+
recipeChoice = (RecipeChoice) templateField.get(recipe);
339+
if (recipeChoice == null) {
340+
return null;
341+
}
342+
} catch (IllegalAccessException e) {
343+
throw new RuntimeException(e);
344+
}
345+
346+
return recipeChoice.clone();
347+
}
348+
349+
public RecipeChoice getBase(SmithingTransformRecipe recipe) {
350+
RecipeChoice recipeChoice;
351+
try {
352+
recipeChoice = (RecipeChoice) baseField.get(recipe);
353+
if (recipeChoice == null) {
354+
return null;
355+
}
356+
} catch (IllegalAccessException e) {
357+
throw new RuntimeException(e);
358+
}
359+
360+
return recipeChoice.clone();
361+
}
362+
363+
public RecipeChoice getAddition(SmithingTransformRecipe recipe) {
364+
RecipeChoice recipeChoice;
365+
try {
366+
recipeChoice = (RecipeChoice) additionField.get(recipe);
367+
if (recipeChoice == null) {
368+
return null;
369+
}
370+
} catch (IllegalAccessException e) {
371+
throw new RuntimeException(e);
372+
}
373+
374+
return recipeChoice.clone();
375+
}
376+
377+
public ItemStack getResult(SmithingTransformRecipe recipe) {
378+
ItemStack result;
379+
try {
380+
result = (ItemStack) resultField.get(recipe);
381+
if (result == null) {
382+
return null;
383+
}
384+
} catch (IllegalAccessException e) {
385+
throw new RuntimeException(e);
386+
}
387+
388+
return result.clone();
389+
}
390+
289391
@Nullable
290392
private ArmorTrim armorTrim(@NotNull ItemStack finalAddition, @NotNull ItemStack finalTemplate) {
291393
TrimMaterial material;

src/main/java/com/artillexstudios/axsmithing/listener/InteractListener.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
package com.artillexstudios.axsmithing.listener;
22

3+
import com.artillexstudios.axapi.scheduler.Scheduler;
4+
import com.artillexstudios.axapi.utils.Version;
35
import com.artillexstudios.axsmithing.AxSmithingPlugin;
46
import com.viaversion.viaversion.api.Via;
57
import com.viaversion.viaversion.api.protocol.version.ProtocolVersion;
6-
import org.bukkit.Bukkit;
78
import org.bukkit.Material;
89
import org.bukkit.entity.Player;
910
import org.bukkit.event.Event;
@@ -24,11 +25,11 @@ public void onPlayerInteractEvent(@NotNull final PlayerInteractEvent event) {
2425
if (event.getAction().equals(Action.LEFT_CLICK_BLOCK)) return;
2526

2627
if (AxSmithingPlugin.is1_20()) {
27-
if (Via.getAPI().getPlayerVersion(event.getPlayer()) >= ProtocolVersion.v1_20.getVersion() && !AxSmithingPlugin.getConfiguration().getBoolean("menu.1_20.force-for-1_20-clients")) {
28+
if (Via.getAPI().getPlayerVersion(event.getPlayer()) >= Version.v1_20_1.protocolId && !AxSmithingPlugin.getConfiguration().getBoolean("menu.1_20.force-for-1_20-clients")) {
2829
return;
2930
}
3031
} else {
31-
if (Via.getAPI().getPlayerVersion(event.getPlayer()) != ProtocolVersion.v1_20.getVersion()) {
32+
if (Via.getAPI().getPlayerVersion(event.getPlayer()) != Version.v1_20_1.protocolId && Via.getAPI().getPlayerVersion(event.getPlayer()) != Version.v1_20_2.protocolId) {
3233
return;
3334
}
3435
}
@@ -45,16 +46,18 @@ public void onInventoryOpenEvent(@NotNull final InventoryOpenEvent event) {
4546
if (event.getInventory().getType() != InventoryType.SMITHING) return;
4647

4748
if (AxSmithingPlugin.is1_20()) {
48-
if (Via.getAPI().getPlayerVersion(event.getPlayer()) >= ProtocolVersion.v1_20.getVersion() && !AxSmithingPlugin.getConfiguration().getBoolean("menu.1_20.force-for-1_20-clients")) {
49+
if (Via.getAPI().getPlayerVersion(event.getPlayer()) >= Version.v1_20_1.protocolId && !AxSmithingPlugin.getConfiguration().getBoolean("menu.1_20.force-for-1_20-clients")) {
4950
return;
5051
}
5152
} else {
52-
if (Via.getAPI().getPlayerVersion(event.getPlayer()) != ProtocolVersion.v1_20.getVersion()) {
53+
if (Via.getAPI().getPlayerVersion(event.getPlayer()) != Version.v1_20_1.protocolId && Via.getAPI().getPlayerVersion(event.getPlayer()) != Version.v1_20_2.protocolId) {
5354
return;
5455
}
5556
}
5657

5758
event.getPlayer().closeInventory();
58-
Bukkit.getScheduler().runTaskLater(AxSmithingPlugin.getInstance(), () -> AxSmithingPlugin.getSmithingTableImpl().open((Player) event.getPlayer()),1L);
59+
Scheduler.get().runLater(t -> {
60+
AxSmithingPlugin.getSmithingTableImpl().open((Player) event.getPlayer());
61+
}, 1);
5962
}
6063
}

0 commit comments

Comments
 (0)