Search in sources :

Example 51 with Item

use of net.minecraft.server.v1_12_R1.DataWatcher.Item in project SilkSpawners by timbru31.

the class NMSHandler method setNBTEntityID.

@Override
public ItemStack setNBTEntityID(final ItemStack item, final String entity) {
    if (item == null || StringUtils.isBlank(entity)) {
        Bukkit.getLogger().warning("[SilkSpawners] Skipping invalid spawner to set NBT data on.");
        return null;
    }
    net.minecraft.server.v1_12_R1.ItemStack itemStack = null;
    final CraftItemStack craftStack = CraftItemStack.asCraftCopy(item);
    itemStack = CraftItemStack.asNMSCopy(craftStack);
    NBTTagCompound tag = itemStack.getTag();
    if (tag == null) {
        tag = new NBTTagCompound();
        itemStack.setTag(tag);
    }
    if (!tag.hasKey("SilkSpawners")) {
        tag.set("SilkSpawners", new NBTTagCompound());
    }
    tag.getCompound("SilkSpawners").setString("entity", entity);
    if (!tag.hasKey("BlockEntityTag")) {
        tag.set("BlockEntityTag", new NBTTagCompound());
    }
    tag.getCompound("BlockEntityTag").setString("EntityId", entity);
    if (!tag.hasKey("SpawnData")) {
        tag.set("SpawnData", new NBTTagCompound());
    }
    tag.getCompound("SpawnData").setString("id", entity);
    if (!tag.getCompound("BlockEntityTag").hasKey("SpawnData")) {
        tag.getCompound("BlockEntityTag").set("SpawnData", new NBTTagCompound());
    }
    tag.getCompound("BlockEntityTag").getCompound("SpawnData").setString("id", entity);
    if (!tag.getCompound("BlockEntityTag").hasKey("SpawnPotentials")) {
        tag.getCompound("BlockEntityTag").set("SpawnPotentials", new NBTTagCompound());
    }
    if (!tag.hasKey("EntityTag")) {
        tag.set("EntityTag", new NBTTagCompound());
    }
    String prefixedEntity;
    if (!entity.startsWith("minecraft:")) {
        prefixedEntity = "minecraft:" + entity;
    } else {
        prefixedEntity = entity;
    }
    tag.getCompound("EntityTag").setString("id", prefixedEntity);
    return CraftItemStack.asCraftMirror(itemStack);
}
Also used : CraftItemStack(org.bukkit.craftbukkit.v1_12_R1.inventory.CraftItemStack) NBTTagCompound(net.minecraft.server.v1_12_R1.NBTTagCompound)

Example 52 with Item

use of net.minecraft.server.v1_12_R1.DataWatcher.Item in project WLib by WizardlyBump17.

the class PacketListener method entityMetadata.

@SuppressWarnings("unchecked")
@SneakyThrows
private void entityMetadata(World world, PacketContainer packet, PacketPlayOutEntityMetadata handle) {
    final Entity entity = packet.getEntityModifier(world).read(0);
    if (!(entity instanceof org.bukkit.entity.Item))
        return;
    final ItemStack itemStack = ((org.bukkit.entity.Item) entity).getItemStack().clone();
    if (isInvalid(itemStack))
        return;
    final Field field = handle.getClass().getDeclaredField("b");
    field.setAccessible(true);
    List<DataWatcher.Item<?>> items = new ArrayList<>((List<DataWatcher.Item<?>>) field.get(handle));
    final DataWatcher.Item<?> item;
    int index;
    if (items.size() == 7)
        // new item
        item = items.get(index = 6).d();
    else
        // item merge
        item = items.get(index = 0).d();
    final Field itemField = item.getClass().getDeclaredField("b");
    itemField.setAccessible(true);
    itemField.set(item, CraftItemStack.asNMSCopy(fixItem(itemStack)));
    items.set(index, item);
    field.set(handle, items);
}
Also used : Entity(org.bukkit.entity.Entity) Field(java.lang.reflect.Field) ArrayList(java.util.ArrayList) CraftItemStack(org.bukkit.craftbukkit.v1_12_R1.inventory.CraftItemStack) ItemStack(org.bukkit.inventory.ItemStack) DataWatcher(net.minecraft.server.v1_12_R1.DataWatcher) SneakyThrows(lombok.SneakyThrows)

Example 53 with Item

use of net.minecraft.server.v1_12_R1.DataWatcher.Item in project PaperDev by Kamillaova.

the class CraftInventoryPlayer method setItem.

