PageRenderTime 24ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/src/main/java/bspkrs/util/BlockID.java

https://github.com/bspkrs-mods/bspkrsCore
Java | 154 lines | 126 code | 27 blank | 1 comment | 38 complexity | 1d53b2f2ee23c27e308ddca8a5358822 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception
  1. package bspkrs.util;
  2. import net.minecraft.block.Block;
  3. import net.minecraft.block.state.IBlockState;
  4. import net.minecraft.init.Blocks;
  5. import net.minecraft.util.math.BlockPos;
  6. import net.minecraft.world.World;
  7. import net.minecraftforge.fml.common.registry.ForgeRegistries;
  8. @Deprecated
  9. public class BlockID
  10. {
  11. public final String id;
  12. public final int metadata;
  13. public BlockID(String id, int metadata)
  14. {
  15. this.id = id;
  16. this.metadata = metadata;
  17. }
  18. public BlockID(String block)
  19. {
  20. this(block, -1);
  21. }
  22. public BlockID(Block block, int metadata)
  23. {
  24. this(ForgeRegistries.BLOCKS.getKey(block).toString(), metadata);
  25. }
  26. public BlockID(Block block)
  27. {
  28. this(ForgeRegistries.BLOCKS.getKey(block).toString(), -1);
  29. }
  30. public BlockID(Block block, IBlockState state)
  31. {
  32. this(block, block.getMetaFromState(state));
  33. }
  34. public BlockID(IBlockState state)
  35. {
  36. this(state.getBlock(), state);
  37. }
  38. public BlockID(World world, BlockPos pos)
  39. {
  40. this(world.getBlockState(pos));
  41. }
  42. public BlockID(World world, BlockPos pos, int metadata)
  43. {
  44. this(world.getBlockState(pos).getBlock(), metadata);
  45. }
  46. public boolean isValid()
  47. {
  48. return getBlock() != null;
  49. }
  50. @Deprecated
  51. public Block getBlock()
  52. {
  53. return Blocks.AIR;
  54. // return ForgeRegistries.BLOCKS.get.getObject(id);
  55. }
  56. @Deprecated
  57. public static BlockID parse(String format)
  58. {
  59. String id;
  60. int metadata;
  61. int metadataModulus = 0;
  62. format = format.trim();
  63. int comma = format.indexOf(",");
  64. int tilde = format.indexOf("~");
  65. if (tilde == -1)
  66. tilde = format.indexOf("%");
  67. if ((comma == -1) && (tilde != -1))
  68. throw new RuntimeException(String.format("ModulusBlockID format error: a \"~\" or \"%1$s\" was found, but no \",\" in format \"%2$s\". " +
  69. "Expected format is \"<blockidstring>, <integer metadata> %1$s <integer modulus>\". EG: \"minecraft:log, 0 %1$s 4\".", "%", format));
  70. if ((tilde != -1) && (comma > tilde))
  71. throw new RuntimeException(String.format("ModulusBlockID format error: a \"~\" or \"%1$s\" was found before a \",\" in format \"%2$s\". " +
  72. "Expected format is \"<blockidstring>, <integer metadata> %1$s <integer modulus>\". EG: \"minecraft:log, 0 %1$s 4\".", "%", format));
  73. if (tilde == -1)
  74. tilde = format.length();
  75. if (comma != -1)
  76. id = format.substring(0, comma).trim();
  77. else
  78. id = format.trim();
  79. metadata = CommonUtils.parseInt(format.substring(comma + 1, tilde).trim(), -1);
  80. if (tilde != format.length())
  81. metadataModulus = CommonUtils.parseInt(format.substring(tilde + 1, format.length()).trim(), 0);
  82. if ((metadata != -1) && (metadataModulus > 0))
  83. return new ModulusBlockID(id, metadata, metadataModulus);
  84. else
  85. return new BlockID(id, metadata);
  86. }
  87. @Override
  88. public BlockID clone()
  89. {
  90. return new BlockID(id, metadata);
  91. }
  92. @Override
  93. public boolean equals(Object obj)
  94. {
  95. if (this == obj)
  96. return true;
  97. if (!(obj instanceof BlockID))
  98. return false;
  99. if ((((BlockID) obj).id != null) && !((BlockID) obj).id.equals(id))
  100. return false;
  101. else if ((((BlockID) obj).id == null) && (id != null))
  102. return false;
  103. if (obj instanceof ModulusBlockID)
  104. {
  105. ModulusBlockID o = (ModulusBlockID) obj;
  106. return (metadata % o.metadataModulus) == (o.metadata % o.metadataModulus);
  107. }
  108. else
  109. {
  110. BlockID o = (BlockID) obj;
  111. if ((o.metadata == -1) || (metadata == -1))
  112. return true;
  113. else
  114. return metadata == o.metadata;
  115. }
  116. }
  117. @Override
  118. public int hashCode()
  119. {
  120. return id.hashCode() * 37;
  121. }
  122. @Override
  123. public String toString()
  124. {
  125. return (metadata == -1 ? id : id + ", " + metadata);
  126. }
  127. }