LibsDisguises/src/me/libraryaddict/disguise/LibsDisguises.java

178 lines
10 KiB
Java
Raw Normal View History

2013-05-17 23:05:19 +02:00
package me.libraryaddict.disguise;
import java.util.List;
import me.libraryaddict.disguise.Commands.DisguiseCommand;
import me.libraryaddict.disguise.Commands.DisguisePlayerCommand;
import me.libraryaddict.disguise.Commands.UndisguiseCommand;
import me.libraryaddict.disguise.Commands.UndisguisePlayerCommand;
2013-05-17 23:05:19 +02:00
import me.libraryaddict.disguise.DisguiseTypes.Disguise;
import me.libraryaddict.disguise.DisguiseTypes.DisguiseType;
2013-05-17 23:05:19 +02:00
import me.libraryaddict.disguise.DisguiseTypes.PlayerDisguise;
2013-07-02 05:29:12 +02:00
import net.minecraft.server.v1_6_R1.WatchableObject;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.entity.Arrow;
import org.bukkit.entity.Entity;
import org.bukkit.entity.ExperienceOrb;
import org.bukkit.entity.Item;
2013-05-17 23:05:19 +02:00
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
2013-05-17 23:05:19 +02:00
import org.bukkit.plugin.java.JavaPlugin;
import com.comphenix.protocol.Packets;
import com.comphenix.protocol.ProtocolLibrary;
import com.comphenix.protocol.ProtocolManager;
2013-05-17 23:05:19 +02:00
import com.comphenix.protocol.events.ConnectionSide;
import com.comphenix.protocol.events.ListenerPriority;
import com.comphenix.protocol.events.PacketAdapter;
import com.comphenix.protocol.events.PacketContainer;
2013-05-17 23:05:19 +02:00
import com.comphenix.protocol.events.PacketEvent;
import com.comphenix.protocol.reflect.StructureModifier;
public class LibsDisguises extends JavaPlugin implements Listener {
private String currentVersion;
2013-07-06 17:43:59 +02:00
private String latestVersion;
private String permission;
2013-06-25 05:15:38 +02:00
private String updateMessage = ChatColor.RED + "[LibsDisguises] " + ChatColor.DARK_RED
+ "There is a update ready to be downloaded! You are using " + ChatColor.RED + "%s" + ChatColor.DARK_RED
+ ", the new version is " + ChatColor.RED + "%s" + ChatColor.DARK_RED + "!";
2013-05-17 23:05:19 +02:00
2013-05-29 00:52:54 +02:00
@Override
2013-05-17 23:05:19 +02:00
public void onEnable() {
if (Bukkit.getPluginManager().getPlugin("ProtocolLib") == null) {
2013-05-29 19:17:48 +02:00
System.out
.print("[LibsDisguises] WARNING! WARNING! LibsDisguises couldn't find ProtocolLib! This plugin depends on it to run!");
System.out
.print("[LibsDisguises] WARNING! WARNING! LibsDisguises couldn't find ProtocolLib! LibsDisguises is now shutting down!");
getPluginLoader().disablePlugin(this);
return;
}
DisguiseAPI.init(this);
DisguiseAPI.enableSounds(true);
final ProtocolManager manager = ProtocolLibrary.getProtocolManager();
manager.addPacketListener(new PacketAdapter(this, ConnectionSide.SERVER_SIDE, ListenerPriority.HIGHEST,
Packets.Server.NAMED_ENTITY_SPAWN, Packets.Server.ENTITY_METADATA, Packets.Server.ARM_ANIMATION,
Packets.Server.REL_ENTITY_MOVE_LOOK, Packets.Server.ENTITY_LOOK, Packets.Server.ENTITY_TELEPORT,
Packets.Server.ADD_EXP_ORB, Packets.Server.VEHICLE_SPAWN, Packets.Server.MOB_SPAWN,
Packets.Server.ENTITY_PAINTING, Packets.Server.COLLECT, 44) {
@Override
public void onPacketSending(PacketEvent event) {
try {
Player observer = event.getPlayer();
StructureModifier<Entity> entityModifer = event.getPacket().getEntityModifier(observer.getWorld());
org.bukkit.entity.Entity entity = entityModifer.read((Packets.Server.COLLECT == event.getPacketID() ? 1 : 0));
if (DisguiseAPI.isDisguised(entity)) {
Disguise disguise = DisguiseAPI.getDisguise(entity);
if (event.getPacketID() == 44) {
if (disguise.getType().isMisc() && entity.getType().isAlive())
event.setCancelled(true);
} else if (event.getPacketID() == Packets.Server.ENTITY_METADATA) {
2013-06-25 03:35:32 +02:00
StructureModifier<Object> mods = event.getPacket().getModifier();
2013-07-06 17:42:25 +02:00
event.setPacket(new PacketContainer(event.getPacketID()));
mods.write(0, mods.read(0));
mods.write(1, disguise.getWatcher().convert((List<WatchableObject>) mods.read(1)));
} else if (event.getPacketID() == Packets.Server.NAMED_ENTITY_SPAWN) {
if (disguise.getType().isPlayer()) {
2013-06-25 03:35:32 +02:00
StructureModifier<Object> mods = event.getPacket().getModifier();
String name = (String) mods.read(1);
if (!name.equals(((PlayerDisguise) disguise).getName())) {
// manager.sendServerPacket(observer, disguise.constructDestroyPacket(entity.getEntityId()));
event.setPacket(disguise.constructPacket(entity));
}
} else {
// manager.sendServerPacket(observer, disguise.constructDestroyPacket(entity.getEntityId()));
event.setPacket(disguise.constructPacket(entity));
}
} else if (event.getPacketID() == Packets.Server.MOB_SPAWN
|| event.getPacketID() == Packets.Server.ADD_EXP_ORB
|| event.getPacketID() == Packets.Server.VEHICLE_SPAWN
|| event.getPacketID() == Packets.Server.ENTITY_PAINTING) {
// manager.sendServerPacket(observer, disguise.constructDestroyPacket(entity.getEntityId()));
event.setPacket(disguise.constructPacket(entity));
} else if (event.getPacketID() == Packets.Server.ARM_ANIMATION
|| event.getPacketID() == Packets.Server.COLLECT) {
if (disguise.getType().isMisc()) {
event.setCancelled(true);
}
} else if (Packets.Server.REL_ENTITY_MOVE_LOOK == event.getPacketID()
|| Packets.Server.ENTITY_LOOK == event.getPacketID()
|| Packets.Server.ENTITY_TELEPORT == event.getPacketID()) {
2013-06-25 03:35:32 +02:00
event.setPacket(event.getPacket().deepClone());
StructureModifier<Object> mods = event.getPacket().getModifier();
if (disguise.getType() == DisguiseType.ENDER_DRAGON) {
byte value = (Byte) mods.read(4);
mods.write(4, (byte) (value - 128));
2013-05-29 22:46:51 +02:00
} else if (disguise.getType().isMisc() || disguise.getType() == DisguiseType.GHAST) {
byte value = (Byte) mods.read(4);
if (disguise.getType() != DisguiseType.PAINTING)
2013-05-29 22:46:51 +02:00
mods.write(4, (byte) (value + 128));
else if (disguise.getType().isMisc())
mods.write(4, (byte) -(value + 128));
else
2013-05-29 22:46:51 +02:00
mods.write(4, (byte) (value - 64));
2013-05-17 23:05:19 +02:00
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
});
manager.addPacketListener(new PacketAdapter(this, ConnectionSide.CLIENT_SIDE, ListenerPriority.NORMAL,
Packets.Client.USE_ENTITY) {
@Override
public void onPacketReceiving(PacketEvent event) {
try {
Player observer = event.getPlayer();
StructureModifier<Entity> entityModifer = event.getPacket().getEntityModifier(observer.getWorld());
org.bukkit.entity.Entity entity = entityModifer.read(1);
if (DisguiseAPI.isDisguised(entity)
&& (entity instanceof ExperienceOrb || entity instanceof Item || entity instanceof Arrow)) {
event.setCancelled(true);
}
} catch (Exception e) {
e.printStackTrace();
}
}
});
getCommand("disguise").setExecutor(new DisguiseCommand());
getCommand("undisguise").setExecutor(new UndisguiseCommand());
getCommand("disguiseplayer").setExecutor(new DisguisePlayerCommand());
getCommand("undisguiseplayer").setExecutor(new UndisguisePlayerCommand());
saveDefaultConfig();
permission = getConfig().getString("Permission");
2013-06-25 03:37:17 +02:00
if (getConfig().getBoolean("NotifyUpdate")) {
currentVersion = getDescription().getVersion();
Bukkit.getScheduler().scheduleAsyncDelayedTask(this, new Runnable() {
public void run() {
try {
UpdateChecker updateChecker = new UpdateChecker();
updateChecker.checkUpdate("v"
+ Bukkit.getPluginManager().getPlugin("LibsDisguises").getDescription().getVersion());
latestVersion = updateChecker.getLatestVersion();
if (latestVersion != null) {
latestVersion = "v" + latestVersion;
for (Player p : Bukkit.getOnlinePlayers())
if (p.hasPermission(permission))
2013-06-25 05:15:38 +02:00
p.sendMessage(String.format(updateMessage, currentVersion, latestVersion));
}
} catch (Exception ex) {
System.out.print(String.format("[LibsDisguises] Failed to check for update: %s", ex.getMessage()));
}
}
});
2013-06-25 03:37:17 +02:00
}
2013-06-01 08:35:31 +02:00
Bukkit.getPluginManager().registerEvents(this, this);
}
@EventHandler
public void onJoin(PlayerJoinEvent event) {
Player p = event.getPlayer();
if (latestVersion != null && p.hasPermission(permission))
2013-06-25 05:15:38 +02:00
p.sendMessage(String.format(updateMessage, currentVersion, latestVersion));
2013-05-17 23:05:19 +02:00
}
}