FoliaFlow/src/main/java/xyz/hynse/bbsdfia/Bbsdfia.java
2023-04-12 09:06:24 +07:00

149 lines
No EOL
5.4 KiB
Java

package xyz.hynse.bbsdfia;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.entity.Entity;
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.BlockBreakEvent;
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(0, 1, 2);
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();
getServer().getPluginManager().registerEvents(this, this);
getServer().getLogger().info("Bbsdfia plugin started");
}
@Override
public void onDisable() {
super.onDisable();
getServer().getLogger().info("Bbsdfia plugin stopped");
}
@EventHandler
public void onFallingBlockToBlock(EntityChangeBlockEvent e){
if(e.getEntityType() == EntityType.FALLING_BLOCK){
Entity entity = e.getEntity();
Location loc = entity.getLocation();
Vector vel = entity.getVelocity();
Block movingTo = getBlockMovingTo(loc, vel);
if(movingTo != null && movingTo.getType() == Material.END_PORTAL){
Location spawnLoc = movingTo.getLocation();
spawnLoc.setX(spawnLoc.getX()+0.5);
spawnLoc.setY(spawnLoc.getY()+0.5);
spawnLoc.setZ(spawnLoc.getZ()+0.5);
FallingBlock dummy = loc.getWorld().spawnFallingBlock(spawnLoc, ((FallingBlock) entity).getBlockData());
Vector dummyVel = vel.clone();
dummyVel.setY(-dummyVel.getY());
dummyVel.multiply(new Vector(2, 2, 2));
dummyVel.add(new Vector(0, -0.2, 0));
dummy.setVelocity(dummyVel);
}
}
}
@EventHandler
public void onBlockBreak(BlockBreakEvent 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);
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;
}
return velocity;
}
Block getBlockMovingTo(Location loc, Vector vel){
double absMax = 0, max = 0;
char dir = ' ';
Block relative = null;
if(Math.abs(vel.getX()) > absMax){
max = vel.getX();
absMax = Math.abs(vel.getX());
dir = 'x';
}
if(Math.abs(vel.getY()) > absMax){
max = vel.getY();
absMax = Math.abs(vel.getY());
dir = 'y';
}
if(Math.abs(vel.getZ()) > absMax){
max = vel.getZ();
dir = 'z';
}
switch (dir) {
case 'x' -> relative = loc.getBlock().getRelative((int) Math.signum(max), 0, 0);
case 'y' -> relative = loc.getBlock().getRelative(0, (int) Math.signum(max), 0);
case 'z' -> relative = loc.getBlock().getRelative(0, 0, (int) Math.signum(max));
}
return relative;
}
}