1

在第 3 行的这段代码中,我不断收到“操作符 == 未定义参数类型 boolean、int”:

    public void loadState(int i)
  {
      if (statesSaved[i] == 0)
      {
        return;
      }
      List list = TMIUtils.getMinecraft().h.at.e;
      for (int j = 0; j < 44; j++)
        {
          sx slot = (sx)list.get(j + 1);
          slot.c(null);
          ul itemstack = TMIUtils.copyStack(states[i][j]);
          if ((itemstack == null) || (itemstack.c < 0) || (itemstack.c >= sv.f.length) || (sv.f[itemstack.c] == null))
            continue;
          slot.c(itemstack);
    }
  }

我不知道为什么会发生这种情况,因为我有另一个类文件,其中没有错误。

如果有帮助,这是我的完整课程文件:

import java.io.PrintStream;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import net.minecraft.client.Minecraft;

public class TMIConfig
{
  public static final String VERSION = "1.7.2 2011-07-01";
  public static final int NUM_SAVES = 7;
  public static final int INVENTORY_SulE = 44;
  public static boolean isModloaderEnabled = false;
  private static TMIConfig instance;
  private static List items = new ArrayList();
  private static HashSet toolIds;
  private static HashSet nonUnlimitedIds;
  private Map settings;
  private static ul[][] states = new ul[7][44];
  private static boolean[] statesSaved = new boolean[7];
  private static HashSet excludeIds;

  public TMIConfig()
  {
    this.settings = new LinkedHashMap();
    this.settings.put("enable", "true");
    this.settings.put("enablemp", "false");
    this.settings.put("give-command", "/give {0} {1} {2}");
    for (int i = 0; i < getNumSaves(); i++)
    {
      this.settings.put(new StringBuilder().append("save-name").append(i + 1).toString(), new StringBuilder().append("").append(i + 1).toString());
    }

    for (int j = 0; j < getNumSaves(); j++)
    {
      this.settings.put(new StringBuilder().append("save").append(j + 1).toString(), "");
    }

    instance = this;
  }

  public static boolean isMultiplayer()
  {
    return TMIUtils.getMinecraft().f.I;
  }

  public static TMIConfig getInstance()
  {
    if (instance == null)
    {
      new TMIConfig();
    }
    return instance;
  }

  public Map getSettings()
  {
    return this.settings;
  }

  public List getItems()
  {
    return items;
  }

  public int getNumSaves()
  {
    return 7;
  }

  public boolean isStateSaved(int i)
  {
    return statesSaved[i];
  }

  public ul[] getState(int i)
  {
    return states[i];
  }

  public boolean getBooleanSetting(String s)
  {
    return Boolean.parseBoolean((String)this.settings.get(s));
  }

  public boolean isEnabled()
  {
    return ((isMultiplayer()) && (getBooleanSetting("enablemp"))) || ((!isMultiplayer()) && (getBooleanSetting("enable")));
  }

  public void toggleEnabled()
  {
    String s = isMultiplayer() ? "enablemp" : "enable";
    this.settings.put(s, Boolean.toString(!getBooleanSetting(s)));
  }

  public void setEnabled(boolean flag)
  {
    String s = isMultiplayer() ? "enablemp" : "enable";
    this.settings.put(s, Boolean.toString(flag));
  }

  public static boolean isItemIncluded(int i)
  {
    return !excludeIds.contains(Integer.valueOf(i));
  }

  public static boolean isTool(sv item)
  {
    return toolIds.contains(Integer.valueOf(item.br));
  }

  public static boolean canItemBeUnlimited(sv item)
  {
    return !nonUnlimitedIds.contains(Integer.valueOf(item.br));
  }

  public boolean areDamageVariantsShown()
  {
    if (isMultiplayer())
    {
      String s = (String)getSettings().get("give-command");
      return s.contains("{3}");
    }

    return true;
  }

  public void clearState(int i)
  {
    for (int j = 0; j < 44; j++)
    {
      states[i][j] = null;
      statesSaved[i] = false;
    }

    this.settings.put(new StringBuilder().append("save").append(i + 1).toString(), "");
  }

  public void loadState(int i)
  {
      if (statesSaved[i] == 0)
      {
        return;
      }
      List list = TMIUtils.getMinecraft().h.at.e;
      for (int j = 0; j < 44; j++)
        {
          sx slot = (sx)list.get(j + 1);
          slot.c(null);
          ul itemstack = TMIUtils.copyStack(states[i][j]);
          if ((itemstack == null) || (itemstack.c < 0) || (itemstack.c >= sv.f.length) || (sv.f[itemstack.c] == null))
            continue;
          slot.c(itemstack);
    }
  }

