BlockClickEffects.java 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. package me.km.effects.passive;
  2. import java.util.HashMap;
  3. import me.km.KajetansMod;
  4. import me.km.api.Location;
  5. import me.km.api.Module;
  6. import me.km.api.ModuleListener;
  7. import me.km.api.Utils;
  8. import me.km.effects.Effect;
  9. import me.km.effects.EffectUtils;
  10. import net.minecraft.block.Block;
  11. import net.minecraft.entity.player.EntityPlayer;
  12. import net.minecraft.entity.player.EntityPlayerMP;
  13. import net.minecraft.init.Blocks;
  14. import net.minecraft.world.World;
  15. import net.minecraftforge.event.entity.player.PlayerInteractEvent;
  16. import net.minecraftforge.event.world.BlockEvent;
  17. import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
  18. public class BlockClickEffects extends ModuleListener
  19. {
  20. private final HashMap<Location, Data> blocks;
  21. private class Data
  22. {
  23. protected EntityPlayerMP p;
  24. protected int guild;
  25. protected int taskId;
  26. public Data(EntityPlayerMP p, int taskId)
  27. {
  28. this.p = p;
  29. this.guild = KajetansMod.playerbank.getGuildId(p);
  30. this.taskId = taskId;
  31. }
  32. }
  33. public BlockClickEffects(Module m)
  34. {
  35. super(m);
  36. blocks = new HashMap<>();
  37. }
  38. public void registerTeleportBlock(Location l, EntityPlayerMP p, int time)
  39. {
  40. int i = KajetansMod.scheduler.scheduleTask(() ->
  41. {
  42. blocks.remove(l);
  43. l.getWorld().setBlockToAir(l.getBlockPos());
  44. }, time);
  45. blocks.put(l, new Data(p, i));
  46. l.getWorld().setBlockState(l.getBlockPos(), Blocks.SEA_LANTERN.getDefaultState());
  47. }
  48. @SubscribeEvent
  49. public void interact(BlockEvent.BreakEvent e)
  50. {
  51. Block b = e.getWorld().getBlockState(e.getPos()).getBlock();
  52. if(b == Blocks.SEA_LANTERN ||
  53. !KajetansMod.worldManager.getWorldPreferences(e.getWorld()).skills)
  54. {
  55. return;
  56. }
  57. if(blocks.containsKey(new Location(e.getWorld(), e.getPos())))
  58. {
  59. e.setCanceled(true);
  60. }
  61. }
  62. @SubscribeEvent
  63. public void interact(PlayerInteractEvent.RightClickBlock e)
  64. {
  65. Block b = e.getWorld().getBlockState(e.getPos()).getBlock();
  66. if(KajetansMod.worldManager.getWorldPreferences(e.getWorld()).skills)
  67. {
  68. if(b == Blocks.SEA_LANTERN)
  69. {
  70. handleTeleportBlock(e);
  71. }
  72. else if(b == Blocks.CAULDRON)
  73. {
  74. handleCauldronFilling(e);
  75. }
  76. }
  77. }
  78. private void handleTeleportBlock(PlayerInteractEvent.RightClickBlock e)
  79. {
  80. Location l = new Location(e.getWorld(), e.getPos());
  81. Data data = blocks.get(l);
  82. if(data == null)
  83. {
  84. return;
  85. }
  86. EntityPlayer p = e.getEntityPlayer();
  87. if(data.guild == KajetansMod.playerbank.getGuildId(p))
  88. {
  89. if(data.p.isEntityAlive())
  90. {
  91. Utils.teleportEntity(p, data.p);
  92. }
  93. blocks.remove(l);
  94. KajetansMod.scheduler.cancelTask(data.taskId);
  95. l.getWorld().setBlockToAir(l.getBlockPos());
  96. }
  97. }
  98. private void handleCauldronFilling(PlayerInteractEvent.RightClickBlock e)
  99. {
  100. if(EffectUtils.getEffectLevel(e.getEntityPlayer(), Effect.CAULDRON_FILLING) >= 1)
  101. {
  102. World w = e.getWorld();
  103. Blocks.CAULDRON.setWaterLevel(w, e.getPos(), w.getBlockState(e.getPos()), 3);
  104. }
  105. }
  106. }