无法在我的世界中生成掉落的方块

Cannot spawn falling blocks in minecraft

所以我试图让 SEA_LANTERN 在它被放置时在其自身下方放置多个沙子,这样我就可以让我的大炮在开火时充满沙子,这是我的代码

主要

package me.zavage.sandbot;

import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.java.JavaPlugin;

import me.zavage.sandbot.commands.SandBotCommand;
import me.zavage.sandbot.listeners.SandBotListener;

public class Main extends JavaPlugin {
    
    @Override
    public void onEnable() {
        new SandBotListener(this);
        new SandBotCommand(this);
        
    }

    public static Plugin getInstance() {
        return null;
    }

}

package me.zavage.sandbot.listeners;

import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.entity.FallingBlock;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockPlaceEvent;
import org.bukkit.scheduler.BukkitTask;
import org.bukkit.util.Vector;

import me.zavage.sandbot.Main;

public class SandBotListener implements Listener {
    
    private BukkitTask task;
    private int keepToSpawn = 0;
    public SandBotListener(Main main) {

    }

    @EventHandler
    public void onPlaceSandbot(BlockPlaceEvent e) {
        Material spawnType = e.getBlockPlaced().getType(); // get placed block
        if(!spawnType.equals(Material.SEA_LANTERN)) // in your case, be sure it's sea lantern
            return;
        keepToSpawn = 5; // amount of spawned item
        Location loc = e.getBlock().getLocation(); // location where entity will spawn
        task = Bukkit.getScheduler().runTaskTimer(Main.getInstance(), () -> {
            // each 0.5s, we made spawn a falling block of given type
            run(loc, spawnType);
            if(keepToSpawn == 0)
                task.cancel();
            keepToSpawn--;
        }, 10, 10); // 10 ticks = 0.5 seconds
    }

    @SuppressWarnings("deprecation")
    private void run(Location loc, Material type) {
        FallingBlock falling = loc.getWorld().spawnFallingBlock(loc, Material.SAND, (byte) 0);
        falling.setDropItem(true);
        falling.setVelocity(new Vector(0, -0.5, 0)); // set the velicoty of the block
    }
}

编辑:我也更新了包括主要部分在内的代码 class 一定有什么我想念的,但我在这一点上尝试了很多不同的东西

这是一个示例代码,它将生成 5 次下落方块:

private BukkitTask task;
private int keepToSpawn = 0;

@EventHandler
public void onPlaceSandbot(BlockPlaceEvent e) {
    Material spawnType = e.getBlockPlaced().getType(); // get placed block
    if(!spawnType.equals(Material.SEA_LANTERN)) // in your case, be sure it's sea lantern
        return;
    keepToSpawn = 5; // amount of spawned item
    Location loc = e.getBlock().getLocation(); // location where entity will spawn
    task = Bukkit.getScheduler().runTaskTimer(Main.getInstance(), () -> {
        // each 0.5s, we made spawn a falling block of given type
        run(loc, spawnType);
        if(keepToSpawn == 0)
            task.cancel();
        keepToSpawn--;
    }, 10, 10); // 10 ticks = 0.5 seconds
}

@SuppressWarnings("deprecation")
private void run(Location loc, Material type) {
    FallingBlock falling = loc.getWorld().spawnFallingBlock(loc, type, (byte) 0);
    falling.setDropItem(true);
    falling.setVelocity(new Vector(0, -0.5, 0)); // set the velicoty of the block
}

不要使用Bukkit.getWorld(e.getBlockPlaced().getLocation().getWorld().getUID()),你已经有了e.getBlockPlaced().getWorld()的世界实例。

对于Vector用作速度,你应该设置方块的方向。我设置负 Y 使其下降(而不是 x/z 使其保持在他的块上)。

PS:不要忘记在 onEnable:

中使用此代码注册监听器
getServer().getPluginManager().registerEvents(new SandBotListener(this), this);

对于您的插件实例,您必须有一个由“this”表示的对象“Main”的实例。

要使用它,请在您的 Main.java :

private static Main instance; // create variable that is "static".
// It means it not depend to an object to get it
public static Main getInstance() { // get the object instance, so the plugin instance
   return instance;
}

@Override
public void onEnable() {
   instance = this; // here set the instance of this, so as the current object
 // it will make this object available for everyone
   getServer().getPluginManager().registerEvents(new SandBotListener(), this);
}

然后,你可以删除SandBotListener.java的构造函数。

现在,您可以使用 Main.getInstance()