  public void saveState(int i)
  {
    List list = TMIUtils.getMinecraft().h.at.e;
    for (int j = 0; j < 44; j++)
    {
      states[i][j] = TMIUtils.copyStack(((sx)list.get(j + 1)).a());
    }

    this.settings.put(new StringBuilder().append("save").append(i + 1).toString(), encodeState(i));
    statesSaved[i] = true;
  }

  public String encodeState(int i)
  {
    StringBuilder stringbuilder = new StringBuilder();
    for (int j = 0; j < 44; j++)
    {
      if (states[i][j] != null)
      {
        stringbuilder.append(states[i][j].c);
        stringbuilder.append(":");
        stringbuilder.append(states[i][j].a);
        stringbuilder.append(":");
        stringbuilder.append(states[i][j].i());
      }
      stringbuilder.append(",");
    }

    return stringbuilder.toString();
  }

  public void decodeState(int i, String s)
  {
    if (s.trim().equals(""))
    {
      statesSaved[i] = false;
    }
    else {
      String[] as = s.split(",", 0);
      for (int j = 0; (j < as.length) && (j < states[i].length); j++)
      {
        String[] as1 = as[j].split(":");
        if (as1.length != 3)
        {
          continue;
        }
        try
        {
          states[i][j] = new ul(Integer.parseInt(as1[0]), Integer.parseInt(as1[1]), Integer.parseInt(as1[2]));
        }
        catch (Exception exception)
        {
          System.out.println(exception);
        }
      }

      statesSaved[i] = true;
    }
  }

  static
  {
    toolIds = new HashSet();
    for (int i = 1; i <= 3; i++)
    {
      toolIds.add(Integer.valueOf(i + 256));
    }

    for (int j = 11; j <= 23; j++)
    {
      toolIds.add(Integer.valueOf(j + 256));
    }

    for (int k = 27; k <= 30; k++)
    {
      toolIds.add(Integer.valueOf(k + 256));
    }

    for (int l = 34; l <= 38; l++)
    {
    toolIds.add(Integer.valueOf(l + 256));
    }

    for (int m = 42; m <= 61; m++)
    {
    toolIds.add(Integer.valueOf(m + 256));
    }

    toolIds.add(Integer.valueOf(359));
    toolIds.add(Integer.valueOf(346));

    nonUnlimitedIds = new HashSet();
    nonUnlimitedIds.add(Integer.valueOf(358));
    excludeIds = new HashSet();
    excludeIds.add(Integer.valueOf(9));
    excludeIds.add(Integer.valueOf(11));
    excludeIds.add(Integer.valueOf(63));
    excludeIds.add(Integer.valueOf(64));
    excludeIds.add(Integer.valueOf(68));
    excludeIds.add(Integer.valueOf(71));
    excludeIds.add(Integer.valueOf(74));
    excludeIds.add(Integer.valueOf(75));
    excludeIds.add(Integer.valueOf(59));
    excludeIds.add(Integer.valueOf(83));
    excludeIds.add(Integer.valueOf(55));
    excludeIds.add(Integer.valueOf(26));
    excludeIds.add(Integer.valueOf(93));
    excludeIds.add(Integer.valueOf(94));
    try
    {
      Class.forName("mod_RedPowerWiring");
      excludeIds.add((Integer)ModLoader.getPrivateValue(mod_RedPowerWiring.class, null, "InsWireID"));
      excludeIds.add((Integer)ModLoader.getPrivateValue(mod_RedPowerWiring.class, null, "CableID"));
    }
    catch (Exception exception)
    {
    }
  }
}
4

2 回答 2

8

Java 与 C 不同,C 允许在某些情况下将变量视为不同的类型有很大的自由度,它在允许您执行的操作方面更加严格。您的两条有问题的行是:

private static boolean[] statesSaved = new boolean[7];
if (statesSaved[i] == 0)

您需要将最后一个重新编码为:

if (!statesSaved[i])

而且,如果您重视那些将维护您的代码的人的理智,请不要使用以下内容:

if (statesSaved[i] == false)

永远不需要明确比较布尔值。您应该选择智能名称,例如hasSavedStateorisBroken这样的表达式:

if (isBroken)

或者:

while (!finished)

用英语说得通。这将大大提高代码的可读性。

无论如何,由于布尔值的显式比较会导致另一个布尔值,你会在哪里停下来。减少荒谬的历史悠久的传统需要如下代码:

if (((finished == true) == true) == true) ...
于 2011-10-12T00:26:33.853 回答
4

正如错误消息所述,您的代码毫无意义;你不能写if (boolean == int)

你可能是说if (statesSaved[i] == false)

于 2011-10-12T00:18:57.957 回答