use of com.bergerkiller.bukkit.common.nbt.CommonTagCompound in project BKCommonLib by bergerhealer.
the class CommonMapController method handleItemSync.
/**
* Adjusts the internal remapping from UUID to Map Id taking into account the new item
* being synchronized to the player. If the item is that of a virtual map, the map Id
* of the item is updated. NBT data that should not be synchronized is dropped.
*
* @param item
* @param tileX the X-coordinate of the tile in which the item is displayed
* @param tileY the Y-coordinate of the tile in which the item is displayed
* @return True if the item was changed and needs to be updated in the packet
*/
public ItemStack handleItemSync(ItemStack item, int tileX, int tileY) {
if (item == null || item.getType() != Material.MAP) {
return null;
}
// When a map UUID is specified, use that to dynamically allocate a map Id to use
CommonTagCompound tag = ItemUtil.getMetaTag(item, false);
if (tag != null) {
UUID mapUUID = tag.getUUID("mapDisplay");
if (mapUUID != null) {
item = trimExtraData(item);
item.setDurability(getMapId(new MapUUID(mapUUID, tileX, tileY)));
return item;
}
}
// Static map Id MUST be enforced
storeStaticMapId(item.getDurability());
return null;
}
use of com.bergerkiller.bukkit.common.nbt.CommonTagCompound in project BKCommonLib by bergerhealer.
the class CommonMapController method handleMapShowEvent.
private synchronized void handleMapShowEvent(MapShowEvent event) {
// Check if there are other map displays that should be shown to the player automatically
// This uses the 'isGlobal()' property of the display
MapDisplayInfo info = CommonMapController.this.getInfo(event.getMapUUID());
boolean hasDisplay = false;
if (info != null) {
for (MapSession session : info.sessions) {
if (session.display.isGlobal()) {
session.display.addOwner(event.getPlayer());
hasDisplay = true;
break;
}
}
}
// When defined in the NBT of the item, construct the Map Display automatically
CommonTagCompound tag = ItemUtil.getMetaTag(event.getMapItem(), false);
if (tag != null && !hasDisplay) {
String pluginName = tag.getValue("mapDisplayPlugin", String.class);
String displayClassName = tag.getValue("mapDisplayClass", String.class);
if (pluginName != null && displayClassName != null) {
Plugin plugin = Bukkit.getPluginManager().getPlugin(pluginName);
Class<?> displayClass = null;
if (plugin != null) {
try {
displayClass = plugin.getClass().getClassLoader().loadClass(displayClassName);
if (!MapDisplay.class.isAssignableFrom(displayClass)) {
displayClass = null;
}
} catch (ClassNotFoundException e) {
}
}
if (displayClass != null && !event.hasDisplay()) {
try {
MapDisplay display = (MapDisplay) displayClass.newInstance();
event.setDisplay((JavaPlugin) plugin, display);
;
} catch (InstantiationException | IllegalAccessException e) {
e.printStackTrace();
}
}
}
}
CommonUtil.callEvent(event);
}
use of com.bergerkiller.bukkit.common.nbt.CommonTagCompound in project BKCommonLib by bergerhealer.
the class EntityHook method c.
@HookMethod("public boolean savePassenger:???(NBTTagCompound nbttagcompound)")
public boolean c(Object tag) {
Object handle = this.instance();
if (EntityHandle.T.dead.getBoolean(handle)) {
return false;
}
CommonTagCompound commonTag = CommonTagCompound.create(tag);
commonTag.putValue("id", getSavedName());
EntityHandle.T.saveToNBT.invoke(handle, commonTag);
return true;
}
use of com.bergerkiller.bukkit.common.nbt.CommonTagCompound in project BKCommonLib by bergerhealer.
the class EntityHook method d.
@HookMethod("public boolean saveEntity:???(NBTTagCompound nbttagcompound)")
public boolean d(Object tag) {
try {
Object handle = this.instance();
if (EntityHandle.T.dead.getBoolean(handle)) {
return false;
}
if (EntityHandle.T.vehicle.raw.get(handle) != null) {
return false;
}
CommonTagCompound commonTag = CommonTagCompound.create(tag);
commonTag.putValue("id", getSavedName());
EntityHandle.T.saveToNBT.invoke(handle, commonTag);
return true;
} catch (Throwable t) {
t.printStackTrace();
return false;
}
}
use of com.bergerkiller.bukkit.common.nbt.CommonTagCompound in project BKCommonLib by bergerhealer.
the class CommonMapController method trimExtraData.
/**
* Removes all NBT data for map items that is unimportant for clients to know
*
* @param item
* @return new item copy with metadata trimmed
*/
public static ItemStack trimExtraData(ItemStack item) {
// If null, return null. Simples.
if (item == null) {
return null;
}
// If item has no metadata tag, there is no need to clone it
CommonTagCompound oldTag = ItemUtil.getMetaTag(item, false);
if (oldTag == null) {
// If metadata tag is null, that's okay.
if (!CraftItemStackHandle.T.isAssignableFrom(item)) {
throw new IllegalArgumentException("Input item is no CraftItemStack");
}
return item;
}
// Get rid of all custom metadata from the item
// Only Minecraft items are interesting (because its the Minecraft client)
CommonTagCompound newTag = new CommonTagCompound();
final String[] nbt_filter = { "ench", "display", "RepairCost", "AttributeModifiers", "CanDestroy", "CanPlaceOn", "Unbreakable", // This is important to prevent potential corruption
"mapDisplayUUIDMost", "mapDisplayUUIDLeast", "mapDisplayPlugin", "mapDisplayClass" };
for (String filter : nbt_filter) {
if (oldTag.containsKey(filter)) {
newTag.put(filter, oldTag.get(filter));
}
}
item = ItemUtil.cloneItem(item);
ItemUtil.setMetaTag(item, newTag);
return item;
}
Aggregations