Skip to content

Commit 7e6ff55

Browse files
committed
(func) command to change guidance preference
1 parent 21f6637 commit 7e6ff55

File tree

7 files changed

+93
-17
lines changed

7 files changed

+93
-17
lines changed

pom.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,10 @@
7070
<id>enginehub-maven</id>
7171
<url>https://maven.enginehub.org/repo/</url>
7272
</repository>
73+
<repository>
74+
<id>codemc</id>
75+
<url>https://repo.codemc.org/repository/maven-public/</url>
76+
</repository>
7377
</repositories>
7478

7579
<dependencies>
@@ -96,6 +100,12 @@
96100
<version>7.2.12-SNAPSHOT</version>
97101
<scope>provided</scope>
98102
</dependency>
103+
<dependency>
104+
<groupId>dev.jorel</groupId>
105+
<artifactId>commandapi-bukkit-core</artifactId>
106+
<version>9.5.2</version>
107+
<scope>provided</scope>
108+
</dependency>
99109
<dependency>
100110
<groupId>org.xerial</groupId>
101111
<artifactId>sqlite-jdbc</artifactId>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package cat.nyaa.nfs;
2+
3+
public enum GuidanceLevel {
4+
ON, PARTIAL, OFF
5+
}

src/main/java/cat/nyaa/nfs/GuidanceService.java

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,25 @@
11
package cat.nyaa.nfs;
22

33
import cat.nyaa.nfs.dataclasses.Point;
4-
import org.bukkit.Bukkit;
5-
import org.bukkit.Location;
6-
import org.bukkit.Particle;
7-
import org.bukkit.Vibration;
4+
import org.bukkit.*;
85
import org.bukkit.entity.EntityType;
96
import org.bukkit.entity.Marker;
7+
import org.bukkit.entity.Player;
108
import org.bukkit.event.EventHandler;
119
import org.bukkit.event.Listener;
1210
import org.bukkit.event.player.PlayerMoveEvent;
11+
import org.bukkit.persistence.PersistentDataType;
1312

1413
import java.util.HashMap;
1514
import java.util.Map;
15+
import java.util.Objects;
1616
import java.util.UUID;
1717

1818
import static java.lang.Math.min;
1919

