package net.simpvp.Misc;

import java.util.Random;

import org.bukkit.Sound;
import org.bukkit.Location;

import com.comphenix.protocol.PacketType;
import com.comphenix.protocol.ProtocolLibrary;
import com.comphenix.protocol.events.ListenerPriority;
import com.comphenix.protocol.events.PacketAdapter;
import com.comphenix.protocol.events.PacketContainer;
import com.comphenix.protocol.events.PacketEvent;

public class Thunder {

	public static void add_protocol_listeners() {
		Misc.instance.getLogger().info("Enabling thunder modification");

		ProtocolLibrary.getProtocolManager().addPacketListener(new PacketAdapter(Misc.instance, ListenerPriority.NORMAL, PacketType.Play.Server.NAMED_SOUND_EFFECT) {

			@Override
			public void onPacketSending(PacketEvent event) {

				PacketContainer p = event.getPacket();

				Sound soundType = p.getSoundEffects().read(0);

				switch (soundType) {
					case ENTITY_LIGHTNING_BOLT_THUNDER:
					case EVENT_RAID_HORN:
					case ENTITY_WITHER_SPAWN:
						break;
					default:
						return;
				}

				/* Change the direction of all thunder packets to
				 * a random direction, but keep the distance and height
				 * of the thunder the same, to prevent it from
				 * sound differently. */
				Location playerlocation = event.getPlayer().getLocation();
				double origx = p.getIntegers().read(0) - playerlocation.getBlockX();
				double origz = p.getIntegers().read(2) - playerlocation.getBlockZ();
				double dist_squared = origx * origx + origz * origz;
				double dist;
				if (dist_squared > 1000 * 1000) {
					dist = 1000.0;
				} else {
					dist = Math.sqrt(dist_squared);
				}

				Random rng = new Random();
				double angle = Math.toRadians(rng.nextInt(360));

				int new_x = (int) (Math.cos(angle) * dist + playerlocation.getBlockX());
				int new_z = (int) (Math.sin(angle) * dist + playerlocation.getBlockZ());

				p.getIntegers().write(0, new_x);
				p.getIntegers().write(2, new_z);
			}

		});	
	}

}


@@ -928,16 +928,63 @@
 HUNDER:%0A
+%09%09%09%09%09// Does this actually work for these two?%0A
 %09%09%09%09%09cas
@@ -1271,17 +1271,733 @@
 erently.
- 
+%0A%09%09%09%09 *%0A%09%09%09%09 * Note: While the packet does include absolute%0A%09%09%09%09 * coordinates (multiplied by 8) the vanilla%0A%09%09%09%09 * server normalizes the packet distance to be%0A%09%09%09%09 * within range of each player. The server will%0A%09%09%09%09 * never send packets with absolute coords%0A%09%09%09%09 * matching that of another player. In other%0A%09%09%09%09 * words, the distance should never be more%0A%09%09%09%09 * than a few hundred.%0A%09%09%09%09 *%0A%09%09%09%09 * This does have the unfortunate effect of not%0A%09%09%09%09 * making it viable for us to know if a given%0A%09%09%09%09 * thunder packet was triggered from a%0A%09%09%09%09 * lightning strike nearby or from far away.%0A%09%09%09%09 * Checking that would be desirable, such that%0A%09%09%09%09 * we could completely skip modifying sounds%0A%09%09%09%09 * that have a source nearby.
 */%0A%09%09%09%09L
@@ -2073,16 +2073,17 @@
 origx = 
+(
 p.getInt
@@ -2097,16 +2097,21 @@
 .read(0)
+ / 8)
  - playe
@@ -2152,16 +2152,17 @@
 origz = 
+(
 p.getInt
@@ -2176,16 +2176,21 @@
 .read(2)
+ / 8)
  - playe
@@ -2231,19 +2231,21 @@
 dist
-_squared = 
+ = Math.sqrt(
 orig
@@ -2273,140 +2273,10 @@
 rigz
+)
 ;
-%0A%09%09%09%09double dist;%0A%09%09%09%09if (dist_squared %3E 1000 * 1000) %7B%0A%09%09%09%09%09dist = 1000.0;%0A%09%09%09%09%7D else %7B%0A%09%09%09%09%09dist = Math.sqrt(dist_squared);%0A%09%09%09%09%7D
 %0A%0A%09%09
@@ -2395,32 +2395,33 @@
 os(angle) * dist
+)
  + playerlocatio
@@ -2433,17 +2433,16 @@
 BlockX()
-)
 ;%0A%09%09%09%09in
@@ -2480,16 +2480,17 @@
 ) * dist
+)
  + playe
@@ -2510,17 +2510,48 @@
 BlockZ()
-)
+;%0A%09%09%09%09new_x *= 8;%0A%09%09%09%09new_z *= 8
 ;%0A%0A%09%09%09%09p

Fix distance computation