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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
|
package net.tylermurphy.hideAndSeek.game.util;
import com.comphenix.protocol.PacketType;
import com.comphenix.protocol.ProtocolLibrary;
import com.comphenix.protocol.ProtocolManager;
import com.comphenix.protocol.events.PacketContainer;
import com.comphenix.protocol.wrappers.BlockPosition;
import com.comphenix.protocol.wrappers.WrappedBlockData;
import net.tylermurphy.hideAndSeek.Main;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.entity.*;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;
import org.bukkit.scoreboard.Scoreboard;
import org.bukkit.scoreboard.Team;
import java.lang.reflect.InvocationTargetException;
public class Disguise {
private static final ProtocolManager protocolManager = ProtocolLibrary.getProtocolManager();
final Player hider;
final Material material;
FallingBlock block;
Horse hitBox;
Location solidLocation;
boolean solid, solidify;
static Team hidden;
static {
if(Main.getInstance().supports(9)) {
Scoreboard board = Bukkit.getScoreboardManager().getMainScoreboard();
hidden = board.getTeam("KenshinHideAndSeek_CollisionGroup");
if (hidden == null) {
hidden = board.registerNewTeam("KenshinHideAndSeek_CollisionGroup");
}
hidden.setOption(Team.Option.COLLISION_RULE, Team.OptionStatus.NEVER);
hidden.setCanSeeFriendlyInvisibles(false);
}
}
public Disguise(Player player, Material material){
this.hider = player;
this.material = material;
this.solid = false;
respawnFallingBlock();
player.addPotionEffect(new PotionEffect(PotionEffectType.INVISIBILITY, 1000000, 0,false, false));
if(Main.getInstance().supports(9)) {
hidden.addEntry(player.getName());
} else {
hider.spigot().setCollidesWithEntities(false);
}
}
public void remove(){
if(block != null)
block.remove();
if(hitBox != null){
if(Main.getInstance().supports(9))
hidden.removeEntry(hitBox.getUniqueId().toString());
hitBox.remove();
}
if(solid)
sendBlockUpdate(Material.AIR);
hider.removePotionEffect(PotionEffectType.INVISIBILITY);
if(Main.getInstance().supports(9)) {
hidden.removeEntry(hider.getName());
} else {
hider.spigot().setCollidesWithEntities(true);
}
}
public int getEntityID() {
if(block == null) return -1;
return block.getEntityId();
}
public int getHitBoxID() {
if(hitBox == null) return -1;
return hitBox.getEntityId();
}
public Player getPlayer() {
return hider;
}
public void update(){
if(block == null || block.isDead()){
if(block != null) block.remove();
respawnFallingBlock();
}
if(solidify){
if(!solid) {
solid = true;
solidLocation = hider.getLocation().getBlock().getLocation();
respawnHotbox();
teleportEntity(hitBox, false);
}
sendBlockUpdate(material);
} else if(solid){
solid = false;
if(Main.getInstance().supports(9))
hidden.removeEntry(hitBox.getUniqueId().toString());
hitBox.remove();
hitBox = null;
sendBlockUpdate(Material.AIR);
}
toggleEntityVisibility(block, !solid);
teleportEntity(block, solid);
}
public void setSolidify(boolean value){
this.solidify = value;
}
private void sendBlockUpdate(Material material){
final PacketContainer packet = protocolManager.createPacket(PacketType.Play.Server.BLOCK_CHANGE);
packet.getModifier().writeDefaults();
packet.getBlockPositionModifier().write(0, new BlockPosition(solidLocation.toVector()));
packet.getBlockData().write(0, WrappedBlockData.createData(material));
Bukkit.getOnlinePlayers().forEach(receiver -> {
if(receiver == hider) return;
try {
protocolManager.sendServerPacket(receiver, packet);
} catch (InvocationTargetException ignored) {}
});
}
private void teleportEntity(Entity entity, boolean center) {
final PacketContainer packet = protocolManager.createPacket(PacketType.Play.Server.ENTITY_TELEPORT);
Location location = hider.getLocation();
packet.getModifier().writeDefaults();
packet.getIntegers().write(0, entity.getEntityId());
if(center){
packet.getDoubles().write(0, Math.round(location.getX()+.5)-.5);
packet.getDoubles().write(1, (double)Math.round(location.getY()));
packet.getDoubles().write(2, Math.round(location.getZ()+.5)-.5);
} else {
packet.getDoubles().write(0, location.getX());
packet.getDoubles().write(1, location.getY());
packet.getDoubles().write(2, location.getZ());
}
Bukkit.getOnlinePlayers().forEach(receiver -> {
try {
protocolManager.sendServerPacket(receiver, packet);
} catch (InvocationTargetException ignored) {}
});
}
private void toggleEntityVisibility(Entity entity, boolean show){
if(entity == null) return;
Bukkit.getOnlinePlayers().forEach(receiver -> {
if(receiver == hider) return;
if(show)
Main.getInstance().getEntityHider().showEntity(receiver, entity);
else
Main.getInstance().getEntityHider().hideEntity(receiver, entity);
});
}
private void respawnFallingBlock(){
block = hider.getLocation().getWorld().spawnFallingBlock(hider.getLocation(), material, (byte)0);
block.setGravity(false);
block.setDropItem(false);
block.setInvulnerable(true);
}
private void respawnHotbox(){
hitBox = (Horse) hider.getLocation().getWorld().spawnEntity(hider.getLocation().add(0, 1000, 0), EntityType.HORSE);
hitBox.setAI(false);
hitBox.setGravity(false);
hitBox.setInvulnerable(true);
hitBox.setCanPickupItems(false);
hitBox.setCollidable(false);
hitBox.addPotionEffect(new PotionEffect(PotionEffectType.INVISIBILITY, 1000000, 0,false, false));
if(Main.getInstance().supports(9)){
hidden.addEntry(hitBox.getUniqueId().toString());
}
}
}
|