Bukkit EntityExplode物理和块的再生

时间:2016-07-11 12:18:01

标签: minecraft bukkit

我在我的代码中遇到这个问题,当tnt或者爬行者爆炸时,块会飞到任何地方,但是当块已经着陆时,一切都应该重新生成,但它不会。谁有结论?

这是我的代码:

      @EventHandler
      void onExplode1(EntityExplodeEvent event) {

    for(Block block : event.blockList()) {

        if(block.getType() != Material.TNT) {
            float x = -0.4F + (float) (Math.random() * 0.9D);
            float y = -1.2F + (float) (Math.random() * 1.9D);
            float z = -0.9F + (float) (Math.random() * 1.4D);

            FallingBlock falling = block.getWorld().spawnFallingBlock(block.getLocation(), block.getType(), block.getData());
            falling.setVelocity(new Vector(x, y, z));
            falling.setDropItem(true);

            block.setType(Material.AIR);

        }

        if(block.getType() != Material.AIR) {

            final BlockState state = block.getState();

            int delay;

            if(block.getType().hasGravity()) {

                delay = 80;

            } else {

                delay = 80 + (int)(Math.random()*60);

            }

            // Set block to air so that no blocks drop
            block.setType(Material.AIR);

            // Regenerate all the blocks in a random order
            Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable() {

                public void run() {

                    state.update(true, false);

                }

            }, delay);

        }

    }

}
}

1 个答案:

答案 0 :(得分:0)

我认为您必须存储您创建的实体,然后返回位置...这是一个示例代码...它不必像其他人一样工作,但这是一次尝试。

package me.mcplayhd.testt;

import java.util.HashMap;
import java.util.Iterator;

import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.entity.FallingBlock;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityChangeBlockEvent;
import org.bukkit.event.entity.EntityDamageEvent;
import org.bukkit.event.entity.EntityDeathEvent;
import org.bukkit.event.entity.EntityExplodeEvent;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.util.Vector;

public class ExplodeBlocks extends JavaPlugin implements Listener {

    public void onEnable() {
        getServer().getPluginManager().registerEvents(this, this);
    }

    HashMap<FallingBlock, Location> blocks = new HashMap<FallingBlock, Location>();

    @SuppressWarnings("deprecation")
    @EventHandler
    void onEntityExplode(EntityExplodeEvent e) {
        Iterator<Block> bi = e.blockList().iterator();
        while(bi.hasNext()) {
            Block b = bi.next();
            if(b.getType() != Material.TNT) {
                bi.remove();

                Material m = b.getType();
                byte d = b.getData();

                b.setType(Material.AIR);

                float x = -0.4F + (float) (Math.random() * 0.9D);
                float y = -1.2F + (float) (Math.random() * 1.9D);
                float z = -0.9F + (float) (Math.random() * 1.4D);

                FallingBlock falling = b.getWorld().spawnFallingBlock(b.getLocation(), m, d);
                falling.setVelocity(new Vector(x, y, z));
                falling.setDropItem(false);

                blocks.put(falling, b.getLocation());
            }
        }
    }

    @EventHandler
    public void onEntityDamage(EntityDamageEvent e) {
        if(e.getEntity() instanceof FallingBlock) {
            FallingBlock fb = (FallingBlock) e.getEntity();
            if(blocks.containsKey(fb)) {
                e.setCancelled(true);
            }
        }
    }

    @SuppressWarnings("deprecation")
    @EventHandler
    public void onEneityDeath(EntityDeathEvent e) {
        if(e.getEntity() instanceof FallingBlock) {
            FallingBlock fb = (FallingBlock) e.getEntity();
            if(blocks.containsKey(fb)) {
                Location loc = blocks.get(fb);
                loc.getBlock().setType(fb.getMaterial());
                loc.getBlock().setData(fb.getBlockData());
                blocks.remove(fb);
                fb.remove();
            }
        }
    }

    @SuppressWarnings("deprecation")
    @EventHandler
    public void onEntityChangeBlock(EntityChangeBlockEvent e) {
        if(e.getEntity() instanceof FallingBlock) {
            FallingBlock fb = (FallingBlock) e.getEntity();
            if(blocks.containsKey(fb)) {
                e.setCancelled(true);
                Location loc = blocks.get(fb);
                loc.getBlock().setType(fb.getMaterial());
                loc.getBlock().setData(fb.getBlockData());
                blocks.remove(fb);
                fb.remove();
            }
        }
    }

}

问题是它不能100%工作,但大多数块都将被恢复。其他我不知道,但我想你会找到原因...

如果这根本没有帮助