Search in sources :

Example 1 with TileCraftingPlate

use of com.teamwizardry.wizardry.common.tile.TileCraftingPlate in project Wizardry by TeamWizardry.

the class BlockCraftingPlate method onBlockActivated.

@Override
public boolean onBlockActivated(World worldIn, BlockPos pos, IBlockState state, EntityPlayer playerIn, EnumHand hand, EnumFacing facing, float hitX, float hitY, float hitZ) {
    ItemStack heldItem = playerIn.getHeldItem(hand);
    if (isStructureComplete(worldIn, pos)) {
        TileCraftingPlate plate = getTE(worldIn, pos);
        if (!plate.inputPearl.getHandler().getStackInSlot(0).isEmpty())
            return false;
        if (!heldItem.isEmpty()) {
            if (heldItem.getItem() == ModItems.BOOK && playerIn.isCreative()) {
                ItemStack pearl = new ItemStack(ModItems.PEARL_NACRE);
                NBTTagList spellList = ItemNBTHelper.getList(heldItem, Constants.NBT.SPELL, net.minecraftforge.common.util.Constants.NBT.TAG_COMPOUND);
                if (spellList == null)
                    return false;
                SpellBuilder builder = new SpellBuilder(SpellUtils.getSpellChains(spellList), true, true);
                // Color lastColor = SpellUtils.getAverageSpellColor(builder.getSpell());
                // 
                // float[] hsv = ColorUtils.getHSVFromColor(lastColor);
                // ItemNBTHelper.setFloat(pearl, "hue", hsv[0]);
                // ItemNBTHelper.setFloat(pearl, "saturation", hsv[1]);
                ItemNBTHelper.setFloat(pearl, Constants.NBT.RAND, playerIn.world.rand.nextFloat());
                ItemNBTHelper.setList(pearl, Constants.NBT.SPELL, spellList);
                plate.outputPearl.getHandler().setStackInSlot(0, pearl);
                plate.markDirty();
                PacketHandler.NETWORK.sendToAllAround(new PacketExplode(new Vec3d(pos).addVector(0.5, 0.5, 0.5), Color.CYAN, Color.BLUE, 2, 2, 500, 300, 20, false), new NetworkRegistry.TargetPoint(worldIn.provider.getDimension(), pos.getX(), pos.getY(), pos.getZ(), 256));
                worldIn.playSound(null, pos, ModSounds.BASS_BOOM, SoundCategory.BLOCKS, 1f, (float) RandUtil.nextDouble(1, 1.5));
                return true;
            } else {
                ItemStack stack = heldItem.copy();
                stack.setCount(1);
                heldItem.shrink(1);
                if (!plate.isInventoryEmpty() && stack.getItem() instanceof IInfusable) {
                    plate.inputPearl.getHandler().setStackInSlot(0, stack);
                } else if (!(stack.getItem() instanceof IInfusable)) {
                    for (int i = 0; i < plate.realInventory.getHandler().getSlots(); i++) {
                        if (plate.realInventory.getHandler().getStackInSlot(i).isEmpty()) {
                            plate.realInventory.getHandler().setStackInSlot(i, stack);
                            ClientRunnable.run(new ClientRunnable() {

                                @Override
                                @SideOnly(Side.CLIENT)
                                public void runIfClient() {
                                    if (plate.renderHandler != null)
                                        ((TileCraftingPlateRenderer) plate.renderHandler).addAnimation();
                                }
                            });
                            break;
                        }
                    }
                }
                playerIn.openContainer.detectAndSendChanges();
                worldIn.notifyBlockUpdate(pos, state, state, 3);
                return true;
            }
        } else {
            if (plate.hasOutputPearl()) {
                playerIn.setHeldItem(hand, plate.outputPearl.getHandler().extractItem(0, 1, false));
                playerIn.openContainer.detectAndSendChanges();
                worldIn.notifyBlockUpdate(pos, state, state, 3);
                return true;
            } else {
                boolean empty = true;
                for (int i = 0; i < plate.realInventory.getHandler().getSlots(); i++) {
                    if (!plate.realInventory.getHandler().getStackInSlot(i).isEmpty()) {
                        empty = false;
                        break;
                    }
                }
                if (!empty) {
                    for (int i = plate.realInventory.getHandler().getSlots() - 1; i >= 0; i--) {
                        ItemStack extracted = plate.realInventory.getHandler().getStackInSlot(i);
                        if (!extracted.isEmpty()) {
                            playerIn.addItemStackToInventory(plate.realInventory.getHandler().extractItem(i, extracted.getCount(), false));
                            plate.markDirty();
                            ClientRunnable.run(new ClientRunnable() {

                                @Override
                                @SideOnly(Side.CLIENT)
                                public void runIfClient() {
                                    if (plate.renderHandler != null)
                                        ((TileCraftingPlateRenderer) plate.renderHandler).clearAll();
                                }
                            });
                            break;
                        }
                    }
                }
            }
            return true;
        }
    } else {
        if (playerIn.isCreative() && playerIn.isSneaking()) {
            tickStructure(worldIn, playerIn, pos);
        } else {
            TileCraftingPlate plate = getTE(worldIn, pos);
            plate.revealStructure = !plate.revealStructure;
            plate.markDirty();
        }
    }
    return heldItem.isEmpty();
}
Also used : PacketExplode(com.teamwizardry.wizardry.common.network.PacketExplode) SpellBuilder(com.teamwizardry.wizardry.api.spell.SpellBuilder) IInfusable(com.teamwizardry.wizardry.api.item.IInfusable) SideOnly(net.minecraftforge.fml.relauncher.SideOnly) ClientRunnable(com.teamwizardry.librarianlib.features.utilities.client.ClientRunnable) Vec3d(net.minecraft.util.math.Vec3d) NBTTagList(net.minecraft.nbt.NBTTagList) NetworkRegistry(net.minecraftforge.fml.common.network.NetworkRegistry) TileCraftingPlateRenderer(com.teamwizardry.wizardry.client.render.block.TileCraftingPlateRenderer) ItemStack(net.minecraft.item.ItemStack) TileCraftingPlate(com.teamwizardry.wizardry.common.tile.TileCraftingPlate)

Aggregations

ClientRunnable (com.teamwizardry.librarianlib.features.utilities.client.ClientRunnable)1 IInfusable (com.teamwizardry.wizardry.api.item.IInfusable)1 SpellBuilder (com.teamwizardry.wizardry.api.spell.SpellBuilder)1 TileCraftingPlateRenderer (com.teamwizardry.wizardry.client.render.block.TileCraftingPlateRenderer)1 PacketExplode (com.teamwizardry.wizardry.common.network.PacketExplode)1 TileCraftingPlate (com.teamwizardry.wizardry.common.tile.TileCraftingPlate)1 ItemStack (net.minecraft.item.ItemStack)1 NBTTagList (net.minecraft.nbt.NBTTagList)1 Vec3d (net.minecraft.util.math.Vec3d)1 NetworkRegistry (net.minecraftforge.fml.common.network.NetworkRegistry)1 SideOnly (net.minecraftforge.fml.relauncher.SideOnly)1