Search in sources :

Example 6 with CommonTagCompound

use of com.bergerkiller.bukkit.common.nbt.CommonTagCompound in project BKCommonLib by bergerhealer.

the class MapDisplayProperties method createNew.

/**
 * Creates map display properties for a unique, new display.
 * A unique ID is generated and the plugin and map display class
 * to display the item to players is registered.<br>
 * <br>
 * To obtain the final map display item, use {@link #getMapItem()}.
 * The map item will automatically initialize the Map Display class
 * when viewed.
 *
 * @param plugin The plugin owner of the display
 * @param mapDisplayClass The map display class to initialize when the item is viewed
 * @return new map display properties
 * @throws IllegalArgumentException If the map display class lacks a no-args constructor
 * @throws UnsupportedOperationException If map displays are disabled in BKCommonLib's configuration
 */
public static MapDisplayProperties createNew(Plugin plugin, Class<? extends MapDisplay> mapDisplayClass) {
    if (!CommonPlugin.getInstance().isMapDisplaysEnabled()) {
        throw new UnsupportedOperationException("Map displays are disabled in BKCommonLib's config.yml!");
    }
    try {
        mapDisplayClass.getConstructor();
    } catch (NoSuchMethodException ex) {
        throw new IllegalArgumentException("The class " + mapDisplayClass.getName() + " does not have an empty constructor. Override onAttached() and use properties instead!");
    }
    ItemStack mapItem = ItemUtil.createItem(CommonMapUUIDStore.FILLED_MAP_TYPE, 1);
    CommonMapUUIDStore.setItemMapId(mapItem, 0);
    CommonTagCompound tag = ItemUtil.getMetaTag(mapItem, true);
    tag.putValue("mapDisplayPlugin", plugin.getName());
    tag.putValue("mapDisplayClass", mapDisplayClass.getName());
    tag.putUUID("mapDisplay", CommonMapUUIDStore.generateDynamicMapUUID());
    return of(mapItem);
}
Also used : CommonTagCompound(com.bergerkiller.bukkit.common.nbt.CommonTagCompound) ItemStack(org.bukkit.inventory.ItemStack)

Example 7 with CommonTagCompound

use of com.bergerkiller.bukkit.common.nbt.CommonTagCompound in project BKCommonLib by bergerhealer.

the class BlockStateChangePacketHandler_1_8_to_1_9_2 method enable.

@Override
public void enable() {
    register(PacketType.OUT_UPDATE_SIGN, (player, commonPacket, listener) -> {
        final PacketPlayOutUpdateSignHandle packet = PacketPlayOutUpdateSignHandle.createHandle(commonPacket.getHandle());
        // Stores decoded String lines that it was before, for easier comparison
        // Only set if the deferred metadata supplier is ever called
        final AtomicReference<String[]> linesBeforeRef = new AtomicReference<>();
        // Initialize the metadata once called
        final DeferredSupplier<CommonTagCompound> metadataSupplier = DeferredSupplier.of(() -> {
            ChatText[] lines = packet.getLines();
            CommonTagCompound metadata = new CommonTagCompound();
            String[] linesBefore = new String[4];
            for (int n = 0; n < 4; n++) {
                metadata.putValue(LINE_META_KEYS[n], linesBefore[n] = lines[n].getJson());
            }
            linesBeforeRef.set(linesBefore);
            return metadata;
        });
        // Errors are handled upstream
        if (!listener.onBlockChange(player, BlockStateChange.deferred(packet.getPosition(), BlockStateType.SIGN, metadataSupplier, () -> true))) {
            return false;
        }
        // Only if getMetadata() was ever even called
        if (metadataSupplier.isInitialized()) {
            // Check if the lines in metadata changed compared to the lines in the packet
            // If they are, write them all back (re-serialize them as chat components)
            // Don't touch lines that weren't modified to retain any original json formatting
            CommonTagCompound metadata = metadataSupplier.get();
            String[] linesBefore = linesBeforeRef.get();
            ChatText[] newLines = packet.getLines().clone();
            boolean changed = false;
            for (int n = 0; n < 4; n++) {
                String newLine = metadata.getValue(LINE_META_KEYS[n], "");
                if (!linesBefore[n].equals(newLine)) {
                    newLines[n] = ChatText.fromJson(newLine);
                    changed = true;
                }
            }
            if (changed) {
                packet.setLines(newLines);
            }
        }
        return true;
    });
}
Also used : PacketPlayOutUpdateSignHandle(com.bergerkiller.generated.net.minecraft.network.protocol.game.PacketPlayOutUpdateSignHandle) ChatText(com.bergerkiller.bukkit.common.wrappers.ChatText) CommonTagCompound(com.bergerkiller.bukkit.common.nbt.CommonTagCompound) AtomicReference(java.util.concurrent.atomic.AtomicReference)

