much simple

This commit is contained in:
MidnightTale 2023-04-12 09:18:52 +07:00
commit 8647f3c2b2

View file

@ -11,41 +11,12 @@ import org.bukkit.entity.FallingBlock;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.event.block.BlockPhysicsEvent;
import org.bukkit.event.entity.EntityChangeBlockEvent;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.util.Vector;
public class Bbsdfia extends JavaPlugin implements Listener {
private static final Material[] BLOCK_TYPES = {
Material.BLACK_CONCRETE_POWDER,
Material.BLUE_CONCRETE_POWDER,
Material.BROWN_CONCRETE_POWDER,
Material.CYAN_CONCRETE_POWDER,
Material.GRAY_CONCRETE_POWDER,
Material.GREEN_CONCRETE_POWDER,
Material.LIGHT_BLUE_CONCRETE_POWDER,
Material.LIGHT_GRAY_CONCRETE_POWDER,
Material.LIME_CONCRETE_POWDER,
Material.MAGENTA_CONCRETE_POWDER,
Material.ORANGE_CONCRETE_POWDER,
Material.PINK_CONCRETE_POWDER,
Material.PURPLE_CONCRETE_POWDER,
Material.RED_CONCRETE_POWDER,
Material.WHITE_CONCRETE_POWDER,
Material.YELLOW_CONCRETE_POWDER,
Material.DRAGON_EGG,
Material.GRAVEL,
Material.RED_SAND,
Material.SAND
};
private static final Vector VELOCITY_SOUTH = new Vector(0, 1, -2);
private static final Vector VELOCITY_EAST = new Vector(2, 1, 0);
private static final Vector VELOCITY_NORTH = new Vector(0, 1, 2);
private static final Vector VELOCITY_WEST = new Vector(-2, 1, 0);
private static final Location START_LOCATION = new Location(Bukkit.getWorld("world_the_end"), 101, 49, 1);
private static final Location END_LOCATION = new Location(Bukkit.getWorld("world_the_end"), 99, 51, -1);
@Override
public void onEnable() {
super.onEnable();
@ -85,38 +56,64 @@ public class Bbsdfia extends JavaPlugin implements Listener {
}
}
@EventHandler
public void onBlockBreak(BlockBreakEvent event) {
public void onBlockPhysics(BlockPhysicsEvent event) {
Block block = event.getBlock();
if (isTargetBlock(block.getType())) {
World world = block.getWorld();
FallingBlock fallingBlock = world.spawnFallingBlock(block.getLocation(), block.getBlockData());
Vector velocity = getVelocity(block.getLocation());
fallingBlock.setVelocity(velocity);
if (isFallingBlock(block.getType())) {
// Get the location of the block
Location loc = block.getLocation();
// Check if the block is within the desired area
if (isWithinArea(loc)) {
// Create a new falling block entity at the location of the block
FallingBlock fallingBlock = loc.getWorld().spawnFallingBlock(loc, block.getBlockData());
// Set the velocity of the falling block entity
fallingBlock.setVelocity(getRandomVelocity());
// Remove the original block
block.setType(Material.AIR);
}
}
private boolean isTargetBlock(Material material) {
for (Material blockType : BLOCK_TYPES) {
if (blockType == material) {
return true;
}
}
return false;
}
private Vector getVelocity(Location location) {
Vector velocity = null;
if (location.getBlockX() == START_LOCATION.getBlockX()) {
velocity = VELOCITY_EAST;
} else if (location.getBlockZ() == END_LOCATION.getBlockZ()) {
velocity = VELOCITY_SOUTH;
} else if (location.getBlockX() == END_LOCATION.getBlockX()) {
velocity = VELOCITY_NORTH;
} else if (location.getBlockX() == END_LOCATION.getBlockX()) {
velocity = VELOCITY_WEST;
private boolean isFallingBlock(Material material) {
return material == Material.BLACK_CONCRETE_POWDER ||
material == Material.BLUE_CONCRETE_POWDER ||
material == Material.BROWN_CONCRETE_POWDER ||
material == Material.CYAN_CONCRETE_POWDER ||
material == Material.GRAY_CONCRETE_POWDER ||
material == Material.GREEN_CONCRETE_POWDER ||
material == Material.LIGHT_BLUE_CONCRETE_POWDER ||
material == Material.LIGHT_GRAY_CONCRETE_POWDER ||
material == Material.LIME_CONCRETE_POWDER ||
material == Material.MAGENTA_CONCRETE_POWDER ||
material == Material.ORANGE_CONCRETE_POWDER ||
material == Material.PINK_CONCRETE_POWDER ||
material == Material.PURPLE_CONCRETE_POWDER ||
material == Material.RED_CONCRETE_POWDER ||
material == Material.WHITE_CONCRETE_POWDER ||
material == Material.YELLOW_CONCRETE_POWDER ||
material == Material.DRAGON_EGG ||
material == Material.GRAVEL ||
material == Material.RED_SAND ||
material == Material.SAND;
}
return velocity;
private boolean isWithinArea(Location loc) {
return loc.getWorld().getName().equals("world_the_end") &&
loc.getBlockX() >= 101 && loc.getBlockX() <= 99 &&
loc.getBlockY() >= 49 && loc.getBlockY() <= 51 &&
loc.getBlockZ() >= -1 && loc.getBlockZ() <= 1;
}
private Vector getRandomVelocity() {
int direction = (int) (Math.random() * 4);
return switch (direction) {
case 0 -> new Vector(0, 1, -2);
case 1 -> new Vector(2, 1, 0);
case 2 -> new Vector(0, 1, 2);
default -> new Vector(-2, 1, 0);
};
}