mirror of
https://github.com/CitizensDev/Citizens2.git
synced 2025-02-25 16:51:50 +01:00
Fix some deprecation warnings, implement EXTRA slot equipping
This commit is contained in:
parent
d9f4047434
commit
298919feee
@ -1,20 +1,20 @@
|
||||
package net.citizensnpcs.editor;
|
||||
|
||||
import net.citizensnpcs.api.npc.NPC;
|
||||
import net.citizensnpcs.api.trait.trait.Equipment;
|
||||
import net.citizensnpcs.api.util.Messaging;
|
||||
import net.citizensnpcs.util.Messages;
|
||||
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.entity.Enderman;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.material.MaterialData;
|
||||
|
||||
import net.citizensnpcs.api.npc.NPC;
|
||||
import net.citizensnpcs.api.trait.trait.Equipment;
|
||||
import net.citizensnpcs.api.util.Messaging;
|
||||
import net.citizensnpcs.util.Messages;
|
||||
|
||||
public class EndermanEquipper implements Equipper {
|
||||
@Override
|
||||
public void equip(Player equipper, NPC npc) {
|
||||
ItemStack hand = equipper.getItemInHand();
|
||||
ItemStack hand = equipper.getInventory().getItemInMainHand();
|
||||
if (!hand.getType().isBlock()) {
|
||||
Messaging.sendErrorTr(equipper, Messages.EQUIPMENT_EDITOR_INVALID_BLOCK);
|
||||
return;
|
||||
@ -35,7 +35,7 @@ public class EndermanEquipper implements Equipper {
|
||||
if (set.getType() != Material.AIR) {
|
||||
set.setAmount(1);
|
||||
hand.setAmount(hand.getAmount() - 1);
|
||||
equipper.setItemInHand(hand);
|
||||
equipper.getInventory().setItemInMainHand(hand);
|
||||
}
|
||||
npc.getTrait(Equipment.class).set(0, set);
|
||||
}
|
||||
|
@ -44,27 +44,22 @@ public class EquipmentEditor extends Editor {
|
||||
|
||||
@EventHandler(ignoreCancelled = true)
|
||||
public void onPlayerChat(final AsyncPlayerChatEvent event) {
|
||||
EquipmentSlot slot = null;
|
||||
if (event.getMessage().equals("helmet")
|
||||
&& event.getPlayer().hasPermission("citizens.npc.edit.equip.any-helmet")) {
|
||||
Bukkit.getScheduler().scheduleSyncDelayedTask(CitizensAPI.getPlugin(), new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
if (!event.getPlayer().isValid())
|
||||
return;
|
||||
ItemStack hand = event.getPlayer().getInventory().getItemInMainHand();
|
||||
if (hand.getType() == Material.AIR || hand.getAmount() <= 0) {
|
||||
return;
|
||||
}
|
||||
npc.getTrait(Equipment.class).set(EquipmentSlot.HELMET,
|
||||
new ItemStack(event.getPlayer().getInventory().getItemInMainHand().getType(), 1));
|
||||
hand.setAmount(hand.getAmount() - 1);
|
||||
event.getPlayer().getInventory().setItemInMainHand(hand);
|
||||
}
|
||||
});
|
||||
event.setCancelled(true);
|
||||
slot = EquipmentSlot.HELMET;
|
||||
}
|
||||
if (event.getMessage().equals("offhand")
|
||||
&& event.getPlayer().hasPermission("citizens.npc.edit.equip.offhand")) {
|
||||
slot = EquipmentSlot.OFF_HAND;
|
||||
}
|
||||
if (event.getMessage().equals("extra") && event.getPlayer().hasPermission("citizens.npc.edit.equip.extra")) {
|
||||
slot = EquipmentSlot.EXTRA;
|
||||
}
|
||||
if (slot == null) {
|
||||
return;
|
||||
}
|
||||
final EquipmentSlot finalSlot = slot;
|
||||
Bukkit.getScheduler().scheduleSyncDelayedTask(CitizensAPI.getPlugin(), new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
@ -74,7 +69,7 @@ public class EquipmentEditor extends Editor {
|
||||
if (hand.getType() == Material.AIR || hand.getAmount() <= 0) {
|
||||
return;
|
||||
}
|
||||
npc.getTrait(Equipment.class).set(EquipmentSlot.OFF_HAND,
|
||||
npc.getTrait(Equipment.class).set(finalSlot,
|
||||
new ItemStack(event.getPlayer().getInventory().getItemInMainHand().getType(), 1));
|
||||
hand.setAmount(hand.getAmount() - 1);
|
||||
event.getPlayer().getInventory().setItemInMainHand(hand);
|
||||
@ -82,7 +77,6 @@ public class EquipmentEditor extends Editor {
|
||||
});
|
||||
event.setCancelled(true);
|
||||
}
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onPlayerInteract(PlayerInteractEvent event) {
|
||||
|
@ -1,19 +1,19 @@
|
||||
package net.citizensnpcs.editor;
|
||||
|
||||
import net.citizensnpcs.api.npc.NPC;
|
||||
import net.citizensnpcs.api.util.Messaging;
|
||||
import net.citizensnpcs.trait.Saddle;
|
||||
import net.citizensnpcs.util.Messages;
|
||||
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.entity.Pig;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
|
||||
import net.citizensnpcs.api.npc.NPC;
|
||||
import net.citizensnpcs.api.util.Messaging;
|
||||
import net.citizensnpcs.trait.Saddle;
|
||||
import net.citizensnpcs.util.Messages;
|
||||
|
||||
public class PigEquipper implements Equipper {
|
||||
@Override
|
||||
public void equip(Player equipper, NPC toEquip) {
|
||||
ItemStack hand = equipper.getItemInHand();
|
||||
ItemStack hand = equipper.getInventory().getItemInMainHand();
|
||||
Pig pig = (Pig) toEquip.getEntity();
|
||||
if (hand.getType() == Material.SADDLE) {
|
||||
if (!pig.hasSaddle()) {
|
||||
@ -26,6 +26,6 @@ public class PigEquipper implements Equipper {
|
||||
toEquip.getTrait(Saddle.class).toggle();
|
||||
Messaging.sendTr(equipper, Messages.SADDLED_STOPPED, toEquip.getName());
|
||||
}
|
||||
equipper.setItemInHand(hand);
|
||||
equipper.getInventory().setItemInMainHand(hand);
|
||||
}
|
||||
}
|
||||
|
@ -1,11 +1,5 @@
|
||||
package net.citizensnpcs.editor;
|
||||
|
||||
import net.citizensnpcs.api.npc.NPC;
|
||||
import net.citizensnpcs.api.util.Messaging;
|
||||
import net.citizensnpcs.trait.SheepTrait;
|
||||
import net.citizensnpcs.trait.WoolColor;
|
||||
import net.citizensnpcs.util.Messages;
|
||||
|
||||
import org.bukkit.DyeColor;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.entity.Player;
|
||||
@ -13,10 +7,16 @@ import org.bukkit.entity.Sheep;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.material.Dye;
|
||||
|
||||
import net.citizensnpcs.api.npc.NPC;
|
||||
import net.citizensnpcs.api.util.Messaging;
|
||||
import net.citizensnpcs.trait.SheepTrait;
|
||||
import net.citizensnpcs.trait.WoolColor;
|
||||
import net.citizensnpcs.util.Messages;
|
||||
|
||||
public class SheepEquipper implements Equipper {
|
||||
@Override
|
||||
public void equip(Player equipper, NPC toEquip) {
|
||||
ItemStack hand = equipper.getItemInHand();
|
||||
ItemStack hand = equipper.getInventory().getItemInMainHand();
|
||||
Sheep sheep = (Sheep) toEquip.getEntity();
|
||||
if (hand.getType() == Material.SHEARS) {
|
||||
Messaging.sendTr(equipper, toEquip.getTrait(SheepTrait.class).toggleSheared() ? Messages.SHEARED_SET
|
||||
@ -35,6 +35,6 @@ public class SheepEquipper implements Equipper {
|
||||
toEquip.getTrait(WoolColor.class).setColor(DyeColor.WHITE);
|
||||
Messaging.sendTr(equipper, Messages.EQUIPMENT_EDITOR_SHEEP_COLOURED, toEquip.getName(), "white");
|
||||
}
|
||||
equipper.setItemInHand(hand);
|
||||
equipper.getInventory().setItemInMainHand(hand);
|
||||
}
|
||||
}
|
||||
|
@ -89,10 +89,6 @@ public class MCTargetStrategy implements PathStrategy, EntityTarget {
|
||||
return aggro;
|
||||
}
|
||||
|
||||
private void setPath() {
|
||||
targetNavigator.setPath();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void stop() {
|
||||
targetNavigator.stop();
|
||||
|
@ -1,5 +1,12 @@
|
||||
package net.citizensnpcs.npc.entity.nonliving;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.craftbukkit.v1_9_R1.CraftServer;
|
||||
import org.bukkit.craftbukkit.v1_9_R1.entity.CraftEntity;
|
||||
import org.bukkit.craftbukkit.v1_9_R1.entity.CraftFish;
|
||||
import org.bukkit.entity.FishHook;
|
||||
import org.bukkit.util.Vector;
|
||||
|
||||
import net.citizensnpcs.api.event.NPCPushEvent;
|
||||
import net.citizensnpcs.api.npc.NPC;
|
||||
import net.citizensnpcs.npc.CitizensNPC;
|
||||
@ -10,21 +17,14 @@ import net.minecraft.server.v1_9_R1.EntityFishingHook;
|
||||
import net.minecraft.server.v1_9_R1.NBTTagCompound;
|
||||
import net.minecraft.server.v1_9_R1.World;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.craftbukkit.v1_9_R1.CraftServer;
|
||||
import org.bukkit.craftbukkit.v1_9_R1.entity.CraftEntity;
|
||||
import org.bukkit.craftbukkit.v1_9_R1.entity.CraftFish;
|
||||
import org.bukkit.entity.Fish;
|
||||
import org.bukkit.util.Vector;
|
||||
|
||||
public class FishingHookController extends MobEntityController {
|
||||
public FishingHookController() {
|
||||
super(EntityFishingHookNPC.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Fish getBukkitEntity() {
|
||||
return (Fish) super.getBukkitEntity();
|
||||
public FishHook getBukkitEntity() {
|
||||
return (FishHook) super.getBukkitEntity();
|
||||
}
|
||||
|
||||
public static class EntityFishingHookNPC extends EntityFishingHook implements NPCHolder {
|
||||
|
@ -12,6 +12,6 @@ public class EmptyNetHandler extends PlayerConnection {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sendPacket(Packet packet) {
|
||||
public void sendPacket(Packet<?> packet) {
|
||||
}
|
||||
}
|
@ -9,14 +9,14 @@ import java.util.WeakHashMap;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.scheduler.BukkitTask;
|
||||
|
||||
import com.google.common.base.Preconditions;
|
||||
import com.google.common.collect.Iterables;
|
||||
import com.mojang.authlib.GameProfile;
|
||||
import com.mojang.authlib.properties.Property;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.scheduler.BukkitTask;
|
||||
|
||||
import net.citizensnpcs.Settings.Setting;
|
||||
import net.citizensnpcs.api.CitizensAPI;
|
||||
import net.citizensnpcs.api.event.DespawnReason;
|
||||
@ -30,6 +30,7 @@ import net.citizensnpcs.npc.profile.ProfileRequest;
|
||||
* Stores data for a single skin.
|
||||
*/
|
||||
public class Skin {
|
||||
private int fetchRetries = -1;
|
||||
private boolean hasFetched;
|
||||
private volatile boolean isValid = true;
|
||||
private final Map<SkinnableEntity, Void> pending = new WeakHashMap<SkinnableEntity, Void>(15);
|
||||
@ -37,7 +38,6 @@ public class Skin {
|
||||
private volatile Property skinData;
|
||||
private volatile UUID skinId;
|
||||
private final String skinName;
|
||||
private int fetchRetries = -1;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
@ -87,8 +87,7 @@ public class Skin {
|
||||
setNPCTexture(entity, localData);
|
||||
|
||||
// check if NPC prefers to use cached skin over the latest skin.
|
||||
if (!entity.getNPC().data().get(NPC.PLAYER_SKIN_USE_LATEST,
|
||||
Setting.NPC_SKIN_USE_LATEST.asBoolean())) {
|
||||
if (!entity.getNPC().data().get(NPC.PLAYER_SKIN_USE_LATEST, Setting.NPC_SKIN_USE_LATEST.asBoolean())) {
|
||||
// cache preferred
|
||||
return true;
|
||||
}
|
||||
@ -97,8 +96,7 @@ public class Skin {
|
||||
if (!hasSkinData()) {
|
||||
if (hasFetched) {
|
||||
return true;
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
pending.put(entity, null);
|
||||
return false;
|
||||
}
|
||||
@ -129,6 +127,52 @@ public class Skin {
|
||||
}
|
||||
}
|
||||
|
||||
private void fetch() {
|
||||
final int maxRetries = Setting.MAX_NPC_SKIN_RETRIES.asInt();
|
||||
if (maxRetries > -1 && fetchRetries >= maxRetries) {
|
||||
if (Messaging.isDebugging()) {
|
||||
Messaging.debug("Reached max skin fetch retries for '" + skinName + "'");
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
ProfileFetcher.fetch(this.skinName, new ProfileFetchHandler() {
|
||||
@Override
|
||||
public void onResult(ProfileRequest request) {
|
||||
hasFetched = true;
|
||||
|
||||
switch (request.getResult()) {
|
||||
case NOT_FOUND:
|
||||
isValid = false;
|
||||
break;
|
||||
case TOO_MANY_REQUESTS:
|
||||
if (maxRetries == 0) {
|
||||
break;
|
||||
}
|
||||
fetchRetries++;
|
||||
long delay = Setting.NPC_SKIN_RETRY_DELAY.asLong();
|
||||
retryTask = Bukkit.getScheduler().runTaskLater(CitizensAPI.getPlugin(), new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
fetch();
|
||||
}
|
||||
}, delay);
|
||||
|
||||
if (Messaging.isDebugging()) {
|
||||
Messaging.debug("Retrying skin fetch for '" + skinName + "' in " + delay + " ticks.");
|
||||
}
|
||||
break;
|
||||
case SUCCESS:
|
||||
GameProfile profile = request.getProfile();
|
||||
setData(profile);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the ID of the player the skin belongs to.
|
||||
*
|
||||
@ -181,49 +225,36 @@ public class Skin {
|
||||
pending.clear();
|
||||
}
|
||||
|
||||
private void fetch() {
|
||||
final int maxRetries = Setting.MAX_NPC_SKIN_RETRIES.asInt();
|
||||
if (maxRetries > -1 && fetchRetries >= maxRetries) {
|
||||
if (Messaging.isDebugging()) {
|
||||
Messaging.debug("Reached max skin fetch retries for '" + skinName + "'");
|
||||
/**
|
||||
* Clear all cached skins.
|
||||
*/
|
||||
public static void clearCache() {
|
||||
synchronized (CACHE) {
|
||||
for (Skin skin : CACHE.values()) {
|
||||
skin.pending.clear();
|
||||
if (skin.retryTask != null) {
|
||||
skin.retryTask.cancel();
|
||||
}
|
||||
}
|
||||
CACHE.clear();
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
ProfileFetcher.fetch(this.skinName, new ProfileFetchHandler() {
|
||||
@Override
|
||||
public void onResult(ProfileRequest request) {
|
||||
/**
|
||||
* Get a skin for a skinnable entity.
|
||||
*
|
||||
* <p>
|
||||
* If a Skin instance does not exist, a new one is created and the skin data is automatically fetched.
|
||||
* </p>
|
||||
*
|
||||
* @param entity
|
||||
* The skinnable entity.
|
||||
*/
|
||||
public static Skin get(SkinnableEntity entity) {
|
||||
Preconditions.checkNotNull(entity);
|
||||
|
||||
hasFetched = true;
|
||||
|
||||
switch (request.getResult()) {
|
||||
case NOT_FOUND:
|
||||
isValid = false;
|
||||
break;
|
||||
case TOO_MANY_REQUESTS:
|
||||
if (maxRetries == 0) {
|
||||
break;
|
||||
}
|
||||
fetchRetries++;
|
||||
long delay = Setting.NPC_SKIN_RETRY_DELAY.asLong();
|
||||
retryTask = Bukkit.getScheduler().runTaskLater(CitizensAPI.getPlugin(), new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
fetch();
|
||||
}
|
||||
}, delay);
|
||||
|
||||
if (Messaging.isDebugging()) {
|
||||
Messaging.debug("Retrying skin fetch for '" + skinName + "' in " + delay + " ticks.");
|
||||
}
|
||||
break;
|
||||
case SUCCESS:
|
||||
GameProfile profile = request.getProfile();
|
||||
setData(profile);
|
||||
break;
|
||||
}
|
||||
}
|
||||
});
|
||||
String skinName = entity.getSkinName().toLowerCase();
|
||||
return get(skinName);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -253,38 +284,6 @@ public class Skin {
|
||||
return skin;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a skin for a skinnable entity.
|
||||
*
|
||||
* <p>
|
||||
* If a Skin instance does not exist, a new one is created and the skin data is automatically fetched.
|
||||
* </p>
|
||||
*
|
||||
* @param entity
|
||||
* The skinnable entity.
|
||||
*/
|
||||
public static Skin get(SkinnableEntity entity) {
|
||||
Preconditions.checkNotNull(entity);
|
||||
|
||||
String skinName = entity.getSkinName().toLowerCase();
|
||||
return get(skinName);
|
||||
}
|
||||
|
||||
/**
|
||||
* Clear all cached skins.
|
||||
*/
|
||||
public static void clearCache() {
|
||||
synchronized (CACHE) {
|
||||
for (Skin skin : CACHE.values()) {
|
||||
skin.pending.clear();
|
||||
if (skin.retryTask != null) {
|
||||
skin.retryTask.cancel();
|
||||
}
|
||||
}
|
||||
CACHE.clear();
|
||||
}
|
||||
}
|
||||
|
||||
private static void setNPCSkinData(SkinnableEntity entity, String skinName, UUID skinId, Property skinProperty) {
|
||||
NPC npc = entity.getNPC();
|
||||
|
||||
@ -308,8 +307,7 @@ public class Skin {
|
||||
// don't set property if already set since this sometimes causes
|
||||
// packet errors that disconnect the client.
|
||||
Property current = Iterables.getFirst(profile.getProperties().get("textures"), null);
|
||||
if (current != null
|
||||
&& current.getValue().equals(skinProperty.getValue())
|
||||
if (current != null && current.getValue().equals(skinProperty.getValue())
|
||||
&& current.getSignature().equals(skinProperty.getSignature())) {
|
||||
return;
|
||||
}
|
||||
|
@ -138,7 +138,7 @@ public class Util {
|
||||
if (parts.contains("*"))
|
||||
return true;
|
||||
for (String part : Splitter.on(',').split(parts)) {
|
||||
if (Material.matchMaterial(part) == player.getItemInHand().getType()) {
|
||||
if (Material.matchMaterial(part) == player.getInventory().getItemInMainHand().getType()) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user