Example 8 with CommonTagCompound

use of com.bergerkiller.bukkit.common.nbt.CommonTagCompound in project BKCommonLib by bergerhealer.

the class EntityHook method onEntitySave.

@HookMethod("public boolean onEntitySave:???(NBTTagCompound nbttagcompound)")
public boolean onEntitySave(Object tag) {
    Object handle = this.instance();
    if (!EntityHandle.T.isSavingAllowed.invoke(handle)) {
        return false;
    }
    CommonTagCompound commonTag = CommonTagCompound.create(tag);
    commonTag.putValue("id", getSavedName(this.instance()));
    EntityHandle.T.saveToNBT.invoke(handle, commonTag);
    return true;
}
Also used : CommonTagCompound(com.bergerkiller.bukkit.common.nbt.CommonTagCompound)

Example 9 with CommonTagCompound

use of com.bergerkiller.bukkit.common.nbt.CommonTagCompound in project BKCommonLib by bergerhealer.

the class ModelInfoLookup method lookupItemRenderOptions.

public static ItemRenderOptions lookupItemRenderOptions(ItemStack item) {
    // Blocks
    Material type = (item == null) ? Material.AIR : item.getType();
    if (item == null || type.isBlock()) {
        BlockRenderOptions blockOpt = BlockData.fromItemStack(item).getDefaultRenderOptions();
        return new ItemRenderOptions(item, blockOpt);
    }
    // Some items, like leather boots, require additional render options passed
    ItemRenderOptions options = new ItemRenderOptions(item, "");
    if (MaterialUtil.ISLEATHERARMOR.get(type)) {
        // Check 'display.color' metadata tag for custom colors
        // default brown
        int color = 5190175;
        CommonTagCompound nbt = ItemUtil.getMetaTag(item, false);
        if (nbt != null) {
            CommonTagCompound display = nbt.getValue("display", CommonTagCompound.class);
            if (display != null) {
                color = display.getValue("color", color);
            }
        }
        // Convert color to hexadecimal and store it as an option
        options.put("layer0tint", String.format("#%06x", color));
    }
    // Similarly, the liquid inside potion bottles have a color set
    if (MaterialUtil.ISPOTION.get(type)) {
        int color = getPotionColor(item.getDurability());
        // Check 'CustomPotionColor' metadata tag for custom colors
        CommonTagCompound nbt = ItemUtil.getMetaTag(item, false);
        if (nbt != null) {
            color = nbt.getValue("CustomPotionColor", color);
        }
        // Convert color to hexadecimal and store it as an option
        options.put("layer0tint", String.format("#%06x", color));
    }
    // damage and damaged properties of weapons, armor and tools
    if (ItemUtil.hasDurability(item)) {
        boolean unbreakable = false;
        CommonTagCompound nbt = ItemUtil.getMetaTag(item, false);
        if (nbt != null) {
            unbreakable = nbt.getValue("Unbreakable", unbreakable);
        }
        options.put("damaged", unbreakable ? "0" : "1");
        options.put("damage", Double.toString((double) item.getDurability() / (double) (ItemUtil.getMaxDurability(item) + 1)));
    }
    return options;
}
Also used : BlockRenderOptions(com.bergerkiller.bukkit.common.wrappers.BlockRenderOptions) ItemRenderOptions(com.bergerkiller.bukkit.common.wrappers.ItemRenderOptions) CommonTagCompound(com.bergerkiller.bukkit.common.nbt.CommonTagCompound) Material(org.bukkit.Material)

Example 10 with CommonTagCompound

use of com.bergerkiller.bukkit.common.nbt.CommonTagCompound in project BKCommonLib by bergerhealer.

the class ItemUtil method getMetaTag.

/**
 * Obtains the CommonTagCompound storing metadata for an item
 *
 * @param stack to get the tag compound for
 * @param create whether to create a new tag if one does not exist
 * @return Tag Compound, or null if none exist and create is false
 */
public static CommonTagCompound getMetaTag(org.bukkit.inventory.ItemStack stack, boolean create) {
    if (CraftItemStackHandle.T.isAssignableFrom(stack)) {
        Object handle = CraftItemStackHandle.T.handle.get(stack);
        if (handle == null) {
            if (create) {
                throw new IllegalArgumentException("Input item is empty and can not have a metadata tag");
            } else {
                return null;
            }
        }
        CommonTagCompound tag = ItemStackHandle.T.tagField.get(handle);
        if (tag == null && create) {
            tag = new CommonTagCompound();
            ItemStackHandle.T.tagField.set(handle, tag);
        }
        return tag;
    } else if (create) {
        throw new IllegalArgumentException("This item is not a CraftItemStack! Please create one using createCraftItem()");
    } else {
        // no tags are stored in Bukkit ItemStacks
        return null;
    }
}
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