Skip to content

Commit

Permalink
Fix protected NPCs being movable with fishing rods
Browse files Browse the repository at this point in the history
  • Loading branch information
fullwall committed Mar 8, 2016
1 parent 0c5796a commit 392a422
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 21 deletions.
14 changes: 12 additions & 2 deletions src/main/java/net/citizensnpcs/EventListen.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.bukkit.event.entity.EntityDeathEvent;
import org.bukkit.event.entity.EntityTargetEvent;
import org.bukkit.event.player.PlayerChangedWorldEvent;
import org.bukkit.event.player.PlayerFishEvent;
import org.bukkit.event.player.PlayerInteractEntityEvent;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerMoveEvent;
Expand All @@ -37,8 +38,8 @@
import org.bukkit.event.world.ChunkUnloadEvent;
import org.bukkit.event.world.WorldLoadEvent;
import org.bukkit.event.world.WorldUnloadEvent;
import org.bukkit.inventory.meta.SkullMeta;
import org.bukkit.inventory.EquipmentSlot;
import org.bukkit.inventory.meta.SkullMeta;
import org.bukkit.scoreboard.Team;

import com.google.common.base.Predicates;
Expand Down Expand Up @@ -268,6 +269,16 @@ public void onEntityTarget(EntityTargetEvent event) {
Bukkit.getPluginManager().callEvent(new EntityTargetNPCEvent(event, npc));
}

@EventHandler(ignoreCancelled = true)
public void onFishCaught(PlayerFishEvent event) {
if (event.getCaught() == null)
return;
NPC npc = npcRegistry.getNPC(event.getCaught());
if (npc == null)
return;
event.setCancelled(npc.data().get(NPC.DEFAULT_PROTECTED_METADATA, true));
}

@EventHandler
public void onMetaDeserialise(CitizensDeserialiseMetaEvent event) {
if (event.getKey().keyExists("skull")) {
Expand Down Expand Up @@ -381,7 +392,6 @@ public void onPlayerInteractEntity(PlayerInteractEntityEvent event) {
if (npc == null || event.getHand() == EquipmentSlot.OFF_HAND) {
return;
}

Player player = event.getPlayer();
NPCRightClickEvent rightClickEvent = new NPCRightClickEvent(npc, player);
Bukkit.getPluginManager().callEvent(rightClickEvent);
Expand Down
26 changes: 17 additions & 9 deletions src/main/java/net/citizensnpcs/util/NMS.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
import java.lang.reflect.Method;
import java.net.SocketAddress;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Set;
Expand Down Expand Up @@ -607,21 +609,27 @@ public static void replaceTrackerEntry(Player player) {
}
}

public static void sendPacket(Player player, Packet packet) {
public static void sendPacket(Player player, Packet<?> packet) {
if (packet == null)
return;
((EntityPlayer) NMS.getHandle(player)).playerConnection.sendPacket(packet);
}

public static void sendPacketNearby(Player from, Location location, Packet packet) {
NMS.sendPacketsNearby(from, location, Arrays.asList(packet), 64);
public static void sendPacketNearby(Player from, Location location, Packet<?> packet) {
NMS.sendPacketNearby(from, location, packet, 64);
}

public static void sendPacketsNearby(Player from, Location location, Collection<Packet> packets) {
public static void sendPacketNearby(Player from, Location location, Packet<?> packet, double radius) {
List<Packet<?>> list = new ArrayList<Packet<?>>();
list.add(packet);
NMS.sendPacketsNearby(from, location, list, radius);
}

public static void sendPacketsNearby(Player from, Location location, Collection<Packet<?>> packets) {
NMS.sendPacketsNearby(from, location, packets, 64);
}

public static void sendPacketsNearby(Player from, Location location, Collection<Packet> packets, double radius) {
public static void sendPacketsNearby(Player from, Location location, Collection<Packet<?>> packets, double radius) {
radius *= radius;
final org.bukkit.World world = location.getWorld();
for (Player ply : Bukkit.getServer().getOnlinePlayers()) {
Expand All @@ -631,13 +639,13 @@ public static void sendPacketsNearby(Player from, Location location, Collection<
if (location.distanceSquared(ply.getLocation(PACKET_CACHE_LOCATION)) > radius) {
continue;
}
for (Packet packet : packets) {
for (Packet<?> packet : packets) {
sendPacket(ply, packet);
}
}
}

public static void sendPacketsNearby(Player from, Location location, Packet... packets) {
public static void sendPacketsNearby(Player from, Location location, Packet<?>... packets) {
NMS.sendPacketsNearby(from, location, Arrays.asList(packets), 64);
}

Expand Down Expand Up @@ -676,12 +684,12 @@ public static void sendTabListRemove(Player recipient, Player listPlayer) {
new PacketPlayOutPlayerInfo(PacketPlayOutPlayerInfo.EnumPlayerInfoAction.REMOVE_PLAYER, entity));
}

public static void sendToOnline(Packet... packets) {
public static void sendToOnline(Packet<?>... packets) {
Validate.notNull(packets, "packets cannot be null");
for (Player player : Bukkit.getOnlinePlayers()) {
if (player == null || !player.isOnline())
continue;
for (Packet packet : packets) {
for (Packet<?> packet : packets) {
sendPacket(player, packet);
}
}
Expand Down
17 changes: 7 additions & 10 deletions src/main/java/net/citizensnpcs/util/PlayerAnimation.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package net.citizensnpcs.util;

import java.util.Arrays;

import org.bukkit.craftbukkit.v1_9_R1.entity.CraftPlayer;
import org.bukkit.entity.Player;
import org.bukkit.metadata.FixedMetadataValue;
Expand All @@ -23,18 +21,18 @@ protected void playAnimation(EntityPlayer player, int radius) {
playDefaultAnimation(player, radius, 0);
}
},
CRIT {
ARM_SWING_OFFHAND {
@Override
protected void playAnimation(EntityPlayer player, int radius) {
playDefaultAnimation(player, radius, 4);
playDefaultAnimation(player, radius, 3);
}
},
ARM_SWING_OFFHAND {
CRIT {
@Override
protected void playAnimation(EntityPlayer player, int radius) {
playDefaultAnimation(player, radius, 3);
playDefaultAnimation(player, radius, 4);
}
},
},
EAT_FOOD {
@Override
protected void playAnimation(EntityPlayer player, int radius) {
Expand Down Expand Up @@ -149,8 +147,7 @@ protected void playDefaultAnimation(EntityPlayer player, int radius, int code) {
sendPacketNearby(packet, player, radius);
}

protected void sendPacketNearby(Packet packet, EntityPlayer player, int radius) {
NMS.sendPacketsNearby(player.getBukkitEntity(), player.getBukkitEntity().getLocation(), Arrays.asList(packet),
radius);
protected void sendPacketNearby(Packet<?> packet, EntityPlayer player, int radius) {
NMS.sendPacketNearby(player.getBukkitEntity(), player.getBukkitEntity().getLocation(), packet, radius);
}
}

0 comments on commit 392a422

Please sign in to comment.