-
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathFireTaskWrapper.java
More file actions
270 lines (228 loc) · 9.91 KB
/
Copy pathFireTaskWrapper.java
File metadata and controls
270 lines (228 loc) · 9.91 KB
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
package at.pavlov.cannons.dao.wrappers;
import at.pavlov.cannons.Cannons;
import at.pavlov.cannons.Enum.BreakCause;
import at.pavlov.internal.enums.FakeBlockType;
import at.pavlov.cannons.Enum.ProjectileCause;
import at.pavlov.cannons.cannon.Cannon;
import at.pavlov.cannons.cannon.CannonDesign;
import at.pavlov.cannons.cannon.CannonManager;
import at.pavlov.cannons.projectile.Projectile;
import at.pavlov.cannons.projectile.ProjectileManager;
import at.pavlov.internal.projectile.ProjectileProperties;
import at.pavlov.cannons.scheduler.FakeBlockHandler;
import at.pavlov.cannons.utils.SoundUtils;
import com.cryptomorin.xseries.XPotion;
import com.cryptomorin.xseries.particles.XParticle;
import org.bukkit.Bukkit;
import org.bukkit.Effect;
import org.bukkit.GameMode;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.potion.PotionEffect;
import org.bukkit.projectiles.ProjectileSource;
import org.bukkit.util.Vector;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
/**
* Wrapper for executing the fire task, this is used internally by cannons,
* for normal use please use FireCannon#fire instead of using this, use this
* class only if you need for some reason to bypass some checks
* <br>
* If you want to change the FireTask, and change its logic, extend this class
* and change fireTask as that is what is fired when the projectile is actually shot
*/
public class FireTaskWrapper implements BaseFireTask {
protected static final Cannons plugin = Cannons.getPlugin();
protected final Cannon cannon;
protected final UUID shooter;
protected final boolean removeCharge;
protected final ProjectileCause projectileCause;
public FireTaskWrapper(
Cannon cannon,
UUID shooter,
boolean removeCharge,
ProjectileCause projectileCause
) {
this.cannon = cannon;
this.shooter = shooter;
this.removeCharge = removeCharge;
this.projectileCause = projectileCause;
}
/**
* fires a cannon and removes the charge from the shooter
*/
public void fireTask() {
CannonDesign design = cannon.getCannonDesign();
Projectile projectile = cannon.getLoadedProjectile();
//the shooter might be null if not online
Player onlinePlayer = Bukkit.getPlayer(shooter);
// no projectile no cannon firing
if (projectile == null) return;
// no gunpowder no cannon firing
if (cannon.getLoadedGunpowder() <= 0) return;
//increased fired cannonballs
cannon.incrementFiredCannonballs();
//get firing location
Location firingLoc = design.getMuzzle(cannon);
World world = cannon.getWorldBukkit();
//Muzzle flash + effects
if (projectile.getProjectileEntity().equals(EntityType.ARROW))
world.playEffect(firingLoc, Effect.BOW_FIRE, 10);
muzzleFire();
world.playEffect(firingLoc, Effect.SMOKE, cannon.getCannonDirection());
//increase heat of the cannon
if (design.isHeatManagementEnabled())
cannon.setTemperature(cannon.getTemperature() + design.getHeatIncreasePerGunpowder() / projectile.getAutomaticFiringMagazineSize() * cannon.getLoadedGunpowder());
//automatic cool down
if (design.isAutomaticCooling())
cannon.automaticCooling();
//for each bullet, but at least once
fireProjectiles(projectile, onlinePlayer);
//check if the temperature exceeds the limit and overloading
if (cannon.checkHeatManagement() || cannon.isExplodedDueOverloading()) {
CannonManager.getInstance().removeCannon(cannon, true, true, BreakCause.Overheating);
return;
}
//reset after firing
cannon.setLastFired(System.currentTimeMillis());
cannon.setSoot(cannon.getSoot() + design.getSootPerGunpowder() * cannon.getLoadedGunpowder());
if (!removeCharge) {
return;
}
cannon.setProjectilePushed(design.getProjectilePushing());
plugin.logDebug("fire event complete, charge removed from the cannon");
//removes the gunpowder and projectile loaded in the cannon if not set otherwise
if (design.isRemoveChargeAfterFiring())
cannon.removeCharge();
}
/**
* Use this method when you want to force shot a cannon without the checks
* such as: Temperature, Gunpowder, Soot etc.
*
* @param projectile shooter projectile
* @param onlinePlayer shooter
*/
public void fireProjectiles(Projectile projectile, Player onlinePlayer) {
var design = cannon.getCannonDesign();
var firingLoc = cannon.getMuzzle();
for (int i = 0; i < Math.max(projectile.getNumberOfBullets(), 1); i++) {
ProjectileSource source = null;
Location playerLoc = null;
if (onlinePlayer != null) {
source = onlinePlayer;
playerLoc = onlinePlayer.getLocation();
}
Vector vect = cannon.getFiringVector(true, true);
Entity projectileEntity = ProjectileManager.getInstance().spawnProjectile(projectile, shooter, source, playerLoc, firingLoc, vect, cannon.getUID(), projectileCause);
if (i == 0 && projectile.hasProperty(ProjectileProperties.SHOOTER_AS_PASSENGER) && onlinePlayer != null)
projectileEntity.setPassenger(onlinePlayer);
//confuse all entity which wear no helmets due to the blast of the cannon
List<Entity> living = projectileEntity.getNearbyEntities(8, 8, 8);
//do only once
if (i == 0) {
confuseShooter(living, firingLoc, design.getBlastConfusion());
}
}
}
/**
* creates an imitated explosion made of blocks which is transmitted to shooter in a give distance
*/
public void muzzleFire() {
var config = plugin.getMyConfig();
double minDist = config.getImitatedBlockMinimumDistance();
double maxDist = config.getImitatedBlockMaximumDistance();
float maxVol = config.getImitatedSoundMaximumVolume();
Location loc = cannon.getMuzzle();
//simple particle effects for close distance
loc.getWorld().spawnParticle(XParticle.EXPLOSION.get(), loc, 1);
//fake blocks effects for far distance
if (!config.isImitatedFiringEffectEnabled()) {
return;
}
int maxSoundDist = config.getImitatedSoundMaximumDistance();
SoundUtils.imitateSound(loc, cannon.getCannonDesign().getSoundFiring(), maxSoundDist, maxVol);
List<Player> players = new ArrayList<>();
for (Player p : loc.getWorld().getPlayers()) {
Location pl = p.getLocation();
double distance = pl.distance(loc);
if (distance >= minDist && distance <= maxDist) {
players.add(p);
}
}
imitateSmoke(players);
}
/**
* creates a sphere of fake block and sends it to the given shooter
*/
public void imitateSmoke(List<Player> players) {
var config = plugin.getMyConfig();
if (!config.isImitatedFiringEffectEnabled())
return;
Vector aimingVector = cannon.getAimingVector().clone();
Location loc = cannon.getMuzzle();
double duration = config.getImitatedFiringTime();
for (Player name : players) {
//make smoke and fire effects for large distance
FakeBlockHandler.getInstance().imitateLine(name, loc, aimingVector, 0, 1, config.getImitatedFireMaterial(), FakeBlockType.MUZZLE_FIRE, duration);
FakeBlockHandler.getInstance().imitatedSphere(name, loc.clone().add(aimingVector.clone().normalize()), 2, config.getImitatedSmokeMaterial(), FakeBlockType.MUZZLE_FIRE, duration);
}
}
/**
* confuses an entity to simulate the blast of a cannon
*
* @param living entity to confuse
* @param firingLoc distance to the muzzle
* @param confuseTime how long the entity is confused in seconds
*/
protected void confuseShooter(List<Entity> living, Location firingLoc, double confuseTime) {
//confuse shooter if he wears no helmet (only for one projectile and if its configured)
if (confuseTime <= 0) {
return;
}
PotionEffect confusionEffect = new PotionEffect(XPotion.NAUSEA.get(), (int) confuseTime * 20, 0);
//damage living entities and unprotected players
for (Entity entity : living) {
if (isHarmEntity(entity)) {
LivingEntity livingEntity = (LivingEntity) entity;
if (livingEntity.getLocation().distance(firingLoc) < 5.0)
livingEntity.damage(1);
livingEntity.addPotionEffect(confusionEffect);
}
}
}
protected boolean isHarmEntity(Entity next) {
if (next instanceof Player player) {
//if shooter has no helmet and is not in creative and there are confusion effects - harm him
return player.isOnline() && !checkHelmet(player) && player.getGameMode() != GameMode.CREATIVE;
}
return next instanceof LivingEntity;
}
protected boolean checkHelmet(Player player) {
return player.getInventory().getHelmet() != null;
}
public Cannon cannon() {
return cannon;
}
public UUID shooter() {
return shooter;
}
public boolean removeCharge() {
return removeCharge;
}
public ProjectileCause projectileCause() {
return projectileCause;
}
@Override
public String toString() {
return "FireTaskWrapper[" +
"cannon=" + cannon + ", " +
"shooter=" + shooter + ", " +
"removeCharge=" + removeCharge + ", " +
"projectileCause=" + projectileCause + ']';
}
}