|
| 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