|
1 | 1 | package nl.dedouwe.items.scroll.water; |
2 | 2 |
|
| 3 | +import java.util.HashMap; |
| 4 | +import java.util.Map.Entry; |
| 5 | + |
| 6 | +import org.bukkit.Bukkit; |
| 7 | +import org.bukkit.Location; |
| 8 | +import org.bukkit.Material; |
| 9 | +import org.bukkit.event.EventHandler; |
| 10 | +import org.bukkit.event.Listener; |
| 11 | +import org.bukkit.event.block.BlockFromToEvent; |
3 | 12 | import org.bukkit.event.player.PlayerDropItemEvent; |
4 | 13 | import org.bukkit.event.player.PlayerInteractEvent; |
| 14 | +import org.bukkit.util.Vector; |
5 | 15 |
|
6 | 16 | import net.kyori.adventure.text.Component; |
7 | 17 | import net.kyori.adventure.text.TextComponent; |
8 | 18 | import net.kyori.adventure.text.format.NamedTextColor; |
| 19 | +import nl.dedouwe.Plugin; |
9 | 20 | import nl.dedouwe.items.Scroll; |
10 | 21 | import nl.dedouwe.items.Sources; |
11 | 22 |
|
12 | | -public class TsunamiScroll extends Scroll { |
| 23 | +public class TsunamiScroll extends Scroll implements Listener { |
13 | 24 |
|
14 | 25 | public TsunamiScroll() { |
15 | 26 | super(Sources.Water, "Tsunami", Component.text("Everything gone, guaranteed...").color(NamedTextColor.GRAY), |
16 | | - Component.text("(No insurance)").color(NamedTextColor.DARK_GRAY)); |
| 27 | + Component.text("(No insurance)").color(NamedTextColor.DARK_GRAY)); |
| 28 | + Plugin.instance.getServer().getPluginManager().registerEvents(this, Plugin.instance); |
17 | 29 | } |
18 | 30 |
|
19 | 31 | @Override |
20 | 32 | public TextComponent GetHelp() { |
21 | 33 | return Component.text("Shift left-click for a tsunami, drop the item for a water splash.").color(NamedTextColor.GRAY); |
22 | 34 | } |
23 | | - |
24 | | - public void onActivate(PlayerInteractEvent e) {} |
25 | | - public void onDrop(PlayerDropItemEvent e) {} |
| 35 | + public Location NoWaterFlowPlace=null; |
| 36 | + private HashMap<Location, Material> previousBlocks = new HashMap<>(); |
| 37 | + private void SetFlooded(Location l) { |
| 38 | + previousBlocks.put(l, l.getBlock().getType()); |
| 39 | + Plugin.instance.getLogger().info("add"); |
| 40 | + l.getBlock().setType(Material.WATER); |
| 41 | + } |
| 42 | + private int tsunamiCount = 0; |
| 43 | + private void AddTsunami(Location l) { |
| 44 | + if (tsunamiCount>=200) {return;} |
| 45 | + Plugin.instance.getLogger().info(l.getBlock().getType().toString()); |
| 46 | + if(l.getBlock().getType()==Material.WATER) {return;} |
| 47 | + SetFlooded(l); |
| 48 | + tsunamiCount++; |
| 49 | + Bukkit.getScheduler().scheduleSyncDelayedTask(Plugin.instance, ()->{ |
| 50 | + AddTsunami(l.clone().add(1, 0, 0)); |
| 51 | + }); |
| 52 | + Bukkit.getScheduler().scheduleSyncDelayedTask(Plugin.instance, ()->{ |
| 53 | + AddTsunami(l.clone().add(-1, 0, 0)); |
| 54 | + }); |
| 55 | + Bukkit.getScheduler().scheduleSyncDelayedTask(Plugin.instance, ()->{ |
| 56 | + AddTsunami(l.clone().add(0, 0, 1)); |
| 57 | + }); |
| 58 | + Bukkit.getScheduler().scheduleSyncDelayedTask(Plugin.instance, ()->{ |
| 59 | + AddTsunami(l.clone().add(0, 0, -1)); |
| 60 | + }); |
| 61 | + } |
| 62 | + public void onActivate(PlayerInteractEvent e) { |
| 63 | + if (!Test(e.getPlayer(), 8, 0.05, 200)) {return;} |
| 64 | + |
| 65 | + AddTsunami(e.getPlayer().getLocation().clone().add(0, 5, 0)); |
| 66 | + Bukkit.getScheduler().scheduleSyncDelayedTask(Plugin.instance, ()->{ |
| 67 | + Plugin.instance.getLogger().info((String.valueOf(previousBlocks.size()))); |
| 68 | + for (Entry<Location, Material> pos : previousBlocks.entrySet()) { |
| 69 | + pos.getKey().getBlock().setType(pos.getValue()); |
| 70 | + } |
| 71 | + previousBlocks.clear(); |
| 72 | + tsunamiCount=0; |
| 73 | + }, 100); |
| 74 | + } |
| 75 | + public void onDrop(PlayerDropItemEvent e) { |
| 76 | + if (!Test(e.getPlayer(), 5, 0.08, 80)) {return;} |
| 77 | + e.setCancelled(true); |
| 78 | + Vector direction = e.getPlayer().getLocation().getDirection().clone().multiply(.7); |
| 79 | + Location position = e.getPlayer().getEyeLocation().clone(); |
| 80 | + for (int i = 0; i < 26; i++) { |
| 81 | + position.add(direction); |
| 82 | + if (position.getBlock().getType()!=Material.AIR) {break;} |
| 83 | + } |
| 84 | + |
| 85 | + NoWaterFlowPlace = position; |
| 86 | + Bukkit.getScheduler().scheduleSyncDelayedTask(Plugin.instance, ()->{ |
| 87 | + position.add(0, 1, 0); |
| 88 | + SetFlooded(position); |
| 89 | + }, 5); |
| 90 | + Bukkit.getScheduler().scheduleSyncDelayedTask(Plugin.instance, ()->{ |
| 91 | + SetFlooded(position.clone().add(0, 1, 0)); |
| 92 | + }, 10); |
| 93 | + Bukkit.getScheduler().scheduleSyncDelayedTask(Plugin.instance, ()->{ |
| 94 | + SetFlooded(position.clone().add(0, 2, 0)); |
| 95 | + }, 15); |
| 96 | + Bukkit.getScheduler().scheduleSyncDelayedTask(Plugin.instance, ()->{ |
| 97 | + SetFlooded(position.clone().add(0, 3, 0)); |
| 98 | + SetFlooded(position.clone().add(1, 3, 0)); |
| 99 | + SetFlooded(position.clone().add(-1, 3, 0)); |
| 100 | + SetFlooded(position.clone().add(0, 3, 1)); |
| 101 | + SetFlooded(position.clone().add(0, 3, -1)); |
| 102 | + }, 20); |
| 103 | + Bukkit.getScheduler().scheduleSyncDelayedTask(Plugin.instance, ()->{ |
| 104 | + SetFlooded(position.clone().add(0, 4, 0)); |
| 105 | + SetFlooded(position.clone().add(1, 4, 0)); |
| 106 | + SetFlooded(position.clone().add(-1, 4, 0)); |
| 107 | + SetFlooded(position.clone().add(0, 4, 1)); |
| 108 | + SetFlooded(position.clone().add(0, 4, -1)); |
| 109 | + |
| 110 | + SetFlooded(position.clone().add(2, 4, 0)); |
| 111 | + SetFlooded(position.clone().add(-2, 4, 0)); |
| 112 | + SetFlooded(position.clone().add(0, 4, 2)); |
| 113 | + SetFlooded(position.clone().add(0, 4, -2)); |
| 114 | + |
| 115 | + SetFlooded(position.clone().add(-1, 4, -1)); |
| 116 | + SetFlooded(position.clone().add(1, 4, -1)); |
| 117 | + SetFlooded(position.clone().add(-1, 4, 1)); |
| 118 | + SetFlooded(position.clone().add(1, 4, 1)); |
| 119 | + SetFlooded(position.clone().add(-2, 4, -1)); |
| 120 | + }, 25); |
| 121 | + Bukkit.getScheduler().scheduleSyncDelayedTask(Plugin.instance, ()->{ |
| 122 | + SetFlooded(position.clone().add(0, 5, 0)); |
| 123 | + }, 30); |
| 124 | + int Cooldown = 30; |
| 125 | + for (Entry<Location, Material> pos : previousBlocks.entrySet()) { |
| 126 | + Plugin.instance.getLogger().info("a"); |
| 127 | + Bukkit.getScheduler().scheduleSyncDelayedTask(Plugin.instance, ()->{ |
| 128 | + pos.getKey().getBlock().setType(pos.getValue()); |
| 129 | + }, Cooldown); |
| 130 | + Cooldown+=2; |
| 131 | + } |
| 132 | + Bukkit.getScheduler().scheduleSyncDelayedTask(Plugin.instance, ()->{ |
| 133 | + previousBlocks.clear(); |
| 134 | + NoWaterFlowPlace=null; |
| 135 | + }, 75); |
| 136 | + } |
| 137 | + @EventHandler |
| 138 | + void onWaterFlow(BlockFromToEvent e) { |
| 139 | + if (e.getBlock().getType()!=Material.WATER) {return;} |
| 140 | + if (NoWaterFlowPlace==null) {return;} |
| 141 | + double dist = Math.sqrt(Math.pow(e.getBlock().getLocation().getX()-NoWaterFlowPlace.getX(), 2)+Math.pow(e.getBlock().getLocation().getY()-NoWaterFlowPlace.getY(), 2)+Math.pow(e.getBlock().getLocation().getZ()-NoWaterFlowPlace.getZ(), 2)); |
| 142 | + if (dist <= 10) { |
| 143 | + e.setCancelled(true); |
| 144 | + } |
| 145 | + } |
26 | 146 | } |
0 commit comments