blob: 4ee7615faf65827deac022245ca2182962f43e5e (
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
|
package dev.genbyte.sunfright;
import java.util.Collection;
import java.util.Random;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.block.Block;
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;
private final Random rand;
public Damager(Sunfright sf) {
this.sf = sf;
this.rand = new Random();
}
public void run() {
if (!timeShouldDamage()) {
return;
}
World sunnedWorld = sf.sunnedWorld;
Collection<? extends Player> players = sunnedWorld.getPlayers();
players.forEach((player) -> {
Location loc = player.getLocation();
int x = loc.getBlockX();
int y = loc.getBlockY();
int z = loc.getBlockZ();
// Returns one lower than it should?
int highestY = sunnedWorld.getHighestBlockAt(loc).getLocation().getBlockY()+1;
for (int i = y; i < highestY; ++i) {
Block current = sunnedWorld.getBlockAt(x, i, z);
if (!blockShouldDamage(current.getType())) {
/* player rulled to be safe. Remove their helmet if it's the one we gave, but
only do so if the skylight is less than three. This will keep us from
removing the starter helmet if they're just chopping down a tree */
if (player.getInventory().getHelmet() != null &&
player.getLocation().getBlock().getLightFromSky() > 3 &&
player.getInventory().getHelmet().getEnchantmentLevel(Enchantment.VANISHING_CURSE) == 2)
{
player.getInventory().setHelmet(new ItemStack(Material.AIR));
}
return;
}
}
new DoDamage(player, sf.damagaPerSecond).runTask(sf);
});
}
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 >= 12734 && time <= 23266) {
return false;
}
} else if (storming && thundering) {
if (time >= 12300 && time <= 23700) {
return false;
}
} else if (time >= 13027 && time <= 22973) {
return false;
}
return true;
}
/*
Material.isTransparent() is buggy and awful and only gives true for some things. This function
checks if a material lets light pass and should damage the player.
I've never seen it give a false positive, only a false negative, so it is one of the first
things we check.
*/
@SuppressWarnings("deprecation")
private boolean blockShouldDamage(Material mat) {
String key = mat.getKey().getKey().toLowerCase();
if (mat == Material.BLACK_STAINED_GLASS) {
return false;
}
return mat.isTransparent() || key.indexOf("glass") != -1 || key.indexOf("leaves") != -1 || key.indexOf("sign") != -1 || key.indexOf("trapdoor") != -1 || key.indexOf("fence") != -1 || key.indexOf("bed") != -1 || mat == Material.ICE || mat == Material.HOPPER || mat == Material.COBWEB;
}
private class DoDamage extends BukkitRunnable {
private final Player player;
private final int damage;
public DoDamage(Player player, int 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();
int fireProtLevel = helmet.getEnchantmentLevel(Enchantment.PROTECTION_FIRE);
if (fireProtLevel < 1) {
damagePlayer();
return;
}
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 {
// Formula from https://minecraft.gamepedia.com/Unbreaking
// Origintal is 60 + (40 / (level+1)) but we subtract one from fireProtLevel
// so the +1 cancels
int chanceToDamage = 60 + (40 / (fireProtLevel));
if (rand.nextInt(99)+1 <= chanceToDamage) {
int calculatedDamage = (int) Math.ceil(damage / 2);
helmetDamageable.setDamage(helmetDamage + calculatedDamage);
helmet.setItemMeta((ItemMeta) helmetDamageable);
}
}
}
} else {
damagePlayer();
}
}
private void damagePlayer() {
player.damage(damage);
}
}
}
|