Skip to content

Commit 525df4b

Browse files
committed
use the new GLIDER DataComponent
1 parent 86cbdd5 commit 525df4b

File tree

3 files changed

+34
-8
lines changed

3 files changed

+34
-8
lines changed

src/main/java/lol/hyper/toolstats/events/AnvilEvent.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ public void onAnvilEvent(PrepareAnvilEvent event) {
169169
}
170170
return;
171171
}
172-
if (firstSlotMaterial == Material.ELYTRA) {
172+
if (toolStats.itemChecker.canGlide(clone)) {
173173
addToken(event, tokenType, "flight-time", clone);
174174
return;
175175
}

src/main/java/lol/hyper/toolstats/events/PlayerMove.java

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,15 +53,23 @@ public void onMove(PlayerMoveEvent event) {
5353
// player is not flying
5454
if (playerStartFlight.containsKey(player)) {
5555
PlayerInventory inventory = player.getInventory();
56-
ItemStack chest = inventory.getChestplate();
57-
// make sure the player is wearing an elytra
58-
if (chest != null && chest.getType() == Material.ELYTRA) {
59-
long duration = (System.currentTimeMillis() - playerStartFlight.get(player));
60-
ItemMeta newItem = toolStats.itemLore.updateFlightTime(chest, duration);
61-
if (newItem != null) {
62-
inventory.getChestplate().setItemMeta(newItem);
56+
// copy their current armor
57+
ItemStack[] armor = inventory.getArmorContents().clone();
58+
for (ItemStack armorPiece : armor) {
59+
// skip missing slots
60+
if (armorPiece == null) {
61+
continue;
62+
}
63+
// if the armor piece can glide, track the flight time
64+
if (toolStats.itemChecker.canGlide(armorPiece)) {
65+
long duration = (System.currentTimeMillis() - playerStartFlight.get(player));
66+
ItemMeta newMeta = toolStats.itemLore.updateFlightTime(armorPiece, duration);
67+
if (newMeta != null) {
68+
armorPiece.setItemMeta(newMeta);
69+
}
6370
}
6471
}
72+
inventory.setArmorContents(armor);
6573
playerStartFlight.remove(player);
6674
}
6775
}

src/main/java/lol/hyper/toolstats/tools/ItemChecker.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
package lol.hyper.toolstats.tools;
1919

20+
import io.papermc.paper.datacomponent.DataComponentType;
21+
import io.papermc.paper.datacomponent.DataComponentTypes;
2022
import lol.hyper.toolstats.ToolStats;
2123
import org.bukkit.Material;
2224
import org.bukkit.NamespacedKey;
@@ -116,6 +118,22 @@ public boolean isMineTool(Material itemType) {
116118
return mineItems.contains(itemType);
117119
}
118120

121+
/**
122+
* In newer versions of Minecraft, you can make items glide, which works
123+
* like an Elytra.
124+
*
125+
* @param itemStack The item to check.
126+
* @return True/false if the item can glide like an Elytra.
127+
*/
128+
public boolean canGlide(ItemStack itemStack) {
129+
// if it's an elytra, we are good
130+
if (itemStack.getType() == Material.ELYTRA) {
131+
return true;
132+
}
133+
// otherwise if it has the GLIDER data
134+
return itemStack.hasData(DataComponentTypes.GLIDER);
135+
}
136+
119137
/**
120138
* Check a given item for a target token.
121139
*

0 commit comments

Comments
 (0)