Skip to content

Commit 85099b8

Browse files
committed
add fullbright command
1 parent 2e4595f commit 85099b8

File tree

3 files changed

+120
-0
lines changed

3 files changed

+120
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.uravgcode.modernessentials.command;
2+
3+
import com.mojang.brigadier.Command;
4+
import com.mojang.brigadier.context.CommandContext;
5+
import com.mojang.brigadier.exceptions.CommandSyntaxException;
6+
import com.mojang.brigadier.tree.LiteralCommandNode;
7+
import com.uravgcode.modernessentials.event.FullBrightEvent;
8+
import io.papermc.paper.command.brigadier.CommandSourceStack;
9+
import io.papermc.paper.command.brigadier.Commands;
10+
11+
@SuppressWarnings("unused")
12+
public final class FullBrightCommand implements CommandBuilder {
13+
14+
@Override
15+
public LiteralCommandNode<CommandSourceStack> build() {
16+
return Commands.literal("fullbright")
17+
.requires(permission("essentials.fullbright"))
18+
.executes(this::execute)
19+
.build();
20+
}
21+
22+
private int execute(CommandContext<CommandSourceStack> context) throws CommandSyntaxException {
23+
final var player = player(context);
24+
player.getServer().getPluginManager().callEvent(new FullBrightEvent(player));
25+
return Command.SINGLE_SUCCESS;
26+
}
27+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.uravgcode.modernessentials.event;
2+
3+
import org.bukkit.entity.Player;
4+
import org.bukkit.event.HandlerList;
5+
import org.bukkit.event.player.PlayerEvent;
6+
import org.jetbrains.annotations.NotNull;
7+
8+
public final class FullBrightEvent extends PlayerEvent {
9+
private static final HandlerList handlers = new HandlerList();
10+
11+
public FullBrightEvent(@NotNull Player player) {
12+
super(player);
13+
}
14+
15+
@Override
16+
public @NotNull HandlerList getHandlers() {
17+
return handlers;
18+
}
19+
20+
@SuppressWarnings("unused")
21+
public static @NotNull HandlerList getHandlerList() {
22+
return handlers;
23+
}
24+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package com.uravgcode.modernessentials.module;
2+
3+
import com.uravgcode.modernessentials.annotation.CommandModule;
4+
import com.uravgcode.modernessentials.event.FullBrightEvent;
5+
import net.kyori.adventure.text.Component;
6+
import net.kyori.adventure.text.format.NamedTextColor;
7+
import org.bukkit.NamespacedKey;
8+
import org.bukkit.entity.Player;
9+
import org.bukkit.event.EventHandler;
10+
import org.bukkit.event.EventPriority;
11+
import org.bukkit.event.player.PlayerChangedWorldEvent;
12+
import org.bukkit.event.player.PlayerJoinEvent;
13+
import org.bukkit.event.player.PlayerRespawnEvent;
14+
import org.bukkit.persistence.PersistentDataType;
15+
import org.bukkit.plugin.java.JavaPlugin;
16+
import org.bukkit.potion.PotionEffect;
17+
import org.bukkit.potion.PotionEffectType;
18+
import org.jetbrains.annotations.NotNull;
19+
20+
@CommandModule(name = "fullbright")
21+
public final class FullBrightModule extends PluginModule {
22+
public final NamespacedKey fullBrightKey;
23+
24+
public FullBrightModule(@NotNull JavaPlugin plugin) {
25+
super(plugin);
26+
fullBrightKey = new NamespacedKey(plugin, "fullbright");
27+
}
28+
29+
@EventHandler(priority = EventPriority.MONITOR)
30+
public void onFullBright(FullBrightEvent event) {
31+
final var player = event.getPlayer();
32+
final var dataContainer = player.getPersistentDataContainer();
33+
34+
if (dataContainer.has(fullBrightKey)) {
35+
dataContainer.remove(fullBrightKey);
36+
player.sendPotionEffectChangeRemove(player, PotionEffectType.NIGHT_VISION);
37+
player.sendMessage(Component.text("Fullbright disabled", NamedTextColor.RED));
38+
} else {
39+
dataContainer.set(fullBrightKey, PersistentDataType.BYTE, (byte) 1);
40+
applyFullBright(event.getPlayer());
41+
player.sendMessage(Component.text("Fullbright enabled", NamedTextColor.GREEN));
42+
}
43+
}
44+
45+
@EventHandler(priority = EventPriority.MONITOR)
46+
public void onPlayerJoin(PlayerJoinEvent event) {
47+
applyFullBright(event.getPlayer());
48+
}
49+
50+
@EventHandler(priority = EventPriority.MONITOR)
51+
public void onPlayerChangedWorld(PlayerChangedWorldEvent event) {
52+
applyFullBright(event.getPlayer());
53+
}
54+
55+
@EventHandler(priority = EventPriority.MONITOR)
56+
public void onPlayerRespawn(PlayerRespawnEvent event) {
57+
applyFullBright(event.getPlayer());
58+
}
59+
60+
private void applyFullBright(@NotNull Player player) {
61+
if (!shouldEnableFullBright(player)) return;
62+
final var effect = new PotionEffect(PotionEffectType.NIGHT_VISION, -1, 0, false, false, false);
63+
player.getScheduler().run(plugin, task -> player.sendPotionEffectChange(player, effect), null);
64+
}
65+
66+
private boolean shouldEnableFullBright(@NotNull Player player) {
67+
return player.getPersistentDataContainer().has(fullBrightKey) && player.hasPermission("essentials.fullbright");
68+
}
69+
}

0 commit comments

Comments
 (0)