1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package com.acyrid.SunSteel.listeners;

import com.acyrid.SunSteel.SunSteel;
import com.acyrid.SunSteel.utils.SSConfig;
import com.acyrid.SunSteel.utils.SSMechanics;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerMoveEvent;
import java.util.*;
import java.util.logging.Level;

import static java.lang.Math.abs;

public class SSPlayerListener implements Listener{
    public SSPlayerListener(SunSteel plugin) {
    }
    public static SunSteel plugin;
    private int lastBlock;
    private Map<Player, Set<Block>> revertSet = new HashMap<Player,Set<Block>>();
    private boolean flightToggle = false;
    @EventHandler
    public void onPlayerMove(PlayerMoveEvent event){
        Player player = event.getPlayer();
        if(SSMechanics.hasSSHoverWater(player)||SSMechanics.hasSSHoverLava(player)){
            if(!revertSet.containsKey(player)){
                revertSet.put(player,new HashSet<Block>());
            }
            double originalY;
            Block toBlockLoc = event.getTo().getBlock();
            Block fromBlockLoc = event.getFrom().getBlock();
            if (!fromBlockLoc.equals(toBlockLoc)){
                final int range = 1;
                for (int dx = -range; dx <=range; dx++) {
                    for (int dz = -range; dz <=range; dz++) {
                        Block block = toBlockLoc.getRelative(dx, -1, dz);
                        if(((block.getType() == Material.WATER || block.getType() == Material.STATIONARY_WATER))&&
                                SSMechanics.hasSSHoverWater(player)){
                            if(SSMechanics.getNoCheatInstalled()){
                                plugin.getLogger().log(Level.INFO,"NoCheat installed? " + SSMechanics.getNoCheatInstalled());
                                player.setAllowFlight(true);
                                plugin.getLogger().log(Level.INFO,"Flight Allowed?? " + player.getAllowFlight());
                                flightToggle = true;
                                plugin.getLogger().log(Level.INFO,"Flying? " + player.isFlying());
                            }
                            if(Material.getMaterial(SSMechanics.getWaterWalkBlock()) != null){
                                player.sendBlockChange(block.getLocation() ,Material.getMaterial(SSMechanics.getWaterWalkBlock()),(byte)0 );
                                originalY = player.getLocation().getY();
                                revertSet.get(player).add(block);
                                revertCheck(player, Material.WATER);
                                lastBlock = 1;
                                if(originalY+2 < player.getLocation().getBlockY()){
                                    player.getLocation().setY(originalY);
                                }
                            }
                        }else if(((block.getType() == Material.LAVA || block.getType() == Material.STATIONARY_LAVA))&&
                                SSMechanics.hasSSHoverLava(player)){
                            if(SSMechanics.getNoCheatInstalled()){
                                player.setAllowFlight(true);
                                flightToggle = true;
                            }
                            if(Material.getMaterial(SSMechanics.getLavaWalkBlock()) != null){
                                player.sendBlockChange(block.getLocation() ,Material.getMaterial(SSMechanics.getLavaWalkBlock()),(byte)0 );
                                originalY = player.getLocation().getY();
                                revertSet.get(player).add(block);
                                revertCheck(player, Material.LAVA);
                                lastBlock = 2;
                                if(originalY+2 < player.getLocation().getBlockY()){
                                    player.getLocation().setY(originalY);
                                }
                            }
                        }
                    }
                }
            }
            if(lastBlock == 1){
                revertCheck(player, Material.WATER);
            }else if(lastBlock ==2){
                revertCheck(player, Material.LAVA);
            }
        }
        if(flightToggle){
            player.setAllowFlight(false);
            if(player.isFlying()){
                player.setFlying(false);
            }
        }
    }

    public void revertCheck(Player player, Material material){
        Block playerBlock = player.getLocation().getBlock();
        Iterator<Block> iterator = revertSet.get(player).iterator();
        while(iterator.hasNext()){
            Block nextBlock = iterator.next();
            int xCheck = abs(nextBlock.getX()) - abs(playerBlock.getX());
            int zCheck = abs(nextBlock.getZ()) - abs(playerBlock.getZ());
            if((abs(xCheck) > 3 || abs(zCheck) >3)){
                if(!SSMechanics.getNoCheatInstalled()){
                    player.sendBlockChange(nextBlock.getLocation(), material,(byte)0 );
                    iterator.remove();
                }else if(SSMechanics.getNoCheatInstalled()){
                    nextBlock.setType(material);
                    iterator.remove();
                }
            }
        }
    }
}