again optimize

This commit is contained in:
MidnightTale 2023-04-12 18:55:46 +07:00
commit 4030fe04d1

View file

@ -1,7 +1,6 @@
package xyz.hynse.bbsdfia;
import com.tcoded.folialib.FoliaLib;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World;
@ -15,24 +14,22 @@ import org.bukkit.event.entity.EntityChangeBlockEvent;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.util.Vector;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class Bbsdfia extends JavaPlugin implements Listener {
private final Vector velocity1 = new Vector(0, 0.5, -1);
private final Vector velocity2 = new Vector(-1, 0.5, 0);
private final Vector velocity3 = new Vector(0, 0.5, 1);
private final Vector velocity4 = new Vector(1, 0.5, 0);
private final Vector[] velocities = { velocity1, velocity2, velocity3, velocity4 };
private final Vector[] velocities = {velocity1, velocity2, velocity3, velocity4};
private int counter = 0;
private ExecutorService executorService = Executors.newFixedThreadPool(4);
FoliaLib foliaLib = new FoliaLib(this);
/*
@Override
public void onEnable() {
super.onEnable();
getServer().getPluginManager().registerEvents(this, this);
getServer().getLogger().info("Bbsdfia plugin started");
}*/
@Override
public void onEnable() {
super.onEnable();
@ -44,111 +41,87 @@ public class Bbsdfia extends JavaPlugin implements Listener {
public void onDisable() {
super.onDisable();
getServer().getLogger().info("Bbsdfia plugin stopped");
executorService.shutdown();
}
/*@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 onFallingBlockToBlock(EntityChangeBlockEvent e) {
if (e.getEntityType() == EntityType.FALLING_BLOCK) {
Entity entity = e.getEntity();
Entity entity = e.getEntity();
if (entity.getType() != EntityType.FALLING_BLOCK || entity.getVelocity().length() == 0) {
return;
}
executorService.execute(() -> {
Location loc = entity.getLocation();
Vector vel = entity.getVelocity();
Block movingTo = getBlockMovingTo(loc, vel);
if (movingTo != null && movingTo.getType() == Material.END_PORTAL) {
if ( movingTo != null && movingTo.getType() == Material.END_PORTAL) {
foliaLib.getImpl().runAtLocation(loc, () -> {
Location spawnLoc = movingTo.getLocation();
spawnLoc.setX(spawnLoc.getX()+0.5);
spawnLoc.setY(spawnLoc.getY()+0.5);
spawnLoc.setZ(spawnLoc.getZ()+0.5);
Location spawnLoc = movingTo.getLocation().add(0.5, 0.5, 0.5);
FallingBlock firstBlock = 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));
firstBlock.setVelocity(dummyVel);
});
}
});
}
// Spawn the first falling block immediately
FallingBlock firstBlock = 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));
firstBlock.setVelocity(dummyVel);
@EventHandler
public void onEntityChangeBlock(EntityChangeBlockEvent event) {
Entity entity = event.getEntity();
if (!(entity instanceof FallingBlock) || entity.getWorld().getEnvironment() != World.Environment.THE_END) {
return;
}
byte data = ((FallingBlock) entity).getBlockData().getAsString().getBytes()[0];
Material material = ((FallingBlock) entity).getBlockData().getMaterial();
synchronized (this) {
counter++;
int index = counter % 4;
Vector velocity = velocities[index];
entity.remove();
fallingBlocks.add(new FallingBlockData(entity.getLocation(), material, data, velocity));
if (fallingBlocks.size() == 3) {
executorService.execute(() -> {
List<FallingBlockData> blocks = new ArrayList<>(fallingBlocks);
fallingBlocks.clear();
for (FallingBlockData blockData : blocks) {
Location location = blockData.getLocation();
Vector blockVelocity = blockData.getVelocity();
spawnFallingBlockWithVelocity(location, blockData.getMaterial(), blockData.getData(), blockVelocity);
}
});
}
}
}
@EventHandler
public void onEntityChangeBlock(EntityChangeBlockEvent event) {
Entity entity = event.getEntity();
if (!(entity instanceof FallingBlock)) {
return;
}
if (entity.getWorld().getEnvironment() != World.Environment.THE_END) {
return;
}
// Spawn a new falling block entity with velocity
entity.remove();
Location fuck = new Location(Bukkit.getWorld("world_the_end"), 100, 49 ,0);
fuck.getBlock().setType(Material.AIR);
World world = entity.getWorld();
Location location = entity.getLocation();
byte data = ((FallingBlock) entity).getBlockData().getAsString().getBytes()[0];
Material material = ((FallingBlock) entity).getBlockData().getMaterial();
int index = counter % 4;
Vector velocity = velocities[index];
counter++;
foliaLib.getImpl().runAtLocation(location, () -> {
FallingBlock newFallingBlock = world.spawnFallingBlock(location, material, data);
private void spawnFallingBlockWithVelocity(Location location, Material material, byte data, Vector velocity) {
FallingBlock newFallingBlock = location.getWorld().spawnFallingBlock(location, material, data);
newFallingBlock.setVelocity(velocity);
});
}
Block getBlockMovingTo(Location loc, Vector vel){
Block getBlockMovingTo(Location loc, Vector vel) {
double absMax = 0, max = 0;
char dir = ' ';
Block relative = null;
if(Math.abs(vel.getX()) > absMax){
if (Math.abs(vel.getX()) > absMax) {
max = vel.getX();
absMax = Math.abs(vel.getX());
dir = 'x';
}
if(Math.abs(vel.getY()) > absMax){
if (Math.abs(vel.getY()) > absMax) {
max = vel.getY();
absMax = Math.abs(vel.getY());
dir = 'y';
}
if(Math.abs(vel.getZ()) > absMax){
if (Math.abs(vel.getZ()) > absMax) {
max = vel.getZ();
dir = 'z';
}
@ -159,4 +132,36 @@ public class Bbsdfia extends JavaPlugin implements Listener {
}
return relative;
}
}
private static class FallingBlockData {
private final Location location;
private final Material material;
private final byte data;
private final Vector velocity;
public FallingBlockData(Location location, Material material, byte data, Vector velocity) {
this.location = location;
this.material = material;
this.data = data;
this.velocity = velocity;
}
public Location getLocation() {
return location;
}
public Material getMaterial() {
return material;
}
public byte getData() {
return data;
}
public Vector getVelocity() {
return velocity;
}
}
private final List<FallingBlockData> fallingBlocks = new ArrayList<>();
}