-
Notifications
You must be signed in to change notification settings - Fork 9
Fixes on hydraulic cannon #294
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Intybyte
wants to merge
6
commits into
master
Choose a base branch
from
fix/hydraulic-cannon-horse-fix-clay-ball-go-brrrrrrrrrrrrr
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
3724d7a
Fix horses being able to equip hydraulic cannons
Intybyte 705c3e5
Remake DisplayProjectile properly and block it from behaving like ray…
Intybyte 29a4ce7
Look, idk why you are behaving like this, but stop spitting the wrong…
Intybyte ab28f64
Task can be stopped here also
Intybyte f21ff27
Sure man
Intybyte e3fd823
clean up
Intybyte File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
165 changes: 103 additions & 62 deletions
165
src/main/java/io/github/pylonmc/pylon/base/util/DisplayProjectile.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,101 +1,142 @@ | ||
| package io.github.pylonmc.pylon.base.util; | ||
|
|
||
| import io.github.pylonmc.pylon.base.PylonBase; | ||
| import io.github.pylonmc.pylon.core.entity.PylonEntity; | ||
| import io.github.pylonmc.pylon.core.entity.display.BlockDisplayBuilder; | ||
| import io.github.pylonmc.pylon.core.entity.display.transform.LineBuilder; | ||
| import lombok.experimental.UtilityClass; | ||
| import org.bukkit.Bukkit; | ||
| import org.bukkit.Location; | ||
| import org.bukkit.Material; | ||
| import org.bukkit.Particle; | ||
| import org.bukkit.damage.DamageSource; | ||
| import org.bukkit.damage.DamageType; | ||
| import org.bukkit.entity.BlockDisplay; | ||
| import org.bukkit.entity.Damageable; | ||
| import org.bukkit.entity.Entity; | ||
| import org.bukkit.entity.Player; | ||
| import org.bukkit.entity.Projectile; | ||
| import org.bukkit.event.entity.EntityDamageEvent; | ||
| import org.bukkit.scheduler.BukkitRunnable; | ||
| import org.bukkit.scheduler.BukkitScheduler; | ||
| import org.bukkit.scheduler.BukkitTask; | ||
| import org.bukkit.util.Vector; | ||
| import org.jetbrains.annotations.NotNull; | ||
| import org.joml.Vector3d; | ||
|
|
||
| import java.util.List; | ||
| import java.util.Optional; | ||
|
|
||
|
|
||
| @UtilityClass | ||
| public class DisplayProjectile { | ||
| public void spawn( | ||
| Player player, | ||
| Material material, | ||
| Location source, | ||
| Vector direction, | ||
| float thickness, | ||
| float length, | ||
| float speedBlockPerSecond, | ||
| double damage, | ||
| int tickInterval, | ||
| int lifetimeTicks | ||
| public final class DisplayProjectile { | ||
| private final Player player; | ||
| private final float thickness; | ||
| private final double damage; | ||
| private final int tickInterval; | ||
| private final Vector locationStep; | ||
| private final BlockDisplay projectile; | ||
|
|
||
|
|
||
| public DisplayProjectile( | ||
| Player player, | ||
| Material material, | ||
| Location source, | ||
| Vector direction, | ||
| float thickness, | ||
| float length, | ||
| float speedBlockPerSecond, | ||
| double damage, | ||
| int tickInterval, | ||
| int lifetimeTicks | ||
| ) { | ||
| Vector locationStep = direction.clone().multiply(speedBlockPerSecond * tickInterval / 20.0); | ||
| BlockDisplay projectile = new BlockDisplayBuilder() | ||
| .transformation(new LineBuilder() | ||
| .from(new Vector3d(0, 0, 0)) | ||
| .to(direction.clone().multiply(length).toVector3d()) | ||
| .thickness(thickness) | ||
| .build() | ||
| ) | ||
| .material(material) | ||
| .build(source); | ||
| projectile.setPersistent(false); | ||
|
|
||
| BukkitTask task = Bukkit.getScheduler().runTaskTimer(PylonBase.getInstance(), () -> { | ||
| tick(player, projectile, locationStep, tickInterval, thickness, damage); | ||
| }, tickInterval, tickInterval); | ||
|
|
||
| Bukkit.getScheduler().runTaskLater(PylonBase.getInstance(), projectile::remove, lifetimeTicks); | ||
| Bukkit.getScheduler().runTaskLater(PylonBase.getInstance(), task::cancel, lifetimeTicks); | ||
| this.player = player; | ||
| this.thickness = thickness; | ||
| this.damage = damage; | ||
| this.tickInterval = tickInterval; | ||
| this.locationStep = direction.clone().multiply(speedBlockPerSecond * tickInterval / 20.0); | ||
| this.projectile = new BlockDisplayBuilder() | ||
| .transformation(new LineBuilder() | ||
| .from(new Vector3d(0, 0, 0)) | ||
| .to(direction.clone().multiply(length).toVector3d()) | ||
| .thickness(thickness) | ||
| .build() | ||
| ) | ||
| .material(material) | ||
| .build(source); | ||
| this.projectile.setPersistent(false); | ||
|
|
||
| new Task(tickInterval, lifetimeTicks).start(); | ||
| } | ||
|
|
||
| private void tick( | ||
| Player player, | ||
| @NotNull BlockDisplay projectile, | ||
| Vector locationStep, | ||
| int tickInterval, | ||
| float thickness, | ||
| double damage | ||
| ) { | ||
| if (projectile.isDead()) { | ||
| return; | ||
| private class Task extends BukkitRunnable { | ||
| private final int tickInterval; | ||
| private final int lifetimeTicks; | ||
| private int livedTicks; | ||
|
|
||
| public Task(int tickInterval, int lifetimeTicks) { | ||
| this.tickInterval = tickInterval; | ||
| this.lifetimeTicks = lifetimeTicks; | ||
| } | ||
|
|
||
| projectile.setTeleportDuration(tickInterval); | ||
| projectile.teleport(projectile.getLocation().add(locationStep)); | ||
|
|
||
| List<Entity> nearbyEntities = projectile.getNearbyEntities(thickness * 1.5, thickness * 1.5, thickness * 1.5); | ||
| if (nearbyEntities.isEmpty()) { | ||
| return; | ||
| } | ||
| @Override | ||
| public void run() { | ||
| if (livedTicks >= lifetimeTicks) { | ||
| projectile.remove(); | ||
| cancel(); | ||
| return; | ||
| } | ||
| livedTicks += tickInterval; | ||
|
|
||
| if (projectile.isDead()) { | ||
| cancel(); | ||
| return; | ||
| } | ||
|
|
||
| projectile.setTeleportDuration(tickInterval); | ||
|
|
||
| Optional<Damageable> hitEntity = nearbyEntities.stream() | ||
| Location teleportTo = projectile.getLocation().add(locationStep); | ||
| if (!teleportTo.getBlock().isPassable()) { | ||
| teleportTo.getWorld().spawnParticle( | ||
| Particle.CRIT, | ||
| teleportTo, | ||
| 15 | ||
| ); | ||
| projectile.remove(); | ||
| cancel(); | ||
| return; | ||
| } | ||
|
|
||
| projectile.teleport(teleportTo); | ||
|
|
||
| List<Entity> nearbyEntities = projectile.getNearbyEntities(thickness * 1.5, thickness * 1.5, thickness * 1.5); | ||
| if (nearbyEntities.isEmpty()) { | ||
| return; | ||
| } | ||
|
|
||
| Optional<Damageable> hitEntity = nearbyEntities.stream() | ||
| .filter(Damageable.class::isInstance) | ||
| .map(Damageable.class::cast) | ||
| .findFirst(); | ||
| hitEntity.ifPresent(entity -> { | ||
| EntityDamageEvent event = new EntityDamageEvent( | ||
| hitEntity.ifPresent(entity -> { | ||
| EntityDamageEvent event = new EntityDamageEvent( | ||
| entity, | ||
| EntityDamageEvent.DamageCause.ENTITY_ATTACK, | ||
| DamageSource.builder(DamageType.ARROW) | ||
| .withCausingEntity(player) | ||
| .withDirectEntity(entity) | ||
| .build(), | ||
| .withCausingEntity(player) | ||
| .withDirectEntity(entity) | ||
| .build(), | ||
| damage | ||
| ); | ||
| if (event.callEvent()) { | ||
| entity.damage(damage); | ||
| entity.setVelocity(locationStep.clone().normalize().multiply(0.2)); | ||
| } | ||
| projectile.remove(); | ||
| }); | ||
| ); | ||
| if (event.callEvent()) { | ||
| entity.damage(damage); | ||
| entity.setVelocity(locationStep.clone().normalize().multiply(0.2)); | ||
| } | ||
| projectile.remove(); | ||
| Task.this.cancel(); | ||
| }); | ||
| } | ||
|
|
||
| public void start() { | ||
| this.runTaskTimer(PylonBase.getInstance(), tickInterval, tickInterval); | ||
| } | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's with all the changes in this class? It seems to be a lot more complex before but I cannot tell why it was needed. Is it not as simple as just checking the block and despawning the projectile if said block is not solid, every tick - or previous block as well or something if necessary?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So I rewrote the entire thing to match a similar logic to the confetti popper where we have a master task and everything dies when we are done, it is more like an optimization
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Um... ????? The constructor has the same number of parameters as the 'massive parameter function' so this changes nothing except adding more code. There is no currying going on here either? Very confused here lol.
I see what you mean with the task not being cancelled, but as there are so few of these tasks running at once, and by far the most performance-intensive part is finding nearby entities/blocks and teleporting, it's a bit of a pointless optimisation.
If you do really want it as a class that is instantiated though, may I suggest you make it a PylonEntity instead? This is IMO a perfect use case for PylonEntity as we're wrapping an entity and adding our own logic. You could use the entity ticker stuff too.