-
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathProjectileObserver.java
More file actions
270 lines (227 loc) · 10.2 KB
/
Copy pathProjectileObserver.java
File metadata and controls
270 lines (227 loc) · 10.2 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.scheduler;
import at.pavlov.cannons.Cannons;
import at.pavlov.cannons.CreateExplosion;
import at.pavlov.internal.enums.FakeBlockType;
import at.pavlov.cannons.container.ItemHolder;
import at.pavlov.cannons.container.SoundHolder;
import at.pavlov.cannons.dao.AsyncTaskManager;
import at.pavlov.cannons.projectile.FlyingProjectile;
import at.pavlov.cannons.projectile.Projectile;
import at.pavlov.cannons.projectile.ProjectileManager;
import at.pavlov.internal.projectile.ProjectileProperties;
import at.pavlov.cannons.utils.CannonsUtil;
import at.pavlov.cannons.utils.SoundUtils;
import io.papermc.lib.PaperLib;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.block.Block;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.util.Vector;
import java.util.Random;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executor;
public class ProjectileObserver {
private final Cannons plugin;
private final Random random = new Random();
/**
* Constructor
* @param plugin - Cannons instance
*/
public ProjectileObserver(Cannons plugin)
{
this.plugin = plugin;
}
/**
* starts the scheduler of the teleporter
*/
public void setupScheduler()
{
//changing angles for aiming mode
var taskManager = AsyncTaskManager.get();
taskManager.scheduler.runTaskTimer(() -> {
//get projectiles
var projectiles = ProjectileManager
.getInstance()
.getFlyingProjectiles();
for(var entry : projectiles.entrySet()) {
FlyingProjectile cannonball = entry.getValue();
Entity projectile_entity = cannonball.getProjectileEntity();
Executor executor = (task) -> {
if (plugin.isFolia()) {
taskManager.scheduler.runTask(projectile_entity, task);
} else {
task.run();
}
};
var key = entry.getKey();
CompletableFuture.runAsync( () -> {
if (cannonball.isValid(projectile_entity)) {
//update the cannonball
checkWaterImpact(cannonball, projectile_entity);
updateTeleporter(cannonball, projectile_entity);
updateSmokeTrail(cannonball, projectile_entity);
if (updateProjectileLocation(cannonball, projectile_entity)) {
projectiles.remove(key);
}
return;
}
//remove a not valid projectile
//teleport the observer back to its start position
CannonsUtil.teleportBack(cannonball);
if (projectile_entity != null)
{
Location l = projectile_entity.getLocation();
projectile_entity.remove();
plugin.logDebug("removed Projectile at " + l.getBlockX() + "," + l.getBlockY() + "," + l.getBlockZ() + " because it was not valid.");
}
else
plugin.logDebug("removed Projectile at because the entity was missing");
//remove entry in hashmap
projectiles.remove(key);
}, executor);
}
}, 1L, 1L);
}
/**
* if cannonball enters water it will spawn a splash effect
* @param cannonball the projectile to check
*/
private void checkWaterImpact(FlyingProjectile cannonball, Entity projectile_entity) {
//the projectile has passed the water surface, make a splash
if (!cannonball.updateWaterSurfaceCheck(projectile_entity)) {
return;
}
//go up until there is air and place the same liquid
Location startLoc = projectile_entity.getLocation().clone();
Vector vel = projectile_entity.getVelocity().clone();
ItemHolder liquid = new ItemHolder(startLoc.getBlock().getType());
for (int i = 0; i<5; i++) {
Block block = startLoc.subtract(vel.clone().multiply(i)).getBlock();
if (block == null || !block.isEmpty()) {
continue;
}
//found a free block - make the splash
sendSplashToPlayers(block.getLocation(), liquid, cannonball.getProjectile().getSoundImpactWater());
break;
}
}
/**
* creates a sphere of fake blocks on the impact for all player in the vicinity
* @param loc - location of the impact
* @param liquid - material of the fake blocks
*/
public void sendSplashToPlayers(Location loc, ItemHolder liquid, SoundHolder sound)
{
int maxDist = (int) plugin.getMyConfig().getImitatedBlockMaximumDistance();
int maxSoundDist = plugin.getMyConfig().getImitatedSoundMaximumDistance();
float maxVol = plugin.getMyConfig().getImitatedSoundMaximumVolume();
for(Player p : loc.getWorld().getPlayers())
{
Location pl = p.getLocation();
double distance = pl.distanceSquared(loc);
if(distance <= maxDist * maxDist)
FakeBlockHandler.getInstance().imitatedSphere(p, loc, 1, Bukkit.createBlockData(liquid.getType()), FakeBlockType.WATER_SPLASH, 1.0);
}
SoundUtils.imitateSound(loc, sound, maxSoundDist, maxVol);
}
/**
* teleports the player to new position of the cannonball
* @param cannonball the FlyingProjectile to check
*/
private void updateTeleporter(FlyingProjectile cannonball, Entity projectile_entity)
{
//do nothing if the teleport was already performed
if (cannonball.isTeleported())
return;
//if projectile has HUMAN_CANNONBALL or OBSERVER - update player position
Projectile projectile = cannonball.getProjectile();
if (!projectile.hasProperty(ProjectileProperties.HUMAN_CANNONBALL) && !projectile.hasProperty(ProjectileProperties.OBSERVER)) {
return;
}
Player shooter = Bukkit.getPlayer(cannonball.getShooterUID());
if(shooter == null)
return;
//set some distance to the snowball to prevent a collision
Location optiLoc = projectile_entity.getLocation().clone().subtract(projectile_entity.getVelocity().normalize().multiply(20.0));
Vector distToOptimum = optiLoc.toVector().subtract(shooter.getLocation().toVector());
Vector playerVel = projectile_entity.getVelocity().add(distToOptimum.multiply(0.1));
//cap for maximum speed
if (playerVel.getX() > 5.0)
playerVel.setX(5.0);
if (playerVel.getY() > 5.0)
playerVel.setY(5.0);
if (playerVel.getZ() > 5.0)
playerVel.setZ(5.0);
shooter.setVelocity(playerVel);
shooter.setFallDistance(0.0f);
//teleport if the player is behind
if (distToOptimum.length() > 30)
{
optiLoc.setYaw(shooter.getLocation().getYaw());
optiLoc.setPitch(shooter.getLocation().getPitch());
PaperLib.teleportAsync(shooter, optiLoc);
}
}
/**
* calculates the location where the projectile should be an teleports the projectile to this location
* @param cannonball projectile to update
* @return true if the projectile must be removed
*/
private boolean updateProjectileLocation(FlyingProjectile cannonball, Entity projectile_entity)
{
if (!plugin.getMyConfig().isKeepAliveEnabled())
return false;
if (cannonball.distanceToProjectile(projectile_entity) > plugin.getMyConfig().getKeepAliveTeleportDistance())
{
Location toLoc = cannonball.getExpectedLocation();
plugin.logDebug("teleported projectile to: " + toLoc.getBlockX() + "," + toLoc.getBlockY() + "," + toLoc.getBlockZ());
cannonball.teleportToPrediction(projectile_entity);
}
//see if we hit something
Block block = cannonball.getExpectedLocation().getBlock();
if (!block.isEmpty() && !block.isLiquid())
{
cannonball.revertUpdate();
cannonball.teleportToPrediction(projectile_entity);
CreateExplosion.getInstance().detonate(cannonball, projectile_entity);
projectile_entity.remove();
return true;
}
//todo proximity fuse
cannonball.update();
return false;
}
/**
* spawn smoke clouds behind the projectile to improve the visibility
* @param cannonball the cannonball entity entry of cannons
* @param projectile_entity the entity of the projectile
*/
private void updateSmokeTrail(FlyingProjectile cannonball, Entity projectile_entity) {
Projectile proj = cannonball.getProjectile();
int maxDist = plugin.getMyConfig().getImitatedBlockMaximumDistance();
double smokeDist = proj.getSmokeTrailDistance()*(0.5 + random.nextDouble());
double smokeDuration = proj.getSmokeTrailDuration()*(0.5 + random.nextGaussian());
if (!proj.isSmokeTrailEnabled() || !(cannonball.getExpectedLocation().distance(cannonball.getLastSmokeTrailLocation()) > smokeDist)) {
return;
}
//create a new smoke trail cloud
Location newLoc = cannonball.getExpectedLocation();
cannonball.setLastSmokeTrailLocation(newLoc);
plugin.logDebug("smoke trail at: " + newLoc.getBlockX() + "," + newLoc.getBlockY() + "," + newLoc.getBlockZ());
if (proj.isSmokeTrailParticleEnabled()) {
proj.getSmokeTrailParticle().at(newLoc);
return;
}
// added null if the world was deleted
if (newLoc.getWorld() == null) {
return;
}
for (Player p : newLoc.getWorld().getPlayers()) {
Location pl = p.getLocation();
double distance = pl.distance(newLoc);
if (distance <= maxDist)
FakeBlockHandler.getInstance().imitatedSphere(p, newLoc, 0, proj.getSmokeTrailMaterial(), FakeBlockType.SMOKE_TRAIL, smokeDuration);
}
}
}