about summary refs log tree commit diff
path: root/src/main/java
diff options
context:
space:
mode:
authorGenny <gen@nyble.dev>2020-06-02 17:34:20 -0500
committerGenny <gen@nyble.dev>2020-06-02 17:34:20 -0500
commit5ee4c899940675d28a4494aef9748e01f949c608 (patch)
treec557bc8327b6acc9856f19360f7418a7bfdd4e9f /src/main/java
parent9afff6f780bdde6f6259b30a4844fdd6d271db57 (diff)
downloadsunfright-5ee4c899940675d28a4494aef9748e01f949c608.tar.gz
sunfright-5ee4c899940675d28a4494aef9748e01f949c608.zip
Change damage conditions
Diffstat (limited to 'src/main/java')
-rw-r--r--src/main/java/dev/genbyte/sunfright/Damager.java51
-rw-r--r--src/main/java/dev/genbyte/sunfright/Sunfright.java38
-rw-r--r--src/main/java/dev/genbyte/sunfright/events/HelmetHandler.java12
3 files changed, 76 insertions, 25 deletions
diff --git a/src/main/java/dev/genbyte/sunfright/Damager.java b/src/main/java/dev/genbyte/sunfright/Damager.java
index cdee7c1..28420d4 100644
--- a/src/main/java/dev/genbyte/sunfright/Damager.java
+++ b/src/main/java/dev/genbyte/sunfright/Damager.java
@@ -1,10 +1,9 @@
 package dev.genbyte.sunfright;
 
 import java.util.Collection;
-import java.util.logging.Level;
 
 import org.bukkit.Material;
-import org.bukkit.block.Block;
+import org.bukkit.World;
 import org.bukkit.enchantments.Enchantment;
 import org.bukkit.entity.Player;
 import org.bukkit.inventory.ItemStack;
