about summary refs log tree commit diff
path: root/src/main/java/dev/genbyte
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/dev/genbyte')
-rw-r--r--src/main/java/dev/genbyte/sunfright/Damager.java48
-rw-r--r--src/main/java/dev/genbyte/sunfright/Sunfright.java30
2 files changed, 78 insertions, 0 deletions
diff --git a/src/main/java/dev/genbyte/sunfright/Damager.java b/src/main/java/dev/genbyte/sunfright/Damager.java
new file mode 100644
index 0000000..c592647
--- /dev/null
+++ b/src/main/java/dev/genbyte/sunfright/Damager.java
@@ -0,0 +1,48 @@
+package dev.genbyte.sunfright;
+
+import java.util.Collection;
+
+import org.bukkit.entity.Player;
+import org.bukkit.inventory.ItemStack;
+import org.bukkit.inventory.meta.Damageable;
+import org.bukkit.scheduler.BukkitRunnable;
+
+public class Damager extends BukkitRunnable {
+	private final Sunfright sf;
+
+	public Damager(Sunfright sf) {
+		this.sf = sf;
+	}
+
+	public void run() {
+		Collection<? extends Player> players = sf.getServer().getOnlinePlayers();
+		players.forEach((player) -> {
+			byte skylight = player.getLocation().getBlock().getLightFromSky();
+			if (skylight > 3 && skylight < 13) {
+				new DoDamage(player, (byte) 1).runTask(sf);
+			} else if (skylight >= 13) {
+				new DoDamage(player, (byte) 2).runTask(sf);
+			}
+		});
+	}
+
+	private class DoDamage extends BukkitRunnable {
+		private final Player player;
+		private final byte damage;
+
+		public DoDamage(Player player, byte damage) {
+			this.player = player;
+			this.damage = damage;
+		}
+
+		public void run() {
+			ItemStack helmet = player.getInventory().getHelmet();
+			if (helmet != null && helmet instanceof Damageable) {
+				Damageable damageable = (Damageable) helmet;
+				damageable.setDamage(damageable.getDamage() - damage);
+			} else {
+				player.damage(damage);
+			}
+		}
+	}
+}
\ No newline at end of file
diff --git a/src/main/java/dev/genbyte/sunfright/Sunfright.java b/src/main/java/dev/genbyte/sunfright/Sunfright.java
new file mode 100644
index 0000000..d05774f
--- /dev/null
+++ b/src/main/java/dev/genbyte/sunfright/Sunfright.java
@@ -0,0 +1,30 @@
+package dev.genbyte.sunfright;
+
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
+import org.bukkit.plugin.java.JavaPlugin;
+import org.bukkit.scheduler.BukkitTask;
+
+public class Sunfright extends JavaPlugin {
+    private Logger logger;
+    private BukkitTask damager;
+
+    @Override
+    public void onLoad() {
+        logger = this.getLogger();
+    }
+
+    @Override
+    public void onEnable() {
+        int time = 20;
+        damager = new Damager(this).runTaskTimerAsynchronously(this, time, time);
+        logger.log(Level.INFO, "Damager task started. and will run every " + (time / 20) + " seconds");
+    }
+
+    @Override
+    public void onDisable() {
+        damager.cancel();
+        logger.log(Level.INFO, "Damager task stopped.");
+    }
+}
\ No newline at end of file