Other fixes

This commit is contained in:
filoghost 2014-11-28 14:04:31 +01:00
parent 3f551e9796
commit e40bc511eb
7 changed files with 47 additions and 34 deletions

View File

@ -4,6 +4,8 @@ import static com.gmail.filoghost.holograms.HolographicDisplays.nmsManager;
import java.util.List;
import net.minecraft.util.com.google.common.collect.Lists;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.entity.Entity;
@ -16,7 +18,6 @@ import com.gmail.filoghost.holograms.object.APICraftHologram;
import com.gmail.filoghost.holograms.object.APIFloatingItemManager;
import com.gmail.filoghost.holograms.object.APIHologramManager;
import com.gmail.filoghost.holograms.object.CraftFloatingItem;
import com.gmail.filoghost.holograms.utils.GenericUtils;
import com.gmail.filoghost.holograms.utils.Validator;
import com.gmail.filoghost.holograms.utils.VisibilityManager;
@ -80,7 +81,7 @@ public class HolographicDisplaysAPI {
* @return the new hologram created.
*/
public static Hologram createIndividualHologram(Plugin plugin, Location source, Player whoCanSee, String... lines) {
return createIndividualHologram(plugin, source, GenericUtils.createList(whoCanSee), lines);
return createIndividualHologram(plugin, source, Lists.newArrayList(whoCanSee), lines);
}
/**
@ -131,7 +132,7 @@ public class HolographicDisplaysAPI {
* @return the new hologram created.
*/
public static FloatingItem createIndividualFloatingItem(Plugin plugin, Location source, Player whoCanSee, ItemStack itemstack) {
return createIndividualFloatingItem(plugin, source, GenericUtils.createList(whoCanSee), itemstack);
return createIndividualFloatingItem(plugin, source, Lists.newArrayList(whoCanSee), itemstack);
}
/**

View File

@ -21,6 +21,7 @@ public class CraftCustomItem extends CraftItem {
public void remove() {
// Cannot be removed, this is the most important to override.
}
// Methods from Entity
@Override public void setVelocity(Vector vel) { }
@ -33,6 +34,8 @@ public class CraftCustomItem extends CraftItem {
@Override public boolean eject() { return false; }
@Override public boolean leaveVehicle() { return false; }
@Override public void playEffect(EntityEffect effect) { }
@Override public void setCustomName(String name) { }
@Override public void setCustomNameVisible(boolean flag) { }
// Methods from Item
@Override public void setItemStack(ItemStack stack) { }

View File

@ -24,6 +24,7 @@ public class CraftTouchSlime extends CraftSlime {
// Cannot be removed, this is the most important to override.
}
// Methods from LivingEntity class
@Override public boolean addPotionEffect(PotionEffect effect) { return false; }
@Override public boolean addPotionEffect(PotionEffect effect, boolean param) { return false; }
@ -41,7 +42,8 @@ public class CraftTouchSlime extends CraftSlime {
@Override public boolean eject() { return false; }
@Override public boolean leaveVehicle() { return false; }
@Override public void playEffect(EntityEffect effect) { }
@Override public void setCustomName(String name) { }
@Override public void setCustomNameVisible(boolean flag) { }
// Methods from Slime
@Override public void setSize(int size) { }

View File

@ -115,6 +115,16 @@ public class EntityCustomItem extends EntityItem implements CustomItem, BasicEnt
lockTick = lock;
}
@Override
public void setCustomName(String customName) {
// Locks the custom name.
}
@Override
public void setCustomNameVisible(boolean visible) {
// Locks the custom name.
}
@Override
public void die() {
setLockTick(false);

View File

@ -19,13 +19,22 @@ import com.comphenix.protocol.wrappers.WrappedDataWatcher;
import com.comphenix.protocol.wrappers.WrappedWatchableObject;
import com.gmail.filoghost.holograms.HolographicDisplays;
import com.gmail.filoghost.holograms.object.HologramBase;
import com.gmail.filoghost.holograms.utils.VersionUtils;
public class ProtocolLibHook {
private static int customNameWatcherIndex;
public static void initialize() {
if (Bukkit.getPluginManager().isPluginEnabled("ProtocolLib")) {
HolographicDisplays.getInstance().getLogger().info("Found ProtocolLib, adding support for player relative variables.");
if (HolographicDisplays.is1_8) {
customNameWatcherIndex = 2;
} else {
customNameWatcherIndex = 10;
}
ProtocolLibrary.getProtocolManager().addPacketListener(new PacketAdapter(HolographicDisplays.getInstance(), ListenerPriority.NORMAL, PacketType.Play.Server.SPAWN_ENTITY_LIVING, PacketType.Play.Server.SPAWN_ENTITY, PacketType.Play.Server.ENTITY_METADATA) {
@ -56,18 +65,18 @@ public class ProtocolLibHook {
return;
}
if (entity.getType() != EntityType.HORSE) {
// Enough, only horses are used with custom names.
if (entity.getType() != EntityType.HORSE && !VersionUtils.isArmorstand(entity.getType())) {
// Enough, only horses and armor stands are used with custom names.
return;
}
WrappedDataWatcher dataWatcher = spawnEntityPacket.getMetadata();
String customName = dataWatcher.getString(10);
String customName = dataWatcher.getString(customNameWatcherIndex);
if (customName.contains("{player}") || customName.contains("{displayname}")) {
WrappedDataWatcher dataWatcherClone = dataWatcher.deepClone();
dataWatcherClone.setObject(10, customName.replace("{player}", player.getName()).replace("{displayname}", player.getDisplayName()));
dataWatcherClone.setObject(customNameWatcherIndex, customName.replace("{player}", player.getName()).replace("{displayname}", player.getDisplayName()));
spawnEntityPacket.setMetadata(dataWatcherClone);
event.setPacket(spawnEntityPacket.getHandle());
@ -116,8 +125,8 @@ public class ProtocolLibHook {
return;
}
if (entity.getType() != EntityType.HORSE) {
// Enough, only horses are used with custom names.
if (entity.getType() != EntityType.HORSE && !VersionUtils.isArmorstand(entity.getType())) {
// Enough, only horses and armorstands are used with custom names.
return;
}
@ -125,7 +134,7 @@ public class ProtocolLibHook {
for (int i = 0; i < dataWatcherValues.size(); i++) {
if (dataWatcherValues.get(i).getIndex() == 10) {
if (dataWatcherValues.get(i).getIndex() == customNameWatcherIndex) {
Object customNameObject = dataWatcherValues.get(i).deepClone().getValue();
if (customNameObject instanceof String == false) {
@ -154,18 +163,9 @@ public class ProtocolLibHook {
}
private static boolean isHologramType(EntityType type) {
return type == EntityType.HORSE || type == EntityType.WITHER_SKULL || type == EntityType.DROPPED_ITEM || type == EntityType.SLIME || isArmorstand(type); // To maintain compatibility with 1.8
return type == EntityType.HORSE || type == EntityType.WITHER_SKULL || type == EntityType.DROPPED_ITEM || type == EntityType.SLIME || VersionUtils.isArmorstand(type); // To maintain backwards compatibility
}
private static boolean isArmorstand(EntityType type) {
if (!HolographicDisplays.is1_8) {
return false;
}
return type == EntityType.ARMOR_STAND;
}
// Horses are always part of a CraftHologram
private static HologramBase getHologram(Entity bukkitEntity) {
return nmsManager.getParentHologram(bukkitEntity);
}

View File

@ -1,19 +1,6 @@
package com.gmail.filoghost.holograms.utils;
import java.util.ArrayList;
import java.util.List;
public class GenericUtils {
public static <E> List<E> createList(E object) {
if (object == null) {
return null;
}
List<E> list = new ArrayList<E>();
list.add(object);
return list;
}
public static double square(double d) {
return d * d;

View File

@ -4,6 +4,9 @@ import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.bukkit.Bukkit;
import org.bukkit.entity.EntityType;
import com.gmail.filoghost.holograms.HolographicDisplays;
public class VersionUtils {
@ -30,4 +33,11 @@ public class VersionUtils {
return Bukkit.getName().toLowerCase().contains("mcpc") || Bukkit.getName().toLowerCase().contains("cauldron");
}
public static boolean isArmorstand(EntityType type) {
if (!HolographicDisplays.is1_8) {
return false;
}
return type == EntityType.ARMOR_STAND;
}
}