Search in sources :

Example 1 with EnderMailboxInventory

use of lumien.randomthings.handler.EnderLetterHandler.EnderMailboxInventory in project Random-Things by lumien231.

the class BlockEnderMailbox method onBlockActivated.

@Override
public boolean onBlockActivated(World worldIn, BlockPos pos, IBlockState state, EntityPlayer playerIn, EnumHand hand, EnumFacing side, float hitX, float hitY, float hitZ) {
    if (!worldIn.isRemote) {
        ItemStack heldItem = playerIn.getHeldItemMainhand();
        if (playerIn.isSneaking() && heldItem.getItem() == ModItems.enderLetter) {
            NBTTagCompound compound;
            if ((compound = heldItem.getTagCompound()) != null) {
                if (compound.hasKey("receiver") && !compound.getBoolean("received")) {
                    GameProfile playerProfile = worldIn.getMinecraftServer().getPlayerProfileCache().getGameProfileForUsername(compound.getString("receiver"));
                    if (playerProfile != null && playerProfile.getId() != null) {
                        EnderMailboxInventory mailboxInventory = EnderLetterHandler.get(worldIn).getOrCreateInventoryForPlayer(playerProfile.getId());
                        for (int slot = 0; slot < mailboxInventory.getSizeInventory(); slot++) {
                            if (mailboxInventory.getStackInSlot(slot).isEmpty()) {
                                ItemStack sendingLetter = heldItem.copy();
                                heldItem.shrink(1);
                                sendingLetter.getTagCompound().setBoolean("received", true);
                                sendingLetter.getTagCompound().setString("sender", playerIn.getGameProfile().getName());
                                mailboxInventory.setInventorySlotContents(slot, sendingLetter);
                                playerIn.world.playSound(null, pos, SoundEvents.ENTITY_ENDERMEN_TELEPORT, SoundCategory.BLOCKS, 1, 1);
                                return true;
                            }
                        }
                        playerIn.sendMessage(new TextComponentTranslation("item.enderLetter.noSpace").setStyle(new Style().setColor(TextFormatting.DARK_PURPLE)));
                    } else {
                        playerIn.sendMessage(new TextComponentTranslation("item.enderLetter.noPlayer", compound.getString("receiver")).setStyle(new Style().setColor(TextFormatting.DARK_PURPLE)));
                    }
                }
            }
            return true;
        }
        TileEntityEnderMailbox te = (TileEntityEnderMailbox) worldIn.getTileEntity(pos);
        if (te.getOwner() != null) {
            if (te.getOwner().equals(playerIn.getGameProfile().getId())) {
                playerIn.openGui(RandomThings.instance, GuiIds.ENDER_MAILBOX, worldIn, pos.getX(), pos.getY(), pos.getZ());
            } else {
                playerIn.sendMessage(new TextComponentTranslation("block.enderMailbox.owner").setStyle(new Style().setColor(TextFormatting.RED)));
            }
        }
    }
    return true;
}
Also used : TileEntityEnderMailbox(lumien.randomthings.tileentity.TileEntityEnderMailbox) EnderMailboxInventory(lumien.randomthings.handler.EnderLetterHandler.EnderMailboxInventory) TextComponentTranslation(net.minecraft.util.text.TextComponentTranslation) GameProfile(com.mojang.authlib.GameProfile) NBTTagCompound(net.minecraft.nbt.NBTTagCompound) Style(net.minecraft.util.text.Style) ItemStack(net.minecraft.item.ItemStack)

Aggregations

GameProfile (com.mojang.authlib.GameProfile)1 EnderMailboxInventory (lumien.randomthings.handler.EnderLetterHandler.EnderMailboxInventory)1 TileEntityEnderMailbox (lumien.randomthings.tileentity.TileEntityEnderMailbox)1 ItemStack (net.minecraft.item.ItemStack)1 NBTTagCompound (net.minecraft.nbt.NBTTagCompound)1 Style (net.minecraft.util.text.Style)1 TextComponentTranslation (net.minecraft.util.text.TextComponentTranslation)1