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.entity.FallingBlock;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockPhysicsEvent;
import org.bukkit.event.entity.EntityChangeBlockEvent; import org.bukkit.event.entity.EntityChangeBlockEvent;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.util.Vector; import org.bukkit.util.Vector;
@ -53,41 +54,25 @@ public class Bbsdfia extends JavaPlugin implements Listener {
} }
} }
@EventHandler @EventHandler
public void onEntityChangeBlock(EntityChangeBlockEvent event) { public void onBlockPhysics(BlockPhysicsEvent event) {
if (event.getEntity() instanceof FallingBlock fallingBlock) { Block block = event.getBlock();
if (fallingBlock.getWorld().getEnvironment() == World.Environment.THE_END && event.getTo() != Material.AIR) { World world = block.getWorld();
Location blockLocation = fallingBlock.getLocation(); if (world.getEnvironment() == World.Environment.THE_END &&
if (isInCube(blockLocation, 101, 49, 1, 99, 51, -1)) { block.getType().hasGravity() &&
if (fallingBlock.getFallDistance() > 0) { block.getX() >= 101 && block.getX() <= 99 &&
Vector velocity; block.getY() >= 49 && block.getY() <= 51 &&
if (fallingBlock.getVelocity().getX() == 0 && fallingBlock.getVelocity().getZ() == 0) { block.getZ() >= -1 && block.getZ() <= 1) {
velocity = new Vector(-1, 0, 0); // Turn the block into a falling block
} else if (fallingBlock.getVelocity().getX() < 0 && fallingBlock.getVelocity().getZ() == 0) { block.setType(Material.AIR);
velocity = new Vector(0, 0, -1); Location location = block.getLocation().add(0.5, 0.5, 0.5);
} else if (fallingBlock.getVelocity().getX() < 0 && fallingBlock.getVelocity().getZ() < 0) { FallingBlock fallingBlock = world.spawnFallingBlock(location, block.getBlockData());
velocity = new Vector(1, 0, 0); // Set the velocity of the falling block
} else { Vector velocity = new Vector(0, -0.5, 1);
velocity = new Vector(0, 0, 1); fallingBlock.setVelocity(velocity);
}
fallingBlock.setVelocity(velocity.multiply(0.5));
}
}
}
} }
} }
}
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){ Block getBlockMovingTo(Location loc, Vector vel){
double absMax = 0, max = 0; double absMax = 0, max = 0;