@@ -20,13 +19,14 @@ public class Damager extends BukkitRunnable {
 	}
 
 	public void run() {
-		Collection<? extends Player> players = sf.getServer().getOnlinePlayers();
+		World sunnedWorld = sf.sunnedWorld;
+		Collection<? extends Player> players = sunnedWorld.getPlayers();
 
 		players.forEach((player) -> {
 			byte skylight = player.getLocation().getBlock().getLightFromSky();
 
-			if (skylight > 3) {
-				new DoDamage(player, (byte) 1).runTask(sf);
+			if (skylight > 3 && timeShouldDamage()) {
+				new DoDamage(player, sf.damagaPerSecond).runTask(sf);
 			} else if (player.getInventory().getHelmet() != null
 					&& player.getInventory().getHelmet().getEnchantmentLevel(Enchantment.VANISHING_CURSE) == 2) {
 				player.getInventory().setHelmet(new ItemStack(Material.AIR));
@@ -34,24 +34,43 @@ public class Damager extends BukkitRunnable {
 		});
 	}
 
+	private boolean timeShouldDamage() {
+		World sunnedWorld = sf.sunnedWorld;
+
+		long time = sunnedWorld.getTime();
+		boolean storming = sunnedWorld.hasStorm();
+		boolean thundering = sunnedWorld.isThundering();
+
+		// Times are pulled from Minecraft Gamepedia page on Light, specifically the internal light
+		// level section. https://minecraft.gamepedia.com/Light
+		// Times correspond to when the light level is over 8.
+		if (!storming && !thundering) {
+			if (time >= 13027 && time <= 22973) {
+				return false;
+			}
+		} else if (storming && !thundering) {
+			if (time >= 12734 && time <= 23266) {
+				return false;
+			}
+		} else if (storming && thundering) {
+			if (time >= 12300 && time <= 23700) {
+				return false;
+			}
+		}
+		return true;
+	}
+
 	private class DoDamage extends BukkitRunnable {
 		private final Player player;
-		private final byte damage;
+		private final int damage;
 
-		public DoDamage(Player player, byte damage) {
+		public DoDamage(Player player, int damage) {
 			this.player = player;
 			this.damage = damage;
 		}
 
 		public void run() {
 			ItemStack helmet = player.getInventory().getHelmet();
-			Block topBlock = player.getWorld().getBlockAt(
-					player.getWorld().getHighestBlockAt(player.getLocation()).getLocation().subtract(0, 1, 0));
-
-			if (topBlock.getLocation().getY() > player.getLocation().getY()
-					&& topBlock.getType().equals(Material.BLACK_STAINED_GLASS)) {
-				return;
-			}
 
 			if (helmet != null) {
 				ItemMeta helmetMeta = helmet.getItemMeta();
@@ -80,7 +99,7 @@ public class Damager extends BukkitRunnable {
 
 						player.getInventory().setHelmet(new ItemStack(Material.AIR));
 					} else {
-						helmetDamageable.setDamage(helmetDamage + damage);
+						helmetDamageable.setDamage(helmetDamage + (damage/2));
 						helmet.setItemMeta((ItemMeta) helmetDamageable);
 					}
 				}
@@ -90,7 +109,7 @@ public class Damager extends BukkitRunnable {
 		}
 
 		private void applyDamage() {
-			player.damage(damage * 2);
+			player.damage(damage);
 		}
 	}
 }
diff --git a/src/main/java/dev/genbyte/sunfright/Sunfright.java b/src/main/java/dev/genbyte/sunfright/Sunfright.java
index 87ab46b..051ac13 100644
--- a/src/main/java/dev/genbyte/sunfright/Sunfright.java
+++ b/src/main/java/dev/genbyte/sunfright/Sunfright.java
@@ -3,32 +3,52 @@ package dev.genbyte.sunfright;
 import java.util.logging.Level;
 import java.util.logging.Logger;
 
+import org.bukkit.World;
 import org.bukkit.plugin.java.JavaPlugin;
 import org.bukkit.scheduler.BukkitTask;
 
 import dev.genbyte.sunfright.events.HelmetHandler;
 
 public class Sunfright extends JavaPlugin {
-    private Logger logger;
+    public World sunnedWorld;
+    public int damagaPerSecond;
     private BukkitTask damager;
 
     @Override
-    public void onLoad() {
-        logger = this.getLogger();
-    }
-
-    @Override
     public void onEnable() {
+        if (!loadConfig()) {
+            getLogger().log(Level.SEVERE, "Could not load config file, aborting load. Plugin not active!");
+            getPluginLoader().disablePlugin(this);
+            return;
+        }
+
         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");
 
-        this.getServer().getPluginManager().registerEvents(new HelmetHandler(), this);
+        this.getServer().getPluginManager().registerEvents(new HelmetHandler(this), this);
     }
 
     @Override
     public void onDisable() {
         damager.cancel();
-        logger.log(Level.INFO, "Damager task stopped.");
+    }
+
+    private boolean loadConfig() {
+        this.saveDefaultConfig();
+
+        String world = getConfig().getString("world");
+        damagaPerSecond = getConfig().getInt("damagePerSecond");
+
+        sunnedWorld = getServer().getWorld(world);
+        if (sunnedWorld == null) {
+            getLogger().log(Level.SEVERE, "World '" + world + "' is not valid! Aborting loading config!");
+            return false;
+        }
+
+        if (damagaPerSecond == 0) {
+            getLogger().log(Level.WARNING, "damagePerSecond is 0. Was this intended?");
+        }
+
+        return true;
     }
 }
\ No newline at end of file
diff --git a/src/main/java/dev/genbyte/sunfright/events/HelmetHandler.java b/src/main/java/dev/genbyte/sunfright/events/HelmetHandler.java
index a583f44..739ee78 100644
--- a/src/main/java/dev/genbyte/sunfright/events/HelmetHandler.java
+++ b/src/main/java/dev/genbyte/sunfright/events/HelmetHandler.java
@@ -11,7 +11,15 @@ import org.bukkit.event.player.PlayerJoinEvent;
 import org.bukkit.inventory.ItemStack;
 import org.bukkit.inventory.PlayerInventory;
 
+import dev.genbyte.sunfright.Sunfright;
+
 public class HelmetHandler implements Listener {
+	private final Sunfright sf;
+
+	public HelmetHandler(Sunfright sf) {
+		this.sf = sf;
+	}
+
 	@EventHandler
 	public void onPlayerRespawn(PlayerRespawnEvent event) {
 		setRespawnHelmet(event.getPlayer());
@@ -37,6 +45,10 @@ public class HelmetHandler implements Listener {
 	}
 
 	private void setRespawnHelmet(Player player) {
+		if (!player.getWorld().equals(sf.sunnedWorld)) {
+			return;
+		}
+
 		PlayerInventory inv = player.getInventory();
 
 		ItemStack stack = new ItemStack(Material.LEATHER_HELMET);