Skip to content

Commit fb0d179

Browse files
committed
add flight time (#62)
1 parent e8ab578 commit fb0d179

File tree

6 files changed

+135
-12
lines changed

6 files changed

+135
-12
lines changed

src/main/java/lol/hyper/toolstats/ToolStats.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,10 @@ public final class ToolStats extends JavaPlugin {
9797
* Key for arrows shot.
9898
*/
9999
public final NamespacedKey arrowsShot = new NamespacedKey(this, "arrows-shot");
100+
/**
101+
* Key for tracking flight time.
102+
*/
103+
public final NamespacedKey flightTime = new NamespacedKey(this, "flightTime");
100104
/**
101105
* Stores how an item was created.
102106
* 0 = crafted.
@@ -134,6 +138,7 @@ public final class ToolStats extends JavaPlugin {
134138
public MorePaperLib morePaperLib;
135139
public HashMaker hashMaker;
136140
public CreativeEvent creativeEvent;
141+
public PlayerMove playerMove;
137142
public ItemChecker itemChecker;
138143
public ShootBow shootBow;
139144
public ConfigTools configTools;
@@ -164,6 +169,7 @@ public void onEnable() {
164169
inventoryOpen = new InventoryOpen(this);
165170
playerJoin = new PlayerJoin(this);
166171
creativeEvent = new CreativeEvent(this);
172+
playerMove = new PlayerMove(this);
167173
itemChecker = new ItemChecker();
168174
shootBow = new ShootBow(this);
169175
configTools = new ConfigTools(this);
@@ -183,6 +189,7 @@ public void onEnable() {
183189
Bukkit.getServer().getPluginManager().registerEvents(playerJoin, this);
184190
Bukkit.getServer().getPluginManager().registerEvents(creativeEvent, this);
185191
Bukkit.getServer().getPluginManager().registerEvents(shootBow, this);
192+
Bukkit.getServer().getPluginManager().registerEvents(playerMove, this);
186193

187194
this.getCommand("toolstats").setExecutor(commandToolStats);
188195

src/main/java/lol/hyper/toolstats/commands/CommandToolStats.java

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -144,20 +144,30 @@ private void fixItemLore(ItemStack original, Player player) {
144144

145145
// hard code elytras
146146
if (finalItem.getType() == Material.ELYTRA) {
147-
if (toolStats.config.getBoolean("enabled.elytra-tag")) {
148-
lore.add(toolStats.configTools.getLoreFromConfig("looted.found-by", true).replace("{player}", player.getName()));
149-
if (container.has(toolStats.timeCreated, PersistentDataType.LONG)) {
150-
Long time = container.get(toolStats.timeCreated, PersistentDataType.LONG);
151-
if (time != null) {
152-
lore.add(toolStats.configTools.getLoreFromConfig("looted.found-on", true).replace("{date}", toolStats.numberFormat.formatDate(new Date(time))));
153-
}
147+
Long flightTime = null;
148+
Long timeCreated = null;
149+
if (container.has(toolStats.timeCreated, PersistentDataType.LONG)) {
150+
timeCreated = container.get(toolStats.timeCreated, PersistentDataType.LONG);
151+
}
152+
if (container.has(toolStats.flightTime, PersistentDataType.LONG)) {
153+
flightTime = container.get(toolStats.flightTime, PersistentDataType.LONG);
154+
}
155+
156+
if (flightTime != null) {
157+
if (toolStats.config.getBoolean("enabled.flight-time")) {
158+
lore.add(toolStats.configTools.getLoreFromConfig("flight-time", true).replace("{time}", toolStats.numberFormat.formatDouble((double) flightTime / 1000)));
154159
}
155-
finalMeta.setLore(lore);
156-
finalItem.setItemMeta(finalMeta);
157-
int slot = player.getInventory().getHeldItemSlot();
158-
player.getInventory().setItem(slot, finalItem);
159-
return;
160160
}
161+
162+
if (timeCreated != null) {
163+
lore.add(toolStats.configTools.getLoreFromConfig("looted.found-by", true).replace("{player}", player.getName()));
164+
lore.add(toolStats.configTools.getLoreFromConfig("looted.found-on", true).replace("{date}", toolStats.numberFormat.formatDate(new Date(timeCreated))));
165+
}
166+
167+
finalMeta.setLore(lore);
168+
finalItem.setItemMeta(finalMeta);
169+
int slot = player.getInventory().getHeldItemSlot();
170+
player.getInventory().setItem(slot, finalItem);
161171
}
162172

163173
if (toolStats.configTools.checkConfig(original.getType(), "created-by")) {
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
/*
2+
* This file is part of ToolStats.
3+
*
4+
* ToolStats is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation, either version 3 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* ToolStats is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with ToolStats. If not, see <https://www.gnu.org/licenses/>.
16+
*/
17+
18+
package lol.hyper.toolstats.events;
19+
20+
import lol.hyper.toolstats.ToolStats;
21+
import org.bukkit.Material;
22+
import org.bukkit.entity.Player;
23+
import org.bukkit.event.EventHandler;
24+
import org.bukkit.event.EventPriority;
25+
import org.bukkit.event.Listener;
26+
import org.bukkit.event.player.PlayerMoveEvent;
27+
import org.bukkit.inventory.ItemStack;
28+
import org.bukkit.inventory.meta.ItemMeta;
29+
import org.bukkit.persistence.PersistentDataContainer;
30+
import org.bukkit.persistence.PersistentDataType;
31+
32+
import java.util.HashMap;
33+
import java.util.List;
34+
import java.util.Map;
35+
36+
public class PlayerMove implements Listener {
37+
38+
private final ToolStats toolStats;
39+
private final Map<Player, Long> playerStartFlight = new HashMap<>();
40+
41+
public PlayerMove(ToolStats toolStats) {
42+
this.toolStats = toolStats;
43+
}
44+
45+
@EventHandler(priority = EventPriority.MONITOR)
46+
public void onCraft(PlayerMoveEvent event) {
47+
Player player = event.getPlayer();
48+
// player starts to fly
49+
if (player.isGliding()) {
50+
// if they are flying, and we don't have them tracked, add them
51+
if (!playerStartFlight.containsKey(player)) {
52+
playerStartFlight.put(player, System.currentTimeMillis());
53+
}
54+
} else {
55+
// player is not flying
56+
if (playerStartFlight.containsKey(player)) {
57+
trackFlight(player, playerStartFlight.get(player));
58+
playerStartFlight.remove(player);
59+
}
60+
}
61+
}
62+
63+
private void trackFlight(Player player, long startTime) {
64+
ItemStack chest = player.getInventory().getChestplate();
65+
// make sure their chest piece is an elytra
66+
if (chest == null || chest.getType() != Material.ELYTRA) {
67+
return;
68+
}
69+
ItemMeta meta = chest.getItemMeta();
70+
if (meta == null) {
71+
toolStats.logger.warning(chest + " does NOT have any meta! Unable to update stats.");
72+
return;
73+
}
74+
75+
// read the current stats from the item
76+
// if they don't exist, then start from 0
77+
Long flightTime = 0L;
78+
PersistentDataContainer container = meta.getPersistentDataContainer();
79+
if (container.has(toolStats.flightTime, PersistentDataType.LONG)) {
80+
flightTime = container.get(toolStats.flightTime, PersistentDataType.LONG);
81+
}
82+
83+
if (flightTime == null) {
84+
flightTime = 0L;
85+
toolStats.logger.warning(flightTime + " does not have valid flight-time set! Resting to zero. This should NEVER happen.");
86+
}
87+
88+
// get the duration of the flight
89+
flightTime = (System.currentTimeMillis() - startTime) + flightTime;
90+
container.set(toolStats.flightTime, PersistentDataType.LONG, flightTime);
91+
92+
// do we add the lore based on the config?
93+
if (toolStats.config.getBoolean("enabled.flight-time")) {
94+
String flightTimeFormatted = toolStats.numberFormat.formatDouble((double) flightTime / 1000);
95+
List<String> newLore = toolStats.itemLore.addItemLore(meta, "{time}", flightTimeFormatted, "flight-time");
96+
meta.setLore(newLore);
97+
}
98+
chest.setItemMeta(meta);
99+
}
100+
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ public ItemChecker() {
5555
validItems.add(Material.BOW);
5656
validItems.add(Material.FISHING_ROD);
5757
validItems.add(Material.CROSSBOW);
58+
validItems.add(Material.ELYTRA);
5859

5960
// combine the lists
6061
validItems.addAll(armorItems);

src/main/java/lol/hyper/toolstats/tools/config/ConfigTools.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,9 @@ public String getLoreFromConfig(String configName, boolean raw) {
171171
if (lore.contains("{arrows}")) {
172172
lore = lore.replace("{arrows}", "");
173173
}
174+
if (lore.contains("{time}")) {
175+
lore = lore.replace("{time}", "");
176+
}
174177
}
175178
return lore;
176179
}

src/main/resources/config.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ enabled:
8181
dropped-by: true
8282
elytra-tag: true
8383
arrows-shot: true
84+
flight-time: true
8485

8586
messages:
8687
created:
@@ -110,6 +111,7 @@ messages:
110111
dropped-by: "&7Dropped by: &8{name}" # name will be player/mob name
111112
damage-taken: "&7Damage taken: &8{damage}"
112113
arrows-shot: "&7Arrows shot: &8{arrows}"
114+
flight-time: "&7Flight time: &8{time}"
113115
# Display this message if the player shift click trades/crafts items. It's not really easy to get every single item
114116
# that is crafted. The tag will only be added to the first item. If you don't want this message, simply replace them both with ""
115117
shift-click-warning:

0 commit comments

Comments
 (0)