@Override
public void setItem(int index, ItemStack item) {
    super.setItem(index, item);
    if (this.getHolder() == null)
        return;
    EntityPlayer player = ((CraftPlayer) this.getHolder()).getHandle();
    if (player.playerConnection == null)
        return;
    // subtracting that value from 8.
    if (index < PlayerInventory.getHotbarSize()) {
        index += 36;
    } else if (index > 39) {
        // Off hand
        index += 5;
    } else if (index > 35) {
        index = 8 - (index - 36);
    }
    player.playerConnection.sendPacket(new PacketPlayOutSetSlot(player.defaultContainer.windowId, index, CraftItemStack.asNMSCopy(item)));
}
Also used : EntityPlayer(net.minecraft.server.v1_12_R1.EntityPlayer) CraftPlayer(org.bukkit.craftbukkit.v1_12_R1.entity.CraftPlayer) PacketPlayOutSetSlot(net.minecraft.server.v1_12_R1.PacketPlayOutSetSlot)

Example 54 with Item

use of net.minecraft.server.v1_12_R1.DataWatcher.Item in project PaperDev by Kamillaova.

the class CraftItemStack method getEnchantments.

static Map<Enchantment, Integer> getEnchantments(net.minecraft.server.v1_12_R1.ItemStack item) {
    NBTTagList list = (item != null && item.hasEnchantments()) ? item.getEnchantments() : null;
    if (list == null || list.size() == 0) {
        return ImmutableMap.of();
    }
    ImmutableMap.Builder<Enchantment, Integer> result = ImmutableMap.builder();
    for (int i = 0; i < list.size(); i++) {
        int id = 0xffff & ((NBTTagCompound) list.get(i)).getShort(ENCHANTMENTS_ID.NBT);
        int level = 0xffff & ((NBTTagCompound) list.get(i)).getShort(ENCHANTMENTS_LVL.NBT);
        result.put(Enchantment.getById(id), level);
    }
    return result.build();
}
Also used : NBTTagList(net.minecraft.server.v1_12_R1.NBTTagList) Enchantment(org.bukkit.enchantments.Enchantment) CraftEnchantment(org.bukkit.craftbukkit.v1_12_R1.enchantments.CraftEnchantment) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 55 with Item

use of net.minecraft.server.v1_12_R1.DataWatcher.Item in project PaperDev by Kamillaova.

the class CraftItemFrame method update.

private void update() {
    EntityItemFrame old = this.getHandle();
    WorldServer world = ((CraftWorld) getWorld()).getHandle();
    BlockPosition position = old.getBlockPosition();
    EnumDirection direction = old.getDirection();
    ItemStack item = old.getItem() != null ? old.getItem().cloneItemStack() : null;
    old.die();
    EntityItemFrame frame = new EntityItemFrame(world, position, direction);
    frame.setItem(item);
    world.addEntity(frame);
    this.entity = frame;
}
Also used : EntityItemFrame(net.minecraft.server.v1_12_R1.EntityItemFrame) BlockPosition(net.minecraft.server.v1_12_R1.BlockPosition) WorldServer(net.minecraft.server.v1_12_R1.WorldServer) EnumDirection(net.minecraft.server.v1_12_R1.EnumDirection) CraftItemStack(org.bukkit.craftbukkit.v1_12_R1.inventory.CraftItemStack) ItemStack(net.minecraft.server.v1_12_R1.ItemStack) CraftWorld(org.bukkit.craftbukkit.v1_12_R1.CraftWorld)

Aggregations

CraftItemStack (org.bukkit.craftbukkit.v1_12_R1.inventory.CraftItemStack)30 NBTTagCompound (net.minecraft.server.v1_12_R1.NBTTagCompound)27 ItemStack (org.bukkit.inventory.ItemStack)22 ItemMeta (org.bukkit.inventory.meta.ItemMeta)12 ArrayList (java.util.ArrayList)10 Player (org.bukkit.entity.Player)9 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)8 ChannelOutboundHandlerAdapter (io.netty.channel.ChannelOutboundHandlerAdapter)8 ChannelPromise (io.netty.channel.ChannelPromise)8 PreparedStatement (java.sql.PreparedStatement)7 ItemStack (net.minecraft.server.v1_12_R1.ItemStack)7 ResultSet (java.sql.ResultSet)6 SQLException (java.sql.SQLException)5 SimpleDateFormat (java.text.SimpleDateFormat)5 Date (java.util.Date)5 Item (net.minecraft.server.v1_12_R1.Item)5 NBTTagList (net.minecraft.server.v1_12_R1.NBTTagList)5 Location (org.bukkit.Location)5 OfflinePlayer (org.bukkit.OfflinePlayer)5 PlayerInventory (org.bukkit.inventory.PlayerInventory)5