PhysicsEvent

This commit is contained in:
MidnightTale 2023-04-12 08:52:26 +07:00
commit 2df4f704bd

View file

@ -9,6 +9,7 @@ import org.bukkit.entity.EntityType;
import org.bukkit.entity.FallingBlock;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockPhysicsEvent;
import org.bukkit.event.entity.EntityChangeBlockEvent;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.util.Vector;
@ -53,41 +54,25 @@ public class Bbsdfia extends JavaPlugin implements Listener {
}
}
@EventHandler
public void onEntityChangeBlock(EntityChangeBlockEvent event) {
if (event.getEntity() instanceof FallingBlock fallingBlock) {
if (fallingBlock.getWorld().getEnvironment() == World.Environment.THE_END && event.getTo() != Material.AIR) {
Location blockLocation = fallingBlock.getLocation();
if (isInCube(blockLocation, 101, 49, 1, 99, 51, -1)) {
if (fallingBlock.getFallDistance() > 0) {
Vector velocity;
if (fallingBlock.getVelocity().getX() == 0 && fallingBlock.getVelocity().getZ() == 0) {
velocity = new Vector(-1, 0, 0);
} else if (fallingBlock.getVelocity().getX() < 0 && fallingBlock.getVelocity().getZ() == 0) {
velocity = new Vector(0, 0, -1);
} else if (fallingBlock.getVelocity().getX() < 0 && fallingBlock.getVelocity().getZ() < 0) {
velocity = new Vector(1, 0, 0);
} else {
velocity = new Vector(0, 0, 1);
}
fallingBlock.setVelocity(velocity.multiply(0.5));
}
}
public void onBlockPhysics(BlockPhysicsEvent event) {
Block block = event.getBlock();
World world = block.getWorld();
if (world.getEnvironment() == World.Environment.THE_END &&
block.getType().hasGravity() &&
block.getX() >= 101 && block.getX() <= 99 &&
block.getY() >= 49 && block.getY() <= 51 &&
block.getZ() >= -1 && block.getZ() <= 1) {
// Turn the block into a falling block
block.setType(Material.AIR);
Location location = block.getLocation().add(0.5, 0.5, 0.5);
FallingBlock fallingBlock = world.spawnFallingBlock(location, block.getBlockData());
// Set the velocity of the falling block
Vector velocity = new Vector(0, -0.5, 1);
fallingBlock.setVelocity(velocity);
}
}
}
private boolean isInCube(Location location, int x1, int y1, int z1, int x2, int y2, int z2) {
int minX = Math.min(x1, x2);
int minY = Math.min(y1, y2);
int minZ = Math.min(z1, z2);
int maxX = Math.max(x1, x2);
int maxY = Math.max(y1, y2);
int maxZ = Math.max(z1, z2);
return location.getBlockX() >= minX && location.getBlockX() <= maxX
&& location.getBlockY() >= minY && location.getBlockY() <= maxY
&& location.getBlockZ() >= minZ && location.getBlockZ() <= maxZ;
}
Block getBlockMovingTo(Location loc, Vector vel){
double absMax = 0, max = 0;