将任何玩家下方的方块设置为发光石,然后将其设置回原始材质 [英] Setting the block underneath any player to glowstone, then setting it back to the original material

查看:58
本文介绍了将任何玩家下方的方块设置为发光石,然后将其设置回原始材质的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个Bukkit插件,它将任何玩家下方的方块更改为发光石。

I'm making a Bukkit plugin that will change the block underneath any player to glowstone.

调度程序无法正常工作。地面将变为发光石,但发光石块不会恢复到原来的状态。

The scheduler isn't working. The ground will change to glowstone but the glowstone block won't revert back to what it originally was.

@EventHandler
public void onStep(PlayerMoveEvent pme) {
    Player player = pme.getPlayer();
    Location locUnderPlayer = player.getLocation();
    locUnderPlayer.setY(locUnderPlayer.getY() - 1);
    Location locForScheduler = player.getLocation();
    locForScheduler.setY(locForScheduler.getY() + 1);
    final Material materialForScheduler = locForScheduler.getBlock().getType();
    Block block = locUnderPlayer.getBlock();
    Material m = player.getItemInHand().getType();
    if (m == Material.GLOWSTONE) {
        if (block.getType() != Material.AIR && block.getType() != Material.WATER && block.getType() != Material.STATIONARY_WATER && block.getType() != Material.LAVA && block.getType() != Material.STATIONARY_LAVA && block.getType() != Material.REDSTONE_WIRE && block.getType() != Material.REDSTONE_COMPARATOR && block.getType() != Material.REDSTONE_TORCH_ON && block.getType() != Material.REDSTONE_TORCH_OFF) {
            block.setType(Material.GLOWSTONE);
            Bukkit.getScheduler().scheduleSyncDelayedTask(Magic.getInstance(), new Runnable() {
                public void run() {
                    block.setType(materialForScheduler);
                }
            }, 1 * 10);
        }
    }
}


推荐答案

要获得玩家下方的方块,您可以使用:

To get the block underneath a player, you could use:

Block block = player.getLocation().subtract(0, 1, 0).getBlock();

这将获得玩家的位置,并从Y轴减去 1

Which gets the player's location, subtracts 1 from the Y-axis, and then gets the block (hence getting the block under the player).

然后获取类型,可以使用 block.getType()

Then to get the type, you could use block.getType():

Material type = block.getType();

要将块设置为发光石,可以使用 block.setType(Material )

To set the block to glowstone, you could use block.setType(Material):

block.setType(Material.GLOWSTONE);

因此,如果您想将玩家下方的方块设置为发光石,则立即将其恢复为原始块,您可以使用:

So, if you wanted to set the block under a player to glowstone, then immediately turn it back to the original block, you could use:

Block block = player.getLocation().subtract(0, 1, 0).getBlock(); //get the block
Material type = block.getType(); //get the block's type
block.setType(Material.GLOWSTONE); //set the block's material to glowstone
block.setType(type); //set the block's material back to the original material

但是要在将图块设置为发光石,然后将其设置回原始块(在您的情况下,延迟为 10个滴答声),您可以使用 Runnable 任务:

But to have a delay between setting the block to glowstone then setting it back to the original block (in your case, the delay is 10 ticks) you could use a Runnable task:

final Block block = player.getLocation().subtract(0, 1, 0).getBlock(); //get the block
final Material type = block.getType(); //get the block's type

block.setType(Material.GLOWSTONE); //set the block's material to glowstone

Bukkit.getScheduler().runTaskLater(Magic.getInstance(), new Runnable(){
  public void run(){
    block.setType(type); //set the block back to the original block
  }
},10L);

我们要确保玩家下方的方块尚未发光,以确保块更改不会成为永久更改。可以使用以下简单方式完成此操作:

We would want to make sure that the block beneath the player is not already glowstone, to insure that the block change does not become permanent. This could be done using simply:

if(!type.equals(Material.GLOWSTONE))

如果我们不检查这一点,那么玩家可以用手拿着发光石移动,从而将其下方的方块设置为发光石,然后启动计时器以将其设置回原始块。但是,如果玩家在计时器处于会话状态时移动(例如,他们最后一次移动5个滴答声之后),则其下方的方块将永久变为发光石。

If we did not check for this, then a player could move with glowstone in their hand, hence setting the block underneath them to glowstone, and then starting a timer to set it back to the original block. But, if the player moves while the timer is in session (for example, 5 ticks after they last moved), the block underneath them would permanently change to glowstone.

所以,您的代码可能看起来像这样:

So, your code could look something like this:

@EventHandler
public void onStep(PlayerMoveEvent pme) {
  Player player = pme.getPlayer(); //get the player in the event
  final Block block = player.getLocation().subtract(0, 1, 0).getBlock(); //get the block
  final Material type = block.getState().getType(); //get the block's material
  if(!type.equals(Material.GLOWSTONE)){//don't change the block if it's already glowstone
    if(player.getItemInHand() != null){//make sure the item in the player's hand is not null, to avoid a null pointer
      Material m = player.getItemInHand().getType(); //get the item in the player's hand
      if(m == Material.GLOWSTONE){ //check if the item in the player's hand is glowstone
        if(type != Material.AIR && type != Material.WATER && type != Material.STATIONARY_WATER && type != Material.LAVA && type != Material.STATIONARY_LAVA && type != Material.REDSTONE_WIRE && type != Material.REDSTONE_COMPARATOR && type != Material.REDSTONE_TORCH_ON && type != Material.REDSTONE_TORCH_OFF){
          block.setType(Material.GLOWSTONE);//set the block type to glowstone
          Bukkit.getScheduler().runTaskLater(Magic.getInstance(), new Runnable() {
            public void run(){
              block.setType(type); //set the block type back to the original type
            }
          },10L);
        }
      }
    }
  }
}

此外,如果语句,您可以减少此

Also, you could reduce this if statement:

if(type != Material.AIR && type != Material.WATER && type != Material.STATIONARY_WATER && type != Material.LAVA && type != Material.STATIONARY_LAVA && type != Material.REDSTONE_WIRE && type != Material.REDSTONE_COMPARATOR && type != Material.REDSTONE_TORCH_ON && type != Material.REDSTONE_TORCH_OFF)

只需 type.isSolid()

if(type.isSolid())

这篇关于将任何玩家下方的方块设置为发光石,然后将其设置回原始材质的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