Skip to content

Commit fe94616

Browse files
authored
Switch to Paper (#86)
Some API breakages - Most areas now avoid Material where possible, preferring ItemType or NamespacedKeys.
1 parent ab20f13 commit fe94616

39 files changed

Lines changed: 1604 additions & 1299 deletions

.github/workflows/auto-merge-dependabot.yml

Lines changed: 0 additions & 15 deletions
This file was deleted.

.github/workflows/pull_request.yml

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,30 @@ on:
66
jobs:
77
run-ci:
88
uses: Jikoo/PlanarActions/.github/workflows/ci_maven.yml@master
9-
store-dependabot-pr-data:
10-
if: "github.actor == 'dependabot[bot]' && github.event_name == 'pull_request'"
11-
uses: Jikoo/PlanarActions/.github/workflows/pr_automerge_prep.yml@master
12-
with:
13-
pr-number: ${{ github.event.number }}
9+
approve-and-merge-dependabot:
10+
if: "github.event_name == 'pull_request' && github.event.pull_request.user.login == 'dependabot[bot]'"
11+
needs: [ "run-ci" ]
12+
runs-on: "ubuntu-latest"
13+
steps:
14+
# Always approve PRs from Dependabot.
15+
- name: Approve
16+
run: gh pr review --approve "$PR_URL"
17+
env:
18+
PR_URL: ${{github.event.pull_request.html_url}}
19+
GH_TOKEN: ${{secrets.GITHUB_TOKEN}}
20+
21+
# Fetch Dependabot metadata for finer decisionmaking later.
22+
- name: Fetch Dependabot metadata
23+
id: metadata
24+
uses: dependabot/fetch-metadata@21025c705c08248db411dc16f3619e6b5f9ea21a
25+
with:
26+
github-token: "${{ secrets.GITHUB_TOKEN }}"
27+
28+
# Enable auto-merge for the PR.
29+
# Auto-merge is used rather than a direct merge so that any other required checks can pass.
30+
- name: Enable auto-merge for minor/patch updates
31+
if: steps.metadata.outputs.update-type == 'version-update:semver-patch' || steps.metadata.outputs.update-type == 'version-update:semver-minor'
32+
run: gh pr merge --auto --squash "$PR_URL"
33+
env:
34+
PR_URL: ${{github.event.pull_request.html_url}}
35+
GH_TOKEN: ${{secrets.GITHUB_TOKEN}}

pom.xml

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@
4444

4545
<repositories>
4646
<repository>
47-
<id>spigot-repo</id>
48-
<url>https://hub.spigotmc.org/nexus/content/groups/public/</url>
47+
<id>papermc</id>
48+
<url>https://repo.papermc.io/repository/maven-public/</url>
4949
</repository>
5050
<repository>
5151
<id>jitpack.io</id>
@@ -62,9 +62,9 @@
6262
<scope>provided</scope>
6363
</dependency>
6464
<dependency>
65-
<groupId>org.spigotmc</groupId>
66-
<artifactId>spigot-api</artifactId>
67-
<version>1.21.4-R0.1-SNAPSHOT</version>
65+
<groupId>io.papermc.paper</groupId>
66+
<artifactId>paper-api</artifactId>
67+
<version>1.21.11-R0.1-SNAPSHOT</version>
6868
<scope>provided</scope>
6969
</dependency>
7070
<dependency>
@@ -107,8 +107,8 @@
107107
<artifactId>annotations</artifactId>
108108
</dependency>
109109
<dependency>
110-
<groupId>org.spigotmc</groupId>
111-
<artifactId>spigot-api</artifactId>
110+
<groupId>io.papermc.paper</groupId>
111+
<artifactId>paper-api</artifactId>
112112
</dependency>
113113
<dependency>
114114
<groupId>com.github.jikoo</groupId>

src/main/java/com/github/jikoo/planarenchanting/anvil/Anvil.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ protected final AnvilResult forge(@NotNull AnvilState state) {
126126
}
127127
// Ignore name changes if addition is not empty.
128128
if (!ItemUtil.isEmpty(state.getAddition().getItem())) {
129-
resultMeta.setDisplayName(baseMeta.getDisplayName());
129+
resultMeta.customName(baseMeta.customName());
130130
}
131131

132132
// If reset meta is identical then no operation is occurring.

src/main/java/com/github/jikoo/planarenchanting/anvil/AnvilBehavior.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package com.github.jikoo.planarenchanting.anvil;
22

33
import com.github.jikoo.planarenchanting.util.MetaCachedStack;
4-
import org.bukkit.Material;
54
import org.bukkit.enchantments.Enchantment;
65
import org.bukkit.inventory.ItemStack;
6+
import org.bukkit.inventory.ItemType;
77
import org.jetbrains.annotations.NotNull;
88

99
public interface AnvilBehavior {
@@ -47,8 +47,8 @@ default int getEnchantMaxLevel(@NotNull Enchantment enchantment) {
4747
* @return whether items should combine {@code Enchantments}
4848
*/
4949
default boolean itemsCombineEnchants(@NotNull MetaCachedStack base, @NotNull MetaCachedStack addition) {
50-
Material additionType = addition.getItem().getType();
51-
return base.getItem().getType() == additionType || additionType == Material.ENCHANTED_BOOK;
50+
ItemType additionType = addition.getItem().getType().asItemType();
51+
return base.getItem().getType().asItemType() == additionType || additionType == ItemType.ENCHANTED_BOOK;
5252
}
5353

5454
/**

src/main/java/com/github/jikoo/planarenchanting/anvil/AnvilFunctions.java

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
package com.github.jikoo.planarenchanting.anvil;
22

3-
import com.github.jikoo.planarenchanting.enchant.EnchantData;
43
import com.github.jikoo.planarenchanting.util.ItemUtil;
54
import com.github.jikoo.planarenchanting.util.MetaCachedStack;
6-
import org.bukkit.Material;
5+
import io.papermc.paper.registry.keys.ItemTypeKeys;
6+
import net.kyori.adventure.text.Component;
7+
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
78
import org.bukkit.enchantments.Enchantment;
9+
import org.bukkit.inventory.ItemType;
810
import org.bukkit.inventory.meta.Damageable;
911
import org.bukkit.inventory.meta.ItemMeta;
1012
import org.bukkit.inventory.meta.Repairable;
1113
import org.jetbrains.annotations.NotNull;
1214
import org.jetbrains.annotations.Nullable;
13-
import java.util.Objects;
1415

1516
public enum AnvilFunctions {
1617
; // Empty enum to hold constants.
@@ -42,9 +43,17 @@ public int getLevelCostIncrease() {
4243
public boolean canApply(@NotNull AnvilBehavior behavior, @NotNull AnvilState state) {
4344
var itemMeta = state.getBase().getMeta();
4445

46+
if (itemMeta == null) {
47+
return false;
48+
}
49+
4550
// If names are not the same, can be applied.
46-
return itemMeta != null
47-
&& !Objects.equals(itemMeta.getDisplayName(), state.getAnvil().getRenameText());
51+
Component customName = itemMeta.customName();
52+
String anvilText = state.getAnvil().getRenameText();
53+
if (customName == null) {
54+
return anvilText != null;
55+
}
56+
return !LegacyComponentSerializer.legacySection().serialize(customName).equals(anvilText);
4857
}
4958

5059
@Override
@@ -64,7 +73,8 @@ public void modifyResult(@Nullable ItemMeta itemMeta) {
6473
return;
6574
}
6675

67-
itemMeta.setDisplayName(state.getAnvil().getRenameText());
76+
String anvilText = state.getAnvil().getRenameText();
77+
itemMeta.customName(anvilText == null ? null : Component.text(anvilText));
6878
if (itemMeta instanceof Repairable repairable) {
6979
int repairCost = Math.max(
7080
ItemUtil.getRepairCost(state.getBase().getMeta()),
@@ -172,8 +182,9 @@ public void modifyResult(@Nullable ItemMeta itemMeta) {
172182
public static final AnvilFunction REPAIR_WITH_COMBINATION = new AnvilFunction() {
173183
@Override
174184
public boolean canApply(@NotNull AnvilBehavior behavior, @NotNull AnvilState state) {
175-
Material baseType = state.getBase().getItem().getType();
176-
return baseType == state.getAddition().getItem().getType()
185+
ItemType baseType = state.getBase().getItem().getType().asItemType();
186+
return baseType != null
187+
&& baseType == state.getAddition().getItem().getType().asItemType()
177188
&& baseType.getMaxDurability() > 0
178189
&& state.getBase().getMeta() instanceof Damageable damageable
179190
&& damageable.getDamage() > 0;
@@ -236,12 +247,11 @@ protected int getNonApplicableCost() {
236247
public static final AnvilFunction COMBINE_ENCHANTMENTS_BEDROCK_EDITION = new CombineEnchantments() {
237248
@Override
238249
protected int getAnvilCost(Enchantment enchantment, boolean isFromBook) {
239-
EnchantData enchantData = EnchantData.of(enchantment);
240-
if (!enchantData.getSecondaryItems().isTagged(Material.TRIDENT)) {
250+
if (!enchantment.getSupportedItems().contains(ItemTypeKeys.TRIDENT)) {
241251
return super.getAnvilCost(enchantment, isFromBook);
242252
}
243253

244-
int base = enchantData.getAnvilCost();
254+
int base = enchantment.getAnvilCost();
245255

246256
// Bedrock Edition rarity is 1 tier lower for trident enchantments.
247257
return Math.max(1, isFromBook ? base / 4 : base / 2);

src/main/java/com/github/jikoo/planarenchanting/anvil/CombineEnchantments.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
package com.github.jikoo.planarenchanting.anvil;
22

3-
import com.github.jikoo.planarenchanting.enchant.EnchantData;
43
import com.github.jikoo.planarenchanting.enchant.EnchantmentUtil;
54
import java.util.HashMap;
65
import java.util.Map;
76
import java.util.Map.Entry;
8-
import org.bukkit.Material;
97
import org.bukkit.enchantments.Enchantment;
8+
import org.bukkit.inventory.ItemType;
109
import org.bukkit.inventory.meta.ItemMeta;
1110
import org.jetbrains.annotations.NotNull;
1211
import org.jetbrains.annotations.Nullable;
@@ -32,7 +31,7 @@ public boolean canApply(@NotNull AnvilBehavior behavior, @NotNull AnvilState sta
3231
}
3332

3433
Map<Enchantment, Integer> newEnchants = new HashMap<>(baseEnchants);
35-
boolean isFromBook = state.getAddition().getItem().getType() == Material.ENCHANTED_BOOK;
34+
boolean isFromBook = state.getAddition().getItem().getType().asItemType() == ItemType.ENCHANTED_BOOK;
3635

3736
int levelCost = 0;
3837
for (Entry<Enchantment, Integer> enchantEntry : additionEnchants.entrySet()) {
@@ -77,7 +76,7 @@ public void modifyResult(@Nullable ItemMeta itemMeta) {
7776
}
7877

7978
protected int getAnvilCost(Enchantment enchantment, boolean isFromBook) {
80-
int value = EnchantData.of(enchantment).getAnvilCost();
79+
int value = enchantment.getAnvilCost();
8180
return isFromBook ? Math.max(1, value / 2) : value;
8281
}
8382

Lines changed: 56 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
package com.github.jikoo.planarenchanting.anvil;
22

3+
import io.papermc.paper.registry.RegistryAccess;
4+
import io.papermc.paper.registry.RegistryKey;
5+
import io.papermc.paper.registry.TypedKey;
6+
import io.papermc.paper.registry.keys.tags.ItemTypeTagKeys;
7+
import io.papermc.paper.registry.tag.Tag;
8+
import io.papermc.paper.registry.tag.TagKey;
39
import java.util.HashMap;
4-
import java.util.List;
510
import java.util.Map;
6-
import org.bukkit.Material;
7-
import org.bukkit.Tag;
11+
import java.util.function.Predicate;
12+
import org.bukkit.NamespacedKey;
813
import org.bukkit.inventory.ItemStack;
9-
import org.bukkit.inventory.RecipeChoice;
10-
import org.bukkit.inventory.RecipeChoice.MaterialChoice;
14+
import org.bukkit.inventory.ItemType;
1115
import org.jetbrains.annotations.NotNull;
12-
import org.jetbrains.annotations.VisibleForTesting;
1316

1417
/**
1518
* Definitions of materials used in anvil repair operations.
@@ -26,69 +29,69 @@ public final class RepairMaterial {
2629
* @return whether the addition material can repair the base material
2730
*/
2831
public static boolean repairs(@NotNull ItemStack base, @NotNull ItemStack addition) {
29-
RecipeChoice recipeChoice = MATERIALS_TO_REPAIRABLE.get(base.getType());
30-
return recipeChoice != null && recipeChoice.test(addition);
32+
Predicate<NamespacedKey> predicate = MATERIALS_TO_REPAIRABLE.get(base.getType().getKey());
33+
return predicate != null && predicate.test(addition.getType().getKey());
3134
}
3235

33-
private static final Map<Material, RecipeChoice> MATERIALS_TO_REPAIRABLE = new HashMap<>();
36+
private static final Map<NamespacedKey, Predicate<NamespacedKey>> MATERIALS_TO_REPAIRABLE = new HashMap<>();
3437

3538
static {
36-
String[] armor = new String[] { "_HELMET", "_CHESTPLATE", "_LEGGINGS", "_BOOTS" };
37-
String[] tools = new String[] { "_AXE", "_SHOVEL", "_PICKAXE", "_HOE", "_SWORD" };
38-
String[] armortools = new String[armor.length + tools.length];
39-
System.arraycopy(armor, 0, armortools, 0, armor.length);
40-
System.arraycopy(tools, 0, armortools, armor.length, tools.length);
41-
42-
// Leather armor
43-
addGear("LEATHER", armor, Material.LEATHER);
44-
45-
// Stone tools
46-
addGear("STONE", tools, new RecipeChoice.MaterialChoice(Tag.ITEMS_STONE_TOOL_MATERIALS));
47-
48-
// Wooden tools, shields
49-
MaterialChoice choicePlanks = new MaterialChoice(Tag.PLANKS);
50-
addGear("WOODEN", tools, choicePlanks);
51-
MATERIALS_TO_REPAIRABLE.put(Material.SHIELD, choicePlanks);
52-
53-
// Chainmail, iron armor and tools
54-
RecipeChoice choiceIronIngot = singleChoice(Material.IRON_INGOT);
55-
addGear("CHAINMAIL", armor, choiceIronIngot);
56-
addGear("IRON", armortools, choiceIronIngot);
57-
58-
// Gold, diamond, and netherite armor and tools
59-
addGear("GOLDEN", armortools, Material.GOLD_INGOT);
60-
addGear("DIAMOND", armortools, Material.DIAMOND);
61-
addGear("NETHERITE", armortools, Material.NETHERITE_INGOT);
39+
// Note that for all choices, we want to use a MaterialChoice.
40+
// ExactChoice also does a full meta match.
41+
42+
// See net.minecraft.world.item.equipment.ArmorMaterials
43+
String[] armor = new String[] { "_helmet", "_chestplate", "_leggings", "_boots" };
44+
addGear("leather", armor, ItemTypeTagKeys.REPAIRS_LEATHER_ARMOR);
45+
addGear("copper", armor, ItemTypeTagKeys.REPAIRS_COPPER_ARMOR);
46+
addGear("chainmail", armor, ItemTypeTagKeys.REPAIRS_CHAIN_ARMOR);
47+
addGear("iron", armor, ItemTypeTagKeys.REPAIRS_IRON_ARMOR);
48+
addGear("golden", armor, ItemTypeTagKeys.REPAIRS_GOLD_ARMOR);
49+
addGear("diamond", armor, ItemTypeTagKeys.REPAIRS_DIAMOND_ARMOR);
50+
MATERIALS_TO_REPAIRABLE.put(ItemType.TURTLE_HELMET.getKey(), new TagPredicate(ItemTypeTagKeys.REPAIRS_TURTLE_HELMET));
51+
addGear("netherite", armor, ItemTypeTagKeys.REPAIRS_NETHERITE_ARMOR);
52+
MATERIALS_TO_REPAIRABLE.put(ItemType.WOLF_ARMOR.getKey(), new TagPredicate(ItemTypeTagKeys.REPAIRS_WOLF_ARMOR));
53+
54+
// See net.minecraft.world.item.ToolMaterial
55+
String[] tools = new String[] { "_axe", "_shovel", "_pickaxe", "_hoe", "_sword", "_spear" };
56+
addGear("stone", tools, ItemTypeTagKeys.STONE_TOOL_MATERIALS);
57+
Predicate<NamespacedKey> woodToolMats = new TagPredicate(ItemTypeTagKeys.WOODEN_TOOL_MATERIALS);
58+
addGear("wooden", tools, woodToolMats);
59+
MATERIALS_TO_REPAIRABLE.put(ItemType.SHIELD.getKey(), woodToolMats);
60+
addGear("iron", tools, ItemTypeTagKeys.IRON_TOOL_MATERIALS);
61+
addGear("golden", tools, ItemTypeTagKeys.GOLD_TOOL_MATERIALS);
62+
addGear("diamond", tools, ItemTypeTagKeys.DIAMOND_TOOL_MATERIALS);
63+
addGear("netherite", tools, ItemTypeTagKeys.NETHERITE_TOOL_MATERIALS);
6264

6365
// Misc. repairable items
64-
MATERIALS_TO_REPAIRABLE.put(Material.TURTLE_HELMET, singleChoice(Material.TURTLE_SCUTE));
65-
MATERIALS_TO_REPAIRABLE.put(Material.ELYTRA, singleChoice(Material.PHANTOM_MEMBRANE));
66-
MATERIALS_TO_REPAIRABLE.put(Material.MACE, singleChoice(Material.BREEZE_ROD));
66+
MATERIALS_TO_REPAIRABLE.put(ItemType.ELYTRA.getKey(), key -> ItemType.PHANTOM_MEMBRANE.getKey().equals(key));
67+
MATERIALS_TO_REPAIRABLE.put(ItemType.MACE.getKey(), key -> ItemType.BREEZE_ROD.getKey().equals(key));
6768
}
6869

69-
private static void addGear(String type, String[] gearType, RecipeChoice repairChoice) {
70+
private static void addGear(String type, String[] gearType, Predicate<NamespacedKey> repairChoice) {
7071
for (String toolType : gearType) {
71-
Material material = Material.getMaterial(type + toolType);
72-
if (material != null) {
73-
MATERIALS_TO_REPAIRABLE.put(material, repairChoice);
74-
}
72+
MATERIALS_TO_REPAIRABLE.put(NamespacedKey.minecraft(type + toolType), repairChoice);
7573
}
7674
}
7775

78-
private static void addGear(String type, String[] gearType, Material repairMaterial) {
79-
addGear(type, gearType, singleChoice(repairMaterial));
76+
private static void addGear(String type, String[] gearType, TagKey<ItemType> tag) {
77+
addGear(type, gearType, new TagPredicate(tag));
8078
}
8179

82-
private static RecipeChoice singleChoice(Material material) {
83-
// RecipeChoice.ExactChoice is a full meta match, which isn't what we want.
84-
return new RecipeChoice.MaterialChoice(List.of(material));
80+
private RepairMaterial() {
81+
throw new IllegalStateException("Cannot instantiate static helper method container.");
8582
}
8683

87-
@VisibleForTesting
88-
static boolean hasEntry(@NotNull Material material) {
89-
return MATERIALS_TO_REPAIRABLE.containsKey(material);
90-
}
84+
private record TagPredicate(Tag<ItemType> tag) implements Predicate<NamespacedKey> {
9185

92-
private RepairMaterial() {}
86+
private TagPredicate(TagKey<ItemType> tag) {
87+
this(RegistryAccess.registryAccess().getRegistry(RegistryKey.ITEM).getTag(tag));
88+
}
89+
90+
@Override
91+
public boolean test(NamespacedKey key) {
92+
return tag.contains(TypedKey.create(RegistryKey.ITEM, key));
93+
}
94+
95+
}
9396

9497
}

0 commit comments

Comments
 (0)