尝试制作if语句时的Bracket Error?

时间:2015-01-14 21:53:03

标签: bukkit

我一直在为Minecraft制作插件,但这篇文章并不重要。我的代码似乎有足够的括号,但只有删除括号后才能工作,然后代码将始终运行我不想要的。这是我的代码 -

package me.impatheimpaler.soundpickup;

import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.Sound;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerPickupItemEvent;
import org.bukkit.plugin.java.JavaPlugin;

public class SoundPickups extends JavaPlugin implements Listener{


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

public void onDisable() {

}

 @EventHandler(priority=EventPriority.LOWEST)
  public void MoneyPickUp(PlayerPickupItemEvent event)  {
  if {
      (event.getItem().getItemStack().getType() == Material.GOLD_INGOT);
      event.getPlayer().sendMessage(ChatColor.GOLD + "+" + ChatColor.GOLD +       event.getItem().getItemStack().getAmount() + ChatColor.GOLD + "" + ChatColor.BOLD + "G");
      event.getPlayer().playSound(event.getPlayer().getLocation(), Sound.ORB_PICKUP, 1, 1);

 }

 }
}

1 个答案:

答案 0 :(得分:2)

您的错误在

@EventHandler(priority=EventPriority.LOWEST)
public void MoneyPickUp(PlayerPickupItemEvent event)  {
if {
  (event.getItem().getItemStack().getType() == Material.GOLD_INGOT);
    event.getPlayer().sendMessage(ChatColor.GOLD + "+" + ChatColor.GOLD + event.getItem().getItemStack().getAmount() + ChatColor.GOLD + "" + ChatColor.BOLD + "G");
    event.getPlayer().playSound(event.getPlayer().getLocation(), Sound.ORB_PICKUP, 1, 1);
  }
}

如果您要使用带有if的括号,则在if语句后立即打开括号:

if(conditional) {
//My code here
}

如果你删除括号它可以工作,但是你的代码没有正确执行,因为你在条件

之后放了一个分号;

将您的代码更改为:

@EventHandler(priority=EventPriority.LOWEST)
public void MoneyPickUp(PlayerPickupItemEvent event) {
  if (event.getItem().getItemStack().getType() == Material.GOLD_INGOT) {
    event.getPlayer().sendMessage(ChatColor.GOLD + "+" + ChatColor.GOLD + event.getItem().getItemStack().getAmount() + ChatColor.GOLD + "" + ChatColor.BOLD + "G");
    event.getPlayer().playSound(event.getPlayer().getLocation(), Sound.ORB_PICKUP, 1, 1);
  }
}