/
BreakerSystem.java
160 lines (140 loc) · 7.18 KB
/
BreakerSystem.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
148
149
150
151
152
153
154
155
156
157
158
159
160
package io.th0rgal.oraxen.utils.breaker;
import com.comphenix.protocol.PacketType;
import com.comphenix.protocol.ProtocolLibrary;
import com.comphenix.protocol.ProtocolManager;
import com.comphenix.protocol.events.ListenerPriority;
import com.comphenix.protocol.events.PacketAdapter;
import com.comphenix.protocol.events.PacketContainer;
import com.comphenix.protocol.events.PacketEvent;
import com.comphenix.protocol.reflect.StructureModifier;
import com.comphenix.protocol.wrappers.BlockPosition;
import com.comphenix.protocol.wrappers.EnumWrappers;
import io.th0rgal.oraxen.OraxenPlugin;
import io.th0rgal.protectionlib.ProtectionLib;
import org.bukkit.Bukkit;
import org.bukkit.GameMode;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.event.player.PlayerItemDamageEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;
import org.bukkit.scheduler.BukkitScheduler;
import org.bukkit.scheduler.BukkitTask;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
public class BreakerSystem {
public static final List<HardnessModifier> MODIFIERS = new ArrayList<>();
private final Map<Location, BukkitScheduler> breakerPerLocation = new HashMap<>();
private final ProtocolManager protocolManager;
private final PacketAdapter listener = new PacketAdapter(OraxenPlugin.get(),
ListenerPriority.LOW, PacketType.Play.Client.BLOCK_DIG) {
@Override
public void onPacketReceiving(final PacketEvent event) {
final PacketContainer packet = event.getPacket();
final Player player = event.getPlayer();
if (player.getGameMode() == GameMode.CREATIVE)
return;
final ItemStack item = player.getInventory().getItemInMainHand();
final StructureModifier<BlockPosition> dataTemp = packet.getBlockPositionModifier();
final StructureModifier<EnumWrappers.PlayerDigType> data = packet
.getEnumModifier(EnumWrappers.PlayerDigType.class, 2);
EnumWrappers.PlayerDigType type;
try {
type = data.getValues().get(0);
} catch (IllegalArgumentException exception) {
type = EnumWrappers.PlayerDigType.SWAP_HELD_ITEMS;
}
final BlockPosition pos = dataTemp.getValues().get(0);
final World world = player.getWorld();
final Block block = world.getBlockAt(pos.getX(), pos.getY(), pos.getZ());
HardnessModifier triggeredModifier = null;
for (final HardnessModifier modifier : MODIFIERS)
if (modifier.isTriggered(player, block, item)) {
triggeredModifier = modifier;
break;
}
if (triggeredModifier == null)
return;
event.setCancelled(true);
final Location location = block.getLocation();
if (type == EnumWrappers.PlayerDigType.START_DESTROY_BLOCK) {
final long period = triggeredModifier.getPeriod(player, block, item);
Bukkit.getScheduler().runTask(OraxenPlugin.get(), () ->
player.addPotionEffect(new PotionEffect(PotionEffectType.SLOW_DIGGING,
(int) (period * 11),
Integer.MAX_VALUE,
false, false, false)));
if (breakerPerLocation.containsKey(location))
breakerPerLocation.get(location).cancelTasks(OraxenPlugin.get());
final BukkitScheduler scheduler = Bukkit.getScheduler();
breakerPerLocation.put(location, scheduler);
final HardnessModifier modifier = triggeredModifier;
scheduler.runTaskTimer(OraxenPlugin.get(), new Consumer<>() {
int value = 0;
@Override
public void accept(final BukkitTask bukkitTask) {
if (!breakerPerLocation.containsKey(location)) {
bukkitTask.cancel();
return;
}
for (final Entity entity : world.getNearbyEntities(location, 16, 16, 16))
if (entity instanceof Player viewer)
sendBlockBreak(viewer, location, value);
if (value++ < 10)
return;
if (!ProtectionLib.canBreak(player, block.getLocation()))
return;
final BlockBreakEvent blockBreakEvent = new BlockBreakEvent(block, player);
Bukkit.getPluginManager().callEvent(blockBreakEvent);
if (!blockBreakEvent.isCancelled()) {
modifier.breakBlock(player, block, item);
PlayerItemDamageEvent playerItemDamageEvent = new PlayerItemDamageEvent(player,
item, 1);
Bukkit.getPluginManager().callEvent(playerItemDamageEvent);
}
Bukkit.getScheduler().runTask(OraxenPlugin.get(), () ->
player.removePotionEffect(PotionEffectType.SLOW_DIGGING));
breakerPerLocation.remove(location);
for (final Entity entity : world.getNearbyEntities(location, 16, 16, 16))
if (entity instanceof Player viewer)
sendBlockBreak(player, location, 10);
bukkitTask.cancel();
}
}, period, period);
} else {
Bukkit.getScheduler().runTask(OraxenPlugin.get(), () -> {
player.removePotionEffect(PotionEffectType.SLOW_DIGGING);
for (final Entity entity : world.getNearbyEntities(location, 16, 16, 16))
if (entity instanceof Player viewer)
sendBlockBreak(player, location, 10);
});
breakerPerLocation.remove(location);
}
}
};
public BreakerSystem() {
protocolManager = ProtocolLibrary.getProtocolManager();
}
private void sendBlockBreak(final Player player, final Location location, final int stage) {
final PacketContainer fakeAnimation = protocolManager.createPacket(PacketType.Play.Server.BLOCK_BREAK_ANIMATION);
fakeAnimation.getIntegers().write(0, location.hashCode()).write(1, stage);
fakeAnimation.getBlockPositionModifier().write(0, new BlockPosition(location.toVector()));
try {
protocolManager.sendServerPacket(player, fakeAnimation);
} catch (final InvocationTargetException e) {
e.printStackTrace();
}
}
public void registerListener() {
protocolManager.addPacketListener(listener);
}
}