BlockBreakEffects.java 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. package me.km.effects.passive;
  2. import me.km.KajetansMod;
  3. import me.km.api.Module;
  4. import me.km.api.ModuleListener;
  5. import me.km.api.Utils;
  6. import me.km.effects.Effect;
  7. import me.km.effects.EffectUtils;
  8. import me.km.utils.ItemStackBuilder;
  9. import net.minecraft.block.Block;
  10. import net.minecraft.block.BlockCrops;
  11. import net.minecraft.block.BlockFalling;
  12. import net.minecraft.block.state.IBlockState;
  13. import net.minecraft.enchantment.EnchantmentHelper;
  14. import net.minecraft.entity.player.EntityPlayer;
  15. import net.minecraft.init.Blocks;
  16. import net.minecraft.init.Enchantments;
  17. import net.minecraft.init.Items;
  18. import net.minecraft.item.ItemStack;
  19. import net.minecraft.util.math.BlockPos;
  20. import net.minecraft.world.World;
  21. import net.minecraftforge.event.entity.player.PlayerEvent;
  22. import net.minecraftforge.event.world.BlockEvent;
  23. import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
  24. public class BlockBreakEffects extends ModuleListener
  25. {
  26. public BlockBreakEffects(Module m)
  27. {
  28. super(m);
  29. }
  30. @SubscribeEvent(receiveCanceled = false)
  31. public void onBlockBreakSkills(BlockEvent.BreakEvent e)
  32. {
  33. if(!KajetansMod.worldManager.getWorldPreferences(e.getWorld()).skills)
  34. {
  35. return;
  36. }
  37. EntityPlayer p = e.getPlayer();
  38. BlockPos pos = e.getPos();
  39. World w = e.getWorld();
  40. IBlockState state = w.getBlockState(pos);
  41. Block b = state.getBlock();
  42. ItemStack hand = p.getHeldItemMainhand();
  43. // ---------------------------------------------------------------------
  44. // Gravel-Sand-Digger
  45. // ---------------------------------------------------------------------
  46. if(b instanceof BlockFalling)
  47. {
  48. int level = EffectUtils.getEffectLevel(p, Effect.GRAVEL_SAND_DIGGER);
  49. if(level >= 1)
  50. {
  51. BlockPos dig;
  52. for(int i = 0; i < level; i++)
  53. {
  54. dig = pos.add(0, i, 0);
  55. state = w.getBlockState(dig);
  56. if(state.getBlock() instanceof BlockFalling)
  57. {
  58. Utils.breakBlock(w, dig, p);
  59. continue;
  60. }
  61. break;
  62. }
  63. }
  64. return;
  65. }
  66. // ---------------------------------------------------------------------
  67. // More Minerals
  68. // ---------------------------------------------------------------------
  69. if(EnchantmentHelper.getEnchantmentLevel(Enchantments.SILK_TOUCH, hand) >= 1)
  70. {
  71. if(b == Blocks.DIAMOND_ORE || b == Blocks.REDSTONE_ORE ||
  72. b == Blocks.LAPIS_ORE || b == Blocks.COAL_ORE)
  73. {
  74. if(Utils.randomBoolean() && EffectUtils.getEffectLevel(p, Effect.MORE_MINERALS) >= 1)
  75. {
  76. b.dropBlockAsItem(w, pos, state, 0);
  77. }
  78. }
  79. }
  80. // ---------------------------------------------------------------------
  81. // More Crops
  82. // ---------------------------------------------------------------------
  83. if(b instanceof BlockCrops && state.getValue(BlockCrops.AGE) == 7)
  84. {
  85. int moreCrops = EffectUtils.getEffectLevel(p, Effect.MORE_CROPS);
  86. if(moreCrops >= 1)
  87. {
  88. b.getDrops(w, pos, state, 0).stream().filter(s -> Utils.randomBoolean()).forEach((stack) ->
  89. {
  90. new ItemStackBuilder(stack, Utils.randomInt(1, moreCrops)).drop(w, pos);
  91. });
  92. }
  93. }
  94. // ---------------------------------------------------------------------
  95. // Tree Picker und More Saplings
  96. // ---------------------------------------------------------------------
  97. if((b == Blocks.LEAVES || b == Blocks.LEAVES2) && hand.getItem() != Items.SHEARS)
  98. {
  99. if(Utils.randomInt(1, 25) <= EffectUtils.getEffectLevel(p, Effect.TREE_PICKER))
  100. {
  101. Utils.dropRandomTreeItem(w, pos, state);
  102. }
  103. if(Utils.randomInt(1, 25) <= EffectUtils.getEffectLevel(p, Effect.MORE_SAPLINGS))
  104. {
  105. if(b == Blocks.LEAVES)
  106. {
  107. new ItemStackBuilder(Blocks.SAPLING, 1, b.getMetaFromState(state) % 4).drop(w, pos);
  108. }
  109. else
  110. {
  111. new ItemStackBuilder(Blocks.SAPLING, 1, (b.getMetaFromState(state) % 4) + 4).drop(w, pos);
  112. }
  113. }
  114. }
  115. }
  116. @SubscribeEvent(receiveCanceled = false)
  117. public void onBlockDamage(PlayerEvent.BreakSpeed e)
  118. {
  119. EntityPlayer p = e.getEntityPlayer();
  120. if(KajetansMod.worldManager.getWorldPreferences(p.world).skills &&
  121. KajetansMod.jobs.isPreferedMaterial(p, e.getState().getBlock()))
  122. {
  123. // Speed up with prefered blocks of current job
  124. int digging = EffectUtils.getEffectLevel(p, Effect.FAST_DIGGING);
  125. if(digging >= 1)
  126. {
  127. e.setNewSpeed(e.getOriginalSpeed() * (1 + (digging / 5)));
  128. }
  129. }
  130. }
  131. }