package
de.howaner.OITC.util;
import
de.howaner.OITC.OITCPlugin;
import
java.lang.reflect.Field;
import
java.util.ArrayList;
import
java.util.HashMap;
import
java.util.List;
import
java.util.Map;
import
net.minecraft.server.v1_7_R3.DataWatcher;
import
net.minecraft.server.v1_7_R3.EntityPlayer;
import
net.minecraft.server.v1_7_R3.EnumClientCommand;
import
net.minecraft.server.v1_7_R3.Packet;
import
net.minecraft.server.v1_7_R3.PacketPlayInClientCommand;
import
net.minecraft.server.v1_7_R3.PacketPlayOutEntityDestroy;
import
net.minecraft.server.v1_7_R3.PacketPlayOutEntityMetadata;
import
net.minecraft.server.v1_7_R3.PacketPlayOutEntityTeleport;
import
net.minecraft.server.v1_7_R3.PacketPlayOutSpawnEntityLiving;
import
org.bukkit.Bukkit;
import
org.bukkit.Location;
import
org.bukkit.craftbukkit.v1_7_R3.entity.CraftPlayer;
import
org.bukkit.entity.EntityType;
import
org.bukkit.entity.Player;
public
class
StatusBar {
public
static
final
int
ENTITY_ID =
1234
;
public
static
List<String> hasHealthBar =
new
ArrayList<String>();
public
static
Map<String, Integer> schedulers =
new
HashMap<String, Integer>();
public
static
void
setVar(String key, Object value, Object c) {
try
{
Field f = c.getClass().getDeclaredField(key);
f.setAccessible(
true
);
f.set(c, value);
}
catch
(Exception e) {
e.printStackTrace();
}
}
public
static
void
sendPacket(Player player, Packet packet) {
EntityPlayer entityPlayer = ((CraftPlayer) player).getHandle();
entityPlayer.playerConnection.sendPacket(packet);
}
public
static
PacketPlayOutSpawnEntityLiving getMobPacket(String text, Location loc,
int
health) {
PacketPlayOutSpawnEntityLiving mobPacket =
new
PacketPlayOutSpawnEntityLiving();
setVar(
"a"
, (
int
) ENTITY_ID, mobPacket);
setVar(
"b"
, (
byte
) EntityType.ENDER_DRAGON.getTypeId(), mobPacket);
setVar(
"c"
, (
int
) Math.floor(loc.getBlockX() *
32
.0D), mobPacket);
setVar(
"d"
, (
int
) Math.floor(loc.getBlockY() *
32
.0D), mobPacket);
setVar(
"e"
, (
int
) Math.floor(loc.getBlockZ() *
32
.0D), mobPacket);
setVar(
"f"
, (
int
)
0
, mobPacket);
setVar(
"g"
, (
byte
)
0
, mobPacket);
setVar(
"h"
, (
byte
)
0
, mobPacket);
setVar(
"i"
, (
byte
)
0
, mobPacket);
setVar(
"j"
, (
byte
)
0
, mobPacket);
setVar(
"k"
, (
byte
)
0
, mobPacket);
setVar(
"l"
, getWatcher(text, health), mobPacket);
return
mobPacket;
}
public
static
PacketPlayOutEntityDestroy getDestroyEntityPacket() {
PacketPlayOutEntityDestroy packet =
new
PacketPlayOutEntityDestroy();
setVar(
"a"
,
new
int
[]{ENTITY_ID}, packet);
return
packet;
}
public
static
PacketPlayOutEntityMetadata getMetadataPacket(DataWatcher watcher) {
PacketPlayOutEntityMetadata metaPacket =
new
PacketPlayOutEntityMetadata();
setVar(
"a"
, (
int
) ENTITY_ID, metaPacket);
setVar(
"b"
, watcher.c(), metaPacket);
return
metaPacket;
}
public
static
PacketPlayOutEntityTeleport getTeleportPacket(Location loc) {
PacketPlayOutEntityTeleport packet =
new
PacketPlayOutEntityTeleport();
setVar(
"a"
, (
int
) ENTITY_ID, packet);
setVar(
"b"
, (
int
) Math.floor(loc.getX() *
32
.0D), packet);
setVar(
"c"
, (
int
) Math.floor(loc.getY() *
32
.0D), packet);
setVar(
"d"
, (
int
) Math.floor(loc.getZ() *
32
.0D), packet);
setVar(
"e"
, (
byte
) ((
int
) (loc.getYaw() *
256
.0F /
360
.F)), packet);
setVar(
"f"
, (
byte
) ((
int
) (loc.getPitch() *
256
.0F /
360
.0F)), packet);
return
packet;
}
public
static
PacketPlayInClientCommand getRespawnPacket() {
PacketPlayInClientCommand packet =
new
PacketPlayInClientCommand();
setVar(
"a"
, EnumClientCommand.PERFORM_RESPAWN, packet);
return
packet;
}
public
static
DataWatcher getWatcher(String text,
int
health) {
DataWatcher watcher =
new
DataWatcher(
null
);
watcher.a(
0
, (Byte) (
byte
)
0x20
);
watcher.a(
6
, (Float) (
float
) health);
watcher.a(
10
, (String) text);
watcher.a(
11
, (Byte) (
byte
)
1
);
watcher.a(
16
, (Integer) (
int
) health);
return
watcher;
}
public
static
void
displayTextBar(String text,
final
Player player) {
displayTextBar(text, player,
200
, -
1
);
}
public
static
void
displayTextBar(String text,
final
Player player,
int
health,
int
time) {
if
(hasHealthBar.contains(player.getName())) {
if
(schedulers.containsKey(player.getName()))
Bukkit.getScheduler().cancelTask(schedulers.get(player.getName()));
schedulers.remove(player.getName());
sendPacket(player, getMetadataPacket(getWatcher(text, health)));
if
(time != -
1
) {
schedulers.put(player.getName(), Bukkit.getScheduler().scheduleSyncDelayedTask(OITCPlugin.getPlugin(),
new
Runnable() {
@Override
public
void
run(){
PacketPlayOutEntityDestroy destroyEntityPacket = getDestroyEntityPacket();
sendPacket(player, destroyEntityPacket);
hasHealthBar.remove(player.getName());
schedulers.remove(player.getName());
}
}, 20L * time));
}
return
;
}
PacketPlayOutSpawnEntityLiving mobPacket = getMobPacket(text, player.getLocation().clone().add(
0
, -
200
,
0
), health);
sendPacket(player, mobPacket);
hasHealthBar.add(player.getName());
if
(time != -
1
) {
schedulers.put(player.getName(), Bukkit.getScheduler().scheduleSyncDelayedTask(OITCPlugin.getPlugin(),
new
Runnable() {
@Override
public
void
run(){
PacketPlayOutEntityDestroy destroyEntityPacket = getDestroyEntityPacket();
sendPacket(player, destroyEntityPacket);
hasHealthBar.remove(player.getName());
schedulers.remove(player.getName());
}
}, 20L * time));
}
}
}