TF-Marriage/src/main/java/com/lenis0012/bukkit/marriage2/listeners/KissListener.java

88 lines
3.2 KiB
Java
Raw Normal View History

2015-07-10 15:02:50 +00:00
package com.lenis0012.bukkit.marriage2.listeners;
import com.lenis0012.bukkit.marriage2.MData;
import com.lenis0012.bukkit.marriage2.MPlayer;
2016-02-23 13:35:45 +00:00
import com.lenis0012.bukkit.marriage2.config.Settings;
2015-07-10 15:02:50 +00:00
import com.lenis0012.bukkit.marriage2.internal.MarriageCore;
2016-02-23 13:35:45 +00:00
import com.lenis0012.bukkit.marriage2.misc.Cooldown;
2016-03-06 03:17:13 +00:00
import com.lenis0012.pluginutils.misc.Reflection;
import com.lenis0012.pluginutils.modules.packets.Packet;
import com.lenis0012.pluginutils.modules.packets.PacketModule;
2015-07-10 15:02:50 +00:00
import org.bukkit.Location;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerInteractEntityEvent;
2016-03-01 00:59:03 +00:00
import java.lang.reflect.Method;
2016-02-23 16:22:31 +00:00
import java.util.Random;
2016-02-23 13:35:45 +00:00
import java.util.concurrent.TimeUnit;
2015-07-10 15:02:50 +00:00
public class KissListener implements Listener {
2016-03-01 00:59:03 +00:00
private final Method GET_PARTICLE_BY_ID = Reflection.getNMSMethod("EnumParticle", "a", int.class);
2016-02-23 13:35:45 +00:00
private final Cooldown<String> cooldown;
2015-07-10 15:02:50 +00:00
private final MarriageCore core;
2016-02-23 16:22:31 +00:00
private final Random random = new Random();
2015-07-10 15:02:50 +00:00
public KissListener(MarriageCore core) {
this.core = core;
2016-02-23 13:35:45 +00:00
this.cooldown = new Cooldown<>(Settings.COOLDOWN_KISS.value(), TimeUnit.SECONDS);
2015-07-10 15:02:50 +00:00
}
@EventHandler
public void onPlayerInteract(PlayerInteractEntityEvent event) {
2016-02-23 16:22:31 +00:00
if(!Settings.KISSES_ENABLED.value()) return; // Disabled
2015-07-10 15:02:50 +00:00
final Player player = event.getPlayer();
Entity e = event.getRightClicked();
2016-03-01 00:59:03 +00:00
if(!(e instanceof Player)) {
return;
}
final Player clicked = (Player) e;
if(!player.isSneaking() || !clicked.isSneaking()) {
return;
}
MPlayer mp = core.getMPlayer(player.getUniqueId());
if(!mp.isMarried()) {
return;
}
MData data = mp.getMarriage();
if(!clicked.getUniqueId().toString().equalsIgnoreCase(data.getOtherPlayer(player.getUniqueId()).toString())) {
return;
}
if(!cooldown.performCheck(player.getName()) || !cooldown.performCheck(clicked.getName())) {
return;
}
Location l1 = player.getEyeLocation();
Location l2 = clicked.getEyeLocation();
sendPacket(l1, l2);
}
private void sendPacket(Location eye1, Location eye2) {
Location l = eye1.clone().add((eye2.getX() - eye1.getX()) / 2, (eye2.getY() - eye1.getY()) / 2, (eye2.getZ() - eye1.getZ()) / 2);
int min = Settings.KISSES_AMOUNT_MIN.value();
int max = Settings.KISSES_AMOUNT_MAX.value();
int amount = min + random.nextInt(max - min + 1);
2016-03-06 03:17:13 +00:00
PacketModule module = core.getPlugin().getModule(PacketModule.class);
Packet packet = module.createPacket("PacketPlayOutWorldParticles");
packet.write("a", Reflection.invokeMethod(GET_PARTICLE_BY_ID, null, 34));
packet.write("b", (float) l.getX());
packet.write("c", (float) l.getY());
packet.write("d", (float) l.getZ());
packet.write("e", 0.3F);
packet.write("f", 0.3F);
packet.write("g", 0.3F);
packet.write("h", 1F);
packet.write("i", amount);
module.broadcastPacket(l.getWorld(), packet);
2015-07-10 15:02:50 +00:00
}
}