-
-
Notifications
You must be signed in to change notification settings - Fork 102
/
FakeBlock.java
148 lines (131 loc) · 5.05 KB
/
FakeBlock.java
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package net.aufdemrand.denizen.utilities.blocks;
import net.aufdemrand.denizen.objects.dLocation;
import net.aufdemrand.denizen.objects.dMaterial;
import net.aufdemrand.denizen.objects.dPlayer;
import net.aufdemrand.denizen.utilities.DenizenAPI;
import net.aufdemrand.denizencore.objects.Duration;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.scheduler.BukkitRunnable;
import org.bukkit.scheduler.BukkitTask;
import java.util.*;
/**
* Creates a temporary fake block and shows it to a dPlayer.
*/
public class FakeBlock {
private final static Map<UUID, Map<dLocation, FakeBlock>> blocks = new HashMap<>();
private final static Map<dLocation, FakeBlock> blocksByLocation = new HashMap<>();
private final dPlayer player;
private final dLocation location;
private dMaterial material;
private long cancelTime = -1;
private BukkitTask currentTask = null;
private FakeBlock(dPlayer player, dLocation location) {
this.player = player;
this.location = location;
}
public static void showFakeBlockTo(List<dPlayer> players, dLocation location, dMaterial material, Duration duration) {
for (dPlayer player : players) {
if (!player.isOnline() || !player.isValid()) {
continue;
}
UUID uuid = player.getPlayerEntity().getUniqueId();
if (!blocks.containsKey(uuid)) {
blocks.put(uuid, new HashMap<>());
}
Map<dLocation, FakeBlock> playerBlocks = blocks.get(uuid);
if (!playerBlocks.containsKey(location)) {
playerBlocks.put(location, new FakeBlock(player, location));
}
playerBlocks.get(location).updateBlock(material, duration.getTicks());
}
}
public static void stopShowingTo(List<dPlayer> players, final dLocation location) {
final List<UUID> uuids = new ArrayList<>();
for (dPlayer player : players) {
if (!player.isOnline() || !player.isValid()) {
continue;
}
UUID uuid = player.getPlayerEntity().getUniqueId();
uuids.add(uuid);
if (blocks.containsKey(uuid)) {
Map<dLocation, FakeBlock> playerBlocks = blocks.get(uuid);
if (playerBlocks.containsKey(location)) {
playerBlocks.get(location).cancelBlock();
}
}
}
new BukkitRunnable() {
@Override
public void run() {
for (UUID uuid : blocks.keySet()) {
if (uuids.contains(uuid)) {
continue;
}
Map<dLocation, FakeBlock> playerBlocks = blocks.get(uuid);
if (playerBlocks.containsKey(location)) {
playerBlocks.get(location).updateBlock();
}
}
}
}.runTaskLater(DenizenAPI.getCurrentInstance(), 2);
}
public static Map<UUID, Map<dLocation, FakeBlock>> getBlocks() {
return blocks;
}
private void cancelBlock() {
if (currentTask != null) {
currentTask.cancel();
currentTask = null;
}
cancelTime = -1;
material = null;
location.getBlockState().update();
blocks.get(player.getOfflinePlayer().getUniqueId()).remove(location);
}
public void updateBlock() {
if (material != null) {
updateBlock(material, cancelTime == -1 ? 0 : cancelTime - location.getWorld().getFullTime());
}
}
private void updateBlock(dMaterial material, long ticks) {
if (currentTask != null) {
currentTask.cancel();
}
this.material = material;
if (!player.hasChunkLoaded(location.getChunk())) {
return;
}
player.getPlayerEntity().sendBlockChange(location, material.getMaterial(),
material.getMaterialData().getData());
if (ticks > 0) {
cancelTime = location.getWorld().getFullTime() + ticks;
currentTask = new BukkitRunnable() {
@Override
public void run() {
currentTask = null;
if (player.isValid() && player.isOnline()) {
cancelBlock();
}
}
}.runTaskLater(DenizenAPI.getCurrentInstance(), ticks);
}
}
static {
final FakeBlockListeners listeners = new FakeBlockListeners();
}
public static class FakeBlockListeners implements Listener {
public FakeBlockListeners() {
DenizenAPI.getCurrentInstance().getServer().getPluginManager()
.registerEvents(this, DenizenAPI.getCurrentInstance());
}
@EventHandler
public void playerQuit(PlayerQuitEvent event) {
UUID uuid = event.getPlayer().getUniqueId();
if (blocks.containsKey(uuid)) {
blocks.remove(uuid);
}
}
}
}