This commit is contained in:
MidnightTale 2023-04-12 20:01:55 +07:00
commit 8e6c4a2371

View file

@ -1,6 +1,7 @@
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;
@ -14,10 +15,7 @@ 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.concurrent.TimeUnit;
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);
@ -26,84 +24,72 @@ public class Bbsdfia extends JavaPlugin implements Listener {
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);
@Override @Override
public void onEnable() { public void onEnable() {
super.onEnable(); super.onEnable();
getServer().getPluginManager().registerEvents(this, this); Bukkit.getPluginManager().registerEvents(this, this);
getServer().getLogger().info("Bbsdfia plugin started"); Bukkit.getLogger().info("Bbsdfia plugin started");
} }
@Override @Override
public void onDisable() { public void onDisable() {
super.onDisable(); super.onDisable();
getServer().getLogger().info("Bbsdfia plugin stopped"); Bukkit.getLogger().info("Bbsdfia plugin stopped");
executorService.shutdown();
} }
@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, () -> { 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);
spawnLoc.setZ(spawnLoc.getZ() + 0.5);
// Spawn the first falling block immediately
FallingBlock overworldfallingblock = loc.getWorld().spawnFallingBlock(spawnLoc, ((FallingBlock) entity).getBlockData());
Vector dummyVel = vel.clone(); Vector dummyVel = vel.clone();
dummyVel.setY(-dummyVel.getY()); dummyVel.setY(-dummyVel.getY());
dummyVel.multiply(new Vector(2, 2, 2)); dummyVel.multiply(new Vector(2, 2, 2));
dummyVel.add(new Vector(0, -0.2, 0)); dummyVel.add(new Vector(0, -0.2, 0));
firstBlock.setVelocity(dummyVel); overworldfallingblock.setVelocity(dummyVel);
}); Location fuck = new Location(Bukkit.getWorld("world_the_end"), 100, 49, 0);
fuck.getBlock().setType(Material.AIR);
}
} }
});
} }
@EventHandler @EventHandler
public void onEntityChangeBlock(EntityChangeBlockEvent event) { public void onEntityChangeBlock(EntityChangeBlockEvent event) {
Entity entity = event.getEntity(); Entity entity = event.getEntity();
if (!(entity instanceof FallingBlock) || entity.getWorld().getEnvironment() != World.Environment.THE_END) { if (!(entity instanceof FallingBlock)) {
return;
}
if (entity.getWorld().getEnvironment() != World.Environment.THE_END) {
return; 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]; byte data = ((FallingBlock) entity).getBlockData().getAsString().getBytes()[0];
Material material = ((FallingBlock) entity).getBlockData().getMaterial(); Material material = ((FallingBlock) entity).getBlockData().getMaterial();
synchronized (this) {
counter++;
int index = counter % 4; int index = counter % 4;
Vector velocity = velocities[index]; Vector velocity = velocities[index];
entity.remove(); counter++;
fallingBlocks.add(new FallingBlockData(entity.getLocation(), material, data, velocity)); FallingBlock newFallingBlock = world.spawnFallingBlock(location, material, data);
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);
newFallingBlock.setVelocity(velocity); newFallingBlock.setVelocity(velocity);
} }
@ -132,36 +118,4 @@ 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<>();
} }