about summary refs log tree commit diff
path: root/src/main/java/dev/genbyte/sunfright/Damager.java
blob: 8c223fbc7737d7a555f446a96f0102ad8408816a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package dev.genbyte.sunfright;

import java.util.Collection;
import java.util.logging.Level;

import org.bukkit.Material;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.Damageable;
import org.bukkit.inventory.meta.ItemMeta;
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) {
				new DoDamage(player, (byte) 1).runTask(sf);
			} else if (player.getInventory().getHelmet().getEnchantmentLevel(Enchantment.VANISHING_CURSE) == 2) {
				player.getInventory().setHelmet(new ItemStack(Material.AIR));
			}
		});
	}

	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) {
				ItemMeta helmetMeta = helmet.getItemMeta();

				if (helmetMeta instanceof Damageable) {
					Damageable helmetDamageable = (Damageable) helmetMeta;
					int helmetDamage = helmetDamageable.getDamage();

					if (helmetDamage + damage >= helmet.getType().getMaxDurability()) {
						if (helmet.getEnchantmentLevel(Enchantment.VANISHING_CURSE) == 2) {
							int bindLevel = helmet.getEnchantmentLevel(Enchantment.BINDING_CURSE);

							if (bindLevel < 5) {
								helmetDamageable.setDamage(0);
								helmet.setItemMeta((ItemMeta) helmetDamageable);
								helmet.addUnsafeEnchantment(Enchantment.BINDING_CURSE, bindLevel + 1);

								return;
							}
						}

						player.getInventory().setHelmet(new ItemStack(Material.AIR));
					} else {
						helmetDamageable.setDamage(helmetDamage + damage);
						helmet.setItemMeta((ItemMeta) helmetDamageable);
					}
				}
			} else {
				player.damage(damage * 2);
			}
		}
	}
}