Search in sources :

Example 1 with CommonTagCompound

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;
}
Also used : CommonTagCompound(com.bergerkiller.bukkit.common.nbt.CommonTagCompound) MapUUID(com.bergerkiller.bukkit.common.map.util.MapUUID) UUID(java.util.UUID) MapUUID(com.bergerkiller.bukkit.common.map.util.MapUUID)

Example 2 with CommonTagCompound

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);
}
Also used : MapDisplay(com.bergerkiller.bukkit.common.map.MapDisplay) CommonTagCompound(com.bergerkiller.bukkit.common.nbt.CommonTagCompound) MapSession(com.bergerkiller.bukkit.common.map.MapSession) Plugin(org.bukkit.plugin.Plugin) JavaPlugin(org.bukkit.plugin.java.JavaPlugin)

Example 3 with CommonTagCompound

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;
}
Also used : CommonTagCompound(com.bergerkiller.bukkit.common.nbt.CommonTagCompound)

Example 4 with CommonTagCompound

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;
    }
}
Also used : CommonTagCompound(com.bergerkiller.bukkit.common.nbt.CommonTagCompound)

Example 5 with CommonTagCompound

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;
}
Also used : CommonTagCompound(com.bergerkiller.bukkit.common.nbt.CommonTagCompound)

Aggregations

CommonTagCompound (com.bergerkiller.bukkit.common.nbt.CommonTagCompound)22 ItemStack (org.bukkit.inventory.ItemStack)4 Test (org.junit.Test)4 IntVector3 (com.bergerkiller.bukkit.common.bases.IntVector3)2 MapUUID (com.bergerkiller.bukkit.common.map.util.MapUUID)2 PacketPlayOutTileEntityDataHandle (com.bergerkiller.generated.net.minecraft.network.protocol.game.PacketPlayOutTileEntityDataHandle)2 UUID (java.util.UUID)2 MapDisplay (com.bergerkiller.bukkit.common.map.MapDisplay)1 MapSession (com.bergerkiller.bukkit.common.map.MapSession)1 CommonTagList (com.bergerkiller.bukkit.common.nbt.CommonTagList)1 BlockStateType (com.bergerkiller.bukkit.common.resources.BlockStateType)1 BlockRenderOptions (com.bergerkiller.bukkit.common.wrappers.BlockRenderOptions)1 BlockStateChange (com.bergerkiller.bukkit.common.wrappers.BlockStateChange)1 ChatText (com.bergerkiller.bukkit.common.wrappers.ChatText)1 ItemRenderOptions (com.bergerkiller.bukkit.common.wrappers.ItemRenderOptions)1 ClientboundLevelChunkPacketDataHandle (com.bergerkiller.generated.net.minecraft.network.protocol.game.ClientboundLevelChunkPacketDataHandle)1 PacketPlayOutMapChunkHandle (com.bergerkiller.generated.net.minecraft.network.protocol.game.PacketPlayOutMapChunkHandle)1 PacketPlayOutUpdateSignHandle (com.bergerkiller.generated.net.minecraft.network.protocol.game.PacketPlayOutUpdateSignHandle)1 File (java.io.File)1 HashSet (java.util.HashSet)1