again optimize
This commit is contained in:
parent
f33863b5b3
commit
4030fe04d1
1 changed files with 85 additions and 80 deletions
|
|
@ -1,7 +1,6 @@
|
||||||
package xyz.hynse.bbsdfia;
|
package xyz.hynse.bbsdfia;
|
||||||
|
|
||||||
import com.tcoded.folialib.FoliaLib;
|
import com.tcoded.folialib.FoliaLib;
|
||||||
import org.bukkit.Bukkit;
|
|
||||||
import org.bukkit.Location;
|
import org.bukkit.Location;
|
||||||
import org.bukkit.Material;
|
import org.bukkit.Material;
|
||||||
import org.bukkit.World;
|
import org.bukkit.World;
|
||||||
|
|
@ -15,24 +14,22 @@ 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;
|
||||||
|
|
||||||
|
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 {
|
public class Bbsdfia extends JavaPlugin implements Listener {
|
||||||
private final Vector velocity1 = new Vector(0, 0.5, -1);
|
private final Vector velocity1 = new Vector(0, 0.5, -1);
|
||||||
private final Vector velocity2 = new Vector(-1, 0.5, 0);
|
private final Vector velocity2 = new Vector(-1, 0.5, 0);
|
||||||
private final Vector velocity3 = new Vector(0, 0.5, 1);
|
private final Vector velocity3 = new Vector(0, 0.5, 1);
|
||||||
private final Vector velocity4 = new Vector(1, 0.5, 0);
|
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 int counter = 0;
|
||||||
|
private ExecutorService executorService = Executors.newFixedThreadPool(4);
|
||||||
|
|
||||||
FoliaLib foliaLib = new FoliaLib(this);
|
FoliaLib foliaLib = new FoliaLib(this);
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
@Override
|
|
||||||
public void onEnable() {
|
|
||||||
super.onEnable();
|
|
||||||
getServer().getPluginManager().registerEvents(this, this);
|
|
||||||
getServer().getLogger().info("Bbsdfia plugin started");
|
|
||||||
}*/
|
|
||||||
@Override
|
@Override
|
||||||
public void onEnable() {
|
public void onEnable() {
|
||||||
super.onEnable();
|
super.onEnable();
|
||||||
|
|
@ -44,111 +41,87 @@ public class Bbsdfia extends JavaPlugin implements Listener {
|
||||||
public void onDisable() {
|
public void onDisable() {
|
||||||
super.onDisable();
|
super.onDisable();
|
||||||
getServer().getLogger().info("Bbsdfia plugin stopped");
|
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
|
@EventHandler
|
||||||
public void onFallingBlockToBlock(EntityChangeBlockEvent e) {
|
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();
|
Location loc = entity.getLocation();
|
||||||
Vector vel = entity.getVelocity();
|
Vector vel = entity.getVelocity();
|
||||||
Block movingTo = getBlockMovingTo(loc, vel);
|
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, () -> {
|
foliaLib.getImpl().runAtLocation(loc, () -> {
|
||||||
Location spawnLoc = movingTo.getLocation();
|
Location spawnLoc = movingTo.getLocation().add(0.5, 0.5, 0.5);
|
||||||
spawnLoc.setX(spawnLoc.getX()+0.5);
|
FallingBlock firstBlock = loc.getWorld().spawnFallingBlock(spawnLoc, ((FallingBlock) entity).getBlockData());
|
||||||
spawnLoc.setY(spawnLoc.getY()+0.5);
|
Vector dummyVel = vel.clone();
|
||||||
spawnLoc.setZ(spawnLoc.getZ()+0.5);
|
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
|
@EventHandler
|
||||||
FallingBlock firstBlock = loc.getWorld().spawnFallingBlock(spawnLoc, ((FallingBlock) entity).getBlockData());
|
public void onEntityChangeBlock(EntityChangeBlockEvent event) {
|
||||||
Vector dummyVel = vel.clone();
|
Entity entity = event.getEntity();
|
||||||
dummyVel.setY(-dummyVel.getY());
|
if (!(entity instanceof FallingBlock) || entity.getWorld().getEnvironment() != World.Environment.THE_END) {
|
||||||
dummyVel.multiply(new Vector(2, 2, 2));
|
return;
|
||||||
dummyVel.add(new Vector(0, -0.2, 0));
|
}
|
||||||
firstBlock.setVelocity(dummyVel);
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void spawnFallingBlockWithVelocity(Location location, Material material, byte data, Vector velocity) {
|
||||||
|
FallingBlock newFallingBlock = location.getWorld().spawnFallingBlock(location, material, data);
|
||||||
|
|
||||||
|
|
||||||
@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);
|
|
||||||
newFallingBlock.setVelocity(velocity);
|
newFallingBlock.setVelocity(velocity);
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Block getBlockMovingTo(Location loc, Vector vel) {
|
||||||
Block getBlockMovingTo(Location loc, Vector vel){
|
|
||||||
double absMax = 0, max = 0;
|
double absMax = 0, max = 0;
|
||||||
char dir = ' ';
|
char dir = ' ';
|
||||||
Block relative = null;
|
Block relative = null;
|
||||||
if(Math.abs(vel.getX()) > absMax){
|
if (Math.abs(vel.getX()) > absMax) {
|
||||||
max = vel.getX();
|
max = vel.getX();
|
||||||
absMax = Math.abs(vel.getX());
|
absMax = Math.abs(vel.getX());
|
||||||
dir = 'x';
|
dir = 'x';
|
||||||
}
|
}
|
||||||
if(Math.abs(vel.getY()) > absMax){
|
if (Math.abs(vel.getY()) > absMax) {
|
||||||
max = vel.getY();
|
max = vel.getY();
|
||||||
absMax = Math.abs(vel.getY());
|
absMax = Math.abs(vel.getY());
|
||||||
dir = 'y';
|
dir = 'y';
|
||||||
}
|
}
|
||||||
if(Math.abs(vel.getZ()) > absMax){
|
if (Math.abs(vel.getZ()) > absMax) {
|
||||||
max = vel.getZ();
|
max = vel.getZ();
|
||||||
dir = 'z';
|
dir = 'z';
|
||||||
}
|
}
|
||||||
|
|
@ -159,4 +132,36 @@ public class Bbsdfia extends JavaPlugin implements Listener {
|
||||||
}
|
}
|
||||||
return relative;
|
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<>();
|
||||||
}
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue