Search in sources :

Example 1 with CraftItemStack

use of org.bukkit.craftbukkit.v.inventory.CraftItemStack in project Arclight by IzzelAliz.

the class DropperBlockMixin method dispense.

/**
 * @author IzzelAliz
 * @reason
 */
@Overwrite
public void dispense(World worldIn, BlockPos pos) {
    ProxyBlockSource proxyblocksource = new ProxyBlockSource(worldIn, pos);
    DispenserTileEntity dispensertileentity = proxyblocksource.getBlockTileEntity();
    int i = dispensertileentity.getDispenseSlot();
    if (i < 0) {
        worldIn.playEvent(1001, pos, 0);
    } else {
        ItemStack itemstack = dispensertileentity.getStackInSlot(i);
        if (!itemstack.isEmpty() && net.minecraftforge.items.VanillaInventoryCodeHooks.dropperInsertHook(worldIn, pos, dispensertileentity, i, itemstack)) {
            Direction direction = worldIn.getBlockState(pos).get(DispenserBlock.FACING);
            IInventory iinventory = HopperTileEntity.getInventoryAtPosition(worldIn, pos.offset(direction));
            ItemStack itemstack1;
            if (iinventory == null) {
                itemstack1 = DISPENSE_BEHAVIOR.dispense(proxyblocksource, itemstack);
            } else {
                ItemStack split = itemstack.copy().split(1);
                CraftItemStack craftItemStack = CraftItemStack.asCraftMirror(split);
                Inventory destinationInventory;
                // Have to special case large chests as they work oddly
                if (iinventory instanceof DoubleSidedInventory) {
                    destinationInventory = new CraftInventoryDoubleChest((DoubleSidedInventory) iinventory);
                } else {
                    destinationInventory = ((IInventoryBridge) iinventory).getOwner().getInventory();
                }
                InventoryMoveItemEvent event = new InventoryMoveItemEvent(((IInventoryBridge) dispensertileentity).getOwner().getInventory(), craftItemStack, destinationInventory, true);
                Bukkit.getPluginManager().callEvent(event);
                if (event.isCancelled()) {
                    return;
                }
                itemstack1 = HopperTileEntity.putStackInInventoryAllSlots(dispensertileentity, iinventory, CraftItemStack.asNMSCopy(event.getItem()), direction.getOpposite());
                if (event.getItem().equals(craftItemStack) && itemstack1.isEmpty()) {
                    itemstack1 = itemstack.copy();
                    itemstack1.shrink(1);
                } else {
                    itemstack1 = itemstack.copy();
                }
            }
            dispensertileentity.setInventorySlotContents(i, itemstack1);
        }
    }
}
Also used : IInventory(net.minecraft.inventory.IInventory) CraftItemStack(org.bukkit.craftbukkit.v.inventory.CraftItemStack) DoubleSidedInventory(net.minecraft.inventory.DoubleSidedInventory) ProxyBlockSource(net.minecraft.dispenser.ProxyBlockSource) IInventoryBridge(io.izzel.arclight.common.bridge.inventory.IInventoryBridge) DispenserTileEntity(net.minecraft.tileentity.DispenserTileEntity) CraftItemStack(org.bukkit.craftbukkit.v.inventory.CraftItemStack) ItemStack(net.minecraft.item.ItemStack) CraftInventoryDoubleChest(org.bukkit.craftbukkit.v.inventory.CraftInventoryDoubleChest) InventoryMoveItemEvent(org.bukkit.event.inventory.InventoryMoveItemEvent) Direction(net.minecraft.util.Direction) DoubleSidedInventory(net.minecraft.inventory.DoubleSidedInventory) Inventory(org.bukkit.inventory.Inventory) IInventory(net.minecraft.inventory.IInventory) Overwrite(org.spongepowered.asm.mixin.Overwrite)

Example 2 with CraftItemStack

use of org.bukkit.craftbukkit.v.inventory.CraftItemStack in project Arclight by IzzelAliz.

the class LootTableMixin method fillInventory.

// @formatter:on
public void fillInventory(IInventory inv, LootContext context, boolean plugin) {
    List<ItemStack> list = this.generate(context);
    Random random = context.getRandom();
    LootGenerateEvent event = CraftEventFactory.callLootGenerateEvent(inv, (LootTable) (Object) this, context, list, plugin);
    if (event.isCancelled()) {
        return;
    }
    list = event.getLoot().stream().map(CraftItemStack::asNMSCopy).collect(Collectors.toList());
    List<Integer> list1 = this.getEmptySlotsRandomized(inv, random);
    this.shuffleItems(list, list1.size(), random);
    for (ItemStack itemstack : list) {
        if (list1.isEmpty()) {
            LOGGER.warn("Tried to over-fill a container");
            return;
        }
        if (itemstack.isEmpty()) {
            inv.setInventorySlotContents(list1.remove(list1.size() - 1), ItemStack.EMPTY);
        } else {
            inv.setInventorySlotContents(list1.remove(list1.size() - 1), itemstack);
        }
    }
}
Also used : CraftItemStack(org.bukkit.craftbukkit.v.inventory.CraftItemStack) Random(java.util.Random) LootGenerateEvent(org.bukkit.event.world.LootGenerateEvent) CraftItemStack(org.bukkit.craftbukkit.v.inventory.CraftItemStack) ItemStack(net.minecraft.item.ItemStack)

Example 3 with CraftItemStack

use of org.bukkit.craftbukkit.v.inventory.CraftItemStack in project Arclight by IzzelAliz.

the class EnchantmentContainerMixin method onCraftMatrixChanged.

/**
 * @author IzzelAliz
 * @reason
 */
@Overwrite
public void onCraftMatrixChanged(IInventory inventoryIn) {
    if (inventoryIn == this.tableInventory) {
        ItemStack itemstack = inventoryIn.getStackInSlot(0);
        if (!itemstack.isEmpty()) {
            this.worldPosCallable.consume((p_217002_2_, p_217002_3_) -> {
                float power = 0;
                for (int k = -1; k <= 1; ++k) {
                    for (int l = -1; l <= 1; ++l) {
                        if ((k != 0 || l != 0) && p_217002_2_.isAirBlock(p_217002_3_.add(l, 0, k)) && p_217002_2_.isAirBlock(p_217002_3_.add(l, 1, k))) {
                            power += getPower(p_217002_2_, p_217002_3_.add(l * 2, 0, k * 2));
                            power += getPower(p_217002_2_, p_217002_3_.add(l * 2, 1, k * 2));
                            if (l != 0 && k != 0) {
                                power += getPower(p_217002_2_, p_217002_3_.add(l * 2, 0, k));
                                power += getPower(p_217002_2_, p_217002_3_.add(l * 2, 1, k));
                                power += getPower(p_217002_2_, p_217002_3_.add(l, 0, k * 2));
                                power += getPower(p_217002_2_, p_217002_3_.add(l, 1, k * 2));
                            }
                        }
                    }
                }
                this.rand.setSeed(this.xpSeed.get());
                for (int i1 = 0; i1 < 3; ++i1) {
                    this.enchantLevels[i1] = EnchantmentHelper.calcItemStackEnchantability(this.rand, i1, (int) power, itemstack);
                    this.enchantClue[i1] = -1;
                    this.worldClue[i1] = -1;
                    if (this.enchantLevels[i1] < i1 + 1) {
                        this.enchantLevels[i1] = 0;
                    }
                    this.enchantLevels[i1] = ForgeEventFactory.onEnchantmentLevelSet(p_217002_2_, p_217002_3_, i1, (int) power, itemstack, enchantLevels[i1]);
                }
                for (int j1 = 0; j1 < 3; ++j1) {
                    if (this.enchantLevels[j1] > 0) {
                        List<EnchantmentData> list = this.getEnchantmentList(itemstack, j1, this.enchantLevels[j1]);
                        if (list != null && !list.isEmpty()) {
                            EnchantmentData enchantmentdata = list.get(this.rand.nextInt(list.size()));
                            this.enchantClue[j1] = Registry.ENCHANTMENT.getId(enchantmentdata.enchantment);
                            this.worldClue[j1] = enchantmentdata.enchantmentLevel;
                        }
                    }
                }
                CraftItemStack item = CraftItemStack.asCraftMirror(itemstack);
                org.bukkit.enchantments.EnchantmentOffer[] offers = new EnchantmentOffer[3];
                for (int j = 0; j < 3; ++j) {
                    org.bukkit.enchantments.Enchantment enchantment = (this.enchantClue[j] >= 0) ? org.bukkit.enchantments.Enchantment.getByKey(CraftNamespacedKey.fromMinecraft(ForgeRegistries.ENCHANTMENTS.getKey(Registry.ENCHANTMENT.getByValue(this.enchantClue[j])))) : null;
                    offers[j] = (enchantment != null) ? new EnchantmentOffer(enchantment, this.worldClue[j], this.enchantLevels[j]) : null;
                }
                PrepareItemEnchantEvent event = new PrepareItemEnchantEvent(((ServerPlayerEntityBridge) this.playerInventory.player).bridge$getBukkitEntity(), this.getBukkitView(), ((IWorldPosCallableBridge) this.worldPosCallable).bridge$getLocation().getBlock(), item, offers, (int) power);
                event.setCancelled(!itemstack.isEnchantable());
                Bukkit.getPluginManager().callEvent(event);
                if (event.isCancelled()) {
                    for (int j = 0; j < 3; ++j) {
                        this.enchantLevels[j] = 0;
                        this.enchantClue[j] = -1;
                        this.worldClue[j] = -1;
                    }
                    return;
                }
                for (int j = 0; j < 3; j++) {
                    EnchantmentOffer offer = event.getOffers()[j];
                    if (offer != null) {
                        this.enchantLevels[j] = offer.getCost();
                        this.enchantClue[j] = Registry.ENCHANTMENT.getId(ForgeRegistries.ENCHANTMENTS.getValue(CraftNamespacedKey.toMinecraft(offer.getEnchantment().getKey())));
                        this.worldClue[j] = offer.getEnchantmentLevel();
                    } else {
                        this.enchantLevels[j] = 0;
                        this.enchantClue[j] = -1;
                        this.worldClue[j] = -1;
                    }
                }
                this.detectAndSendChanges();
            });
        } else {
            for (int i = 0; i < 3; ++i) {
                this.enchantLevels[i] = 0;
                this.enchantClue[i] = -1;
                this.worldClue[i] = -1;
            }
        }
    }
}
Also used : IWorldPosCallableBridge(io.izzel.arclight.common.bridge.util.IWorldPosCallableBridge) Enchantment(org.bukkit.enchantments.Enchantment) PrepareItemEnchantEvent(org.bukkit.event.enchantment.PrepareItemEnchantEvent) EnchantmentData(net.minecraft.enchantment.EnchantmentData) CraftItemStack(org.bukkit.craftbukkit.v.inventory.CraftItemStack) EnchantmentOffer(org.bukkit.enchantments.EnchantmentOffer) CraftItemStack(org.bukkit.craftbukkit.v.inventory.CraftItemStack) ItemStack(net.minecraft.item.ItemStack) Overwrite(org.spongepowered.asm.mixin.Overwrite)

Example 4 with CraftItemStack

use of org.bukkit.craftbukkit.v.inventory.CraftItemStack in project Arclight by IzzelAliz.

the class BlastingRecipeMixin method bridge$toBukkitRecipe.

@Override
public Recipe bridge$toBukkitRecipe() {
    CraftItemStack result = CraftItemStack.asCraftMirror(this.result);
    CraftBlastingRecipe recipe = new CraftBlastingRecipe(CraftNamespacedKey.fromMinecraft(this.getId()), result, CraftRecipe.toBukkit(this.ingredient), this.experience, this.cookTime);
    recipe.setGroup(this.group);
    return recipe;
}
Also used : CraftItemStack(org.bukkit.craftbukkit.v.inventory.CraftItemStack) CraftBlastingRecipe(org.bukkit.craftbukkit.v.inventory.CraftBlastingRecipe)

Example 5 with CraftItemStack

use of org.bukkit.craftbukkit.v.inventory.CraftItemStack in project Arclight by IzzelAliz.

the class ServerPlayNetHandlerMixin method processPlayerDigging.

/**
 * @author IzzelAliz
 * @reason
 */
@Overwrite
public void processPlayerDigging(CPlayerDiggingPacket packetplayinblockdig) {
    PacketThreadUtil.checkThreadAndEnqueue(packetplayinblockdig, (ServerPlayNetHandler) (Object) this, this.player.getServerWorld());
    if (((ServerPlayerEntityBridge) this.player).bridge$isMovementBlocked()) {
        return;
    }
    BlockPos blockposition = packetplayinblockdig.getPosition();
    this.player.markPlayerActive();
    CPlayerDiggingPacket.Action packetplayinblockdig_enumplayerdigtype = packetplayinblockdig.getAction();
    switch(packetplayinblockdig_enumplayerdigtype) {
        case SWAP_HELD_ITEMS:
            {
                if (!this.player.isSpectator()) {
                    ItemStack itemstack = this.player.getHeldItem(Hand.OFF_HAND);
                    CraftItemStack mainHand = CraftItemStack.asCraftMirror(itemstack);
                    CraftItemStack offHand = CraftItemStack.asCraftMirror(this.player.getHeldItem(Hand.MAIN_HAND));
                    PlayerSwapHandItemsEvent swapItemsEvent = new PlayerSwapHandItemsEvent(this.getPlayer(), mainHand.clone(), offHand.clone());
                    this.server.getPluginManager().callEvent(swapItemsEvent);
                    if (swapItemsEvent.isCancelled()) {
                        return;
                    }
                    if (swapItemsEvent.getOffHandItem().equals(offHand)) {
                        this.player.setHeldItem(Hand.OFF_HAND, this.player.getHeldItem(Hand.MAIN_HAND));
                    } else {
                        this.player.setHeldItem(Hand.OFF_HAND, CraftItemStack.asNMSCopy(swapItemsEvent.getOffHandItem()));
                    }
                    if (swapItemsEvent.getMainHandItem().equals(mainHand)) {
                        this.player.setHeldItem(Hand.MAIN_HAND, itemstack);
                    } else {
                        this.player.setHeldItem(Hand.MAIN_HAND, CraftItemStack.asNMSCopy(swapItemsEvent.getMainHandItem()));
                    }
                }
                return;
            }
        case DROP_ITEM:
            {
                if (!this.player.isSpectator()) {
                    if (this.lastDropTick != ArclightConstants.currentTick) {
                        this.dropCount = 0;
                        this.lastDropTick = ArclightConstants.currentTick;
                    } else {
                        ++this.dropCount;
                        if (this.dropCount >= 20) {
                            LOGGER.warn(this.player.getScoreboardName() + " dropped their items too quickly!");
                            this.disconnect("You dropped your items too quickly (Hacking?)");
                            return;
                        }
                    }
                    this.bridge$dropItems(this.player, false);
                }
                return;
            }
        case DROP_ALL_ITEMS:
            {
                if (!this.player.isSpectator()) {
                    this.bridge$dropItems(this.player, true);
                }
                return;
            }
        case RELEASE_USE_ITEM:
            {
                this.player.stopActiveHand();
                return;
            }
        case START_DESTROY_BLOCK:
        case ABORT_DESTROY_BLOCK:
        case STOP_DESTROY_BLOCK:
            {
                this.player.interactionManager.func_225416_a(blockposition, packetplayinblockdig_enumplayerdigtype, packetplayinblockdig.getFacing(), this.minecraftServer.getBuildLimit());
                return;
            }
        default:
            {
                throw new IllegalArgumentException("Invalid player action");
            }
    }
}
Also used : CraftItemStack(org.bukkit.craftbukkit.v.inventory.CraftItemStack) BlockPos(net.minecraft.util.math.BlockPos) CraftItemStack(org.bukkit.craftbukkit.v.inventory.CraftItemStack) ItemStack(net.minecraft.item.ItemStack) PlayerSwapHandItemsEvent(org.bukkit.event.player.PlayerSwapHandItemsEvent) ServerPlayerEntityBridge(io.izzel.arclight.common.bridge.entity.player.ServerPlayerEntityBridge) Overwrite(org.spongepowered.asm.mixin.Overwrite)

Aggregations

CraftItemStack (org.bukkit.craftbukkit.v.inventory.CraftItemStack)17 ItemStack (net.minecraft.item.ItemStack)8 Overwrite (org.spongepowered.asm.mixin.Overwrite)6 IInventory (net.minecraft.inventory.IInventory)5 IInventoryBridge (io.izzel.arclight.common.bridge.inventory.IInventoryBridge)3 DoubleSidedInventory (net.minecraft.inventory.DoubleSidedInventory)3 BlockPos (net.minecraft.util.math.BlockPos)3 CraftInventoryDoubleChest (org.bukkit.craftbukkit.v.inventory.CraftInventoryDoubleChest)3 InventoryMoveItemEvent (org.bukkit.event.inventory.InventoryMoveItemEvent)3 Inventory (org.bukkit.inventory.Inventory)3 ServerPlayerEntityBridge (io.izzel.arclight.common.bridge.entity.player.ServerPlayerEntityBridge)2 IWorldPosCallableBridge (io.izzel.arclight.common.bridge.util.IWorldPosCallableBridge)2 Random (java.util.Random)2 EnchantmentData (net.minecraft.enchantment.EnchantmentData)2 Ingredient (net.minecraft.item.crafting.Ingredient)2 Bukkit (org.bukkit.Bukkit)2 Final (org.spongepowered.asm.mixin.Final)2 Mixin (org.spongepowered.asm.mixin.Mixin)2 Shadow (org.spongepowered.asm.mixin.Shadow)2 Inject (org.spongepowered.asm.mixin.injection.Inject)2