2020
public class GuidanceService implements Listener {
2121
private final Map<UUID, Marker> guidanceMap = new HashMap<>();
22+
public static final NamespacedKey guidancePreferenceKey = new NamespacedKey(NeedForSpeed.instance, "guidance");
2223

2324
public void updateGuidance(UUID playerUniqueID, Point location, String world) {
2425
removeGuidance(playerUniqueID);
@@ -36,24 +37,39 @@ public void removeGuidance(UUID playerUniqueID) {
3637
}
3738
}
3839

40+
public void updatePlayerPreference(Player player, GuidanceLevel guidanceLevel) {
41+
player.getPersistentDataContainer().set(guidancePreferenceKey, PersistentDataType.INTEGER, guidanceLevel.ordinal());
42+
}
43+
44+
public GuidanceLevel getGuidanceLevel(Player player) {
45+
if (!player.getPersistentDataContainer().has(guidancePreferenceKey))
46+
return GuidanceLevel.ON;
47+
var ordinal = player.getPersistentDataContainer().get(guidancePreferenceKey, PersistentDataType.INTEGER);
48+
return GuidanceLevel.values()[Objects.requireNonNullElseGet(ordinal, GuidanceLevel.ON::ordinal)];
49+
50+
}
51+
3952
@EventHandler
4053
public void onPlayerMove(PlayerMoveEvent event) {
54+
var guidanceLevel = getGuidanceLevel(event.getPlayer());
55+
if (guidanceLevel == GuidanceLevel.OFF) return;
4156
var player = event.getPlayer();
4257
if (!guidanceMap.containsKey(player.getUniqueId())) return;
4358
var guidanceMarker = guidanceMap.get(event.getPlayer().getUniqueId());
4459

4560
player.spawnParticle(Particle.HAPPY_VILLAGER, guidanceMarker.getLocation(), 10);
4661

4762
if (!player.isGliding()) return;
63+
if (guidanceLevel != GuidanceLevel.ON) return;
4864
var playerLocation = player.getLocation();
4965
var playerSpeed = player.getVelocity().length();
5066
var playerDistance = playerLocation.distance(guidanceMarker.getLocation());
5167

52-
if (playerSpeed > 0.15 && playerDistance > 5) {
53-
var locInFront = playerLocation.clone().add(playerLocation.getDirection().multiply(5));
68+
if (playerSpeed > 0.15 && playerDistance > 8) {
69+
var locInFront = playerLocation.clone().add(playerLocation.getDirection().multiply(8));
5470
var vibrationSpeed = min(playerDistance / playerSpeed / 1.5, playerDistance / 2);
5571
var data = new Vibration(locInFront, new Vibration.Destination.BlockDestination(guidanceMarker.getLocation().getBlock()), (int) (vibrationSpeed));
56-
player.spawnParticle(Particle.VIBRATION, locInFront, 1, data);
72+
player.spawnParticle(Particle.VIBRATION, locInFront, 3, data);
5773
}
5874
}
5975

src/main/java/cat/nyaa/nfs/Language.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,8 @@ public class Language {
4040
public Text nameUsed = Text.of("&7name &c{name}&7 is used, please change another one.");
4141
public Text failedToLoadPersonalBestOn = Text.of("&cFailed to load personal best on {objective}&c, new record notification disabled");
4242
public Text failedToLogRecord = Text.of("&6Failed to log record, but you can still make a screenshot!");
43+
public Text guidanceEnabled = Text.of("&8Flying guidance level changed to: {level}");
44+
public Text on = Text.of("ON");
45+
public Text off = Text.of("OFF");
46+
public Text partial = Text.of("PARTIAL ON");
4347
}

src/main/java/cat/nyaa/nfs/NeedForSpeed.java

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,14 @@
22

33
import cat.nyaa.nfs.command.NFSCommand;
44
import cat.nyaa.nfs.save.Recorder;
5+
import dev.jorel.commandapi.CommandAPI;
6+
import dev.jorel.commandapi.CommandAPICommand;
7+
import dev.jorel.commandapi.arguments.ArgumentSuggestions;
8+
import dev.jorel.commandapi.arguments.StringArgument;
59
import land.melon.lab.simplelanguageloader.SimpleLanguageLoader;
10+
import land.melon.lab.simplelanguageloader.utils.Pair;
611
import org.bukkit.event.HandlerList;
12+
import org.bukkit.persistence.PersistentDataType;
713
import org.bukkit.plugin.java.JavaPlugin;
814

915
import java.io.File;
@@ -41,10 +47,8 @@ public void onEnable() {
4147
}
4248

4349
private void reload() throws IOException, SQLException {
44-
4550
onDisable();
4651

47-
4852
var languageFile = new File(dataFolder, "language.json");
4953
language = simpleLanguageLoader.loadOrInitialize(languageFile, Language.class, Language::new);
5054

@@ -57,6 +61,7 @@ private void reload() throws IOException, SQLException {
5761
getServer().getPluginManager().registerEvents(guidanceService, this);
5862

5963
getCommand("nfs").setExecutor(new NFSCommand(this));
64+
registerCommands();
6065
}
6166

6267
public Recorder getRecorder() {
@@ -75,6 +80,37 @@ public void onDisable() {
7580
throw new RuntimeException(e);
7681
}
7782
if (timerListenersManager != null) timerListenersManager.shutdown();
83+
unregisterCommands();
7884
HandlerList.unregisterAll(this);
7985
}
86+
87+
private void registerCommands() {
88+
new CommandAPICommand("guidance").withArguments(new StringArgument("status").replaceSuggestions(ArgumentSuggestions.strings("on", "partial", "off"))).executesPlayer((player, args) -> {
89+
var arg = (String) args.get("status");
90+
if (arg == null) return;
91+
var pdc = player.getPersistentDataContainer();
92+
GuidanceLevel guidanceLevel;
93+
String statusText;
94+
switch (arg) {
95+
case "partial" -> {
96+
guidanceLevel = GuidanceLevel.PARTIAL;
97+
statusText = language.partial.produce();
98+
}
99+
case "off" -> {
100+
guidanceLevel = GuidanceLevel.OFF;
101+
statusText = language.off.produce();
102+
}
103+
default -> {
104+
guidanceLevel = GuidanceLevel.ON;
105+
statusText = language.on.produce();
106+
}
107+
}
108+
player.sendMessage(language.guidanceEnabled.produce(Pair.of("level", statusText)));
109+
pdc.set(GuidanceService.guidancePreferenceKey, PersistentDataType.INTEGER, guidanceLevel.ordinal());
110+
}).register(this);
111+
}
112+
113+
private void unregisterCommands() {
114+
CommandAPI.unregister("guidance");
115+
}
80116
}

src/main/java/cat/nyaa/nfs/TimerListenerInstance.java

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -166,22 +166,26 @@ private boolean isBestPlay(Player player, long timeUsedInMilliseconds) {
166166

167167
private void pushRecordAsync(PlayerRecord record) {
168168
NeedForSpeed.instance.getServer().getScheduler().runTaskAsynchronously(NeedForSpeed.instance, () -> {
169-
try {
170-
recorder.record(record);
171-
} catch (SQLException e) {
172-
NeedForSpeed.instance.getLogger().severe("Failed to log record");
173-
NeedForSpeed.instance.getLogger().severe(record.toString());
174-
throw new RuntimeException(e);
175-
}
169+
pushRecordSync(record);
176170
});
177171
}
178172

173+
private void pushRecordSync(PlayerRecord record) {
174+
try {
175+
recorder.record(record);
176+
} catch (SQLException e) {
177+
NeedForSpeed.instance.getLogger().severe("Failed to log record");
178+
NeedForSpeed.instance.getLogger().severe(record.toString());
179+
throw new RuntimeException(e);
180+
}
181+
}
182+
179183
public void shutdown() {
180184
HandlerList.unregisterAll(this);
181185
Bukkit.getServer().getOnlinePlayers().forEach(player -> {
182186
if (isPlayingThisObjective(player)) {
183187
var record = recordThenReset(player, RecordBy.SERVER_SHUTDOWN);
184-
pushRecordAsync(record);
188+
pushRecordSync(record);
185189
}
186190
});
187191
}

src/main/resources/plugin.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ api-version: 1.18
55
authors: [ langua ]
66
depend:
77
- WorldEdit
8+
- CommandAPI
89
libraries:
910
- org.xerial:sqlite-jdbc:3.46.1.0
1011
commands:

0 commit comments

Comments
 (0)