summaryrefslogtreewikicommitdiff
path: root/src/main/java/net/tylermurphy/hideAndSeek/game/Disguiser.java
blob: b4f70ad00794e0bb101b577cd7f75f2b70d36ccb (plain)
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
package net.tylermurphy.hideAndSeek.game;

import org.bukkit.Material;
import org.bukkit.entity.FallingBlock;
import org.bukkit.entity.Player;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;

import java.util.HashMap;
import java.util.Map;

public class Disguiser {

    private final Map<Player, FallingBlock> blocks;

    public Disguiser(){
        this.blocks = new HashMap<>();
    }

    public FallingBlock getBlock(Player player){
        return blocks.get(player);
    }

    public boolean contains(FallingBlock block) { return blocks.containsValue(block); }

    public boolean disguised(Player player) { return blocks.containsKey(player); }

    public void check(){
        for(Map.Entry<Player, FallingBlock> set : blocks.entrySet()){
            Player player = set.getKey();
            FallingBlock block = set.getValue();
            if(block.isDead()){
                block.remove();
                FallingBlock replacement = player.getLocation().getWorld().spawnFallingBlock(player.getLocation(), block.getMaterial(), (byte)0);
                replacement.setGravity(false);
                replacement.setDropItem(false);
                blocks.put(player, replacement);
            }
        }
    }

    public void disguise(Player player, Material material){
        if(blocks.containsKey(player)){
            FallingBlock block = blocks.get(player);
            block.remove();
        }
        FallingBlock block = player.getLocation().getWorld().spawnFallingBlock(player.getLocation(), material, (byte)0);
        block.setGravity(false);
        block.setDropItem(false);
        blocks.put(player, block);
        player.addPotionEffect(new PotionEffect(PotionEffectType.INVISIBILITY, 1000000, 0,false, false));
    }

    public void reveal(Player player){
        if(!blocks.containsKey(player)) return;
        FallingBlock block = blocks.get(player);
        block.remove();
        blocks.remove(player);
        player.removePotionEffect(PotionEffectType.INVISIBILITY);
    }

}