SBDevelopment
Search…
Examples

VehiclesPlus:

Example 1: Broadcast warning if player is driving too fast

This code will broadcast a waning to everyone with the permission myplugin.staff if someone drives more than 80 km/h. There is a cooldown of 5 seconds, so that there is no spam.
1
private final WeakHashMap<UUID, LocalDateTime> cooldown = new WeakHashMap<>();
2
3
@EventHandler
4
public void onSendKey(SendKeyEvent e) {
5
/* STEP 1: Get the driver */
6
Player driver = e.getDriver();
7
8
/* STEP 2: Convert Entity to SpawnedVehicle */
9
Entity seatArmorstand = e.getRiding();
10
if (!seatArmorstand.hasMetadata("PART_DATA")) return; //It's not a part? Skip for now.
11
Part part = (Part) seatArmorstand.getMetadata(NBTDataType.PART_DATA.name()).get(0).value(); //Convert to a Part
12
if (part == null) return; //The part is null? Skip for now.
13
SpawnedVehicle v = part.getOwningVehicle();
14
15
/* STEP 3: Check speed */
16
if (v.getCurrentSpeedInKm() > 80) {
17
/* STEP 4: Check if driver has no cooldown, and send speedwarning */
18
if (!hasCooldown(driver)) Bukkit.broadcast(ChatColor.GOLD + driver.getName() + ChatColor.WHITE + " drives too fast! Current speed: " + ChatColor.GOLD + v.getCurrentSpeedInKm(), "myplugin.staff");
19
}
20
}
21
22
private boolean hasCooldown(Player driver) {
23
boolean hasCooldown = false;
24
if (cooldown.containsKey(driver.getUniqueId())) {
25
LocalDateTime past = LocalDateTime.now();
26
LocalDateTime now = cooldown.get(driver.getUniqueId());
27
int seconds = (int) ChronoUnit.SECONDS.between(now, past);
28
if (seconds > 5) {
29
cooldown.remove(driver.getUniqueId());
30
}
31
} else {
32
cooldown.put(driver.getUniqueId(), LocalDateTime.now());
33
hasCooldown = true;
34
}
35
36
return hasCooldown;
37
}
Copied!
Last modified 2mo ago