Last active
August 29, 2015 14:04
-
-
Save Bastian/fce59ebb16160917b6c4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package de.oppermann.bastian.lib.listener; | |
import java.util.ArrayList; | |
import java.util.Arrays; | |
import java.util.Iterator; | |
import java.util.LinkedList; | |
import java.util.List; | |
import java.util.UUID; | |
import net.minecraft.server.v1_7_R4.Packet; | |
import net.minecraft.server.v1_7_R4.PacketPlayOutNamedEntitySpawn; | |
import net.minecraft.server.v1_7_R4.PacketPlayOutSpawnEntity; | |
import net.minecraft.server.v1_7_R4.PacketPlayOutSpawnEntityLiving; | |
import net.minecraft.util.com.mojang.authlib.GameProfile; | |
import org.bukkit.Bukkit; | |
import org.bukkit.event.EventHandler; | |
import org.bukkit.event.Listener; | |
import de.oppermann.bastian.lib.Main; | |
import de.oppermann.bastian.lib.events.AsyncOutgoingPacketEvent; | |
import de.oppermann.bastian.lib.miscellaneous.DisguiseManager; | |
import de.oppermann.bastian.lib.miscellaneous.ReflectionUtil; | |
/** | |
* Intern required class. | |
* <p> | |
* Do not call any method of it! | |
* This class is used for internal purposes and can lead to serious errors if it is used. | |
*/ | |
public class AsyncOutgoingPacketListener implements Listener { | |
private static final ArrayList<Packet> NOT_LISTEN_TO = new ArrayList<>(); | |
@EventHandler | |
public void onOutgoingPacket(final AsyncOutgoingPacketEvent event) { | |
if (NOT_LISTEN_TO.contains(event.getPacket())) { | |
NOT_LISTEN_TO.remove(event.getPacket()); | |
return; | |
} | |
if (event.getPacket() instanceof PacketPlayOutNamedEntitySpawn) { | |
PacketPlayOutNamedEntitySpawn packet = (PacketPlayOutNamedEntitySpawn) event.getPacket(); | |
final int ID = ReflectionUtil.getField(packet, "a"); // a is the entityId | |
final int X = (int) (((double) (int) ReflectionUtil.getField(packet, "c") / 32D)); // c is (x / 32) | |
final int Y = (int) (((double) (int) ReflectionUtil.getField(packet, "d") / 32D)); // d is (y / 32) | |
final int Z = (int) (((double) (int) ReflectionUtil.getField(packet, "e") / 32D)); // e is (z / 32) | |
Bukkit.getScheduler().runTaskLater(Main.getInstance(), new Runnable() { | |
@Override | |
public void run() { | |
DisguiseManager.onEntityAppear(event.getPlayer(), ID, X < 0 ? X - 1 : X, Y, Z < 0 ? Z - 1 : Z); | |
} | |
}, 4); | |
} | |
if (event.getPacket() instanceof PacketPlayOutSpawnEntity) { | |
PacketPlayOutSpawnEntity packet = (PacketPlayOutSpawnEntity) event.getPacket(); | |
final int ID = ReflectionUtil.getField(packet, "a"); // a is the entityId | |
final int X = (int) (((double) (int) ReflectionUtil.getField(packet, "b") / 32D)); // b is (x / 32) | |
final int Y = (int) (((double) (int) ReflectionUtil.getField(packet, "c") / 32D)); // c is (y / 32) | |
final int Z = (int) (((double) (int) ReflectionUtil.getField(packet, "d") / 32D)); // d is (z / 32) | |
Bukkit.getScheduler().runTaskLater(Main.getInstance(), new Runnable() { | |
@Override | |
public void run() { | |
DisguiseManager.onEntityAppear(event.getPlayer(), ID, X < 0 ? X - 1 : X, Y, Z < 0 ? Z - 1 : Z); | |
} | |
}, 4); | |
} | |
if (event.getPacket() instanceof PacketPlayOutSpawnEntityLiving) { | |
PacketPlayOutSpawnEntityLiving packet = (PacketPlayOutSpawnEntityLiving) event.getPacket(); | |
final int ID = ReflectionUtil.getField(packet, "a"); // a is the entityId | |
final int X = (int) (((double) (int) ReflectionUtil.getField(packet, "c") / 32D)); // c is (x / 32) | |
final int Y = (int) (((double) (int) ReflectionUtil.getField(packet, "d") / 32D)); // d is (y / 32) | |
final int Z = (int) (((double) (int) ReflectionUtil.getField(packet, "e") / 32D)); // e is (z / 32) | |
Bukkit.getScheduler().runTaskLater(Main.getInstance(), new Runnable() { | |
@Override | |
public void run() { | |
DisguiseManager.onEntityAppear(event.getPlayer(), ID, X < 0 ? X - 1 : X, Y, Z < 0 ? Z - 1 : Z); | |
} | |
}, 4); | |
} | |
} | |
/** | |
* Intern required method. | |
* <p> | |
* Do not call it! | |
* This method is used for internal purposes and can lead to serious errors if it is called. | |
*/ | |
public static void addNotListenToPacket(Packet packet) { | |
NOT_LISTEN_TO.add(packet); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment