EnvironmentEvents.java 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. package me.km.environment;
  2. import me.km.KajetansMod;
  3. import me.km.api.Module;
  4. import me.km.api.ModuleListener;
  5. import net.minecraft.block.state.IBlockState;
  6. import net.minecraft.entity.player.EntityPlayer;
  7. import net.minecraft.init.Blocks;
  8. import net.minecraft.init.Items;
  9. import net.minecraft.util.EnumHand;
  10. import net.minecraft.world.World;
  11. import net.minecraftforge.event.entity.living.LivingEntityUseItemEvent;
  12. import net.minecraftforge.event.entity.player.AttackEntityEvent;
  13. import net.minecraftforge.event.entity.player.PlayerInteractEvent;
  14. import net.minecraftforge.event.world.BlockEvent;
  15. import net.minecraftforge.fml.common.eventhandler.EventPriority;
  16. import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
  17. import net.minecraftforge.fml.common.gameevent.PlayerEvent;
  18. public class EnvironmentEvents extends ModuleListener
  19. {
  20. public EnvironmentEvents(Module m)
  21. {
  22. super(m);
  23. EnvironmentAPI.initStatusAPI();
  24. KajetansMod.scheduler.scheduleRepeatingTask(new EnvironmentTick(), 40, 30);
  25. }
  26. @SubscribeEvent
  27. public void onRespawn(PlayerEvent.PlayerRespawnEvent e)
  28. {
  29. EntityPlayer p = e.player;
  30. EnvironmentAPI.resetThirst(p);
  31. EnvironmentAPI.resetEnergy(p);
  32. EnvironmentAPI.resetCold(p);
  33. EnvironmentAPI.resetMana(p);
  34. }
  35. @SubscribeEvent
  36. public void onInteract(AttackEntityEvent e)
  37. {
  38. EntityPlayer p = e.getEntityPlayer();
  39. if(KajetansMod.worldManager.getWorldPreferences(p.getEntityWorld()).statusEffects &&
  40. !(p.isCreative() || p.isSpectator()))
  41. {
  42. EnvironmentAPI.changeThirst(p, -40);
  43. EnvironmentAPI.changeEnergy(p, -20);
  44. }
  45. }
  46. @SubscribeEvent
  47. public void onBedClick(PlayerInteractEvent.RightClickBlock e)
  48. {
  49. if(e.getHand() != EnumHand.MAIN_HAND ||
  50. !KajetansMod.worldManager.getWorldPreferences(e.getWorld()).statusEffects ||
  51. e.getWorld().getBlockState(e.getPos()).getBlock() != Blocks.BED)
  52. {
  53. return;
  54. }
  55. EntityPlayer p = e.getEntityPlayer();
  56. if(!(p.isCreative() || p.isSpectator()))
  57. {
  58. EnvironmentAPI.resetEnergy(p);
  59. }
  60. }
  61. @SubscribeEvent(receiveCanceled = false)
  62. public void onBlockBreak(BlockEvent.BreakEvent e)
  63. {
  64. if(KajetansMod.worldManager.getWorldPreferences(e.getWorld()).statusEffects)
  65. {
  66. EntityPlayer p = e.getPlayer();
  67. if(!(p.isCreative() || p.isSpectator()))
  68. {
  69. EnvironmentAPI.changeThirst(p, -20);
  70. EnvironmentAPI.changeEnergy(p, -10);
  71. }
  72. }
  73. }
  74. @SubscribeEvent(receiveCanceled = false)
  75. public void onBlockPlace(BlockEvent.PlaceEvent e)
  76. {
  77. if(KajetansMod.worldManager.getWorldPreferences(e.getWorld()).statusEffects)
  78. {
  79. EntityPlayer p = e.getPlayer();
  80. if(!(p.isCreative() || p.isSpectator()))
  81. {
  82. EnvironmentAPI.changeThirst(p, -20);
  83. EnvironmentAPI.changeEnergy(p, -10);
  84. }
  85. }
  86. }
  87. @SubscribeEvent(receiveCanceled = false)
  88. public void onWaterConsume(LivingEntityUseItemEvent.Finish e)
  89. {
  90. if(e.getItem().getItem() == Items.POTIONITEM)
  91. {
  92. if(e.getEntityLiving() instanceof EntityPlayer)
  93. {
  94. EntityPlayer p = (EntityPlayer) e.getEntityLiving();
  95. if(KajetansMod.worldManager.getWorldPreferences(p.getEntityWorld()).statusEffects)
  96. {
  97. EnvironmentAPI.resetThirst(p);
  98. }
  99. }
  100. }
  101. }
  102. @SubscribeEvent(priority = EventPriority.HIGH, receiveCanceled = false)
  103. public void onCauldronClick(PlayerInteractEvent.RightClickBlock e)
  104. {
  105. if(e.getHand() != EnumHand.MAIN_HAND ||
  106. !KajetansMod.worldManager.getWorldPreferences(e.getWorld()).statusEffects)
  107. {
  108. return;
  109. }
  110. EntityPlayer p = e.getEntityPlayer();
  111. if(p.getHeldItemMainhand().getItem() == Items.POTIONITEM)
  112. {
  113. return;
  114. }
  115. World w = e.getWorld();
  116. IBlockState state = w.getBlockState(e.getPos());
  117. if(state.getBlock() == Blocks.CAULDRON)
  118. {
  119. int meta = Blocks.CAULDRON.getMetaFromState(state) - 1;
  120. if(meta < 0)
  121. {
  122. return;
  123. }
  124. Blocks.CAULDRON.setWaterLevel(w, e.getPos(), state, meta);
  125. EnvironmentAPI.resetThirst(p);
  126. }
  127. }
  128. }