diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/util/NightVisionCommand.java b/BauSystem_Main/src/de/steamwar/bausystem/features/util/NightVisionCommand.java index e2cf756b..1b28d0fd 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/util/NightVisionCommand.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/util/NightVisionCommand.java @@ -20,16 +20,22 @@ package de.steamwar.bausystem.features.util; import de.steamwar.bausystem.BauSystem; +import de.steamwar.bausystem.configplayer.Config; import de.steamwar.bausystem.linkage.LinkageType; import de.steamwar.bausystem.linkage.Linked; import de.steamwar.command.SWCommand; import net.md_5.bungee.api.ChatMessageType; import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.player.PlayerJoinEvent; import org.bukkit.potion.PotionEffect; import org.bukkit.potion.PotionEffectType; +import yapion.hierarchy.types.YAPIONObject; @Linked(LinkageType.COMMAND) -public class NightVisionCommand extends SWCommand { +@Linked(LinkageType.LISTENER) +public class NightVisionCommand extends SWCommand implements Listener { public NightVisionCommand() { super("nightvision", "nv", "light"); @@ -37,12 +43,24 @@ public class NightVisionCommand extends SWCommand { @Register(description = "NIGHT_VISION_HELP") public void genericCommand(Player p) { - if (p.hasPotionEffect(PotionEffectType.NIGHT_VISION)) { + YAPIONObject yapionObject = Config.getInstance().get(p); + boolean value = !yapionObject.getBooleanOrDefault("nightvision", false); + yapionObject.put("nightvision", value); + setNightVision(p, value); + } + + @EventHandler + public void onPlayerJoin(PlayerJoinEvent event) { + setNightVision(event.getPlayer(), Config.getInstance().get(event.getPlayer()).getBooleanOrDefault("nightvision", false)); + } + + private void setNightVision(Player p, boolean value) { + if (p.hasPotionEffect(PotionEffectType.NIGHT_VISION) && !value) { p.removePotionEffect(PotionEffectType.NIGHT_VISION); BauSystem.MESSAGE.sendPrefixless("NIGHT_VISION_OFF", p, ChatMessageType.ACTION_BAR); - return; + } else if (!p.hasPotionEffect(PotionEffectType.NIGHT_VISION) && value) { + p.addPotionEffect(new PotionEffect(PotionEffectType.NIGHT_VISION, 1000000, 255, false, false)); + BauSystem.MESSAGE.sendPrefixless("NIGHT_VISION_ON", p, ChatMessageType.ACTION_BAR); } - p.addPotionEffect(new PotionEffect(PotionEffectType.NIGHT_VISION, 1000000, 255, false, false)); - BauSystem.MESSAGE.sendPrefixless("NIGHT_VISION_ON", p, ChatMessageType.ACTION_BAR); } } \ No newline at end of file