This commit is contained in:
MidnightTale 2023-04-11 21:28:26 +07:00
commit 327aa217dc
2 changed files with 36 additions and 67 deletions

View file

@ -15,10 +15,15 @@ repositories {
name = "sonatype" name = "sonatype"
url = "https://oss.sonatype.org/content/groups/public/" url = "https://oss.sonatype.org/content/groups/public/"
} }
maven {
name = "devmart-other"
url = "https://nexuslite.gcnt.net/repos/other/"
}
} }
dependencies { dependencies {
compileOnly "dev.folia:folia-api:1.19.4-R0.1-SNAPSHOT" compileOnly "dev.folia:folia-api:1.19.4-R0.1-SNAPSHOT"
implementation "com.tcoded:FoliaLib:0.2.0"
} }
def targetJavaVersion = 17 def targetJavaVersion = 17

View file

@ -1,11 +1,10 @@
package xyz.hynse.bbsdfia; package xyz.hynse.bbsdfia;
import com.tcoded.folialib.*;
import org.bukkit.Bukkit; 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.block.Block; import org.bukkit.block.Block;
import org.bukkit.block.data.BlockData;
import org.bukkit.entity.Entity; import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType; import org.bukkit.entity.EntityType;
import org.bukkit.entity.FallingBlock; import org.bukkit.entity.FallingBlock;
@ -16,113 +15,78 @@ import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.util.Vector; import org.bukkit.util.Vector;
public class Bbsdfia extends JavaPlugin implements Listener { public class Bbsdfia extends JavaPlugin implements Listener {
// This method is called when the plugin is enabled
@Override @Override
public void onEnable() { public void onEnable() {
super.onEnable(); super.onEnable();
// Get a reference to the world_the_end world
// Register the plugin to listen to events
getServer().getPluginManager().registerEvents(this, this); getServer().getPluginManager().registerEvents(this, this);
// Log that the plugin has started
getServer().getLogger().info("Bbsdfia plugin started"); getServer().getLogger().info("Bbsdfia plugin started");
} }
// This method is called when the plugin is disabled
@Override @Override
public void onDisable() { public void onDisable() {
super.onDisable(); super.onDisable();
// Log that the plugin has stopped
getServer().getLogger().info("Bbsdfia plugin stopped"); getServer().getLogger().info("Bbsdfia plugin stopped");
} }
// This method is called when a falling block entity changes its block
// Register the method as an event handler for the EntityChangeBlockEvent
@EventHandler @EventHandler
public void onFallingBlockToBlock(EntityChangeBlockEvent e) { public void onFallingBlockToBlock(EntityChangeBlockEvent e){
// Check if the entity is a falling block if(e.getEntityType() == EntityType.FALLING_BLOCK){
if (e.getEntityType() == EntityType.FALLING_BLOCK) {
// Get the entity and its location and velocity
Entity entity = e.getEntity(); Entity entity = e.getEntity();
Location loc = entity.getLocation(); Location loc = entity.getLocation();
Vector vel = entity.getVelocity(); Vector vel = entity.getVelocity();
// Get the block that the entity is moving towards
Block movingTo = getBlockMovingTo(loc, vel); Block movingTo = getBlockMovingTo(loc, vel);
// If the block it's moving towards is an end portal block Location loc2 = new Location(Bukkit.getWorld("world_the_end"), 100, 50, 0);
if (movingTo != null && movingTo.getType() == Material.END_PORTAL) { FoliaLib foliaLib = new FoliaLib(this);
try {
// Spawn a new falling block entity in the end
World endWorld = Bukkit.getWorld("world_the_end");
World currentWorld = entity.getWorld();
Location location = new Location(endWorld, 100, 50, 0);
getLogger().info("Current world: " + currentWorld.getName());
getLogger().info("End world: " + endWorld.getName());
getLogger().info("New location: " + "World/" + location.getWorld() + " (X:" + location.getX() + " Y:" + location.getY() + " Z:" + location.getZ() + ")");
World world = endWorld;
getLogger().info("XoaN " + "world=" + world.getName() );
Location blockLocation = location;
getLogger().info("XoaN " + "blockLocation=" + blockLocation.toString());
BlockData blockData = e.getBlock().getBlockData();
getLogger().info("XoaN " + "blockData=" + blockData.getAsString());
Material material = blockData.getMaterial();
getLogger().info("XoaN " + "material=" + material.name());
BlockData fallingBlockData = material.createBlockData();
getLogger().info("XoaN " + "fallingBlockData=" + fallingBlockData.getAsString());
FallingBlock fallingBlock = world.spawnFallingBlock(blockLocation, fallingBlockData);
getLogger().info("XoaN " + "fallingBlock=" + fallingBlock.getUniqueId().toString());
/*
getLogger().info("XoaT " + endWorld.spawnEntity(location, EntityType.FALLING_BLOCK));
getLogger().info("XoaN " + endWorld.spawnFallingBlock(location, e.getBlock().getBlockData().getMaterial().createBlockData()));
FallingBlock fallingBlock = endWorld.spawnFallingBlock(location, e.getBlock().getBlockData().getMaterial().createBlockData()); if(movingTo != null && movingTo.getType() == Material.END_PORTAL){
FallingBlock fallingBlock = (FallingBlock) endWorld.spawnEntity(location, EntityType.FALLING_BLOCK); /*
World world = Bukkit.getServer().getWorld("world");
fallingBlock.setVelocity(vel); Location spawnLoc = new Location(world, 100, 50, 0);
*/ */
} catch (Exception ex) { //Entity Scheduler Task
getLogger().severe("Error spawning falling block entity: " + ex.getMessage()); foliaLib.getImpl().runAtEntity(entity, () -> {
ex.printStackTrace(); //spawn new falling block in the end dimension and have same properties entity type and material same form entity that detect near end portal
} FallingBlock dummy = (FallingBlock) loc2.getWorld().spawnEntity(loc2, EntityType.FALLING_BLOCK);
Vector dummyVel = vel.clone();
dummy.setVelocity(dummyVel);
//velocity to north
dummy.setVelocity(new Vector(0, 1, 0));
//velocity to south
dummy.setVelocity(new Vector(0, -1, 0));
//velocity to east
dummy.setVelocity(new Vector(1, 0, 0));
//velocity to west
dummy.setVelocity(new Vector(-1, 0, 0));
});
} }
} }
} }
Block getBlockMovingTo(Location loc, Vector vel){
// This function returns the block that the entity is moving towards
Block getBlockMovingTo(Location loc, Vector vel) {
// Initialize variables to keep track of the maximum absolute velocity and its direction
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){
// Check which component of the velocity vector has the highest magnitude
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';
} }
// Use the direction with the highest magnitude to determine the block the entity is moving towards
switch (dir) { switch (dir) {
case 'x' -> relative = loc.getBlock().getRelative((int) Math.signum(max), 0, 0); 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 'y' -> relative = loc.getBlock().getRelative(0, (int) Math.signum(max), 0);
case 'z' -> relative = loc.getBlock().getRelative(0, 0, (int) Math.signum(max)); case 'z' -> relative = loc.getBlock().getRelative(0, 0, (int) Math.signum(max));
} }
// Return the block the entity is moving towards
return relative; return relative;
} }
} }