Advertisement
ought

Radioactive Fire

Jun 28th, 2014
305
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 16.90 KB | None | 0 0
  1. package refugeecraft.blocks;
  2.  
  3. import cpw.mods.fml.relauncher.Side;
  4. import cpw.mods.fml.relauncher.SideOnly;
  5. import net.minecraft.block.BlockFire;
  6. import net.minecraft.client.renderer.texture.IIconRegister;
  7. import refugeecraft.lib.References;
  8.  
  9. /**
  10.  * Created by OUGHT on 21.06.2014.
  11.  */
  12. public class BlockRMFire extends BlockFire
  13. {
  14.     public BlockRMFire()
  15.     {
  16.         super();
  17.     }
  18.  
  19.     @Override
  20.     public String getUnlocalizedName()
  21.     {
  22.         return String.format("tile.%s%s", References.RESOURCESPREFIX,
  23.                              getUnwrappedUnlocalizedName(super.getUnlocalizedName()));
  24.     }
  25.  
  26.     public String getUnwrappedUnlocalizedName(String unlocalizedName)
  27.     {
  28.         return unlocalizedName.substring(unlocalizedName.indexOf(".") + 1);
  29.     }
  30.  
  31.     @Override
  32.     @SideOnly (Side.CLIENT)
  33.     public void registerBlockIcons(IIconRegister iconRegister)
  34.     {
  35.         this.blockIcon = iconRegister.registerIcon(References.RESOURCESPREFIX +
  36.                                                            getUnwrappedUnlocalizedName(super.getUnlocalizedName()));
  37.     }
  38.  
  39.  
  40. }
  41.  
  42.  
  43. package refugeecraft.blocks;
  44.  
  45. import com.google.common.collect.Maps;
  46. import cpw.mods.fml.relauncher.Side;
  47. import cpw.mods.fml.relauncher.SideOnly;
  48. import net.minecraft.block.Block;
  49. import net.minecraft.block.material.MapColor;
  50. import net.minecraft.client.renderer.texture.IIconRegister;
  51. import net.minecraft.init.Blocks;
  52. import net.minecraft.util.AxisAlignedBB;
  53. import net.minecraft.util.IIcon;
  54. import net.minecraft.world.IBlockAccess;
  55. import net.minecraft.world.World;
  56. import net.minecraftforge.common.util.ForgeDirection;
  57. import refugeecraft.RefugeeCraft;
  58. import refugeecraft.lib.Strings;
  59.  
  60. import java.util.IdentityHashMap;
  61. import java.util.Map;
  62. import java.util.Random;
  63.  
  64. /**
  65.  * Created by OUGHT on 21.06.2014.
  66.  */
  67. public class BlockRadioactiveFire extends BlockRMFire
  68. {
  69.     @Deprecated
  70.     private int[] field_149849_a = new int[4096];
  71.     @Deprecated
  72.     private int[] field_149848_b = new int[4096];
  73.  
  74.     @SideOnly (Side.CLIENT)
  75.     private IIcon[] icons;
  76.  
  77.     public BlockRadioactiveFire ()
  78.     {
  79.         super();
  80.         this.setBlockName(Strings.RadioactiveFireName);
  81.         this.setCreativeTab(RefugeeCraft.getCreativeTab());
  82.         this.setLightLevel(1F);
  83.         this.setTickRandomly(true);
  84.         this.setHardness(0.0F);
  85.         this.disableStats();
  86.         RModBlocks.register(this);
  87.     }
  88.  
  89.     @SideOnly (Side.CLIENT)
  90.     @Override
  91.     public void registerBlockIcons (IIconRegister iconRegister)
  92.     {
  93.         super.registerBlockIcons(iconRegister);
  94.         this.icons = new IIcon[]
  95.                 {iconRegister.registerIcon(
  96.                         getUnwrappedUnlocalizedName(super.getUnlocalizedName()) + "_layer_0"),
  97.                  iconRegister.registerIcon(
  98.                          getUnwrappedUnlocalizedName(super.getUnlocalizedName()) + "_layer_1")};
  99.     }
  100.  
  101.     @SideOnly (Side.CLIENT)
  102.     @Override
  103.     public IIcon getFireIcon (int i)
  104.     {
  105.         return this.icons[i];
  106.     }
  107.  
  108.     /**
  109.      * Gets the block's texture. Args: side, meta
  110.      */
  111.     @SideOnly(Side.CLIENT)
  112.     @Override
  113.     public IIcon getIcon(int p_149691_1_, int p_149691_2_)
  114.     {
  115.         return this.icons[0];
  116.     }
  117.  
  118.     @Override
  119.     public MapColor getMapColor(int p_149728_1_)
  120.     {
  121.         return MapColor.greenColor;
  122.     }
  123.  
  124.     /**
  125.      * Returns a bounding box from the pool of bounding boxes (this means this box can change after the pool has been
  126.      * cleared to be reused)
  127.      */
  128.     @Override
  129.     public AxisAlignedBB getCollisionBoundingBoxFromPool (World p_149668_1_, int p_149668_2_, int p_149668_3_,
  130.                                                           int p_149668_4_)
  131.     {
  132.         return null;
  133.     }
  134.  
  135.     /**
  136.      * Is this block (a) opaque and (b) a full 1m cube?  This determines whether or not to render the shared face of two
  137.      * adjacent blocks and also whether the player can attach torches, redstone wire, etc to this block.
  138.      */
  139.     @Override
  140.     public boolean isOpaqueCube ()
  141.     {
  142.         return false;
  143.     }
  144.  
  145.     /**
  146.      * If this block doesn't render as an ordinary block it will return False (examples: signs, buttons, stairs, etc)
  147.      */
  148.     public boolean renderAsNormalBlock ()
  149.     {
  150.         return false;
  151.     }
  152.  
  153.     /**
  154.      * The type of render function that is called for this block
  155.      * 3 is Fire
  156.      */
  157.  
  158.     public int getRenderType()
  159.     {
  160.         return 3;
  161.     }
  162.  
  163.     /**
  164.      * Returns the quantity of items to drop on block destruction.
  165.      */
  166.     public int quantityDropped (Random p_149745_1_)
  167.     {
  168.         return 0;
  169.     }
  170.  
  171.     /**
  172.      * How many world ticks before ticking
  173.      */
  174.     public int tickRate (World p_149738_1_)
  175.     {
  176.         return 10;
  177.     }
  178.  
  179.     /**
  180.      * Что-то странное
  181.      * Возвращает у блока 1, а у огня 0
  182.      */
  183.     @Override
  184.     public boolean func_149698_L ()
  185.     {
  186.         return true;
  187.     }
  188.  
  189.     /**
  190.      * Tries to set block on fire.
  191.      */
  192.     private void tryCatchFire (World world, int x, int y, int z, int flameHumidity,
  193.                                Random random, int flameLimitMetadata, ForgeDirection face)
  194.     {
  195.         int j1 = world.getBlock(x, y, z).getFlammability(world, x, y, z, face);
  196.  
  197.         if (random.nextInt(flameHumidity) < j1)
  198.         {
  199.             boolean isTNT = world.getBlock(x, y, z) == Blocks.tnt;
  200.  
  201.             if (random.nextInt(flameLimitMetadata + 10) < 5 && !world
  202.                     .canLightningStrikeAt(x, y, z))   // не под дождем ли блок
  203.             {
  204.                 int metadata = flameLimitMetadata + random.nextInt(5) / 4;
  205.  
  206.                 if (metadata > 15)
  207.                 {
  208.                     metadata = 15;
  209.                 }
  210.  
  211.                 world.setBlock(x, y, z, this, metadata, 3);
  212.             }
  213.             else
  214.             {
  215.                 world.setBlockToAir(x, y, z);
  216.             }
  217.  
  218.             if (isTNT)
  219.             {
  220.                 Blocks.tnt.onBlockDestroyedByPlayer(world, x, y, z, 1);
  221.             }
  222.         }
  223.     }
  224.  
  225.     /**
  226.      * Returns true if at least one block next to this one can burn.
  227.      */
  228.     private boolean canNeighborBurn (World world, int x, int y, int z)
  229.     {
  230.         return this.canCatchFire(world, x + 1, y, z, ForgeDirection.WEST) ||
  231.                 this.canCatchFire(world, x - 1, y, z, ForgeDirection.EAST) ||
  232.                 this.canCatchFire(world, x, y - 1, z, ForgeDirection.UP) ||
  233.                 this.canCatchFire(world, x, y + 1, z, ForgeDirection.DOWN) ||
  234.                 this.canCatchFire(world, x, y, z - 1, ForgeDirection.SOUTH) ||
  235.                 this.canCatchFire(world, x, y, z + 1, ForgeDirection.NORTH);
  236.     }
  237.  
  238.     /**
  239.      * Gets the highest chance of a neighbor block encouraging this block to catch fire
  240.      */
  241.     private int getChanceOfNeighborsEncouragingFire (World world, int x, int y, int z)
  242.     {
  243.         byte byte0 = 0;
  244.  
  245.         if (!world.isAirBlock(x, y, z))
  246.         {
  247.             return 0;
  248.         }
  249.         else
  250.         {
  251.             int chance = byte0;
  252.             chance = this.getChanceToEncourageFire(world, x + 1, y, z, chance, ForgeDirection.WEST);
  253.             chance = this.getChanceToEncourageFire(world, x - 1, y, z, chance, ForgeDirection.EAST);
  254.             chance = this.getChanceToEncourageFire(world, x, y - 1, z, chance, ForgeDirection.UP);
  255.             chance = this.getChanceToEncourageFire(world, x, y + 1, z, chance, ForgeDirection.DOWN);
  256.             chance = this.getChanceToEncourageFire(world, x, y, z - 1, chance, ForgeDirection.SOUTH);
  257.             chance = this.getChanceToEncourageFire(world, x, y, z + 1, chance, ForgeDirection.NORTH);
  258.             return chance;
  259.         }
  260.     }
  261.  
  262.     /**
  263.      * Ticks the block if it's been scheduled
  264.      */
  265.     public void updateTick (World world, int x, int y, int z, Random random)
  266.     {
  267.         if (world.getGameRules().getGameRuleBooleanValue("doFireTick"))
  268.         {
  269.             doFireTick(world, x, y, z, random);
  270.         }
  271.     }
  272.  
  273.     public void doFireTick (World world, int x, int y, int z, Random random)
  274.     {
  275.         boolean isInfiniteFire = world.getBlock(x, y - 1, z).isFireSource(world, x, y - 1, z, ForgeDirection.UP);
  276.  
  277.         if (!this.canPlaceBlockAt(world, x, y, z))
  278.         {
  279.             world.setBlockToAir(x, y, z);
  280.         }
  281.  
  282.         if (!isInfiniteFire && world.isRaining() &&
  283.                 (world.canLightningStrikeAt(x, y, z) ||
  284.                         world.canLightningStrikeAt(x - 1, y, z) ||
  285.                         world.canLightningStrikeAt(x + 1, y, z) ||
  286.                         world.canLightningStrikeAt(x, y, z - 1) ||
  287.                         world.canLightningStrikeAt(x, y, z + 1)))
  288.         {
  289.             world.setBlockToAir(x, y, z);
  290.         }
  291.         else
  292.         {
  293.             int fireMetadata = world.getBlockMetadata(x, y, z);
  294.  
  295.             if (fireMetadata < 15)
  296.             {
  297.                 world.setBlockMetadataWithNotify(x, y, z,
  298.                                                  fireMetadata + random.nextInt(3) / 2, 4);
  299.             }
  300.  
  301.             world.scheduleBlockUpdate(x, y, z, this,
  302.                                       this.tickRate(world) + random.nextInt(10));
  303.  
  304.             if (!isInfiniteFire && !this.canNeighborBurn(world, x, y, z))
  305.             {
  306.                 if (!World.doesBlockHaveSolidTopSurface(world, x, y - 1,
  307.                                                         z) || fireMetadata > 3)
  308.                 {
  309.                     world.setBlockToAir(x, y, z);
  310.                 }
  311.             }
  312.             else if (!isInfiniteFire && !this.canCatchFire(world, x, y - 1, z, ForgeDirection.UP) &&
  313.                     fireMetadata == 15 && random.nextInt(4) == 0)
  314.             {
  315.                 world.setBlockToAir(x, y, z);
  316.             }
  317.             else
  318.             {
  319.                 boolean isBlockHumid = world.isBlockHighHumidity(x, y, z);
  320.                 byte humidFactor = 0;
  321.  
  322.                 if (isBlockHumid)
  323.                 {
  324.                     humidFactor = -50;
  325.                 }
  326.  
  327.                 this.tryCatchFire(world, x + 1, y, z, 300 + humidFactor, random, fireMetadata,
  328.                                   ForgeDirection.WEST);
  329.                 this.tryCatchFire(world, x - 1, y, z, 300 + humidFactor, random, fireMetadata,
  330.                                   ForgeDirection.EAST);
  331.                 this.tryCatchFire(world, x, y - 1, z, 250 + humidFactor, random, fireMetadata,
  332.                                   ForgeDirection.UP);
  333.                 this.tryCatchFire(world, x, y + 1, z, 250 + humidFactor, random, fireMetadata,
  334.                                   ForgeDirection.DOWN);
  335.                 this.tryCatchFire(world, x, y, z - 1, 300 + humidFactor, random, fireMetadata,
  336.                                   ForgeDirection.SOUTH);
  337.                 this.tryCatchFire(world, x, y, z + 1, 300 + humidFactor, random, fireMetadata,
  338.                                   ForgeDirection.NORTH);
  339.  
  340.                 tryToStopFire(world, x, y, z, random, fireMetadata, isBlockHumid);
  341.             }
  342.         }
  343.     }
  344.  
  345.     public void tryToStopFire (World world, int x, int y, int z, Random random, int fireMetadata,
  346.                                boolean isBlockHumid)
  347.     {
  348.         for (int i1 = x - 1; i1 <= x + 1; ++i1)
  349.         {
  350.             for (int j1 = z - 1; j1 <= z + 1; ++j1)
  351.             {
  352.                 for (int k1 = y - 1; k1 <= y + 4; ++k1)
  353.                 {
  354.                     if (i1 != x || k1 != y || j1 != z)
  355.                     {
  356.                         int spreadingFactor = 100;
  357.  
  358.                         if (k1 > y + 1)
  359.                         {
  360.                             spreadingFactor += (k1 - (y + 1)) * 100;    // в высоту маловероятнее, чем в ширину
  361.                         }
  362.  
  363.                         int chanceOnSupport = this.getChanceOfNeighborsEncouragingFire(world, i1, k1, j1);
  364.  
  365.                         if (chanceOnSupport > 0)
  366.                         {
  367.                             int spreadStopTheshold = (chanceOnSupport + 40 + world.difficultySetting
  368.                                     .getDifficultyId() * 7) / (fireMetadata + 30);
  369.  
  370.                             if (isBlockHumid)
  371.                             {
  372.                                 spreadStopTheshold /= 2;
  373.                             }
  374.  
  375.                             if (spreadStopTheshold > 0 && random.nextInt(spreadingFactor) <= spreadStopTheshold &&
  376.                                     (!world.isRaining() ||
  377.                                             !world.canLightningStrikeAt(i1, k1, j1)) &&
  378.                                     !world.canLightningStrikeAt(i1 - 1, k1, z) &&
  379.                                     !world.canLightningStrikeAt(i1 + 1, k1, j1) &&
  380.                                     !world.canLightningStrikeAt(i1, k1, j1 - 1) &&
  381.                                     !world.canLightningStrikeAt(i1, k1, j1 + 1))
  382.                             {
  383.                                 int newFireMetadata = fireMetadata + random.nextInt(5) / 4;
  384.  
  385.                                 if (newFireMetadata > 15)
  386.                                 {
  387.                                     newFireMetadata = 15;
  388.                                 }
  389.  
  390.                                 world.setBlock(i1, k1, j1, this, newFireMetadata, 3);
  391.                             }
  392.                         }
  393.                     }
  394.                 }
  395.             }
  396.         }
  397.     }
  398.  
  399.  
  400.     /**
  401.      * Returns if this block is collidable (only used by Fire). Args: x, y, z
  402.      */
  403.     public boolean isCollidable()
  404.     {
  405.         return false;
  406.     }
  407.  
  408.     /**
  409.      * Checks the specified block coordinate to see if it can catch fire.  Args: blockAccess, x, y, z
  410.      */
  411.     @Deprecated
  412.     public boolean canBlockCatchFire(IBlockAccess p_149844_1_, int p_149844_2_, int p_149844_3_, int p_149844_4_)
  413.     {
  414.         return canCatchFire(p_149844_1_, p_149844_2_, p_149844_3_, p_149844_4_, ForgeDirection.UP);
  415.     }
  416.  
  417.     @Deprecated
  418.     public int func_149846_a(World p_149846_1_, int p_149846_2_, int p_149846_3_, int p_149846_4_, int p_149846_5_)
  419.     {
  420.         return getChanceToEncourageFire(p_149846_1_, p_149846_2_, p_149846_3_, p_149846_4_, p_149846_5_, ForgeDirection.UP);
  421.     }
  422.  
  423.     /**
  424.      * Checks to see if its valid to put this block at the specified coordinates. Args: world, x, y, z
  425.      */
  426.     public boolean canPlaceBlockAt(World p_149742_1_, int p_149742_2_, int p_149742_3_, int p_149742_4_)
  427.     {
  428.         return World.doesBlockHaveSolidTopSurface(p_149742_1_, p_149742_2_, p_149742_3_ - 1, p_149742_4_) || this.canNeighborBurn(p_149742_1_, p_149742_2_, p_149742_3_, p_149742_4_);
  429.     }
  430.  
  431.     /**
  432.      * Lets the block know when one of its neighbor changes. Doesn't know which neighbor changed (coordinates passed are
  433.      * their own) Args: x, y, z, neighbor Block
  434.      */
  435.     public void onNeighborBlockChange(World p_149695_1_, int p_149695_2_, int p_149695_3_, int p_149695_4_, Block p_149695_5_)
  436.     {
  437.         if (!World.doesBlockHaveSolidTopSurface(p_149695_1_, p_149695_2_, p_149695_3_ - 1, p_149695_4_) && !this.canNeighborBurn(p_149695_1_, p_149695_2_, p_149695_3_, p_149695_4_))
  438.         {
  439.             p_149695_1_.setBlockToAir(p_149695_2_, p_149695_3_, p_149695_4_);
  440.         }
  441.     }
  442.  
  443.     /**
  444.      * Called whenever the block is added into the world. Args: world, x, y, z
  445.      */
  446.     public void onBlockAdded(World p_149726_1_, int p_149726_2_, int p_149726_3_, int p_149726_4_)
  447.     {
  448.         if (p_149726_1_.provider.dimensionId > 0 || !Blocks.portal.func_150000_e(p_149726_1_, p_149726_2_, p_149726_3_, p_149726_4_))
  449.         {
  450.             if (!World.doesBlockHaveSolidTopSurface(p_149726_1_, p_149726_2_, p_149726_3_ - 1, p_149726_4_) && !this.canNeighborBurn(p_149726_1_, p_149726_2_, p_149726_3_, p_149726_4_))
  451.             {
  452.                 p_149726_1_.setBlockToAir(p_149726_2_, p_149726_3_, p_149726_4_);
  453.             }
  454.             else
  455.             {
  456.                 p_149726_1_.scheduleBlockUpdate(p_149726_2_, p_149726_3_, p_149726_4_, this, this.tickRate(p_149726_1_) + p_149726_1_.rand.nextInt(10));
  457.             }
  458.         }
  459.     }
  460.  
  461.     /**
  462.      * A randomly called display update to be able to add particles or other items for display
  463.      */
  464.     @SideOnly(Side.CLIENT)
  465.     @Override
  466.     public void randomDisplayTick(World p_149734_1_, int p_149734_2_, int p_149734_3_, int p_149734_4_, Random p_149734_5_)
  467.     {
  468.         if (p_149734_5_.nextInt(24) == 0)
  469.         {
  470.             p_149734_1_.playSound((double)((float)p_149734_2_ + 0.5F), (double)((float)p_149734_3_ + 0.5F), (double)((float)p_149734_4_ + 0.5F), "fire.fire", 1.0F + p_149734_5_.nextFloat(), p_149734_5_.nextFloat() * 0.7F + 0.3F, false);
  471.         }
  472.     }
  473.  
  474.  
  475.     /*================================= Forge Start ======================================*/
  476.     private static class FireInfo
  477.     {
  478.         private int encouragement = 0;
  479.         private int flammibility = 0;
  480.     }
  481.  
  482.     private IdentityHashMap<Block, FireInfo> blockInfo = Maps.newIdentityHashMap();
  483.  
  484.     public void setFireInfo (Block block, int encouragement, int flammibility)
  485.     {
  486.         if (block == Blocks.air) throw new IllegalArgumentException("Tried to set air on fire... This is bad.");
  487.         int id = Block.getIdFromBlock(block);
  488.         this.field_149849_a[id] = encouragement;
  489.         this.field_149848_b[id] = flammibility;
  490.  
  491.         FireInfo info = getInfo(block, true);
  492.         info.encouragement = encouragement;
  493.         info.flammibility = flammibility;
  494.     }
  495.  
  496.     private FireInfo getInfo (Block block, boolean garentee)
  497.     {
  498.         FireInfo ret = blockInfo.get(block);
  499.         if (ret == null && garentee)
  500.         {
  501.             ret = new FireInfo();
  502.             blockInfo.put(block, ret);
  503.         }
  504.         return ret;
  505.     }
  506.  
  507.     public void rebuildFireInfo ()
  508.     {
  509.         for (int x = 0; x < 4096; x++)
  510.         {
  511.             //If we care.. we could detect changes in here and make sure we keep them, however
  512.             //it's my thinking that anyone who hacks into the private variables should DIAF and we don't care about them.
  513.             field_149849_a[x] = 0;
  514.             field_149848_b[x] = 0;
  515.         }
  516.  
  517.         for (Map.Entry<Block, FireInfo> e : blockInfo.entrySet())
  518.         {
  519.             int id = Block.getIdFromBlock(e.getKey());
  520.             if (id >= 0 && id < 4096)
  521.             {
  522.                 field_149849_a[id] = e.getValue().encouragement;
  523.                 field_149848_b[id] = e.getValue().flammibility;
  524.             }
  525.         }
  526.     }
  527.  
  528.     public int getFlammability (Block block)
  529.     {
  530.         int id = Block.getIdFromBlock(block);
  531.         return id >= 0 && id < 4096 ? field_149848_b[id] : 0;
  532.     }
  533.  
  534.     public int getEncouragement (Block block)
  535.     {
  536.         int id = Block.getIdFromBlock(block);
  537.         return id >= 0 && id < 4096 ? field_149849_a[id] : 0;
  538.     }
  539.  
  540.     /**
  541.      * Side sensitive version that calls the block function.
  542.      *
  543.      * @param world The current world
  544.      * @param x     X Position
  545.      * @param y     Y Position
  546.      * @param z     Z Position
  547.      * @param face  The side the fire is coming from
  548.      * @return True if the face can catch fire.
  549.      */
  550.     public boolean canCatchFire (IBlockAccess world, int x, int y, int z, ForgeDirection face)
  551.     {
  552.         return world.getBlock(x, y, z).isFlammable(world, x, y, z, face);
  553.     }
  554.  
  555.     /**
  556.      * Side sensitive version that calls the block function.
  557.      *
  558.      * @param world     The current world
  559.      * @param x         X Position
  560.      * @param y         Y Position
  561.      * @param z         Z Position
  562.      * @param oldChance The previous maximum chance.
  563.      * @param face      The side the fire is coming from
  564.      * @return The chance of the block catching fire& or oldChance if it is higher
  565.      */
  566.     public int getChanceToEncourageFire (IBlockAccess world, int x, int y, int z, int oldChance, ForgeDirection face)
  567.     {
  568.         int newChance = world.getBlock(x, y, z).getFireSpreadSpeed(world, x, y, z, face);
  569.         return (newChance > oldChance ? newChance : oldChance);
  570.     }
  571. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement